]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/warpzone/server.qc
Update default video settings
[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 <common/weapons/_all.qh>
13         #include <common/stats.qh>
14         #include <server/utils.qh>
15         #include <server/weapons/common.qh>
16 #endif
17
18 #ifdef SVQC
19 bool autocvar_sv_warpzone_allow_selftarget;
20 #endif
21
22 #ifdef WARPZONELIB_KEEPDEBUG
23 #define WARPZONELIB_REMOVEHACK
24 #endif
25
26 // for think function
27 .vector warpzone_save_origin;
28 .vector warpzone_save_angles;
29 .vector warpzone_save_eorigin;
30 .vector warpzone_save_eangles;
31
32 // for all entities
33 .vector warpzone_oldorigin, warpzone_oldvelocity, warpzone_oldangles;
34 .float warpzone_teleport_time;
35 .float warpzone_teleport_finishtime;
36 .entity warpzone_teleport_zone;
37
38 #define WarpZone_StoreProjectileData(e_) MACRO_BEGIN \
39         entity e = e_; \
40         e.warpzone_oldorigin = e.origin; \
41         e.warpzone_oldvelocity = e.velocity; \
42         e.warpzone_oldangles = e.angles; \
43         MACRO_END
44
45 void WarpZone_TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity)
46 {
47 #ifdef SVQC
48         player.lastteleport_origin = player.origin;
49         player.lastteleporttime = time;
50 #endif
51         setorigin(player, to); // NOTE: this also aborts the move, when this is called by touch
52         player.angles = to_angles;
53 #ifdef SVQC
54         player.oldorigin = to; // for DP's unsticking
55         player.fixangle = true;
56         if (IS_BOT_CLIENT(player))
57         {
58                 // FIXME find a way to smooth view's angles change for bots too
59                 player.v_angle = player.angles;
60                 bot_aim_reset(player);
61         }
62 #endif
63         player.velocity = to_velocity;
64
65         BITXOR_ASSIGN(player.effects, EF_TELEPORT_BIT);
66
67         if(IS_PLAYER(player))
68                 BITCLR_ASSIGN(player.flags, FL_ONGROUND);
69
70         WarpZone_PostTeleportPlayer_Callback(player);
71 }
72
73 #ifdef SVQC
74 bool WarpZone_Teleported_Send(entity this, entity to, int sf)
75 {
76         WriteHeader(MSG_ENTITY, ENT_CLIENT_WARPZONE_TELEPORTED);
77         WriteVector(MSG_ENTITY, this.angles);
78         return true;
79 }
80 #endif
81
82 float WarpZone_Teleport(entity wz, entity player, float f0, float f1)
83 {
84         vector o0, a0, v0, o1, a1, v1, o10;
85
86         o0 = player.origin + player.view_ofs;
87         v0 = player.velocity;
88         a0 = player.angles;
89
90         o10 = o1 = WarpZone_TransformOrigin(wz, o0);
91         v1 = WarpZone_TransformVelocity(wz, v0);
92         if (!IS_NOT_A_CLIENT(player))
93                 a1 = WarpZone_TransformVAngles(wz, PHYS_INPUT_ANGLES(player));
94         else
95                 a1 = WarpZone_TransformAngles(wz, a0);
96
97         if(f0 != 0 || f1 != 0)
98         {
99                 // retry last move but behind the warpzone!
100                 // we must first go back as far as we can, then forward again, to not cause double touch events!
101
102                 tracebox(o1 - player.view_ofs + v1 * frametime * f1, player.mins, player.maxs, o1 - player.view_ofs + v1 * frametime * f0, MOVE_WORLDONLY, player);
103                 {
104                         entity own;
105                         own = player.owner;
106                         player.owner = NULL;
107                         tracebox(trace_endpos, player.mins, player.maxs, o1 - player.view_ofs + v1 * frametime * f1, MOVE_NORMAL, player); // this should get us through the warpzone
108                         player.owner = own;
109                 }
110                 o1 = trace_endpos + player.view_ofs;
111
112                 float d, dv, md;
113                 md = max(vlen(player.mins), vlen(player.maxs));
114                 d = WarpZone_TargetPlaneDist(wz, o1);
115                 dv = WarpZone_TargetPlaneDist(wz, v1);
116                 if(d < 0)
117                         o1 = o1 - v1 * (d / dv);
118         }
119
120         // put them out of solid
121         tracebox(o1 - player.view_ofs, player.mins, player.maxs, o1 - player.view_ofs, MOVE_NOMONSTERS, player);
122         if(trace_startsolid)
123         {
124                 setorigin(player, o1 - player.view_ofs);
125                 if(WarpZoneLib_MoveOutOfSolid(player))
126                 {
127                         o1 = player.origin + player.view_ofs;
128                         setorigin(player, o0 - player.view_ofs);
129                 }
130                 else
131                 {
132                         LOG_INFO("would have to put player in solid, won't do that");
133                         setorigin(player, o0 - player.view_ofs);
134                         return 0;
135                 }
136         }
137
138         // do the teleport
139         WarpZone_RefSys_Add(player, wz);
140         WarpZone_TeleportPlayer(wz, player, o1 - player.view_ofs, a1, v1);
141         WarpZone_StoreProjectileData(player);
142         player.warpzone_teleport_time = time;
143         player.warpzone_teleport_finishtime = time;
144         player.warpzone_teleport_zone = wz;
145
146 #ifdef SVQC
147         // prevent further teleports back
148         float dt = (o1 - o10) * v1 * (1 / (v1 * v1));
149         if(dt < PHYS_INPUT_FRAMETIME)
150                 player.warpzone_teleport_finishtime += PHYS_INPUT_FRAMETIME - dt;
151 #endif
152
153 #ifndef WARPZONE_USE_FIXANGLE
154         #ifdef SVQC
155         if(IS_VEHICLE(player) && player.owner)
156                 player = player.owner; // hax
157         if(IS_PLAYER(player))
158         {
159                 // instead of fixangle, send the transform to the client for smoother operation
160                 player.fixangle = false;
161
162                 entity ts = new(warpzone_teleported);
163                 setmodel(ts, MDL_Null);
164                 setSendEntity(ts, WarpZone_Teleported_Send);
165                 ts.SendFlags = 0xFFFFFF;
166                 ts.drawonlytoclient = player;
167                 setthink(ts, SUB_Remove);
168                 ts.nextthink = time + 1;
169                 ts.owner = player;
170                 ts.enemy = wz;
171                 ts.effects = EF_NODEPTHTEST;
172                 ts.angles = wz.warpzone_transform;
173         }
174         #elif defined(CSQC)
175         setproperty(VF_CL_VIEWANGLES, WarpZone_TransformVAngles(wz, getpropertyvec(VF_CL_VIEWANGLES)));
176         //if(checkextension("DP_CSQC_ROTATEMOVES"))
177                 //CL_RotateMoves(wz.warpzone_transform);
178         #endif
179 #endif
180
181         return 1;
182 }
183
184 void WarpZone_Touch(entity this, entity toucher)
185 {
186         if(toucher.classname == "trigger_warpzone")
187                 return;
188
189         if(time <= toucher.warpzone_teleport_finishtime) // already teleported this frame
190                 return;
191
192         // FIXME needs a better check to know what is safe to teleport and what not
193         if(toucher.move_movetype == MOVETYPE_NONE || toucher.move_movetype == MOVETYPE_FOLLOW || toucher.tag_entity)
194                 return;
195
196         if(WarpZone_PlaneDist(this, toucher.origin + toucher.view_ofs) >= 0) // wrong side of the trigger_warpzone (don't teleport yet)
197                 return;
198
199         EXACTTRIGGER_TOUCH(this, toucher);
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
706         WarpZoneLib_ExactTrigger_Init(this, false);
707
708         setSendEntity(this, WarpZone_Send);
709         this.SendFlags = 0xFFFFFF;
710         BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
711         this.warpzone_next = warpzone_first;
712         warpzone_first = this;
713
714         IL_PUSH(g_warpzones, this);
715 }
716 spawnfunc(func_camera)
717 {
718         if(!this.scale)
719                 this.scale = this.modelscale;
720         if(!this.scale)
721                 this.scale = 1;
722         if(this.model != "")
723         {
724                 precache_model(this.model);
725                 _setmodel(this, this.model); // 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         if(!this.solid)
733                 this.solid = SOLID_BSP;
734         else if(this.solid < 0)
735                 this.solid = SOLID_NOT;
736         setSendEntity(this, WarpZone_Camera_Send);
737         this.SendFlags = 0xFFFFFF;
738         this.warpzone_next = warpzone_camera_first;
739         warpzone_camera_first = this;
740 }
741 void WarpZones_Reconnect()
742 {
743         for(entity e = warpzone_first; e; e = e.warpzone_next)
744                 WarpZone_InitStep_ClearTarget(e);
745         for(entity e = warpzone_first; e; e = e.warpzone_next)
746                 WarpZone_InitStep_FindTarget(e);
747         for(entity e = warpzone_camera_first; e; e = e.warpzone_next)
748                 WarpZoneCamera_InitStep_FindTarget(e);
749         for(entity e = warpzone_first; e; e = e.warpzone_next)
750                 WarpZone_InitStep_FinalizeTransform(e);
751 }
752
753 void WarpZone_Think(entity this)
754 {
755         if(this.warpzone_save_origin != this.origin
756         || this.warpzone_save_angles != this.angles
757         || this.warpzone_save_eorigin != this.enemy.origin
758         || this.warpzone_save_eangles != this.enemy.angles)
759         {
760                 WarpZone_InitStep_UpdateTransform(this);
761                 WarpZone_InitStep_UpdateTransform(this.enemy);
762                 WarpZone_InitStep_FinalizeTransform(this);
763                 WarpZone_InitStep_FinalizeTransform(this.enemy);
764                 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         this.nextthink = time;
770 }
771
772 void WarpZone_StartFrame()
773 {
774         if (!warpzone_initialized)
775         {
776                 warpzone_initialized = true;
777                 for(entity e = warpzone_first; e; e = e.warpzone_next)
778                         WarpZone_InitStep_FindOriginTarget(e);
779                 for(entity e = warpzone_position_first; e; e = e.warpzone_next)
780                         WarpZonePosition_InitStep_FindTarget(e);
781                 for(entity e = warpzone_first; e; e = e.warpzone_next)
782                         WarpZone_InitStep_UpdateTransform(e);
783                 WarpZones_Reconnect();
784                 WarpZone_PostInitialize_Callback();
785         }
786
787         if(warpzone_warpzones_exist)
788         {
789                 IL_EACH(g_projectiles, true,
790                 {
791                         WarpZone_StoreProjectileData(it);
792                 });
793         }
794
795
796         FOREACH_CLIENT(true,
797         {
798                 if(warpzone_warpzones_exist)
799                         WarpZone_StoreProjectileData(it); // TODO: not actually needed
800
801                 if((IS_OBSERVER(it) || it.solid == SOLID_NOT))
802                 {
803                         // warpzones
804                         if (warpzone_warpzones_exist) {
805                                 entity e = WarpZone_Find(it.origin + it.mins, it.origin + it.maxs);
806                                 if (e)
807                                 if (WarpZoneLib_ExactTrigger_Touch(e, it, false))
808                                 if (WarpZone_PlaneDist(e, it.origin + it.view_ofs) <= 0)
809                                         WarpZone_Teleport(e, it, -1, 0); // NOT triggering targets by this!
810                         }
811
812                         // teleporters
813                         if(it.teleportable)
814                         {
815                                 entity ent = Teleport_Find(it.origin + it.mins, it.origin + it.maxs);
816                                 if (ent)
817                                 if (WarpZoneLib_ExactTrigger_Touch(ent, it, false))
818                                         Simple_TeleportPlayer(ent, it); // NOT triggering targets by this!
819                         }
820                 }
821         });
822 }
823
824 .float warpzone_reconnecting;
825 bool visible_to_some_client(entity ent)
826 {
827         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && checkpvs(it.origin + it.view_ofs, ent),
828         {
829                 return true;
830         });
831         return false;
832 }
833 void trigger_warpzone_reconnect_use(entity this, entity actor, entity trigger)
834 {
835         // NOTE: this matches for target, not targetname, but of course
836         // targetname must be set too on the other entities
837         for(entity e = warpzone_first; e; e = e.warpzone_next)
838                 e.warpzone_reconnecting = ((this.target == "" || e.target == this.target) && !((this.spawnflags & 1) && (visible_to_some_client(e) || visible_to_some_client(e.enemy))));
839         for(entity e = warpzone_camera_first; e; e = e.warpzone_next)
840                 e.warpzone_reconnecting = ((this.target == "" || e.target == this.target) && !((this.spawnflags & 1) && visible_to_some_client(e)));
841         for(entity e = warpzone_first; e; e = e.warpzone_next)
842                 if(e.warpzone_reconnecting)
843                         WarpZone_InitStep_ClearTarget(e);
844         for(entity e = warpzone_first; e; e = e.warpzone_next)
845                 if(e.warpzone_reconnecting)
846                         WarpZone_InitStep_FindTarget(e);
847         for(entity e = warpzone_camera_first; e; e = e.warpzone_next)
848                 if(e.warpzone_reconnecting)
849                         WarpZoneCamera_InitStep_FindTarget(e);
850         for(entity e = warpzone_first; e; e = e.warpzone_next)
851                 if(e.warpzone_reconnecting || e.enemy.warpzone_reconnecting)
852                         WarpZone_InitStep_FinalizeTransform(e);
853 }
854
855 spawnfunc(trigger_warpzone_reconnect)
856 {
857         this.use = trigger_warpzone_reconnect_use;
858 }
859
860 spawnfunc(target_warpzone_reconnect)
861 {
862         spawnfunc_trigger_warpzone_reconnect(this); // both names make sense here :(
863 }
864
865 void WarpZone_PlayerPhysics_FixVAngle(entity this)
866 {
867 #ifndef WARPZONE_DONT_FIX_VANGLE
868         if(IS_REAL_CLIENT(this))
869         if(this.v_angle.z <= 360) // if not already adjusted
870         if(time - CS(this).ping * 0.001 < this.warpzone_teleport_time)
871         {
872                 this.v_angle = WarpZone_TransformVAngles(this.warpzone_teleport_zone, this.v_angle);
873                 this.v_angle_z += 720; // mark as adjusted
874         }
875 #endif
876 }
877
878 #endif