Weapons module added, still needs better validations

This commit is contained in:
Greyscale
2009-04-12 08:04:00 +02:00
parent d063def554
commit 035d6182a9
12 changed files with 930 additions and 201 deletions

View File

@ -8,32 +8,34 @@
/**
* Array to store restricted weapon names.
*/
*/
new Handle:gRestrictedWeapons = INVALID_HANDLE;
/**
* Array to store keyvalue data.
*/
*/
new Handle:kvWeaponGroups = INVALID_HANDLE;
/**
* Array that stores the "HookID" to be later unhooked on player disconnect.
*/
*/
new gCanUseHookID[MAXPLAYERS+1];
/**
* Query results returned when (un)restricting a weapon.
*/
*/
enum WpnRestrictQuery
{
Successful_Weapon, /** Weapon (un)restrict query was successful. */
Successful_Group, /** Group (un)restrict query was successful. */
Failed_Weapon, /** Weapon (un)restrict was unsuccessful */
Failed_Group, /** Group (un)restrict was unsuccessful */
Invalid, /** Weapon/Group invalid */
}
/**
* Initialize data and hook commands.
*/
*/
WeaponRestrictInit()
{
// Initialize weapon restrict array
@ -45,8 +47,8 @@ WeaponRestrictInit()
/**
* Loads weapon data from file.
*/
WeaponRestrictMapStart()
*/
WeaponRestrictOnMapStart()
{
// Clear restricted weapons
RestrictWeaponUnrestrictAll();
@ -60,17 +62,44 @@ WeaponRestrictMapStart()
kvWeaponGroups = CreateKeyValues("weapongroups");
decl String:path[PLATFORM_MAX_PATH];
BuildPath(Path_SM, path, sizeof(path), "configs/zr/weapongroups.txt");
BuildPath(Path_SM, path, sizeof(path), "configs/zr/weapons/weapongroups.txt");
// If file isn't found, stop plugin
if (!FileToKeyValues(kvWeaponGroups, path))
{
SetFailState("\"%s\" missing from server", path);
}
KvRewind(kvWeaponGroups);
if (KvGotoFirstSubKey(kvWeaponGroups))
{
do
{
if (KvGotoFirstSubKey(kvWeaponGroups))
{
decl String:groupweapon[32];
do
{
KvGetSectionName(kvWeaponGroups, groupweapon, sizeof(groupweapon));
if (!WeaponsIsValidWeapon(groupweapon))
{
// TODO: LOG INVALID WEAPON
}
} while (KvGotoNextKey(kvWeaponGroups));
KvGoBack(kvWeaponGroups);
}
} while (KvGotoNextKey(kvWeaponGroups));
}
}
/**
* Clears restricted weapon array.
*/
*/
RestrictWeaponUnrestrictAll()
{
ClearArray(gRestrictedWeapons);
@ -80,7 +109,7 @@ RestrictWeaponUnrestrictAll()
* Hook Weapon_CanUse function on a client.
*
* @param client The client index.
*/
*/
WeaponRestrictClientInit(client)
{
gCanUseHookID[client] = Hacks_Hook(client, HACKS_HTYPE_WEAPON_CANUSE, WeaponRestrictCanUse, false);
@ -90,7 +119,7 @@ WeaponRestrictClientInit(client)
* Unhook Weapon_CanUse function on a client.
*
* @param client The client index.
*/
*/
WeaponRestrictClientDisconnect(client)
{
Hacks_Unhook(gCanUseHookID[client]);
@ -102,7 +131,7 @@ WeaponRestrictClientDisconnect(client)
*
* @param client The client index.
* @param argc Argument count.
*/
*/
public Action:WeaponRestrictBuyHook(client, argc)
{
// If plugin is disabled then stop
@ -146,8 +175,10 @@ public Action:WeaponRestrictBuyHook(client, argc)
* @param weapon The weapon/group name.
* @return Successful_Weapon: The call successfully restricted a weapon.
* Successful_Group: The call successfully restricted a weapon group.
* Failed_Weapon: The call failed to restrict a weapon.
* Failed_Group: The call failed to restrict a weapon group.
* Invalid: The call was unsuccessful due to invalid weapon.
*/
*/
WpnRestrictQuery:WeaponRestrictRestrict(const String:weapon[])
{
if (WeaponRestrictIsCustomGroup(weapon))
@ -164,6 +195,12 @@ WpnRestrictQuery:WeaponRestrictRestrict(const String:weapon[])
KvGetSectionName(kvWeaponGroups, groupweapon, sizeof(groupweapon));
// If weapon is invalid, then skip
if (!WeaponsIsValidWeapon(groupweapon))
{
continue;
}
if (!WeaponRestrictIsRestricted(groupweapon))
{
PushArrayString(gRestrictedWeapons, groupweapon);
@ -174,11 +211,18 @@ WpnRestrictQuery:WeaponRestrictRestrict(const String:weapon[])
return Successful_Group;
}
if (!WeaponRestrictIsRestricted(weapon))
if (!WeaponsIsValidWeapon(weapon))
{
PushArrayString(gRestrictedWeapons, weapon);
return Invalid;
}
if (WeaponRestrictIsRestricted(weapon))
{
return Failed_Weapon;
}
PushArrayString(gRestrictedWeapons, weapon);
return Successful_Weapon;
}
@ -188,8 +232,10 @@ WpnRestrictQuery:WeaponRestrictRestrict(const String:weapon[])
* @param weapon The weapon/group name.
* @return Successful_Weapon: The call successfully restricted a weapon.
* Successful_Group: The call successfully restricted a weapon group.
* Invalid: The call was unsuccessful due to invalid weapon.
*/
* Failed_Weapon: The call failed to restrict a weapon.
* Failed_Group: The call failed to restrict a weapon group.
* Invalid: The call was unsuccessful due to invalid weapon.
*/
WpnRestrictQuery:WeaponRestrictUnrestrict(const String:weapon[])
{
if (WeaponRestrictIsCustomGroup(weapon))
@ -199,12 +245,18 @@ WpnRestrictQuery:WeaponRestrictUnrestrict(const String:weapon[])
if (KvGotoFirstSubKey(kvWeaponGroups))
{
decl String:groupweapon[32];
decl String:groupweapon[WEAPONS_MAX_LENGTH];
do
{
KvGetSectionName(kvWeaponGroups, groupweapon, sizeof(groupweapon));
// If weapon is invalid, then skip
if (!WeaponsIsValidWeapon(groupweapon))
{
continue;
}
if (WeaponRestrictIsRestricted(groupweapon))
{
new weaponindex = WeaponRestrictGetIndex(groupweapon);
@ -219,31 +271,36 @@ WpnRestrictQuery:WeaponRestrictUnrestrict(const String:weapon[])
return Successful_Group;
}
if (WeaponRestrictIsRestricted(weapon))
if (!WeaponsIsValidWeapon(weapon))
{
new weaponindex = WeaponRestrictGetIndex(weapon);
if (weaponindex > -1)
{
RemoveFromArray(gRestrictedWeapons, weaponindex);
return Successful_Weapon;
}
return Invalid;
}
return Invalid;
if (!WeaponRestrictIsRestricted(weapon))
{
return Failed_Weapon;
}
new weaponindex = WeaponRestrictGetIndex(weapon);
if (weaponindex > -1)
{
RemoveFromArray(gRestrictedWeapons, weaponindex);
}
return Successful_Weapon;
}
/**
* Checks if a weapon is restricted.
*
* @param weapon The weapon name.
*/
*/
bool:WeaponRestrictIsRestricted(const String:weapon[])
{
new size = GetArraySize(gRestrictedWeapons);
for (new x = 0; x < size; x++)
{
decl String:restrictedweapon[32];
decl String:restrictedweapon[WEAPONS_MAX_LENGTH];
GetArrayString(gRestrictedWeapons, x, restrictedweapon, sizeof(restrictedweapon));
if (StrEqual(weapon, restrictedweapon, false))
@ -259,13 +316,13 @@ bool:WeaponRestrictIsRestricted(const String:weapon[])
* Returns the array index of the restricted weapon.
*
* @param weapon The weapon name.
*/
*/
WeaponRestrictGetIndex(const String:weapon[])
{
new size = GetArraySize(gRestrictedWeapons);
for (new x = 0; x < size; x++)
{
decl String:restrictedweapon[32];
decl String:restrictedweapon[WEAPONS_MAX_LENGTH];
GetArrayString(gRestrictedWeapons, x, restrictedweapon, sizeof(restrictedweapon));
if (StrEqual(weapon, restrictedweapon, false))
@ -282,7 +339,7 @@ WeaponRestrictGetIndex(const String:weapon[])
*
* @param groupname Name of the group to check.
* @return True if it's a group, false if not.
*/
*/
bool:WeaponRestrictIsCustomGroup(const String:groupname[])
{
// Reset the traversal stack
@ -300,7 +357,7 @@ bool:WeaponRestrictIsCustomGroup(const String:groupname[])
* @param weaponlist Variable to store weapon list string in.
* @param maxlen Maximum length of the weapon list, the rest is truncated.
* @param separator Separator character between weapon names.
*/
*/
WeaponRestrictGetWeaponList(const String:groupname[], String:weaponlist[], maxlen, const String:separator[])
{
KvRewind(kvWeaponGroups);
@ -308,7 +365,7 @@ WeaponRestrictGetWeaponList(const String:groupname[], String:weaponlist[], maxle
if (KvGotoFirstSubKey(kvWeaponGroups))
{
decl String:groupweapon[32];
decl String:groupweapon[WEAPONS_MAX_LENGTH];
strcopy(weaponlist, maxlen, "");
@ -316,6 +373,12 @@ WeaponRestrictGetWeaponList(const String:groupname[], String:weaponlist[], maxle
{
KvGetSectionName(kvWeaponGroups, groupweapon, sizeof(groupweapon));
// If weapon is invalid, then skip
if (!WeaponsIsValidWeapon(groupweapon))
{
continue;
}
if (!weaponlist[0])
{
strcopy(weaponlist, maxlen, groupweapon);
@ -333,7 +396,7 @@ WeaponRestrictGetWeaponList(const String:groupname[], String:weaponlist[], maxle
* @param client The client index.
* @param weapon The weapon index.
* @return 0 to block weapon pickup, Hacks_Continue to allow.
*/
*/
public WeaponRestrictCanUse(client, weapon, dummy1, dummy2, dummy3, dummy4)
{
// If plugin is disabled then stop
@ -343,7 +406,7 @@ public WeaponRestrictCanUse(client, weapon, dummy1, dummy2, dummy3, dummy4)
return Hacks_Continue;
}
new String:weaponname[32];
new String:weaponname[WEAPONS_MAX_LENGTH];
GetEdictClassname(weapon, weaponname, sizeof(weaponname));
// Strip "weapon_" from entity name