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