]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/common.qc
Item Pickup panel
[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
40 void W_PlayStrengthSound(entity player)
41 {
42         MUTATOR_CALLHOOK(W_PlayStrengthSound, player);
43 }
44
45 float W_CheckProjectileDamage(entity inflictor, entity projowner, int deathtype, float exception)
46 {
47         float is_from_contents = (deathtype == DEATH_SLIME.m_id || deathtype == DEATH_LAVA.m_id);
48         float is_from_owner = (inflictor == projowner);
49         float is_from_exception = (exception != -1);
50
51         //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")));
52
53         if(autocvar_g_projectiles_damage <= -2)
54         {
55                 return false; // no damage to projectiles at all, not even with the exceptions
56         }
57         else if(autocvar_g_projectiles_damage == -1)
58         {
59                 if(is_from_exception)
60                         return (exception); // if exception is detected, allow it to override
61                 else
62                         return false; // otherwise, no other damage is allowed
63         }
64         else if(autocvar_g_projectiles_damage == 0)
65         {
66                 if(is_from_exception)
67                         return (exception); // if exception is detected, allow it to override
68                 else if(!is_from_contents)
69                         return false; // otherwise, only allow damage from contents
70         }
71         else if(autocvar_g_projectiles_damage == 1)
72         {
73                 if(is_from_exception)
74                         return (exception); // if exception is detected, allow it to override
75                 else if(!(is_from_contents || is_from_owner))
76                         return false; // otherwise, only allow self damage and damage from contents
77         }
78         else if(autocvar_g_projectiles_damage == 2) // allow any damage, but override for exceptions
79         {
80                 if(is_from_exception)
81                         return (exception); // if exception is detected, allow it to override
82         }
83
84         return true; // if none of these return, then allow damage anyway.
85 }
86
87 void W_PrepareExplosionByDamage(entity this, entity attacker, void(entity this) explode)
88 {
89         this.takedamage = DAMAGE_NO;
90         this.event_damage = func_null;
91
92         MUTATOR_CALLHOOK(PrepareExplosionByDamage, this, attacker);
93
94         if(IS_CLIENT(attacker) && !autocvar_g_projectiles_keep_owner)
95         {
96                 this.owner = attacker;
97                 this.realowner = attacker;
98         }
99
100         // do not explode NOW but in the NEXT FRAME!
101         // because recursive calls to RadiusDamage are not allowed
102         this.nextthink = time;
103         setthink(this, explode);
104 }
105
106 void adaptor_think2use_hittype_splash(entity this) // for timed projectile detonation
107 {
108         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
109                 this.projectiledeathtype |= HITTYPE_SPLASH;
110         adaptor_think2use(this);
111 }
112
113 bool SUB_NoImpactCheck(entity this, entity toucher)
114 {
115         // zero hitcontents = this is not the real impact, but either the
116         // mirror-impact of something hitting the projectile instead of the
117         // projectile hitting the something, or a touchareagrid one. Neither of
118         // these stop the projectile from moving, so...
119         // NOTE: this notice is disabled to prevent spam as projectiles can hit content-less objects (other projectiles!)
120 #if 0
121         if(trace_dphitcontents == 0)
122         {
123                 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);
124                 checkclient(this); // TODO: .health is checked in the engine with this, possibly replace with a QC function?
125         }
126 #endif
127     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
128         return true;
129     if (toucher == NULL && this.size != '0 0 0')
130     {
131         vector tic;
132         tic = this.velocity * sys_frametime;
133         tic = tic + normalize(tic) * vlen(this.maxs - this.mins);
134         traceline(this.origin - tic, this.origin + tic, MOVE_NORMAL, this);
135         if (trace_fraction >= 1)
136         {
137             // NOTE: this notice can occur when projectiles hit non-world objects, better to not spam the console!
138             //LOG_TRACE("Odd... did not hit...?");
139         }
140         else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
141         {
142             LOG_TRACE("Detected and prevented the sky-grapple bug.");
143             return true;
144         }
145     }
146
147     return false;
148 }
149
150 bool WarpZone_Projectile_Touch_ImpactFilter_Callback(entity this, entity toucher)
151 {
152         // owner check
153         if(toucher && toucher == this.owner)
154                 return true;
155         if(SUB_NoImpactCheck(this, toucher))
156         {
157                 if(this.classname == "nade")
158                         return false; // no checks here
159                 else if(this.classname == "grapplinghook")
160                         RemoveHook(this);
161                 else
162                         delete(this);
163                 return true;
164         }
165         if(trace_ent && trace_ent.solid > SOLID_TRIGGER)
166                 UpdateCSQCProjectile(this);
167         return false;
168 }