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