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