]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/w_common.qc
86ae8866ce692617f0c00e88b49cc3c8ddb64462
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_common.qc
1
2 void W_GiveWeapon (entity e, float wep)
3 {
4         entity oldself;
5
6         if (!wep)
7                 return;
8
9         e.weapons |= WepSet_FromWeapon(wep);
10
11         oldself = self;
12         self = e;
13
14         if(IS_PLAYER(other))
15                 { Send_Notification(NOTIF_ONE, other, MSG_MULTI, ITEM_WEAPON_GOT, wep); }
16
17         self = oldself;
18 }
19
20 .float railgundistance;
21 .vector railgunforce;
22 void FireRailgunBullet (vector start, vector end, float bdamage, float bforce, float mindist, float maxdist, float halflifedist, float forcehalflifedist, float deathtype)
23 {
24         vector hitloc, force, endpoint, dir;
25         entity ent, endent;
26         float endq3surfaceflags;
27         float totaldmg;
28         entity o;
29
30         float length;
31         vector beampos;
32         string snd;
33         entity pseudoprojectile;
34         float f, ffs;
35
36         pseudoprojectile = world;
37
38         dir = normalize(end - start);
39         length = vlen(end - start);
40         force = dir * bforce;
41
42         // go a little bit into the wall because we need to hit this wall later
43         end = end + dir;
44
45         totaldmg = 0;
46
47         // trace multiple times until we hit a wall, each obstacle will be made
48         // non-solid so we can hit the next, while doing this we spawn effects and
49         // note down which entities were hit so we can damage them later
50         o = self;
51         while (1)
52         {
53                 if(self.antilag_debug)
54                         WarpZone_traceline_antilag (self, start, end, FALSE, o, self.antilag_debug);
55                 else
56                         WarpZone_traceline_antilag (self, start, end, FALSE, o, ANTILAG_LATENCY(self));
57                 if(o && WarpZone_trace_firstzone)
58                 {
59                         o = world;
60                         continue;
61                 }
62
63                 if(trace_ent.solid == SOLID_BSP || trace_ent.solid == SOLID_SLIDEBOX)
64                         Damage_DamageInfo(trace_endpos, bdamage, 0, 0, force, deathtype, trace_ent.species, self);
65
66                 // if it is world we can't hurt it so stop now
67                 if (trace_ent == world || trace_fraction == 1)
68                         break;
69
70                 // make the entity non-solid so we can hit the next one
71                 trace_ent.railgunhit = TRUE;
72                 trace_ent.railgunhitloc = end;
73                 trace_ent.railgunhitsolidbackup = trace_ent.solid;
74                 trace_ent.railgundistance = vlen(WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos) - start);
75                 trace_ent.railgunforce = WarpZone_TransformVelocity(WarpZone_trace_transform, force);
76
77                 // stop if this is a wall
78                 if (trace_ent.solid == SOLID_BSP)
79                         break;
80
81                 // make the entity non-solid
82                 trace_ent.solid = SOLID_NOT;
83         }
84
85         endpoint = trace_endpos;
86         endent = trace_ent;
87         endq3surfaceflags = trace_dphitq3surfaceflags;
88
89         // find all the entities the railgun hit and restore their solid state
90         ent = findfloat(world, railgunhit, TRUE);
91         while (ent)
92         {
93                 // restore their solid type
94                 ent.solid = ent.railgunhitsolidbackup;
95                 ent = findfloat(ent, railgunhit, TRUE);
96         }
97
98         // spawn a temporary explosion entity for RadiusDamage calls
99         //explosion = spawn();
100
101         // Find all non-hit players the beam passed close by
102         if(deathtype == WEP_MINSTANEX || deathtype == WEP_NEX)
103         {
104                 FOR_EACH_REALCLIENT(msg_entity)
105                 if(msg_entity != self)
106                 if(!msg_entity.railgunhit)
107                 if(!(IS_SPEC(msg_entity) && msg_entity.enemy == self)) // we use realclient, so spectators can hear the whoosh too
108                 {
109                         // nearest point on the beam
110                         beampos = start + dir * bound(0, (msg_entity.origin - start) * dir, length);
111
112                         f = bound(0, 1 - vlen(beampos - msg_entity.origin) / 512, 1);
113                         if(f <= 0)
114                                 continue;
115
116                         snd = strcat("weapons/nexwhoosh", ftos(floor(random() * 3) + 1), ".wav");
117
118                         if(!pseudoprojectile)
119                                 pseudoprojectile = spawn(); // we need this so the sound uses the "entchannel4" volume
120                         soundtoat(MSG_ONE, pseudoprojectile, beampos, CH_SHOTS, snd, VOL_BASE * f, ATTEN_NONE);
121                 }
122
123                 if(pseudoprojectile)
124                         remove(pseudoprojectile);
125         }
126
127         // find all the entities the railgun hit and hurt them
128         ent = findfloat(world, railgunhit, TRUE);
129         while (ent)
130         {
131                 // get the details we need to call the damage function
132                 hitloc = ent.railgunhitloc;
133
134                 f = ExponentialFalloff(mindist, maxdist, halflifedist, ent.railgundistance);
135                 ffs = ExponentialFalloff(mindist, maxdist, forcehalflifedist, ent.railgundistance);
136
137                 if(accuracy_isgooddamage(self.realowner, ent))
138                         totaldmg += bdamage * f;
139
140                 // apply the damage
141                 if (ent.takedamage)
142                         Damage (ent, self, self, bdamage * f, deathtype, hitloc, ent.railgunforce * ffs);
143
144                 // create a small explosion to throw gibs around (if applicable)
145                 //setorigin (explosion, hitloc);
146                 //RadiusDamage (explosion, self, 10, 0, 50, world, 300, deathtype);
147
148                 ent.railgunhitloc = '0 0 0';
149                 ent.railgunhitsolidbackup = SOLID_NOT;
150                 ent.railgunhit = FALSE;
151                 ent.railgundistance = 0;
152
153                 // advance to the next entity
154                 ent = findfloat(ent, railgunhit, TRUE);
155         }
156
157         // calculate hits and fired shots for hitscan
158         accuracy_add(self, self.weapon, 0, min(bdamage, totaldmg));
159
160         trace_endpos = endpoint;
161         trace_ent = endent;
162         trace_dphitq3surfaceflags = endq3surfaceflags;
163 }
164
165 float fireBullet_trace_callback_eff;
166 void fireBullet_trace_callback(vector start, vector hit, vector end)
167 {
168         if(vlen(hit - start) > 16)
169                 trailparticles(world, fireBullet_trace_callback_eff, start, hit);
170         WarpZone_trace_forent = world;
171 }
172
173 void fireBullet(vector start, vector dir, float spread, float max_solid_penetration, float damage, float force, float dtype, float tracereffects)
174 {
175         // TODO antilag takeback
176         vector  end;
177
178         dir = normalize(dir + randomvec() * spread);
179         end = start + dir * MAX_SHOT_DISTANCE;
180
181         entity pl;
182         entity last_hit = world;
183         float solid_penetration_left = 1;
184         float total_damage = 0;
185
186         if(tracereffects & EF_RED)
187                 fireBullet_trace_callback_eff = particleeffectnum("tr_rifle");
188         else if(tracereffects & EF_BLUE)
189                 fireBullet_trace_callback_eff = particleeffectnum("tr_rifle_weak");
190         else
191                 fireBullet_trace_callback_eff = particleeffectnum("tr_bullet");
192
193         float lag = ANTILAG_LATENCY(self);
194         if(lag < 0.001)
195                 lag = 0;
196         if (!IS_REAL_CLIENT(self))
197                 lag = 0;
198         if(autocvar_g_antilag == 0 || self.cvar_cl_noantilag)
199                 lag = 0; // only do hitscan, but no antilag
200         if(lag)
201                 FOR_EACH_PLAYER(pl)
202                         if(pl != self)
203                                 antilag_takeback(pl, time - lag);
204
205         WarpZone_trace_forent = self;
206
207         for (;;)
208         {
209                 // TODO also show effect while tracing
210                 WarpZone_TraceBox_ThroughZone(start, '0 0 0', '0 0 0', end, FALSE, WarpZone_trace_forent, world, fireBullet_trace_callback);
211                 dir = WarpZone_TransformVelocity(WarpZone_trace_transform, dir);
212                 end = WarpZone_TransformOrigin(WarpZone_trace_transform, end);
213                 start = trace_endpos;
214                 entity hit = trace_ent;
215
216                 // When hitting sky, stop.
217                 if (pointcontents(start) == CONTENT_SKY)
218                         break;
219
220                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
221                         break;
222
223                 // if we hit "weapclip", bail out
224                 //
225                 // rationale of this check:
226                 //
227                 // any shader that is solid, nodraw AND trans is meant to clip weapon
228                 // shots and players, but has no other effect!
229                 //
230                 // if it is not trans, it is caulk and should not have this side effect
231                 //
232                 // matching shaders:
233                 //   common/weapclip (intended)
234                 //   common/noimpact (is supposed to eat projectiles, but is erased anyway)
235                 float is_weapclip = 0;
236                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
237                 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID))
238                 if (!(trace_dphitcontents & DPCONTENTS_OPAQUE))
239                         is_weapclip = 1;
240
241                 if(!hit || hit.solid == SOLID_BSP || hit.solid == SOLID_SLIDEBOX)
242                         Damage_DamageInfo(start, damage * solid_penetration_left, 0, 0, max(1, force) * dir * solid_penetration_left, dtype, hit.species, self);
243
244                 if (hit && hit != WarpZone_trace_forent)  // Avoid self-damage (except after going through a warp).
245                 {
246                         if (hit == last_hit)
247                                 if (WarpZone_trace_forent)
248                                         dprint("NOTE: a player was hit twice in a row by the same bullet. But a warpzone was involved. Please verify that this is OK.\n"); // TODO make sure this doesn't actually happen unless when it should.
249                                 else
250                                         print("^4WARNING:^7 a player was hit twice in a row by the same bullet.\n");
251                         last_hit = hit;
252
253                         yoda = 0;
254                         float g = accuracy_isgooddamage(self, hit);
255                         Damage(hit, self, self, damage * solid_penetration_left, dtype, start, force * dir * solid_penetration_left);
256                         // calculate hits for ballistic weapons
257                         if(g)
258                         {
259                                 // do not exceed 100%
260                                 float added_damage = min(damage - total_damage, damage * solid_penetration_left);
261                                 total_damage += damage * solid_penetration_left;
262                                 accuracy_add(self, self.weapon, 0, added_damage);
263                         }
264                 }
265
266                 if (is_weapclip)
267                         break;
268
269                 // go through solid!
270                 // outside the world? forget it
271                 if(start_x > world.maxs_x || start_y > world.maxs_y || start_z > world.maxs_z || start_x < world.mins_x || start_y < world.mins_y || start_z < world.mins_z)
272                         break;
273
274                 float maxdist;
275                 if(max_solid_penetration < 0)
276                         break;
277                 else if(hit.ballistics_density < -1)
278                         break; // -2: no solid penetration, ever
279                 else if(hit.ballistics_density < 0)
280                         maxdist = vlen(hit.maxs - hit.mins) + 1; // -1: infinite travel distance
281                 else if(hit.ballistics_density == 0)
282                         maxdist = max_solid_penetration * solid_penetration_left;
283                 else
284                         maxdist = max_solid_penetration * solid_penetration_left * hit.ballistics_density;
285
286                 if(maxdist <= autocvar_g_ballistics_mindistance)
287                         break;
288
289                 // move the entity along its velocity until it's out of solid, then let it resume
290                 traceline_inverted (start, start + dir * maxdist, MOVE_NORMAL, WarpZone_trace_forent, TRUE);
291                 if(trace_fraction == 1) // 1: we never got out of solid
292                         break;
293
294                 float dist_taken = max(autocvar_g_ballistics_mindistance, vlen(trace_endpos - start));
295                 solid_penetration_left *= (dist_taken / maxdist);
296
297                 // Only show effect when going through a player (invisible otherwise)
298                 if (hit && (hit.solid != SOLID_BSP))
299                         if(vlen(trace_endpos - start) > 4)
300                                 trailparticles(self, fireBullet_trace_callback_eff, start, trace_endpos);
301
302                 start = trace_endpos;
303
304                 if(hit.solid == SOLID_BSP)
305                         Damage_DamageInfo(start, 0, 0, 0, max(1, force) * normalize(dir) * -solid_penetration_left, dtype, 0, self);
306         }
307
308         if(lag)
309                 FOR_EACH_PLAYER(pl)
310                         if(pl != self)
311                                 antilag_restore(pl);
312 }
313
314 float W_CheckProjectileDamage(entity inflictor, entity projowner, float deathtype, float exception)
315 {
316         float is_from_contents = (deathtype == DEATH_SLIME || deathtype == DEATH_LAVA);
317         float is_from_owner = (inflictor == projowner);
318         float is_from_exception = (exception != -1);
319
320         //dprint(strcat("W_CheckProjectileDamage: from_contents ", ftos(is_from_contents), " : from_owner ", ftos(is_from_owner), " : exception ", strcat(ftos(is_from_exception), " (", ftos(exception), "). \n")));
321
322         if(autocvar_g_projectiles_damage <= -2)
323         {
324                 return FALSE; // no damage to projectiles at all, not even with the exceptions
325         }
326         else if(autocvar_g_projectiles_damage == -1)
327         {
328                 if(is_from_exception)
329                         return (exception); // if exception is detected, allow it to override
330                 else
331                         return FALSE; // otherwise, no other damage is allowed
332         }
333         else if(autocvar_g_projectiles_damage == 0)
334         {
335                 if(is_from_exception)
336                         return (exception); // if exception is detected, allow it to override
337                 else if (!is_from_contents)
338                         return FALSE; // otherwise, only allow damage from contents
339         }
340         else if(autocvar_g_projectiles_damage == 1)
341         {
342                 if(is_from_exception)
343                         return (exception); // if exception is detected, allow it to override
344                 else if (!(is_from_contents || is_from_owner))
345                         return FALSE; // otherwise, only allow self damage and damage from contents
346         }
347         else if(autocvar_g_projectiles_damage == 2) // allow any damage, but override for exceptions
348         {
349                 if(is_from_exception)
350                         return (exception); // if exception is detected, allow it to override
351         }
352
353         return TRUE; // if none of these return, then allow damage anyway.
354 }
355
356 void W_PrepareExplosionByDamage(entity attacker, void() explode)
357 {
358         self.takedamage = DAMAGE_NO;
359         self.event_damage = func_null;
360
361         if(IS_CLIENT(attacker) && !autocvar_g_projectiles_keep_owner)
362         {
363                 self.owner = attacker;
364                 self.realowner = attacker;
365         }
366
367         // do not explode NOW but in the NEXT FRAME!
368         // because recursive calls to RadiusDamage are not allowed
369         self.nextthink = time;
370         self.think = explode;
371 }