]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/weapons/selection.qc
Typecheck weaponframe
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / selection.qc
index b7eb2fe097e0e169d3cf6a1f9177c0292701dc6c..1337a1d71b89a097082565df800e6ea2a2e203ba 100644 (file)
@@ -1,42 +1,65 @@
 #include "selection.qh"
 
 #include "weaponsystem.qh"
-#include "../t_items.qh"
-#include "../../common/constants.qh"
-#include "../../common/util.qh"
-#include "../../common/items/item.qh"
-#include "../../common/weapons/all.qh"
-#include "../../common/mutators/mutator/waypoints/waypointsprites.qh"
+#include <common/t_items.qh>
+#include <common/constants.qh>
+#include <common/util.qh>
+#include <common/items/item.qh>
+#include <common/weapons/all.qh>
+#include <common/state.qh>
+#include <common/mutators/mutator/waypoints/waypointsprites.qh>
 
 // switch between weapons
 void Send_WeaponComplain(entity e, float wpn, float type)
 {
        msg_entity = e;
-       WriteByte(MSG_ONE, SVC_TEMPENTITY);
-       WriteByte(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
+       WriteHeader(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
        WriteByte(MSG_ONE, wpn);
        WriteByte(MSG_ONE, type);
 }
 
-float client_hasweapon(entity cl, float wpn, float andammo, float complain)
-{SELFPARAM();
-       float f;
+void Weapon_whereis(Weapon this, entity cl)
+{
+       if (!autocvar_g_showweaponspawns) return;
+       for (entity it = NULL; (it = findfloat(it, weapon, this.m_id)); )
+       {
+               if (it.classname == "droppedweapon" && autocvar_g_showweaponspawns < 2)
+                       continue;
+               if (!(it.flags & FL_ITEM))
+                       continue;
+               entity wp = WaypointSprite_Spawn(
+                       WP_Weapon,
+                       1, 0,
+                       NULL, it.origin + ('0 0 1' * it.maxs.z) * 1.2,
+                       cl, 0,
+                       NULL, enemy,
+                       0,
+                       RADARICON_NONE
+               );
+               wp.wp_extra = this.m_id;
+       }
+}
 
-       if(time < self.hasweapon_complain_spam)
+bool client_hasweapon(entity cl, Weapon wpn, float andammo, bool complain)
+{
+    SELFPARAM();
+       float f = 0;
+
+       if (time < cl.hasweapon_complain_spam)
                complain = 0;
 
        // ignore hook button when using other offhand equipment
        if (cl.offhand != OFFHAND_HOOK)
-       if (wpn == WEP_HOOK.m_id && !((cl.weapons | weaponsInMap) & WepSet_FromWeapon(wpn)))
+       if (wpn == WEP_HOOK && !((cl.weapons | weaponsInMap) & WepSet_FromWeapon(wpn)))
            complain = 0;
 
-       if(complain)
-               self.hasweapon_complain_spam = time + 0.2;
+       if (complain)
+               cl.hasweapon_complain_spam = time + 0.2;
 
-       if (wpn < WEP_FIRST || wpn > WEP_LAST)
+       if (wpn == WEP_Null)
        {
                if (complain)
-                       sprint(self, "Invalid weapon\n");
+                       sprint(cl, "Invalid weapon\n");
                return false;
        }
        if (cl.weapons & WepSet_FromWeapon(wpn))
@@ -49,17 +72,15 @@ float client_hasweapon(entity cl, float wpn, float andammo, float complain)
                        }
                        else
                        {
-                               setself(cl);
-                               Weapon w = get_weaponinfo(wpn);
-                               f = w.wr_checkammo1(w) + w.wr_checkammo2(w);
+                               WITH(entity, self, cl, f = wpn.wr_checkammo1(wpn) + wpn.wr_checkammo2(wpn));
 
                                // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
-                               entity mine;
-                               if(wpn == WEP_MINE_LAYER.m_id)
-                               for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
-                                       f = 1;
-
-                               setself(this);
+                               if(wpn == WEP_MINE_LAYER)
+                                       FOREACH_ENTITY_CLASS("mine", it.owner == cl,
+                                       {
+                                               f = 1;
+                                               break; // no need to continue
+                                       });
                        }
                        if (!f)
                        {
@@ -67,7 +88,7 @@ float client_hasweapon(entity cl, float wpn, float andammo, float complain)
                                if(IS_REAL_CLIENT(cl))
                                {
                                        play2(cl, SND(UNAVAILABLE));
-                                       Send_WeaponComplain (cl, wpn, 0);
+                                       Send_WeaponComplain (cl, wpn.m_id, 0);
                                }
                                return false;
                        }
@@ -80,34 +101,12 @@ float client_hasweapon(entity cl, float wpn, float andammo, float complain)
                // Report Proper Weapon Status / Modified Weapon Ownership Message
                if (weaponsInMap & WepSet_FromWeapon(wpn))
                {
-                       Send_WeaponComplain(cl, wpn, 1);
-
-                       if(autocvar_g_showweaponspawns)
-                       {
-                               entity e;
-
-                               for(e = world; (e = findfloat(e, weapon, wpn)); )
-                               {
-                                       if(e.classname == "droppedweapon" && autocvar_g_showweaponspawns < 2)
-                                               continue;
-                                       if(!(e.flags & FL_ITEM))
-                                               continue;
-                                       entity wp = WaypointSprite_Spawn(
-                                               WP_Weapon,
-                                               1, 0,
-                                               world, e.origin + ('0 0 1' * e.maxs.z) * 1.2,
-                                               self, 0,
-                                               world, enemy,
-                                               0,
-                                               RADARICON_NONE
-                                       );
-                                       wp.wp_extra = wpn;
-                               }
-                       }
+                       Send_WeaponComplain(cl, wpn.m_id, 1);
+                       Weapon_whereis(wpn, cl);
                }
                else
                {
-                       Send_WeaponComplain (cl, wpn, 2);
+                       Send_WeaponComplain (cl, wpn.m_id, 2);
                }
 
                play2(cl, SND(UNAVAILABLE));
@@ -127,7 +126,7 @@ float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, floa
        entity wep;
 
        if(skipmissing || pl.selectweapon == 0)
-               weaponcur = pl.switchweapon;
+               weaponcur = PS(pl).m_switchweapon.m_id;
        else
                weaponcur = pl.selectweapon;
 
@@ -140,20 +139,19 @@ float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, floa
        while(rest != "")
        {
                weaponwant = stof(car(rest)); rest = cdr(rest);
-               wep = get_weaponinfo(weaponwant);
-               wepset = WepSet_FromWeapon(weaponwant);
+               wep = Weapons_from(weaponwant);
+               wepset = wep.m_wepset;
                if(imp >= 0)
                if(wep.impulse != imp)
                        continue;
 
-               float i, have_other = false;
-               for(i = WEP_FIRST; i <= WEP_LAST; ++i)
-               {
+               bool have_other = false;
+               FOREACH(Weapons, it != WEP_Null, {
                        if(i != weaponwant)
-                       if((get_weaponinfo(i)).impulse == imp || imp < 0)
-                       if((pl.weapons & WepSet_FromWeapon(i)) || (weaponsInMap & WepSet_FromWeapon(i)))
+                       if(it.impulse == imp || imp < 0)
+                       if((pl.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
                                have_other = true;
-               }
+               });
 
                // skip weapons we don't own that aren't normal and aren't in the map
                if(!(pl.weapons & wepset))
@@ -163,7 +161,7 @@ float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, floa
 
                ++c;
 
-               if(!skipmissing || client_hasweapon(pl, weaponwant, true, false))
+               if(!skipmissing || client_hasweapon(pl, wep, true, false))
                {
                        if(switchtonext)
                                return weaponwant;
@@ -197,20 +195,19 @@ float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, floa
                while(rest != "")
                {
                        weaponwant = stof(car(rest)); rest = cdr(rest);
-                       wep = get_weaponinfo(weaponwant);
-                       wepset = WepSet_FromWeapon(weaponwant);
+                       wep = Weapons_from(weaponwant);
+                       wepset = wep.m_wepset;
                        if(imp >= 0)
                                if(wep.impulse != imp)
                                        continue;
 
-                       float i, have_other = false;
-                       for(i = WEP_FIRST; i <= WEP_LAST; ++i)
-                       {
+                       bool have_other = false;
+                       FOREACH(Weapons, it != WEP_Null, {
                                if(i != weaponwant)
-                               if((get_weaponinfo(i)).impulse == imp || imp < 0)
-                               if((pl.weapons & WepSet_FromWeapon(i)) || (weaponsInMap & WepSet_FromWeapon(i)))
+                               if(it.impulse == imp || imp < 0)
+                               if((pl.weapons & (it.m_wepset)) || (weaponsInMap & (it.m_wepset)))
                                        have_other = true;
-                       }
+                       });
 
                        // skip weapons we don't own that aren't normal and aren't in the map
                        if(!(pl.weapons & wepset))
@@ -221,7 +218,7 @@ float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, floa
                        --c;
                        if(c == 0)
                        {
-                               client_hasweapon(pl, weaponwant, true, true);
+                               client_hasweapon(pl, wep, true, true);
                                break;
                        }
                }
@@ -229,43 +226,47 @@ float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, floa
        return 0;
 }
 
-void W_SwitchWeapon_Force(entity e, float w)
+void W_SwitchWeapon_Force(Player this, Weapon wep)
 {
-       e.cnt = e.switchweapon;
-       e.switchweapon = w;
-       e.selectweapon = w;
+    TC(Player, this); TC(Weapon, wep);
+       this.cnt = PS(this).m_switchweapon.m_id;
+       PS(this).m_switchweapon = wep;
+       this.selectweapon = wep.m_id;
 }
 
 // perform weapon to attack (weaponstate and attack_finished check is here)
 void W_SwitchToOtherWeapon(entity pl)
 {
        // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
-       float w, ww;
-       w = pl.weapon;
-       if(pl.weapons & WepSet_FromWeapon(w))
+       Weapon ww;
+       WepSet set = WepSet_FromWeapon(PS(pl).m_weapon);
+       if (pl.weapons & set)
        {
-               pl.weapons &= ~WepSet_FromWeapon(w);
+               pl.weapons &= ~set;
                ww = w_getbestweapon(pl);
-               pl.weapons |= WepSet_FromWeapon(w);
+               pl.weapons |= set;
        }
        else
+       {
                ww = w_getbestweapon(pl);
-       if(ww)
-               W_SwitchWeapon_Force(pl, ww);
+       }
+       if (ww == WEP_Null) return;
+       W_SwitchWeapon_Force(pl, ww);
 }
 
-void W_SwitchWeapon(float imp)
+void W_SwitchWeapon(Weapon w)
 {SELFPARAM();
-       if (self.switchweapon != imp)
+       if (PS(self).m_switchweapon != w)
        {
-               if (client_hasweapon(self, imp, true, true))
-                       W_SwitchWeapon_Force(self, imp);
+               if (client_hasweapon(self, w, true, true))
+                       W_SwitchWeapon_Force(self, w);
                else
-                       self.selectweapon = imp; // update selectweapon ANYWAY
+                       self.selectweapon = w.m_id; // update selectweapon ANYWAY
        }
        else if(!forbidWeaponUse(self)) {
-               Weapon w = get_weaponinfo(self.weapon);
-               w.wr_reload(w);
+               entity actor = this;
+               .entity weaponentity = weaponentities[0]; // TODO: unhardcode
+               w.wr_reload(w, actor, weaponentity);
        }
 }
 
@@ -274,7 +275,7 @@ void W_CycleWeapon(string weaponorder, float dir)
        float w;
        w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, true);
        if(w > 0)
-               W_SwitchWeapon(w);
+               W_SwitchWeapon(Weapons_from(w));
 }
 
 void W_NextWeaponOnImpulse(float imp)
@@ -282,7 +283,7 @@ void W_NextWeaponOnImpulse(float imp)
        float w;
        w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
        if(w > 0)
-               W_SwitchWeapon(w);
+               W_SwitchWeapon(Weapons_from(w));
 }
 
 // next weapon
@@ -308,10 +309,11 @@ void W_PreviousWeapon(float list)
 }
 
 // previously used if exists and has ammo, (second) best otherwise
-void W_LastWeapon(void)
-{SELFPARAM();
-       if(client_hasweapon(self, self.cnt, true, false))
-               W_SwitchWeapon(self.cnt);
+void W_LastWeapon(entity this)
+{
+       Weapon wep = Weapons_from(this.cnt);
+       if (client_hasweapon(this, wep, true, false))
+               W_SwitchWeapon(wep);
        else
-               W_SwitchToOtherWeapon(self);
+               W_SwitchToOtherWeapon(this);
 }