From: drjaska Date: Wed, 28 Dec 2022 18:20:22 +0000 (+0200) Subject: rewrite small functions into macros X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=8775c9c23a166191b5ccdb9101b7043727a4b8f9;p=xonotic%2Fxonotic-data.pk3dir.git rewrite small functions into macros --- diff --git a/qcsrc/client/command/cl_cmd.qc b/qcsrc/client/command/cl_cmd.qc index 0f8a69e72..e991e66b5 100644 --- a/qcsrc/client/command/cl_cmd.qc +++ b/qcsrc/client/command/cl_cmd.qc @@ -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); diff --git a/qcsrc/client/hud/panel/ammo.qc b/qcsrc/client/hud/panel/ammo.qc index 0cc1f2395..cd64cafb1 100644 --- a/qcsrc/client/hud/panel/ammo.qc +++ b/qcsrc/client/hud/panel/ammo.qc @@ -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, diff --git a/qcsrc/client/hud/panel/weapons.qc b/qcsrc/client/hud/panel/weapons.qc index b51c5761d..720a73991 100644 --- a/qcsrc/client/hud/panel/weapons.qc +++ b/qcsrc/client/hud/panel/weapons.qc @@ -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) { diff --git a/qcsrc/common/notifications/all.qh b/qcsrc/common/notifications/all.qh index 74e313dce..e9eb9895c 100644 --- a/qcsrc/common/notifications/all.qh +++ b/qcsrc/common/notifications/all.qh @@ -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); } diff --git a/qcsrc/common/weapons/all.qc b/qcsrc/common/weapons/all.qc index 6bf2d105f..27ab16644 100644 --- a/qcsrc/common/weapons/all.qc +++ b/qcsrc/common/weapons/all.qc @@ -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); diff --git a/qcsrc/common/weapons/all.qh b/qcsrc/common/weapons/all.qh index acf436659..1618b1dc6 100644 --- a/qcsrc/common/weapons/all.qh +++ b/qcsrc/common/weapons/all.qh @@ -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 diff --git a/qcsrc/common/weapons/weapon.qh b/qcsrc/common/weapons/weapon.qh index 70009b15f..b2cc8434b 100644 --- a/qcsrc/common/weapons/weapon.qh +++ b/qcsrc/common/weapons/weapon.qh @@ -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); diff --git a/qcsrc/menu/xonotic/weaponslist.qc b/qcsrc/menu/xonotic/weaponslist.qc index 157d22ffe..248e871e8 100644 --- a/qcsrc/menu/xonotic/weaponslist.qc +++ b/qcsrc/menu/xonotic/weaponslist.qc @@ -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) { diff --git a/qcsrc/server/bot/default/bot.qc b/qcsrc/server/bot/default/bot.qc index b81507890..d96056427 100644 --- a/qcsrc/server/bot/default/bot.qc +++ b/qcsrc/server/bot/default/bot.qc @@ -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) { \ diff --git a/qcsrc/server/chat.qc b/qcsrc/server/chat.qc index a73de2a1f..0e6b2a3f9 100644 --- a/qcsrc/server/chat.qc +++ b/qcsrc/server/chat.qc @@ -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; diff --git a/qcsrc/server/items/items.qc b/qcsrc/server/items/items.qc index 5e900a603..5a973264b 100644 --- a/qcsrc/server/items/items.qc +++ b/qcsrc/server/items/items.qc @@ -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; }); diff --git a/qcsrc/server/weapons/weaponsystem.qc b/qcsrc/server/weapons/weaponsystem.qc index c61ef3fc4..4fe946b47 100644 --- a/qcsrc/server/weapons/weaponsystem.qc +++ b/qcsrc/server/weapons/weaponsystem.qc @@ -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); }