]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
rewrite small functions into macros
authordrjaska <drjaska83@gmail.com>
Wed, 28 Dec 2022 18:20:22 +0000 (20:20 +0200)
committerdrjaska <drjaska83@gmail.com>
Wed, 28 Dec 2022 18:20:22 +0000 (20:20 +0200)
12 files changed:
qcsrc/client/command/cl_cmd.qc
qcsrc/client/hud/panel/ammo.qc
qcsrc/client/hud/panel/weapons.qc
qcsrc/common/notifications/all.qh
qcsrc/common/weapons/all.qc
qcsrc/common/weapons/all.qh
qcsrc/common/weapons/weapon.qh
qcsrc/menu/xonotic/weaponslist.qc
qcsrc/server/bot/default/bot.qc
qcsrc/server/chat.qc
qcsrc/server/items/items.qc
qcsrc/server/weapons/weaponsystem.qc

index 0f8a69e727d6b43c48bb9eaf17c6ed1a78029e20..e991e66b58e72ddc13ccf5f606b299897f6cfa4a 100644 (file)
@@ -403,9 +403,9 @@ void LocalCommand_sendcvar(int request, int argc)
                                string s = cvar_string(thiscvar);
 
                                if (thiscvar == "cl_weaponpriority")
-                                       s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 1);
+                                       s = W_FIXWEAPONORDER(W_NUMBERWEAPONORDER(s), 1);
                                else if (substring(thiscvar, 0, 17) == "cl_weaponpriority" && strlen(thiscvar) == 18)
-                                       s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 0);
+                                       s = W_FIXWEAPONORDER(W_NUMBERWEAPONORDER(s), 0);
 
                                localcmd("cmd sentcvar ", thiscvar, " \"", s, "\"\n");
                                strfree(thiscvar);
index 0cc1f23956aa417f554c186183ff2f0438d6f53d..cd64cafb14d85e5366266d27031e30e6e40b78a0 100644 (file)
@@ -47,7 +47,7 @@ void DrawAmmoItem(vector myPos, vector mySize, Resource ammoType, bool isCurrent
                ammo = 60;
        }
        else
-               ammo = getstati(GetAmmoStat(ammoType));
+               ammo = getstati(GETAMMOSTAT(ammoType));
 
        if(!isCurrent)
        {
@@ -102,7 +102,7 @@ void DrawAmmoItem(vector myPos, vector mySize, Resource ammoType, bool isCurrent
        if(autocvar_hud_panel_ammo_text)
                drawstring_aspect(textPos, text, eX * (2/3) * mySize.x + eY * mySize.y, textColor, alpha, DRAWFLAG_NORMAL);
 
-       drawpic_aspect_skin(iconPos, GetAmmoPicture(ammoType), '1 1 0' * mySize.y, iconColor, alpha, DRAWFLAG_NORMAL);
+       drawpic_aspect_skin(iconPos, GETAMMOPICTURE(ammoType), '1 1 0' * mySize.y, iconColor, alpha, DRAWFLAG_NORMAL);
 }
 
 int nade_prevstatus;
@@ -222,7 +222,7 @@ void HUD_Ammo()
                //FOREACH(Resources, it.instanceOfAmmoResource && !it.m_hidden,
                for(int j = 0; j < AMMO_COUNT; ++j)
                {
-                       Resource ammotype = GetAmmoTypeFromNum(j);
+                       Resource ammotype = GETAMMOTYPEFROMNUM(j);
                        DrawAmmoItem(
                                pos + vec2(column * (ammo_size.x + offset.x), row * (ammo_size.y + offset.y)),
                                ammo_size,
index b51c5761d77819902787d194525dd2301093b751..720a739910054d03e621202853283e96f76f61b4 100644 (file)
@@ -171,7 +171,7 @@ void HUD_Weapons()
        {
                weapons_orderbyimpulse_old = autocvar_hud_panel_weapons_orderbyimpulse;
                strcpy(cl_weaponpriority_old, autocvar_cl_weaponpriority);
-               string weporder = W_FixWeaponOrder_ForceComplete(W_NumberWeaponOrder(cl_weaponpriority_old));
+               string weporder = W_FIXWEAPNORDER_FORCECOMPLETE(W_NUMBERWEAPONORDER(cl_weaponpriority_old));
                if(autocvar_hud_panel_weapons_orderbyimpulse)
                {
                        weporder = W_FixWeaponOrder_BuildImpulseList(weporder);
@@ -577,7 +577,7 @@ void HUD_Weapons()
                        if(!infinite_ammo && autocvar_hud_panel_weapons_ammo && (it.ammo_type != RES_NONE))
                        {
                                float ammo_full;
-                               a = getstati(GetAmmoStat(it.ammo_type)); // how much ammo do we have?
+                               a = getstati(GETAMMOSTAT(it.ammo_type)); // how much ammo do we have?
 
                                if(a > 0)
                                {
index 74e313dce000995b724132e09eecf722a4fa4809..e9eb9895ca6c3300045173db92b25accb11b534d 100644 (file)
@@ -632,17 +632,8 @@ string notif_arg_item_wepammo(float f1, float f2)
 {
        string ammoitems = "";
        Weapon wep = REGISTRY_GET(Weapons, f1);
-       // TODO: registry handles
-       switch (wep.ammo_type)
-       {
-               case RES_SHELLS:  ammoitems = ITEM_Shells.m_name;      break;
-               case RES_BULLETS: ammoitems = ITEM_Bullets.m_name;     break;
-               case RES_ROCKETS: ammoitems = ITEM_Rockets.m_name;     break;
-               case RES_CELLS:   ammoitems = ITEM_Cells.m_name;       break;
-               case RES_PLASMA:  ammoitems = ITEM_Plasma.m_name;      break;
-               case RES_FUEL:    ammoitems = ITEM_JetpackFuel.m_name; break;
-               default: return ""; // doesn't use ammo
-       }
+       if (wep.ammo_type.m_name)
+               ammoitems = wep.ammo_type.m_name;
        return sprintf(_(" with %d %s"), f2, ammoitems);
 }
 
index 6bf2d105f8d85c4c82299c9a216f3e20cd818062..27ab16644322c420936c7a2cb19957adeb996b55 100644 (file)
@@ -92,10 +92,6 @@ WepSet _WepSet_FromWeapon(int a)
        }
 #endif
 
-string W_FixWeaponOrder(string order, float complete)
-{
-       return fixPriorityList(order, WEP_FIRST, WEP_LAST, WEP_IMPULSE_BEGIN - WEP_FIRST, complete);
-}
 string W_NameWeaponOrder_MapFunc(string s)
 {
        int i = stof(s);
@@ -106,37 +102,13 @@ string W_NameWeaponOrder_MapFunc(string s)
        }
        return s;
 }
-
-string W_UndeprecateName(string s)
-{
-       switch (s)
-       {
-               case "nex": return "vortex";
-               case "rocketlauncher": return "devastator";
-               case "laser": return "blaster";
-               case "minstanex": return "vaporizer";
-               case "grenadelauncher": return "mortar";
-               case "uzi": return "machinegun";
-               case "hmg": return "okhmg";
-               case "rpc": return "okrpc";
-               default: return s;
-       }
-}
-string W_NameWeaponOrder(string order)
-{
-       return mapPriorityList(order, W_NameWeaponOrder_MapFunc);
-}
 string W_NumberWeaponOrder_MapFunc(string s)
 {
        if (s == "0" || stof(s)) return s;
-       s = W_UndeprecateName(s);
+       s = W_UNDEPRECATENAME(s);
        FOREACH(Weapons, it != WEP_Null && it.netname == s, return ftos(i));
        return s;
 }
-string W_NumberWeaponOrder(string order)
-{
-       return mapPriorityList(order, W_NumberWeaponOrder_MapFunc);
-}
 
 float W_FixWeaponOrder_BuildImpulseList_buf[REGISTRY_MAX(Weapons)];
 string W_FixWeaponOrder_BuildImpulseList_order;
@@ -176,13 +148,7 @@ string W_FixWeaponOrder_BuildImpulseList(string o)
 
 string W_FixWeaponOrder_AllowIncomplete(entity this, string order)
 {
-       return W_FixWeaponOrder(order, 0);
-}
-
-string W_FixWeaponOrder_ForceComplete(string order)
-{
-       if (order == "") order = W_NumberWeaponOrder(cvar_defstring("cl_weaponpriority"));
-       return W_FixWeaponOrder(order, 1);
+       return W_FIXWEAPONORDER(order, 0);
 }
 
 WepSet W_RandomWeapons(entity e, WepSet remaining, int n)
@@ -203,6 +169,10 @@ WepSet W_RandomWeapons(entity e, WepSet remaining, int n)
 }
 
 // TODO: registry handles for below functions
+// drjaska: made these functions into macros in
+// common/weapons/weapon.qh
+// instead of registry handles
+#if 0
 string GetAmmoPicture(Resource ammotype)
 {
        switch (ammotype)
@@ -230,6 +200,7 @@ string GetAmmoName(Resource ammotype)
                default:          return "batteries";
        }
 }
+#endif
 
 entity GetAmmoItem(Resource ammotype)
 {
@@ -245,39 +216,9 @@ entity GetAmmoItem(Resource ammotype)
        LOG_WARNF("Invalid ammo type %d ", ammotype.m_id);
        return NULL;
        // WEAPONTODO: use this generic func to reduce duplication ?
-       // GetAmmoPicture  GetAmmoName  notif_arg_item_wepammo  ammo_pickupevalfunc ?
-}
-
-#ifdef CSQC
-Resource GetAmmoTypeFromNum(int i)
-{
-       switch (i)
-       {
-               case 0: return RES_SHELLS;
-               case 1: return RES_BULLETS;
-               case 2: return RES_ROCKETS;
-               case 3: return RES_CELLS;
-               case 4: return RES_PLASMA;
-               case 5: return RES_FUEL;
-               default: return RES_NONE;
-       }
+       // GETAMMOPICTURE  GETAMMONAME  notif_arg_item_wepammo  ammo_pickupevalfunc ?
 }
 
-int GetAmmoStat(Resource ammotype)
-{
-       switch (ammotype)
-       {
-               case RES_SHELLS: return STAT_SHELLS;
-               case RES_BULLETS: return STAT_NAILS;
-               case RES_ROCKETS: return STAT_ROCKETS;
-               case RES_CELLS: return STAT_CELLS;
-               case RES_PLASMA: return STAT_PLASMA.m_id;
-               case RES_FUEL: return STAT_FUEL.m_id;
-               default: return -1;
-       }
-}
-#endif
-
 string W_Sound(string w_snd)
 {
        string output = strcat("weapons/", w_snd);
@@ -829,9 +770,9 @@ NET_HANDLE(w_muzzleflash, bool isNew)
 #endif
 
 #ifdef SVQC
-string W_FixWeaponOrder_ForceComplete_AndBuildImpulseList(entity this, string wo)
+string W_FIXWEAPNORDER_FORCECOMPLETE_AndBuildImpulseList(entity this, string wo)
 {
-       string o = W_FixWeaponOrder_ForceComplete(wo);
+       string o = W_FIXWEAPNORDER_FORCECOMPLETE(wo);
        strcpy(CS_CVAR(this).weaponorder_byimpulse, W_FixWeaponOrder_BuildImpulseList(o));
        return o;
 }
@@ -847,7 +788,7 @@ REPLICATE(cvar_cl_gunalign, int, "cl_gunalign");
 REPLICATE(cvar_cl_weapon_switch_reload, bool, "cl_weapon_switch_reload");
 REPLICATE(cvar_cl_weapon_switch_fallback_to_impulse, bool, "cl_weapon_switch_fallback_to_impulse");
 REPLICATE(cvar_cl_weaponimpulsemode, int, "cl_weaponimpulsemode");
-REPLICATE(cvar_cl_weaponpriority, string, "cl_weaponpriority", W_FixWeaponOrder_ForceComplete_AndBuildImpulseList);
+REPLICATE(cvar_cl_weaponpriority, string, "cl_weaponpriority", W_FIXWEAPNORDER_FORCECOMPLETE_AndBuildImpulseList);
 REPLICATE(cvar_cl_weaponpriorities[0], string, "cl_weaponpriority0", W_FixWeaponOrder_AllowIncomplete);
 REPLICATE(cvar_cl_weaponpriorities[1], string, "cl_weaponpriority1", W_FixWeaponOrder_AllowIncomplete);
 REPLICATE(cvar_cl_weaponpriorities[2], string, "cl_weaponpriority2", W_FixWeaponOrder_AllowIncomplete);
index acf4366593e62d3a1b72ba012b30e9121d6777e9..1618b1dc6c3b43dbe958915ae294e60024fe19df 100644 (file)
@@ -409,7 +409,7 @@ void wframe_send(entity actor, entity weaponentity, int wepframe, float attackra
 
 void W_MuzzleFlash(Weapon thiswep, entity actor, .entity weaponentity, vector shotorg, vector shotdir);
 
-string W_FixWeaponOrder_ForceComplete_AndBuildImpulseList(entity this, string wo);
+string W_FIXWEAPNORDER_FORCECOMPLETE_AndBuildImpulseList(entity this, string wo);
 string W_FixWeaponOrder_AllowIncomplete(entity this, string order);
 #endif
 
index 70009b15ffc32152ddf3a1c769e534146aebc4b7..b2cc8434b420e81b6828e117c475ac43bfe7b18b 100644 (file)
@@ -214,25 +214,48 @@ const int WEP_FLAG_SPECIALATTACK  =  BIT(16); // marked as a special attack (not
 string weaponorder_byid;
 
 // functions:
-string W_FixWeaponOrder(string order, float complete);
-string W_UndeprecateName(string s);
-string W_NameWeaponOrder(string order);
-string W_NumberWeaponOrder(string order);
+#define W_FIXWEAPONORDER(order, complete) fixPriorityList(order, WEP_FIRST, WEP_LAST, WEP_IMPULSE_BEGIN - WEP_FIRST, complete)
+
+#define W_UNDEPRECATENAME(s) \
+       (s == "nex" ? "vortex" : \
+       s == "rocketlauncher" ? "devastator" : \
+       s == "laser" ? "blaster" : \
+       s == "minstanex" ? "vaporizer" : \
+       s == "grenadelauncher" ? "mortar" : \
+       s == "uzi" ? "machinegun" : \
+       s == "hmg" ? "okhmg" : \
+       s == "rpc" ? "okrpc" : s)
+
+string W_NameWeaponOrder_MapFunc(string s);
+string W_NumberWeaponOrder_MapFunc(string s);
+#define W_NAMEWEAPONORDER(order) mapPriorityList(order, W_NameWeaponOrder_MapFunc)
+#define W_NUMBERWEAPONORDER(order) mapPriorityList(order, W_NumberWeaponOrder_MapFunc)
 string W_FixWeaponOrder_BuildImpulseList(string o);
 string W_FixWeaponOrder_AllowIncomplete(entity this, string order);
-string W_FixWeaponOrder_ForceComplete(string order);
 WepSet W_RandomWeapons(entity e, WepSet remaining, int n);
+#define W_FIXWEAPNORDER_FORCECOMPLETE(order) \
+       W_FIXWEAPONORDER(((order == "") ? W_NUMBERWEAPONORDER(cvar_defstring("cl_weaponpriority")) : order), 1)
 
-string GetAmmoPicture(Resource ammotype);
-
-string GetAmmoName(Resource ammotype);
-
+#define GETAMMOPICTURE(ammotype) GetAmmoItem(ammotype).m_icon
+#define GETAMMONAME(ammotype) GetAmmoItem(ammotype).m_name
 entity GetAmmoItem(Resource ammotype);
 
 #ifdef CSQC
-Resource GetAmmoTypeFromNum(int i);
+#define GETAMMOTYPEFROMNUM(i) \
+       i == 0 ? RES_SHELLS : \
+       i == 1 ? RES_BULLETS : \
+       i == 2 ? RES_ROCKETS : \
+       i == 3 ? RES_CELLS : \
+       i == 4 ? RES_PLASMA : \
+       i == 5 ? RES_FUEL : RES_NONE
 
-int GetAmmoStat(Resource ammotype);
+#define GETAMMOSTAT(res) \
+       res == RES_SHELLS ? STAT_SHELLS : \
+       res == RES_BULLETS ? STAT_NAILS : \
+       res == RES_ROCKETS ? STAT_ROCKETS : \
+       res == RES_CELLS ? STAT_CELLS : \
+       res == RES_PLASMA ? STAT_PLASMA.m_id : \
+       res == RES_FUEL ? STAT_FUEL.m_id : -1
 #endif
 
 string W_Sound(string w_snd);
index 157d22ffee2453db8d1713a90dc647f918ec4049..248e871e8e516cc9f3d14c46059cc52b5bf105e0 100644 (file)
@@ -19,10 +19,10 @@ void XonoticWeaponsList_draw(entity me)
 {
        // read in cvar?
        string s, t;
-       s = W_NumberWeaponOrder(cvar_string("cl_weaponpriority"));
-       t = W_FixWeaponOrder(s, 1);
+       s = W_NUMBERWEAPONORDER(cvar_string("cl_weaponpriority"));
+       t = W_FIXWEAPONORDER(s, 1);
        if(t != s)
-               cvar_set("cl_weaponpriority", W_NameWeaponOrder(t));
+               cvar_set("cl_weaponpriority", W_NAMEWEAPONORDER(t));
        me.nItems = tokenize_console(t);
        SUPER(XonoticWeaponsList).draw(me);
 }
@@ -72,7 +72,7 @@ string XonoticWeaponsList_toString(entity me)
        float n, i;
        string s;
        entity e;
-       n = tokenize_console(W_NumberWeaponOrder(cvar_string("cl_weaponpriority")));
+       n = tokenize_console(W_NUMBERWEAPONORDER(cvar_string("cl_weaponpriority")));
        s = "";
        for(i = 0; i < n; ++i)
        {
index b81507890f302da772d225f28f44c12a73ac85ae..d96056427f34d8653b6c4d03961859f178b3642a 100644 (file)
@@ -358,7 +358,7 @@ void bot_custom_weapon_priority_setup()
        #define PARSE_WEAPON_PRIORITIES(dist) MACRO_BEGIN \
                if (bot_priority_##dist##_prev != autocvar_bot_ai_custom_weapon_priority_##dist) { \
                        strcpy(bot_priority_##dist##_prev, autocvar_bot_ai_custom_weapon_priority_##dist); \
-                       tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_##dist)," "); \
+                       tokens = tokenizebyseparator(W_NUMBERWEAPONORDER(autocvar_bot_ai_custom_weapon_priority_##dist)," "); \
                        bot_weapons_##dist[0] = -1; \
                        c = 0; \
                        for(i = 0; i < tokens && c < REGISTRY_COUNT(Weapons); ++i) { \
index a73de2a1f6c58f2ed97e4303ae5179a7354b07db..0e6b2a3f9f59ddab3354e1733dc87150e1459365 100644 (file)
@@ -506,7 +506,7 @@ string formatmessage(entity this, string msg)
                        case "y": replacement = NearestLocation(cursor); break;
                        case "d": replacement = NearestLocation(this.death_origin); break;
                        case "w": replacement = WeaponNameFromWeaponentity(this, weaponentity); break;
-                       case "W": replacement = GetAmmoName(this.(weaponentity).m_weapon.ammo_type); break;
+                       case "W": replacement = GETAMMONAME(this.(weaponentity).m_weapon.ammo_type); break;
                        case "x": replacement = ((cursor_ent.netname == "" || !cursor_ent) ? "nothing" : cursor_ent.netname); break;
                        case "s": replacement = ftos(vlen(this.velocity - this.velocity_z * '0 0 1')); break;
                        case "S": replacement = ftos(vlen(this.velocity)); break;
index 5e900a603977c0b12cc1eb6d834dd545aa699e6e..5a973264b2e1baff24c02d5b658ebe743e2ee8b0 100644 (file)
@@ -831,7 +831,7 @@ float ammo_pickupevalfunc(entity player, entity item)
        {
                entity ammo = NULL;
                if(GetResource(item, RES_SHELLS))       { need_shells  = true; ammo = ITEM_Shells;      }
-               else if(GetResource(item, RES_BULLETS))   { need_nails   = true; ammo = ITEM_Bullets;     }
+               else if(GetResource(item, RES_BULLETS)) { need_nails   = true; ammo = ITEM_Bullets;     }
                else if(GetResource(item, RES_ROCKETS)) { need_rockets = true; ammo = ITEM_Rockets;     }
                else if(GetResource(item, RES_CELLS))   { need_cells   = true; ammo = ITEM_Cells;       }
                else if(GetResource(item, RES_PLASMA))  { need_plasma  = true; ammo = ITEM_Plasma;      }
@@ -1298,7 +1298,7 @@ spawnfunc(target_items)
                                        }
                                });
                                FOREACH(Weapons, it != WEP_Null, {
-                                       string s = W_UndeprecateName(argv(j));
+                                       string s = W_UNDEPRECATENAME(argv(j));
                                        if(s == it.netname)
                                        {
                                                STAT(WEAPONS, this) |= (it.m_wepset);
@@ -1364,7 +1364,7 @@ spawnfunc(target_items)
        n = tokenize_console(this.netname);
        for(int j = 0; j < n; ++j)
        {
-               FOREACH(Weapons, it != WEP_Null && W_UndeprecateName(argv(j)) == it.netname, {
+               FOREACH(Weapons, it != WEP_Null && W_UNDEPRECATENAME(argv(j)) == it.netname, {
                        it.wr_init(it);
                        break;
                });
@@ -1668,7 +1668,7 @@ float GiveItems(entity e, float beginarg, float endarg)
                                        got += GiveBuff(e, it, op, val);
                                        break;
                                });
-                               FOREACH(Weapons, it != WEP_Null && W_UndeprecateName(cmd) == it.netname, {
+                               FOREACH(Weapons, it != WEP_Null && W_UNDEPRECATENAME(cmd) == it.netname, {
                     got += GiveWeapon(e, it.m_id, op, val);
                     break;
                                });
index c61ef3fc414eeb76f0ab19794291e3573c11b463..4fe946b4776eb85763ce04a4e0eb231e90ad97e0 100644 (file)
@@ -710,7 +710,7 @@ void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use, .entity weaponenti
                        backtrace(sprintf(
                                "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
                                "Please notify the developers immediately with a copy of this backtrace!\n",
-                               ammo_use, wep.netname, GetAmmoPicture(wep.ammo_type), actor.netname, ammo));
+                               ammo_use, wep.netname, GETAMMOPICTURE(wep.ammo_type), actor.netname, ammo));
                }
                SetResource(actor, wep.ammo_type, ammo - ammo_use);
        }