/*
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "PoolHandler.h"
#include "ObjectMgr.h"
#include "ProgressBar.h"
#include "Log.h"
#include "MapManager.h"
#include "Policies/SingletonImp.h"
INSTANTIATE_SINGLETON_1(PoolHandler);
////////////////////////////////////////////////////////////
// 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(void)
{
    if (EqualChanced.size() == 0)
    {
        float chance = 0;
        for (uint32 i=0; i
bool PoolGroup::IsSpawnedObject(uint32 guid)
{
    for (uint32 i=0; i
void PoolGroup::RollOne(int32& index, PoolObjectList** store, uint32 triggerFrom)
{
    if (!ExplicitlyChanced.empty())
    {
        float roll = (float)rand_chance();
        for (uint32 i=0; i
void PoolGroup::DespawnObject(uint32 guid)
{
    for (size_t i=0; i 0)
                    --m_SpawnedPoolAmount;
            }
        }
    }
    for (size_t i = 0; i < ExplicitlyChanced.size(); ++i)
    {
        if (ExplicitlyChanced[i].spawned)
        {
            if (!guid || ExplicitlyChanced[i].guid == guid)
            {
                Despawn1Object(ExplicitlyChanced[i].guid);
                ExplicitlyChanced[i].spawned = false;
                if (m_SpawnedPoolAmount > 0)
                    --m_SpawnedPoolAmount;
            }
        }
    }
}
// Method that is actualy doing the removal job on one creature
template<>
void 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->AddObjectToRemoveList();
    }
}
// Same on one gameobject
template<>
void 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();
    }
}
// Same on one pool
template<>
void PoolGroup::Despawn1Object(uint32 child_pool_id)
{
    poolhandler.DespawnPool(child_pool_id);
}
// Method for a pool only to remove any found record causing a circular dependency loop
template<>
void PoolGroup::RemoveOneRelation(uint16 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(uint32 limit, uint32 triggerFrom)
{
    uint32 lastDespawned = 0;
    int count = limit - m_SpawnedPoolAmount;
    // 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)
    {
        int index;
        PoolObjectList* store;
        RollOne(index, &store, triggerFrom);
        if (index == -1)
            continue;
        if ((*store)[index].guid == lastDespawned)
            continue;
        if ((*store)[index].guid == triggerFrom)
        {
            (*store)[index].spawned = ReSpawn1Object(triggerFrom);
            triggerFrom = 0;
            continue;
        }
        else
            (*store)[index].spawned = Spawn1Object((*store)[index].guid);
        if (triggerFrom)
        {
            // One spawn one despawn no count increase
            DespawnObject(triggerFrom);
            lastDespawned = triggerFrom;
            triggerFrom = 0;
        }
        else
            ++m_SpawnedPoolAmount;
    }
}
// Method that is actualy doing the spawn job on 1 creature
template <>
bool PoolGroup::Spawn1Object(uint32 guid)
{
    if (CreatureData const* data = objmgr.GetCreatureData(guid))
    {
        objmgr.AddCreatureToGrid(guid, data);
        // Spawn if necessary (loaded grids only)
        Map* map = const_cast