1 float trace_dphitcontents;
2 .float dphitcontentsmask;
4 void WarpZone_Accumulator_Clear(entity acc)
6 acc.warpzone_transform = '0 0 0';
7 acc.warpzone_shift = '0 0 0';
9 void WarpZone_Accumulator_AddTransform(entity acc, vector t, vector s)
12 tr = AnglesTransform_Multiply(t, acc.warpzone_transform);
13 st = AnglesTransform_Multiply_GetPostShift(t, s, acc.warpzone_transform, acc.warpzone_shift);
14 acc.warpzone_transform = tr;
15 acc.warpzone_shift = st;
17 void WarpZone_Accumulator_Add(entity acc, entity wz)
20 t = AnglesTransform_Multiply(wz.warpzone_transform, acc.warpzone_transform);
21 st = AnglesTransform_Multiply_GetPostShift(wz.warpzone_transform, wz.warpzone_shift, acc.warpzone_transform, acc.warpzone_shift);
22 acc.warpzone_transform = t;
23 acc.warpzone_shift = st;
26 .vector(vector, vector) camera_transform;
27 var float autocvar_cl_warpzone_usetrace = 1;
28 vector WarpZone_camera_transform(vector org, vector ang)
31 if(self.warpzone_fadestart)
32 if(vlen(org - self.origin - 0.5 * (self.mins + self.maxs)) > self.warpzone_fadeend + 400)
34 // don't transform if zone faded out (plus 400qu safety margin for typical speeds and latencies)
35 // unneeded on client, on server this helps a lot
39 org = WarpZone_TransformOrigin(self, org);
40 vf = WarpZone_TransformVelocity(self, vf);
41 vr = WarpZone_TransformVelocity(self, vr);
42 vu = WarpZone_TransformVelocity(self, vu);
43 if(autocvar_cl_warpzone_usetrace)
44 traceline(self.warpzone_targetorigin, org, MOVE_NOMONSTERS, world);
46 trace_endpos = self.warpzone_targetorigin;
53 void WarpZone_SetUp(entity e, vector my_org, vector my_ang, vector other_org, vector other_ang)
55 e.warpzone_transform = AnglesTransform_RightDivide(other_ang, AnglesTransform_TurnDirectionFR(my_ang));
56 e.warpzone_shift = AnglesTransform_PrePostShift_GetPostShift(my_org, e.warpzone_transform, other_org);
57 e.warpzone_origin = my_org;
58 e.warpzone_targetorigin = other_org;
59 e.warpzone_angles = my_ang;
60 e.warpzone_targetangles = other_ang;
61 fixedmakevectors(my_ang); e.warpzone_forward = v_forward;
62 fixedmakevectors(other_ang); e.warpzone_targetforward = v_forward;
63 e.camera_transform = WarpZone_camera_transform;
66 vector WarpZone_Camera_camera_transform(vector org, vector ang)
68 // a fixed camera view
69 if(self.warpzone_fadestart)
70 if(vlen(org - self.origin - 0.5 * (self.mins + self.maxs)) > self.warpzone_fadeend + 400)
72 // don't transform if zone faded out (plus 400qu safety margin for typical speeds and latencies)
73 // unneeded on client, on server this helps a lot
74 trace_endpos = self.warpzone_origin;
75 makevectors(self.warpzone_angles);
76 return self.warpzone_origin;
79 void WarpZone_Camera_SetUp(entity e, vector my_org, vector my_ang) // we assume that e.oldorigin and e.avelocity point to view origin and direction
81 e.warpzone_origin = my_org;
82 e.warpzone_angles = my_ang;
83 e.camera_transform = WarpZone_Camera_camera_transform;
88 vector WarpZoneLib_BoxTouchesBrush_mins;
89 vector WarpZoneLib_BoxTouchesBrush_maxs;
90 entity WarpZoneLib_BoxTouchesBrush_ent;
91 entity WarpZoneLib_BoxTouchesBrush_ignore;
92 float WarpZoneLib_BoxTouchesBrush_Recurse()
98 tracebox('0 0 0', WarpZoneLib_BoxTouchesBrush_mins, WarpZoneLib_BoxTouchesBrush_maxs, '0 0 0', MOVE_NOMONSTERS, WarpZoneLib_BoxTouchesBrush_ignore);
100 if (trace_networkentity)
102 dprint("hit a network ent, cannot continue WarpZoneLib_BoxTouchesBrush\n");
103 // we cannot continue, as a player blocks us...
110 if (trace_ent == WarpZoneLib_BoxTouchesBrush_ent)
115 se.solid = SOLID_NOT;
116 f = WarpZoneLib_BoxTouchesBrush_Recurse();
122 float WarpZoneLib_BoxTouchesBrush(vector mi, vector ma, entity e, entity ig)
126 if(!e.modelindex || e.warpzone_isboxy)
131 WarpZoneLib_BoxTouchesBrush_mins = mi;
132 WarpZoneLib_BoxTouchesBrush_maxs = ma;
133 WarpZoneLib_BoxTouchesBrush_ent = e;
134 WarpZoneLib_BoxTouchesBrush_ignore = ig;
135 f = WarpZoneLib_BoxTouchesBrush_Recurse();
141 entity WarpZone_Find(vector mi, vector ma)
143 // if we are near any warpzone planes - MOVE AWAY (work around nearclip)
145 if(!warpzone_warpzones_exist)
147 for(e = world; (e = find(e, classname, "trigger_warpzone")); )
148 if(WarpZoneLib_BoxTouchesBrush(mi, ma, e, world))
153 void WarpZone_MakeAllSolid()
156 if(!warpzone_warpzones_exist)
158 for(e = world; (e = find(e, classname, "trigger_warpzone")); )
162 void WarpZone_MakeAllOther()
165 if(!warpzone_warpzones_exist)
167 for(e = world; (e = find(e, classname, "trigger_warpzone")); )
168 e.solid = SOLID_TRIGGER;
171 void WarpZone_Trace_InitTransform()
173 if(!WarpZone_trace_transform)
175 WarpZone_trace_transform = spawn();
176 WarpZone_trace_transform.classname = "warpzone_trace_transform";
178 WarpZone_Accumulator_Clear(WarpZone_trace_transform);
180 void WarpZone_Trace_AddTransform(entity wz)
182 WarpZone_Accumulator_Add(WarpZone_trace_transform, wz);
185 void WarpZone_TraceBox_ThroughZone(vector org, vector mi, vector ma, vector end, float nomonsters, entity forent, entity zone, WarpZone_trace_callback_t cb)
187 float nomonsters_adjusted;
194 WarpZone_trace_firstzone = world;
195 WarpZone_trace_lastzone = world;
196 WarpZone_Trace_InitTransform();
197 if(!warpzone_warpzones_exist)
199 if(nomonsters == MOVE_NOTHING)
204 cb(org, trace_endpos, end);
209 tracebox(org, mi, ma, end, nomonsters, forent);
211 cb(org, trace_endpos, end);
226 nomonsters_adjusted = MOVE_NOMONSTERS;
229 nomonsters_adjusted = nomonsters;
232 if((contentshack = (forent.dphitcontentsmask && !(forent.dphitcontentsmask & DPCONTENTS_SOLID))))
233 BITSET_ASSIGN(forent.dphitcontentsmask, DPCONTENTS_SOLID);
235 // if starting in warpzone, first transform
236 wz = WarpZone_Find(org + mi, org + ma);
239 WarpZone_trace_firstzone = wz;
240 WarpZone_trace_lastzone = wz;
241 if(zone && wz != zone)
243 // we are in ANOTHER warpzone. This is bad. Make a zero length trace and return.
249 WarpZone_Trace_AddTransform(wz);
250 org = WarpZone_TransformOrigin(wz, org);
251 end = WarpZone_TransformOrigin(wz, end);
253 WarpZone_MakeAllSolid();
261 dprint("Too many warpzones in sequence, aborting trace.\n");
265 tracebox(org, mi, ma, end, nomonsters_adjusted, forent);
267 cb(org, trace_endpos, end);
269 sol = trace_startsolid;
271 frac = trace_fraction = frac + (1 - frac) * trace_fraction;
272 if(trace_fraction >= 1)
274 if(trace_ent.classname != "trigger_warpzone")
276 if((nomonsters == MOVE_NOTHING) || ((nomonsters == MOVE_WORLDONLY) && trace_ent) || (contentshack && (trace_dphitcontents & forent.dphitcontentsmask) == DPCONTENTS_SOLID))
278 // continue the trace, ignoring this hit (we only care for warpzones)
279 org = trace_endpos + normalize(end - org);
281 // we cannot do an inverted trace here, as we do care for further warpzones inside that "solid" to be found
282 // otherwise, players could block entrances that way
288 // FIXME can this check be removed? Do we really need it?
289 dprint("I transformed into the same zone again, wtf, aborting the trace\n");
294 if(!WarpZone_trace_firstzone)
295 WarpZone_trace_firstzone = wz;
296 WarpZone_trace_lastzone = wz;
297 if(zone && wz != zone)
299 WarpZone_Trace_AddTransform(wz);
300 // we hit a warpzone... so, let's perform the trace after the warp again
301 org = WarpZone_TransformOrigin(wz, trace_endpos);
302 end = WarpZone_TransformOrigin(wz, end);
304 // we got warped, so let's step back a bit
305 tracebox(org, mi, ma, org + normalize(org - end) * 32, nomonsters_adjusted, forent);
308 WarpZone_MakeAllOther();
311 BITCLR_ASSIGN(forent.dphitcontentsmask, DPCONTENTS_SOLID);
312 trace_startsolid = sol;
318 void WarpZone_TraceBox(vector org, vector mi, vector ma, vector end, float nomonsters, entity forent)
320 WarpZone_TraceBox_ThroughZone(org, mi, ma, end, nomonsters, forent, world, WarpZone_trace_callback_t_null);
323 void WarpZone_TraceLine(vector org, vector end, float nomonsters, entity forent)
325 WarpZone_TraceBox(org, '0 0 0', '0 0 0', end, nomonsters, forent);
328 void WarpZone_TraceToss_ThroughZone(entity e, entity forent, entity zone, WarpZone_trace_callback_t cb)
331 vector vf, vr, vu, v0, o0;
337 WarpZone_trace_firstzone = world;
338 WarpZone_trace_lastzone = world;
339 WarpZone_Trace_InitTransform();
340 WarpZone_tracetoss_time = 0;
341 if(!warpzone_warpzones_exist)
343 tracetoss(e, forent);
345 cb(e.origin, trace_endpos, trace_endpos);
346 dt = vlen(e.origin - o0) / vlen(e.velocity);
347 WarpZone_tracetoss_time += dt;
348 e.velocity_z -= dt * g;
349 WarpZone_tracetoss_velocity = e.velocity;
358 // if starting in warpzone, first transform
359 wz = WarpZone_Find(e.origin + e.mins, e.origin + e.maxs);
362 WarpZone_trace_firstzone = wz;
363 WarpZone_trace_lastzone = wz;
364 if(zone && wz != zone)
366 // we are in ANOTHER warpzone. This is bad. Make a zero length trace and return.
368 WarpZone_tracetoss_time = 0;
372 WarpZone_Trace_AddTransform(wz);
373 setorigin(e, WarpZone_TransformOrigin(wz, e.origin));
374 e.velocity = WarpZone_TransformVelocity(wz, e.velocity);
376 WarpZone_MakeAllSolid();
377 g = cvar("sv_gravity") * e.gravity;
383 dprint("Too many warpzones in sequence, aborting trace.\n");
387 tracetoss(e, forent);
389 cb(e.origin, trace_endpos, trace_endpos);
390 dt = vlen(trace_endpos - e.origin) / vlen(e.velocity);
391 WarpZone_tracetoss_time += dt;
392 e.origin = trace_endpos;
393 e.velocity_z -= dt * g;
394 if(trace_fraction >= 1)
396 if(trace_ent.classname != "trigger_warpzone")
400 // FIXME can this check be removed? Do we really need it?
401 dprint("I transformed into the same zone again, wtf, aborting the trace\n");
406 if(!WarpZone_trace_firstzone)
407 WarpZone_trace_firstzone = wz;
408 WarpZone_trace_lastzone = wz;
409 if(zone && wz != zone)
411 WarpZone_Trace_AddTransform(wz);
412 // we hit a warpzone... so, let's perform the trace after the warp again
413 e.origin = WarpZone_TransformOrigin(wz, e.origin);
414 e.velocity = WarpZone_TransformVelocity(wz, e.velocity);
416 // we got warped, so let's step back a bit
417 e.velocity = -e.velocity;
418 tracetoss(e, forent);
419 dt = vlen(trace_endpos - e.origin) / vlen(e.velocity);
420 WarpZone_tracetoss_time -= dt;
421 e.origin = trace_endpos;
422 e.velocity = -e.velocity;
424 WarpZone_MakeAllOther();
426 WarpZone_tracetoss_velocity = e.velocity;
430 // restore old entity data (caller just uses trace_endpos, WarpZone_tracetoss_velocity and the transform)
435 void WarpZone_TraceToss(entity e, entity forent)
437 WarpZone_TraceToss_ThroughZone(e, forent, world, WarpZone_trace_callback_t_null);
440 entity WarpZone_TrailParticles_trace_callback_own;
441 float WarpZone_TrailParticles_trace_callback_eff;
442 void WarpZone_TrailParticles_trace_callback(vector from, vector endpos, vector to)
444 trailparticles(WarpZone_TrailParticles_trace_callback_own, WarpZone_TrailParticles_trace_callback_eff, from, endpos);
447 void WarpZone_TrailParticles(entity own, float eff, vector org, vector end)
449 WarpZone_TrailParticles_trace_callback_own = own;
450 WarpZone_TrailParticles_trace_callback_eff = eff;
451 WarpZone_TraceBox_ThroughZone(org, '0 0 0', '0 0 0', end, MOVE_NOMONSTERS, world, world, WarpZone_TrailParticles_trace_callback);
455 float WarpZone_TrailParticles_trace_callback_f;
456 float WarpZone_TrailParticles_trace_callback_flags;
457 void WarpZone_TrailParticles_WithMultiplier_trace_callback(vector from, vector endpos, vector to)
459 boxparticles(WarpZone_TrailParticles_trace_callback_eff, WarpZone_TrailParticles_trace_callback_own, from, endpos, WarpZone_TrailParticles_trace_callback_own.velocity, WarpZone_TrailParticles_trace_callback_own.velocity, WarpZone_TrailParticles_trace_callback_f, WarpZone_TrailParticles_trace_callback_flags);
462 void WarpZone_TrailParticles_WithMultiplier(entity own, float eff, vector org, vector end, float f, float boxflags)
464 WarpZone_TrailParticles_trace_callback_own = own;
465 WarpZone_TrailParticles_trace_callback_eff = eff;
466 WarpZone_TrailParticles_trace_callback_f = f;
467 WarpZone_TrailParticles_trace_callback_flags = boxflags;
468 WarpZone_TraceBox_ThroughZone(org, '0 0 0', '0 0 0', end, MOVE_NOMONSTERS, world, world, WarpZone_TrailParticles_WithMultiplier_trace_callback);
472 float WarpZone_PlaneDist(entity wz, vector v)
474 return (v - wz.warpzone_origin) * wz.warpzone_forward;
477 float WarpZone_TargetPlaneDist(entity wz, vector v)
479 return (v - wz.warpzone_targetorigin) * wz.warpzone_targetforward;
482 vector WarpZone_TransformOrigin(entity wz, vector v)
484 return wz.warpzone_shift + AnglesTransform_Apply(wz.warpzone_transform, v);
487 vector WarpZone_TransformVelocity(entity wz, vector v)
489 return AnglesTransform_Apply(wz.warpzone_transform, v);
492 vector WarpZone_TransformAngles(entity wz, vector v)
494 return AnglesTransform_ApplyToAngles(wz.warpzone_transform, v);
497 vector WarpZone_TransformVAngles(entity wz, vector ang)
505 ang = AnglesTransform_ApplyToVAngles(wz.warpzone_transform, ang);
508 ang = AnglesTransform_Normalize(ang, TRUE);
509 ang = AnglesTransform_CancelRoll(ang);
512 ang = AnglesTransform_Normalize(ang, FALSE);
518 vector WarpZone_UnTransformOrigin(entity wz, vector v)
520 return AnglesTransform_Apply(AnglesTransform_Invert(wz.warpzone_transform), v - wz.warpzone_shift);
523 vector WarpZone_UnTransformVelocity(entity wz, vector v)
525 return AnglesTransform_Apply(AnglesTransform_Invert(wz.warpzone_transform), v);
528 vector WarpZone_UnTransformAngles(entity wz, vector v)
530 return AnglesTransform_ApplyToAngles(AnglesTransform_Invert(wz.warpzone_transform), v);
533 vector WarpZone_UnTransformVAngles(entity wz, vector ang)
540 ang = AnglesTransform_ApplyToVAngles(AnglesTransform_Invert(wz.warpzone_transform), ang);
541 ang = AnglesTransform_Normalize(ang, TRUE);
542 ang = AnglesTransform_CancelRoll(ang);
548 vector WarpZoneLib_NearestPointOnBox(vector mi, vector ma, vector org)
551 nearest_x = bound(mi_x, org_x, ma_x);
552 nearest_y = bound(mi_y, org_y, ma_y);
553 nearest_z = bound(mi_z, org_z, ma_z);
557 .float WarpZone_findradius_hit;
558 .entity WarpZone_findradius_next;
559 void WarpZone_FindRadius_Recurse(vector org, float rad, vector org0, vector transform, vector shift, float needlineofsight)
560 // blast origin of current search original blast origin how to untransform (victim to blast system)
564 vector shift_new, transform_new;
570 e0 = findradius(org, rad);
573 for(e = e0; e; e = e.chain)
575 p = WarpZoneLib_NearestPointOnBox(e.origin + e.mins, e.origin + e.maxs, org0);
578 traceline(org, p, MOVE_NOMONSTERS, e);
579 if(trace_fraction < 1)
582 if(!e.WarpZone_findradius_hit || vlen(e.WarpZone_findradius_dist) > vlen(org0 - p))
584 e.WarpZone_findradius_nearest = p;
585 e.WarpZone_findradius_dist = org0 - p;
586 e.WarpZone_findradius_findorigin = org;
587 e.WarpZone_findradius_findradius = rad;
588 if(e.classname == "warpzone_refsys")
590 // ignore, especially: do not overwrite the refsys parameters
592 else if(e.classname == "trigger_warpzone")
594 e.WarpZone_findradius_next = wz;
596 e.WarpZone_findradius_hit = 1;
597 e.enemy.WarpZone_findradius_dist = '0 0 0'; // we don't want to go through this zone ever again
598 e.enemy.WarpZone_findradius_hit = 1;
602 e.warpzone_transform = transform;
603 e.warpzone_shift = shift;
604 e.WarpZone_findradius_hit = 1;
608 for(e = wz; e; e = e.WarpZone_findradius_next)
610 org0_new = WarpZone_TransformOrigin(e, org);
611 traceline(e.warpzone_targetorigin, org0_new, MOVE_NOMONSTERS, e);
612 org_new = trace_endpos;
614 transform_new = AnglesTransform_Multiply(e.warpzone_transform, transform);
615 shift_new = AnglesTransform_Multiply_GetPostShift(e.warpzone_transform, e.warpzone_shift, transform, shift);
616 WarpZone_FindRadius_Recurse(
618 bound(0, rad - vlen(org_new - org0_new), rad - 8),
620 transform_new, shift_new,
622 e.WarpZone_findradius_hit = 0;
623 e.enemy.WarpZone_findradius_hit = 0;
626 entity WarpZone_FindRadius(vector org, float rad, float needlineofsight)
629 WarpZone_FindRadius_Recurse(org, rad, org, '0 0 0', '0 0 0', needlineofsight);
630 e0 = findchainfloat(WarpZone_findradius_hit, 1);
631 for(e = e0; e; e = e.chain)
632 e.WarpZone_findradius_hit = 0;
636 .entity WarpZone_refsys;
637 void WarpZone_RefSys_GC()
639 // garbage collect unused reference systems
640 self.nextthink = time + 1;
641 if(self.owner.WarpZone_refsys != self)
644 void WarpZone_RefSys_Add(entity me, entity wz)
646 if(me.WarpZone_refsys.owner != me)
648 me.WarpZone_refsys = spawn();
649 me.WarpZone_refsys.classname = "warpzone_refsys";
650 me.WarpZone_refsys.owner = me;
651 me.WarpZone_refsys.think = WarpZone_RefSys_GC;
652 me.WarpZone_refsys.nextthink = time + 1;
653 WarpZone_Accumulator_Clear(me.WarpZone_refsys);
656 WarpZone_Accumulator_Add(me.WarpZone_refsys, wz);
658 .vector WarpZone_refsys_incremental_shift;
659 .vector WarpZone_refsys_incremental_transform;
660 void WarpZone_RefSys_AddIncrementally(entity me, entity ref)
663 if(me.WarpZone_refsys_incremental_transform == ref.WarpZone_refsys.warpzone_transform)
664 if(me.WarpZone_refsys_incremental_shift == ref.WarpZone_refsys.warpzone_shift)
666 if(me.WarpZone_refsys.owner != me)
668 me.WarpZone_refsys = spawn();
669 me.WarpZone_refsys.classname = "warpzone_refsys";
670 me.WarpZone_refsys.owner = me;
671 me.WarpZone_refsys.think = WarpZone_RefSys_GC;
672 me.WarpZone_refsys.nextthink = time + 1;
673 WarpZone_Accumulator_Clear(me.WarpZone_refsys);
675 t = AnglesTransform_Invert(me.WarpZone_refsys_incremental_transform);
676 s = AnglesTransform_PrePostShift_GetPostShift(me.WarpZone_refsys_incremental_shift, t, '0 0 0');
677 WarpZone_Accumulator_AddTransform(me.WarpZone_refsys, t, s);
678 WarpZone_Accumulator_Add(me.WarpZone_refsys, ref.WarpZone_refsys);
679 me.WarpZone_refsys_incremental_shift = ref.WarpZone_refsys.warpzone_shift;
680 me.WarpZone_refsys_incremental_transform = ref.WarpZone_refsys.warpzone_transform;
682 void WarpZone_RefSys_BeginAddingIncrementally(entity me, entity ref)
684 me.WarpZone_refsys_incremental_shift = ref.WarpZone_refsys.warpzone_shift;
685 me.WarpZone_refsys_incremental_transform = ref.WarpZone_refsys.warpzone_transform;
687 vector WarpZone_RefSys_TransformOrigin(entity from, entity to, vector org)
689 if(from.WarpZone_refsys)
690 org = WarpZone_UnTransformOrigin(from.WarpZone_refsys, org);
691 if(to.WarpZone_refsys)
692 org = WarpZone_TransformOrigin(to.WarpZone_refsys, org);
695 vector WarpZone_RefSys_TransformVelocity(entity from, entity to, vector vel)
697 if(from.WarpZone_refsys)
698 vel = WarpZone_UnTransformVelocity(from.WarpZone_refsys, vel);
699 if(to.WarpZone_refsys)
700 vel = WarpZone_TransformVelocity(to.WarpZone_refsys, vel);
703 vector WarpZone_RefSys_TransformAngles(entity from, entity to, vector ang)
705 if(from.WarpZone_refsys)
706 ang = WarpZone_UnTransformAngles(from.WarpZone_refsys, ang);
707 if(to.WarpZone_refsys)
708 ang = WarpZone_TransformAngles(to.WarpZone_refsys, ang);
711 vector WarpZone_RefSys_TransformVAngles(entity from, entity to, vector ang)
713 if(from.WarpZone_refsys)
714 ang = WarpZone_UnTransformVAngles(from.WarpZone_refsys, ang);
715 if(to.WarpZone_refsys)
716 ang = WarpZone_TransformVAngles(to.WarpZone_refsys, ang);
719 entity WarpZone_RefSys_SpawnSameRefSys(entity me)
723 if(me.WarpZone_refsys)
725 e.WarpZone_refsys = spawn();
726 e.WarpZone_refsys.classname = "warpzone_refsys";
727 e.WarpZone_refsys.owner = e;
728 e.WarpZone_refsys.think = WarpZone_RefSys_GC;
729 e.WarpZone_refsys.nextthink = time + 1;
730 e.WarpZone_refsys.warpzone_shift = me.WarpZone_refsys.warpzone_shift;
731 e.WarpZone_refsys.warpzone_transform = me.WarpZone_refsys.warpzone_transform;