]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/w_common.qc
fix extra casings :P
[divverent/nexuiz.git] / data / qcsrc / server / w_common.qc
1
2 void W_GiveWeapon (entity e, float wep, string name)
3 {
4         entity oldself;
5
6         if (!wep)
7                 return;
8
9         e.weapons = e.weapons | W_WeaponBit(wep);
10
11         oldself = self;
12         self = e;
13
14         if (other.classname == "player")
15         {
16                 sprint (other, "You got the ^2");
17                 sprint (other, name);
18                 sprint (other, "\n");
19         }
20
21
22         self = oldself;
23 }
24
25 void FireRailgunBullet (vector start, vector end, float bdamage, float bforce, float deathtype)
26 {
27         local vector hitloc, force, endpoint, dir;
28         local entity ent, endent;
29         local float endq3surfaceflags;
30         //local entity explosion;
31         
32         railgun_start = start;
33         railgun_end = end;
34
35         dir = normalize(end - start);
36         force = dir * bforce;
37
38         // go a little bit into the wall because we need to hit this wall later
39         end = end + dir;
40
41         // trace multiple times until we hit a wall, each obstacle will be made
42         // non-solid so we can hit the next, while doing this we spawn effects and
43         // note down which entities were hit so we can damage them later
44         while (1)
45         {
46                 if(self.antilag_debug)
47                         traceline_antilag (self, start, end, FALSE, self, self.antilag_debug);
48                 else
49                         traceline_antilag (self, start, end, FALSE, self, ANTILAG_LATENCY(self));
50
51                 // if it is world we can't hurt it so stop now
52                 if (trace_ent == world || trace_fraction == 1)
53                         break;
54
55                 // make the entity non-solid so we can hit the next one
56                 trace_ent.railgunhit = TRUE;
57                 trace_ent.railgunhitloc = end;
58                 trace_ent.railgunhitsolidbackup = trace_ent.solid;
59
60                 // stop if this is a wall
61                 if (trace_ent.solid == SOLID_BSP)
62                         break;
63
64                 // make the entity non-solid
65                 trace_ent.solid = SOLID_NOT;
66         }
67
68         endpoint = trace_endpos;
69         endent = trace_ent;
70         endq3surfaceflags = trace_dphitq3surfaceflags;
71
72         // find all the entities the railgun hit and restore their solid state
73         ent = findfloat(world, railgunhit, TRUE);
74         while (ent)
75         {
76                 // restore their solid type
77                 ent.solid = ent.railgunhitsolidbackup;
78                 ent = findfloat(ent, railgunhit, TRUE);
79         }
80
81         // spawn a temporary explosion entity for RadiusDamage calls
82         //explosion = spawn();
83
84         // find all the entities the railgun hit and hurt them
85         ent = findfloat(world, railgunhit, TRUE);
86         while (ent)
87         {
88                 // get the details we need to call the damage function
89                 hitloc = ent.railgunhitloc;
90                 ent.railgunhitloc = '0 0 0';
91                 ent.railgunhitsolidbackup = SOLID_NOT;
92                 ent.railgunhit = FALSE;
93
94                 // apply the damage
95                 if (ent.takedamage || ent.classname == "case")
96                         Damage (ent, self, self, bdamage, deathtype, hitloc, force);
97
98                 // create a small explosion to throw gibs around (if applicable)
99                 //setorigin (explosion, hitloc);
100                 //RadiusDamage (explosion, self, 10, 0, 50, world, 300, deathtype);
101
102                 // advance to the next entity
103                 ent = findfloat(ent, railgunhit, TRUE);
104         }
105
106         // we're done with the explosion entity, remove it
107         //remove(explosion);
108
109         trace_endpos = endpoint;
110         trace_ent = endent;
111         trace_dphitq3surfaceflags = endq3surfaceflags;
112 }
113
114 .float dmg_edge;
115 .float dmg_force;
116 .float dmg_radius;
117 void W_BallisticBullet_Hit (void)
118 {
119         vector org2;
120         float f;
121
122         org2 = self.origin - 6 * normalize(self.oldvelocity);
123         if (DEATH_ISWEAPON(self.projectiledeathtype, WEP_SHOTGUN))
124                 pointparticles(particleeffectnum("shotgun_impact"), org2, normalize(self.velocity) * 1000, 1);
125         else
126                 pointparticles(particleeffectnum("machinegun_impact"), org2, normalize(self.velocity) * 1000, 1);
127
128         if(other && other != self.enemy)
129         {
130                 self.enemy = other; // don't hit the same player twice with the same bullet
131
132                 f = vlen(self.velocity) / vlen(self.oldvelocity);
133
134                 headshot = 0;
135                 yoda = 0;
136                 damage_headshotbonus = self.dmg_edge;
137                 railgun_start = self.origin - 2 * frametime * self.oldvelocity;
138                 railgun_end = self.origin + 2 * frametime * self.oldvelocity;
139                 Damage(other, self, self.owner, self.dmg * f, self.projectiledeathtype, self.origin, self.dmg_force * normalize(self.velocity) * f);
140                 damage_headshotbonus = 0;
141
142                 if(self.dmg_edge != 0)
143                 {
144                         if(headshot)
145                                 announce(self.owner, "announcer/male/headshot.wav");
146                         if(yoda)
147                                 announce(self.owner, "announcer/male/yoda.wav");
148                 }
149
150                 //sound (self, CHAN_PROJECTILE, "weapons/electro_impact.wav", VOL_BASE, ATTN_NORM);
151         }
152 }
153
154 .void(void) W_BallisticBullet_LeaveSolid_think_save;
155 .float W_BallisticBullet_LeaveSolid_nextthink_save;
156 .vector W_BallisticBullet_LeaveSolid_origin;
157 .vector W_BallisticBullet_LeaveSolid_velocity;
158
159 void W_BallisticBullet_LeaveSolid_think()
160 {
161         vector org2;
162
163         setorigin(self, self.W_BallisticBullet_LeaveSolid_origin);
164         self.velocity = self.W_BallisticBullet_LeaveSolid_velocity;
165
166         self.think = self.W_BallisticBullet_LeaveSolid_think_save;
167         self.nextthink = max(time, self.W_BallisticBullet_LeaveSolid_nextthink_save) + 1;
168         self.W_BallisticBullet_LeaveSolid_think_save = SUB_Null;
169
170         self.flags &~= FL_ONGROUND;
171         self.effects &~= EF_NODRAW;
172
173         org2 = self.origin - 6 * normalize(self.oldvelocity);
174         if (DEATH_ISWEAPON(self.projectiledeathtype, WEP_SHOTGUN))
175                 pointparticles(particleeffectnum("shotgun_impact"), org2, normalize(self.velocity) * 1000, 1);
176         else
177                 pointparticles(particleeffectnum("machinegun_impact"), org2, normalize(self.velocity) * 1000, 1);
178 }
179
180 // a fake logarithm function
181 float log(float x)
182 {
183         if(x < 0.0001)
184                 return 0;
185         if(x > 0.9 && x < 1.1)
186                 return x - 1;
187         return 2 * log(sqrt(x));
188 }
189
190 float W_BallisticBullet_LeaveSolid(entity e, vector vel, float speedhalflife)
191 {
192         // move the entity along its velocity until it's out of solid, then let it resume
193         
194         vector tracevel, org, skiporg, endorg, t;
195         float dt, dst, velfactor, v0;
196         float maxdist;
197
198         speedhalflife *= 1.442695040888963; // distance for 1/eth of the speed
199         v0 = vlen(vel);
200
201         // maxdist: max distance that CAN be travelled using current velocity and speed halflife
202         //
203         // v(t) = v(0) * e^(-t / speedhalflife)
204         // integrate
205         // V(t) = - v(0) * e^(-t / speedhalflife) * speedhalflife
206         // s(t) = V(t) - V(0)
207         // s(t) = (speedhalflife * v(0)) * (1 - e^(-t / speedhalflife))
208         // lim s = speedhalflife * v(0)
209         // t(s) = speedhalflife * log((speedhalflife * v(0)) / (speedhalflife * v(0) - s))
210         // v(s) = (speedhalflife * v(0) - s) / speedhalflife
211
212         maxdist = speedhalflife * v0;
213         //print("max dist = ", ftos(maxdist), "\n");
214
215         if(maxdist <= 0)
216                 return 0;
217
218         tracevel = normalize(vel);
219
220         org = self.origin;
221         skiporg = org + tracevel;
222         endorg = org + tracevel * maxdist;
223
224         for(;;)
225         {
226                 traceline(skiporg, endorg, MOVE_NORMAL, self);
227                 t = trace_endpos;
228
229                 if(trace_startsolid)
230                 {
231                         // good: skiporg is actually in solid
232                         traceline(t, skiporg, MOVE_NORMAL, self);
233                         t = trace_endpos;
234
235                         if(trace_startsolid)
236                         {
237                                 // we're stuck inside solid :(
238                                 // force advance by 1 unit, and retry
239                                 // CAN we go by 1 unit?
240                                 if(vlen(skiporg + tracevel - org) < maxdist)
241                                         skiporg = skiporg + tracevel;
242                                 else
243                                         return 0;
244                         }
245                         else
246                         {
247                                 // we managed to leave solid
248                                 // so trace_endpos is good
249                                 self.W_BallisticBullet_LeaveSolid_origin = t;
250                                 break;
251                         }
252                 }
253                 else
254                 {
255                         // bad: skiporg is outside solid. Then imagine it's alright.
256                         self.W_BallisticBullet_LeaveSolid_origin = skiporg;
257                         break;
258                 }
259         }
260
261         dst = vlen(self.W_BallisticBullet_LeaveSolid_origin - org);
262         velfactor = (speedhalflife * v0 - dst) / (speedhalflife * v0);
263
264         // t(s) = speedhalflife * log((speedhalflife * v(0)) / (speedhalflife * v(0) - s))
265         dt = speedhalflife * log((speedhalflife * v0) / (speedhalflife * v0 - dst));
266
267         //print("slowdown by ", ftos(dst), " units = ", ftos(velfactor), "\n");
268         //print("takes time ", ftos(dt), "\n");
269
270         self.W_BallisticBullet_LeaveSolid_think_save = self.think;
271         self.W_BallisticBullet_LeaveSolid_nextthink_save = self.nextthink;
272         self.think = W_BallisticBullet_LeaveSolid_think;
273         self.nextthink = time + dt;
274
275         vel = vel * velfactor;
276
277         self.velocity = '0 0 0';
278         self.flags |= FL_ONGROUND; // prevent moving
279         self.effects |= EF_NODRAW;
280         self.W_BallisticBullet_LeaveSolid_velocity = vel;
281
282         return 1;
283 }
284
285 void W_BallisticBullet_Touch (void)
286 {
287         if(self.think == W_BallisticBullet_LeaveSolid_think) // skip this!
288                 return;
289
290         PROJECTILE_TOUCH;
291         W_BallisticBullet_Hit ();
292
293         // go through solid!
294         if(!W_BallisticBullet_LeaveSolid(self, self.velocity, self.dmg_radius))
295         {
296                 remove(self);
297                 return;
298         }
299
300         self.projectiledeathtype |= HITTYPE_BOUNCE;
301 }
302
303 void fireBallisticBullet(vector start, vector dir, float spread, float pSpeed, float lifetime, float damage, float headshotbonus, float force, float dtype, float tracereffects, float gravityfactor)
304 {
305         entity proj;
306         proj = spawn();
307         proj.owner = self;
308         proj.solid = SOLID_BBOX;
309         if(gravityfactor > 0)
310         {
311                 proj.movetype = MOVETYPE_TOSS;
312                 proj.gravity = gravityfactor;
313         }
314         else
315                 proj.movetype = MOVETYPE_FLY;
316         proj.think = SUB_Remove;
317         proj.nextthink = time + lifetime; // min(pLifetime, vlen(world.maxs - world.mins) / pSpeed);
318         proj.velocity = (dir + randomvec() * spread) * pSpeed;
319         W_SetupProjectileVelocity(proj);
320         proj.angles = vectoangles(proj.velocity);
321         proj.dmg_radius = cvar("g_ballistics_solidspeedhalflife");
322         setmodel(proj, "models/tracer.mdl");
323         setsize(proj, '0 0 0', '0 0 0');
324         setorigin(proj, start);
325         proj.effects = EF_LOWPRECISION | tracereffects;
326         proj.flags = FL_PROJECTILE;
327
328         proj.touch = W_BallisticBullet_Touch;
329         proj.dmg = damage;
330         proj.dmg_edge = headshotbonus;
331         proj.dmg_force = force;
332         proj.projectiledeathtype = dtype;
333
334         proj.oldvelocity = proj.velocity;
335 }
336
337 void fireBullet (vector start, vector dir, float spread, float damage, float force, float dtype, float tracer)
338 {
339         vector  end;
340         local entity e;
341
342         if(cvar("g_ballistics_force"))
343         {
344                 if (DEATH_ISWEAPON(dtype, WEP_SHOTGUN))
345                         fireBallisticBullet(start, dir, spread, cvar("g_ballistics_force_shotgun_speed"), 5, damage, 0, force, dtype, 0, 1);
346                 else
347                         fireBallisticBullet(start, dir, spread, cvar("g_ballistics_force_uzi_speed"), 5, damage, 0, force, dtype, 0, 1);
348                 return;
349         }
350
351         dir = dir + randomvec() * spread;
352         end = start + dir * MAX_SHOT_DISTANCE;
353         if(self.antilag_debug)
354                 traceline_antilag (self, start, end, FALSE, self, self.antilag_debug);
355         else
356                 traceline_antilag (self, start, end, FALSE, self, ANTILAG_LATENCY(self));
357
358         if (tracer)
359         {
360                 e = spawn();
361                 e.owner = self;
362                 e.movetype = MOVETYPE_FLY;
363                 e.solid = SOLID_NOT;
364                 e.think = SUB_Remove;
365                 e.nextthink = time + vlen(trace_endpos - start) / 6000;
366                 e.velocity = dir * 6000;
367                 e.angles = vectoangles(e.velocity);
368                 setmodel (e, "models/tracer.mdl"); // precision set below
369                 setsize (e, '0 0 0', '0 0 0');
370                 setorigin (e, start);
371                 e.effects = EF_LOWPRECISION;
372                 e.flags = FL_PROJECTILE;
373         }
374
375         if ((trace_fraction != 1.0) && (pointcontents (trace_endpos) != CONTENT_SKY))
376         {
377                 if (trace_ent.solid == SOLID_BSP && !(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))
378                 {
379                         if (DEATH_ISWEAPON(dtype, WEP_SHOTGUN))
380                                 pointparticles(particleeffectnum("shotgun_impact"), trace_endpos, trace_plane_normal * 1000, 1);
381                         else
382                                 pointparticles(particleeffectnum("machinegun_impact"), trace_endpos, trace_plane_normal * 1000, 1);
383                 }
384                 Damage (trace_ent, self, self, damage, dtype, trace_endpos, dir * force);
385         }
386 }
387
388 void W_PrepareExplosionByDamage(entity attacker, void() explode)
389 {
390         self.takedamage = DAMAGE_NO;
391         self.event_damage = SUB_Null;
392         self.owner = attacker;
393
394         // do not explode NOW but in the NEXT FRAME!
395         // because recursive calls to RadiusDamage are not allowed
396         self.nextthink = time;
397         self.think = explode;
398 }