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