Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: bots weapons configuration #99

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion cstrike/addons/amxmodx/configs/redm/gamemode_deathmatch.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@
"weapon_p228",
"weapon_elite",
"weapon_fiveseven"
]
],
"botEquip": {
"primary": [
"weapon_famas",
"weapon_galil"
],
"secondary": [
"weapon_deagle",
"weapon_p228",
"weapon_elite"
]
}
},
"cvars": {
// ReDM: Spawns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ enum {
/**
* Arrays to store player equipment choices.
*/
static g_equip[EquipType_e]
static Array: g_equip[EquipType_e]
static g_playerWeapons[MAX_PLAYERS + 1][EquipType_e]

static Array: g_botWeapons[EquipType_e]

/**
* Array to store the last weapon slot chosen by players.
*/
Expand Down Expand Up @@ -113,16 +115,16 @@ EquipManager_PutInServer(const player) {
*/
EquipManager_LoadConfig(const JSON: objEquip) {
for (new EquipType_e: section; section < EquipType_e; section++) {
LoadConfigEquip(objEquip, section)
LoadConfigEquip(objEquip, section, g_equip[section])
LoadConfigBotEquip(objEquip, section)
}
}

static LoadConfigEquip(const JSON: objEquip, const EquipType_e: section) {
if (g_equip[section] == _: Invalid_Array) {
g_equip[section] = _: ArrayCreate(32)
}
static LoadConfigEquip(const JSON: objEquip, const EquipType_e: section, &Array: equipArray) {
if (equipArray == Invalid_Array)
equipArray = ArrayCreate(32)

ArrayClear(Array: g_equip[section])
ArrayClear(equipArray)

if (!json_object_has_value(objEquip, g_equipSections[section]))
return
Expand Down Expand Up @@ -151,20 +153,37 @@ static LoadConfigEquip(const JSON: objEquip, const EquipType_e: section) {
continue
}

if (ArrayFindString(Array: g_equip[section], weapon) != -1) {
if (ArrayFindString(equipArray, weapon) != -1) {
LogMessageEx(Warning, "LoadConfigEquip(): WARNING! Weapon `%s` already in equip `%s` list. Skipped.",
weapon, g_equipSections[section]
)

continue
}

ArrayPushString(Array: g_equip[section], weapon)
ArrayPushString(equipArray, weapon)
}

json_free(equipWeapons)
}

static LoadConfigBotEquip(const JSON: objEquip, const EquipType_e: section) {
if (!json_object_has_value(objEquip, "botEquip"))
return

new JSON: equipWeapons = json_object_get_value(objEquip, "botEquip")
if (equipWeapons == Invalid_JSON)
set_fail_state("Can't read `%s` section from config", g_equipSections[section])

if (g_botWeapons[section] == Invalid_Array)
g_botWeapons[section] = ArrayCreate(32)

ArrayClear(g_botWeapons[section])
LoadConfigEquip(equipWeapons, section, g_botWeapons[section])

json_free(equipWeapons)
}

/**
* Handles the "cl_autobuy" client command (key F1).
*
Expand Down Expand Up @@ -251,7 +270,7 @@ static bool: Player_CallEquipMenu(const player) {
}

for (new EquipType_e: section; section < EquipType_e; section++) {
if (!HasEquipItems(section))
if (!HasEquipItems(player, section))
continue

Menu_ChooseEquip(player, section)
Expand Down Expand Up @@ -357,7 +376,7 @@ public MenuHandler_ChooseEquip(const player, const menu, const item) {
return PLUGIN_HANDLED
}

if (!HasEquipItems(section))
if (!HasEquipItems(player, section))
return PLUGIN_HANDLED

Menu_ChooseEquip(player, section)
Expand All @@ -382,11 +401,11 @@ public CBasePlayer_OnSpawnEquip(const player, bool: addDefault, bool: equipGame)

new bool: fullyEquipped = true
for (new EquipType_e: section; section < EquipType_e; section++) {
if (!HasEquipItems(section))
if (!HasEquipItems(player, section))
continue

if (g_playerRandomWeapons[player])
g_playerWeapons[player][section] = random_num(0, ArraySize(Array: g_equip[section]) - 1)
g_playerWeapons[player][section] = random_num(0, ArraySize(GetEquipConfig(player, section)) - 1)

if (g_playerWeapons[player][section] != EQUIP_NOT_CHOOSEN)
continue
Expand Down Expand Up @@ -513,7 +532,7 @@ static bool: Player_GiveWeapon(const player, const EquipType_e: section) {

new weaponName[32]
ArrayGetString(
Array: g_equip[section],
GetEquipConfig(player, section),
g_playerWeapons[player][section],
weaponName,
charsmax(weaponName)
Expand Down Expand Up @@ -545,6 +564,9 @@ EquipManager_Reset(const player) {
for (new EquipType_e: section; section < EquipType_e; section++) {
g_playerWeapons[player][section] = EQUIP_NOT_CHOOSEN
}

if (is_user_bot(player))
g_playerRandomWeapons[player] = true
}

/**
Expand All @@ -557,8 +579,18 @@ EquipManager_Reset(const player) {
*
* @return Returns true if the specified section has available items, false otherwise.
*/
static bool: HasEquipItems(const EquipType_e: section) {
return ArraySize(Array: g_equip[section]) != 0
static bool: HasEquipItems(const player, const EquipType_e: section) {
return ArraySize(GetEquipConfig(player, section)) != 0
}

static Array: GetEquipConfig(const player, const EquipType_e: section) {
if (!is_user_bot(player))
return g_equip[section]

if (g_botWeapons[section] == Invalid_Array || ArraySize(g_botWeapons[section]) == 0)
return g_equip[section]

return g_botWeapons[section]
}

public ConCmd_redm_dump_equip(const player, const level, const commandId) {
Expand Down
Loading