/*
* Copyright (C) 2005-2011 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 "PoolManager.h"
#include "ObjectMgr.h"
#include "ObjectGuid.h"
#include "ProgressBar.h"
#include "Log.h"
#include "MapPersistentStateMgr.h"
#include "MapManager.h"
#include "World.h"
#include "Policies/SingletonImp.h"
INSTANTIATE_SINGLETON_1(PoolManager);
////////////////////////////////////////////////////////////
// template class SpawnedPoolData
// Method that tell amount spawned objects/subpools
uint32 SpawnedPoolData::GetSpawnedObjects(uint32 pool_id) const
{
SpawnedPoolPools::const_iterator itr = mSpawnedPools.find(pool_id);
return itr != mSpawnedPools.end() ? itr->second : 0;
}
// Method that tell if a creature is spawned currently
template<>
bool SpawnedPoolData::IsSpawnedObject(uint32 db_guid) const
{
return mSpawnedCreatures.find(db_guid) != mSpawnedCreatures.end();
}
// Method that tell if a gameobject is spawned currently
template<>
bool SpawnedPoolData::IsSpawnedObject(uint32 db_guid) const
{
return mSpawnedGameobjects.find(db_guid) != mSpawnedGameobjects.end();
}
// Method that tell if a pool is spawned currently
template<>
bool SpawnedPoolData::IsSpawnedObject(uint32 sub_pool_id) const
{
return mSpawnedPools.find(sub_pool_id) != mSpawnedPools.end();
}
template<>
void SpawnedPoolData::AddSpawn(uint32 db_guid, uint32 pool_id)
{
mSpawnedCreatures.insert(db_guid);
++mSpawnedPools[pool_id];
}
template<>
void SpawnedPoolData::AddSpawn(uint32 db_guid, uint32 pool_id)
{
mSpawnedGameobjects.insert(db_guid);
++mSpawnedPools[pool_id];
}
template<>
void SpawnedPoolData::AddSpawn(uint32 sub_pool_id, uint32 pool_id)
{
mSpawnedPools[sub_pool_id] = 0;
++mSpawnedPools[pool_id];
}
template<>
void SpawnedPoolData::RemoveSpawn(uint32 db_guid, uint32 pool_id)
{
mSpawnedCreatures.erase(db_guid);
uint32& val = mSpawnedPools[pool_id];
if (val > 0)
--val;
}
template<>
void SpawnedPoolData::RemoveSpawn(uint32 db_guid, uint32 pool_id)
{
mSpawnedGameobjects.erase(db_guid);
uint32& val = mSpawnedPools[pool_id];
if (val > 0)
--val;
}
template<>
void SpawnedPoolData::RemoveSpawn(uint32 sub_pool_id, uint32 pool_id)
{
mSpawnedPools.erase(sub_pool_id);
uint32& val = mSpawnedPools[pool_id];
if (val > 0)
--val;
}
////////////////////////////////////////////////////////////
// Methods of class PoolObject
template<>
void PoolObject::CheckEventLinkAndReport(uint32 poolId, int16 event_id, std::map const& creature2event, std::map const& /*go2event*/) const
{
std::map::const_iterator itr = creature2event.find(guid);
if (itr == creature2event.end() || itr->second != event_id)
sLog.outErrorDb("Creature (GUID: %u) expected to be listed in `game_event_creature` for event %u as part pool %u", guid, event_id, poolId);
}
template<>
void PoolObject::CheckEventLinkAndReport(uint32 poolId, int16 event_id, std::map const& /*creature2event*/, std::map const& go2event) const
{
std::map::const_iterator itr = go2event.find(guid);
if (itr == go2event.end() || itr->second != event_id)
sLog.outErrorDb("Gameobject (GUID: %u) expected to be listed in `game_event_gameobject` for event %u as part pool %u", guid, event_id, poolId);
}
template<>
void PoolObject::CheckEventLinkAndReport(uint32 poolId, int16 event_id, std::map const& creature2event, std::map const& go2event) const
{
sPoolMgr.CheckEventLinkAndReport(guid, event_id, creature2event, go2event);
}
////////////////////////////////////////////////////////////
// Methods of template class PoolGroup
// Method to add a gameobject/creature guid to the proper list depending on pool type and chance value
template
void 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 PoolGroup::CheckPool() const
{
if (EqualChanced.size() == 0)
{
float chance = 0;
for (uint32 i=0; i
void PoolGroup::CheckEventLinkAndReport(int16 event_id, std::map const& creature2event, std::map const& go2event) const
{
for (uint32 i=0; i < EqualChanced.size(); ++i)
EqualChanced[i].CheckEventLinkAndReport(poolId, event_id, creature2event, go2event);
for (uint32 i=0; i(poolId, event_id, creature2event, go2event);
}
template
void PoolGroup::SetExcludeObject(uint32 guid, bool state)
{
for (uint32 i=0; i < EqualChanced.size(); ++i)
{
if (EqualChanced[i].guid == guid)
{
EqualChanced[i].exclude = state;
return;
}
}
for (uint32 i=0; i
PoolObject* PoolGroup::RollOne(SpawnedPoolData& spawns, uint32 triggerFrom)
{
if (!ExplicitlyChanced.empty())
{
float roll = (float)rand_chance();
for (uint32 i = 0; i < ExplicitlyChanced.size(); ++i)
{
roll -= ExplicitlyChanced[i].chance;
// Triggering object is marked as spawned at this time and can be also rolled (respawn case)
// so this need explicit check for this case
if (roll < 0 && !ExplicitlyChanced[i].exclude && (ExplicitlyChanced[i].guid == triggerFrom || !spawns.IsSpawnedObject(ExplicitlyChanced[i].guid)))
return &ExplicitlyChanced[i];
}
}
if (!EqualChanced.empty())
{
int32 index = irand(0, EqualChanced.size()-1);
// Triggering object is marked as spawned at this time and can be also rolled (respawn case)
// so this need explicit check for this case
if (!EqualChanced[index].exclude && (EqualChanced[index].guid == triggerFrom || !spawns.IsSpawnedObject(EqualChanced[index].guid)))
return &EqualChanced[index];
}
return NULL;
}
// Main method to despawn a creature or gameobject in a pool
// If no guid is passed, the pool is just removed (event end case)
// If guid is filled, cache will be used and no removal will occur, it just fill the cache
template
void PoolGroup::DespawnObject(SpawnedPoolData& spawns, uint32 guid)
{
for (size_t i = 0; i < EqualChanced.size(); ++i)
{
// if spawned
if (spawns.IsSpawnedObject(EqualChanced[i].guid))
{
// any or specially requested
if (!guid || EqualChanced[i].guid == guid)
{
Despawn1Object(EqualChanced[i].guid);
spawns.RemoveSpawn(EqualChanced[i].guid,poolId);
}
}
}
for (size_t i = 0; i < ExplicitlyChanced.size(); ++i)
{
// spawned
if (spawns.IsSpawnedObject(ExplicitlyChanced[i].guid))
{
// any or specially requested
if (!guid || ExplicitlyChanced[i].guid == guid)
{
Despawn1Object(ExplicitlyChanced[i].guid);
spawns.RemoveSpawn(ExplicitlyChanced[i].guid,poolId);
}
}
}
}
// Method that is actualy doing the removal job on one creature
template<>
void PoolGroup::Despawn1Object(uint32 guid)
{
if (CreatureData const* data = sObjectMgr.GetCreatureData(guid))
{
sObjectMgr.RemoveCreatureFromGrid(guid, data);
// FIXME: pool system must have local state for each instanced map copy
// Current code preserve existed single state for all instanced map copies way
// specially because pool system not spawn object in instanceable maps
MapEntry const* mapEntry = sMapStore.LookupEntry(data->mapid);
// temporary limit pool system full power work to continents
if (mapEntry && !mapEntry->Instanceable())
{
if (Map* map = const_cast