also set SendFlags when updating a camera
[xonotic/xonotic-data.pk3dir.git] / qcsrc / warpzonelib / server.qc
1 .vector warpzone_oldorigin, warpzone_oldvelocity, warpzone_oldangles;
2 .float warpzone_teleport_time;
3
4 void WarpZone_StoreProjectileData(entity e)
5 {
6         e.warpzone_oldorigin = e.origin;
7         e.warpzone_oldvelocity = e.velocity;
8         e.warpzone_oldangles = e.angles;
9 }
10
11 void WarpZone_TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity)
12 {
13         vector from;
14
15         makevectors (to_angles);
16
17         from = player.origin;
18         setorigin (player, to);
19         player.oldorigin = to; // for DP's unsticking
20         player.angles = to_angles;
21         player.fixangle = TRUE;
22         player.velocity = to_velocity;
23
24         if(player.effects & EF_TELEPORT_BIT)
25                 player.effects &~= EF_TELEPORT_BIT;
26         else
27                 player.effects |= EF_TELEPORT_BIT;
28
29         if(player.classname == "player")
30                 player.flags &~= FL_ONGROUND;
31
32         WarpZone_PostTeleportPlayer_Callback(player);
33 }
34
35 float WarpZone_Teleport(entity player)
36 {
37         vector o0, a0, v0, o1, a1, v1;
38
39         o0 = player.origin + player.view_ofs;
40         v0 = player.velocity;
41         a0 = player.angles;
42
43         if(WarpZone_PlaneDist(self, o0) >= 0) // wrong side of the trigger_warpzone
44                 return 2;
45         // no failure, we simply don't want to teleport yet; TODO in
46         // this situation we may want to create a temporary clone
47         // entity of the player to fix graphics glitch
48
49         o1 = WarpZone_TransformOrigin(self, o0);
50         v1 = WarpZone_TransformVelocity(self, v0);
51         if(clienttype(player) != CLIENTTYPE_NOTACLIENT)
52                 a1 = WarpZone_TransformVAngles(self, player.v_angle);
53         else
54                 a1 = WarpZone_TransformAngles(self, a0);
55
56         // put him inside solid
57         tracebox(o1 - player.view_ofs, player.mins, player.maxs, o1 - player.view_ofs, MOVE_NOMONSTERS, player);
58         if(trace_startsolid)
59         {
60                 vector mi, ma;
61                 mi = player.mins;
62                 ma = player.maxs;
63                 setsize(player, mi - player.view_ofs, ma - player.view_ofs);
64                 setorigin(player, o1);
65                 if(WarpZoneLib_MoveOutOfSolid(player))
66                 {
67                         o1 = player.origin;
68                         setsize(player, mi, ma);
69                         setorigin(player, o0);
70                 }
71                 else
72                 {
73                         print("would have to put player in solid, won't do that\n");
74                         setsize(player, mi, ma);
75                         setorigin(player, o0 - player.view_ofs);
76                         return 0; // cannot fix
77                 }
78         }
79
80         if(WarpZone_TargetPlaneDist(self, o1) <= 0)
81         {
82                 print("inconsistent warp zones or evil roundoff error\n");
83                 return 0;
84         }
85
86         //print(sprintf("warpzone: %f %f %f -> %f %f %f\n", o0_x, o0_y, o0_z, o1_x, o1_y, o1_z));
87
88         //o1 = trace_endpos;
89         WarpZone_RefSys_Add(player, self);
90         WarpZone_TeleportPlayer(self, player, o1 - player.view_ofs, a1, v1);
91         WarpZone_StoreProjectileData(player);
92         player.warpzone_teleport_time = time;
93
94         return 1;
95 }
96
97 void WarpZone_Touch (void)
98 {
99         entity oldself, e;
100
101         if(other.classname == "trigger_warpzone")
102                 return;
103
104         // FIXME needs a better check to know what is safe to teleport and what not
105         if(other.movetype == MOVETYPE_NONE)
106                 return;
107
108         if(WarpZoneLib_ExactTrigger_Touch())
109                 return;
110
111         e = self.enemy;
112         if(WarpZone_Teleport(other))
113         {
114                 string save1, save2;
115                 activator = other;
116
117                 save1 = self.target; self.target = string_null;
118                 save2 = self.target3; self.target3 = string_null;
119                 SUB_UseTargets();
120                 if not(self.target) self.target = save1;
121                 if not(self.target3) self.target3 = save2;
122
123                 oldself = self;
124                 self = self.enemy;
125                 save1 = self.target; self.target = string_null;
126                 save2 = self.target2; self.target2 = string_null;
127                 SUB_UseTargets();
128                 if not(self.target) self.target = save1;
129                 if not(self.target2) self.target2 = save2;
130                 self = oldself;
131         }
132         else
133         {
134                 dprint("WARPZONE FAIL AHAHAHAHAH))\n");
135         }
136 }
137
138 float WarpZone_Send(entity to, float sendflags)
139 {
140         float f;
141         WriteByte(MSG_ENTITY, ENT_CLIENT_WARPZONE);
142
143         // we must send this flag for clientside to match properly too
144         f = 0;
145         if(self.warpzone_isboxy)
146                 f |= 1;
147         if(self.warpzone_fadestart)
148                 f |= 2;
149         if(self.origin != '0 0 0')
150                 f |= 4;
151         WriteByte(MSG_ENTITY, f);
152
153         // we need THESE to render the warpzone (and cull properly)...
154         if(f & 4)
155         {
156                 WriteCoord(MSG_ENTITY, self.origin_x);
157                 WriteCoord(MSG_ENTITY, self.origin_y);
158                 WriteCoord(MSG_ENTITY, self.origin_z);
159         }
160
161         WriteShort(MSG_ENTITY, self.modelindex);
162         WriteCoord(MSG_ENTITY, self.mins_x);
163         WriteCoord(MSG_ENTITY, self.mins_y);
164         WriteCoord(MSG_ENTITY, self.mins_z);
165         WriteCoord(MSG_ENTITY, self.maxs_x);
166         WriteCoord(MSG_ENTITY, self.maxs_y);
167         WriteCoord(MSG_ENTITY, self.maxs_z);
168         WriteByte(MSG_ENTITY, bound(1, self.scale * 16, 255));
169
170         // we need THESE to calculate the proper transform
171         WriteCoord(MSG_ENTITY, self.warpzone_origin_x);
172         WriteCoord(MSG_ENTITY, self.warpzone_origin_y);
173         WriteCoord(MSG_ENTITY, self.warpzone_origin_z);
174         WriteCoord(MSG_ENTITY, self.warpzone_angles_x);
175         WriteCoord(MSG_ENTITY, self.warpzone_angles_y);
176         WriteCoord(MSG_ENTITY, self.warpzone_angles_z);
177         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin_x);
178         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin_y);
179         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin_z);
180         WriteCoord(MSG_ENTITY, self.warpzone_targetangles_x);
181         WriteCoord(MSG_ENTITY, self.warpzone_targetangles_y);
182         WriteCoord(MSG_ENTITY, self.warpzone_targetangles_z);
183
184         if(f & 2)
185         {
186                 WriteShort(MSG_ENTITY, self.warpzone_fadestart);
187                 WriteShort(MSG_ENTITY, self.warpzone_fadeend);
188         }
189
190         return TRUE;
191 }
192
193 float WarpZone_Camera_Send(entity to, float sendflags)
194 {
195         float f;
196         WriteByte(MSG_ENTITY, ENT_CLIENT_WARPZONE_CAMERA);
197
198         if(self.warpzone_fadestart)
199                 f |= 2;
200         if(self.origin != '0 0 0')
201                 f |= 4;
202         WriteByte(MSG_ENTITY, f);
203
204         // we need THESE to render the warpzone (and cull properly)...
205         if(f & 4)
206         {
207                 WriteCoord(MSG_ENTITY, self.origin_x);
208                 WriteCoord(MSG_ENTITY, self.origin_y);
209                 WriteCoord(MSG_ENTITY, self.origin_z);
210         }
211
212         WriteShort(MSG_ENTITY, self.modelindex);
213         WriteCoord(MSG_ENTITY, self.mins_x);
214         WriteCoord(MSG_ENTITY, self.mins_y);
215         WriteCoord(MSG_ENTITY, self.mins_z);
216         WriteCoord(MSG_ENTITY, self.maxs_x);
217         WriteCoord(MSG_ENTITY, self.maxs_y);
218         WriteCoord(MSG_ENTITY, self.maxs_z);
219         WriteByte(MSG_ENTITY, bound(1, self.scale * 16, 255));
220
221         // we need THESE to calculate the proper transform
222         WriteCoord(MSG_ENTITY, self.enemy.origin_x);
223         WriteCoord(MSG_ENTITY, self.enemy.origin_y);
224         WriteCoord(MSG_ENTITY, self.enemy.origin_z);
225         WriteCoord(MSG_ENTITY, self.enemy.angles_x);
226         WriteCoord(MSG_ENTITY, self.enemy.angles_y);
227         WriteCoord(MSG_ENTITY, self.enemy.angles_z);
228
229         if(f & 2)
230         {
231                 WriteShort(MSG_ENTITY, self.warpzone_fadestart);
232                 WriteShort(MSG_ENTITY, self.warpzone_fadeend);
233         }
234
235         return TRUE;
236 }
237
238 float WarpZone_CheckProjectileImpact()
239 {
240         // if self hit a warpzone, abort
241         vector o0, v0, a0;
242         float mpd, pd, dpd;
243         entity wz;
244         wz = WarpZone_Find(self.origin + self.mins, self.origin + self.maxs);
245         if(!wz)
246                 return FALSE;
247         o0 = self.origin;
248         v0 = self.velocity;
249         a0 = self.angles;
250
251         // this approach transports the projectile at its full speed, but does
252         // not properly retain the projectile trail (but we can't retain it
253         // easily anyway without delaying the projectile by two frames, so who
254         // cares)
255         WarpZone_TraceBox_ThroughZone(self.warpzone_oldorigin, self.mins, self.maxs, self.warpzone_oldorigin + self.warpzone_oldvelocity * frametime, MOVE_NORMAL, self, wz, WarpZone_trace_callback_t_null); // this will get us through the warpzone
256         setorigin(self, trace_endpos);
257         self.angles = WarpZone_TransformAngles(WarpZone_trace_transform, self.angles);
258         self.velocity = WarpZone_TransformVelocity(WarpZone_trace_transform, self.warpzone_oldvelocity);
259         
260         // in case we are in our warp zone post-teleport, shift the projectile forward a bit
261         mpd = max(vlen(self.mins), vlen(self.maxs));
262         pd = WarpZone_TargetPlaneDist(wz, self.origin);
263         if(pd < mpd)
264         {
265                 dpd = normalize(self.velocity) * self.warpzone_targetforward;
266                 setorigin(self, self.origin + normalize(self.velocity) * ((mpd - pd) / dpd));
267                 if(!WarpZoneLib_MoveOutOfSolid(self))
268                 {
269                         setorigin(self, o0);
270                         self.angles = a0;
271                         self.velocity = v0;
272                         return FALSE;
273                 }
274         }
275         WarpZone_RefSys_Add(self, wz);
276         WarpZone_StoreProjectileData(self);
277         self.warpzone_teleport_time = time;
278
279         return TRUE;
280 }
281 float WarpZone_Projectile_Touch()
282 {
283         if(other.classname == "trigger_warpzone")
284                 return TRUE;
285         if(WarpZone_Projectile_Touch_ImpactFilter_Callback())
286                 return TRUE;
287         if(WarpZone_CheckProjectileImpact())
288                 return TRUE;
289         if(self.warpzone_teleport_time == time) // already got teleported this frame? no collision then please
290         {
291                 setorigin(self, self.warpzone_oldorigin);
292                 self.velocity = self.warpzone_oldvelocity;
293                 self.angles = self.warpzone_oldangles;
294                 return TRUE;
295         }
296
297         return FALSE;
298 }
299
300 void WarpZone_InitStep_FindOriginTarget()
301 {
302         if(self.killtarget != "")
303         {
304                 self.aiment = find(world, targetname, self.killtarget);
305                 if(self.aiment == world)
306                 {
307                         error("Warp zone with nonexisting killtarget");
308                         return;
309                 }
310                 self.killtarget = string_null;
311         }
312 }
313
314 void WarpZonePosition_InitStep_FindTarget()
315 {
316         if(self.target == "")
317         {
318                 error("Warp zone position with no target");
319                 return;
320         }
321         self.enemy = find(world, targetname, self.target);
322         if(self.enemy == world)
323         {
324                 error("Warp zone position with nonexisting target");
325                 return;
326         }
327         if(self.enemy.aiment)
328         {
329                 // already is positioned
330                 error("Warp zone position targeting already oriented warpzone");
331                 return;
332         }
333         self.enemy.aiment = self;
334 }
335
336 void WarpZoneCamera_InitStep_FindTarget()
337 {
338         entity e;
339         float i;
340         if(self.target == "")
341         {
342                 error("Camera with no target");
343                 return;
344         }
345         self.enemy = world;
346         for(e = world, i = 0; (e = find(e, targetname, self.target)); )
347                 if(random() * ++i < 1)
348                         self.enemy = e;
349         if(self.enemy == world)
350         {
351                 error("Camera with nonexisting target");
352                 return;
353         }
354         ++warpzone_cameras_exist;
355         WarpZone_Camera_SetUp(self, self.enemy.origin, self.enemy.angles);
356         self.SendFlags = 0xFFFFFF;
357 }
358
359 void WarpZone_InitStep_UpdateTransform()
360 {
361         vector org, ang, norm, point;
362         float area;
363         vector tri, a, b, c, p, q, n;
364         float i_s, i_t, n_t;
365         string tex;
366
367         org = self.origin;
368         if(org == '0 0 0')
369                 org = 0.5 * (self.mins + self.maxs);
370
371         norm = point = '0 0 0';
372         area = 0;
373         for(i_s = 0; ; ++i_s)
374         {
375                 tex = getsurfacetexture(self, i_s);
376                 if not(tex)
377                         break; // this is beyond the last one
378                 if(tex == "textures/common/trigger")
379                         continue;
380                 n_t = getsurfacenumtriangles(self, i_s);
381                 for(i_t = 0; i_t < n_t; ++i_t)
382                 {
383                         tri = getsurfacetriangle(self, i_s, i_t);
384                         a = getsurfacepoint(self, i_s, tri_x);
385                         b = getsurfacepoint(self, i_s, tri_y);
386                         c = getsurfacepoint(self, i_s, tri_z);
387                         p = b - a;
388                         q = c - a;
389                         n =     '1 0 0' * (q_y * p_z - q_z * p_y)
390                         +       '0 1 0' * (q_z * p_x - q_x * p_z)
391                         +       '0 0 1' * (q_x * p_y - q_y * p_x);
392                         area = area + vlen(n);
393                         norm = norm + n;
394                         point = point + vlen(n) * (a + b + c);
395                 }
396         }
397         if(area > 0)
398         {
399                 norm = norm * (1 / area);
400                 point = point * (1 / (3 * area));
401                 if(vlen(norm) < 0.99)
402                 {
403                         print("trigger_warpzone near ", vtos(self.aiment.origin), " is nonplanar. BEWARE.\n");
404                         area = 0; // no autofixing in this case
405                 }
406                 norm = normalize(norm);
407         }
408
409         if(self.aiment)
410         {
411                 org = self.aiment.origin;
412                 ang = self.aiment.angles;
413                 if(area > 0)
414                 {
415                         org = org - ((org - point) * norm) * norm; // project to plane
416                         makevectors(ang);
417                         if(norm * v_forward < 0)
418                         {
419                                 print("Position target of trigger_warpzone near ", vtos(self.aiment.origin), " points into trigger_warpzone. BEWARE.\n");
420                                 norm = -1 * norm;
421                         }
422                         ang = vectoangles(norm, v_up); // keep rotation, but turn exactly against plane
423                         ang_x = -ang_x;
424                         if(norm * v_forward < 0.99)
425                                 print("trigger_warpzone near ", vtos(self.aiment.origin), " has been turned to match plane orientation (", vtos(self.aiment.angles), " -> ", vtos(ang), "\n");
426                         if(vlen(org - self.aiment.origin) > 0.5)
427                                 print("trigger_warpzone near ", vtos(self.aiment.origin), " has been moved to match the plane (", vtos(self.aiment.origin), " -> ", vtos(org), ").\n");
428                 }
429         }
430         else if(area > 0)
431         {
432                 org = point;
433                 ang = vectoangles(norm);
434                 ang_x = -ang_x;
435         }
436         else
437                 error("cannot infer origin/angles for this warpzone, please use a killtarget or a trigger_warpzone_position");
438
439         self.warpzone_origin = org;
440         self.warpzone_angles = ang;
441 }
442
443 void WarpZone_InitStep_ClearTarget()
444 {
445         if(self.enemy)
446                 self.enemy.enemy = world;
447         self.enemy = world;
448 }
449
450 entity warpzone_first; .entity warpzone_next;
451 void WarpZone_InitStep_FindTarget()
452 {
453         float i;
454         entity e, e2;
455
456         if(self.enemy)
457                 return;
458
459         // this way only one of the two ents needs to target
460         if(self.target != "")
461         {
462                 self.enemy = self; // so the if(!e.enemy) check also skips self, saves one IF
463
464                 e2 = world;
465                 for(e = world, i = 0; (e = find(e, targetname, self.target)); )
466                         if(!e.enemy)
467                                 if(e.classname == self.classname) // possibly non-warpzones may use the same targetname!
468                                         if(random() * ++i < 1)
469                                                 e2 = e;
470                 if(!e2)
471                 {
472                         self.enemy = world;
473                         error("Warpzone with non-existing target");
474                         return;
475                 }
476                 self.enemy = e2;
477                 e2.enemy = self;
478         }
479 }
480
481 void WarpZone_InitStep_FinalizeTransform()
482 {
483         if(!self.enemy || self.enemy.enemy != self)
484         {
485                 error("Invalid warp zone detected. Killed.");
486                 return;
487         }
488
489         ++warpzone_warpzones_exist;
490         WarpZone_SetUp(self, self.warpzone_origin, self.warpzone_angles, self.enemy.warpzone_origin, self.enemy.warpzone_angles);
491         self.touch = WarpZone_Touch;
492         self.SendFlags = 0xFFFFFF;
493 }
494
495 float warpzone_initialized;
496 entity warpzone_first;
497 entity warpzone_position_first;
498 entity warpzone_camera_first;
499 .entity warpzone_next;
500 void spawnfunc_misc_warpzone_position(void)
501 {
502         // "target", "angles", "origin"
503         self.warpzone_next = warpzone_position_first;
504         warpzone_position_first = self;
505 }
506 void spawnfunc_trigger_warpzone_position(void)
507 {
508         spawnfunc_misc_warpzone_position();
509 }
510 void spawnfunc_trigger_warpzone(void)
511 {
512         // warp zone entities must have:
513         // "killtarget" pointing to a target_position with a direction arrow
514         //              that points AWAY from the warp zone, and that is inside
515         //              the warp zone trigger
516         // "target"     pointing to an identical warp zone at another place in
517         //              the map, with another killtarget to designate its
518         //              orientation
519
520         if(!self.scale)
521                 self.scale = self.modelscale;
522         if(!self.scale)
523                 self.scale = 1;
524         string m;
525         m = self.model;
526         WarpZoneLib_ExactTrigger_Init();
527         if(m != "")
528         {
529                 precache_model(m);
530                 setmodel(self, m); // no precision needed
531         }
532         setorigin(self, self.origin);
533         if(self.scale)
534                 setsize(self, self.mins * self.scale, self.maxs * self.scale);
535         else
536                 setsize(self, self.mins, self.maxs);
537         self.SendEntity = WarpZone_Send;
538         self.SendFlags = 0xFFFFFF;
539         self.effects |= EF_NODEPTHTEST;
540         self.warpzone_next = warpzone_first;
541         warpzone_first = self;
542 }
543 void spawnfunc_func_camera(void)
544 {
545         if(!self.scale)
546                 self.scale = self.modelscale;
547         if(!self.scale)
548                 self.scale = 1;
549         if(self.model != "")
550         {
551                 precache_model(self.model);
552                 setmodel(self, self.model); // no precision needed
553         }
554         setorigin(self, self.origin);
555         if(self.scale)
556                 setsize(self, self.mins * self.scale, self.maxs * self.scale);
557         else
558                 setsize(self, self.mins, self.maxs);
559         if(!self.solid)
560                 self.solid = SOLID_BSP;
561         else if(self.solid < 0)
562                 self.solid = SOLID_NOT;
563         self.SendEntity = WarpZone_Camera_Send;
564         self.SendFlags = 0xFFFFFF;
565         self.warpzone_next = warpzone_camera_first;
566         warpzone_camera_first = self;
567 }
568 void WarpZones_Reconnect()
569 {
570         entity e;
571         e = self;
572         for(self = warpzone_first; self; self = self.warpzone_next)
573                 WarpZone_InitStep_ClearTarget();
574         for(self = warpzone_first; self; self = self.warpzone_next)
575                 WarpZone_InitStep_FindTarget();
576         for(self = warpzone_camera_first; self; self = self.warpzone_next)
577                 WarpZoneCamera_InitStep_FindTarget();
578         for(self = warpzone_first; self; self = self.warpzone_next)
579                 WarpZone_InitStep_FinalizeTransform();
580         self = e;
581 }
582
583 void WarpZone_StartFrame()
584 {
585         entity e;
586         if(warpzone_initialized == 0)
587         {
588                 warpzone_initialized = 1;
589                 e = self;
590                 for(self = warpzone_first; self; self = self.warpzone_next)
591                         WarpZone_InitStep_FindOriginTarget();
592                 for(self = warpzone_position_first; self; self = self.warpzone_next)
593                         WarpZonePosition_InitStep_FindTarget();
594                 for(self = warpzone_first; self; self = self.warpzone_next)
595                         WarpZone_InitStep_UpdateTransform();
596                 self = e;
597                 WarpZones_Reconnect();
598         }
599         for(e = world; (e = nextent(e)); )
600                 WarpZone_StoreProjectileData(e);
601 }
602
603 .float warpzone_reconnecting;
604 float visible_to_some_client(entity ent)
605 {
606         entity e;
607         for(e = nextent(world); clienttype(e) != CLIENTTYPE_NOTACLIENT; e = nextent(e))
608                 if(e.classname == "player" && clienttype(e) == CLIENTTYPE_REAL)
609                         if(checkpvs(e.origin + e.view_ofs, ent))
610                                 return 1;
611         return 0;
612 }
613 void trigger_warpzone_reconnect_use()
614 {
615         entity e;
616         e = self;
617         // NOTE: this matches for target, not targetname, but of course
618         // targetname must be set too on the other entities
619         for(self = warpzone_first; self; self = self.warpzone_next)
620                 self.warpzone_reconnecting = ((e.target == "" || self.target == e.target) && !((e.spawnflags & 1) && (visible_to_some_client(self) || visible_to_some_client(self.enemy))));
621         for(self = warpzone_camera_first; self; self = self.warpzone_next)
622                 self.warpzone_reconnecting = ((e.target == "" || self.target == e.target) && !((e.spawnflags & 1) && visible_to_some_client(self)));
623         for(self = warpzone_first; self; self = self.warpzone_next)
624                 if(self.warpzone_reconnecting)
625                         WarpZone_InitStep_ClearTarget();
626         for(self = warpzone_first; self; self = self.warpzone_next)
627                 if(self.warpzone_reconnecting)
628                         WarpZone_InitStep_FindTarget();
629         for(self = warpzone_camera_first; self; self = self.warpzone_next)
630                 if(self.warpzone_reconnecting)
631                         WarpZoneCamera_InitStep_FindTarget();
632         for(self = warpzone_first; self; self = self.warpzone_next)
633                 if(self.warpzone_reconnecting || self.enemy.warpzone_reconnecting)
634                         WarpZone_InitStep_FinalizeTransform();
635         self = e;
636 }
637
638 void spawnfunc_trigger_warpzone_reconnect()
639 {
640         self.use = trigger_warpzone_reconnect_use;
641 }
642
643 void spawnfunc_target_warpzone_reconnect()
644 {
645         spawnfunc_trigger_warpzone_reconnect(); // both names make sense here :(
646 }