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