19#include <unordered_map>
27 typedef std::unordered_map<uint32, int32> BattlePetWildPoolZoneMapCache;
28 static BattlePetWildPoolZoneMapCache zoneMapCache;
30 BattlePetWildPoolZoneMapCache::const_iterator cacheItr = zoneMapCache.find(zoneId);
31 if (cacheItr != zoneMapCache.end())
32 return cacheItr->second;
37 if (areaEntry && areaEntry->
m_ID == zoneId)
44 zoneMapCache[zoneId] = -1;
53 if (!maxLevel || maxLevel < minLevel)
56 if (maxLevel == minLevel)
62 uint8 BattlePetWildPoolMaxCount(
uint8 databaseMax)
68 if (!configuredMin || databaseMax >= configuredMin)
71 return uint8(configuredMin > 255 ? 255 : configuredMin);
76 if (!creature || !creature->
GetMap())
96 QueryResult result =
WorldDatabase.Query(
"SELECT zone, species, entry, max, minLevel, maxLevel FROM battle_pet_wild_pool");
99 SF_LOG_INFO(
"server.loading",
">> Loaded 0 battle pet wild pool definitions. DB table `battle_pet_wild_pool` is empty.");
106 Field* fields = result->Fetch();
117 SF_LOG_ERROR(
"sql.sql",
"Table `battle_pet_wild_pool` has invalid species %u for creature entry %u, skipped.", species, entry);
123 SF_LOG_ERROR(
"sql.sql",
"Table `battle_pet_wild_pool` has invalid replacement source entry %u for species %u, skipped.", entry, species);
129 SF_LOG_ERROR(
"sql.sql",
"Table `battle_pet_wild_pool` species %u references missing creature template %u, skipped.", species, speciesEntry->
NpcId);
133 int32 const mapId = BattlePetWildPoolMapForZone(zoneId);
136 SF_LOG_ERROR(
"sql.sql",
"Table `battle_pet_wild_pool` has invalid zone %u for species %u, skipped.", zoneId, species);
141 spawnTemplate.
Species = species;
142 spawnTemplate.
Entry = entry;
143 spawnTemplate.
Max = BattlePetWildPoolMaxCount(max);
144 spawnTemplate.
MinLevel = minLevel ? minLevel : 1;
149 }
while (result->NextRow());
161 for (BattlePetMapPoolMap::iterator mapItr =
_mapPools.begin(); mapItr !=
_mapPools.end(); ++mapItr)
162 for (BattlePetZonePoolMap::iterator zoneItr = mapItr->second.begin(); zoneItr != mapItr->second.end(); ++zoneItr)
163 zoneItr->second.Populate();
172 zonePool->OnCreatureAdded(creature);
181 zonePool->OnCreatureRemoved(creature);
190 return zonePool->RemoveReplacement(creature);
201 return zonePool->IsReplacement(creature->
GetGUID());
215 uint32 const zoneId = BattlePetWildPoolZoneForCreature(creature);
219 BattlePetZonePoolMap::iterator zoneItr = mapItr->second.find(zoneId);
220 if (zoneItr == mapItr->second.end())
223 return &zoneItr->second;
231 BattlePetMapPoolMap::const_iterator mapItr =
_mapPools.find(creature->
GetMapId());
235 uint32 const zoneId = BattlePetWildPoolZoneForCreature(creature);
239 BattlePetZonePoolMap::const_iterator zoneItr = mapItr->second.find(zoneId);
240 if (zoneItr == mapItr->second.end())
243 return &zoneItr->second;
248 for (std::vector<BattlePetWildPoolTemplate>::const_iterator itr =
_templates.begin(); itr !=
_templates.end(); ++itr)
249 if (itr->Entry == spawnTemplate.
Entry && itr->Species == spawnTemplate.
Species)
257 for (std::vector<BattlePetWildPoolTemplate>::iterator itr =
_templates.begin(); itr !=
_templates.end(); ++itr)
264 for (std::set<uint64>::const_iterator guidItr = ready.begin(); guidItr != ready.end(); ++guidItr)
279 for (std::vector<BattlePetWildPoolTemplate>::iterator itr =
_templates.begin(); itr !=
_templates.end(); ++itr)
292 for (std::vector<BattlePetWildPoolTemplate>::iterator itr =
_templates.begin(); itr !=
_templates.end(); ++itr)
306 for (std::vector<BattlePetWildPoolTemplate>::iterator itr =
_templates.begin(); itr !=
_templates.end(); ++itr)
309 for (std::map<uint64, uint64>::const_iterator relationItr = spawnTemplate.
Replacements.begin();
310 relationItr != spawnTemplate.
Replacements.end(); ++relationItr)
312 if (relationItr->second != replacementGuid)
324 for (std::vector<BattlePetWildPoolTemplate>::const_iterator itr =
_templates.begin(); itr !=
_templates.end(); ++itr)
325 for (std::map<uint64, uint64>::const_iterator relationItr = itr->Replacements.begin();
326 relationItr != itr->Replacements.end(); ++relationItr)
327 if (relationItr->second == guid)
367 uint8 const level = BattlePetWildPoolLevel(*original, spawnTemplate.
MinLevel, spawnTemplate.
MaxLevel);
390 std::map<uint64, uint64>::iterator relationItr = spawnTemplate.
Replacements.find(originalGuid);
394 uint64 const replacementGuid = relationItr->second;
404 if (
CreatureData const* creatureData = original->GetCreatureData())
405 original->SetRespawnTime(creatureData->spawntimesecs);
407 original->SetRespawnTime(0);
DB2Storage< BattlePetSpeciesEntry > sBattlePetSpeciesStore(BattlePetSpeciesfmt)
DBCStorage< AreaTableEntry > sAreaStore(AreaTableEntryfmt)
#define SF_LOG_ERROR(filterType__,...)
#define SF_LOG_INFO(filterType__,...)
Skyfire::AutoPtr< ResultSet, Skyfire::Mutex > QueryResult
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
@ UNIT_NPC_FLAG_WILDPET_CAPTURABLE
@ UNIT_FLAG_IMMUNE_TO_NPC
@ UNIT_FIELD_WILD_BATTLE_PET_LEVEL
BattlePetMapPoolMap _mapPools
bool ResolveWildBattle(Creature *creature)
BattlePetWildZonePool * GetZonePool(Creature const *creature)
void OnCreatureAdded(Creature *creature)
bool IsWildBattlePetReplacement(Creature const *creature) const
void OnCreatureRemoved(Creature *creature)
static BattlePetSpawnMgr * instance()
void OnCreatureAdded(Creature *creature)
void SpawnReplacement(uint64 originalGuid, BattlePetWildPoolTemplate &spawnTemplate)
bool RemoveReplacement(Creature *replacement)
std::vector< BattlePetWildPoolTemplate > _templates
void AddTemplate(BattlePetWildPoolTemplate const &spawnTemplate)
void OnCreatureRemoved(Creature *creature)
bool IsReplacement(uint64 guid) const
void SetRespawnTime(uint32 respawn)
void DespawnOrUnsummon(uint32 msTimeToDespawn=0)
void RemoveCorpse(bool setSpawnTime=true)
bool Create(uint32 guidlow, Map *map, uint32 Entry, uint32 vehId, uint32 team, float x, float y, float z, float ang, const CreatureData *data=NULL)
void RemoveFromWorld() OVERRIDE
uint32 GetZoneId(float x, float y, float z) const
static T * GetObjectInWorld(uint64 guid, T *)
void SetFlag(uint16 index, uint32 newFlag)
void SetUInt32Value(uint16 index, uint32 value)
void AddObjectToRemoveList()
WorldDatabaseWorkerPool WorldDatabase
Accessor to the world database.
@ CONFIG_BATTLE_PET_WILD_SPAWN_MIN_COUNT
std::set< uint64 > ReadyForReplacement
std::map< uint64, uint64 > Replacements
float GetPositionZ() const
float GetOrientation() const
float GetPositionX() const
float GetPositionY() const