]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/common.qc
Merge branch 'master' into terencehill/lms_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / common.qc
1 #include "common.qh"
2
3 #include <common/constants.qh>
4 #include <common/deathtypes/all.qh>
5 #include <common/items/_mod.qh>
6 #include <common/net_linked.qh>
7 #include <common/notifications/all.qh>
8 #include <common/state.qh>
9 #include <common/stats.qh>
10 #include <common/util.qh>
11 #include <common/weapons/_all.qh>
12 #include <common/wepent.qh>
13 #include <server/command/common.qh>
14 #include <server/damage.qh>
15 #include <server/hook.qh>
16 #include <server/items/items.qh>
17 #include <server/mutators/_mod.qh>
18 #include <server/weapons/csqcprojectile.qh>
19
20 bool W_DualWielding(entity player)
21 {
22         int held_weapons = 0;
23         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
24         {
25                 .entity weaponentity = weaponentities[slot];
26                 if(player.(weaponentity) && player.(weaponentity).m_switchweapon != WEP_Null)
27                         ++held_weapons;
28         }
29
30         return held_weapons > 1;
31 }
32
33 void W_GiveWeapon(entity e, int wep)
34 {
35         if (!wep) return;
36
37         STAT(WEAPONS, e) |= WepSet_FromWeapon(REGISTRY_GET(Weapons, wep));
38
39         if (IS_PLAYER(e)) {
40             Send_Notification(NOTIF_ONE, e, MSG_MULTI, ITEM_WEAPON_GOT, wep);
41     }
42 }
43
44 void W_PlayStrengthSound(entity player)
45 {
46         entity store = IS_PLAYER(player) ? PS(player) : player; // because non-player entities can fire, but can they have items? TODO
47
48         if((player.items & ITEM_Strength.m_itemid)
49                 && ((time > store.prevstrengthsound + autocvar_sv_strengthsound_antispam_time) // prevent insane sound spam
50                 || (time > store.prevstrengthsoundattempt + autocvar_sv_strengthsound_antispam_refire_threshold)))
51                 {
52                         sound(player, CH_TRIGGER, SND_STRENGTH_FIRE, VOL_BASE, ATTEN_NORM);
53                         store.prevstrengthsound = time;
54                 }
55                 store.prevstrengthsoundattempt = time;
56 }
57
58 float W_CheckProjectileDamage(entity inflictor, entity projowner, int deathtype, float exception)
59 {
60         float is_from_contents = (deathtype == DEATH_SLIME.m_id || deathtype == DEATH_LAVA.m_id);
61         float is_from_owner = (inflictor == projowner);
62         float is_from_exception = (exception != -1);
63
64         //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")));
65
66         if(autocvar_g_projectiles_damage <= -2)
67         {
68                 return false; // no damage to projectiles at all, not even with the exceptions
69         }
70         else if(autocvar_g_projectiles_damage == -1)
71         {
72                 if(is_from_exception)
73                         return (exception); // if exception is detected, allow it to override
74                 else
75                         return false; // otherwise, no other damage is allowed
76         }
77         else if(autocvar_g_projectiles_damage == 0)
78         {
79                 if(is_from_exception)
80                         return (exception); // if exception is detected, allow it to override
81                 else if(!is_from_contents)
82                         return false; // otherwise, only allow damage from contents
83         }
84         else if(autocvar_g_projectiles_damage == 1)
85         {
86                 if(is_from_exception)
87                         return (exception); // if exception is detected, allow it to override
88                 else if(!(is_from_contents || is_from_owner))
89                         return false; // otherwise, only allow self damage and damage from contents
90         }
91         else if(autocvar_g_projectiles_damage == 2) // allow any damage, but override for exceptions
92         {
93                 if(is_from_exception)
94                         return (exception); // if exception is detected, allow it to override
95         }
96
97         return true; // if none of these return, then allow damage anyway.
98 }
99
100 void W_PrepareExplosionByDamage(entity this, entity attacker, void(entity this) explode)
101 {
102         this.takedamage = DAMAGE_NO;
103         this.event_damage = func_null;
104
105         MUTATOR_CALLHOOK(PrepareExplosionByDamage, this, attacker);
106
107         if(IS_CLIENT(attacker) && !autocvar_g_projectiles_keep_owner)
108         {
109                 this.owner = attacker;
110                 this.realowner = attacker;
111         }
112
113         // do not explode NOW but in the NEXT FRAME!
114         // because recursive calls to RadiusDamage are not allowed
115         this.nextthink = time;
116         setthink(this, explode);
117 }
118
119 void adaptor_think2use_hittype_splash(entity this) // for timed projectile detonation
120 {
121         if(!(IS_ONGROUND(this))) // if onground, we ARE touching something, but HITTYPE_SPLASH is to be networked if the damage causing projectile is not touching ANYTHING
122                 this.projectiledeathtype |= HITTYPE_SPLASH;
123         adaptor_think2use(this);
124 }
125
126 bool SUB_NoImpactCheck(entity this, entity toucher)
127 {
128         // zero hitcontents = this is not the real impact, but either the
129         // mirror-impact of something hitting the projectile instead of the
130         // projectile hitting the something, or a touchareagrid one. Neither of
131         // these stop the projectile from moving, so...
132         if(trace_dphitcontents == 0)
133         {
134                 LOG_TRACEF("A hit from a projectile happened with no hit contents! DEBUG THIS, this should never happen for projectiles! Projectile will self-destruct. (edict: %i, classname: %s, origin: %v)", this, this.classname, this.origin);
135                 checkclient(this); // TODO: .health is checked in the engine with this, possibly replace with a QC function?
136         }
137     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
138         return true;
139     if (toucher == NULL && this.size != '0 0 0')
140     {
141         vector tic;
142         tic = this.velocity * sys_frametime;
143         tic = tic + normalize(tic) * vlen(this.maxs - this.mins);
144         traceline(this.origin - tic, this.origin + tic, MOVE_NORMAL, this);
145         if (trace_fraction >= 1)
146         {
147             LOG_TRACE("Odd... did not hit...?");
148         }
149         else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
150         {
151             LOG_TRACE("Detected and prevented the sky-grapple bug.");
152             return true;
153         }
154     }
155
156     return false;
157 }
158
159 bool WarpZone_Projectile_Touch_ImpactFilter_Callback(entity this, entity toucher)
160 {
161         // owner check
162         if(toucher && toucher == this.owner)
163                 return true;
164         if(SUB_NoImpactCheck(this, toucher))
165         {
166                 if(this.classname == "nade")
167                         return false; // no checks here
168                 else if(this.classname == "grapplinghook")
169                         RemoveHook(this);
170                 else
171                         delete(this);
172                 return true;
173         }
174         if(trace_ent && trace_ent.solid > SOLID_TRIGGER)
175                 UpdateCSQCProjectile(this);
176         return false;
177 }