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