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