Made weapon management menu toggle restrictions (easier), and weapon names are now case-insensitive

This commit is contained in:
Greyscale
2009-04-13 20:33:13 +02:00
parent d074b68edf
commit 842e4030b9
11 changed files with 440 additions and 246 deletions

View File

@ -6,21 +6,11 @@
* ====================
*/
/**
* Handle to store trie weapon data
*/
new Handle:trieWeapons = INVALID_HANDLE;
/**
* Maximum length of a weapon name string
*/
#define WEAPONS_MAX_LENGTH 32
/**
* @section Config array indexes.
*/
#define WEAPON_RESTRICT 0
#define WEAPON_MENU 1
/**
* @endsection
*/
@ -34,20 +24,17 @@ new Handle:kvWeapons = INVALID_HANDLE;
#include "zr/weapons/markethandler"
#include "zr/weapons/menu_weapons"
WeaponsInit()
{
// Forward event to sub-module
RestrictInit();
}
/**
* Loads weapon data from file.
*/
WeaponsOnMapStart()
{
// Destroy trie data if populated
if (trieWeapons != INVALID_HANDLE)
{
CloseHandle(trieWeapons);
}
// Create trie to store weapon data
trieWeapons = CreateTrie();
// Load weapon data
if (kvWeapons != INVALID_HANDLE)
{
@ -65,6 +52,15 @@ WeaponsOnMapStart()
SetFailState("\"%s\" missing from server", path);
}
// Validate weapons config
WeaponsValidateWeaponsConfig();
// Forward event to sub-module
RestrictOnMapStart();
}
WeaponsValidateWeaponsConfig()
{
KvRewind(kvWeapons);
if (KvGotoFirstSubKey(kvWeapons))
{
@ -78,17 +74,22 @@ WeaponsOnMapStart()
KvGetString(kvWeapons, "restrict", restrict, sizeof(restrict), "no");
KvGetString(kvWeapons, "menu", menu, sizeof(menu), "yes");
new bool:config[2];
config[WEAPON_RESTRICT] = ConfigOptionToBool(restrict);
config[WEAPON_MENU] = ConfigOptionToBool(menu);
SetTrieArray(trieWeapons, weapon, config, sizeof(config), false);
// VALIDATE
} while (KvGotoNextKey(kvWeapons));
}
// Weapon restrict
WeaponRestrictOnMapStart();
}
WeaponsClientInit(client)
{
// Forward event to sub-module
RestrictClientInit(client);
}
WeaponsOnClientDisconnect(client)
{
// Forward event to sub-module
RestrictOnClientDisconnect(client);
}
/**
@ -119,34 +120,6 @@ WeaponsCreateWeaponArray(&Handle:arrayWeapons, maxlen = WEAPONS_MAX_LENGTH)
return count;
}
/**
* Creates an array of all listed weapon groups in weapongroups.txt.
* @param arrayWeaponGroups The handle of the array, don't forget to call CloseHandle
* on it when finished!
* @return The size of the array.
*/
WeaponsCreateWeaponGroupArray(&Handle:arrayWeaponGroups, maxlen = WEAPONS_MAX_LENGTH)
{
arrayWeaponGroups = CreateArray(maxlen);
new count = 0;
KvRewind(kvWeaponGroups);
if (KvGotoFirstSubKey(kvWeaponGroups))
{
decl String:weapongroup[maxlen];
do
{
KvGetSectionName(kvWeaponGroups, weapongroup, maxlen);
PushArrayString(arrayWeaponGroups, weapongroup);
count++;
} while (KvGotoNextKey(kvWeaponGroups));
}
return count;
}
/**
* Checks if a weapon is valid (aka listed in weapons.txt)
* @param weapon The weapon name.
@ -154,9 +127,49 @@ WeaponsCreateWeaponGroupArray(&Handle:arrayWeaponGroups, maxlen = WEAPONS_MAX_LE
*/
bool:WeaponsIsValidWeapon(const String:weapon[])
{
new bool:config[2];
KvRewind(kvWeapons);
if (KvGotoFirstSubKey(kvWeapons))
{
decl String:validweapon[WEAPONS_MAX_LENGTH];
do
{
KvGetSectionName(kvWeapons, validweapon, sizeof(validweapon));
if (StrEqual(validweapon, weapon, false))
{
return true;
}
} while (KvGotoNextKey(kvWeapons));
}
return GetTrieArray(trieWeapons, weapon, config, sizeof(config));
return false;
}
/**
* Looks up a weapon in weapons.txt and returns exact display name
* @param weapon The weapon name.
* @param display Returns with the display name, is not changed if weapon is invalid.
*/
WeaponGetDisplayName(const String:weapon[], String:display[])
{
KvRewind(kvWeapons);
if (KvGotoFirstSubKey(kvWeapons))
{
decl String:validweapon[WEAPONS_MAX_LENGTH];
do
{
KvGetSectionName(kvWeapons, validweapon, sizeof(validweapon));
if (StrEqual(validweapon, weapon, false))
{
strcopy(display, WEAPONS_MAX_LENGTH, validweapon);
}
} while (KvGotoNextKey(kvWeapons));
}
}
/**
@ -164,12 +177,28 @@ bool:WeaponsIsValidWeapon(const String:weapon[])
* @param weapon The weapon name.
* @return Returns true if restricted, false it not.
*/
bool:WeaponsIsWeaponRestrict(const String:weapon[])
bool:WeaponsIsRestrict(const String:weapon[])
{
new bool:config[2];
GetTrieArray(trieWeapons, weapon, config, sizeof(config));
KvRewind(kvWeapons);
if (KvGotoFirstSubKey(kvWeapons))
{
decl String:validweapon[WEAPONS_MAX_LENGTH];
decl String:restrict[8];
do
{
KvGetSectionName(kvWeapons, validweapon, sizeof(validweapon));
if (StrEqual(validweapon, weapon, false))
{
KvGetString(kvWeapons, validweapon, restrict, sizeof(restrict), "no");
return ConfigOptionToBool(restrict);
}
} while (KvGotoNextKey(kvWeapons));
}
return config[WEAPON_RESTRICT];
return false;
}
/**
@ -179,8 +208,24 @@ bool:WeaponsIsWeaponRestrict(const String:weapon[])
*/
bool:WeaponsIsWeaponMenu(const String:weapon[])
{
new bool:config[2];
GetTrieArray(trieWeapons, weapon, config, sizeof(config));
KvRewind(kvWeapons);
if (KvGotoFirstSubKey(kvWeapons))
{
decl String:validweapon[WEAPONS_MAX_LENGTH];
decl String:menu[8];
do
{
KvGetSectionName(kvWeapons, validweapon, sizeof(validweapon));
if (StrEqual(validweapon, weapon, false))
{
KvGetString(kvWeapons, validweapon, menu, sizeof(menu), "yes");
return ConfigOptionToBool(menu);
}
} while (KvGotoNextKey(kvWeapons));
}
return config[WEAPON_MENU];
return false;
}