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