1 float trace_dphitcontents;
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_Divide(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_InitTransform();
195 if(!warpzone_warpzones_exist)
197 if(nomonsters == MOVE_NOTHING)
202 cb(org, trace_endpos, end);
207 tracebox(org, mi, ma, end, nomonsters, forent);
209 cb(org, trace_endpos, end);
224 nomonsters_adjusted = MOVE_NOMONSTERS;
227 nomonsters_adjusted = nomonsters;
230 if((contentshack = (forent.dphitcontentsmask && !(forent.dphitcontentsmask & DPCONTENTS_SOLID))))
231 forent.dphitcontentsmask |= DPCONTENTS_SOLID;
233 // if starting in warpzone, first transform
234 wz = WarpZone_Find(org + mi, org + ma);
237 if(zone && wz != zone)
239 // we are in ANOTHER warpzone. This is bad. Make a zero length trace and return.
245 WarpZone_Trace_AddTransform(wz);
246 org = WarpZone_TransformOrigin(wz, org);
247 end = WarpZone_TransformOrigin(wz, end);
249 WarpZone_MakeAllSolid();
257 dprint("Too many warpzones in sequence, aborting trace.\n");
261 tracebox(org, mi, ma, end, nomonsters_adjusted, forent);
263 cb(org, trace_endpos, end);
265 sol = trace_startsolid;
267 frac = trace_fraction = frac + (1 - frac) * trace_fraction;
268 if(trace_fraction >= 1)
270 if(trace_ent.classname != "trigger_warpzone")
272 if((nomonsters == MOVE_NOTHING) || ((nomonsters == MOVE_WORLDONLY) && trace_ent) || (contentshack && (trace_dphitcontents & forent.dphitcontentsmask) == DPCONTENTS_SOLID))
274 // continue the trace, ignoring this hit (we only care for warpzones)
275 org = trace_endpos + normalize(end - org);
277 // we cannot do an inverted trace here, as we do care for further warpzones inside that "solid" to be found
278 // otherwise, players could block entrances that way
284 dprint("I transformed into the same zone again, wtf, aborting the trace\n");
289 if(zone && wz != zone)
291 WarpZone_Trace_AddTransform(wz);
292 // we hit a warpzone... so, let's perform the trace after the warp again
293 org = WarpZone_TransformOrigin(wz, trace_endpos);
294 end = WarpZone_TransformOrigin(wz, end);
296 WarpZone_MakeAllOther();
299 forent.dphitcontentsmask &~= DPCONTENTS_SOLID;
300 trace_startsolid = sol;
306 void WarpZone_TraceBox(vector org, vector mi, vector ma, vector end, float nomonsters, entity forent)
308 WarpZone_TraceBox_ThroughZone(org, mi, ma, end, nomonsters, forent, world, WarpZone_trace_callback_t_null);
311 void WarpZone_TraceLine(vector org, vector end, float nomonsters, entity forent)
313 WarpZone_TraceBox(org, '0 0 0', '0 0 0', end, nomonsters, forent);
316 void WarpZone_TraceToss_ThroughZone(entity e, entity forent, entity zone, WarpZone_trace_callback_t cb)
319 vector vf, vr, vu, v0, o0;
325 WarpZone_Trace_InitTransform();
326 WarpZone_tracetoss_time = 0;
327 if(!warpzone_warpzones_exist)
329 tracetoss(e, forent);
331 cb(e.origin, trace_endpos, trace_endpos);
332 dt = vlen(e.origin - o0) / vlen(e.velocity);
333 WarpZone_tracetoss_time += dt;
334 e.velocity_z -= dt * g;
335 WarpZone_tracetoss_velocity = e.velocity;
344 // if starting in warpzone, first transform
345 wz = WarpZone_Find(e.origin + e.mins, e.origin + e.maxs);
348 if(zone && wz != zone)
350 // we are in ANOTHER warpzone. This is bad. Make a zero length trace and return.
352 WarpZone_tracetoss_time = 0;
356 WarpZone_Trace_AddTransform(wz);
357 setorigin(e, WarpZone_TransformOrigin(wz, e.origin));
358 e.velocity = WarpZone_TransformVelocity(wz, e.velocity);
360 WarpZone_MakeAllSolid();
361 g = cvar("sv_gravity") * e.gravity;
367 dprint("Too many warpzones in sequence, aborting trace.\n");
371 tracetoss(e, forent);
373 cb(e.origin, trace_endpos, trace_endpos);
374 e.origin = trace_endpos;
375 dt = vlen(e.origin - o0) / vlen(e.velocity);
376 WarpZone_tracetoss_time += dt;
377 e.velocity_z -= dt * g;
378 if(trace_fraction >= 1)
380 if(trace_ent.classname != "trigger_warpzone")
384 dprint("I transformed into the same zone again, wtf, aborting the trace\n");
389 if(zone && wz != zone)
391 WarpZone_Trace_AddTransform(wz);
392 // we hit a warpzone... so, let's perform the trace after the warp again
393 e.origin = WarpZone_TransformOrigin(wz, e.origin);
394 e.velocity = WarpZone_TransformVelocity(wz, e.velocity);
396 WarpZone_MakeAllOther();
398 WarpZone_tracetoss_velocity = e.velocity;
402 // restore old entity data (caller just uses trace_endpos, WarpZone_tracetoss_velocity and the transform)
407 void WarpZone_TraceToss(entity e, entity forent)
409 WarpZone_TraceToss_ThroughZone(e, forent, world, WarpZone_trace_callback_t_null);
412 entity WarpZone_TrailParticles_trace_callback_own;
413 float WarpZone_TrailParticles_trace_callback_eff;
414 void WarpZone_TrailParticles_trace_callback(vector from, vector endpos, vector to)
416 trailparticles(WarpZone_TrailParticles_trace_callback_own, WarpZone_TrailParticles_trace_callback_eff, from, endpos);
419 void WarpZone_TrailParticles(entity own, float eff, vector org, vector end)
421 WarpZone_TrailParticles_trace_callback_own = own;
422 WarpZone_TrailParticles_trace_callback_eff = eff;
423 WarpZone_TraceBox_ThroughZone(org, '0 0 0', '0 0 0', end, MOVE_NOMONSTERS, world, world, WarpZone_TrailParticles_trace_callback);
426 float WarpZone_PlaneDist(entity wz, vector v)
428 return (v - wz.warpzone_origin) * wz.warpzone_forward;
431 float WarpZone_TargetPlaneDist(entity wz, vector v)
433 return (v - wz.warpzone_targetorigin) * wz.warpzone_targetforward;
436 vector WarpZone_TransformOrigin(entity wz, vector v)
438 return wz.warpzone_shift + AnglesTransform_Apply(wz.warpzone_transform, v);
441 vector WarpZone_TransformVelocity(entity wz, vector v)
443 return AnglesTransform_Apply(wz.warpzone_transform, v);
446 vector WarpZone_TransformAngles(entity wz, vector v)
448 return AnglesTransform_ApplyToAngles(wz.warpzone_transform, v);
451 vector WarpZone_TransformVAngles(entity wz, vector ang)
459 ang = AnglesTransform_ApplyToVAngles(wz.warpzone_transform, ang);
462 ang = AnglesTransform_Normalize(ang, TRUE);
463 ang = AnglesTransform_CancelRoll(ang);
466 ang = AnglesTransform_Normalize(ang, FALSE);
472 vector WarpZone_UnTransformOrigin(entity wz, vector v)
474 return AnglesTransform_Apply(AnglesTransform_Invert(wz.warpzone_transform), v - wz.warpzone_shift);
477 vector WarpZone_UnTransformVelocity(entity wz, vector v)
479 return AnglesTransform_Apply(AnglesTransform_Invert(wz.warpzone_transform), v);
482 vector WarpZone_UnTransformAngles(entity wz, vector v)
484 return AnglesTransform_ApplyToAngles(AnglesTransform_Invert(wz.warpzone_transform), v);
487 vector WarpZone_UnTransformVAngles(entity wz, vector ang)
494 ang = AnglesTransform_ApplyToVAngles(AnglesTransform_Invert(wz.warpzone_transform), ang);
495 ang = AnglesTransform_Normalize(ang, TRUE);
496 ang = AnglesTransform_CancelRoll(ang);
502 vector WarpZoneLib_NearestPointOnBox(vector mi, vector ma, vector org)
505 nearest_x = bound(mi_x, org_x, ma_x);
506 nearest_y = bound(mi_y, org_y, ma_y);
507 nearest_z = bound(mi_z, org_z, ma_z);
511 .float WarpZone_findradius_hit;
512 .entity WarpZone_findradius_next;
513 void WarpZone_FindRadius_Recurse(vector org, float rad, vector org0, vector transform, vector shift, float needlineofsight)
514 // blast origin of current search original blast origin how to untransform (victim to blast system)
518 vector shift_new, transform_new;
524 e0 = findradius(org, rad);
527 for(e = e0; e; e = e.chain)
529 p = WarpZoneLib_NearestPointOnBox(e.origin + e.mins, e.origin + e.maxs, org0);
532 traceline(org, p, MOVE_NOMONSTERS, e);
533 if(trace_fraction < 1)
536 if(!e.WarpZone_findradius_hit || vlen(e.WarpZone_findradius_dist) > vlen(org0 - p))
538 e.WarpZone_findradius_nearest = p;
539 e.WarpZone_findradius_dist = org0 - p;
540 e.WarpZone_findradius_findorigin = org;
541 e.WarpZone_findradius_findradius = rad;
542 if(e.classname == "warpzone_refsys")
544 // ignore, especially: do not overwrite the refsys parameters
546 else if(e.classname == "trigger_warpzone")
548 e.WarpZone_findradius_next = wz;
550 e.WarpZone_findradius_hit = 1;
551 e.enemy.WarpZone_findradius_dist = '0 0 0'; // we don't want to go through this zone ever again
552 e.enemy.WarpZone_findradius_hit = 1;
556 e.warpzone_transform = transform;
557 e.warpzone_shift = shift;
558 e.WarpZone_findradius_hit = 1;
562 for(e = wz; e; e = e.WarpZone_findradius_next)
564 org0_new = WarpZone_TransformOrigin(e, org);
565 traceline(e.warpzone_targetorigin, org0_new, MOVE_NOMONSTERS, e);
566 org_new = trace_endpos;
568 transform_new = AnglesTransform_Multiply(e.warpzone_transform, transform);
569 shift_new = AnglesTransform_Multiply_GetPostShift(e.warpzone_transform, e.warpzone_shift, transform, shift);
570 WarpZone_FindRadius_Recurse(
572 bound(0, rad - vlen(org_new - org0_new), rad - 8),
574 transform_new, shift_new,
576 e.WarpZone_findradius_hit = 0;
577 e.enemy.WarpZone_findradius_hit = 0;
580 entity WarpZone_FindRadius(vector org, float rad, float needlineofsight)
583 WarpZone_FindRadius_Recurse(org, rad, org, '0 0 0', '0 0 0', needlineofsight);
584 e0 = findchainfloat(WarpZone_findradius_hit, 1);
585 for(e = e0; e; e = e.chain)
586 e.WarpZone_findradius_hit = 0;
590 .entity WarpZone_refsys;
591 void WarpZone_RefSys_GC()
593 // garbage collect unused reference systems
594 self.nextthink = time + 1;
595 if(self.owner.WarpZone_refsys != self)
598 void WarpZone_RefSys_Add(entity me, entity wz)
600 if(me.WarpZone_refsys.owner != me)
602 me.WarpZone_refsys = spawn();
603 me.WarpZone_refsys.classname = "warpzone_refsys";
604 me.WarpZone_refsys.owner = me;
605 me.WarpZone_refsys.think = WarpZone_RefSys_GC;
606 me.WarpZone_refsys.nextthink = time + 1;
607 WarpZone_Accumulator_Clear(me.WarpZone_refsys);
610 WarpZone_Accumulator_Add(me.WarpZone_refsys, wz);
612 .vector WarpZone_refsys_incremental_shift;
613 .vector WarpZone_refsys_incremental_transform;
614 void WarpZone_RefSys_AddIncrementally(entity me, entity ref)
617 if(me.WarpZone_refsys_incremental_transform == ref.WarpZone_refsys.warpzone_transform)
618 if(me.WarpZone_refsys_incremental_shift == ref.WarpZone_refsys.warpzone_shift)
620 if(me.WarpZone_refsys.owner != me)
622 me.WarpZone_refsys = spawn();
623 me.WarpZone_refsys.classname = "warpzone_refsys";
624 me.WarpZone_refsys.owner = me;
625 me.WarpZone_refsys.think = WarpZone_RefSys_GC;
626 me.WarpZone_refsys.nextthink = time + 1;
627 WarpZone_Accumulator_Clear(me.WarpZone_refsys);
629 t = AnglesTransform_Invert(me.WarpZone_refsys_incremental_transform);
630 s = AnglesTransform_PrePostShift_GetPostShift(me.WarpZone_refsys_incremental_shift, t, '0 0 0');
631 WarpZone_Accumulator_AddTransform(me.WarpZone_refsys, t, s);
632 WarpZone_Accumulator_Add(me.WarpZone_refsys, ref);
633 me.WarpZone_refsys_incremental_shift = ref.WarpZone_refsys.warpzone_shift;
634 me.WarpZone_refsys_incremental_transform = ref.WarpZone_refsys.warpzone_transform;
636 void WarpZone_RefSys_BeginAddingIncrementally(entity me, entity ref)
638 me.WarpZone_refsys_incremental_shift = ref.WarpZone_refsys.warpzone_shift;
639 me.WarpZone_refsys_incremental_transform = ref.WarpZone_refsys.warpzone_transform;
641 vector WarpZone_RefSys_TransformOrigin(entity from, entity to, vector org)
643 if(from.WarpZone_refsys)
644 org = WarpZone_UnTransformOrigin(from.WarpZone_refsys, org);
645 if(to.WarpZone_refsys)
646 org = WarpZone_TransformOrigin(to.WarpZone_refsys, org);
649 vector WarpZone_RefSys_TransformVelocity(entity from, entity to, vector vel)
651 if(from.WarpZone_refsys)
652 vel = WarpZone_UnTransformVelocity(from.WarpZone_refsys, vel);
653 if(to.WarpZone_refsys)
654 vel = WarpZone_TransformVelocity(to.WarpZone_refsys, vel);
657 vector WarpZone_RefSys_TransformAngles(entity from, entity to, vector ang)
659 if(from.WarpZone_refsys)
660 ang = WarpZone_UnTransformAngles(from.WarpZone_refsys, ang);
661 if(to.WarpZone_refsys)
662 ang = WarpZone_TransformAngles(to.WarpZone_refsys, ang);
665 vector WarpZone_RefSys_TransformVAngles(entity from, entity to, vector ang)
667 if(from.WarpZone_refsys)
668 ang = WarpZone_UnTransformVAngles(from.WarpZone_refsys, ang);
669 if(to.WarpZone_refsys)
670 ang = WarpZone_TransformVAngles(to.WarpZone_refsys, ang);
673 entity WarpZone_RefSys_SpawnSameRefSys(entity me)
677 if(me.WarpZone_refsys)
679 e.WarpZone_refsys = spawn();
680 e.WarpZone_refsys.classname = "warpzone_refsys";
681 e.WarpZone_refsys.owner = e;
682 e.WarpZone_refsys.think = WarpZone_RefSys_GC;
683 e.WarpZone_refsys.nextthink = time + 1;
684 e.WarpZone_refsys.warpzone_shift = me.WarpZone_refsys.warpzone_shift;
685 e.WarpZone_refsys.warpzone_transform = me.WarpZone_refsys.warpzone_transform;