3292085805d85ac832d1dc96664798ab450a61ae
[xonotic/xonotic-data.pk3dir.git] / qcsrc / warpzonelib / server.qc
1 .vector warpzone_oldorigin, warpzone_oldvelocity, warpzone_oldangles;
2 .float warpzone_teleport_time;
3
4 void WarpZone_StoreProjectileData(entity e)
5 {
6         e.warpzone_oldorigin = e.origin;
7         e.warpzone_oldvelocity = e.velocity;
8         e.warpzone_oldangles = e.angles;
9 }
10
11 void WarpZone_TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity)
12 {
13         vector from;
14
15         makevectors (to_angles);
16
17         from = player.origin;
18         setorigin (player, to);
19         player.oldorigin = to; // for DP's unsticking
20         player.angles = to_angles;
21         player.fixangle = TRUE;
22         player.velocity = to_velocity;
23
24         if(player.effects & EF_TELEPORT_BIT)
25                 player.effects &~= EF_TELEPORT_BIT;
26         else
27                 player.effects |= EF_TELEPORT_BIT;
28
29         if(player.classname == "player")
30                 player.flags &~= FL_ONGROUND;
31
32         WarpZone_PostTeleportPlayer_Callback(player);
33 }
34
35 float WarpZone_Teleport(entity player)
36 {
37         vector o0, a0, v0, o1, a1, v1;
38
39         o0 = player.origin + player.view_ofs;
40         v0 = player.velocity;
41         a0 = player.angles;
42
43         if(WarpZone_PlaneDist(self, o0) >= 0) // wrong side of the trigger_warpzone
44                 return 2;
45         // no failure, we simply don't want to teleport yet; TODO in
46         // this situation we may want to create a temporary clone
47         // entity of the player to fix graphics glitch
48
49         o1 = WarpZone_TransformOrigin(self, o0);
50         v1 = WarpZone_TransformVelocity(self, v0);
51         if(clienttype(player) != CLIENTTYPE_NOTACLIENT)
52                 a1 = WarpZone_TransformVAngles(self, player.v_angle);
53         else
54                 a1 = WarpZone_TransformAngles(self, a0);
55
56         // put him inside solid
57         tracebox(o1 - player.view_ofs, player.mins, player.maxs, o1 - player.view_ofs, MOVE_NOMONSTERS, player);
58         if(trace_startsolid)
59         {
60                 vector mi, ma;
61                 mi = player.mins;
62                 ma = player.maxs;
63                 setsize(player, mi - player.view_ofs, ma - player.view_ofs);
64                 setorigin(player, o1);
65                 if(WarpZoneLib_MoveOutOfSolid(player))
66                 {
67                         o1 = player.origin;
68                         setsize(player, mi, ma);
69                         setorigin(player, o0);
70                 }
71                 else
72                 {
73                         print("would have to put player in solid, won't do that\n");
74                         setsize(player, mi, ma);
75                         setorigin(player, o0 - player.view_ofs);
76                         return 0; // cannot fix
77                 }
78         }
79
80         if(WarpZone_TargetPlaneDist(self, o1) <= 0)
81         {
82                 print("inconsistent warp zones or evil roundoff error\n");
83                 return 0;
84         }
85
86         //print(sprintf("warpzone: %f %f %f -> %f %f %f\n", o0_x, o0_y, o0_z, o1_x, o1_y, o1_z));
87
88         //o1 = trace_endpos;
89         WarpZone_RefSys_Add(player, self);
90         WarpZone_TeleportPlayer(self, player, o1 - player.view_ofs, a1, v1);
91         WarpZone_StoreProjectileData(player);
92         player.warpzone_teleport_time = time;
93
94         return 1;
95 }
96
97 void WarpZone_Touch (void)
98 {
99         entity oldself, e;
100
101         if(other.classname == "trigger_warpzone")
102                 return;
103
104         // FIXME needs a better check to know what is safe to teleport and what not
105         if(other.movetype == MOVETYPE_NONE)
106                 return;
107
108         if(WarpZoneLib_ExactTrigger_Touch())
109                 return;
110
111         e = self.enemy;
112         if(WarpZone_Teleport(other))
113         {
114                 string save1, save2;
115                 activator = other;
116
117                 save1 = self.target; self.target = string_null;
118                 save2 = self.target3; self.target3 = string_null;
119                 SUB_UseTargets();
120                 if not(self.target) self.target = save1;
121                 if not(self.target3) self.target3 = save2;
122
123                 oldself = self;
124                 self = self.enemy;
125                 save1 = self.target; self.target = string_null;
126                 save2 = self.target2; self.target2 = string_null;
127                 SUB_UseTargets();
128                 if not(self.target) self.target = save1;
129                 if not(self.target2) self.target2 = save2;
130                 self = oldself;
131         }
132         else
133         {
134                 dprint("WARPZONE FAIL AHAHAHAHAH))\n");
135         }
136 }
137
138 float WarpZone_Send(entity to, float sendflags)
139 {
140         float f;
141         WriteByte(MSG_ENTITY, ENT_CLIENT_WARPZONE);
142
143         // we must send this flag for clientside to match properly too
144         f = 0;
145         if(self.warpzone_isboxy)
146                 f |= 1;
147         if(self.warpzone_fadestart)
148                 f |= 2;
149         if(self.origin != '0 0 0')
150                 f |= 4;
151         WriteByte(MSG_ENTITY, f);
152
153         // we need THESE to render the warpzone (and cull properly)...
154         if(f & 4)
155         {
156                 WriteCoord(MSG_ENTITY, self.origin_x);
157                 WriteCoord(MSG_ENTITY, self.origin_y);
158                 WriteCoord(MSG_ENTITY, self.origin_z);
159         }
160
161         WriteShort(MSG_ENTITY, self.modelindex);
162         WriteCoord(MSG_ENTITY, self.mins_x);
163         WriteCoord(MSG_ENTITY, self.mins_y);
164         WriteCoord(MSG_ENTITY, self.mins_z);
165         WriteCoord(MSG_ENTITY, self.maxs_x);
166         WriteCoord(MSG_ENTITY, self.maxs_y);
167         WriteCoord(MSG_ENTITY, self.maxs_z);
168         WriteByte(MSG_ENTITY, bound(1, self.scale * 16, 255));
169
170         // we need THESE to calculate the proper transform
171         WriteCoord(MSG_ENTITY, self.warpzone_origin_x);
172         WriteCoord(MSG_ENTITY, self.warpzone_origin_y);
173         WriteCoord(MSG_ENTITY, self.warpzone_origin_z);
174         WriteCoord(MSG_ENTITY, self.warpzone_angles_x);
175         WriteCoord(MSG_ENTITY, self.warpzone_angles_y);
176         WriteCoord(MSG_ENTITY, self.warpzone_angles_z);
177         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin_x);
178         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin_y);
179         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin_z);
180         WriteCoord(MSG_ENTITY, self.warpzone_targetangles_x);
181         WriteCoord(MSG_ENTITY, self.warpzone_targetangles_y);
182         WriteCoord(MSG_ENTITY, self.warpzone_targetangles_z);
183
184         if(f & 2)
185         {
186                 WriteShort(MSG_ENTITY, self.warpzone_fadestart);
187                 WriteShort(MSG_ENTITY, self.warpzone_fadeend);
188         }
189
190         return TRUE;
191 }
192
193 float WarpZone_Camera_Send(entity to, float sendflags)
194 {
195         float f;
196         WriteByte(MSG_ENTITY, ENT_CLIENT_WARPZONE_CAMERA);
197
198         if(self.warpzone_fadestart)
199                 f |= 2;
200         if(self.origin != '0 0 0')
201                 f |= 4;
202         WriteByte(MSG_ENTITY, f);
203
204         // we need THESE to render the warpzone (and cull properly)...
205         if(f & 4)
206         {
207                 WriteCoord(MSG_ENTITY, self.origin_x);
208                 WriteCoord(MSG_ENTITY, self.origin_y);
209                 WriteCoord(MSG_ENTITY, self.origin_z);
210         }
211
212         WriteShort(MSG_ENTITY, self.modelindex);
213         WriteCoord(MSG_ENTITY, self.mins_x);
214         WriteCoord(MSG_ENTITY, self.mins_y);
215         WriteCoord(MSG_ENTITY, self.mins_z);
216         WriteCoord(MSG_ENTITY, self.maxs_x);
217         WriteCoord(MSG_ENTITY, self.maxs_y);
218         WriteCoord(MSG_ENTITY, self.maxs_z);
219         WriteByte(MSG_ENTITY, bound(1, self.scale * 16, 255));
220
221         // we need THESE to calculate the proper transform
222         WriteCoord(MSG_ENTITY, self.enemy.origin_x);
223         WriteCoord(MSG_ENTITY, self.enemy.origin_y);
224         WriteCoord(MSG_ENTITY, self.enemy.origin_z);
225         WriteCoord(MSG_ENTITY, self.enemy.angles_x);
226         WriteCoord(MSG_ENTITY, self.enemy.angles_y);
227         WriteCoord(MSG_ENTITY, self.enemy.angles_z);
228
229         if(f & 2)
230         {
231                 WriteShort(MSG_ENTITY, self.warpzone_fadestart);
232                 WriteShort(MSG_ENTITY, self.warpzone_fadeend);
233         }
234
235         return TRUE;
236 }
237
238 float WarpZone_CheckProjectileImpact()
239 {
240         // if self hit a warpzone, abort
241         vector o0, v0, a0;
242         float mpd, pd, dpd;
243         entity wz;
244         wz = WarpZone_Find(self.origin + self.mins, self.origin + self.maxs);
245         if(!wz)
246                 return FALSE;
247         o0 = self.origin;
248         v0 = self.velocity;
249         a0 = self.angles;
250
251         // this approach transports the projectile at its full speed, but does
252         // not properly retain the projectile trail (but we can't retain it
253         // easily anyway without delaying the projectile by two frames, so who
254         // cares)
255         WarpZone_TraceBox_ThroughZone(self.warpzone_oldorigin, self.mins, self.maxs, self.warpzone_oldorigin + self.warpzone_oldvelocity * frametime, MOVE_NORMAL, self, wz, WarpZone_trace_callback_t_null); // this will get us through the warpzone
256         setorigin(self, trace_endpos);
257         self.angles = WarpZone_TransformAngles(WarpZone_trace_transform, self.angles);
258         self.velocity = WarpZone_TransformVelocity(WarpZone_trace_transform, self.warpzone_oldvelocity);
259         
260         // in case we are in our warp zone post-teleport, shift the projectile forward a bit
261         mpd = max(vlen(self.mins), vlen(self.maxs));
262         pd = WarpZone_TargetPlaneDist(wz, self.origin);
263         if(pd < mpd)
264         {
265                 dpd = normalize(self.velocity) * self.warpzone_targetforward;
266                 setorigin(self, self.origin + normalize(self.velocity) * ((mpd - pd) / dpd));
267                 if(!WarpZoneLib_MoveOutOfSolid(self))
268                 {
269                         setorigin(self, o0);
270                         self.angles = a0;
271                         self.velocity = v0;
272                         return FALSE;
273                 }
274         }
275         WarpZone_RefSys_Add(self, wz);
276         WarpZone_StoreProjectileData(self);
277         self.warpzone_teleport_time = time;
278
279         return TRUE;
280 }
281 float WarpZone_Projectile_Touch()
282 {
283         if(other.classname == "trigger_warpzone")
284                 return TRUE;
285         if(WarpZone_Projectile_Touch_ImpactFilter_Callback())
286                 return TRUE;
287         if(WarpZone_CheckProjectileImpact())
288                 return TRUE;
289         if(self.warpzone_teleport_time == time) // already got teleported this frame? no collision then please
290         {
291                 setorigin(self, self.warpzone_oldorigin);
292                 self.velocity = self.warpzone_oldvelocity;
293                 self.angles = self.warpzone_oldangles;
294                 return TRUE;
295         }
296
297         return FALSE;
298 }
299
300 void WarpZone_InitStep_FindOriginTarget()
301 {
302         if(self.killtarget != "")
303         {
304                 self.aiment = find(world, targetname, self.killtarget);
305                 if(self.aiment == world)
306                 {
307                         error("Warp zone with nonexisting killtarget");
308                         return;
309                 }
310                 self.killtarget = string_null;
311         }
312 }
313
314 void WarpZonePosition_InitStep_FindTarget()
315 {
316         if(self.target == "")
317         {
318                 error("Warp zone position with no target");
319                 return;
320         }
321         self.enemy = find(world, targetname, self.target);
322         if(self.enemy == world)
323         {
324                 error("Warp zone position with nonexisting target");
325                 return;
326         }
327         if(self.enemy.aiment)
328         {
329                 // already is positioned
330                 error("Warp zone position targeting already oriented warpzone");
331                 return;
332         }
333         self.enemy.aiment = self;
334 }
335
336 void WarpZoneCamera_InitStep_FindTarget()
337 {
338         entity e;
339         float i;
340         if(self.target == "")
341         {
342                 error("Camera with no target");
343                 return;
344         }
345         self.enemy = world;
346         for(e = world, i = 0; (e = find(e, targetname, self.target)); )
347                 if(random() * ++i < 1)
348                         self.enemy = e;
349         if(self.enemy == world)
350         {
351                 error("Camera with nonexisting target");
352                 return;
353         }
354         ++warpzone_cameras_exist;
355         WarpZone_Camera_SetUp(self, self.enemy.origin, self.enemy.angles);
356 }
357
358 void WarpZone_InitStep_UpdateTransform()
359 {
360         vector org, ang, norm, point;
361         float area;
362         vector tri, a, b, c, p, q, n;
363         float i_s, i_t, n_t;
364         string tex;
365
366         org = self.origin;
367         if(org == '0 0 0')
368                 org = 0.5 * (self.mins + self.maxs);
369
370         norm = point = '0 0 0';
371         area = 0;
372         for(i_s = 0; ; ++i_s)
373         {
374                 tex = getsurfacetexture(self, i_s);
375                 if not(tex)
376                         break; // this is beyond the last one
377                 if(tex == "textures/common/trigger")
378                         continue;
379                 n_t = getsurfacenumtriangles(self, i_s);
380                 for(i_t = 0; i_t < n_t; ++i_t)
381                 {
382                         tri = getsurfacetriangle(self, i_s, i_t);
383                         a = getsurfacepoint(self, i_s, tri_x);
384                         b = getsurfacepoint(self, i_s, tri_y);
385                         c = getsurfacepoint(self, i_s, tri_z);
386                         p = b - a;
387                         q = c - a;
388                         n =     '1 0 0' * (q_y * p_z - q_z * p_y)
389                         +       '0 1 0' * (q_z * p_x - q_x * p_z)
390                         +       '0 0 1' * (q_x * p_y - q_y * p_x);
391                         area = area + vlen(n);
392                         norm = norm + n;
393                         point = point + vlen(n) * (a + b + c);
394                 }
395         }
396         if(area > 0)
397         {
398                 norm = norm * (1 / area);
399                 point = point * (1 / (3 * area));
400                 if(vlen(norm) < 0.99)
401                 {
402                         print("trigger_warpzone near ", vtos(self.aiment.origin), " is nonplanar. BEWARE.\n");
403                         area = 0; // no autofixing in this case
404                 }
405                 norm = normalize(norm);
406         }
407
408         if(self.aiment)
409         {
410                 org = self.aiment.origin;
411                 ang = self.aiment.angles;
412                 if(area > 0)
413                 {
414                         org = org - ((org - point) * norm) * norm; // project to plane
415                         makevectors(ang);
416                         if(norm * v_forward < 0)
417                         {
418                                 print("Position target of trigger_warpzone near ", vtos(self.aiment.origin), " points into trigger_warpzone. BEWARE.\n");
419                                 norm = -1 * norm;
420                         }
421                         ang = vectoangles(norm, v_up); // keep rotation, but turn exactly against plane
422                         ang_x = -ang_x;
423                         if(norm * v_forward < 0.99)
424                                 print("trigger_warpzone near ", vtos(self.aiment.origin), " has been turned to match plane orientation (", vtos(self.aiment.angles), " -> ", vtos(ang), "\n");
425                         if(vlen(org - self.aiment.origin) > 0.5)
426                                 print("trigger_warpzone near ", vtos(self.aiment.origin), " has been moved to match the plane (", vtos(self.aiment.origin), " -> ", vtos(org), ").\n");
427                 }
428         }
429         else if(area > 0)
430         {
431                 org = point;
432                 ang = vectoangles(norm);
433                 ang_x = -ang_x;
434         }
435         else
436                 error("cannot infer origin/angles for this warpzone, please use a killtarget or a trigger_warpzone_position");
437
438         self.warpzone_origin = org;
439         self.warpzone_angles = ang;
440 }
441
442 void WarpZone_InitStep_ClearTarget()
443 {
444         if(self.enemy)
445                 self.enemy.enemy = world;
446         self.enemy = world;
447 }
448
449 entity warpzone_first; .entity warpzone_next;
450 void WarpZone_InitStep_FindTarget()
451 {
452         float i;
453         entity e, e2;
454
455         if(self.enemy)
456                 return;
457
458         // this way only one of the two ents needs to target
459         if(self.target != "")
460         {
461                 self.enemy = self; // so the if(!e.enemy) check also skips self, saves one IF
462
463                 e2 = world;
464                 for(e = world, i = 0; (e = find(e, targetname, self.target)); )
465                         if(!e.enemy)
466                                 if(e.classname == self.classname) // possibly non-warpzones may use the same targetname!
467                                         if(random() * ++i < 1)
468                                                 e2 = e;
469                 if(!e2)
470                 {
471                         self.enemy = world;
472                         error("Warpzone with non-existing target");
473                         return;
474                 }
475                 self.enemy = e2;
476                 e2.enemy = self;
477         }
478 }
479
480 void WarpZone_InitStep_FinalizeTransform()
481 {
482         if(!self.enemy || self.enemy.enemy != self)
483         {
484                 error("Invalid warp zone detected. Killed.");
485                 return;
486         }
487
488         ++warpzone_warpzones_exist;
489         WarpZone_SetUp(self, self.warpzone_origin, self.warpzone_angles, self.enemy.warpzone_origin, self.enemy.warpzone_angles);
490         self.touch = WarpZone_Touch;
491         self.SendFlags = 0xFFFFFF;
492 }
493
494 float warpzone_initialized;
495 entity warpzone_first;
496 entity warpzone_position_first;
497 entity warpzone_camera_first;
498 .entity warpzone_next;
499 void spawnfunc_misc_warpzone_position(void)
500 {
501         // "target", "angles", "origin"
502         self.warpzone_next = warpzone_position_first;
503         warpzone_position_first = self;
504 }
505 void spawnfunc_trigger_warpzone_position(void)
506 {
507         spawnfunc_misc_warpzone_position();
508 }
509 void spawnfunc_trigger_warpzone(void)
510 {
511         // warp zone entities must have:
512         // "killtarget" pointing to a target_position with a direction arrow
513         //              that points AWAY from the warp zone, and that is inside
514         //              the warp zone trigger
515         // "target"     pointing to an identical warp zone at another place in
516         //              the map, with another killtarget to designate its
517         //              orientation
518
519         if(!self.scale)
520                 self.scale = self.modelscale;
521         if(!self.scale)
522                 self.scale = 1;
523         string m;
524         m = self.model;
525         WarpZoneLib_ExactTrigger_Init();
526         if(m != "")
527         {
528                 precache_model(m);
529                 setmodel(self, m); // no precision needed
530         }
531         setorigin(self, self.origin);
532         if(self.scale)
533                 setsize(self, self.mins * self.scale, self.maxs * self.scale);
534         else
535                 setsize(self, self.mins, self.maxs);
536         self.SendEntity = WarpZone_Send;
537         self.SendFlags = 0xFFFFFF;
538         self.effects |= EF_NODEPTHTEST;
539         self.warpzone_next = warpzone_first;
540         warpzone_first = self;
541 }
542 void spawnfunc_func_camera(void)
543 {
544         if(!self.scale)
545                 self.scale = self.modelscale;
546         if(!self.scale)
547                 self.scale = 1;
548         if(self.model != "")
549         {
550                 precache_model(self.model);
551                 setmodel(self, self.model); // no precision needed
552         }
553         setorigin(self, self.origin);
554         if(self.scale)
555                 setsize(self, self.mins * self.scale, self.maxs * self.scale);
556         else
557                 setsize(self, self.mins, self.maxs);
558         if(!self.solid)
559                 self.solid = SOLID_BSP;
560         else if(self.solid < 0)
561                 self.solid = SOLID_NOT;
562         self.SendEntity = WarpZone_Camera_Send;
563         self.SendFlags = 0xFFFFFF;
564         self.warpzone_next = warpzone_camera_first;
565         warpzone_camera_first = self;
566 }
567 void WarpZones_Reconnect()
568 {
569         entity e;
570         e = self;
571         for(self = warpzone_first; self; self = self.warpzone_next)
572                 WarpZone_InitStep_ClearTarget();
573         for(self = warpzone_first; self; self = self.warpzone_next)
574                 WarpZone_InitStep_FindTarget();
575         for(self = warpzone_camera_first; self; self = self.warpzone_next)
576                 WarpZoneCamera_InitStep_FindTarget();
577         for(self = warpzone_first; self; self = self.warpzone_next)
578                 WarpZone_InitStep_FinalizeTransform();
579         self = e;
580 }
581
582 void WarpZone_StartFrame()
583 {
584         entity e;
585         if(warpzone_initialized == 0)
586         {
587                 warpzone_initialized = 1;
588                 e = self;
589                 for(self = warpzone_first; self; self = self.warpzone_next)
590                         WarpZone_InitStep_FindOriginTarget();
591                 for(self = warpzone_position_first; self; self = self.warpzone_next)
592                         WarpZonePosition_InitStep_FindTarget();
593                 for(self = warpzone_first; self; self = self.warpzone_next)
594                         WarpZone_InitStep_UpdateTransform();
595                 self = e;
596                 WarpZones_Reconnect();
597         }
598         for(e = world; (e = nextent(e)); )
599                 WarpZone_StoreProjectileData(e);
600 }
601
602 .float warpzone_reconnecting;
603 float visible_to_some_client(entity ent)
604 {
605         entity e;
606         for(e = nextent(world); clienttype(e) != CLIENTTYPE_NOTACLIENT; e = nextent(e))
607                 if(e.classname == "player" && clienttype(e) == CLIENTTYPE_REAL)
608                         if(checkpvs(e.origin + e.view_ofs, ent))
609                                 return 1;
610         return 0;
611 }
612 void trigger_warpzone_reconnect_use()
613 {
614         entity e;
615         e = self;
616         // NOTE: this matches for target, not targetname, but of course
617         // targetname must be set too on the other entities
618         for(self = warpzone_first; self; self = self.warpzone_next)
619                 self.warpzone_reconnecting = ((e.target == "" || self.target == e.target) && !((e.spawnflags & 1) && (visible_to_some_client(self) || visible_to_some_client(self.enemy))));
620         for(self = warpzone_camera_first; self; self = self.warpzone_next)
621                 self.warpzone_reconnecting = ((e.target == "" || self.target == e.target) && !((e.spawnflags & 1) && visible_to_some_client(self)));
622         for(self = warpzone_first; self; self = self.warpzone_next)
623                 if(self.warpzone_reconnecting)
624                         WarpZone_InitStep_ClearTarget();
625         for(self = warpzone_first; self; self = self.warpzone_next)
626                 if(self.warpzone_reconnecting)
627                         WarpZone_InitStep_FindTarget();
628         for(self = warpzone_camera_first; self; self = self.warpzone_next)
629                 if(self.warpzone_reconnecting)
630                         WarpZoneCamera_InitStep_FindTarget();
631         for(self = warpzone_first; self; self = self.warpzone_next)
632                 if(self.warpzone_reconnecting || self.enemy.warpzone_reconnecting)
633                         WarpZone_InitStep_FinalizeTransform();
634         self = e;
635 }
636
637 void spawnfunc_trigger_warpzone_reconnect()
638 {
639         self.use = trigger_warpzone_reconnect_use;
640 }
641
642 void spawnfunc_target_warpzone_reconnect()
643 {
644         spawnfunc_trigger_warpzone_reconnect(); // both names make sense here :(
645 }