Merge branch 'master' into Mario/vehicles
[xonotic/xonotic-data.pk3dir.git] / qcsrc / warpzonelib / 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 "../dpdefs/dpextensions.qh"
11         #include "../dpdefs/progsdefs.qh"
12         #include "../server/command/common.qh"
13         #include "../server/constants.qh"
14         #include "../server/defs.qh"
15 #endif
16
17 #ifdef WARPZONELIB_KEEPDEBUG
18 #define WARPZONELIB_REMOVEHACK
19 #endif
20
21 // for think function
22 .vector warpzone_save_origin;
23 .vector warpzone_save_angles;
24 .vector warpzone_save_eorigin;
25 .vector warpzone_save_eangles;
26
27 // for all entities
28 .vector warpzone_oldorigin, warpzone_oldvelocity, warpzone_oldangles;
29 .float warpzone_teleport_time;
30 .float warpzone_teleport_finishtime;
31 .entity warpzone_teleport_zone;
32
33 void WarpZone_StoreProjectileData(entity e)
34 {
35         e.warpzone_oldorigin = e.origin;
36         e.warpzone_oldvelocity = e.velocity;
37         e.warpzone_oldangles = e.angles;
38 }
39
40 void WarpZone_TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity)
41 {
42         setorigin (player, to); // NOTE: this also aborts the move, when this is called by touch
43         player.oldorigin = to; // for DP's unsticking
44         player.angles = to_angles;
45         player.fixangle = true;
46         player.velocity = to_velocity;
47
48         BITXOR_ASSIGN(player.effects, EF_TELEPORT_BIT);
49
50         if(IS_PLAYER(player))
51                 BITCLR_ASSIGN(player.flags, FL_ONGROUND);
52
53         WarpZone_PostTeleportPlayer_Callback(player);
54 }
55
56 bool WarpZone_Teleported_Send(entity to, int sf)
57 {
58         WriteByte(MSG_ENTITY, ENT_CLIENT_WARPZONE_TELEPORTED);
59         WriteCoord(MSG_ENTITY, self.angles.x);
60         WriteCoord(MSG_ENTITY, self.angles.y);
61         WriteCoord(MSG_ENTITY, self.angles.z);
62         return true;
63 }
64
65 float WarpZone_Teleport(entity wz, entity player, float f0, float f1)
66 {
67         vector o0, a0, v0, o1, a1, v1, o10;
68
69         o0 = player.origin + player.view_ofs;
70         v0 = player.velocity;
71         a0 = player.angles;
72
73         o10 = o1 = WarpZone_TransformOrigin(wz, o0);
74         v1 = WarpZone_TransformVelocity(wz, v0);
75         if (!IS_NOT_A_CLIENT(player))
76                 a1 = WarpZone_TransformVAngles(wz, player.v_angle);
77         else
78                 a1 = WarpZone_TransformAngles(wz, a0);
79
80         if(f0 != 0 || f1 != 0)
81         {
82                 // retry last move but behind the warpzone!
83                 // we must first go back as far as we can, then forward again, to not cause double touch events!
84
85                 tracebox(o1 - player.view_ofs + v1 * frametime * f1, player.mins, player.maxs, o1 - player.view_ofs + v1 * frametime * f0, MOVE_WORLDONLY, player);
86                 {
87                         entity own;
88                         own = player.owner;
89                         player.owner = world;
90                         tracebox(trace_endpos, player.mins, player.maxs, o1 - player.view_ofs + v1 * frametime * f1, MOVE_NORMAL, player); // this should get us through the warpzone
91                         player.owner = own;
92                 }
93                 o1 = trace_endpos + player.view_ofs;
94
95                 float d, dv, md;
96                 md = max(vlen(player.mins), vlen(player.maxs));
97                 d = WarpZone_TargetPlaneDist(wz, o1);
98                 dv = WarpZone_TargetPlaneDist(wz, v1);
99                 if(d < 0)
100                         o1 = o1 - v1 * (d / dv);
101         }
102
103         // put him out of solid
104         tracebox(o1 - player.view_ofs, player.mins, player.maxs, o1 - player.view_ofs, MOVE_NOMONSTERS, player);
105         if(trace_startsolid)
106         {
107                 setorigin(player, o1 - player.view_ofs);
108                 if(WarpZoneLib_MoveOutOfSolid(player))
109                 {
110                         o1 = player.origin + player.view_ofs;
111                         setorigin(player, o0 - player.view_ofs);
112                 }
113                 else
114                 {
115                         print("would have to put player in solid, won't do that\n");
116                         setorigin(player, o0 - player.view_ofs);
117                         return 0;
118                 }
119         }
120
121         // do the teleport
122         WarpZone_RefSys_Add(player, wz);
123         WarpZone_TeleportPlayer(wz, player, o1 - player.view_ofs, a1, v1);
124         WarpZone_StoreProjectileData(player);
125         player.warpzone_teleport_time = time;
126         player.warpzone_teleport_finishtime = time;
127         player.warpzone_teleport_zone = wz;
128
129         // prevent further teleports back
130         float dt = (o1 - o10) * v1 * (1 / (v1 * v1));
131         if(dt < sys_frametime)
132                 player.warpzone_teleport_finishtime += sys_frametime - dt;
133
134 #ifndef WARPZONE_USE_FIXANGLE
135         if(IS_VEHICLE(player) && player.owner)
136                 player = player.owner; // hax
137         if(IS_PLAYER(player))
138         {
139                 // instead of fixangle, send the transform to the client for smoother operation
140                 player.fixangle = false;
141
142                 entity ts = spawn();
143                 setmodel(ts, "null");
144                 ts.SendEntity = WarpZone_Teleported_Send;
145                 ts.SendFlags = 0xFFFFFF;
146                 ts.drawonlytoclient = player;
147                 ts.think = SUB_Remove;
148                 ts.nextthink = time + 1;
149                 ts.owner = player;
150                 ts.enemy = wz;
151                 ts.effects = EF_NODEPTHTEST;
152                 ts.classname = "warpzone_teleported";
153                 ts.angles = wz.warpzone_transform;
154         }
155 #endif
156
157         return 1;
158 }
159
160 void WarpZone_Touch (void)
161 {
162         entity oldself;
163
164         if(other.classname == "trigger_warpzone")
165                 return;
166
167         if(time <= other.warpzone_teleport_finishtime) // already teleported this frame
168                 return;
169
170         // FIXME needs a better check to know what is safe to teleport and what not
171         if(other.movetype == MOVETYPE_NONE || other.movetype == MOVETYPE_FOLLOW || other.tag_entity)
172                 return;
173
174         if(WarpZoneLib_ExactTrigger_Touch())
175                 return;
176
177         if(WarpZone_PlaneDist(self, other.origin + other.view_ofs) >= 0) // wrong side of the trigger_warpzone (don't teleport yet)
178                 return;
179
180         float f;
181         // number of frames we need to go back:
182         //   dist = 16*sqrt(2) qu
183         //   dist ~ 24 qu
184         //   24 qu = v*t
185         //   24 qu = v*frametime*n
186         //       n = 24 qu/(v*frametime)
187         // for clients go only one frame though, may be too irritating otherwise
188         // but max 0.25 sec = 0.25/frametime frames
189         //       24/(0.25/frametime)
190         //       96*frametime
191         float d;
192         d = 24 + max(vlen(other.mins), vlen(other.maxs));
193         if(IS_NOT_A_CLIENT(other))
194                 f = -d / bound(frametime * d * 1, frametime * vlen(other.velocity), d);
195         else
196                 f = -1;
197         if(WarpZone_Teleport(self, other, f, 0))
198         {
199                 string save1, save2;
200                 activator = other;
201
202                 save1 = self.target; self.target = string_null;
203                 save2 = self.target3; self.target3 = string_null;
204                 SUB_UseTargets();
205                 if (!self.target) self.target = save1;
206                 if (!self.target3) self.target3 = save2;
207
208                 oldself = self;
209                 self = self.enemy;
210                 save1 = self.target; self.target = string_null;
211                 save2 = self.target2; self.target2 = string_null;
212                 SUB_UseTargets();
213                 if (!self.target) self.target = save1;
214                 if (!self.target2) self.target2 = save2;
215                 self = oldself;
216         }
217         else
218         {
219                 dprint("WARPZONE FAIL AHAHAHAHAH))\n");
220         }
221 }
222
223 bool WarpZone_Send(entity to, int sendflags)
224 {
225         WriteByte(MSG_ENTITY, ENT_CLIENT_WARPZONE);
226
227         // we must send this flag for clientside to match properly too
228         int f = 0;
229         if(self.warpzone_isboxy)
230                 BITSET_ASSIGN(f, 1);
231         if(self.warpzone_fadestart)
232                 BITSET_ASSIGN(f, 2);
233         if(self.origin != '0 0 0')
234                 BITSET_ASSIGN(f, 4);
235         WriteByte(MSG_ENTITY, f);
236
237         // we need THESE to render the warpzone (and cull properly)...
238         if(f & 4)
239         {
240                 WriteCoord(MSG_ENTITY, self.origin.x);
241                 WriteCoord(MSG_ENTITY, self.origin.y);
242                 WriteCoord(MSG_ENTITY, self.origin.z);
243         }
244
245         WriteShort(MSG_ENTITY, self.modelindex);
246         WriteCoord(MSG_ENTITY, self.mins.x);
247         WriteCoord(MSG_ENTITY, self.mins.y);
248         WriteCoord(MSG_ENTITY, self.mins.z);
249         WriteCoord(MSG_ENTITY, self.maxs.x);
250         WriteCoord(MSG_ENTITY, self.maxs.y);
251         WriteCoord(MSG_ENTITY, self.maxs.z);
252         WriteByte(MSG_ENTITY, bound(1, self.scale * 16, 255));
253
254         // we need THESE to calculate the proper transform
255         WriteCoord(MSG_ENTITY, self.warpzone_origin.x);
256         WriteCoord(MSG_ENTITY, self.warpzone_origin.y);
257         WriteCoord(MSG_ENTITY, self.warpzone_origin.z);
258         WriteCoord(MSG_ENTITY, self.warpzone_angles.x);
259         WriteCoord(MSG_ENTITY, self.warpzone_angles.y);
260         WriteCoord(MSG_ENTITY, self.warpzone_angles.z);
261         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin.x);
262         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin.y);
263         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin.z);
264         WriteCoord(MSG_ENTITY, self.warpzone_targetangles.x);
265         WriteCoord(MSG_ENTITY, self.warpzone_targetangles.y);
266         WriteCoord(MSG_ENTITY, self.warpzone_targetangles.z);
267
268         if(f & 2)
269         {
270                 WriteShort(MSG_ENTITY, self.warpzone_fadestart);
271                 WriteShort(MSG_ENTITY, self.warpzone_fadeend);
272         }
273
274         return true;
275 }
276
277 bool WarpZone_Camera_Send(entity to, int sendflags)
278 {
279         int f = 0;
280         WriteByte(MSG_ENTITY, ENT_CLIENT_WARPZONE_CAMERA);
281
282         if(self.warpzone_fadestart)
283                 BITSET_ASSIGN(f, 2);
284         if(self.origin != '0 0 0')
285                 BITSET_ASSIGN(f, 4);
286         WriteByte(MSG_ENTITY, f);
287
288         // we need THESE to render the warpzone (and cull properly)...
289         if(f & 4)
290         {
291                 WriteCoord(MSG_ENTITY, self.origin.x);
292                 WriteCoord(MSG_ENTITY, self.origin.y);
293                 WriteCoord(MSG_ENTITY, self.origin.z);
294         }
295
296         WriteShort(MSG_ENTITY, self.modelindex);
297         WriteCoord(MSG_ENTITY, self.mins.x);
298         WriteCoord(MSG_ENTITY, self.mins.y);
299         WriteCoord(MSG_ENTITY, self.mins.z);
300         WriteCoord(MSG_ENTITY, self.maxs.x);
301         WriteCoord(MSG_ENTITY, self.maxs.y);
302         WriteCoord(MSG_ENTITY, self.maxs.z);
303         WriteByte(MSG_ENTITY, bound(1, self.scale * 16, 255));
304
305         // we need THESE to calculate the proper transform
306         WriteCoord(MSG_ENTITY, self.enemy.origin.x);
307         WriteCoord(MSG_ENTITY, self.enemy.origin.y);
308         WriteCoord(MSG_ENTITY, self.enemy.origin.z);
309         WriteCoord(MSG_ENTITY, self.enemy.angles.x);
310         WriteCoord(MSG_ENTITY, self.enemy.angles.y);
311         WriteCoord(MSG_ENTITY, self.enemy.angles.z);
312
313         if(f & 2)
314         {
315                 WriteShort(MSG_ENTITY, self.warpzone_fadestart);
316                 WriteShort(MSG_ENTITY, self.warpzone_fadeend);
317         }
318
319         return true;
320 }
321
322 #ifdef WARPZONELIB_KEEPDEBUG
323 float WarpZone_CheckProjectileImpact(entity player)
324 {
325         vector o0, v0;
326
327         o0 = player.origin + player.view_ofs;
328         v0 = player.velocity;
329
330         // if we teleported shortly before, abort
331         if(time <= player.warpzone_teleport_finishtime + 0.1)
332                 return 0;
333
334         // if player hit a warpzone, abort
335         entity wz;
336         wz = WarpZone_Find(o0 + player.mins, o0 + player.maxs);
337         if(!wz)
338                 return 0;
339
340 #ifdef WARPZONELIB_REMOVEHACK
341         print("impactfilter found something - and it no longer gets handled correctly - please tell divVerent whether anything behaves broken now\n");
342 #else
343         print("impactfilter found something - and it even gets handled correctly - please tell divVerent that this code apparently gets triggered again\n");
344 #endif
345         print("Entity type: ", player.classname, "\n");
346         print("Origin: ", vtos(player.origin), "\n");
347         print("Velocity: ", vtos(player.velocity), "\n");
348
349 #ifdef WARPZONELIB_REMOVEHACK
350         return 0;
351 #else
352         // retry previous move
353         setorigin(player, player.warpzone_oldorigin);
354         player.velocity = player.warpzone_oldvelocity;
355         if(WarpZone_Teleport(wz, player, 0, 1))
356         {
357                 entity oldself;
358                 string save1, save2;
359
360                 oldself = self;
361                 self = wz;
362                 other = player;
363                 activator = player;
364
365                 save1 = self.target; self.target = string_null;
366                 save2 = self.target3; self.target3 = string_null;
367                 SUB_UseTargets();
368                 if (!self.target) self.target = save1;
369                 if (!self.target3) self.target3 = save2;
370
371                 self = self.enemy;
372                 save1 = self.target; self.target = string_null;
373                 save2 = self.target2; self.target2 = string_null;
374                 SUB_UseTargets();
375                 if (!self.target) self.target = save1;
376                 if (!self.target2) self.target2 = save2;
377                 self = oldself;
378         }
379         else
380         {
381                 setorigin(player, o0 - player.view_ofs);
382                 player.velocity = v0;
383         }
384
385         return +1;
386 #endif
387 }
388 #endif
389
390 float WarpZone_Projectile_Touch()
391 {
392         if(other.classname == "trigger_warpzone")
393                 return true;
394
395         // no further impacts if we teleported this frame!
396         // this is because even if we did teleport, the engine still may raise
397         // touch events for the previous location
398         // engine now aborts moves on teleport, so this SHOULD not happen any more
399         // but if this is called from TouchAreaGrid of the projectile moving,
400         // then this won't do
401         if(time == self.warpzone_teleport_time)
402                 return true;
403
404 #ifdef WARPZONELIB_KEEPDEBUG
405         // this SEEMS to not happen at the moment, but if it did, it would be more reliable
406         {
407                 float save_dpstartcontents;
408                 float save_dphitcontents;
409                 float save_dphitq3surfaceflags;
410                 string save_dphittexturename;
411                 float save_allsolid;
412                 float save_startsolid;
413                 float save_fraction;
414                 vector save_endpos;
415                 vector save_plane_normal;
416                 float save_plane_dist;
417                 entity save_ent;
418                 float save_inopen;
419                 float save_inwater;
420                 save_dpstartcontents = trace_dpstartcontents;
421                 save_dphitcontents = trace_dphitcontents;
422                 save_dphitq3surfaceflags = trace_dphitq3surfaceflags;
423                 save_dphittexturename = trace_dphittexturename;
424                 save_allsolid = trace_allsolid;
425                 save_startsolid = trace_startsolid;
426                 save_fraction = trace_fraction;
427                 save_endpos = trace_endpos;
428                 save_plane_normal = trace_plane_normal;
429                 save_plane_dist = trace_plane_dist;
430                 save_ent = trace_ent;
431                 save_inopen = trace_inopen;
432                 save_inwater = trace_inwater;
433                 float f;
434                 if((f = WarpZone_CheckProjectileImpact(self)) != 0)
435                         return (f > 0);
436                 trace_dpstartcontents = save_dpstartcontents;
437                 trace_dphitcontents = save_dphitcontents;
438                 trace_dphitq3surfaceflags = save_dphitq3surfaceflags;
439                 trace_dphittexturename = save_dphittexturename;
440                 trace_allsolid = save_allsolid;
441                 trace_startsolid = save_startsolid;
442                 trace_fraction = save_fraction;
443                 trace_endpos = save_endpos;
444                 trace_plane_normal = save_plane_normal;
445                 trace_plane_dist = save_plane_dist;
446                 trace_ent = save_ent;
447                 trace_inopen = save_inopen;
448                 trace_inwater = save_inwater;
449         }
450 #endif
451
452         if(WarpZone_Projectile_Touch_ImpactFilter_Callback())
453                 return true;
454
455         return false;
456 }
457
458 void WarpZone_InitStep_FindOriginTarget()
459 {
460         if(self.killtarget != "")
461         {
462                 self.aiment = find(world, targetname, self.killtarget);
463                 if(self.aiment == world)
464                 {
465                         error("Warp zone with nonexisting killtarget");
466                         return;
467                 }
468                 self.killtarget = string_null;
469         }
470 }
471
472 void WarpZonePosition_InitStep_FindTarget()
473 {
474         if(self.target == "")
475         {
476                 error("Warp zone position with no target");
477                 return;
478         }
479         self.enemy = find(world, targetname, self.target);
480         if(self.enemy == world)
481         {
482                 error("Warp zone position with nonexisting target");
483                 return;
484         }
485         if(self.enemy.aiment)
486         {
487                 // already is positioned
488                 error("Warp zone position targeting already oriented warpzone");
489                 return;
490         }
491         self.enemy.aiment = self;
492 }
493
494 void WarpZoneCamera_Think(void)
495 {
496         if(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                 WarpZone_Camera_SetUp(self, self.enemy.origin, self.enemy.angles);
502                 self.warpzone_save_origin = self.origin;
503                 self.warpzone_save_angles = self.angles;
504                 self.warpzone_save_eorigin = self.enemy.origin;
505                 self.warpzone_save_eangles = self.enemy.angles;
506         }
507         self.nextthink = time;
508 }
509
510 void WarpZoneCamera_InitStep_FindTarget()
511 {
512         entity e;
513         float i;
514         if(self.target == "")
515         {
516                 error("Camera with no target");
517                 return;
518         }
519         self.enemy = world;
520         for(e = world, i = 0; (e = find(e, targetname, self.target)); )
521                 if(random() * ++i < 1)
522                         self.enemy = e;
523         if(self.enemy == world)
524         {
525                 error("Camera with nonexisting target");
526                 return;
527         }
528         warpzone_cameras_exist = 1;
529         WarpZone_Camera_SetUp(self, self.enemy.origin, self.enemy.angles);
530         self.SendFlags = 0xFFFFFF;
531         if(self.spawnflags & 1)
532         {
533                 self.think = WarpZoneCamera_Think;
534                 self.nextthink = time;
535         }
536         else
537                 self.nextthink = 0;
538 }
539
540 void WarpZone_InitStep_UpdateTransform()
541 {
542         vector org, ang, norm, point;
543         float area;
544         vector tri, a, b, c, n;
545         float i_s, i_t, n_t;
546         string tex;
547
548         org = self.origin;
549         if(org == '0 0 0')
550                 org = 0.5 * (self.mins + self.maxs);
551
552         norm = point = '0 0 0';
553         area = 0;
554         for(i_s = 0; ; ++i_s)
555         {
556                 tex = getsurfacetexture(self, i_s);
557                 if (!tex)
558                         break; // this is beyond the last one
559                 if(tex == "textures/common/trigger" || tex == "trigger")
560                         continue;
561                 n_t = getsurfacenumtriangles(self, i_s);
562                 for(i_t = 0; i_t < n_t; ++i_t)
563                 {
564                         tri = getsurfacetriangle(self, i_s, i_t);
565                         a = getsurfacepoint(self, i_s, tri.x);
566                         b = getsurfacepoint(self, i_s, tri.y);
567                         c = getsurfacepoint(self, i_s, tri.z);
568                         n = cross(c - a, b - a);
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 }