properly draw the hook through warpzones again too; fix interpolation issues
[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                 if(self.aiment.target)
115                 {
116                         oldself = self;
117                         activator = other;
118                         self = self.aiment;
119                         SUB_UseTargets();
120                         self = oldself;
121                 }
122         }
123         else
124         {
125                 dprint("WARPZONE FAIL AHAHAHAHAH))\n");
126         }
127 }
128
129 float WarpZone_Send(entity to, float sendflags)
130 {
131         WriteByte(MSG_ENTITY, ENT_CLIENT_WARPZONE);
132
133         // we need THESE to render the warpzone (and cull properly)...
134         WriteCoord(MSG_ENTITY, self.origin_x);
135         WriteCoord(MSG_ENTITY, self.origin_y);
136         WriteCoord(MSG_ENTITY, self.origin_z);
137
138         WriteShort(MSG_ENTITY, self.modelindex);
139         WriteCoord(MSG_ENTITY, self.mins_x);
140         WriteCoord(MSG_ENTITY, self.mins_y);
141         WriteCoord(MSG_ENTITY, self.mins_z);
142         WriteCoord(MSG_ENTITY, self.maxs_x);
143         WriteCoord(MSG_ENTITY, self.maxs_y);
144         WriteCoord(MSG_ENTITY, self.maxs_z);
145         WriteByte(MSG_ENTITY, bound(1, self.scale * 16, 255));
146
147         // we need THESE to calculate the proper transform
148         WriteCoord(MSG_ENTITY, self.warpzone_origin_x);
149         WriteCoord(MSG_ENTITY, self.warpzone_origin_y);
150         WriteCoord(MSG_ENTITY, self.warpzone_origin_z);
151         WriteCoord(MSG_ENTITY, self.warpzone_angles_x);
152         WriteCoord(MSG_ENTITY, self.warpzone_angles_y);
153         WriteCoord(MSG_ENTITY, self.warpzone_angles_z);
154         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin_x);
155         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin_y);
156         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin_z);
157         WriteCoord(MSG_ENTITY, self.warpzone_targetangles_x);
158         WriteCoord(MSG_ENTITY, self.warpzone_targetangles_y);
159         WriteCoord(MSG_ENTITY, self.warpzone_targetangles_z);
160
161         return TRUE;
162 }
163
164 float WarpZone_Camera_Send(entity to, float sendflags)
165 {
166         WriteByte(MSG_ENTITY, ENT_CLIENT_WARPZONE_CAMERA);
167
168         // we need THESE to render the warpzone (and cull properly)...
169         WriteCoord(MSG_ENTITY, self.origin_x);
170         WriteCoord(MSG_ENTITY, self.origin_y);
171         WriteCoord(MSG_ENTITY, self.origin_z);
172
173         WriteShort(MSG_ENTITY, self.modelindex);
174         WriteCoord(MSG_ENTITY, self.mins_x);
175         WriteCoord(MSG_ENTITY, self.mins_y);
176         WriteCoord(MSG_ENTITY, self.mins_z);
177         WriteCoord(MSG_ENTITY, self.maxs_x);
178         WriteCoord(MSG_ENTITY, self.maxs_y);
179         WriteCoord(MSG_ENTITY, self.maxs_z);
180         WriteByte(MSG_ENTITY, bound(1, self.scale * 16, 255));
181
182         // we need THESE to calculate the proper transform
183         WriteCoord(MSG_ENTITY, self.enemy.origin_x);
184         WriteCoord(MSG_ENTITY, self.enemy.origin_y);
185         WriteCoord(MSG_ENTITY, self.enemy.origin_z);
186         WriteCoord(MSG_ENTITY, self.enemy.angles_x);
187         WriteCoord(MSG_ENTITY, self.enemy.angles_y);
188         WriteCoord(MSG_ENTITY, self.enemy.angles_z);
189
190         return TRUE;
191 }
192
193 float WarpZone_CheckProjectileImpact()
194 {
195         // if self hit a warpzone, abort
196         vector o0, v0, a0;
197         float mpd, pd, dpd;
198         entity wz;
199         wz = WarpZone_Find(self.origin + self.mins, self.origin + self.maxs);
200         if(!wz)
201                 return FALSE;
202         o0 = self.origin;
203         v0 = self.velocity;
204         a0 = self.angles;
205
206         // this approach transports the projectile at its full speed, but does
207         // not properly retain the projectile trail (but we can't retain it
208         // easily anyway without delaying the projectile by two frames, so who
209         // cares)
210         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
211         setorigin(self, trace_endpos);
212         self.angles = WarpZone_TransformAngles(WarpZone_trace_transform, self.angles);
213         self.velocity = WarpZone_TransformVelocity(WarpZone_trace_transform, self.velocity);
214         
215         // in case we are in our warp zone post-teleport, shift the projectile forward a bit
216         mpd = max(vlen(self.mins), vlen(self.maxs));
217         pd = WarpZone_TargetPlaneDist(wz, self.origin);
218         if(pd < mpd)
219         {
220                 dpd = normalize(self.velocity) * self.warpzone_targetforward;
221                 setorigin(self, self.origin + normalize(self.velocity) * ((mpd - pd) / dpd));
222                 if(!WarpZoneLib_MoveOutOfSolid(self))
223                 {
224                         setorigin(self, o0);
225                         self.angles = a0;
226                         self.velocity = v0;
227                         return FALSE;
228                 }
229         }
230         WarpZone_RefSys_Add(self, wz);
231         WarpZone_StoreProjectileData(self);
232         self.warpzone_teleport_time = time;
233
234         return TRUE;
235 }
236 float WarpZone_Projectile_Touch()
237 {
238         if(other.classname == "trigger_warpzone")
239                 return TRUE;
240         if(WarpZone_Projectile_Touch_ImpactFilter_Callback())
241                 return TRUE;
242         if(WarpZone_CheckProjectileImpact())
243                 return TRUE;
244         if(self.warpzone_teleport_time == time) // already got teleported this frame? no collision then please
245         {
246                 setorigin(self, self.warpzone_oldorigin);
247                 self.velocity = self.warpzone_oldvelocity;
248                 self.angles = self.warpzone_oldangles;
249                 return TRUE;
250         }
251
252         return FALSE;
253 }
254
255 void WarpZone_InitStep_FindOriginTarget()
256 {
257         if(self.killtarget != "")
258         {
259                 self.aiment = find(world, targetname, self.killtarget);
260                 if(self.aiment == world)
261                 {
262                         error("Warp zone with nonexisting killtarget");
263                         return;
264                 }
265         }
266 }
267
268 void WarpZonePosition_InitStep_FindTarget()
269 {
270         if(self.target == "")
271         {
272                 error("Warp zone position with no target");
273                 return;
274         }
275         self.enemy = find(world, targetname, self.target);
276         if(self.enemy == world)
277         {
278                 error("Warp zone position with nonexisting target");
279                 return;
280         }
281         if(self.enemy.aiment)
282         {
283                 // already is positioned
284                 error("Warp zone position targeting already oriented warpzone");
285                 return;
286         }
287         self.enemy.aiment = self;
288 }
289
290 void WarpZoneCamera_InitStep_FindTarget()
291 {
292         if(self.target == "")
293         {
294                 error("Camera with no target");
295                 return;
296         }
297         self.enemy = find(world, targetname, self.target);
298         if(self.enemy == world)
299         {
300                 error("Camera with nonexisting target");
301                 return;
302         }
303         WarpZone_Camera_SetUp(self, self.enemy.origin, self.enemy.angles);
304 }
305
306 void WarpZone_InitStep_UpdateTransform()
307 {
308         vector org, ang, norm, point;
309         float area;
310         vector tri, a, b, c, p, q, n;
311         float i_s, i_t, n_t;
312         string tex;
313
314         org = self.origin;
315         if(org == '0 0 0')
316                 org = 0.5 * (self.mins + self.maxs);
317
318         norm = point = '0 0 0';
319         area = 0;
320         for(i_s = 0; ; ++i_s)
321         {
322                 tex = getsurfacetexture(self, i_s);
323                 if not(tex)
324                         break; // this is beyond the last one
325                 if(tex == "textures/common/trigger")
326                         continue;
327                 n_t = getsurfacenumtriangles(self, i_s);
328                 for(i_t = 0; i_t < n_t; ++i_t)
329                 {
330                         tri = getsurfacetriangle(self, i_s, i_t);
331                         a = getsurfacepoint(self, i_s, tri_x);
332                         b = getsurfacepoint(self, i_s, tri_y);
333                         c = getsurfacepoint(self, i_s, tri_z);
334                         p = b - a;
335                         q = c - a;
336                         n =     '1 0 0' * (q_y * p_z - q_z * p_y)
337                         +       '0 1 0' * (q_z * p_x - q_x * p_z)
338                         +       '0 0 1' * (q_x * p_y - q_y * p_x);
339                         area = area + vlen(n);
340                         norm = norm + n;
341                         point = point + vlen(n) * (a + b + c);
342                 }
343         }
344         if(area > 0)
345         {
346                 norm = norm * (1 / area);
347                 point = point * (1 / (3 * area));
348                 if(vlen(norm) < 0.99)
349                 {
350                         print("trigger_warpzone near ", vtos(self.aiment.origin), " is nonplanar. BEWARE.\n");
351                         area = 0; // no autofixing in this case
352                 }
353                 norm = normalize(norm);
354         }
355
356         if(self.aiment)
357         {
358                 org = self.aiment.origin;
359                 ang = self.aiment.angles;
360                 if(area > 0)
361                 {
362                         org = org - ((org - point) * norm) * norm; // project to plane
363                         makevectors(ang);
364                         if(norm * v_forward < 0)
365                         {
366                                 print("Position target of trigger_warpzone near ", vtos(self.aiment.origin), " points into trigger_warpzone. BEWARE.\n");
367                                 norm = -1 * norm;
368                         }
369                         ang = vectoangles(norm, v_up); // keep rotation, but turn exactly against plane
370                         ang_x = -ang_x;
371                         if(norm * v_forward < 0.99)
372                                 print("trigger_warpzone near ", vtos(self.aiment.origin), " has been turned to match plane orientation (", vtos(self.aiment.angles), " -> ", vtos(ang), "\n");
373                         if(vlen(org - self.aiment.origin) > 0.5)
374                                 print("trigger_warpzone near ", vtos(self.aiment.origin), " has been moved to match the plane (", vtos(self.aiment.origin), " -> ", vtos(org), ").\n");
375                 }
376         }
377         else if(area > 0)
378         {
379                 org = point;
380                 ang = vectoangles(norm);
381                 ang_x = -ang_x;
382         }
383         else
384                 error("cannot infer origin/angles for this warpzone, please use a killtarget or a trigger_warpzone_position");
385
386         self.warpzone_origin = org;
387         self.warpzone_angles = ang;
388 }
389
390 void WarpZone_InitStep_ClearTarget()
391 {
392         if(self.enemy)
393                 self.enemy.enemy = world;
394         self.enemy = world;
395 }
396
397 entity warpzone_first; .entity warpzone_next;
398 void WarpZone_InitStep_FindTarget()
399 {
400         float i;
401         entity e, e2;
402
403         if(self.enemy)
404                 return;
405
406         // this way only one of the two ents needs to target
407         if(self.target != "")
408         {
409                 self.enemy = self; // so the if(!e.enemy) check also skips self, saves one IF
410
411                 e2 = world;
412                 for(e = world, i = 0; (e = find(e, targetname, self.target)); )
413                         if(!e.enemy)
414                                 if(e.classname == self.classname) // possibly non-warpzones may use the same targetname!
415                                         if(random() * ++i < 1)
416                                                 e2 = e;
417                 if(!e2)
418                 {
419                         self.enemy = world;
420                         error("Warpzone with non-existing target");
421                         return;
422                 }
423                 self.enemy = e2;
424                 e2.enemy = self;
425         }
426 }
427
428 void WarpZone_InitStep_FinalizeTransform()
429 {
430         if(!self.enemy || self.enemy.enemy != self)
431         {
432                 error("Invalid warp zone detected. Killed.");
433                 return;
434         }
435
436         WarpZone_SetUp(self, self.warpzone_origin, self.warpzone_angles, self.enemy.warpzone_origin, self.enemy.warpzone_angles);
437         self.touch = WarpZone_Touch;
438         self.SendFlags = 0xFFFFFF;
439 }
440
441 float warpzone_initialized;
442 entity warpzone_first;
443 entity warpzone_position_first;
444 entity warpzone_camera_first;
445 .entity warpzone_next;
446 void spawnfunc_misc_warpzone_position(void)
447 {
448         // "target", "angles", "origin"
449         self.warpzone_next = warpzone_position_first;
450         warpzone_position_first = self;
451 }
452 void spawnfunc_trigger_warpzone_position(void)
453 {
454         spawnfunc_misc_warpzone_position();
455 }
456 void spawnfunc_trigger_warpzone(void)
457 {
458         // warp zone entities must have:
459         // "killtarget" pointing to a target_position with a direction arrow
460         //              that points AWAY from the warp zone, and that is inside
461         //              the warp zone trigger
462         // "target"     pointing to an identical warp zone at another place in
463         //              the map, with another killtarget to designate its
464         //              orientation
465
466         if(!self.scale)
467                 self.scale = self.modelscale;
468         if(!self.scale)
469                 self.scale = 1;
470         string m;
471         m = self.model;
472         WarpZoneLib_ExactTrigger_Init();
473         if(m != "")
474         {
475                 precache_model(m);
476                 setmodel(self, m); // no precision needed
477         }
478         setorigin(self, self.origin);
479         if(self.scale)
480                 setsize(self, self.mins * self.scale, self.maxs * self.scale);
481         else
482                 setsize(self, self.mins, self.maxs);
483         self.SendEntity = WarpZone_Send;
484         self.SendFlags = 0xFFFFFF;
485         self.effects |= EF_NODEPTHTEST;
486         self.warpzone_next = warpzone_first;
487         warpzone_first = self;
488 }
489 void spawnfunc_func_camera(void)
490 {
491         if(!self.scale)
492                 self.scale = self.modelscale;
493         if(!self.scale)
494                 self.scale = 1;
495         if(self.model != "")
496         {
497                 precache_model(self.model);
498                 setmodel(self, self.model); // no precision needed
499         }
500         setorigin(self, self.origin);
501         if(self.scale)
502                 setsize(self, self.mins * self.scale, self.maxs * self.scale);
503         else
504                 setsize(self, self.mins, self.maxs);
505         if(!self.solid)
506                 self.solid = SOLID_BSP;
507         else if(self.solid < 0)
508                 self.solid = SOLID_NOT;
509         self.SendEntity = WarpZone_Camera_Send;
510         self.SendFlags = 0xFFFFFF;
511         self.warpzone_next = warpzone_camera_first;
512         warpzone_camera_first = self;
513 }
514 void WarpZones_Reconnect()
515 {
516         entity e;
517         e = self;
518         for(self = warpzone_first; self; self = self.warpzone_next)
519                 WarpZone_InitStep_ClearTarget();
520         for(self = warpzone_first; self; self = self.warpzone_next)
521                 WarpZone_InitStep_FindTarget();
522         for(self = warpzone_first; self; self = self.warpzone_next)
523                 WarpZone_InitStep_FinalizeTransform();
524         self = e;
525 }
526
527 void WarpZone_StartFrame()
528 {
529         entity e;
530         if(warpzone_initialized == 0)
531         {
532                 warpzone_initialized = 1;
533                 e = self;
534                 for(self = warpzone_first; self; self = self.warpzone_next)
535                         WarpZone_InitStep_FindOriginTarget();
536                 for(self = warpzone_position_first; self; self = self.warpzone_next)
537                         WarpZonePosition_InitStep_FindTarget();
538                 for(self = warpzone_camera_first; self; self = self.warpzone_next)
539                         WarpZoneCamera_InitStep_FindTarget();
540                 for(self = warpzone_first; self; self = self.warpzone_next)
541                         WarpZone_InitStep_UpdateTransform();
542                 self = e;
543                 WarpZones_Reconnect();
544         }
545         for(e = world; (e = nextent(e)); )
546                 WarpZone_StoreProjectileData(e);
547 }
548
549 void trigger_warpzone_reconnect_use()
550 {
551         entity e;
552         e = self;
553         // NOTE: this matches for target, not targetname, but of course
554         // targetname must be set too on the other entities
555         for(self = warpzone_first; self; self = self.warpzone_next)
556                 if(e.target == "" || self.target == e.target)
557                         WarpZone_InitStep_ClearTarget();
558         for(self = warpzone_first; self; self = self.warpzone_next)
559                 if(e.target == "" || self.target == e.target)
560                         WarpZone_InitStep_FindTarget();
561         for(self = warpzone_first; self; self = self.warpzone_next)
562                 if(e.target == "" || self.target == e.target || self.enemy.target == e.target)
563                         WarpZone_InitStep_FinalizeTransform();
564         self = e;
565 }
566
567 void spawnfunc_trigger_warpzone_reconnect()
568 {
569         self.use = trigger_warpzone_reconnect_use;
570 }
571
572 void spawnfunc_target_warpzone_reconnect()
573 {
574         spawnfunc_trigger_warpzone_reconnect(); // both names make sense here :(
575 }