]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/w_common.qc
fix the other precaches
[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         float f;
120
121         f = pow(bound(0, vlen(self.velocity) / vlen(self.oldvelocity), 1), 2); // energy multiplier
122
123         if(other.solid == SOLID_BSP)
124                 Damage_DamageInfo(self.origin, self.dmg * f, 0, 0, self.dmg_force * normalize(self.velocity) * f, self.projectiledeathtype);
125
126         if(other && other != self.enemy)
127         {
128                 headshot = 0;
129                 yoda = 0;
130                 damage_headshotbonus = self.dmg_edge;
131                 railgun_start = self.origin - 2 * frametime * self.oldvelocity;
132                 railgun_end = self.origin + 2 * frametime * self.oldvelocity;
133                 Damage(other, self, self.owner, self.dmg * f, self.projectiledeathtype, self.origin, self.dmg_force * normalize(self.velocity) * f);
134                 damage_headshotbonus = 0;
135
136                 if(self.dmg_edge != 0)
137                 {
138                         if(headshot)
139                                 announce(self.owner, "announcer/male/headshot.wav");
140                         if(yoda)
141                                 announce(self.owner, "announcer/male/yoda.wav");
142                 }
143
144                 //sound (self, CHAN_PROJECTILE, "weapons/electro_impact.wav", VOL_BASE, ATTN_NORM);
145         }
146
147         self.enemy = other; // don't hit the same player twice with the same bullet
148 }
149
150 .void(void) W_BallisticBullet_LeaveSolid_think_save;
151 .float W_BallisticBullet_LeaveSolid_nextthink_save;
152 .vector W_BallisticBullet_LeaveSolid_origin;
153 .vector W_BallisticBullet_LeaveSolid_velocity;
154
155 void W_BallisticBullet_LeaveSolid_think()
156 {
157         setorigin(self, self.W_BallisticBullet_LeaveSolid_origin);
158         self.velocity = self.W_BallisticBullet_LeaveSolid_velocity;
159
160         self.think = self.W_BallisticBullet_LeaveSolid_think_save;
161         self.nextthink = max(time, self.W_BallisticBullet_LeaveSolid_nextthink_save) + 1;
162         self.W_BallisticBullet_LeaveSolid_think_save = SUB_Null;
163
164         self.flags &~= FL_ONGROUND;
165
166         if(self.enemy.solid == SOLID_BSP)
167         {
168                 float f;
169                 f = pow(bound(0, vlen(self.velocity) / vlen(self.oldvelocity), 1), 2); // energy multiplier
170                 Damage_DamageInfo(self.origin, 0, 0, 0, self.dmg_force * normalize(self.velocity) * f, self.projectiledeathtype);
171         }
172         
173         UpdateCSQCProjectile(self);
174 }
175
176 // a fake logarithm function
177 float log(float x)
178 {
179         if(x < 0.0001)
180                 return 0;
181         if(x > 0.9 && x < 1.1)
182                 return x - 1;
183         return 2 * log(sqrt(x));
184 }
185
186 float W_BallisticBullet_LeaveSolid(entity e, vector vel, float constant)
187 {
188         // move the entity along its velocity until it's out of solid, then let it resume
189         
190         float dt, dst, velfactor, v0, vs;
191         float maxdist;
192         float E0_m, Es_m;
193
194         // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
195         v0 = vlen(vel);
196
197         E0_m = 0.5 * v0 * v0;
198         maxdist = E0_m / constant;
199         // maxdist = 0.5 * v0 * v0 / constant
200         // dprint("max dist = ", ftos(maxdist), "\n");
201
202         if(maxdist <= 0.5)
203                 return 0;
204
205         traceline_inverted (self.origin, self.origin + normalize(vel) * maxdist, MOVE_NORMAL, self);
206
207         if(trace_fraction == 1) // 1: we never got out of solid
208                 return 0;
209
210         self.W_BallisticBullet_LeaveSolid_origin = trace_endpos;
211
212         dst = vlen(trace_endpos - self.origin);
213         // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
214         Es_m = E0_m - constant * dst;
215         if(Es_m <= 0)
216         {
217                 // roundoff errors got us
218                 return 0;
219         }
220         vs = sqrt(2 * Es_m);
221         velfactor = vs / v0;
222
223         dt = dst / (0.5 * (v0 + vs));
224         // this is not correct, but the differential equations have no analytic
225         // solution - and these times are very small anyway
226         //print("dt = ", ftos(dt), "\n");
227
228         self.W_BallisticBullet_LeaveSolid_think_save = self.think;
229         self.W_BallisticBullet_LeaveSolid_nextthink_save = self.nextthink;
230         self.think = W_BallisticBullet_LeaveSolid_think;
231         self.nextthink = time + dt;
232
233         vel = vel * velfactor;
234
235         self.velocity = '0 0 0';
236         self.flags |= FL_ONGROUND; // prevent moving
237         self.W_BallisticBullet_LeaveSolid_velocity = vel;
238
239         return 1;
240 }
241
242 void W_BallisticBullet_Touch (void)
243 {
244         if(self.think == W_BallisticBullet_LeaveSolid_think) // skip this!
245                 return;
246
247         PROJECTILE_TOUCH;
248         W_BallisticBullet_Hit ();
249
250         // go through solid!
251         if(!W_BallisticBullet_LeaveSolid(self, self.velocity, self.dmg_radius))
252         {
253                 remove(self);
254                 return;
255         }
256
257         self.projectiledeathtype |= HITTYPE_BOUNCE;
258 }
259
260 void fireBallisticBullet(vector start, vector dir, float spread, float pSpeed, float lifetime, float damage, float headshotbonus, float force, float dtype, float tracereffects, float gravityfactor, float bulletconstant)
261 {
262         entity proj;
263         proj = spawn();
264         proj.classname = "bullet";
265         proj.owner = self;
266         proj.solid = SOLID_BBOX;
267         if(gravityfactor > 0)
268         {
269                 proj.movetype = MOVETYPE_TOSS;
270                 proj.gravity = gravityfactor;
271         }
272         else
273                 proj.movetype = MOVETYPE_FLY;
274         proj.think = SUB_Remove;
275         proj.nextthink = time + lifetime; // min(pLifetime, vlen(world.maxs - world.mins) / pSpeed);
276         proj.velocity = (dir + randomvec() * spread) * pSpeed;
277         W_SetupProjectileVelocity(proj);
278         proj.angles = vectoangles(proj.velocity);
279         proj.dmg_radius = cvar("g_ballistics_materialconstant") / bulletconstant;
280         // so: bulletconstant = bullet mass / area of bullet circle
281         setmodel(proj, "null");
282         setsize(proj, '0 0 0', '0 0 0');
283         setorigin(proj, start);
284         proj.flags = FL_PROJECTILE;
285
286         proj.touch = W_BallisticBullet_Touch;
287         proj.dmg = damage;
288         proj.dmg_edge = headshotbonus;
289         proj.dmg_force = force;
290         proj.projectiledeathtype = dtype;
291
292         proj.oldvelocity = proj.velocity;
293
294         if(tracereffects & EF_RED)
295                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING);
296         else
297                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET);
298 }
299
300 void fireBullet (vector start, vector dir, float spread, float damage, float force, float dtype, float tracer)
301 {
302         vector  end;
303         local entity e;
304
305         if(cvar("g_ballistics_force"))
306         {
307                 if (DEATH_ISWEAPON(dtype, WEP_SHOTGUN))
308                         fireBallisticBullet(start, dir, spread, cvar("g_ballistics_force_shotgun_speed"), 5, damage, 0, force, dtype, 0, 1, cvar("g_ballistics_force_shotgun_bulletconstant"));
309                 else
310                         fireBallisticBullet(start, dir, spread, cvar("g_ballistics_force_uzi_speed"), 5, damage, 0, force, dtype, 0, 1, cvar("g_ballistics_force_shotgun_bulletconstant"));
311                 return;
312         }
313
314         dir = dir + randomvec() * spread;
315         end = start + dir * MAX_SHOT_DISTANCE;
316         if(self.antilag_debug)
317                 traceline_antilag (self, start, end, FALSE, self, self.antilag_debug);
318         else
319                 traceline_antilag (self, start, end, FALSE, self, ANTILAG_LATENCY(self));
320
321         if (tracer)
322         {
323                 e = spawn();
324                 e.owner = self;
325                 e.movetype = MOVETYPE_FLY;
326                 e.solid = SOLID_NOT;
327                 e.think = SUB_Remove;
328                 e.nextthink = time + vlen(trace_endpos - start) / 6000;
329                 e.velocity = dir * 6000;
330                 e.angles = vectoangles(e.velocity);
331                 setmodel (e, "null"); // precision set below
332                 setsize (e, '0 0 0', '0 0 0');
333                 setorigin (e, start);
334                 e.flags = FL_PROJECTILE;
335
336                 CSQCProjectile(e, TRUE, PROJECTILE_BULLET);
337         }
338
339         if ((trace_fraction != 1.0) && (pointcontents (trace_endpos) != CONTENT_SKY))
340         {
341                 if (trace_ent.solid == SOLID_BSP && !(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))
342                         Damage_DamageInfo(trace_endpos, damage, 0, 0, dir * force, dtype);
343                 Damage (trace_ent, self, self, damage, dtype, trace_endpos, dir * force);
344         }
345 }
346
347 void W_PrepareExplosionByDamage(entity attacker, void() explode)
348 {
349         self.takedamage = DAMAGE_NO;
350         self.event_damage = SUB_Null;
351         self.owner = attacker;
352
353         // do not explode NOW but in the NEXT FRAME!
354         // because recursive calls to RadiusDamage are not allowed
355         self.nextthink = time;
356         self.think = explode;
357 }