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