]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Clean up ammo displaying to not require direct item references
authorMario <mario.mario@y7mail.com>
Sat, 31 Aug 2024 08:47:47 +0000 (08:47 +0000)
committerterencehill <piuntn@gmail.com>
Sat, 31 Aug 2024 08:47:47 +0000 (08:47 +0000)
qcsrc/client/hud/panel/ammo.qc
qcsrc/common/notifications/all.qh
qcsrc/common/resources/all.inc
qcsrc/common/resources/resources.qh
qcsrc/common/weapons/all.qc
qcsrc/common/weapons/weapon.qh
qcsrc/server/chat.qc
qcsrc/server/weapons/weaponsystem.qc

index 1f748d5427f7129c98b74765baddd6d4ff4fdfc7..891123bf1e3693b8d2c1655dcd437a87f377c6dc 100644 (file)
@@ -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;
index ffda57cc33e6849ba413b1c9480c6b5fdb3bf1d5..e56658a682e124e33f4018109e22b7922a313224 100644 (file)
@@ -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));
 }
 
 
index 0ad9ba31e77bd680f159f39556d671f2646f0026..8ef77ebf1592152b2590cfbbc47c0505f840ac17 100644 (file)
@@ -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
index 28ad1f82adb628e128887661a1ddf191f346a034..8839b3a88e22b15140563f62814d203ad20bb449 100644 (file)
@@ -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)
index 8425bb810faa76f4c2ebb227c393b1b109a2a5bd..255a921f039a6f3a6da69e7c42f47d661e5d19c5 100644 (file)
@@ -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;
index cf0f637f0324d614da2ffe5212cf408a5988f357..14afd2b7cec5754c02c66865e4fbf89696ad62a6 100644 (file)
@@ -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
index b0f3492331de7203a2ba0d301a009881cbb14c3c..154644a37e882afc09bf3f95e3486a0f6f132ca6 100644 (file)
@@ -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;
index dc30452569c6b982b3923875c09428423509aa6d..dccec6c4803bb2adb36aff899479baaf86e1e2a4 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, wep.ammo_type.netname, actor.netname, ammo));
                }
                SetResource(actor, wep.ammo_type, ammo - ammo_use);
        }