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