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