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