From 7d8dc0eeefe4e7efad1a562c1f0991079d82189c Mon Sep 17 00:00:00 2001 From: Neo2003 Date: Sat, 6 Dec 2008 00:31:42 +0100 Subject: [PATCH] Implemented gameobjects and creatures grouping (pools of them) Groups (called pools) can be also member of any game event Signed-off-by: Neo2003 Signed-off-by: freghar --- sql/updates/7299_01_mangos_pools.sql | 25 ++ src/game/Creature.cpp | 7 +- src/game/GameEvent.cpp | 73 ++++ src/game/GameEvent.h | 3 + src/game/GameObject.cpp | 15 +- src/game/Level2.cpp | 37 +- src/game/Makefile.am | 2 + src/game/ObjectMgr.cpp | 58 +-- src/game/PoolHandler.cpp | 563 +++++++++++++++++++++++++++ src/game/PoolHandler.h | 106 +++++ src/game/World.cpp | 7 + win/VC71/game.vcproj | 6 + win/VC80/game.vcproj | 8 + win/VC90/game.vcproj | 8 + 14 files changed, 879 insertions(+), 39 deletions(-) create mode 100644 sql/updates/7299_01_mangos_pools.sql create mode 100644 src/game/PoolHandler.cpp create mode 100644 src/game/PoolHandler.h diff --git a/sql/updates/7299_01_mangos_pools.sql b/sql/updates/7299_01_mangos_pools.sql new file mode 100644 index 000000000..0b212fd73 --- /dev/null +++ b/sql/updates/7299_01_mangos_pools.sql @@ -0,0 +1,25 @@ +CREATE TABLE `pool_creature` ( + `guid` int(10) unsigned NOT NULL default '0', + `pool_entry` mediumint(8) unsigned NOT NULL default '0', + `chance` float unsigned NOT NULL default '0', + PRIMARY KEY (`pool_entry`,`guid`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; + +CREATE TABLE `pool_gameobject` ( + `guid` int(10) unsigned NOT NULL default '0', + `pool_entry` mediumint(8) unsigned NOT NULL default '0', + `chance` float unsigned NOT NULL default '0', + PRIMARY KEY (`guid`,`pool_entry`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; + +CREATE TABLE `pool_template` ( + `entry` mediumint(8) unsigned NOT NULL default '0' COMMENT 'Pool entry', + `max_limit` int(10) unsigned NOT NULL default '0' COMMENT 'Max number of objects (0) is no limit', + PRIMARY KEY (`entry`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; + +CREATE TABLE `game_event_pool` ( + `pool_entry` mediumint(8) unsigned NOT NULL default '0' COMMENT 'Id of the pool', + `event` smallint(6) NOT NULL default '0' COMMENT 'Put negatives values to remove during event', + PRIMARY KEY (`pool_entry`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; diff --git a/src/game/Creature.cpp b/src/game/Creature.cpp index c84a39761..808ff9ba5 100644 --- a/src/game/Creature.cpp +++ b/src/game/Creature.cpp @@ -27,6 +27,7 @@ #include "QuestDef.h" #include "GossipDef.h" #include "Player.h" +#include "PoolHandler.h" #include "Opcodes.h" #include "Log.h" #include "LootMgr.h" @@ -349,7 +350,11 @@ void Creature::Update(uint32 diff) //Call AI respawn virtual function i_AI->JustRespawned(); - GetMap()->Add(this); + uint16 poolid = poolhandler.IsPartOfAPool(GetGUIDLow(), GetTypeId()); + if (poolid) + poolhandler.UpdatePool(poolid, GetGUIDLow(), GetTypeId()); + else + GetMap()->Add(this); } break; } diff --git a/src/game/GameEvent.cpp b/src/game/GameEvent.cpp index 4b910f8df..464083499 100644 --- a/src/game/GameEvent.cpp +++ b/src/game/GameEvent.cpp @@ -19,6 +19,7 @@ #include "GameEvent.h" #include "World.h" #include "ObjectMgr.h" +#include "PoolHandler.h" #include "ProgressBar.h" #include "Language.h" #include "Log.h" @@ -351,6 +352,57 @@ void GameEvent::LoadFromDB() sLog.outString(); sLog.outString( ">> Loaded %u quests additions in game events", count ); } + + mGameEventPoolIds.resize(mGameEvent.size()*2-1); + // 1 2 + result = WorldDatabase.Query("SELECT pool_template.entry, game_event_pool.event " + "FROM pool_template JOIN game_event_pool ON pool_template.entry = game_event_pool.pool_entry"); + + count = 0; + if( !result ) + { + barGoLink bar2(1); + bar2.step(); + + sLog.outString(); + sLog.outString(">> Loaded %u pools in game events", count ); + } + else + { + + barGoLink bar2( result->GetRowCount() ); + do + { + Field *fields = result->Fetch(); + + bar2.step(); + + uint32 entry = fields[0].GetUInt16(); + int16 event_id = fields[1].GetInt16(); + + int32 internal_event_id = mGameEvent.size() + event_id - 1; + + if(internal_event_id < 0 || internal_event_id >= mGameEventPoolIds.size()) + { + sLog.outErrorDb("`game_event_pool` game event id (%i) is out of range compared to max event id in `game_event`",event_id); + continue; + } + + if (!poolhandler.CheckPool(entry)) + { + sLog.outErrorDb("Pool Id (%u) has all creatures or gameobjects with explicit chance sum <>100 and no equal chance defined. The pool system cannot pick one to spawn.", entry); + continue; + } + + ++count; + IdList& poollist = mGameEventPoolIds[internal_event_id]; + poollist.push_back(entry); + + } while( result->NextRow() ); + sLog.outString(); + sLog.outString( ">> Loaded %u pools in game events", count ); + delete result; + } } uint32 GameEvent::Initialize() // return the next event delay in ms @@ -505,6 +557,17 @@ void GameEvent::GameEventSpawn(int16 event_id) } } } + + if(internal_event_id < 0 || internal_event_id >= mGameEventPoolIds.size()) + { + sLog.outError("GameEvent::GameEventSpawn attempt access to out of range mGameEventPoolIds element %i (size: %u)",internal_event_id,mGameEventPoolIds.size()); + return; + } + + for (IdList::iterator itr = mGameEventPoolIds[internal_event_id].begin();itr != mGameEventPoolIds[internal_event_id].end();++itr) + { + poolhandler.SpawnPool(*itr); + } } void GameEvent::GameEventUnspawn(int16 event_id) @@ -549,6 +612,16 @@ void GameEvent::GameEventUnspawn(int16 event_id) pGameobject->AddObjectToRemoveList(); } } + if(internal_event_id < 0 || internal_event_id >= mGameEventPoolIds.size()) + { + sLog.outError("GameEvent::GameEventUnspawn attempt access to out of range mGameEventPoolIds element %i (size: %u)",internal_event_id,mGameEventPoolIds.size()); + return; + } + + for (IdList::iterator itr = mGameEventPoolIds[internal_event_id].begin();itr != mGameEventPoolIds[internal_event_id].end();++itr) + { + poolhandler.DespawnPool(*itr); + } } void GameEvent::ChangeEquipOrModel(int16 event_id, bool activate) diff --git a/src/game/GameEvent.h b/src/game/GameEvent.h index a60fb59c5..e1752a952 100644 --- a/src/game/GameEvent.h +++ b/src/game/GameEvent.h @@ -73,7 +73,9 @@ class GameEvent void UpdateEventQuests(uint16 event_id, bool Activate); protected: typedef std::list GuidList; + typedef std::list IdList; typedef std::vector GameEventGuidMap; + typedef std::vector GameEventIdMap; typedef std::pair ModelEquipPair; typedef std::list ModelEquipList; typedef std::vector GameEventModelEquipMap; @@ -84,6 +86,7 @@ class GameEvent GameEventModelEquipMap mGameEventModelEquip; GameEventGuidMap mGameEventCreatureGuids; GameEventGuidMap mGameEventGameobjectGuids; + GameEventIdMap mGameEventPoolIds; GameEventDataMap mGameEvent; ActiveEvents m_ActiveEvents; bool isSystemInit; diff --git a/src/game/GameObject.cpp b/src/game/GameObject.cpp index b852e2dcf..5f7cf6c4b 100644 --- a/src/game/GameObject.cpp +++ b/src/game/GameObject.cpp @@ -20,6 +20,7 @@ #include "QuestDef.h" #include "GameObject.h" #include "ObjectMgr.h" +#include "PoolHandler.h" #include "SpellMgr.h" #include "Spell.h" #include "UpdateMask.h" @@ -62,7 +63,7 @@ GameObject::~GameObject() { if(m_uint32Values) // field array can be not exist if GameOBject not loaded { - // crash possible at access to deleted GO in Unit::m_gameobj + // Possible crash at access to deleted GO in Unit::m_gameobj uint64 owner_guid = GetOwnerGUID(); if(owner_guid) { @@ -275,7 +276,11 @@ void GameObject::Update(uint32 /*p_time*/) return; } // respawn timer - GetMap()->Add(this); + uint16 poolid = poolhandler.IsPartOfAPool(GetGUIDLow(), TYPEID_GAMEOBJECT); + if (poolid) + poolhandler.UpdatePool(poolid, GetGUIDLow(), TYPEID_GAMEOBJECT); + else + GetMap()->Add(this); break; } } @@ -477,7 +482,11 @@ void GameObject::Delete() SetGoState(1); SetUInt32Value(GAMEOBJECT_FLAGS, GetGOInfo()->flags); - AddObjectToRemoveList(); + uint16 poolid = poolhandler.IsPartOfAPool(GetGUIDLow(), TYPEID_GAMEOBJECT); + if (poolid) + poolhandler.UpdatePool(poolid, GetGUIDLow(), TYPEID_GAMEOBJECT); + else + AddObjectToRemoveList(); } void GameObject::getFishLoot(Loot *fishloot, Player* loot_owner) diff --git a/src/game/Level2.cpp b/src/game/Level2.cpp index ebb9816df..3e623a5ac 100644 --- a/src/game/Level2.cpp +++ b/src/game/Level2.cpp @@ -33,6 +33,7 @@ #include "World.h" #include "GameEvent.h" #include "SpellMgr.h" +#include "PoolHandler.h" #include "AccountMgr.h" #include "GMTicketMgr.h" #include "WaypointManager.h" @@ -202,7 +203,7 @@ bool ChatHandler::HandleTargetObjectCommand(const char* args) result = WorldDatabase.PQuery("SELECT gameobject.guid, id, position_x, position_y, position_z, orientation, map, " "(POW(position_x - %f, 2) + POW(position_y - %f, 2) + POW(position_z - %f, 2)) AS order_ FROM gameobject " - "LEFT OUTER JOIN game_event_gameobject on gameobject.guid=game_event_gameobject.guid WHERE map = '%i' %s ORDER BY order_ ASC LIMIT 1", + "LEFT OUTER JOIN game_event_gameobject on gameobject.guid=game_event_gameobject.guid WHERE map = '%i' %s ORDER BY order_ ASC LIMIT 10", m_session->GetPlayer()->GetPositionX(), m_session->GetPlayer()->GetPositionY(), m_session->GetPlayer()->GetPositionZ(), m_session->GetPlayer()->GetMapId(),eventFilter.str().c_str()); } @@ -212,16 +213,34 @@ bool ChatHandler::HandleTargetObjectCommand(const char* args) return true; } - Field *fields = result->Fetch(); - uint32 lowguid = fields[0].GetUInt32(); - uint32 id = fields[1].GetUInt32(); - float x = fields[2].GetFloat(); - float y = fields[3].GetFloat(); - float z = fields[4].GetFloat(); - float o = fields[5].GetFloat(); - int mapid = fields[6].GetUInt16(); + bool found = false; + float x, y, z, o; + uint32 lowguid, id; + uint16 mapid, pool_id; + + do + { + Field *fields = result->Fetch(); + lowguid = fields[0].GetUInt32(); + id = fields[1].GetUInt32(); + x = fields[2].GetFloat(); + y = fields[3].GetFloat(); + z = fields[4].GetFloat(); + o = fields[5].GetFloat(); + mapid = fields[6].GetUInt16(); + pool_id = poolhandler.IsPartOfAPool(lowguid, TYPEID_GAMEOBJECT); + if (!pool_id || (pool_id && poolhandler.IsSpawnedObject(pool_id, lowguid, TYPEID_GAMEOBJECT))) + found = true; + } while( result->NextRow() && (!found) ); + delete result; + if (!found) + { + PSendSysMessage(LANG_GAMEOBJECT_NOT_EXIST,id); + return false; + } + GameObjectInfo const* goI = objmgr.GetGameObjectInfo(id); if (!goI) diff --git a/src/game/Makefile.am b/src/game/Makefile.am index 60b31d0d5..a162214ca 100644 --- a/src/game/Makefile.am +++ b/src/game/Makefile.am @@ -206,6 +206,8 @@ libmangosgame_a_SOURCES = \ PlayerDump.h \ PointMovementGenerator.cpp \ PointMovementGenerator.h \ + PoolHandler.cpp \ + PoolHandler.h \ QueryHandler.cpp \ QuestDef.cpp \ QuestDef.h \ diff --git a/src/game/ObjectMgr.cpp b/src/game/ObjectMgr.cpp index 8fa8c5401..c194d9f14 100644 --- a/src/game/ObjectMgr.cpp +++ b/src/game/ObjectMgr.cpp @@ -849,9 +849,10 @@ void ObjectMgr::LoadCreatures() QueryResult *result = WorldDatabase.Query("SELECT creature.guid, id, map, modelid," // 4 5 6 7 8 9 10 11 "equipment_id, position_x, position_y, position_z, orientation, spawntimesecs, spawndist, currentwaypoint," - // 12 13 14 15 16 17 18 - "curhealth, curmana, DeathState, MovementType, spawnMask, phaseMask, event " - "FROM creature LEFT OUTER JOIN game_event_creature ON creature.guid = game_event_creature.guid"); + // 12 13 14 15 16 17 18 19 + "curhealth, curmana, DeathState, MovementType, spawnMask, phaseMask, event, pool_entry " + "FROM creature LEFT OUTER JOIN game_event_creature ON creature.guid = game_event_creature.guid " + "LEFT OUTER JOIN pool_creature ON creature.guid = pool_creature.guid"); if(!result) { @@ -878,11 +879,19 @@ void ObjectMgr::LoadCreatures() Field *fields = result->Fetch(); bar.step(); - uint32 guid = fields[0].GetUInt32(); + uint32 guid = fields[ 0].GetUInt32(); + uint32 entry = fields[ 1].GetUInt32(); + + CreatureInfo const* cInfo = GetCreatureTemplate(entry); + if(!cInfo) + { + sLog.outErrorDb("Table `creature` has creature (GUID: %u) with non existing creature entry %u, skipped.", guid, entry); + continue; + } CreatureData& data = mCreatureDataMap[guid]; - data.id = fields[ 1].GetUInt32(); + data.id = entry; data.mapid = fields[ 2].GetUInt32(); data.displayid = fields[ 3].GetUInt32(); data.equipmentId = fields[ 4].GetUInt32(); @@ -900,13 +909,7 @@ void ObjectMgr::LoadCreatures() data.spawnMask = fields[16].GetUInt8(); data.phaseMask = fields[17].GetUInt16(); int16 gameEvent = fields[18].GetInt16(); - - CreatureInfo const* cInfo = GetCreatureTemplate(data.id); - if(!cInfo) - { - sLog.outErrorDb("Table `creature` have creature (GUID: %u) with not existed creature entry %u, skipped.",guid,data.id ); - continue; - } + int16 PoolId = fields[19].GetInt16(); if(heroicCreatures.find(data.id)!=heroicCreatures.end()) { @@ -991,7 +994,7 @@ void ObjectMgr::LoadCreatures() } } - if (gameEvent==0) // if not this is to be managed by GameEvent System + if (gameEvent==0 && PoolId==0) // if not this is to be managed by GameEvent System or Pool system AddCreatureToGrid(guid, &data); ++count; @@ -1041,9 +1044,10 @@ void ObjectMgr::LoadGameobjects() // 0 1 2 3 4 5 6 QueryResult *result = WorldDatabase.Query("SELECT gameobject.guid, id, map, position_x, position_y, position_z, orientation," - // 7 8 9 10 11 12 13 14 15 16 - "rotation0, rotation1, rotation2, rotation3, spawntimesecs, animprogress, state, spawnMask, phaseMask, event " - "FROM gameobject LEFT OUTER JOIN game_event_gameobject ON gameobject.guid = game_event_gameobject.guid"); + // 7 8 9 10 11 12 13 14 15 16 17 + "rotation0, rotation1, rotation2, rotation3, spawntimesecs, animprogress, state, spawnMask, phaseMask, event, pool_entry " + "FROM gameobject LEFT OUTER JOIN game_event_gameobject ON gameobject.guid = game_event_gameobject.guid " + "LEFT OUTER JOIN pool_gameobject ON gameobject.guid = pool_gameobject.guid"); if(!result) { @@ -1063,11 +1067,19 @@ void ObjectMgr::LoadGameobjects() Field *fields = result->Fetch(); bar.step(); - uint32 guid = fields[0].GetUInt32(); + uint32 guid = fields[ 0].GetUInt32(); + uint32 entry = fields[ 1].GetUInt32(); + + GameObjectInfo const* gInfo = GetGameObjectInfo(entry); + if(!gInfo) + { + sLog.outErrorDb("Table `gameobject` has gameobject (GUID: %u) with non existing gameobject entry %u, skipped.", guid, entry); + continue; + } GameObjectData& data = mGameObjectDataMap[guid]; - data.id = fields[ 1].GetUInt32(); + data.id = entry; data.mapid = fields[ 2].GetUInt32(); data.posX = fields[ 3].GetFloat(); data.posY = fields[ 4].GetFloat(); @@ -1083,13 +1095,7 @@ void ObjectMgr::LoadGameobjects() data.spawnMask = fields[14].GetUInt8(); data.phaseMask = fields[15].GetUInt16(); int16 gameEvent = fields[16].GetInt16(); - - GameObjectInfo const* gInfo = GetGameObjectInfo(data.id); - if(!gInfo) - { - sLog.outErrorDb("Table `gameobject` have gameobject (GUID: %u) with not existed gameobject entry %u, skipped.",guid,data.id ); - continue; - } + int16 PoolId = fields[17].GetInt16(); if(data.phaseMask==0) { @@ -1097,7 +1103,7 @@ void ObjectMgr::LoadGameobjects() data.phaseMask = 1; } - if (gameEvent==0) // if not this is to be managed by GameEvent System + if (gameEvent==0 && PoolId==0) // if not this is to be managed by GameEvent System or Pool system AddGameobjectToGrid(guid, &data); ++count; diff --git a/src/game/PoolHandler.cpp b/src/game/PoolHandler.cpp new file mode 100644 index 000000000..0af93f06b --- /dev/null +++ b/src/game/PoolHandler.cpp @@ -0,0 +1,563 @@ +/* + * Copyright (C) 2005-2008 MaNGOS + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "PoolHandler.h" +#include "World.h" +#include "ObjectMgr.h" +#include "ProgressBar.h" +#include "Language.h" +#include "Log.h" +#include "MapManager.h" +#include "Policies/SingletonImp.h" + +INSTANTIATE_SINGLETON_1(PoolHandler); + +PoolHandler::PoolHandler() +{ + isSystemInit = false; +} + +void PoolHandler::LoadFromDB() +{ + QueryResult *result = WorldDatabase.Query("SELECT MAX(entry) FROM pool_template"); + if (!result) + { + sLog.outString(">> Table pool_template is empty."); + sLog.outString(); + return; + } + else + { + Field *fields = result->Fetch(); + max_pool_id = fields[0].GetUInt16(); + delete result; + } + + mPoolTemplate.resize(max_pool_id + 1); + + result = WorldDatabase.Query("SELECT entry,max_limit FROM pool_template"); + if (!result) + { + mPoolTemplate.clear(); + sLog.outString(">> Table pool_template is empty:"); + sLog.outString(); + return; + } + + uint32 count = 0; + + barGoLink bar(result->GetRowCount()); + do + { + ++count; + Field *fields = result->Fetch(); + + bar.step(); + + uint16 pool_id = fields[0].GetUInt16(); + + PoolTemplateData& pPoolTemplate = mPoolTemplate[pool_id]; + pPoolTemplate.MaxLimit = fields[1].GetUInt32(); + + } while (result->NextRow()); + + sLog.outString(); + sLog.outString( ">> Loaded %u objects pools", count ); + delete result; + + // Creatures + + mPoolCreatureGroups.resize(max_pool_id + 1); + mCreatureSearchMap.clear(); + // 1 2 3 + result = WorldDatabase.Query("SELECT guid, pool_entry, chance FROM pool_creature"); + + count = 0; + if (!result) + { + barGoLink bar2(1); + bar2.step(); + + sLog.outString(); + sLog.outString(">> Loaded %u creatures in pools", count ); + } + else + { + + barGoLink bar2(result->GetRowCount()); + do + { + Field *fields = result->Fetch(); + + bar2.step(); + + uint32 guid = fields[0].GetUInt32(); + uint16 pool_id = fields[1].GetUInt16(); + float chance = fields[2].GetFloat(); + + CreatureData const* data = objmgr.GetCreatureData(guid); + if (!data) + { + sLog.outErrorDb("`pool_creature` has a non existing creature spawn (GUID: %u) defined for pool id (%u), skipped.", guid, pool_id ); + continue; + } + if (pool_id > max_pool_id) + { + sLog.outErrorDb("`pool_creature` pool id (%i) is out of range compared to max pool id in `pool_template`, skipped.",pool_id); + continue; + } + if (chance < 0 || chance > 100) + { + sLog.outErrorDb("`pool_creature` has an invalid chance (%f) for creature guid (%u) in pool id (%i), skipped.", chance, guid, pool_id); + continue; + } + PoolTemplateData *pPoolTemplate = &mPoolTemplate[pool_id]; + ++count; + + PoolObject plObject = PoolObject(guid, chance); + PoolGroup& cregroup = mPoolCreatureGroups[pool_id]; + cregroup.AddEntry(plObject, pPoolTemplate->MaxLimit); + SearchPair p(guid, pool_id); + mCreatureSearchMap.insert(p); + + } while (result->NextRow()); + sLog.outString(); + sLog.outString( ">> Loaded %u creatures in pools", count ); + delete result; + } + + // Gameobjects + + mPoolGameobjectGroups.resize(max_pool_id + 1); + mGameobjectSearchMap.clear(); + // 1 2 3 + result = WorldDatabase.Query("SELECT guid, pool_entry, chance FROM pool_gameobject"); + + count = 0; + if (!result) + { + barGoLink bar2(1); + bar2.step(); + + sLog.outString(); + sLog.outString(">> Loaded %u gameobject in pools", count ); + } + else + { + + barGoLink bar2(result->GetRowCount()); + do + { + Field *fields = result->Fetch(); + + bar2.step(); + + uint32 guid = fields[0].GetUInt32(); + uint16 pool_id = fields[1].GetUInt16(); + float chance = fields[2].GetFloat(); + + GameObjectData const* data = objmgr.GetGOData(guid); + if (!data) + { + sLog.outErrorDb("`pool_gameobject` has a non existing gameobject spawn (GUID: %u) defined for pool id (%u), skipped.", guid, pool_id ); + continue; + } + GameObjectInfo const* goinfo = objmgr.GetGameObjectInfo(data->id); + if (goinfo->type != GAMEOBJECT_TYPE_CHEST && + goinfo->type != GAMEOBJECT_TYPE_GOOBER && + goinfo->type != GAMEOBJECT_TYPE_FISHINGHOLE) + { + sLog.outErrorDb("`pool_gameobject` has a not lootable gameobject spawn (GUID: %u, type: %u) defined for pool id (%u), skipped.", guid, goinfo->type, pool_id ); + continue; + } + if (pool_id > max_pool_id) + { + sLog.outErrorDb("`pool_gameobject` pool id (%i) is out of range compared to max pool id in `pool_template`, skipped.",pool_id); + continue; + } + if (chance < 0 || chance > 100) + { + sLog.outErrorDb("`pool_gameobject` has an invalid chance (%f) for gameobject guid (%u) in pool id (%i), skipped.", chance, guid, pool_id); + continue; + } + PoolTemplateData *pPoolTemplate = &mPoolTemplate[pool_id]; + + ++count; + + PoolObject plObject = PoolObject(guid, chance); + PoolGroup& gogroup = mPoolGameobjectGroups[pool_id]; + gogroup.AddEntry(plObject, pPoolTemplate->MaxLimit); + SearchPair p(guid, pool_id); + mGameobjectSearchMap.insert(p); + + } while( result->NextRow() ); + sLog.outString(); + sLog.outString( ">> Loaded %u gameobject in pools", count ); + delete result; + } +} + +// The initialize method will spawn all pools not in an event and not in another pool, this is why there is 2 left joins with 2 null checks +void PoolHandler::Initialize() +{ + QueryResult *result = WorldDatabase.Query("SELECT DISTINCT pool_template.entry FROM pool_template LEFT JOIN game_event_pool ON pool_template.entry=game_event_pool.pool_entry LEFT JOIN pool_pool ON pool_template.entry=pool_pool.pool_id WHERE game_event_pool.pool_entry IS NULL AND pool_pool.pool_id IS NULL"); + uint32 count=0; + if (result) + { + do + { + Field *fields = result->Fetch(); + uint16 pool_entry = fields[0].GetUInt16(); + if (!CheckPool(pool_entry)) + { + sLog.outErrorDb("Pool Id (%u) has all creatures or gameobjects with explicit chance sum <>100 and no equal chance defined. The pool system cannot pick one to spawn.", pool_entry); + continue; + } + SpawnPool(pool_entry); + count++; + } while (result->NextRow()); + } + + sLog.outBasic("Pool handling system initialized, %u pools spawned.", count); + isSystemInit = true; +} + +// Call to spawn a pool, if cache if true the method will spawn only if cached entry is different +// If it's same, the gameobject/creature is respawned only (added back to map) +void PoolHandler::SpawnPool(uint16 pool_id, bool cache) +{ + if (!mPoolGameobjectGroups[pool_id].isEmpty()) + mPoolGameobjectGroups[pool_id].SpawnObject(mPoolTemplate[pool_id].MaxLimit, cache); + if (!mPoolCreatureGroups[pool_id].isEmpty()) + mPoolCreatureGroups[pool_id].SpawnObject(mPoolTemplate[pool_id].MaxLimit, cache); +} + +// Call to despawn a pool, all gameobjects/creatures in this pool are removed +void PoolHandler::DespawnPool(uint16 pool_id) +{ + if (!mPoolGameobjectGroups[pool_id].isEmpty()) + mPoolGameobjectGroups[pool_id].DespawnObject(); + if (!mPoolCreatureGroups[pool_id].isEmpty()) + mPoolCreatureGroups[pool_id].DespawnObject(); +} + +// Call to update the pool when a gameobject/creature part of pool [pool_id] is ready to respawn +// Here we cache only the creature/gameobject whose guid is passed as parameter +// Then the spawn pool call will use this cache to decide +void PoolHandler::UpdatePool(uint16 pool_id, uint32 guid, uint32 type) +{ + if (type == TYPEID_GAMEOBJECT && !mPoolGameobjectGroups[pool_id].isEmpty()) + mPoolGameobjectGroups[pool_id].DespawnObject(guid); + else if (type != TYPEID_GAMEOBJECT && !mPoolCreatureGroups[pool_id].isEmpty()) + mPoolCreatureGroups[pool_id].DespawnObject(guid); + + SpawnPool(pool_id, true); +} + +// Method that tell if the gameobject/creature is part of a pool and return the pool id if yes +uint16 PoolHandler::IsPartOfAPool(uint32 guid, uint32 type) +{ + if (type == TYPEID_GAMEOBJECT) + { + SearchMap::const_iterator itr = mGameobjectSearchMap.find(guid); + if (itr != mGameobjectSearchMap.end()) + return itr->second; + } + else // creature + { + SearchMap::const_iterator itr = mCreatureSearchMap.find(guid); + if (itr != mCreatureSearchMap.end()) + return itr->second; + } + return 0; +} + +// Method that check chance integrity of the creatures and gameobjects in this pool +bool PoolHandler::CheckPool(uint16 pool_id) +{ + return mPoolCreatureGroups[pool_id].CheckPool() && mPoolGameobjectGroups[pool_id].CheckPool(); +} + +// Method that tell if a creature or gameobject in pool_id is spawned currently +bool PoolHandler::IsSpawnedObject(uint16 pool_id, uint32 guid, uint32 type) +{ + if (pool_id > max_pool_id) + return false; + if (type == TYPEID_GAMEOBJECT) + return mPoolGameobjectGroups[pool_id].IsSpawnedObject(guid); + else + return mPoolCreatureGroups[pool_id].IsSpawnedObject(guid); +} + +//////////////////////////////////////////////////////////// +// Methods of templated Pool Group class above + +template +PoolHandler::PoolGroup::PoolGroup() +{ + Spawned = 0; +} + +// Method to add a gameobject/creature guid to the proper list depending on pool type and chance value +template +void PoolHandler::PoolGroup::AddEntry(PoolObject& poolitem, uint32 maxentries) +{ + if (poolitem.chance != 0 && maxentries == 1) + ExplicitlyChanced.push_back(poolitem); + else + EqualChanced.push_back(poolitem); +} + +// Method to check the chances are proper in this object pool +template +bool PoolHandler::PoolGroup::CheckPool(void) +{ + if (EqualChanced.size() == 0) + { + float chance = 0; + for (uint32 i=0; i +bool PoolHandler::PoolGroup::IsSpawnedObject(uint32 guid) +{ + for (uint32 i=0; i +uint32 PoolHandler::PoolGroup::RollOne(void) +{ + if (!ExplicitlyChanced.empty()) // First explicitly chanced entries are checked + { + float roll = rand_chance(); + + for (uint32 i=0; i +void PoolHandler::PoolGroup::DespawnObject(uint32 guid) +{ + for (int i=0; i +void PoolHandler::PoolGroup::Despawn1Object(uint32 guid) +{ + if (CreatureData const* data = objmgr.GetCreatureData(guid)) + { + objmgr.RemoveCreatureFromGrid(guid, data); + + if (Creature* pCreature = ObjectAccessor::Instance().GetObjectInWorld(MAKE_NEW_GUID(guid, data->id, HIGHGUID_UNIT), (Creature*)NULL)) + { + pCreature->CleanupsBeforeDelete(); + pCreature->AddObjectToRemoveList(); + } + } +} + +// Same on one gameobject +template<> +void PoolHandler::PoolGroup::Despawn1Object(uint32 guid) +{ + if (GameObjectData const* data = objmgr.GetGOData(guid)) + { + objmgr.RemoveGameobjectFromGrid(guid, data); + + if (GameObject* pGameobject = ObjectAccessor::Instance().GetObjectInWorld(MAKE_NEW_GUID(guid, data->id, HIGHGUID_GAMEOBJECT), (GameObject*)NULL)) + pGameobject->AddObjectToRemoveList(); + } +} + +// Method that Spawn 1+ creatures or gameobject +// if cache is false (initialization or event start), X creatures are spawned with X <= limit (< if limit higher that the number of creatures in pool) +// if cache is true, this means only one has to be spawned (or respawned if the rolled one is same as cached one) +template +void PoolHandler::PoolGroup::SpawnObject(uint32 limit, bool cache) +{ + if (limit == 1) // This is the only case where explicit chance is used + { + uint32 roll = RollOne(); + if (cache && CacheValue != roll) + Despawn1Object(CacheValue); + CacheValue = Spawn1Object(roll); + } + else if (limit < EqualChanced.size() && Spawned < limit) + { + std::vector IndexList; + for (int i=0; i 0) + { + uint32 roll = urand(1, IndexList.size()) - 1; + uint32 index = IndexList[roll]; + if (!cache || (cache && EqualChanced[index].guid != CacheValue)) + { + if (cache) + Despawn1Object(CacheValue); + EqualChanced[index].spawned = Spawn1Object(EqualChanced[index].guid); + } + else + EqualChanced[index].spawned = ReSpawn1Object(EqualChanced[index].guid); + + if (EqualChanced[index].spawned) + ++Spawned; // limited group use the Spawned variable to store the number of actualy spawned creatures + std::vector::iterator itr = IndexList.begin()+roll; + IndexList.erase(itr); + } + CacheValue = 0; + } + else // Not enough objects in pool, so spawn all + { + for (int i=0; i +bool PoolHandler::PoolGroup::Spawn1Object(uint32 guid) +{ + CreatureData const* data = objmgr.GetCreatureData(guid); + if (data) + { + objmgr.AddCreatureToGrid(guid, data); + + // Spawn if necessary (loaded grids only) + Map* map = const_cast(MapManager::Instance().GetBaseMap(data->mapid)); + // We use spawn coords to spawn + if (!map->Instanceable() && !map->IsRemovalGrid(data->posX, data->posY)) + { + Creature* pCreature = new Creature; + //sLog.outDebug("Spawning creature %u",guid); + if (!pCreature->LoadFromDB(guid, map)) + { + delete pCreature; + } + else + { + map->Add(pCreature); + } + } + return true; + } + return false; +} + +// Same for 1 gameobject +template <> +bool PoolHandler::PoolGroup::Spawn1Object(uint32 guid) +{ + GameObjectData const* data = objmgr.GetGOData(guid); + if (data) + { + objmgr.AddGameobjectToGrid(guid, data); + // Spawn if necessary (loaded grids only) + // this base map checked as non-instanced and then only existed + Map* map = const_cast(MapManager::Instance().GetBaseMap(data->mapid)); + // We use current coords to unspawn, not spawn coords since creature can have changed grid + if (!map->Instanceable() && !map->IsRemovalGrid(data->posX, data->posY)) + { + GameObject* pGameobject = new GameObject; + //sLog.outDebug("Spawning gameobject %u", guid); + if (!pGameobject->LoadFromDB(guid, map)) + { + delete pGameobject; + } + else + { + if (pGameobject->isSpawnedByDefault()) + map->Add(pGameobject); + } + } + return true; + } + return false; +} + +// Method that does the respawn job on the specified creature +template <> +bool PoolHandler::PoolGroup::ReSpawn1Object(uint32 guid) +{ + CreatureData const* data = objmgr.GetCreatureData(guid); + if (data) + { + if (Creature* pCreature = ObjectAccessor::Instance().GetObjectInWorld(MAKE_NEW_GUID(guid, data->id, HIGHGUID_UNIT), (Creature*)NULL)) + pCreature->GetMap()->Add(pCreature); + return true; + } + return false; +} + +// Same for 1 gameobject +template <> +bool PoolHandler::PoolGroup::ReSpawn1Object(uint32 guid) +{ + GameObjectData const* data = objmgr.GetGOData(guid); + if (data) + { + if (GameObject* pGameobject = ObjectAccessor::Instance().GetObjectInWorld(MAKE_NEW_GUID(guid, data->id, HIGHGUID_GAMEOBJECT), (GameObject*)NULL)) + pGameobject->GetMap()->Add(pGameobject); + return true; + } + return false; +} diff --git a/src/game/PoolHandler.h b/src/game/PoolHandler.h new file mode 100644 index 000000000..43138943d --- /dev/null +++ b/src/game/PoolHandler.h @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2005-2008 MaNGOS + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef MANGOS_POOLHANDLER_H +#define MANGOS_POOLHANDLER_H + +#include "Platform/Define.h" +#include "Creature.h" +#include "GameObject.h" + +struct PoolTemplateData +{ + uint32 MaxLimit; +}; + +struct PoolObject +{ + uint32 guid; + float chance; + bool spawned; + PoolObject(uint32 _guid, float _chance): guid(_guid), chance(fabs(_chance)), spawned(false) {} +}; + +class PoolHandler +{ + template class PoolGroup; + class Pool; + + public: + PoolHandler(); + ~PoolHandler() {}; + void LoadFromDB(); + uint16 IsPartOfAPool(uint32 guid, uint32 type); + bool IsSpawnedObject(uint16 pool_id, uint32 guid, uint32 type); + bool CheckPool(uint16 pool_id); + void SpawnPool(uint16 pool_id, bool cache=false); + void DespawnPool(uint16 pool_id); + void UpdatePool(uint16 pool_id, uint32 guid, uint32 type); + void Initialize(); + + private: + + protected: + bool isSystemInit; + uint16 max_pool_id; + typedef std::vector PoolTemplateDataMap; + typedef std::vector> PoolGroupCreatureMap; + typedef std::vector> PoolGroupGameObjectMap; + typedef std::vector> PoolGroupPoolMap; + typedef std::pair SearchPair; + typedef std::map SearchMap; + + PoolTemplateDataMap mPoolTemplate; + PoolGroupCreatureMap mPoolCreatureGroups; + PoolGroupGameObjectMap mPoolGameobjectGroups; + SearchMap mCreatureSearchMap; + SearchMap mGameobjectSearchMap; + +}; + +template +class PoolHandler::PoolGroup +{ + public: + PoolGroup(); + ~PoolGroup() {}; + bool isEmpty() { return ExplicitlyChanced.size()==0 && EqualChanced.size()==0; } + void AddEntry(PoolObject& poolitem, uint32 maxentries); + bool CheckPool(void); + uint32 RollOne(void); + bool IsSpawnedObject(uint32 guid); + void DespawnObject(uint32 guid=0); + void Despawn1Object(uint32 guid); + void SpawnObject(uint32 limit, bool cache=false); + bool Spawn1Object(uint32 guid); + bool ReSpawn1Object(uint32 guid); + private: + typedef std::vector PoolObjectList; + uint32 CacheValue; // Store the guid of the removed creature/gameobject during a pool update + PoolObjectList ExplicitlyChanced; + PoolObjectList EqualChanced; + uint32 Spawned; // Used to know the number of spawned objects + +}; + +class PoolHandler::Pool +{ +}; + +#define poolhandler MaNGOS::Singleton::Instance() +#endif diff --git a/src/game/World.cpp b/src/game/World.cpp index d8be5cf1a..9ccdf1378 100644 --- a/src/game/World.cpp +++ b/src/game/World.cpp @@ -53,6 +53,7 @@ #include "VMapFactory.h" #include "GlobalEvents.h" #include "GameEvent.h" +#include "PoolHandler.h" #include "Database/DatabaseImpl.h" #include "GridNotifiersImpl.h" #include "CellImpl.h" @@ -1177,6 +1178,9 @@ void World::SetInitialWorldSettings() sLog.outString( "Loading Gameobject Respawn Data..." ); // must be after PackInstances() objmgr.LoadGameobjectRespawnTimes(); + sLog.outString( "Loading Objects Pooling Data..."); + poolhandler.LoadFromDB(); + sLog.outString( "Loading Game Event Data..."); sLog.outString(); gameeventmgr.LoadFromDB(); @@ -1393,6 +1397,9 @@ void World::SetInitialWorldSettings() sLog.outString("Calculate next daily quest reset time..." ); InitDailyQuestResetTime(); + sLog.outString("Starting objects Pooling system..." ); + poolhandler.Initialize(); + sLog.outString("Starting Game Event system..." ); uint32 nextGameEvent = gameeventmgr.Initialize(); m_timers[WUPDATE_EVENTS].SetInterval(nextGameEvent); //depend on next event diff --git a/win/VC71/game.vcproj b/win/VC71/game.vcproj index 6438ba1b6..0643908d5 100644 --- a/win/VC71/game.vcproj +++ b/win/VC71/game.vcproj @@ -392,6 +392,12 @@ + + + + diff --git a/win/VC80/game.vcproj b/win/VC80/game.vcproj index e53a59e55..0ac846d64 100644 --- a/win/VC80/game.vcproj +++ b/win/VC80/game.vcproj @@ -694,6 +694,14 @@ RelativePath="..\..\src\game\PetitionsHandler.cpp" > + + + + diff --git a/win/VC90/game.vcproj b/win/VC90/game.vcproj index 265f33138..d3908ff2b 100644 --- a/win/VC90/game.vcproj +++ b/win/VC90/game.vcproj @@ -696,6 +696,14 @@ RelativePath="..\..\src\game\PetitionsHandler.cpp" > + + + +