]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/warpzone/server.qc
Merge branch 'Mario/xonoticless_fix' into 'master'
[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 void WarpZone_InitStep_FindTarget(entity this)
635 {
636         float i;
637         entity e, e2;
638
639         if(this.enemy)
640                 return;
641
642         // this way only one of the two ents needs to target
643         if(this.target != "")
644         {
645                 this.enemy = this; // so the if(!e.enemy) check also skips this, saves one IF
646
647                 e2 = NULL;
648                 for(e = NULL, i = 0; (e = find(e, targetname, this.target)); )
649                         if(!e.enemy)
650                                 if(e.classname == this.classname) // possibly non-warpzones may use the same targetname!
651                                         if(random() * ++i < 1)
652                                                 e2 = e;
653                 if(!e2)
654                 {
655                         this.enemy = NULL;
656                         error("Warpzone with non-existing target");
657                         return;
658                 }
659                 this.enemy = e2;
660                 e2.enemy = this;
661         }
662 }
663
664 void WarpZone_Think(entity this);
665 void WarpZone_InitStep_FinalizeTransform(entity this)
666 {
667         if(!this.enemy || this.enemy.enemy != this)
668         {
669                 error("Invalid warp zone detected. Killed.");
670                 return;
671         }
672
673         warpzone_warpzones_exist = 1;
674         WarpZone_SetUp(this, this.warpzone_origin, this.warpzone_angles, this.enemy.warpzone_origin, this.enemy.warpzone_angles);
675         settouch(this, WarpZone_Touch);
676         this.SendFlags = 0xFFFFFF;
677         if(this.spawnflags & 1)
678         {
679                 setthink(this, WarpZone_Think);
680                 this.nextthink = time;
681         }
682         else
683                 this.nextthink = 0;
684 }
685
686 float warpzone_initialized;
687 //entity warpzone_first;
688 entity warpzone_position_first;
689 entity warpzone_camera_first;
690 .entity warpzone_next;
691 spawnfunc(misc_warpzone_position)
692 {
693         // "target", "angles", "origin"
694         this.warpzone_next = warpzone_position_first;
695         warpzone_position_first = this;
696 }
697 spawnfunc(trigger_warpzone_position)
698 {
699         spawnfunc_misc_warpzone_position(this);
700 }
701 spawnfunc(trigger_warpzone)
702 {
703         // warp zone entities must have:
704         // "killtarget" pointing to a target_position with a direction arrow
705         //              that points AWAY from the warp zone, and that is inside
706         //              the warp zone trigger
707         // "target"     pointing to an identical warp zone at another place in
708         //              the map, with another killtarget to designate its
709         //              orientation
710
711         if(!this.scale)
712                 this.scale = this.modelscale;
713         if(!this.scale)
714                 this.scale = 1;
715         string m;
716         m = this.model;
717         WarpZoneLib_ExactTrigger_Init(this);
718         if(m != "")
719         {
720                 precache_model(m);
721                 _setmodel(this, m); // no precision needed
722         }
723         setorigin(this, this.origin);
724         if(this.scale)
725                 setsize(this, this.mins * this.scale, this.maxs * this.scale);
726         else
727                 setsize(this, this.mins, this.maxs);
728         setSendEntity(this, WarpZone_Send);
729         this.SendFlags = 0xFFFFFF;
730         BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
731         this.warpzone_next = warpzone_first;
732         warpzone_first = this;
733
734         IL_PUSH(g_warpzones, 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         if(warpzone_warpzones_exist)
808         {
809                 IL_EACH(g_projectiles, true,
810                 {
811                         WarpZone_StoreProjectileData(it);
812                 });
813         }
814                 
815
816         FOREACH_CLIENT(true,
817         {
818                 if(warpzone_warpzones_exist)
819                         WarpZone_StoreProjectileData(it); // TODO: not actually needed
820
821                 if(IS_OBSERVER(it) || it.solid == SOLID_NOT)
822                 if(IS_CLIENT(it)) // we don't care about it being a bot
823                 {
824                         // warpzones
825                         if (warpzone_warpzones_exist) {
826                                 entity e = WarpZone_Find(it.origin + it.mins, it.origin + it.maxs);
827                                 if (e)
828                                 if (!WarpZoneLib_ExactTrigger_Touch(e, it))
829                                 if (WarpZone_PlaneDist(e, it.origin + it.view_ofs) <= 0)
830                                         WarpZone_Teleport(e, it, -1, 0); // NOT triggering targets by this!
831                         }
832
833                         // teleporters
834                         if(it.teleportable)
835                         {
836                                 entity ent = Teleport_Find(it.origin + it.mins, it.origin + it.maxs);
837                                 if (ent)
838                                 if (!WarpZoneLib_ExactTrigger_Touch(ent, it))
839                                         Simple_TeleportPlayer(ent, it); // NOT triggering targets by this!
840                         }
841                 }
842         });
843 }
844
845 .float warpzone_reconnecting;
846 bool visible_to_some_client(entity ent)
847 {
848         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && checkpvs(it.origin + it.view_ofs, ent),
849         {
850                 return true;
851         });
852         return false;
853 }
854 void trigger_warpzone_reconnect_use(entity this, entity actor, entity trigger)
855 {
856         // NOTE: this matches for target, not targetname, but of course
857         // targetname must be set too on the other entities
858         for(entity e = warpzone_first; e; e = e.warpzone_next)
859                 e.warpzone_reconnecting = ((this.target == "" || e.target == this.target) && !((this.spawnflags & 1) && (visible_to_some_client(e) || visible_to_some_client(e.enemy))));
860         for(entity e = warpzone_camera_first; e; e = e.warpzone_next)
861                 e.warpzone_reconnecting = ((this.target == "" || e.target == this.target) && !((this.spawnflags & 1) && visible_to_some_client(e)));
862         for(entity e = warpzone_first; e; e = e.warpzone_next)
863                 if(e.warpzone_reconnecting)
864                         WarpZone_InitStep_ClearTarget(e);
865         for(entity e = warpzone_first; e; e = e.warpzone_next)
866                 if(e.warpzone_reconnecting)
867                         WarpZone_InitStep_FindTarget(e);
868         for(entity e = warpzone_camera_first; e; e = e.warpzone_next)
869                 if(e.warpzone_reconnecting)
870                         WarpZoneCamera_InitStep_FindTarget(e);
871         for(entity e = warpzone_first; e; e = e.warpzone_next)
872                 if(e.warpzone_reconnecting || e.enemy.warpzone_reconnecting)
873                         WarpZone_InitStep_FinalizeTransform(e);
874 }
875
876 spawnfunc(trigger_warpzone_reconnect)
877 {
878         this.use = trigger_warpzone_reconnect_use;
879 }
880
881 spawnfunc(target_warpzone_reconnect)
882 {
883         spawnfunc_trigger_warpzone_reconnect(this); // both names make sense here :(
884 }
885
886 void WarpZone_PlayerPhysics_FixVAngle(entity this)
887 {
888 #ifndef WARPZONE_DONT_FIX_VANGLE
889         if(IS_REAL_CLIENT(this))
890         if(this.v_angle.z <= 360) // if not already adjusted
891         if(time - this.ping * 0.001 < this.warpzone_teleport_time)
892         {
893                 this.v_angle = WarpZone_TransformVAngles(this.warpzone_teleport_zone, this.v_angle);
894                 this.v_angle_z += 720; // mark as adjusted
895         }
896 #endif
897 }
898
899 #endif