diff --git a/zone/QGlobals.cpp b/zone/QGlobals.cpp index 54a081cc5..73faa8b2c 100644 --- a/zone/QGlobals.cpp +++ b/zone/QGlobals.cpp @@ -1,6 +1,8 @@ #include "../common/debug.h" #include "../common/StringUtil.h" #include "QGlobals.h" +#include "masterentity.h" +#include "zone.h" #include "zonedb.h" void QGlobalCache::AddGlobal(uint32 id, QGlobal global) @@ -16,7 +18,9 @@ void QGlobalCache::RemoveGlobal(std::string name, uint32 npcID, uint32 charID, u { if(name.compare((*iter).name) == 0) { - if((npcID == (*iter).npc_id || (*iter).npc_id == 0) && (charID == (*iter).char_id || (*iter).char_id == 0) && (zoneID == (*iter).zone_id || (*iter).zone_id == 0)) + if((npcID == (*iter).npc_id || (*iter).npc_id == 0) && + (charID == (*iter).char_id || (*iter).char_id == 0) && + (zoneID == (*iter).zone_id || (*iter).zone_id == 0)) { qGlobalBucket.erase(iter); return; @@ -33,7 +37,8 @@ void QGlobalCache::Combine(std::list &cacheA, std::list cacheB { QGlobal cur = (*iter); - if((cur.npc_id == npcID || cur.npc_id == 0) && (cur.char_id == charID || cur.char_id == 0) && (cur.zone_id == zoneID || cur.zone_id == 0)) + if((cur.npc_id == npcID || cur.npc_id == 0) && (cur.char_id == charID || cur.char_id == 0) && + (cur.zone_id == zoneID || cur.zone_id == 0)) { if(Timer::GetTimeSeconds() < cur.expdate) { @@ -44,6 +49,76 @@ void QGlobalCache::Combine(std::list &cacheA, std::list cacheB } } +void QGlobalCache::GetQGlobals(std::list &globals, NPC *n, Client *c, Zone *z) { + globals.clear(); + + QGlobalCache *npc_c = nullptr; + QGlobalCache *char_c = nullptr; + QGlobalCache *zone_c = nullptr; + uint32 npc_id = 0; + uint32 char_id = 0; + uint32 zone_id = 0; + + if(n) { + npc_id = n->GetNPCTypeID(); + npc_c = n->GetQGlobals(); + } + + if(c) { + char_id = c->CharacterID(); + char_c = c->GetQGlobals(); + } + + if(z) { + zone_id = z->GetZoneID(); + zone_c = z->GetQGlobals(); + } + + if(!npc_c && n) { + npc_c = n->CreateQGlobals(); + npc_c->LoadByNPCID(npc_id); + } + + if(!char_c && c) { + char_c = c->CreateQGlobals(); + char_c->LoadByCharID(char_id); + } + + if(!zone_c && z) { + zone_c = z->CreateQGlobals(); + zone_c->LoadByZoneID(zone_id); + zone_c->LoadByGlobalContext(); + } + + if(npc_c) { + QGlobalCache::Combine(globals, npc_c->GetBucket(), npc_id, char_id, zone_id); + } + + if(char_c) { + QGlobalCache::Combine(globals, char_c->GetBucket(), npc_id, char_id, zone_id); + } + + if(zone_c) { + QGlobalCache::Combine(globals, zone_c->GetBucket(), npc_id, char_id, zone_id); + } +} + +bool QGlobalCache::GetQGlobal(QGlobal &g, std::string name, NPC *n, Client *c, Zone *z) { + std::list globals; + QGlobalCache::GetQGlobals(globals, n, c, z); + + auto iter = globals.begin(); + while(iter != globals.end()) { + if(iter->name.compare(name) == 0) { + g = (*iter); + return true; + } + ++iter; + } + + return false; +} + void QGlobalCache::PurgeExpiredGlobals() { if(!qGlobalBucket.size()) diff --git a/zone/QGlobals.h b/zone/QGlobals.h index 577fd3c11..73a795186 100644 --- a/zone/QGlobals.h +++ b/zone/QGlobals.h @@ -6,6 +6,10 @@ #include #include "../common/timer.h" +class NPC; +class Client; +class Zone; + struct QGlobal { QGlobal() { } @@ -29,6 +33,8 @@ public: //assumes cacheA is already a valid or empty list and doesn't check for valid items. static void Combine(std::list &cacheA, std::list cacheB, uint32 npcID, uint32 charID, uint32 zoneID); + static void GetQGlobals(std::list &globals, NPC *n, Client *c, Zone *z); + static bool GetQGlobal(QGlobal &g, std::string name, NPC *n, Client *c, Zone *z); void PurgeExpiredGlobals(); void LoadByNPCID(uint32 npcID); //npc