PHP Code:
public Handle:GetWeaponSlotMap() {
new Handle:hKvItems = CreateKeyValues("");
if (!FileToKeyValues(hKvItems, PATH_ITEMS_GAME)) {
SetFailState("Could not parse items_game.txt. Something is seriously wrong!");
return INVALID_HANDLE;
}
new Handle:hSlotMap = CreateTrie();
new Handle:hTriePrefab = CreateTrie();
KvRewind(hKvItems);
if(KvJumpToKey(hKvItems, "prefabs")) {
// There is a prefabs section
KvGotoFirstSubKey(hKvItems, false);
do {
decl String:sPFName[64];
KvGetSectionName(hKvItems, sPFName, sizeof(sPFName));
new String:sItemSlot[16];
KvGetString(hKvItems, "item_slot", sItemSlot, sizeof(sItemSlot));
SetTrieString(hTriePrefab, sPFName, sItemSlot);
} while (KvGotoNextKey(hKvItems, false));
}
new String:sDefaultSlot[16] = "melee";
KvRewind(hKvItems);
KvJumpToKey(hKvItems, "items");
if(KvJumpToKey(hKvItems, "default")) {
KvGetString(hKvItems, "item_slot", sDefaultSlot, sizeof(sDefaultSlot));
}
KvRewind(hKvItems);
KvJumpToKey(hKvItems, "items");
KvGotoFirstSubKey(hKvItems, false);
new String:sIndex[8];
do {
KvGetSectionName(hKvItems, sIndex, sizeof(sIndex));
//Skip item with id 'default'
if(StrEqual(sIndex, "default"))continue;
// Initialize with the default slot
new String:sItemSlot[16];
strcopy(sItemSlot, sizeof(sItemSlot), sDefaultSlot);
// Overwrite if a prefab is set
new String:sPrefab[64];
KvGetString(hKvItems, "prefab", sPrefab, sizeof(sPrefab));
GetTrieString(hTriePrefab, sPrefab, sItemSlot, sizeof(sItemSlot));
// Overwrite if set directly
KvGetString(hKvItems, "item_slot", sItemSlot, sizeof(sItemSlot), sItemSlot);
if(StrEqual(sItemSlot, "primary"))SetTrieValue(hSlotMap, sIndex, 0);
if(StrEqual(sItemSlot, "secondary"))SetTrieValue(hSlotMap, sIndex, 1);
if(StrEqual(sItemSlot, "melee"))SetTrieValue(hSlotMap, sIndex, 2);
if(StrEqual(sItemSlot, "pda"))SetTrieValue(hSlotMap, sIndex, 3);
if(StrEqual(sItemSlot, "pda2"))SetTrieValue(hSlotMap, sIndex, 4);
} while (KvGotoNextKey(hKvItems, false));
CloseHandle(hTriePrefab);
CloseHandle(hKvItems);
return hSlotMap;
}
This creates a trie with item definition index as key and weapon slot as value, using data from the current items_game.txt. Taken from the tNoUnlocksPls plugin.
__________________