findradius wrapper - more guns work now
[divverent/nexuiz.git] / data / qcsrc / warpzonelib / common.qc
1 void WarpZone_SetUp(entity e, vector my_org, vector my_ang, vector other_org, vector other_ang)
2 {
3         e.warpzone_transform = AnglesTransform_Divide(other_ang, AnglesTransform_TurnDirectionFR(my_ang));
4         e.warpzone_origin = my_org;
5         e.warpzone_targetorigin = other_org;
6         e.warpzone_angles = my_ang;
7         e.warpzone_targetangles = other_ang;
8         fixedmakevectors(my_ang); e.warpzone_forward = v_forward;
9         fixedmakevectors(other_ang); e.warpzone_targetforward = v_forward;
10 }
11
12 .entity enemy;
13
14 vector WarpZoneLib_BoxTouchesBrush_mins;
15 vector WarpZoneLib_BoxTouchesBrush_maxs;
16 entity WarpZoneLib_BoxTouchesBrush_ent;
17 entity WarpZoneLib_BoxTouchesBrush_ignore;
18 float WarpZoneLib_BoxTouchesBrush_Recurse()
19 {
20         float s;
21         entity se;
22         float f;
23
24         tracebox('0 0 0', WarpZoneLib_BoxTouchesBrush_mins, WarpZoneLib_BoxTouchesBrush_maxs, '0 0 0', MOVE_NOMONSTERS, WarpZoneLib_BoxTouchesBrush_ignore);
25 #ifdef CSQC
26         if (trace_networkentity)
27         {
28                 dprint("hit a network ent, cannot continue WarpZoneLib_BoxTouchesBrush\n");
29                 // we cannot continue, as a player blocks us...
30                 // so, abort
31                 return 0;
32         }
33 #endif
34         if not(trace_ent)
35                 return 0;
36         if (trace_ent == WarpZoneLib_BoxTouchesBrush_ent)
37                 return 1;
38
39         
40
41         se = trace_ent;
42         s = se.solid;
43         se.solid = SOLID_NOT;
44         f = WarpZoneLib_BoxTouchesBrush_Recurse();
45         se.solid = s;
46
47         return f;
48 }
49
50 float WarpZoneLib_BoxTouchesBrush(vector mi, vector ma, entity e, entity ig)
51 {
52     float f, s;
53
54     if not(e.modelindex)
55         return 1;
56
57     s = e.solid;
58     e.solid = SOLID_BSP;
59     WarpZoneLib_BoxTouchesBrush_mins = mi;
60     WarpZoneLib_BoxTouchesBrush_maxs = ma;
61     WarpZoneLib_BoxTouchesBrush_ent = e;
62     WarpZoneLib_BoxTouchesBrush_ignore = ig;
63     f = WarpZoneLib_BoxTouchesBrush_Recurse();
64     e.solid = s;
65
66     return f;
67 }
68
69 entity WarpZone_Find(vector mi, vector ma)
70 {
71         // if we are near any warpzone planes - MOVE AWAY (work around nearclip)
72         entity e;
73         for(e = world; (e = find(e, classname, "trigger_warpzone")); )
74                 if(WarpZoneLib_BoxTouchesBrush(mi, ma, e, world))
75                         return e;
76         return world;
77 }
78
79 void WarpZone_MakeAllSolid()
80 {
81         entity e;
82         for(e = world; (e = find(e, classname, "trigger_warpzone")); )
83                 e.solid = SOLID_BSP;
84 }
85
86 void WarpZone_MakeAllOther()
87 {
88         entity e;
89         for(e = world; (e = find(e, classname, "trigger_warpzone")); )
90                 e.solid = SOLID_TRIGGER;
91 }
92
93 void WarpZone_TraceBox(vector org, vector mi, vector ma, vector end, float nomonsters, entity forent)
94 {
95         float frac, sol;
96         vector o0, e0;
97         entity e;
98         vector vf, vr, vu;
99         vf = v_forward;
100         vr = v_right;
101         vu = v_up;
102         WarpZone_MakeAllSolid();
103         o0 = org;
104         e0 = end;
105         sol = -1;
106         frac = 0;
107         e = world;
108         for(;;)
109         {
110                 tracebox(org, mi, ma, end, nomonsters, forent);
111                 if(WarpZone_trace_callback)
112                         WarpZone_trace_callback();
113                 if(sol < 0)
114                         sol = trace_startsolid;
115                 if(trace_fraction >= 1)
116                         break;
117                 frac = trace_fraction = frac + (1 - frac) * trace_fraction;
118                 if(trace_ent.classname != "trigger_warpzone")
119                         break;
120                 // we hit a warpzone... so, let's perform the trace after the warp again
121                 org = WarpZone_TransformOrigin(trace_ent, trace_endpos);
122                 end = WarpZone_TransformOrigin(trace_ent, end);
123                 WarpZone_trace_velocity = WarpZone_TransformVelocity(trace_ent, WarpZone_trace_velocity);
124                 WarpZone_trace_angles = WarpZone_TransformAngles(trace_ent, WarpZone_trace_angles);
125                 WarpZone_trace_v_angle = WarpZone_TransformVAngles(trace_ent, WarpZone_trace_v_angle);
126                 if(trace_ent == e)
127                 {
128                         dprint("I transformed into the same zone again, wtf, aborting the trace\n");
129                         break;
130                 }
131                 e = trace_ent;
132         }
133         WarpZone_MakeAllOther();
134         trace_startsolid = sol;
135         WarpZone_trace_endpos = o0 + (e0 - o0) * trace_fraction;
136         v_forward = vf;
137         v_right = vr;
138         v_up = vu;
139 }
140
141 void WarpZone_TraceLine(vector org, vector end, float nomonsters, entity forent)
142 {
143         WarpZone_TraceBox(org, '0 0 0', '0 0 0', end, nomonsters, forent);
144 }
145
146 void WarpZone_TraceToss(entity e, entity forent)
147 {
148         float g, dt;
149         vector vf, vr, vu, v0, o0;
150         vf = v_forward;
151         vr = v_right;
152         vu = v_up;
153         WarpZone_MakeAllSolid();
154         g = cvar("sv_gravity") * e.gravity;
155         WarpZone_tracetoss_time = 0;
156         v0 = e.velocity;
157         o0 = e.origin;
158         for(;;)
159         {
160                 tracetoss(e, forent);
161                 if(WarpZone_trace_callback)
162                         WarpZone_trace_callback();
163                 e.origin = trace_endpos;
164                 dt = vlen(e.origin - o0) / vlen(e.velocity);
165                 WarpZone_tracetoss_time += dt;
166                 e.velocity_z -= WarpZone_tracetoss_time * g;
167                 if(trace_fraction >= 1)
168                         break;
169                 if(trace_ent.classname != "trigger_warpzone")
170                         break;
171                 // we hit a warpzone... so, let's perform the trace after the warp again
172                 e.origin = WarpZone_TransformOrigin(trace_ent, e.origin);
173                 e.velocity = WarpZone_TransformVelocity(trace_ent, e.velocity);
174         }
175         WarpZone_MakeAllOther();
176         v_forward = vf;
177         v_right = vr;
178         v_up = vu;
179         WarpZone_tracetoss_velocity = e.velocity;
180         e.velocity = v0;
181         e.origin = o0;
182         WarpZone_trace_endpos = e.origin + e.velocity * WarpZone_tracetoss_time;
183         WarpZone_trace_endpos_z -= 0.5 * g * WarpZone_tracetoss_time * WarpZone_tracetoss_time;
184 }
185
186 void WarpZone_TrailParticles(entity own, float eff, vector org, vector end)
187 {
188         float frac;
189         vector vf, vr, vu;
190         vf = v_forward;
191         vr = v_right;
192         vu = v_up;
193         WarpZone_MakeAllSolid();
194         frac = 0;
195         for(;;)
196         {
197                 traceline(org, end, MOVE_NOMONSTERS, world);
198                 //print(vtos(org), " to ", vtos(trace_endpos), "\n");
199                 trailparticles(own, eff, org, trace_endpos);
200                 if(trace_fraction >= 1)
201                         break;
202                 if(trace_ent.classname != "trigger_warpzone")
203                         break;
204                 // we hit a warpzone... so, let's perform the trace after the warp again
205                 org = WarpZone_TransformOrigin(trace_ent, trace_endpos);
206                 end = WarpZone_TransformOrigin(trace_ent, end);
207         }
208         WarpZone_MakeAllOther();
209         v_forward = vf;
210         v_right = vr;
211         v_up = vu;
212 }
213
214 float WarpZone_PlaneDist(entity wz, vector v)
215 {
216         return (v - wz.warpzone_origin) * wz.warpzone_forward;
217 }
218
219 float WarpZone_TargetPlaneDist(entity wz, vector v)
220 {
221         return (v - wz.warpzone_targetorigin) * wz.warpzone_targetforward;
222 }
223
224 vector WarpZone_TransformOrigin(entity wz, vector v)
225 {
226         return wz.warpzone_targetorigin + AnglesTransform_Apply(wz.warpzone_transform, v - wz.warpzone_origin);
227 }
228
229 vector WarpZone_TransformVelocity(entity wz, vector v)
230 {
231         return AnglesTransform_Apply(wz.warpzone_transform, v);
232 }
233
234 vector WarpZone_TransformAngles(entity wz, vector v)
235 {
236         return AnglesTransform_ApplyToAngles(wz.warpzone_transform, v);
237 }
238
239 vector WarpZone_TransformVAngles(entity wz, vector ang)
240 {
241         float roll;
242
243         roll = ang_z;
244         ang_z = 0;
245
246         ang = AnglesTransform_ApplyToVAngles(wz.warpzone_transform, ang);
247         ang = AnglesTransform_Normalize(ang, TRUE);
248         ang = AnglesTransform_CancelRoll(ang);
249
250         ang_z = roll;
251         return ang;
252 }
253
254 vector WarpZone_UnTransformOrigin(entity wz, vector v)
255 {
256         return wz.warpzone_origin + AnglesTransform_Apply(AnglesTransform_Invert(wz.warpzone_transform), v - wz.warpzone_targetorigin);
257 }
258
259 vector WarpZone_UnTransformVelocity(entity wz, vector v)
260 {
261         return AnglesTransform_Apply(AnglesTransform_Invert(wz.warpzone_transform), v);
262 }
263
264 vector WarpZone_UnTransformAngles(entity wz, vector v)
265 {
266         return AnglesTransform_ApplyToAngles(AnglesTransform_Invert(wz.warpzone_transform), v);
267 }
268
269 vector WarpZone_UnTransformVAngles(entity wz, vector ang)
270 {
271         float roll;
272
273         roll = ang_z;
274         ang_z = 0;
275
276         ang = AnglesTransform_ApplyToVAngles(AnglesTransform_Invert(wz.warpzone_transform), ang);
277         ang = AnglesTransform_Normalize(ang, TRUE);
278         ang = AnglesTransform_CancelRoll(ang);
279
280         ang_z = roll;
281         return ang;
282 }
283
284 vector WarpZoneLib_NearestPointOnBox(vector mi, vector ma, vector org)
285 {
286         vector nearest;
287         nearest_x = bound(mi_x, org_x, ma_x);
288         nearest_y = bound(mi_y, org_y, ma_y);
289         nearest_z = bound(mi_z, org_z, ma_z);
290         return nearest;
291 }
292
293 .float WarpZone_findradius_hit;
294 .entity WarpZone_findradius_next;
295 void WarpZone_FindRadius_Recurse(vector org, float rad,        vector org0,            vector shift0, vector transform, vector shift1, float needlineofsight)
296 //                               blast origin of current search   original blast origin   how to untransform (victim to blast system)
297 {
298         vector org_new;
299         vector org0_new;
300         vector shift0_new, transform_new, shift1_new;
301         vector p;
302         entity e, e0;
303         entity wz;
304         if(rad <= 0)
305                 return;
306         e0 = findradius(org, rad);
307         wz = world;
308
309         for(e = e0; e; e = e.chain)
310         {
311                 p = WarpZoneLib_NearestPointOnBox(e.origin + e.mins, e.origin + e.maxs, org0);
312                 if(needlineofsight)
313                 {
314                         traceline(org, p, MOVE_NOMONSTERS, e);
315                         if(trace_fraction < 1)
316                                 continue;
317                 }
318                 if(!e.WarpZone_findradius_hit || vlen(e.WarpZone_findradius_dist) > vlen(org0 - p))
319                 {
320                         e.WarpZone_findradius_nearest = p;
321                         e.WarpZone_findradius_dist = org0 - p;
322                         e.WarpZone_findradius_findorigin = org;
323                         e.WarpZone_findradius_findradius = rad;
324                         if(e.classname == "trigger_warpzone")
325                         {
326                                 e.WarpZone_findradius_next = wz;
327                                 wz = e;
328
329                                 e.WarpZone_findradius_hit = 1;
330                                 e.enemy.WarpZone_findradius_dist = '0 0 0'; // we don't want to go through this zone ever again
331                                 e.enemy.WarpZone_findradius_hit = 1;
332                         }
333                         else
334                         {
335                                 e.warpzone_transform = transform;
336                                 e.warpzone_origin = shift0;
337                                 e.warpzone_targetorigin = shift1;
338
339                                 e.WarpZone_findradius_hit = 1;
340                         }
341                 }
342         }
343         for(e = wz; e; e = e.WarpZone_findradius_next)
344         {
345                 org0_new = WarpZone_TransformOrigin(e, org);
346                 traceline(e.warpzone_targetorigin, org0_new, MOVE_NOMONSTERS, e);
347                 org_new = trace_endpos;
348                 transform_new = AnglesTransform_Multiply(transform, AnglesTransform_Invert(e.warpzone_transform));
349                 shift0_new = e.warpzone_targetorigin;
350                 shift1_new = AnglesTransform_Apply(transform, e.warpzone_origin - shift0) + shift1;
351                 WarpZone_FindRadius_Recurse(
352                         org_new,
353                         bound(0, rad - vlen(org_new - org0_new), rad - 8),
354                         org0_new,
355                         shift0_new, transform_new, shift1_new,
356                         needlineofsight);
357                 e.WarpZone_findradius_hit = 0;
358                 e.enemy.WarpZone_findradius_hit = 0;
359         }
360 }
361 entity WarpZone_FindRadius(vector org, float rad, float needlineofsight)
362 {
363         entity e0, e;
364         WarpZone_FindRadius_Recurse(org, rad, org, '0 0 0', '0 0 0', '0 0 0', needlineofsight);
365         e0 = findchainfloat(WarpZone_findradius_hit, 1);
366         for(e = e0; e; e = e.chain)
367                 e.WarpZone_findradius_hit = 0;
368         return e0;
369 }