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