]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/warpzonelib/server.qc
Merge remote-tracking branch 'origin/TimePath/experiments/csqc_prediction' into TimeP...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / warpzonelib / server.qc
1 #if defined(CSQC)
2 #elif defined(MENUQC)
3 #elif defined(SVQC)
4         #include "../dpdefs/progsdefs.qh"
5         #include "../dpdefs/dpextensions.qh"
6         #include "common.qh"
7         #include "server.qh"
8         #include "../common/constants.qh"
9         #include "../common/util.qh"
10         #include "../server/constants.qh"
11         #include "../server/defs.qh"
12         #include "../server/command/common.qh"
13 #endif
14
15 #ifdef WARPZONELIB_KEEPDEBUG
16 #define WARPZONELIB_REMOVEHACK
17 #endif
18
19 // for think function
20 .vector warpzone_save_origin;
21 .vector warpzone_save_angles;
22 .vector warpzone_save_eorigin;
23 .vector warpzone_save_eangles;
24
25 // for all entities
26 .vector warpzone_oldorigin, warpzone_oldvelocity, warpzone_oldangles;
27 .float warpzone_teleport_time;
28 .float warpzone_teleport_finishtime;
29 .entity warpzone_teleport_zone;
30
31 void WarpZone_StoreProjectileData(entity e)
32 {
33         e.warpzone_oldorigin = e.origin;
34         e.warpzone_oldvelocity = e.velocity;
35         e.warpzone_oldangles = e.angles;
36 }
37
38 void WarpZone_TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity)
39 {
40         setorigin (player, to); // NOTE: this also aborts the move, when this is called by touch
41         player.oldorigin = to; // for DP's unsticking
42         player.angles = to_angles;
43         player.fixangle = true;
44         player.velocity = to_velocity;
45
46         BITXOR_ASSIGN(player.effects, EF_TELEPORT_BIT);
47
48         if(IS_PLAYER(player))
49                 BITCLR_ASSIGN(player.flags, FL_ONGROUND);
50
51         WarpZone_PostTeleportPlayer_Callback(player);
52 }
53
54 float WarpZone_Teleported_Send(entity to, float sf)
55 {
56         WriteByte(MSG_ENTITY, ENT_CLIENT_WARPZONE_TELEPORTED);
57         WriteCoord(MSG_ENTITY, self.angles.x);
58         WriteCoord(MSG_ENTITY, self.angles.y);
59         WriteCoord(MSG_ENTITY, self.angles.z);
60         return true;
61 }
62
63 float WarpZone_Teleport(entity wz, entity player, float f0, float f1)
64 {
65         vector o0, a0, v0, o1, a1, v1, o10;
66
67         o0 = player.origin + player.view_ofs;
68         v0 = player.velocity;
69         a0 = player.angles;
70
71         o10 = o1 = WarpZone_TransformOrigin(wz, o0);
72         v1 = WarpZone_TransformVelocity(wz, v0);
73         if (!IS_NOT_A_CLIENT(player))
74                 a1 = WarpZone_TransformVAngles(wz, player.v_angle);
75         else
76                 a1 = WarpZone_TransformAngles(wz, a0);
77
78         if(f0 != 0 || f1 != 0)
79         {
80                 // retry last move but behind the warpzone!
81                 // we must first go back as far as we can, then forward again, to not cause double touch events!
82
83                 tracebox(o1 - player.view_ofs + v1 * frametime * f1, player.mins, player.maxs, o1 - player.view_ofs + v1 * frametime * f0, MOVE_WORLDONLY, player);
84                 {
85                         entity own;
86                         own = player.owner;
87                         player.owner = world;
88                         tracebox(trace_endpos, player.mins, player.maxs, o1 - player.view_ofs + v1 * frametime * f1, MOVE_NORMAL, player); // this should get us through the warpzone
89                         player.owner = own;
90                 }
91                 o1 = trace_endpos + player.view_ofs;
92
93                 float d, dv, md;
94                 md = max(vlen(player.mins), vlen(player.maxs));
95                 d = WarpZone_TargetPlaneDist(wz, o1);
96                 dv = WarpZone_TargetPlaneDist(wz, v1);
97                 if(d < 0)
98                         o1 = o1 - v1 * (d / dv);
99         }
100
101         // put him out of solid
102         tracebox(o1 - player.view_ofs, player.mins, player.maxs, o1 - player.view_ofs, MOVE_NOMONSTERS, player);
103         if(trace_startsolid)
104         {
105                 setorigin(player, o1 - player.view_ofs);
106                 if(WarpZoneLib_MoveOutOfSolid(player))
107                 {
108                         o1 = player.origin + player.view_ofs;
109                         setorigin(player, o0 - player.view_ofs);
110                 }
111                 else
112                 {
113                         print("would have to put player in solid, won't do that\n");
114                         setorigin(player, o0 - player.view_ofs);
115                         return 0;
116                 }
117         }
118
119         // do the teleport
120         WarpZone_RefSys_Add(player, wz);
121         WarpZone_TeleportPlayer(wz, player, o1 - player.view_ofs, a1, v1);
122         WarpZone_StoreProjectileData(player);
123         player.warpzone_teleport_time = time;
124         player.warpzone_teleport_finishtime = time;
125         player.warpzone_teleport_zone = wz;
126
127         // prevent further teleports back
128         float dt = (o1 - o10) * v1 * (1 / (v1 * v1));
129         if(dt < sys_frametime)
130                 player.warpzone_teleport_finishtime += sys_frametime - dt;
131
132 #ifndef WARPZONE_USE_FIXANGLE
133         if(IS_PLAYER(player))
134         {
135                 // instead of fixangle, send the transform to the client for smoother operation
136                 player.fixangle = false;
137
138                 entity ts = spawn();
139                 setmodel(ts, "null");
140                 ts.SendEntity = WarpZone_Teleported_Send;
141                 ts.SendFlags = 0xFFFFFF;
142                 ts.drawonlytoclient = player;
143                 ts.think = SUB_Remove;
144                 ts.nextthink = time + 1;
145                 ts.owner = player;
146                 ts.enemy = wz;
147                 ts.effects = EF_NODEPTHTEST;
148                 ts.classname = "warpzone_teleported";
149                 ts.angles = wz.warpzone_transform;
150         }
151 #endif
152
153         return 1;
154 }
155
156 void WarpZone_Touch (void)
157 {
158         entity oldself;
159
160         if(other.classname == "trigger_warpzone")
161                 return;
162
163         if(time <= other.warpzone_teleport_finishtime) // already teleported this frame
164                 return;
165
166         // FIXME needs a better check to know what is safe to teleport and what not
167         if(other.movetype == MOVETYPE_NONE || other.movetype == MOVETYPE_FOLLOW || other.tag_entity)
168                 return;
169
170         if(WarpZoneLib_ExactTrigger_Touch())
171                 return;
172
173         if(WarpZone_PlaneDist(self, other.origin + other.view_ofs) >= 0) // wrong side of the trigger_warpzone (don't teleport yet)
174                 return;
175
176         float f;
177         // number of frames we need to go back:
178         //   dist = 16*sqrt(2) qu
179         //   dist ~ 24 qu
180         //   24 qu = v*t
181         //   24 qu = v*frametime*n
182         //       n = 24 qu/(v*frametime)
183         // for clients go only one frame though, may be too irritating otherwise
184         // but max 0.25 sec = 0.25/frametime frames
185         //       24/(0.25/frametime)
186         //       96*frametime
187         float d;
188         d = 24 + max(vlen(other.mins), vlen(other.maxs));
189         if(IS_NOT_A_CLIENT(other))
190                 f = -d / bound(frametime * d * 1, frametime * vlen(other.velocity), d);
191         else
192                 f = -1;
193         if(WarpZone_Teleport(self, other, f, 0))
194         {
195                 string save1, save2;
196                 activator = other;
197
198                 save1 = self.target; self.target = string_null;
199                 save2 = self.target3; self.target3 = string_null;
200                 SUB_UseTargets();
201                 if (!self.target) self.target = save1;
202                 if (!self.target3) self.target3 = save2;
203
204                 oldself = self;
205                 self = self.enemy;
206                 save1 = self.target; self.target = string_null;
207                 save2 = self.target2; self.target2 = string_null;
208                 SUB_UseTargets();
209                 if (!self.target) self.target = save1;
210                 if (!self.target2) self.target2 = save2;
211                 self = oldself;
212         }
213         else
214         {
215                 dprint("WARPZONE FAIL AHAHAHAHAH))\n");
216         }
217 }
218
219 float WarpZone_Send(entity to, float sendflags)
220 {
221         WriteByte(MSG_ENTITY, ENT_CLIENT_WARPZONE);
222
223         // we must send this flag for clientside to match properly too
224         int f = 0;
225         if(self.warpzone_isboxy)
226                 BITSET_ASSIGN(f, 1);
227         if(self.warpzone_fadestart)
228                 BITSET_ASSIGN(f, 2);
229         if(self.origin != '0 0 0')
230                 BITSET_ASSIGN(f, 4);
231         WriteByte(MSG_ENTITY, f);
232
233         // we need THESE to render the warpzone (and cull properly)...
234         if(f & 4)
235         {
236                 WriteCoord(MSG_ENTITY, self.origin.x);
237                 WriteCoord(MSG_ENTITY, self.origin.y);
238                 WriteCoord(MSG_ENTITY, self.origin.z);
239         }
240
241         WriteShort(MSG_ENTITY, self.modelindex);
242         WriteCoord(MSG_ENTITY, self.mins.x);
243         WriteCoord(MSG_ENTITY, self.mins.y);
244         WriteCoord(MSG_ENTITY, self.mins.z);
245         WriteCoord(MSG_ENTITY, self.maxs.x);
246         WriteCoord(MSG_ENTITY, self.maxs.y);
247         WriteCoord(MSG_ENTITY, self.maxs.z);
248         WriteByte(MSG_ENTITY, bound(1, self.scale * 16, 255));
249
250         // we need THESE to calculate the proper transform
251         WriteCoord(MSG_ENTITY, self.warpzone_origin.x);
252         WriteCoord(MSG_ENTITY, self.warpzone_origin.y);
253         WriteCoord(MSG_ENTITY, self.warpzone_origin.z);
254         WriteCoord(MSG_ENTITY, self.warpzone_angles.x);
255         WriteCoord(MSG_ENTITY, self.warpzone_angles.y);
256         WriteCoord(MSG_ENTITY, self.warpzone_angles.z);
257         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin.x);
258         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin.y);
259         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin.z);
260         WriteCoord(MSG_ENTITY, self.warpzone_targetangles.x);
261         WriteCoord(MSG_ENTITY, self.warpzone_targetangles.y);
262         WriteCoord(MSG_ENTITY, self.warpzone_targetangles.z);
263
264         if(f & 2)
265         {
266                 WriteShort(MSG_ENTITY, self.warpzone_fadestart);
267                 WriteShort(MSG_ENTITY, self.warpzone_fadeend);
268         }
269
270         return true;
271 }
272
273 float WarpZone_Camera_Send(entity to, float sendflags)
274 {
275         int f = 0;
276         WriteByte(MSG_ENTITY, ENT_CLIENT_WARPZONE_CAMERA);
277
278         if(self.warpzone_fadestart)
279                 BITSET_ASSIGN(f, 2);
280         if(self.origin != '0 0 0')
281                 BITSET_ASSIGN(f, 4);
282         WriteByte(MSG_ENTITY, f);
283
284         // we need THESE to render the warpzone (and cull properly)...
285         if(f & 4)
286         {
287                 WriteCoord(MSG_ENTITY, self.origin.x);
288                 WriteCoord(MSG_ENTITY, self.origin.y);
289                 WriteCoord(MSG_ENTITY, self.origin.z);
290         }
291
292         WriteShort(MSG_ENTITY, self.modelindex);
293         WriteCoord(MSG_ENTITY, self.mins.x);
294         WriteCoord(MSG_ENTITY, self.mins.y);
295         WriteCoord(MSG_ENTITY, self.mins.z);
296         WriteCoord(MSG_ENTITY, self.maxs.x);
297         WriteCoord(MSG_ENTITY, self.maxs.y);
298         WriteCoord(MSG_ENTITY, self.maxs.z);
299         WriteByte(MSG_ENTITY, bound(1, self.scale * 16, 255));
300
301         // we need THESE to calculate the proper transform
302         WriteCoord(MSG_ENTITY, self.enemy.origin.x);
303         WriteCoord(MSG_ENTITY, self.enemy.origin.y);
304         WriteCoord(MSG_ENTITY, self.enemy.origin.z);
305         WriteCoord(MSG_ENTITY, self.enemy.angles.x);
306         WriteCoord(MSG_ENTITY, self.enemy.angles.y);
307         WriteCoord(MSG_ENTITY, self.enemy.angles.z);
308
309         if(f & 2)
310         {
311                 WriteShort(MSG_ENTITY, self.warpzone_fadestart);
312                 WriteShort(MSG_ENTITY, self.warpzone_fadeend);
313         }
314
315         return true;
316 }
317
318 #ifdef WARPZONELIB_KEEPDEBUG
319 float WarpZone_CheckProjectileImpact(entity player)
320 {
321         vector o0, v0;
322
323         o0 = player.origin + player.view_ofs;
324         v0 = player.velocity;
325
326         // if we teleported shortly before, abort
327         if(time <= player.warpzone_teleport_finishtime + 0.1)
328                 return 0;
329
330         // if player hit a warpzone, abort
331         entity wz;
332         wz = WarpZone_Find(o0 + player.mins, o0 + player.maxs);
333         if(!wz)
334                 return 0;
335
336 #ifdef WARPZONELIB_REMOVEHACK
337         print("impactfilter found something - and it no longer gets handled correctly - please tell divVerent whether anything behaves broken now\n");
338 #else
339         print("impactfilter found something - and it even gets handled correctly - please tell divVerent that this code apparently gets triggered again\n");
340 #endif
341         print("Entity type: ", player.classname, "\n");
342         print("Origin: ", vtos(player.origin), "\n");
343         print("Velocity: ", vtos(player.velocity), "\n");
344
345 #ifdef WARPZONELIB_REMOVEHACK
346         return 0;
347 #else
348         // retry previous move
349         setorigin(player, player.warpzone_oldorigin);
350         player.velocity = player.warpzone_oldvelocity;
351         if(WarpZone_Teleport(wz, player, 0, 1))
352         {
353                 entity oldself;
354                 string save1, save2;
355
356                 oldself = self;
357                 self = wz;
358                 other = player;
359                 activator = player;
360
361                 save1 = self.target; self.target = string_null;
362                 save2 = self.target3; self.target3 = string_null;
363                 SUB_UseTargets();
364                 if (!self.target) self.target = save1;
365                 if (!self.target3) self.target3 = save2;
366
367                 self = self.enemy;
368                 save1 = self.target; self.target = string_null;
369                 save2 = self.target2; self.target2 = string_null;
370                 SUB_UseTargets();
371                 if (!self.target) self.target = save1;
372                 if (!self.target2) self.target2 = save2;
373                 self = oldself;
374         }
375         else
376         {
377                 setorigin(player, o0 - player.view_ofs);
378                 player.velocity = v0;
379         }
380
381         return +1;
382 #endif
383 }
384 #endif
385
386 float WarpZone_Projectile_Touch()
387 {
388         if(other.classname == "trigger_warpzone")
389                 return true;
390
391         // no further impacts if we teleported this frame!
392         // this is because even if we did teleport, the engine still may raise
393         // touch events for the previous location
394         // engine now aborts moves on teleport, so this SHOULD not happen any more
395         // but if this is called from TouchAreaGrid of the projectile moving,
396         // then this won't do
397         if(time == self.warpzone_teleport_time)
398                 return true;
399
400 #ifdef WARPZONELIB_KEEPDEBUG
401         // this SEEMS to not happen at the moment, but if it did, it would be more reliable
402         {
403                 float save_dpstartcontents;
404                 float save_dphitcontents;
405                 float save_dphitq3surfaceflags;
406                 string save_dphittexturename;
407                 float save_allsolid;
408                 float save_startsolid;
409                 float save_fraction;
410                 vector save_endpos;
411                 vector save_plane_normal;
412                 float save_plane_dist;
413                 entity save_ent;
414                 float save_inopen;
415                 float save_inwater;
416                 save_dpstartcontents = trace_dpstartcontents;
417                 save_dphitcontents = trace_dphitcontents;
418                 save_dphitq3surfaceflags = trace_dphitq3surfaceflags;
419                 save_dphittexturename = trace_dphittexturename;
420                 save_allsolid = trace_allsolid;
421                 save_startsolid = trace_startsolid;
422                 save_fraction = trace_fraction;
423                 save_endpos = trace_endpos;
424                 save_plane_normal = trace_plane_normal;
425                 save_plane_dist = trace_plane_dist;
426                 save_ent = trace_ent;
427                 save_inopen = trace_inopen;
428                 save_inwater = trace_inwater;
429                 float f;
430                 if((f = WarpZone_CheckProjectileImpact(self)) != 0)
431                         return (f > 0);
432                 trace_dpstartcontents = save_dpstartcontents;
433                 trace_dphitcontents = save_dphitcontents;
434                 trace_dphitq3surfaceflags = save_dphitq3surfaceflags;
435                 trace_dphittexturename = save_dphittexturename;
436                 trace_allsolid = save_allsolid;
437                 trace_startsolid = save_startsolid;
438                 trace_fraction = save_fraction;
439                 trace_endpos = save_endpos;
440                 trace_plane_normal = save_plane_normal;
441                 trace_plane_dist = save_plane_dist;
442                 trace_ent = save_ent;
443                 trace_inopen = save_inopen;
444                 trace_inwater = save_inwater;
445         }
446 #endif
447
448         if(WarpZone_Projectile_Touch_ImpactFilter_Callback())
449                 return true;
450
451         return false;
452 }
453
454 void WarpZone_InitStep_FindOriginTarget()
455 {
456         if(self.killtarget != "")
457         {
458                 self.aiment = find(world, targetname, self.killtarget);
459                 if(self.aiment == world)
460                 {
461                         error("Warp zone with nonexisting killtarget");
462                         return;
463                 }
464                 self.killtarget = string_null;
465         }
466 }
467
468 void WarpZonePosition_InitStep_FindTarget()
469 {
470         if(self.target == "")
471         {
472                 error("Warp zone position with no target");
473                 return;
474         }
475         self.enemy = find(world, targetname, self.target);
476         if(self.enemy == world)
477         {
478                 error("Warp zone position with nonexisting target");
479                 return;
480         }
481         if(self.enemy.aiment)
482         {
483                 // already is positioned
484                 error("Warp zone position targeting already oriented warpzone");
485                 return;
486         }
487         self.enemy.aiment = self;
488 }
489
490 void WarpZoneCamera_Think(void)
491 {
492         if(self.warpzone_save_origin != self.origin
493         || self.warpzone_save_angles != self.angles
494         || self.warpzone_save_eorigin != self.enemy.origin
495         || self.warpzone_save_eangles != self.enemy.angles)
496         {
497                 WarpZone_Camera_SetUp(self, self.enemy.origin, self.enemy.angles);
498                 self.warpzone_save_origin = self.origin;
499                 self.warpzone_save_angles = self.angles;
500                 self.warpzone_save_eorigin = self.enemy.origin;
501                 self.warpzone_save_eangles = self.enemy.angles;
502         }
503         self.nextthink = time;
504 }
505
506 void WarpZoneCamera_InitStep_FindTarget()
507 {
508         entity e;
509         float i;
510         if(self.target == "")
511         {
512                 error("Camera with no target");
513                 return;
514         }
515         self.enemy = world;
516         for(e = world, i = 0; (e = find(e, targetname, self.target)); )
517                 if(random() * ++i < 1)
518                         self.enemy = e;
519         if(self.enemy == world)
520         {
521                 error("Camera with nonexisting target");
522                 return;
523         }
524         warpzone_cameras_exist = 1;
525         WarpZone_Camera_SetUp(self, self.enemy.origin, self.enemy.angles);
526         self.SendFlags = 0xFFFFFF;
527         if(self.spawnflags & 1)
528         {
529                 self.think = WarpZoneCamera_Think;
530                 self.nextthink = time;
531         }
532         else
533                 self.nextthink = 0;
534 }
535
536 void WarpZone_InitStep_UpdateTransform()
537 {
538         vector org, ang, norm, point;
539         float area;
540         vector tri, a, b, c, p, q, n;
541         float i_s, i_t, n_t;
542         string tex;
543
544         org = self.origin;
545         if(org == '0 0 0')
546                 org = 0.5 * (self.mins + self.maxs);
547
548         norm = point = '0 0 0';
549         area = 0;
550         for(i_s = 0; ; ++i_s)
551         {
552                 tex = getsurfacetexture(self, i_s);
553                 if (!tex)
554                         break; // this is beyond the last one
555                 if(tex == "textures/common/trigger" || tex == "trigger")
556                         continue;
557                 n_t = getsurfacenumtriangles(self, i_s);
558                 for(i_t = 0; i_t < n_t; ++i_t)
559                 {
560                         tri = getsurfacetriangle(self, i_s, i_t);
561                         a = getsurfacepoint(self, i_s, tri.x);
562                         b = getsurfacepoint(self, i_s, tri.y);
563                         c = getsurfacepoint(self, i_s, tri.z);
564                         p = b - a;
565                         q = c - a;
566                         n =     '1 0 0' * (q.y * p.z - q.z * p.y)
567                         +       '0 1 0' * (q.z * p.x - q.x * p.z)
568                         +       '0 0 1' * (q.x * p.y - q.y * p.x);
569                         area = area + vlen(n);
570                         norm = norm + n;
571                         point = point + vlen(n) * (a + b + c);
572                 }
573         }
574         if(area > 0)
575         {
576                 norm = norm * (1 / area);
577                 point = point * (1 / (3 * area));
578                 if(vlen(norm) < 0.99)
579                 {
580                         print("trigger_warpzone near ", vtos(self.aiment.origin), " is nonplanar. BEWARE.\n");
581                         area = 0; // no autofixing in this case
582                 }
583                 norm = normalize(norm);
584         }
585
586         ang = '0 0 0';
587         if(self.aiment)
588         {
589                 org = self.aiment.origin;
590                 ang = self.aiment.angles;
591                 if(area > 0)
592                 {
593                         org = org - ((org - point) * norm) * norm; // project to plane
594                         makevectors(ang);
595                         if(norm * v_forward < 0)
596                         {
597                                 print("Position target of trigger_warpzone near ", vtos(self.aiment.origin), " points into trigger_warpzone. BEWARE.\n");
598                                 norm = -1 * norm;
599                         }
600                         ang = vectoangles2(norm, v_up); // keep rotation, but turn exactly against plane
601                         ang.x = -ang.x;
602                         if(norm * v_forward < 0.99)
603                                 print("trigger_warpzone near ", vtos(self.aiment.origin), " has been turned to match plane orientation (", vtos(self.aiment.angles), " -> ", vtos(ang), "\n");
604                         if(vlen(org - self.aiment.origin) > 0.5)
605                                 print("trigger_warpzone near ", vtos(self.aiment.origin), " has been moved to match the plane (", vtos(self.aiment.origin), " -> ", vtos(org), ").\n");
606                 }
607         }
608         else if(area > 0)
609         {
610                 org = point;
611                 ang = vectoangles(norm);
612                 ang.x = -ang.x;
613         }
614         else
615                 error("cannot infer origin/angles for this warpzone, please use a killtarget or a trigger_warpzone_position");
616
617         self.warpzone_origin = org;
618         self.warpzone_angles = ang;
619 }
620
621 void WarpZone_InitStep_ClearTarget()
622 {
623         if(self.enemy)
624                 self.enemy.enemy = world;
625         self.enemy = world;
626 }
627
628 entity warpzone_first; .entity warpzone_next;
629 void WarpZone_InitStep_FindTarget()
630 {
631         float i;
632         entity e, e2;
633
634         if(self.enemy)
635                 return;
636
637         // this way only one of the two ents needs to target
638         if(self.target != "")
639         {
640                 self.enemy = self; // so the if(!e.enemy) check also skips self, saves one IF
641
642                 e2 = world;
643                 for(e = world, i = 0; (e = find(e, targetname, self.target)); )
644                         if(!e.enemy)
645                                 if(e.classname == self.classname) // possibly non-warpzones may use the same targetname!
646                                         if(random() * ++i < 1)
647                                                 e2 = e;
648                 if(!e2)
649                 {
650                         self.enemy = world;
651                         error("Warpzone with non-existing target");
652                         return;
653                 }
654                 self.enemy = e2;
655                 e2.enemy = self;
656         }
657 }
658
659 void WarpZone_Think();
660 void WarpZone_InitStep_FinalizeTransform()
661 {
662         if(!self.enemy || self.enemy.enemy != self)
663         {
664                 error("Invalid warp zone detected. Killed.");
665                 return;
666         }
667
668         warpzone_warpzones_exist = 1;
669         WarpZone_SetUp(self, self.warpzone_origin, self.warpzone_angles, self.enemy.warpzone_origin, self.enemy.warpzone_angles);
670         self.touch = WarpZone_Touch;
671         self.SendFlags = 0xFFFFFF;
672         if(self.spawnflags & 1)
673         {
674                 self.think = WarpZone_Think;
675                 self.nextthink = time;
676         }
677         else
678                 self.nextthink = 0;
679 }
680
681 float warpzone_initialized;
682 //entity warpzone_first;
683 entity warpzone_position_first;
684 entity warpzone_camera_first;
685 .entity warpzone_next;
686 void spawnfunc_misc_warpzone_position(void)
687 {
688         // "target", "angles", "origin"
689         self.warpzone_next = warpzone_position_first;
690         warpzone_position_first = self;
691 }
692 void spawnfunc_trigger_warpzone_position(void)
693 {
694         spawnfunc_misc_warpzone_position();
695 }
696 void spawnfunc_trigger_warpzone(void)
697 {
698         // warp zone entities must have:
699         // "killtarget" pointing to a target_position with a direction arrow
700         //              that points AWAY from the warp zone, and that is inside
701         //              the warp zone trigger
702         // "target"     pointing to an identical warp zone at another place in
703         //              the map, with another killtarget to designate its
704         //              orientation
705
706 #ifndef WARPZONE_USE_FIXANGLE
707         // used when teleporting
708         precache_model("null");
709 #endif
710
711         if(!self.scale)
712                 self.scale = self.modelscale;
713         if(!self.scale)
714                 self.scale = 1;
715         string m;
716         m = self.model;
717         WarpZoneLib_ExactTrigger_Init();
718         if(m != "")
719         {
720                 precache_model(m);
721                 setmodel(self, m); // no precision needed
722         }
723         setorigin(self, self.origin);
724         if(self.scale)
725                 setsize(self, self.mins * self.scale, self.maxs * self.scale);
726         else
727                 setsize(self, self.mins, self.maxs);
728         self.SendEntity = WarpZone_Send;
729         self.SendFlags = 0xFFFFFF;
730         BITSET_ASSIGN(self.effects, EF_NODEPTHTEST);
731         self.warpzone_next = warpzone_first;
732         warpzone_first = self;
733 }
734 void spawnfunc_func_camera(void)
735 {
736         if(!self.scale)
737                 self.scale = self.modelscale;
738         if(!self.scale)
739                 self.scale = 1;
740         if(self.model != "")
741         {
742                 precache_model(self.model);
743                 setmodel(self, self.model); // no precision needed
744         }
745         setorigin(self, self.origin);
746         if(self.scale)
747                 setsize(self, self.mins * self.scale, self.maxs * self.scale);
748         else
749                 setsize(self, self.mins, self.maxs);
750         if(!self.solid)
751                 self.solid = SOLID_BSP;
752         else if(self.solid < 0)
753                 self.solid = SOLID_NOT;
754         self.SendEntity = WarpZone_Camera_Send;
755         self.SendFlags = 0xFFFFFF;
756         self.warpzone_next = warpzone_camera_first;
757         warpzone_camera_first = self;
758 }
759 void WarpZones_Reconnect()
760 {
761         entity e;
762         e = self;
763         for(self = warpzone_first; self; self = self.warpzone_next)
764                 WarpZone_InitStep_ClearTarget();
765         for(self = warpzone_first; self; self = self.warpzone_next)
766                 WarpZone_InitStep_FindTarget();
767         for(self = warpzone_camera_first; self; self = self.warpzone_next)
768                 WarpZoneCamera_InitStep_FindTarget();
769         for(self = warpzone_first; self; self = self.warpzone_next)
770                 WarpZone_InitStep_FinalizeTransform();
771         self = e;
772 }
773
774 void WarpZone_Think()
775 {
776         if(self.warpzone_save_origin != self.origin
777         || self.warpzone_save_angles != self.angles
778         || self.warpzone_save_eorigin != self.enemy.origin
779         || self.warpzone_save_eangles != self.enemy.angles)
780         {
781                 entity oldself;
782                 oldself = self;
783                 WarpZone_InitStep_UpdateTransform();
784                 self = self.enemy;
785                 WarpZone_InitStep_UpdateTransform();
786                 self = oldself;
787                 WarpZone_InitStep_FinalizeTransform();
788                 self = self.enemy;
789                 WarpZone_InitStep_FinalizeTransform();
790                 self = oldself;
791                 self.warpzone_save_origin = self.origin;
792                 self.warpzone_save_angles = self.angles;
793                 self.warpzone_save_eorigin = self.enemy.origin;
794                 self.warpzone_save_eangles = self.enemy.angles;
795         }
796         self.nextthink = time;
797 }
798
799 void WarpZone_StartFrame()
800 {
801         entity e;
802         if(warpzone_initialized == 0)
803         {
804                 warpzone_initialized = 1;
805                 e = self;
806                 for(self = warpzone_first; self; self = self.warpzone_next)
807                         WarpZone_InitStep_FindOriginTarget();
808                 for(self = warpzone_position_first; self; self = self.warpzone_next)
809                         WarpZonePosition_InitStep_FindTarget();
810                 for(self = warpzone_first; self; self = self.warpzone_next)
811                         WarpZone_InitStep_UpdateTransform();
812                 self = e;
813                 WarpZones_Reconnect();
814                 WarpZone_PostInitialize_Callback();
815         }
816
817         entity oldself, oldother;
818         oldself = self;
819         oldother = other;
820         for(e = world; (e = nextent(e)); )
821         {
822                 if(warpzone_warpzones_exist) { WarpZone_StoreProjectileData(e); }
823
824                 if(IS_REAL_CLIENT(e))
825                 {
826                         if(e.solid == SOLID_NOT) // not spectating?
827                         if(e.movetype == MOVETYPE_NOCLIP || e.movetype == MOVETYPE_FLY || e.movetype == MOVETYPE_FLY_WORLDONLY) // not spectating? (this is to catch observers)
828                         {
829                                 other = e; // player
830
831                                 // warpzones
832                                 if(warpzone_warpzones_exist) {
833                                 self = WarpZone_Find(e.origin + e.mins, e.origin + e.maxs);
834                                 if(self)
835                                 if(!WarpZoneLib_ExactTrigger_Touch())
836                                         if(WarpZone_PlaneDist(self, e.origin + e.view_ofs) <= 0)
837                                                 WarpZone_Teleport(self, e, -1, 0); } // NOT triggering targets by this!
838
839                                 // teleporters
840                                 self = Teleport_Find(e.origin + e.mins, e.origin + e.maxs);
841                                 if(self)
842                                 if(!WarpZoneLib_ExactTrigger_Touch())
843                                         Simple_TeleportPlayer(self, other); // NOT triggering targets by this!
844                         }
845                 }
846
847                 if(IS_NOT_A_CLIENT(e))
848                 {
849                         if(warpzone_warpzones_exist)
850                                 for (; (e = nextent(e)); )
851                                         WarpZone_StoreProjectileData(e);
852                         break;
853                 }
854         }
855         self = oldself;
856         other = oldother;
857 }
858
859 .float warpzone_reconnecting;
860 float visible_to_some_client(entity ent)
861 {
862         entity e;
863         for(e = nextent(world); !IS_NOT_A_CLIENT(e); e = nextent(e))
864                 if(IS_PLAYER(e) && IS_REAL_CLIENT(e))
865                         if(checkpvs(e.origin + e.view_ofs, ent))
866                                 return 1;
867         return 0;
868 }
869 void trigger_warpzone_reconnect_use()
870 {
871         entity e;
872         e = self;
873         // NOTE: this matches for target, not targetname, but of course
874         // targetname must be set too on the other entities
875         for(self = warpzone_first; self; self = self.warpzone_next)
876                 self.warpzone_reconnecting = ((e.target == "" || self.target == e.target) && !((e.spawnflags & 1) && (visible_to_some_client(self) || visible_to_some_client(self.enemy))));
877         for(self = warpzone_camera_first; self; self = self.warpzone_next)
878                 self.warpzone_reconnecting = ((e.target == "" || self.target == e.target) && !((e.spawnflags & 1) && visible_to_some_client(self)));
879         for(self = warpzone_first; self; self = self.warpzone_next)
880                 if(self.warpzone_reconnecting)
881                         WarpZone_InitStep_ClearTarget();
882         for(self = warpzone_first; self; self = self.warpzone_next)
883                 if(self.warpzone_reconnecting)
884                         WarpZone_InitStep_FindTarget();
885         for(self = warpzone_camera_first; self; self = self.warpzone_next)
886                 if(self.warpzone_reconnecting)
887                         WarpZoneCamera_InitStep_FindTarget();
888         for(self = warpzone_first; self; self = self.warpzone_next)
889                 if(self.warpzone_reconnecting || self.enemy.warpzone_reconnecting)
890                         WarpZone_InitStep_FinalizeTransform();
891         self = e;
892 }
893
894 void spawnfunc_trigger_warpzone_reconnect()
895 {
896         self.use = trigger_warpzone_reconnect_use;
897 }
898
899 void spawnfunc_target_warpzone_reconnect()
900 {
901         spawnfunc_trigger_warpzone_reconnect(); // both names make sense here :(
902 }
903
904 void WarpZone_PlayerPhysics_FixVAngle(void)
905 {
906 #ifndef WARPZONE_DONT_FIX_VANGLE
907         if(IS_REAL_CLIENT(self))
908         if(self.v_angle.z <= 360) // if not already adjusted
909         if(time - self.ping * 0.001 < self.warpzone_teleport_time)
910         {
911                 self.v_angle = WarpZone_TransformVAngles(self.warpzone_teleport_zone, self.v_angle);
912                 self.v_angle_z += 720; // mark as adjusted
913         }
914 #endif
915 }