9486404b9bb008820ad66be17f2b17e001a564fb
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / common.qc
1 #include "common.qh"
2
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
5 #include <common/t_items.qh>
6 #include <common/constants.qh>
7 #include <common/net_linked.qh>
8 #include <common/deathtypes/all.qh>
9 #include <common/notifications/all.qh>
10 #include <common/state.qh>
11 #include <common/util.qh>
12 #include <common/weapons/_all.qh>
13 #include <common/wepent.qh>
14 #include <common/items/_mod.qh>
15
16 bool W_DualWielding(entity player)
17 {
18         int held_weapons = 0;
19         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
20         {
21                 .entity weaponentity = weaponentities[slot];
22                 if(player.(weaponentity) && player.(weaponentity).m_switchweapon != WEP_Null)
23                         ++held_weapons;
24         }
25
26         return held_weapons > 1;
27 }
28
29 void W_GiveWeapon(entity e, int wep)
30 {
31         if (!wep) return;
32
33         STAT(WEAPONS, e) |= WepSet_FromWeapon(REGISTRY_GET(Weapons, wep));
34
35         if (IS_PLAYER(e)) {
36             Send_Notification(NOTIF_ONE, e, MSG_MULTI, ITEM_WEAPON_GOT, wep);
37     }
38 }
39
40 void W_PlayStrengthSound(entity player)
41 {
42         entity store = IS_PLAYER(player) ? PS(player) : player; // because non-player entities can fire, but can they have items? TODO
43
44         if((player.items & ITEM_Strength.m_itemid)
45                 && ((time > store.prevstrengthsound + autocvar_sv_strengthsound_antispam_time) // prevent insane sound spam
46                 || (time > store.prevstrengthsoundattempt + autocvar_sv_strengthsound_antispam_refire_threshold)))
47                 {
48                         sound(player, CH_TRIGGER, SND_STRENGTH_FIRE, VOL_BASE, ATTEN_NORM);
49                         store.prevstrengthsound = time;
50                 }
51                 store.prevstrengthsoundattempt = time;
52 }
53
54 float W_CheckProjectileDamage(entity inflictor, entity projowner, int deathtype, float exception)
55 {
56         float is_from_contents = (deathtype == DEATH_SLIME.m_id || deathtype == DEATH_LAVA.m_id);
57         float is_from_owner = (inflictor == projowner);
58         float is_from_exception = (exception != -1);
59
60         //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")));
61
62         if(autocvar_g_projectiles_damage <= -2)
63         {
64                 return false; // no damage to projectiles at all, not even with the exceptions
65         }
66         else if(autocvar_g_projectiles_damage == -1)
67         {
68                 if(is_from_exception)
69                         return (exception); // if exception is detected, allow it to override
70                 else
71                         return false; // otherwise, no other damage is allowed
72         }
73         else if(autocvar_g_projectiles_damage == 0)
74         {
75                 if(is_from_exception)
76                         return (exception); // if exception is detected, allow it to override
77                 else if(!is_from_contents)
78                         return false; // otherwise, only allow damage from contents
79         }
80         else if(autocvar_g_projectiles_damage == 1)
81         {
82                 if(is_from_exception)
83                         return (exception); // if exception is detected, allow it to override
84                 else if(!(is_from_contents || is_from_owner))
85                         return false; // otherwise, only allow self damage and damage from contents
86         }
87         else if(autocvar_g_projectiles_damage == 2) // allow any damage, but override for exceptions
88         {
89                 if(is_from_exception)
90                         return (exception); // if exception is detected, allow it to override
91         }
92
93         return true; // if none of these return, then allow damage anyway.
94 }
95
96 void W_PrepareExplosionByDamage(entity this, entity attacker, void(entity this) explode)
97 {
98         this.takedamage = DAMAGE_NO;
99         this.event_damage = func_null;
100
101         MUTATOR_CALLHOOK(PrepareExplosionByDamage, this, attacker);
102
103         if(IS_CLIENT(attacker) && !autocvar_g_projectiles_keep_owner)
104         {
105                 this.owner = attacker;
106                 this.realowner = attacker;
107         }
108
109         // do not explode NOW but in the NEXT FRAME!
110         // because recursive calls to RadiusDamage are not allowed
111         this.nextthink = time;
112         setthink(this, explode);
113 }