/*
* Copyright (C) 2008-2010 TrinityCore
* Copyright (C) 2005-2009 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, see .
*/
#include "PoolMgr.h"
#include "ObjectMgr.h"
#include "Log.h"
#include "MapManager.h"
////////////////////////////////////////////////////////////
// template class ActivePoolData
// Method that tell amount spawned objects/subpools
uint32 ActivePoolData::GetActiveObjectCount(uint32 pool_id) const
{
ActivePoolPools::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 ActivePoolData::IsActiveObject(uint32 db_guid) const
{
return mSpawnedCreatures.find(db_guid) != mSpawnedCreatures.end();
}
// Method that tell if a gameobject is spawned currently
template<>
bool ActivePoolData::IsActiveObject(uint32 db_guid) const
{
return mSpawnedGameobjects.find(db_guid) != mSpawnedGameobjects.end();
}
// Method that tell if a pool is spawned currently
template<>
bool ActivePoolData::IsActiveObject(uint32 sub_pool_id) const
{
return mSpawnedPools.find(sub_pool_id) != mSpawnedPools.end();
}
// Method that tell if a quest can be started
template<>
bool ActivePoolData::IsActiveObject(uint32 quest_id) const
{
return mActiveQuests.find(quest_id) != mActiveQuests.end();
}
template<>
void ActivePoolData::ActivateObject(uint32 db_guid, uint32 pool_id)
{
mSpawnedCreatures.insert(db_guid);
++mSpawnedPools[pool_id];
}
template<>
void ActivePoolData::ActivateObject(uint32 db_guid, uint32 pool_id)
{
mSpawnedGameobjects.insert(db_guid);
++mSpawnedPools[pool_id];
}
template<>
void ActivePoolData::ActivateObject(uint32 sub_pool_id, uint32 pool_id)
{
mSpawnedPools[sub_pool_id] = 0;
++mSpawnedPools[pool_id];
}
template<>
void ActivePoolData::ActivateObject(uint32 quest_id, uint32 pool_id)
{
mActiveQuests.insert(quest_id);
++mSpawnedPools[pool_id];
}
template<>
void ActivePoolData::RemoveObject(uint32 db_guid, uint32 pool_id)
{
mSpawnedCreatures.erase(db_guid);
uint32& val = mSpawnedPools[pool_id];
if (val > 0)
--val;
}
template<>
void ActivePoolData::RemoveObject(uint32 db_guid, uint32 pool_id)
{
mSpawnedGameobjects.erase(db_guid);
uint32& val = mSpawnedPools[pool_id];
if (val > 0)
--val;
}
template<>
void ActivePoolData::RemoveObject(uint32 sub_pool_id, uint32 pool_id)
{
mSpawnedPools.erase(sub_pool_id);
uint32& val = mSpawnedPools[pool_id];
if (val > 0)
--val;
}
template<>
void ActivePoolData::RemoveObject(uint32 quest_id, uint32 pool_id)
{
mActiveQuests.erase(quest_id);
uint32& val = mSpawnedPools[pool_id];
if (val > 0)
--val;
}
////////////////////////////////////////////////////////////
// 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())
{
float chance = 0;
for (uint32 i = 0; i < ExplicitlyChanced.size(); ++i)
chance += ExplicitlyChanced[i].chance;
if (chance != 100 && chance != 0)
return false;
}
return true;
}
template
PoolObject* PoolGroup::RollOne(ActivePoolData& 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].guid == triggerFrom || !spawns.IsActiveObject(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].guid == triggerFrom || !spawns.IsActiveObject(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(ActivePoolData& spawns, uint32 guid)
{
for (size_t i=0; i < EqualChanced.size(); ++i)
{
// if spawned
if (spawns.IsActiveObject(EqualChanced[i].guid))
{
if (!guid || EqualChanced[i].guid == guid)
{
Despawn1Object(EqualChanced[i].guid);
spawns.RemoveObject(EqualChanced[i].guid,poolId);
}
}
}
for (size_t i = 0; i < ExplicitlyChanced.size(); ++i)
{
// spawned
if (spawns.IsActiveObject(ExplicitlyChanced[i].guid))
{
if (!guid || ExplicitlyChanced[i].guid == guid)
{
Despawn1Object(ExplicitlyChanced[i].guid);
spawns.RemoveObject(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);
if (Creature* pCreature = sObjectAccessor.GetObjectInWorld(MAKE_NEW_GUID(guid, data->id, HIGHGUID_UNIT), (Creature*)NULL))
pCreature->AddObjectToRemoveList();
}
}
// Same on one gameobject
template<>
void PoolGroup::Despawn1Object(uint32 guid)
{
if (GameObjectData const* data = sObjectMgr.GetGOData(guid))
{
sObjectMgr.RemoveGameobjectFromGrid(guid, data);
if (GameObject* pGameobject = sObjectAccessor.GetObjectInWorld(MAKE_NEW_GUID(guid, data->id, HIGHGUID_GAMEOBJECT), (GameObject*)NULL))
pGameobject->AddObjectToRemoveList();
}
}
// Same on one pool
template<>
void PoolGroup::Despawn1Object(uint32 child_pool_id)
{
sPoolMgr.DespawnPool(child_pool_id);
}
// Same on one quest
template<>
void PoolGroup::Despawn1Object(uint32 quest_id)
{
// Creatures
QuestRelations* questMap = sObjectMgr.GetCreatureQuestRelationMap();
PooledQuestRelationBoundsNC qr = sPoolMgr.mQuestCreatureRelation.equal_range(quest_id);
for (PooledQuestRelation::iterator itr = qr.first; itr != qr.second; ++itr)
{
QuestRelations::iterator qitr = questMap->find(itr->second);
if (qitr == questMap->end())
continue;
QuestRelations::iterator lastElement = questMap->upper_bound(itr->second);
for (; qitr != lastElement; ++qitr)
{
if (qitr->first == itr->second)
{
questMap->erase(qitr); // iterator is now no more valid
break; // but we can exit loop since the element is found
}
}
}
// Gameobjects
questMap = sObjectMgr.GetGOQuestRelationMap();
qr = sPoolMgr.mQuestGORelation.equal_range(quest_id);
for (PooledQuestRelation::iterator itr = qr.first; itr != qr.second; ++itr)
{
QuestRelations::iterator qitr = questMap->find(itr->second);
if (qitr == questMap->end())
continue;
QuestRelations::iterator lastElement = questMap->upper_bound(itr->second);
for (; qitr != lastElement; ++qitr)
{
if (qitr->first == itr->second)
{
questMap->erase(qitr); // iterator is now no more valid
break; // but we can exit loop since the element is found
}
}
}
}
// Method for a pool only to remove any found record causing a circular dependency loop
template<>
void PoolGroup::RemoveOneRelation(uint32 child_pool_id)
{
for (PoolObjectList::iterator itr = ExplicitlyChanced.begin(); itr != ExplicitlyChanced.end(); ++itr)
{
if (itr->guid == child_pool_id)
{
ExplicitlyChanced.erase(itr);
break;
}
}
for (PoolObjectList::iterator itr = EqualChanced.begin(); itr != EqualChanced.end(); ++itr)
{
if (itr->guid == child_pool_id)
{
EqualChanced.erase(itr);
break;
}
}
}
template
void PoolGroup::SpawnObject(ActivePoolData& spawns, uint32 limit, uint32 triggerFrom)
{
uint32 lastDespawned = 0;
int count = limit - spawns.GetActiveObjectCount(poolId);
// If triggered from some object respawn this object is still marked as spawned
// and also counted into m_SpawnedPoolAmount so we need increase count to be
// spawned by 1
if (triggerFrom)
++count;
// This will try to spawn the rest of pool, not guaranteed
for (int i = 0; i < count; ++i)
{
PoolObject* obj = RollOne(spawns, triggerFrom);
if (!obj)
continue;
if (obj->guid == lastDespawned)
continue;
if (obj->guid == triggerFrom)
{
ReSpawn1Object(obj);
triggerFrom = 0;
continue;
}
spawns.ActivateObject(obj->guid, poolId);
Spawn1Object(obj);
if (triggerFrom)
{
// One spawn one despawn no count increase
DespawnObject(spawns, triggerFrom);
lastDespawned = triggerFrom;
triggerFrom = 0;
}
}
}
// Method that is actualy doing the spawn job on 1 creature
template <>
void PoolGroup::Spawn1Object(PoolObject* obj)
{
if (CreatureData const* data = sObjectMgr.GetCreatureData(obj->guid))
{
sObjectMgr.AddCreatureToGrid(obj->guid, data);
// Spawn if necessary (loaded grids only)
Map* map = const_cast