]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/warpzonelib/server.qc
precache the crosshair_ring to prevent a slight stutter when switching to the nex...
[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(player.classname == "player")
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         if(self.target == "")
339         {
340                 error("Camera with no target");
341                 return;
342         }
343         self.enemy = find(world, targetname, self.target);
344         if(self.enemy == world)
345         {
346                 error("Camera with nonexisting target");
347                 return;
348         }
349         ++warpzone_cameras_exist;
350         WarpZone_Camera_SetUp(self, self.enemy.origin, self.enemy.angles);
351 }
352
353 void WarpZone_InitStep_UpdateTransform()
354 {
355         vector org, ang, norm, point;
356         float area;
357         vector tri, a, b, c, p, q, n;
358         float i_s, i_t, n_t;
359         string tex;
360
361         org = self.origin;
362         if(org == '0 0 0')
363                 org = 0.5 * (self.mins + self.maxs);
364
365         norm = point = '0 0 0';
366         area = 0;
367         for(i_s = 0; ; ++i_s)
368         {
369                 tex = getsurfacetexture(self, i_s);
370                 if not(tex)
371                         break; // this is beyond the last one
372                 if(tex == "textures/common/trigger")
373                         continue;
374                 n_t = getsurfacenumtriangles(self, i_s);
375                 for(i_t = 0; i_t < n_t; ++i_t)
376                 {
377                         tri = getsurfacetriangle(self, i_s, i_t);
378                         a = getsurfacepoint(self, i_s, tri_x);
379                         b = getsurfacepoint(self, i_s, tri_y);
380                         c = getsurfacepoint(self, i_s, tri_z);
381                         p = b - a;
382                         q = c - a;
383                         n =     '1 0 0' * (q_y * p_z - q_z * p_y)
384                         +       '0 1 0' * (q_z * p_x - q_x * p_z)
385                         +       '0 0 1' * (q_x * p_y - q_y * p_x);
386                         area = area + vlen(n);
387                         norm = norm + n;
388                         point = point + vlen(n) * (a + b + c);
389                 }
390         }
391         if(area > 0)
392         {
393                 norm = norm * (1 / area);
394                 point = point * (1 / (3 * area));
395                 if(vlen(norm) < 0.99)
396                 {
397                         print("trigger_warpzone near ", vtos(self.aiment.origin), " is nonplanar. BEWARE.\n");
398                         area = 0; // no autofixing in this case
399                 }
400                 norm = normalize(norm);
401         }
402
403         if(self.aiment)
404         {
405                 org = self.aiment.origin;
406                 ang = self.aiment.angles;
407                 if(area > 0)
408                 {
409                         org = org - ((org - point) * norm) * norm; // project to plane
410                         makevectors(ang);
411                         if(norm * v_forward < 0)
412                         {
413                                 print("Position target of trigger_warpzone near ", vtos(self.aiment.origin), " points into trigger_warpzone. BEWARE.\n");
414                                 norm = -1 * norm;
415                         }
416                         ang = vectoangles(norm, v_up); // keep rotation, but turn exactly against plane
417                         ang_x = -ang_x;
418                         if(norm * v_forward < 0.99)
419                                 print("trigger_warpzone near ", vtos(self.aiment.origin), " has been turned to match plane orientation (", vtos(self.aiment.angles), " -> ", vtos(ang), "\n");
420                         if(vlen(org - self.aiment.origin) > 0.5)
421                                 print("trigger_warpzone near ", vtos(self.aiment.origin), " has been moved to match the plane (", vtos(self.aiment.origin), " -> ", vtos(org), ").\n");
422                 }
423         }
424         else if(area > 0)
425         {
426                 org = point;
427                 ang = vectoangles(norm);
428                 ang_x = -ang_x;
429         }
430         else
431                 error("cannot infer origin/angles for this warpzone, please use a killtarget or a trigger_warpzone_position");
432
433         self.warpzone_origin = org;
434         self.warpzone_angles = ang;
435 }
436
437 void WarpZone_InitStep_ClearTarget()
438 {
439         if(self.enemy)
440                 self.enemy.enemy = world;
441         self.enemy = world;
442 }
443
444 entity warpzone_first; .entity warpzone_next;
445 void WarpZone_InitStep_FindTarget()
446 {
447         float i;
448         entity e, e2;
449
450         if(self.enemy)
451                 return;
452
453         // this way only one of the two ents needs to target
454         if(self.target != "")
455         {
456                 self.enemy = self; // so the if(!e.enemy) check also skips self, saves one IF
457
458                 e2 = world;
459                 for(e = world, i = 0; (e = find(e, targetname, self.target)); )
460                         if(!e.enemy)
461                                 if(e.classname == self.classname) // possibly non-warpzones may use the same targetname!
462                                         if(random() * ++i < 1)
463                                                 e2 = e;
464                 if(!e2)
465                 {
466                         self.enemy = world;
467                         error("Warpzone with non-existing target");
468                         return;
469                 }
470                 self.enemy = e2;
471                 e2.enemy = self;
472         }
473 }
474
475 void WarpZone_InitStep_FinalizeTransform()
476 {
477         if(!self.enemy || self.enemy.enemy != self)
478         {
479                 error("Invalid warp zone detected. Killed.");
480                 return;
481         }
482
483         ++warpzone_warpzones_exist;
484         WarpZone_SetUp(self, self.warpzone_origin, self.warpzone_angles, self.enemy.warpzone_origin, self.enemy.warpzone_angles);
485         self.touch = WarpZone_Touch;
486         self.SendFlags = 0xFFFFFF;
487 }
488
489 float warpzone_initialized;
490 entity warpzone_first;
491 entity warpzone_position_first;
492 entity warpzone_camera_first;
493 .entity warpzone_next;
494 void spawnfunc_misc_warpzone_position(void)
495 {
496         // "target", "angles", "origin"
497         self.warpzone_next = warpzone_position_first;
498         warpzone_position_first = self;
499 }
500 void spawnfunc_trigger_warpzone_position(void)
501 {
502         spawnfunc_misc_warpzone_position();
503 }
504 void spawnfunc_trigger_warpzone(void)
505 {
506         // warp zone entities must have:
507         // "killtarget" pointing to a target_position with a direction arrow
508         //              that points AWAY from the warp zone, and that is inside
509         //              the warp zone trigger
510         // "target"     pointing to an identical warp zone at another place in
511         //              the map, with another killtarget to designate its
512         //              orientation
513
514         if(!self.scale)
515                 self.scale = self.modelscale;
516         if(!self.scale)
517                 self.scale = 1;
518         string m;
519         m = self.model;
520         WarpZoneLib_ExactTrigger_Init();
521         if(m != "")
522         {
523                 precache_model(m);
524                 setmodel(self, m); // no precision needed
525         }
526         setorigin(self, self.origin);
527         if(self.scale)
528                 setsize(self, self.mins * self.scale, self.maxs * self.scale);
529         else
530                 setsize(self, self.mins, self.maxs);
531         self.SendEntity = WarpZone_Send;
532         self.SendFlags = 0xFFFFFF;
533         self.effects |= EF_NODEPTHTEST;
534         self.warpzone_next = warpzone_first;
535         warpzone_first = self;
536 }
537 void spawnfunc_func_camera(void)
538 {
539         if(!self.scale)
540                 self.scale = self.modelscale;
541         if(!self.scale)
542                 self.scale = 1;
543         if(self.model != "")
544         {
545                 precache_model(self.model);
546                 setmodel(self, self.model); // no precision needed
547         }
548         setorigin(self, self.origin);
549         if(self.scale)
550                 setsize(self, self.mins * self.scale, self.maxs * self.scale);
551         else
552                 setsize(self, self.mins, self.maxs);
553         if(!self.solid)
554                 self.solid = SOLID_BSP;
555         else if(self.solid < 0)
556                 self.solid = SOLID_NOT;
557         self.SendEntity = WarpZone_Camera_Send;
558         self.SendFlags = 0xFFFFFF;
559         self.warpzone_next = warpzone_camera_first;
560         warpzone_camera_first = self;
561 }
562 void WarpZones_Reconnect()
563 {
564         entity e;
565         e = self;
566         for(self = warpzone_first; self; self = self.warpzone_next)
567                 WarpZone_InitStep_ClearTarget();
568         for(self = warpzone_first; self; self = self.warpzone_next)
569                 WarpZone_InitStep_FindTarget();
570         for(self = warpzone_first; self; self = self.warpzone_next)
571                 WarpZone_InitStep_FinalizeTransform();
572         self = e;
573 }
574
575 void WarpZone_StartFrame()
576 {
577         entity e;
578         if(warpzone_initialized == 0)
579         {
580                 warpzone_initialized = 1;
581                 e = self;
582                 for(self = warpzone_first; self; self = self.warpzone_next)
583                         WarpZone_InitStep_FindOriginTarget();
584                 for(self = warpzone_position_first; self; self = self.warpzone_next)
585                         WarpZonePosition_InitStep_FindTarget();
586                 for(self = warpzone_camera_first; self; self = self.warpzone_next)
587                         WarpZoneCamera_InitStep_FindTarget();
588                 for(self = warpzone_first; self; self = self.warpzone_next)
589                         WarpZone_InitStep_UpdateTransform();
590                 self = e;
591                 WarpZones_Reconnect();
592         }
593         for(e = world; (e = nextent(e)); )
594                 WarpZone_StoreProjectileData(e);
595 }
596
597 void trigger_warpzone_reconnect_use()
598 {
599         entity e;
600         e = self;
601         // NOTE: this matches for target, not targetname, but of course
602         // targetname must be set too on the other entities
603         for(self = warpzone_first; self; self = self.warpzone_next)
604                 if(e.target == "" || self.target == e.target)
605                         WarpZone_InitStep_ClearTarget();
606         for(self = warpzone_first; self; self = self.warpzone_next)
607                 if(e.target == "" || self.target == e.target)
608                         WarpZone_InitStep_FindTarget();
609         for(self = warpzone_first; self; self = self.warpzone_next)
610                 if(e.target == "" || self.target == e.target || self.enemy.target == e.target)
611                         WarpZone_InitStep_FinalizeTransform();
612         self = e;
613 }
614
615 void spawnfunc_trigger_warpzone_reconnect()
616 {
617         self.use = trigger_warpzone_reconnect_use;
618 }
619
620 void spawnfunc_target_warpzone_reconnect()
621 {
622         spawnfunc_trigger_warpzone_reconnect(); // both names make sense here :(
623 }