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