Senior Member
|
12-21-2015
, 13:18
Re: Need a good array for this...
|
#12
|
Quote:
Originally Posted by Dr. Greg House
Well the kv tree is in memory, but that doesn't mean that lookup is particularly fast or equal compared to other storage techniques.
You still haven't said what you want to do, so everything from building a menu handle and caching that, using an adt-array, or using a tree might be better or not worth the effort.
I don't want this thread to be another example for "parse through huge kv-structures in game".
|
Let me be more specific:
PHP Code:
#pragma semicolon 1
#define DEBUG
#define PLUGIN_AUTHOR "" #define PLUGIN_VERSION "0.00"
#define Kv_Location "configs/loc/location.cfg"
#include <sourcemod> #include <sdktools> #include <cstrike>
bool clwhitelisted[MAXPLAYERS + 1]; KeyValues kv;
Handle g_adtArray;
public Plugin myinfo = { name = "", author = PLUGIN_AUTHOR, description = "", version = PLUGIN_VERSION, url = "" };
public void OnPluginStart() { RegConsoleCmd("sm_tpmenu", buildUI, "builds UI"); CreateTimer(2.0, loopremove, _, TIMER_REPEAT); g_adtArray = CreateArray(32); }
public void OnMapStart() { CacheVl(); }
public bool CacheVl() { kv = new KeyValues("locations");
char buffer[256]; char dirLoc[PLATFORM_MAX_PATH]; BuildPath(Path_SM, dirLoc, sizeof(dirLoc), Kv_Location); FileToKeyValues(kv, dirLoc); if (!KvGotoFirstSubKey(kv)) { CloseHandle(kv); return false; // cfg is broken... } do { KvGetSectionName(kv, buffer, sizeof(buffer)); PushArrayString(g_adtArray, buffer); // get section name, store into a dynamic array } while (KvGotoNextKey(kv)); return true; }
public int handlerUI(Menu menu, MenuAction action, int parameterCL, int parameterARGS) { /* If an option was selected, tell the client about the item. */ if (action == MenuAction_Select) { char selectedName[32]; menu.GetItem(parameterARGS, selectedName, sizeof(selectedName)); retrieveCachedData(parameterCL, selectedName); /* Know which section we talkin' bout = selectedName = Section*/ /* Compare data with the keyValues */ /* Retrieve Info */ /* Teleport */ } /* If the menu was cancelled, print a message to the server about it. */ else if (action == MenuAction_Cancel) { PrintToServer("Client %d's menu was cancelled. Reason: %d", parameterCL, parameterARGS); } /* If the menu has ended, destroy it */ else if (action == MenuAction_End) { delete menu; } }
public Action buildUI(int cl, int args) { char buffer[32]; Menu menu = new Menu(handlerUI); menu.SetTitle("Teleport to:"); for (int i = 0; i < GetArraySize(g_adtArray); i++) { GetArrayString(g_adtArray, i, buffer, sizeof(buffer)); menu.AddItem(buffer, "location"); } menu.ExitButton = true; menu.Display(cl, MENU_TIME_FOREVER); return Plugin_Handled; }
public bool retrieveCachedData(int cl, char[] section) { float temploc[3]; bool whitelist; if (!kv.JumpToKey(section)) { return false; // no setted section ??? } int clteam = GetClientTeam(cl); switch (clteam) { case CS_TEAM_CT: { kv.GetFloat("ctlocx", temploc[0]); kv.GetFloat("ctlocy", temploc[1]); kv.GetFloat("ctlocz", temploc[2]); } case CS_TEAM_T: { kv.GetFloat("tlocx", temploc[0]); kv.GetFloat("tlocy", temploc[1]); kv.GetFloat("tlocz", temploc[2]); } } //whitelist = kv.GetNum("tlocy", temploc[1]); teleportcl(cl, temploc); delete kv; return true; }
public teleportcl(int cl, float pos[3]) { TeleportEntity(cl, pos, NULL_VECTOR, NULL_VECTOR); }
public Action loopremove(Handle timer) { int ent; if (GameRules_GetProp("m_bWarmupPeriod") == 0) // it's not warm up { for (int i = 1; i < MaxClients; i++) { if (IsClientInGame(i) && IsPlayerAlive(i)) { for (int wep; wep < 4; wep++) { if ((ent = GetPlayerWeaponSlot(i, wep)) != -1) { SetEntProp(ent, Prop_Send, "m_iPrimaryReserveAmmoCount", 0); SetEntProp(ent, Prop_Send, "m_iClip1", 0); } } } } } }
My KeyValue:
PHP Code:
"Locations" { "Arena" { "ammo" "1" "ctlocx" "535" "ctlocy" "5290" "ctlocz" "1924" "tlocx" "4467" "tlocy" "45361" "tlocz" "375" } "TradeRooms" { "ammo" "0" "ctlocx" "535" "ctlocy" "5290" "ctlocz" "1924" "tlocx" "4467" "tlocy" "45361" "tlocz" "375" } "Surf" { "ammo" "0" "ctlocx" "535" "ctlocy" "5290" "ctlocz" "1924" "tlocx" "4467" "tlocy" "45361" "tlocz" "375" } "Bhop" { "ammo" "0" "ctlocx" "535" "ctlocy" "5290" "ctlocz" "1924" "tlocx" "4467" "tlocy" "45361" "tlocz" "375" } }
This is my code & KeyValue, I want to access these values in such way that I won't stress my server, what would be the better way, keeping my KeyValue Handle open and search sections for a value each time a user inputs data, or cache all the data in an adt_array or an adt_trie?
__________________
Perhaps my lack of faith was my undoing,
Last edited by bally; 12-21-2015 at 14:07.
|
|