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