From 2c5cc4289e3f90362108e98d1744a1107aad9b61 Mon Sep 17 00:00:00 2001 From: Arkshine Date: Mon, 27 Jul 2015 11:26:32 +0200 Subject: [PATCH] Cstrike: Add cs_is_valid_itemid() stock --- modules/cstrike/cstrike/CstrikeItemsInfos.cpp | 2 +- plugins/include/cstrike.inc | 105 ++++++++++++------ 2 files changed, 73 insertions(+), 34 deletions(-) diff --git a/modules/cstrike/cstrike/CstrikeItemsInfos.cpp b/modules/cstrike/cstrike/CstrikeItemsInfos.cpp index ad8c5532..41510368 100644 --- a/modules/cstrike/cstrike/CstrikeItemsInfos.cpp +++ b/modules/cstrike/cstrike/CstrikeItemsInfos.cpp @@ -11,7 +11,7 @@ // Counter-Strike Module // -#include "CstrikeitemsInfos.h" +#include "CstrikeItemsInfos.h" CsItemInfo ItemsManager; diff --git a/plugins/include/cstrike.inc b/plugins/include/cstrike.inc index d45fb904..a7324a19 100755 --- a/plugins/include/cstrike.inc +++ b/plugins/include/cstrike.inc @@ -1096,47 +1096,86 @@ stock CsWeaponClassType:cs_get_weapon_class(weapon_id) { new CsWeaponClassType:type = CS_WEAPONCLASS_NONE; - if (CSI_NONE <= weapon_id <= CSI_LAST_WEAPON || weapon_id == CSI_SHIELDGUN || weapon_id == CSI_SHIELD) + if (cs_is_valid_itemid(weapon_id, .weapon_only = true) || weapon_id == CSI_SHIELD) { - new const bits = (1 << weapon_id); + switch (weapon_id) + { + case CSI_SHIELDGUN, CSI_SHIELD: + { + type = CS_WEAPONCLASS_PISTOL; + } + case CSI_KNIFE: + { + type = CS_WEAPONCLASS_KNIFE; + } + default: + { + new const bits = (1 << weapon_id); - if (bits & (1 << CSI_KNIFE)) - { - type = CS_WEAPONCLASS_KNIFE; - } - else if(bits & CSI_ALL_PISTOLS || weapon_id == CSI_SHIELDGUN || weapon_id == CSI_SHIELD) - { - type = CS_WEAPONCLASS_PISTOL; - } - else if(bits & CSI_ALL_GRENADES) - { - type = CS_WEAPONCLASS_GRENADE; - } - else if(bits & CSI_ALL_SMGS) - { - type = CS_WEAPONCLASS_SUBMACHINEGUN; - } - else if(bits & CSI_ALL_SHOTGUNS) - { - type = CS_WEAPONCLASS_SHOTGUN; - } - else if(bits & CSI_ALL_MACHINEGUNS) - { - type = CS_WEAPONCLASS_MACHINEGUN; - } - else if(bits & CSI_ALL_RIFLES) - { - type = CS_WEAPONCLASS_RIFLE; - } - else if(bits & CSI_ALL_SNIPERRIFLES) - { - type = CS_WEAPONCLASS_SNIPERRIFLE; + if(bits & CSI_ALL_PISTOLS) + { + type = CS_WEAPONCLASS_PISTOL; + } + else if(bits & CSI_ALL_GRENADES) + { + type = CS_WEAPONCLASS_GRENADE; + } + else if(bits & CSI_ALL_SMGS) + { + type = CS_WEAPONCLASS_SUBMACHINEGUN; + } + else if(bits & CSI_ALL_SHOTGUNS) + { + type = CS_WEAPONCLASS_SHOTGUN; + } + else if(bits & CSI_ALL_MACHINEGUNS) + { + type = CS_WEAPONCLASS_MACHINEGUN; + } + else if(bits & CSI_ALL_RIFLES) + { + type = CS_WEAPONCLASS_RIFLE; + } + else if(bits & CSI_ALL_SNIPERRIFLES) + { + type = CS_WEAPONCLASS_SNIPERRIFLE; + } + } } } return type; } +/** + * Checks whether an item id is not out of bounds. + * + * @param id Item id (CSI_* constants) + * @param weapon_only If true, only the real weapon ids will be checked, + * including shield as well + * + * @return True if item id is valid, false otherwise + */ +stock bool:cs_is_valid_itemid(id, bool:weapon_only = false) +{ + if (id <= CSI_NONE) + { + return false; + } + + if (id > CSI_LAST_WEAPON && id != CSI_SHIELDGUN && weapon_only) + { + return false; + } + + if (id >= CSI_MAX_COUNT) + { + return false; + } + + return true; +} + /** * Called when CS internally fires a command to a player. *