]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/w_common.qc
Fix use of "self".
[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                 // Avoid self-damage (except after going through a warp)
242                 // FIXME can this actually happen in any other case? Probably
243                 // only with weird shotorigin outside the player bbox.
244                 if (hit != WarpZone_trace_forent)
245                 {
246                         if(!hit || hit.solid == SOLID_BSP || hit.solid == SOLID_SLIDEBOX)
247                                 Damage_DamageInfo(start, damage * solid_penetration_left, 0, 0, max(1, force) * dir * solid_penetration_left, dtype, hit.species, self);
248
249                         if(hit && hit != last_hit)
250                         {
251                                 yoda = 0;
252                                 float g = accuracy_isgooddamage(self, hit);
253                                 Damage(hit, self, self, damage * solid_penetration_left, dtype, start, force * dir * solid_penetration_left);
254                                 // calculate hits for ballistic weapons
255                                 if(g)
256                                 {
257                                         // do not exceed 100%
258                                         float added_damage = min(damage - total_damage, damage * solid_penetration_left);
259                                         total_damage += damage * solid_penetration_left;
260                                         accuracy_add(self, self.weapon, 0, added_damage);
261                                 }
262                         }
263
264                         last_hit = hit; // don't hit the same player twice with the same bullet
265                 }
266
267                 if (is_weapclip)
268                         break;
269
270                 // go through solid!
271                 // outside the world? forget it
272                 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)
273                         break;
274
275                 float maxdist;
276                 if(max_solid_penetration < 0)
277                         break;
278                 else if(hit.ballistics_density < -1)
279                         break; // -2: no solid penetration, ever
280                 else if(hit.ballistics_density < 0)
281                         maxdist = vlen(hit.maxs - hit.mins) + 1; // -1: infinite travel distance
282                 else if(hit.ballistics_density == 0)
283                         maxdist = max_solid_penetration * solid_penetration_left;
284                 else
285                         maxdist = max_solid_penetration * solid_penetration_left * hit.ballistics_density;
286
287                 if(maxdist <= autocvar_g_ballistics_mindistance)
288                         break;
289
290                 // move the entity along its velocity until it's out of solid, then let it resume
291                 traceline_inverted (start, start + dir * maxdist, MOVE_NORMAL, WarpZone_trace_forent, TRUE);
292                 if(trace_fraction == 1) // 1: we never got out of solid
293                         break;
294
295                 float dist_taken = max(autocvar_g_ballistics_mindistance, vlen(trace_endpos - start));
296                 solid_penetration_left *= (dist_taken / maxdist);
297
298                 // Only show effect when going through a player (invisible otherwise)
299                 if (hit && (hit.solid != SOLID_BSP))
300                         if(vlen(trace_endpos - start) > 4)
301                                 trailparticles(self, fireBullet_trace_callback_eff, start, trace_endpos);
302
303                 start = trace_endpos;
304
305                 if(hit.solid == SOLID_BSP)
306                         Damage_DamageInfo(start, 0, 0, 0, max(1, force) * normalize(dir) * -solid_penetration_left, dtype, 0, self);
307         }
308
309         if(lag)
310                 FOR_EACH_PLAYER(pl)
311                         if(pl != self)
312                                 antilag_restore(pl);
313 }
314
315 float W_CheckProjectileDamage(entity inflictor, entity projowner, float deathtype, float exception)
316 {
317         float is_from_contents = (deathtype == DEATH_SLIME || deathtype == DEATH_LAVA);
318         float is_from_owner = (inflictor == projowner);
319         float is_from_exception = (exception != -1);
320
321         //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")));
322
323         if(autocvar_g_projectiles_damage <= -2)
324         {
325                 return FALSE; // no damage to projectiles at all, not even with the exceptions
326         }
327         else if(autocvar_g_projectiles_damage == -1)
328         {
329                 if(is_from_exception)
330                         return (exception); // if exception is detected, allow it to override
331                 else
332                         return FALSE; // otherwise, no other damage is allowed
333         }
334         else if(autocvar_g_projectiles_damage == 0)
335         {
336                 if(is_from_exception)
337                         return (exception); // if exception is detected, allow it to override
338                 else if (!is_from_contents)
339                         return FALSE; // otherwise, only allow damage from contents
340         }
341         else if(autocvar_g_projectiles_damage == 1)
342         {
343                 if(is_from_exception)
344                         return (exception); // if exception is detected, allow it to override
345                 else if (!(is_from_contents || is_from_owner))
346                         return FALSE; // otherwise, only allow self damage and damage from contents
347         }
348         else if(autocvar_g_projectiles_damage == 2) // allow any damage, but override for exceptions
349         {
350                 if(is_from_exception)
351                         return (exception); // if exception is detected, allow it to override
352         }
353
354         return TRUE; // if none of these return, then allow damage anyway.
355 }
356
357 void W_PrepareExplosionByDamage(entity attacker, void() explode)
358 {
359         self.takedamage = DAMAGE_NO;
360         self.event_damage = func_null;
361
362         if(IS_CLIENT(attacker) && !autocvar_g_projectiles_keep_owner)
363         {
364                 self.owner = attacker;
365                 self.realowner = attacker;
366         }
367
368         // do not explode NOW but in the NEXT FRAME!
369         // because recursive calls to RadiusDamage are not allowed
370         self.nextthink = time;
371         self.think = explode;
372 }