Merge pull request #70 from protocultor/prefweap

Added prefweap command to select weapon by priority
This commit is contained in:
Yamagi 2022-04-13 11:51:11 +02:00 committed by GitHub
commit fdf95542f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 110 additions and 0 deletions

View File

@ -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);