]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/warpzone/server.qc
Step 5: complete
[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                 setthink(ts, SUB_Remove);
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 (entity this)
194 {
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(this, 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 {
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(entity this)
439 {
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 = WarpZone_CheckProjectileImpact(this);
483                 if (f) return (f > 0);
484                 trace_dpstartcontents = save_dpstartcontents;
485                 trace_dphitcontents = save_dphitcontents;
486                 trace_dphitq3surfaceflags = save_dphitq3surfaceflags;
487                 trace_dphittexturename = save_dphittexturename;
488                 trace_allsolid = save_allsolid;
489                 trace_startsolid = save_startsolid;
490                 trace_fraction = save_fraction;
491                 trace_endpos = save_endpos;
492                 trace_plane_normal = save_plane_normal;
493                 trace_plane_dist = save_plane_dist;
494                 trace_ent = save_ent;
495                 trace_inopen = save_inopen;
496                 trace_inwater = save_inwater;
497         }
498 #endif
499
500         if(WarpZone_Projectile_Touch_ImpactFilter_Callback(this, other))
501                 return true;
502 #endif
503
504         return false;
505 }
506
507 #ifdef SVQC
508
509 void WarpZone_InitStep_FindOriginTarget(entity this)
510 {
511         if(this.killtarget != "")
512         {
513                 this.aiment = find(world, targetname, this.killtarget);
514                 if(this.aiment == world)
515                 {
516                         error("Warp zone with nonexisting killtarget");
517                         return;
518                 }
519                 this.killtarget = string_null;
520         }
521 }
522
523 void WarpZonePosition_InitStep_FindTarget(entity this)
524 {
525         if(this.target == "")
526         {
527                 error("Warp zone position with no target");
528                 return;
529         }
530         this.enemy = find(world, targetname, this.target);
531         if(this.enemy == world)
532         {
533                 error("Warp zone position with nonexisting target");
534                 return;
535         }
536         if(this.enemy.aiment)
537         {
538                 // already is positioned
539                 error("Warp zone position targeting already oriented warpzone");
540                 return;
541         }
542         this.enemy.aiment = this;
543 }
544
545 void WarpZoneCamera_Think(entity this)
546 {
547         if(this.warpzone_save_origin != this.origin
548         || this.warpzone_save_angles != this.angles
549         || this.warpzone_save_eorigin != this.enemy.origin
550         || this.warpzone_save_eangles != this.enemy.angles)
551         {
552                 WarpZone_Camera_SetUp(this, this.enemy.origin, this.enemy.angles);
553                 this.warpzone_save_origin = this.origin;
554                 this.warpzone_save_angles = this.angles;
555                 this.warpzone_save_eorigin = this.enemy.origin;
556                 this.warpzone_save_eangles = this.enemy.angles;
557         }
558         this.nextthink = time;
559 }
560
561 void WarpZoneCamera_InitStep_FindTarget(entity this)
562 {
563         entity e;
564         float i;
565         if(this.target == "")
566         {
567                 error("Camera with no target");
568                 return;
569         }
570         this.enemy = world;
571         for(e = world, i = 0; (e = find(e, targetname, this.target)); )
572                 if(random() * ++i < 1)
573                         this.enemy = e;
574         if(this.enemy == world)
575         {
576                 error("Camera with nonexisting target");
577                 return;
578         }
579         warpzone_cameras_exist = 1;
580         WarpZone_Camera_SetUp(this, this.enemy.origin, this.enemy.angles);
581         this.SendFlags = 0xFFFFFF;
582         if(this.spawnflags & 1)
583         {
584                 setthink(this, WarpZoneCamera_Think);
585                 this.nextthink = time;
586         }
587         else
588                 this.nextthink = 0;
589 }
590
591 void WarpZone_InitStep_UpdateTransform(entity this)
592 {
593         vector org, ang, norm, point;
594         float area;
595         vector tri, a, b, c, n;
596         float i_s, i_t, n_t;
597         string tex;
598
599         org = this.origin;
600         if(org == '0 0 0')
601                 org = 0.5 * (this.mins + this.maxs);
602
603         norm = point = '0 0 0';
604         area = 0;
605         for(i_s = 0; ; ++i_s)
606         {
607                 tex = getsurfacetexture(this, i_s);
608                 if (!tex)
609                         break; // this is beyond the last one
610                 if(tex == "textures/common/trigger" || tex == "trigger")
611                         continue;
612                 n_t = getsurfacenumtriangles(this, i_s);
613                 for(i_t = 0; i_t < n_t; ++i_t)
614                 {
615                         tri = getsurfacetriangle(this, i_s, i_t);
616                         a = getsurfacepoint(this, i_s, tri.x);
617                         b = getsurfacepoint(this, i_s, tri.y);
618                         c = getsurfacepoint(this, i_s, tri.z);
619                         n = cross(c - a, b - a);
620                         area = area + vlen(n);
621                         norm = norm + n;
622                         point = point + vlen(n) * (a + b + c);
623                 }
624         }
625         if(area > 0)
626         {
627                 norm = norm * (1 / area);
628                 point = point * (1 / (3 * area));
629                 if(vdist(norm, <, 0.99))
630                 {
631                         LOG_INFO("trigger_warpzone near ", vtos(this.aiment.origin), " is nonplanar. BEWARE.\n");
632                         area = 0; // no autofixing in this case
633                 }
634                 norm = normalize(norm);
635         }
636
637         ang = '0 0 0';
638         if(this.aiment)
639         {
640                 org = this.aiment.origin;
641                 ang = this.aiment.angles;
642                 if(area > 0)
643                 {
644                         org = org - ((org - point) * norm) * norm; // project to plane
645                         makevectors(ang);
646                         if(norm * v_forward < 0)
647                         {
648                                 LOG_INFO("Position target of trigger_warpzone near ", vtos(this.aiment.origin), " points into trigger_warpzone. BEWARE.\n");
649                                 norm = -1 * norm;
650                         }
651                         ang = vectoangles2(norm, v_up); // keep rotation, but turn exactly against plane
652                         ang.x = -ang.x;
653                         if(norm * v_forward < 0.99)
654                                 LOG_INFO("trigger_warpzone near ", vtos(this.aiment.origin), " has been turned to match plane orientation (", vtos(this.aiment.angles), " -> ", vtos(ang), "\n");
655                         if(vdist(org - this.aiment.origin, >, 0.5))
656                                 LOG_INFO("trigger_warpzone near ", vtos(this.aiment.origin), " has been moved to match the plane (", vtos(this.aiment.origin), " -> ", vtos(org), ").\n");
657                 }
658         }
659         else if(area > 0)
660         {
661                 org = point;
662                 ang = vectoangles(norm);
663                 ang.x = -ang.x;
664         }
665         else
666                 error("cannot infer origin/angles for this warpzone, please use a killtarget or a trigger_warpzone_position");
667
668         this.warpzone_origin = org;
669         this.warpzone_angles = ang;
670 }
671
672 void WarpZone_InitStep_ClearTarget(entity this)
673 {
674         if(this.enemy)
675                 this.enemy.enemy = world;
676         this.enemy = world;
677 }
678
679 entity warpzone_first; .entity warpzone_next;
680 void WarpZone_InitStep_FindTarget(entity this)
681 {
682         float i;
683         entity e, e2;
684
685         if(this.enemy)
686                 return;
687
688         // this way only one of the two ents needs to target
689         if(this.target != "")
690         {
691                 this.enemy = this; // so the if(!e.enemy) check also skips this, saves one IF
692
693                 e2 = world;
694                 for(e = world, i = 0; (e = find(e, targetname, this.target)); )
695                         if(!e.enemy)
696                                 if(e.classname == this.classname) // possibly non-warpzones may use the same targetname!
697                                         if(random() * ++i < 1)
698                                                 e2 = e;
699                 if(!e2)
700                 {
701                         this.enemy = world;
702                         error("Warpzone with non-existing target");
703                         return;
704                 }
705                 this.enemy = e2;
706                 e2.enemy = this;
707         }
708 }
709
710 void WarpZone_Think(entity this);
711 void WarpZone_InitStep_FinalizeTransform(entity this)
712 {
713         if(!this.enemy || this.enemy.enemy != this)
714         {
715                 error("Invalid warp zone detected. Killed.");
716                 return;
717         }
718
719         warpzone_warpzones_exist = 1;
720         WarpZone_SetUp(this, this.warpzone_origin, this.warpzone_angles, this.enemy.warpzone_origin, this.enemy.warpzone_angles);
721         settouch(this, WarpZone_Touch);
722         this.SendFlags = 0xFFFFFF;
723         if(this.spawnflags & 1)
724         {
725                 setthink(this, WarpZone_Think);
726                 this.nextthink = time;
727         }
728         else
729                 this.nextthink = 0;
730 }
731
732 float warpzone_initialized;
733 //entity warpzone_first;
734 entity warpzone_position_first;
735 entity warpzone_camera_first;
736 .entity warpzone_next;
737 spawnfunc(misc_warpzone_position)
738 {
739         // "target", "angles", "origin"
740         this.warpzone_next = warpzone_position_first;
741         warpzone_position_first = this;
742 }
743 spawnfunc(trigger_warpzone_position)
744 {
745         spawnfunc_misc_warpzone_position(this);
746 }
747 spawnfunc(trigger_warpzone)
748 {
749         // warp zone entities must have:
750         // "killtarget" pointing to a target_position with a direction arrow
751         //              that points AWAY from the warp zone, and that is inside
752         //              the warp zone trigger
753         // "target"     pointing to an identical warp zone at another place in
754         //              the map, with another killtarget to designate its
755         //              orientation
756
757         if(!this.scale)
758                 this.scale = this.modelscale;
759         if(!this.scale)
760                 this.scale = 1;
761         string m;
762         m = this.model;
763         WarpZoneLib_ExactTrigger_Init(this);
764         if(m != "")
765         {
766                 precache_model(m);
767                 _setmodel(this, m); // no precision needed
768         }
769         setorigin(this, this.origin);
770         if(this.scale)
771                 setsize(this, this.mins * this.scale, this.maxs * this.scale);
772         else
773                 setsize(this, this.mins, this.maxs);
774         this.SendEntity = SendEntity_self;
775         this.SendEntity3 = WarpZone_Send;
776         this.SendFlags = 0xFFFFFF;
777         BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
778         this.warpzone_next = warpzone_first;
779         warpzone_first = this;
780 }
781 spawnfunc(func_camera)
782 {
783         if(!this.scale)
784                 this.scale = this.modelscale;
785         if(!this.scale)
786                 this.scale = 1;
787         if(this.model != "")
788         {
789                 precache_model(this.model);
790                 _setmodel(this, this.model); // no precision needed
791         }
792         setorigin(this, this.origin);
793         if(this.scale)
794                 setsize(this, this.mins * this.scale, this.maxs * this.scale);
795         else
796                 setsize(this, this.mins, this.maxs);
797         if(!this.solid)
798                 this.solid = SOLID_BSP;
799         else if(this.solid < 0)
800                 this.solid = SOLID_NOT;
801         this.SendEntity = SendEntity_self;
802         this.SendEntity3 = WarpZone_Camera_Send;
803         this.SendFlags = 0xFFFFFF;
804         this.warpzone_next = warpzone_camera_first;
805         warpzone_camera_first = this;
806 }
807 void WarpZones_Reconnect()
808 {
809         for(entity e = warpzone_first; e; e = e.warpzone_next)
810                 WarpZone_InitStep_ClearTarget(e);
811         for(entity e = warpzone_first; e; e = e.warpzone_next)
812                 WarpZone_InitStep_FindTarget(e);
813         for(entity e = warpzone_camera_first; e; e = e.warpzone_next)
814                 WarpZoneCamera_InitStep_FindTarget(e);
815         for(entity e = warpzone_first; e; e = e.warpzone_next)
816                 WarpZone_InitStep_FinalizeTransform(e);
817 }
818
819 void WarpZone_Think(entity this)
820 {
821         if(this.warpzone_save_origin != this.origin
822         || this.warpzone_save_angles != this.angles
823         || this.warpzone_save_eorigin != this.enemy.origin
824         || this.warpzone_save_eangles != this.enemy.angles)
825         {
826                 WarpZone_InitStep_UpdateTransform(this);
827                 WarpZone_InitStep_UpdateTransform(this.enemy);
828                 WarpZone_InitStep_FinalizeTransform(this);
829                 WarpZone_InitStep_FinalizeTransform(this.enemy);
830                 this.warpzone_save_origin = this.origin;
831                 this.warpzone_save_angles = this.angles;
832                 this.warpzone_save_eorigin = this.enemy.origin;
833                 this.warpzone_save_eangles = this.enemy.angles;
834         }
835         this.nextthink = time;
836 }
837
838 void WarpZone_StartFrame()
839 {
840         if (!warpzone_initialized)
841         {
842                 warpzone_initialized = true;
843                 for(entity e = warpzone_first; e; e = e.warpzone_next)
844                         WarpZone_InitStep_FindOriginTarget(e);
845                 for(entity e = warpzone_position_first; e; e = e.warpzone_next)
846                         WarpZonePosition_InitStep_FindTarget(e);
847                 for(entity e = warpzone_first; e; e = e.warpzone_next)
848                         WarpZone_InitStep_UpdateTransform(e);
849                 WarpZones_Reconnect();
850                 WarpZone_PostInitialize_Callback();
851         }
852
853         entity oldother = other;
854
855         FOREACH_ENTITY(!is_pure(it),
856         {
857                 if(warpzone_warpzones_exist)
858                         WarpZone_StoreProjectileData(it);
859
860                 if(IS_OBSERVER(it) || it.solid == SOLID_NOT)
861                 if(IS_CLIENT(it)) // we don't care about it being a bot
862                 {
863                         other = it; // player
864
865                         // warpzones
866                         if (warpzone_warpzones_exist) {
867                                 entity e = WarpZone_Find(it.origin + it.mins, it.origin + it.maxs);
868                                 if (e)
869                                 if (!WarpZoneLib_ExactTrigger_Touch(e, other))
870                                 if (WarpZone_PlaneDist(e, it.origin + it.view_ofs) <= 0)
871                                         WarpZone_Teleport(e, it, -1, 0); // NOT triggering targets by this!
872                         }
873
874                         // teleporters
875                         if(other.teleportable)
876                         {
877                                 entity ent = Teleport_Find(it.origin + it.mins, it.origin + it.maxs);
878                                 if (ent)
879                                 if (!WarpZoneLib_ExactTrigger_Touch(ent, other))
880                                         Simple_TeleportPlayer(ent, other); // NOT triggering targets by this!
881                         }
882                 }
883         });
884         other = oldother;
885 }
886
887 .float warpzone_reconnecting;
888 bool visible_to_some_client(entity ent)
889 {
890         FOREACH_ENTITY(!IS_NOT_A_CLIENT(it), LAMBDA(
891                 if (IS_PLAYER(it) && IS_REAL_CLIENT(it) && checkpvs(it.origin + it.view_ofs, ent)) return true;
892         ));
893         return false;
894 }
895 void trigger_warpzone_reconnect_use(entity this, entity actor, entity trigger)
896 {
897         // NOTE: this matches for target, not targetname, but of course
898         // targetname must be set too on the other entities
899         for(entity e = warpzone_first; e; e = e.warpzone_next)
900                 e.warpzone_reconnecting = ((this.target == "" || e.target == this.target) && !((this.spawnflags & 1) && (visible_to_some_client(e) || visible_to_some_client(e.enemy))));
901         for(entity e = warpzone_camera_first; e; e = e.warpzone_next)
902                 e.warpzone_reconnecting = ((this.target == "" || e.target == this.target) && !((this.spawnflags & 1) && visible_to_some_client(e)));
903         for(entity e = warpzone_first; e; e = e.warpzone_next)
904                 if(e.warpzone_reconnecting)
905                         WarpZone_InitStep_ClearTarget(e);
906         for(entity e = warpzone_first; e; e = e.warpzone_next)
907                 if(e.warpzone_reconnecting)
908                         WarpZone_InitStep_FindTarget(e);
909         for(entity e = warpzone_camera_first; e; e = e.warpzone_next)
910                 if(e.warpzone_reconnecting)
911                         WarpZoneCamera_InitStep_FindTarget(e);
912         for(entity e = warpzone_first; e; e = e.warpzone_next)
913                 if(e.warpzone_reconnecting || e.enemy.warpzone_reconnecting)
914                         WarpZone_InitStep_FinalizeTransform(e);
915 }
916
917 spawnfunc(trigger_warpzone_reconnect)
918 {
919         this.use = trigger_warpzone_reconnect_use;
920 }
921
922 spawnfunc(target_warpzone_reconnect)
923 {
924         spawnfunc_trigger_warpzone_reconnect(this); // both names make sense here :(
925 }
926
927 void WarpZone_PlayerPhysics_FixVAngle(entity this)
928 {
929 #ifndef WARPZONE_DONT_FIX_VANGLE
930         if(IS_REAL_CLIENT(this))
931         if(this.v_angle.z <= 360) // if not already adjusted
932         if(time - this.ping * 0.001 < this.warpzone_teleport_time)
933         {
934                 this.v_angle = WarpZone_TransformVAngles(this.warpzone_teleport_zone, this.v_angle);
935                 this.v_angle_z += 720; // mark as adjusted
936         }
937 #endif
938 }
939
940 #endif