From dfbab9f12ff221e0f7ce646370c0a6e937ebe490 Mon Sep 17 00:00:00 2001 From: Mario Date: Sat, 31 Aug 2024 08:47:47 +0000 Subject: [PATCH] Clean up ammo displaying to not require direct item references --- qcsrc/client/hud/panel/ammo.qc | 2 +- qcsrc/common/notifications/all.qh | 16 ++--------- qcsrc/common/resources/all.inc | 13 +++++++++ qcsrc/common/resources/resources.qh | 2 ++ qcsrc/common/weapons/all.qc | 43 +++------------------------- qcsrc/common/weapons/weapon.qh | 4 --- qcsrc/server/chat.qc | 19 ++++++++++-- qcsrc/server/weapons/weaponsystem.qc | 2 +- 8 files changed, 40 insertions(+), 61 deletions(-) diff --git a/qcsrc/client/hud/panel/ammo.qc b/qcsrc/client/hud/panel/ammo.qc index 1f748d542..891123bf1 100644 --- a/qcsrc/client/hud/panel/ammo.qc +++ b/qcsrc/client/hud/panel/ammo.qc @@ -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, ammoType.m_icon, '1 1 0' * mySize.y, iconColor, alpha, DRAWFLAG_NORMAL); } int nade_prevstatus; diff --git a/qcsrc/common/notifications/all.qh b/qcsrc/common/notifications/all.qh index ffda57cc3..e56658a68 100644 --- a/qcsrc/common/notifications/all.qh +++ b/qcsrc/common/notifications/all.qh @@ -651,21 +651,11 @@ string notif_arg_spree_inf(float type, string input, string player, float spree) string notif_arg_item_wepammo(float f1, float f2) { - string ammotype = ""; Weapon wep = REGISTRY_GET(Weapons, f1); - // TODO: registry handles - switch (wep.ammo_type) - { - case RES_SHELLS: ammotype = ITEM_Shells.m_name; break; - case RES_BULLETS: ammotype = ITEM_Bullets.m_name; break; - case RES_ROCKETS: ammotype = ITEM_Rockets.m_name; break; - case RES_CELLS: ammotype = ITEM_Cells.m_name; break; - case RES_PLASMA: ammotype = ITEM_Plasma.m_name; break; - case RES_FUEL: ammotype = ITEM_JetpackFuel.m_name; break; - default: return ""; // doesn't use ammo - } + if(wep.ammo_type == RES_NONE) + return ""; // doesn't use ammo // example for translators: You dropped the Vortex with 5 cells - return sprintf(_(" with %d %s"), f2, strtolower(ammotype)); + return sprintf(_(" with %d %s"), f2, strtolower(wep.ammo_type.m_name)); } diff --git a/qcsrc/common/resources/all.inc b/qcsrc/common/resources/all.inc index 0ad9ba31e..8ef77ebf1 100644 --- a/qcsrc/common/resources/all.inc +++ b/qcsrc/common/resources/all.inc @@ -19,36 +19,47 @@ ENDCLASS(AmmoResource) // NOTE: ammo resource registration order should match ammo (item) registration order // see REGISTER_ITEM calls order +// ALSO make sure m_name and m_icon fields match ammo item registries REGISTER_RESOURCE(SHELLS, NEW(AmmoResource)) { this.netname = "shells"; #ifdef GAMEQC this.m_field = ammo_shells; #endif + this.m_name = _("Shells"); + this.m_icon = "ammo_shells"; } REGISTER_RESOURCE(BULLETS, NEW(AmmoResource)) { this.netname = "bullets"; #ifdef GAMEQC this.m_field = ammo_nails; #endif + this.m_name = _("Bullets"); + this.m_icon = "ammo_bullets"; } REGISTER_RESOURCE(ROCKETS, NEW(AmmoResource)) { this.netname = "rockets"; #ifdef GAMEQC this.m_field = ammo_rockets; #endif + this.m_name = _("Rockets"); + this.m_icon = "ammo_rockets"; } REGISTER_RESOURCE(CELLS, NEW(AmmoResource)) { this.netname = "cells"; #ifdef GAMEQC this.m_field = ammo_cells; #endif + this.m_name = _("Cells"); + this.m_icon = "ammo_cells"; } REGISTER_RESOURCE(PLASMA, NEW(AmmoResource)) { this.netname = "plasma"; #ifdef GAMEQC this.m_field = ammo_plasma; #endif + this.m_name = _("Plasma"); + this.m_icon = "ammo_plasma"; #ifdef CSQC this.m_hidden = true; // WIP ammo type #endif @@ -58,6 +69,8 @@ REGISTER_RESOURCE(FUEL, NEW(AmmoResource)) { #ifdef GAMEQC this.m_field = ammo_fuel; #endif + this.m_name = _("Fuel"); + this.m_icon = "ammo_fuel"; #ifdef CSQC this.m_hidden = true; // displayed in a separate panel #endif diff --git a/qcsrc/common/resources/resources.qh b/qcsrc/common/resources/resources.qh index 28ad1f82a..8839b3a88 100644 --- a/qcsrc/common/resources/resources.qh +++ b/qcsrc/common/resources/resources.qh @@ -30,6 +30,8 @@ CLASS(Resource, Object) #ifdef GAMEQC ATTRIB(Resource, m_field, .float, health); #endif + ATTRIB(Resource, m_name, string, ""); + ATTRIB(Resource, m_icon, string, ""); ENDCLASS(Resource) #define REGISTER_RESOURCE(id, inst) REGISTER(Resources, RES, id, m_id, inst) diff --git a/qcsrc/common/weapons/all.qc b/qcsrc/common/weapons/all.qc index 8425bb810..255a921f0 100644 --- a/qcsrc/common/weapons/all.qc +++ b/qcsrc/common/weapons/all.qc @@ -186,55 +186,20 @@ WepSet W_RandomWeapons(entity e, WepSet remaining, int n) return result; } -// TODO: registry handles for below functions -string GetAmmoPicture(Resource ammotype) -{ - switch (ammotype) - { - case RES_SHELLS: return ITEM_Shells.m_icon; - case RES_BULLETS: return ITEM_Bullets.m_icon; - case RES_ROCKETS: return ITEM_Rockets.m_icon; - case RES_CELLS: return ITEM_Cells.m_icon; - case RES_PLASMA: return ITEM_Plasma.m_icon; - case RES_FUEL: return ITEM_JetpackFuel.m_icon; - default: return ""; // wtf, no ammo type? - } -} - -string GetAmmoName(Resource ammotype) -{ - switch (ammotype) - { - case RES_SHELLS: return ITEM_Shells.m_name; - case RES_BULLETS: return ITEM_Bullets.m_name; - case RES_ROCKETS: return ITEM_Rockets.m_name; - case RES_CELLS: return ITEM_Cells.m_name; - case RES_PLASMA: return ITEM_Plasma.m_name; - case RES_FUEL: return ITEM_JetpackFuel.m_name; - default: return "batteries"; - } -} - entity GetAmmoItem(Resource ammotype) { - switch (ammotype) + FOREACH(Items, it.netname == ammotype.netname, { - case RES_SHELLS: return ITEM_Shells; - case RES_BULLETS: return ITEM_Bullets; - case RES_ROCKETS: return ITEM_Rockets; - case RES_CELLS: return ITEM_Cells; - case RES_PLASMA: return ITEM_Plasma; - case RES_FUEL: return ITEM_JetpackFuel; - } + return it; + }); 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 int GetAmmoStat(Resource ammotype) { + // TODO: handle networking via resources switch (ammotype) { case RES_SHELLS: return STAT_SHELLS; diff --git a/qcsrc/common/weapons/weapon.qh b/qcsrc/common/weapons/weapon.qh index cf0f637f0..14afd2b7c 100644 --- a/qcsrc/common/weapons/weapon.qh +++ b/qcsrc/common/weapons/weapon.qh @@ -224,10 +224,6 @@ string W_FixWeaponOrder_AllowIncomplete(entity this, string order); string W_FixWeaponOrder_ForceComplete(string order); WepSet W_RandomWeapons(entity e, WepSet remaining, int n); -string GetAmmoPicture(Resource ammotype); - -string GetAmmoName(Resource ammotype); - entity GetAmmoItem(Resource ammotype); #ifdef CSQC diff --git a/qcsrc/server/chat.qc b/qcsrc/server/chat.qc index b0f349233..154644a37 100644 --- a/qcsrc/server/chat.qc +++ b/qcsrc/server/chat.qc @@ -480,8 +480,8 @@ string PlayerHealth(entity this) string WeaponNameFromWeaponentity(entity this, .entity weaponentity) { entity wepent = this.(weaponentity); - if(!wepent) - return "none"; + if(!wepent || !IS_PLAYER(this)) + return "N/A"; else if(wepent.m_weapon != WEP_Null) return wepent.m_weapon.m_name; else if(wepent.m_switchweapon != WEP_Null) @@ -489,6 +489,19 @@ string WeaponNameFromWeaponentity(entity this, .entity weaponentity) return "none"; //REGISTRY_GET(Weapons, wepent.cnt).m_name; } +string AmmoNameFromWeaponentity(entity this, .entity weaponentity) +{ + entity wepent = this.(weaponentity); + string fallback = "N/A"; + if(!wepent || !IS_PLAYER(this)) + return fallback; + else if(wepent.m_weapon.ammo_type != RES_NONE) + return wepent.m_weapon.ammo_type.m_name; + else if(wepent.m_switchweapon.ammo_type != RES_NONE) + return wepent.m_switchweapon.ammo_type.m_name; + return fallback; // REGISTRY_GET(Weapons, wepent.cnt).ammo_type.m_name; +} + string formatmessage(entity this, string msg) { float p, p1, p2; @@ -549,7 +562,7 @@ string formatmessage(entity this, string msg) case "o": replacement = vtos(this.origin); break; case "O": replacement = sprintf("'%f %f %f'", this.origin.x, this.origin.y, this.origin.z); break; case "w": replacement = WeaponNameFromWeaponentity(this, weaponentity); break; - case "W": replacement = GetAmmoName(this.(weaponentity).m_weapon.ammo_type); break; + case "W": replacement = AmmoNameFromWeaponentity(this, weaponentity); 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/weapons/weaponsystem.qc b/qcsrc/server/weapons/weaponsystem.qc index dc3045256..dccec6c48 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, wep.ammo_type.netname, actor.netname, ammo)); } SetResource(actor, wep.ammo_type, ammo - ammo_use); } -- 2.39.2