]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/w_common.qc
New stuff:
[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         org2 = self.origin - 6 * normalize(self.oldvelocity);
122
123         if (DEATH_ISWEAPON(self.projectiledeathtype, WEP_SHOTGUN))
124                 pointparticles(particleeffectnum("shotgun_impact"), trace_endpos, trace_plane_normal * 1000, 1);
125         else
126                 pointparticles(particleeffectnum("machinegun_impact"), trace_endpos, trace_plane_normal * 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) leave_solid_think_save;
155 .float leave_solid_nextthink_save;
156 .vector leave_solid_origin;
157 .vector leave_solid_velocity;
158
159 void leave_solid_think()
160 {
161         setorigin(self, self.leave_solid_origin);
162         self.velocity = self.leave_solid_velocity;
163
164         self.think = self.leave_solid_think_save;
165         self.nextthink = max(time, self.leave_solid_nextthink_save) + 1;
166         self.leave_solid_think_save = SUB_Null;
167
168         self.flags &~= FL_ONGROUND;
169         self.effects &~= EF_NODRAW;
170 }
171
172 // a fake logarithm function
173 float log(float x)
174 {
175         if(x < 0.0001)
176                 return 0;
177         if(x > 0.9 && x < 1.1)
178                 return x - 1;
179         return 2 * log(sqrt(x));
180 }
181
182 float leave_solid(entity e, vector vel, float speedhalflife)
183 {
184         // move the entity along its velocity until it's out of solid, then let it resume
185         
186         vector tracevel, org, skiporg, endorg, t;
187         float dt, dst, velfactor, v0;
188         float maxdist;
189
190         speedhalflife *= 1.442695040888963; // distance for 1/eth of the speed
191         v0 = vlen(vel);
192
193         // maxdist: max distance that CAN be travelled using current velocity and speed halflife
194         //
195         // v(t) = v(0) * e^(-t / speedhalflife)
196         // integrate
197         // V(t) = - v(0) * e^(-t / speedhalflife) * speedhalflife
198         // s(t) = V(t) - V(0)
199         // s(t) = (speedhalflife * v(0)) * (1 - e^(-t / speedhalflife))
200         // lim s = speedhalflife * v(0)
201         // t(s) = speedhalflife * log((speedhalflife * v(0)) / (speedhalflife * v(0) - s))
202         // v(s) = (speedhalflife * v(0) - s) / speedhalflife
203
204         maxdist = speedhalflife * v0;
205         //print("max dist = ", ftos(maxdist), "\n");
206
207         if(maxdist <= 0)
208                 return 0;
209
210         tracevel = normalize(vel);
211
212         org = self.origin;
213         skiporg = org + tracevel;
214         endorg = org + tracevel * maxdist;
215
216         for(;;)
217         {
218                 traceline(skiporg, endorg, MOVE_NORMAL, self);
219                 t = trace_endpos;
220
221                 if(trace_startsolid)
222                 {
223                         // good: skiporg is actually in solid
224                         traceline(t, skiporg, MOVE_NORMAL, self);
225                         t = trace_endpos;
226
227                         if(trace_startsolid)
228                         {
229                                 // we're stuck inside solid :(
230                                 // force advance by 1 unit, and retry
231                                 // CAN we go by 1 unit?
232                                 if(vlen(skiporg + tracevel - org) < maxdist)
233                                         skiporg = skiporg + tracevel;
234                                 else
235                                         return 0;
236                         }
237                         else
238                         {
239                                 // we managed to leave solid
240                                 // so trace_endpos is good
241                                 self.leave_solid_origin = t;
242                                 break;
243                         }
244                 }
245                 else
246                 {
247                         // bad: skiporg is outside solid. Then imagine it's alright.
248                         self.leave_solid_origin = skiporg;
249                         break;
250                 }
251         }
252
253         dst = vlen(self.leave_solid_origin - org);
254         velfactor = (speedhalflife * v0 - dst) / (speedhalflife * v0);
255
256         // t(s) = speedhalflife * log((speedhalflife * v(0)) / (speedhalflife * v(0) - s))
257         dt = speedhalflife * log((speedhalflife * v0) / (speedhalflife * v0 - dst));
258
259         //print("slowdown by ", ftos(dst), " units = ", ftos(velfactor), "\n");
260         //print("takes time ", ftos(dt), "\n");
261
262         self.leave_solid_think_save = self.think;
263         self.leave_solid_nextthink_save = self.nextthink;
264         self.think = leave_solid_think;
265         self.nextthink = time + dt;
266
267         vel = vel * velfactor;
268
269         self.velocity = '0 0 0';
270         self.flags |= FL_ONGROUND; // prevent moving
271         self.effects |= EF_NODRAW;
272         self.leave_solid_velocity = vel;
273
274         return 1;
275 }
276
277 void W_BallisticBullet_Touch (void)
278 {
279         if(self.think == leave_solid_think) // skip this!
280                 return;
281
282         PROJECTILE_TOUCH;
283         W_BallisticBullet_Hit ();
284
285         // go through solid!
286         if(!leave_solid(self, self.velocity, self.dmg_radius))
287         {
288                 remove(self);
289                 return;
290         }
291
292         self.projectiledeathtype |= HITTYPE_BOUNCE;
293 }
294
295 void fireBallisticBullet(vector start, vector dir, float spread, float pSpeed, float lifetime, float damage, float headshotbonus, float force, float dtype, float tracereffects, float gravityfactor)
296 {
297         entity proj;
298         proj = spawn();
299         proj.owner = self;
300         proj.solid = SOLID_BBOX;
301         if(gravityfactor > 0)
302         {
303                 proj.movetype = MOVETYPE_TOSS;
304                 proj.gravity = gravityfactor;
305         }
306         else
307                 proj.movetype = MOVETYPE_FLY;
308         proj.think = SUB_Remove;
309         proj.nextthink = time + lifetime; // min(pLifetime, vlen(world.maxs - world.mins) / pSpeed);
310         proj.velocity = (dir + randomvec() * spread) * pSpeed;
311         W_SetupProjectileVelocity(proj);
312         proj.angles = vectoangles(proj.velocity);
313         proj.dmg_radius = cvar("g_ballistics_solidspeedhalflife");
314         setmodel(proj, "models/tracer.mdl");
315         setsize(proj, '0 0 0', '0 0 0');
316         setorigin(proj, w_shotorg);
317         proj.effects = EF_LOWPRECISION | tracereffects;
318         proj.flags = FL_PROJECTILE;
319
320         proj.touch = W_BallisticBullet_Touch;
321         proj.dmg = damage;
322         proj.dmg_edge = headshotbonus;
323         proj.dmg_force = force;
324         proj.projectiledeathtype = dtype;
325
326         proj.oldvelocity = proj.velocity;
327
328         if (cvar("g_casings") >= 2)
329                 SpawnCasing (w_shotorg + v_forward * 10, ((random () * 50 + 50) * v_right) - (v_forward * (random () * 25 + 25)) - ((random () * 5 - 70) * v_up), 2, vectoangles(v_forward),'0 250 0', 100, 3);
330 }
331
332 void fireBullet (vector start, vector dir, float spread, float damage, float force, float dtype, float tracer)
333 {
334         vector  end;
335         local entity e;
336
337         if(cvar("g_ballistics_force"))
338         {
339                 if (DEATH_ISWEAPON(dtype, WEP_SHOTGUN))
340                         fireBallisticBullet(start, dir, spread, cvar("g_ballistics_force_shotgun_speed"), 5, damage, 0, force, dtype, 0, 1);
341                 else
342                         fireBallisticBullet(start, dir, spread, cvar("g_ballistics_force_uzi_speed"), 5, damage, 0, force, dtype, 0, 1);
343                 return;
344         }
345
346         dir = dir + randomvec() * spread;
347         end = start + dir * MAX_SHOT_DISTANCE;
348         if(self.antilag_debug)
349                 traceline_antilag (self, start, end, FALSE, self, self.antilag_debug);
350         else
351                 traceline_antilag (self, start, end, FALSE, self, ANTILAG_LATENCY(self));
352
353         if (tracer)
354         {
355                 e = spawn();
356                 e.owner = self;
357                 e.movetype = MOVETYPE_FLY;
358                 e.solid = SOLID_NOT;
359                 e.think = SUB_Remove;
360                 e.nextthink = time + vlen(trace_endpos - start) / 6000;
361                 e.velocity = dir * 6000;
362                 e.angles = vectoangles(e.velocity);
363                 setmodel (e, "models/tracer.mdl"); // precision set below
364                 setsize (e, '0 0 0', '0 0 0');
365                 setorigin (e, start);
366                 e.effects = EF_LOWPRECISION;
367                 e.flags = FL_PROJECTILE;
368         }
369
370         if ((trace_fraction != 1.0) && (pointcontents (trace_endpos) != CONTENT_SKY))
371         {
372                 if (trace_ent.solid == SOLID_BSP && !(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))
373                 {
374                         if (DEATH_ISWEAPON(dtype, WEP_SHOTGUN))
375                                 pointparticles(particleeffectnum("shotgun_impact"), trace_endpos, trace_plane_normal * 1000, 1);
376                         else
377                                 pointparticles(particleeffectnum("machinegun_impact"), trace_endpos, trace_plane_normal * 1000, 1);
378                 }
379                 Damage (trace_ent, self, self, damage, dtype, trace_endpos, dir * force);
380         }
381 }
382
383 void W_PrepareExplosionByDamage(entity attacker, void() explode)
384 {
385         self.takedamage = DAMAGE_NO;
386         self.event_damage = SUB_Null;
387         self.owner = attacker;
388
389         // do not explode NOW but in the NEXT FRAME!
390         // because recursive calls to RadiusDamage are not allowed
391         self.nextthink = time;
392         self.think = explode;
393 }