From d0ec2a5c7236bad38eceedec09674e9f6ef10996 Mon Sep 17 00:00:00 2001 From: Jaime Moreira Date: Mon, 11 Apr 2022 09:39:52 -0400 Subject: [PATCH] Added prefweap command to select weapon by priority --- src/g_cmds.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/src/g_cmds.c b/src/g_cmds.c index 24bf4f2..1d621e7 100644 --- a/src/g_cmds.c +++ b/src/g_cmds.c @@ -1589,6 +1589,112 @@ Cmd_CycleWeap_f(edict_t *ent) } } +static gitem_t * +preferred_weapon(edict_t *ent) +{ + gclient_t *cl; + gitem_t *noammo_fallback; + gitem_t *noweap_fallback; + gitem_t *weap; + gitem_t *ammo; + int i; + int num_weaps; + + if (!ent) + { + return NULL; + } + + cl = ent->client; + + if (!cl) + { + return NULL; + } + + num_weaps = gi.argc(); + noammo_fallback = NULL; + noweap_fallback = NULL; + + /* find the first eligible weapon in the list we can switch to */ + for (i = 1; i < num_weaps; i++) + { + weap = FindItemByClassname(gi.argv(i)); + + if (weap && (weap->flags & IT_WEAPON) && weap->use) + { + if (cl->pers.inventory[ITEM_INDEX(weap)] > 0) + { + if (weap->ammo) + { + ammo = FindItem(weap->ammo); + if (ammo) + { + if (cl->pers.inventory[ITEM_INDEX(ammo)] >= get_ammo_usage(weap)) + { + return weap; + } + + if (!noammo_fallback) + { + noammo_fallback = weap; + } + } + } + else + { + return weap; + } + } + else if (!noweap_fallback) + { + noweap_fallback = weap; + } + } + } + + /* if no weapon was found, the fallbacks will be used for + printing the appropriate error message to the console + */ + + if (noammo_fallback) + { + return noammo_fallback; + } + + return noweap_fallback; +} + +void +Cmd_PrefWeap_f(edict_t *ent) +{ + gitem_t *weap; + + if (!ent) + { + return; + } + + if (gi.argc() <= 1) + { + gi.cprintf(ent, PRINT_HIGH, "Usage: prefweap classname1 classname2 .. classnameN\n"); + return; + } + + weap = preferred_weapon(ent); + if (weap) + { + if (ent->client->pers.inventory[ITEM_INDEX(weap)] <= 0) + { + gi.cprintf(ent, PRINT_HIGH, "Out of item: %s\n", weap->pickup_name); + } + else + { + weap->use(ent, weap); + } + } +} + void ClientCommand(edict_t *ent) { @@ -1741,6 +1847,10 @@ ClientCommand(edict_t *ent) { Cmd_CycleWeap_f(ent); } + else if (Q_stricmp(cmd, "prefweap") == 0) + { + Cmd_PrefWeap_f(ent); + } else /* anything that doesn't match a command will be a chat */ { Cmd_Say_f(ent, false, true);