diff --git a/src/ABAllCreatureScript.cpp b/src/ABAllCreatureScript.cpp index e9bdbce..46bf0d7 100644 --- a/src/ABAllCreatureScript.cpp +++ b/src/ABAllCreatureScript.cpp @@ -298,7 +298,7 @@ bool AutoBalance_AllCreatureScript::ResetCreatureIfNeeded(Creature* creature) return false; // get (or create) map and creature info - AutoBalanceMapInfo* mapABInfo = creature->GetMap()->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(creature->GetMap()); AutoBalanceCreatureInfo* creatureABInfo = creature->CustomData.GetDefault("AutoBalanceCreatureInfo"); // if creature is dead and mapConfigTime is 0, skip for now @@ -421,7 +421,7 @@ void AutoBalance_AllCreatureScript::ModifyCreatureAttributes(Creature* creature) AutoBalanceCreatureInfo* creatureABInfo = creature->CustomData.GetDefault("AutoBalanceCreatureInfo"); Map* map = creature->GetMap(); InstanceMap* instanceMap = map->ToInstanceMap(); - AutoBalanceMapInfo* mapABInfo = instanceMap->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(instanceMap); // mark the creature as updated using the current settings if needed // if this creature is brand new, do not update this so that it will be re-processed next OnCreatureUpdate diff --git a/src/ABAllMapScript.cpp b/src/ABAllMapScript.cpp index 527493f..e0cb3bf 100644 --- a/src/ABAllMapScript.cpp +++ b/src/ABAllMapScript.cpp @@ -7,91 +7,6 @@ #include "Chat.h" #include "Message.h" -void AutoBalance_AllMapScript::OnCreateMap(Map* map) -{ - LOG_DEBUG("module.AutoBalance", "AutoBalance_AllMapScript::OnCreateMap(): Map {} ({}{})", - map->GetMapName(), - map->GetId(), - map->GetInstanceId() ? "-" + std::to_string(map->GetInstanceId()) : "" - ); - - // clear out any previously-recorded data - map->CustomData.Erase("AutoBalanceMapInfo"); - - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); - - if (map->IsDungeon()) - { - // get the map's LFG stats even if not enabled - LFGDungeonEntry const* dungeon = GetLFGDungeon(map->GetId(), map->GetDifficulty()); - if (dungeon) { - mapABInfo->lfgMinLevel = dungeon->MinLevel; - mapABInfo->lfgMaxLevel = dungeon->MaxLevel; - mapABInfo->lfgTargetLevel = dungeon->TargetLevel; - } - // if this is a heroic dungeon that isn't in LFG, get the stats from the non-heroic version - else if (map->IsHeroic()) - { - LFGDungeonEntry const* nonHeroicDungeon = nullptr; - if (map->GetDifficulty() == DUNGEON_DIFFICULTY_HEROIC) - nonHeroicDungeon = GetLFGDungeon(map->GetId(), DUNGEON_DIFFICULTY_NORMAL); - else if (map->GetDifficulty() == RAID_DIFFICULTY_10MAN_HEROIC) - nonHeroicDungeon = GetLFGDungeon(map->GetId(), RAID_DIFFICULTY_10MAN_NORMAL); - else if (map->GetDifficulty() == RAID_DIFFICULTY_25MAN_HEROIC) - nonHeroicDungeon = GetLFGDungeon(map->GetId(), RAID_DIFFICULTY_25MAN_NORMAL); - - LOG_DEBUG("module.AutoBalance", "AutoBalance_AllMapScript::OnCreateMap(): Map {} ({}{}) | is a Heroic dungeon that is not in LFG. Using non-heroic LFG levels.", - map->GetMapName(), - map->GetId(), - map->GetInstanceId() ? "-" + std::to_string(map->GetInstanceId()) : "" - ); - - if (nonHeroicDungeon) - { - mapABInfo->lfgMinLevel = nonHeroicDungeon->MinLevel; - mapABInfo->lfgMaxLevel = nonHeroicDungeon->MaxLevel; - mapABInfo->lfgTargetLevel = nonHeroicDungeon->TargetLevel; - } - else - { - LOG_ERROR("module.AutoBalance", "AutoBalance_AllMapScript::OnCreateMap(): Map {} ({}{}) | Could not determine LFG level ranges for this map. Level will bet set to 0.", - map->GetMapName(), - map->GetId(), - map->GetInstanceId() ? "-" + std::to_string(map->GetInstanceId()) : "" - ); - } - } - - if (map->GetInstanceId()) - { - LOG_DEBUG("module.AutoBalance", "AutoBalance_AllMapScript::OnCreateMap(): Map {} ({}{}) | is an instance of a map. Loading initial map data.", - map->GetMapName(), - map->GetId(), - map->GetInstanceId() ? "-" + std::to_string(map->GetInstanceId()) : "" - ); - UpdateMapDataIfNeeded(map); - - // provide a concise summary of the map data we collected - LOG_DEBUG("module.AutoBalance", "AutoBalance_AllMapScript::OnCreateMap(): Map {} ({}{}) | LFG levels ({}-{}) (target {}). {} for AutoBalancing.", - map->GetMapName(), - map->GetId(), - map->GetInstanceId() ? "-" + std::to_string(map->GetInstanceId()) : "", - mapABInfo->lfgMinLevel ? std::to_string(mapABInfo->lfgMinLevel) : "?", - mapABInfo->lfgMaxLevel ? std::to_string(mapABInfo->lfgMaxLevel) : "?", - mapABInfo->lfgTargetLevel ? std::to_string(mapABInfo->lfgTargetLevel) : "?", - mapABInfo->enabled ? "Enabled" : "Disabled" - ); - } - else - { - LOG_DEBUG( - "module.AutoBalance", "AutoBalance_AllMapScript::OnCreateMap(): Map {} ({}) | is an instance base map.", - map->GetMapName(), - map->GetId() - ); - } - } -} void AutoBalance_AllMapScript::OnPlayerEnterAll(Map* map, Player* player) { @@ -112,7 +27,7 @@ void AutoBalance_AllMapScript::OnPlayerEnterAll(Map* map, Player* player) ); // get the map's info - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(map); // store the previous difficulty for comparison later int prevAdjustedPlayerCount = mapABInfo->adjustedPlayerCount; @@ -218,7 +133,7 @@ void AutoBalance_AllMapScript::OnPlayerLeaveAll(Map* map, Player* player) ); // get the map's info - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(map); // store the previous difficulty for comparison later int prevAdjustedPlayerCount = mapABInfo->adjustedPlayerCount; diff --git a/src/ABAllMapScript.h b/src/ABAllMapScript.h index 7ec65de..14d4f26 100644 --- a/src/ABAllMapScript.h +++ b/src/ABAllMapScript.h @@ -12,14 +12,12 @@ class AutoBalance_AllMapScript : public AllMapScript public: AutoBalance_AllMapScript() : AllMapScript("AutoBalance_AllMapScript", { - ALLMAPHOOK_ON_CREATE_MAP, ALLMAPHOOK_ON_PLAYER_ENTER_ALL, ALLMAPHOOK_ON_PLAYER_LEAVE_ALL }) { } - void OnCreateMap(Map* map) override; // hook triggers after the player has already entered the world void OnPlayerEnterAll(Map* map, Player* player) override; // hook triggers just before the player left the world diff --git a/src/ABCommandScript.cpp b/src/ABCommandScript.cpp index 891123f..14b9356 100644 --- a/src/ABCommandScript.cpp +++ b/src/ABCommandScript.cpp @@ -42,7 +42,7 @@ bool AutoBalance_CommandScript::HandleABMapStatsCommand(ChatHandler* handler, co Player* player = handler->GetPlayer(); auto locale = handler->GetSession()->GetSessionDbLocaleIndex(); - AutoBalanceMapInfo* mapABInfo = player->GetMap()->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(player->GetMap()); if (player->GetMap()->IsDungeon()) { diff --git a/src/ABGameObjectScript.cpp b/src/ABGameObjectScript.cpp index b4e265a..d38b437 100644 --- a/src/ABGameObjectScript.cpp +++ b/src/ABGameObjectScript.cpp @@ -2,6 +2,7 @@ #include "ABConfig.h" #include "ABMapInfo.h" +#include "ABUtils.h" void AutoBalance_GameObjectScript::OnGameObjectModifyHealth(GameObject* target, Unit* source, int32& amount, SpellInfo const* spellInfo) { @@ -135,7 +136,7 @@ int32 AutoBalance_GameObjectScript::_Modify_GameObject_Damage_Healing(GameObject } // get the map's info - AutoBalanceMapInfo* targetMapABInfo = target->GetMap()->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* targetMapABInfo = GetMapInfo(target->GetMap()); // if the target's map is not enabled, return the original damage if (!targetMapABInfo->enabled) diff --git a/src/ABGlobalScript.cpp b/src/ABGlobalScript.cpp index 7819416..e465fec 100644 --- a/src/ABGlobalScript.cpp +++ b/src/ABGlobalScript.cpp @@ -2,6 +2,7 @@ #include "ABConfig.h" #include "ABMapInfo.h" +#include "ABUtils.h" void AutoBalance_GlobalScript::OnAfterUpdateEncounterState(Map* map, EncounterCreditType type, uint32 /*creditEntry*/, Unit* /*source*/, Difficulty /*difficulty_fixed*/, DungeonEncounterList const* /*encounters*/, uint32 /*dungeonCompleted*/, bool updated) { @@ -11,7 +12,7 @@ void AutoBalance_GlobalScript::OnAfterUpdateEncounterState(Map* map, EncounterCr if (!rewardEnabled || !updated) return; - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(map); if (mapABInfo->adjustedPlayerCount < MinPlayerReward) return; diff --git a/src/ABMapInfo.h b/src/ABMapInfo.h index e678ba2..b6ba23a 100644 --- a/src/ABMapInfo.h +++ b/src/ABMapInfo.h @@ -57,6 +57,6 @@ class AutoBalanceMapInfo : public DataMap::Base uint8 levelScalingDynamicFloor = 0; // How many levels LESS than the highestPlayerLevel creature should be scaled to uint8 prevMapLevel = 0; // Used to reduce calculations when they are not necessary + bool initialized = false; // Whether or not the map has been initialized }; - #endif diff --git a/src/ABPlayerScript.cpp b/src/ABPlayerScript.cpp index cb3c908..d802ede 100644 --- a/src/ABPlayerScript.cpp +++ b/src/ABPlayerScript.cpp @@ -32,7 +32,7 @@ void AutoBalance_PlayerScript::OnPlayerLevelChanged(Player* player, uint8 oldlev UpdateMapPlayerStats(map); // schedule all creatures for an update - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(map); mapABInfo->mapConfigTime = GetCurrentConfigTime(); } @@ -44,7 +44,7 @@ void AutoBalance_PlayerScript::OnPlayerGiveXP(Player* player, uint32& amount, Un if (!map->IsDungeon() || !map->GetInstanceId() || !victim) return; - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(map); if (victim && RewardScalingXP && mapABInfo->enabled) { @@ -81,7 +81,7 @@ void AutoBalance_PlayerScript::OnPlayerBeforeLootMoney(Player* player, Loot* loo if (!map->IsDungeon()) return; - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(map); ObjectGuid sourceGuid = loot->sourceWorldObjectGUID; if (mapABInfo->enabled && RewardScalingMoney) @@ -136,7 +136,7 @@ void AutoBalance_PlayerScript::OnPlayerEnterCombat(Player* player, Unit* /*enemy LOG_DEBUG("module.AutoBalance_CombatLocking", "AutoBalance_PlayerScript::OnPlayerEnterCombat: {} enters combat.", player->GetName()); - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(map); // if this map isn't enabled, no work to do if (!mapABInfo->enabled) @@ -175,7 +175,7 @@ void AutoBalance_PlayerScript::OnPlayerLeaveCombat(Player* player) // unfortunately, `player->IsInCombat()` doesn't work here LOG_DEBUG("module.AutoBalance_CombatLocking", "AutoBalance_PlayerScript::OnPlayerLeaveCombat: {} leaves (or wasn't in) combat.", player->GetName()); - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(map); // if this map isn't enabled, no work to do if (!mapABInfo->enabled) diff --git a/src/ABUnitScript.cpp b/src/ABUnitScript.cpp index 3473459..aba1126 100644 --- a/src/ABUnitScript.cpp +++ b/src/ABUnitScript.cpp @@ -3,6 +3,7 @@ #include "ABConfig.h" #include "ABCreatureInfo.h" #include "ABMapInfo.h" +#include "ABUtils.h" void AutoBalance_UnitScript::ModifyPeriodicDamageAurasTick(Unit* target, Unit* source, uint32& amount, SpellInfo const* spellInfo) { @@ -253,8 +254,8 @@ int32 AutoBalance_UnitScript::_Modify_Damage_Healing(Unit* target, Unit* source, } // get the maps' info - AutoBalanceMapInfo* sourceMapABInfo = source->GetMap()->CustomData.GetDefault("AutoBalanceMapInfo"); - AutoBalanceMapInfo* targetMapABInfo = target->GetMap()->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* sourceMapABInfo = GetMapInfo(source->GetMap()); + AutoBalanceMapInfo* targetMapABInfo = GetMapInfo(target->GetMap()); // if either the target or the source's maps are not enabled, return the original damage if (!sourceMapABInfo->enabled || !targetMapABInfo->enabled) diff --git a/src/ABUtils.cpp b/src/ABUtils.cpp index 59bdebf..a4d5240 100644 --- a/src/ABUtils.cpp +++ b/src/ABUtils.cpp @@ -37,7 +37,7 @@ void AddCreatureToMapCreatureList(Creature* creature, bool addToCreatureList, bo Map* map = creature->GetMap(); InstanceMap* instanceMap = map->ToInstanceMap(); - AutoBalanceMapInfo* mapABInfo = instanceMap->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(instanceMap); AutoBalanceCreatureInfo* creatureABInfo = creature->CustomData.GetDefault("AutoBalanceCreatureInfo"); // @@ -494,7 +494,7 @@ void RemoveCreatureFromMapData(Creature* creature) // Get map data // - AutoBalanceMapInfo *mapABInfo = creature->GetMap()->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo *mapABInfo = GetMapInfo(creature->GetMap()); // // If the creature is in the all creature list, remove it @@ -626,7 +626,7 @@ float getDefaultMultiplier(Map* map, AutoBalanceInflectionPointSettings inflecti // // Get the adjustedPlayerCount for this instance // - AutoBalanceMapInfo *mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo *mapABInfo = GetMapInfo(map); float adjustedPlayerCount = mapABInfo->adjustedPlayerCount; // @@ -684,7 +684,7 @@ World_Multipliers getWorldMultiplier(Map* map, BaseValueType baseValueType) // // Grab map data // - AutoBalanceMapInfo *mapABInfo=map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo *mapABInfo = GetMapInfo(map); // // If the map isn't enabled, return defaults @@ -1723,7 +1723,7 @@ bool isCreatureRelevant(Creature* creature) // get the creature's map's info Map* creatureMap = creature->GetMap(); - AutoBalanceMapInfo *mapABInfo = creatureMap->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo *mapABInfo = GetMapInfo(creatureMap); InstanceMap* instanceMap = creatureMap->ToInstanceMap(); // if this creature is in the dungeon's base map, make no changes @@ -2037,7 +2037,7 @@ void LoadMapSettings(Map* map) // Load (or create) the map's info // - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(map); // // Create an InstanceMap object @@ -2391,7 +2391,7 @@ void UpdateMapPlayerStats(Map* map) // Get the map's info // - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(map); InstanceMap* instanceMap = map->ToInstanceMap(); // @@ -2584,7 +2584,7 @@ void AddPlayerToMap(Map* map, Player* player) // Get map data // - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(map); if (!player) @@ -2638,7 +2638,7 @@ bool RemovePlayerFromMap(Map* map, Player* player) // Get map data // - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(map); // // If this player isn't in the map's player list, skip @@ -2679,7 +2679,7 @@ bool UpdateMapDataIfNeeded(Map* map, bool force) // Get map data // - AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + AutoBalanceMapInfo* mapABInfo = GetMapInfo(map); // // If map needs update @@ -2996,3 +2996,72 @@ bool UpdateMapDataIfNeeded(Map* map, bool force) return false; } } + +AutoBalanceMapInfo* GetMapInfo(Map* map) +{ + AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault("AutoBalanceMapInfo"); + if (mapABInfo->initialized) + return mapABInfo; + + LOG_DEBUG("module.AutoBalance", "AutoBalance::InitializeMap: Map {} ({}{}) | Initializing map.", + map->GetMapName(), + map->GetId(), + map->GetInstanceId() ? "-" + std::to_string(map->GetInstanceId()) : ""); + + mapABInfo->initialized = true; + + if (!map->IsDungeon()) + return mapABInfo; + + // get the map's LFG stats even if not enabled + LFGDungeonEntry const* dungeon = GetLFGDungeon(map->GetId(), map->GetDifficulty()); + if (dungeon) { + mapABInfo->lfgMinLevel = dungeon->MinLevel; + mapABInfo->lfgMaxLevel = dungeon->MaxLevel; + mapABInfo->lfgTargetLevel = dungeon->TargetLevel; + } + // if this is a heroic dungeon that isn't in LFG, get the stats from the non-heroic version + else if (map->IsHeroic()) + { + LFGDungeonEntry const* nonHeroicDungeon = nullptr; + if (map->GetDifficulty() == DUNGEON_DIFFICULTY_HEROIC) + nonHeroicDungeon = GetLFGDungeon(map->GetId(), DUNGEON_DIFFICULTY_NORMAL); + else if (map->GetDifficulty() == RAID_DIFFICULTY_10MAN_HEROIC) + nonHeroicDungeon = GetLFGDungeon(map->GetId(), RAID_DIFFICULTY_10MAN_NORMAL); + else if (map->GetDifficulty() == RAID_DIFFICULTY_25MAN_HEROIC) + nonHeroicDungeon = GetLFGDungeon(map->GetId(), RAID_DIFFICULTY_25MAN_NORMAL); + + LOG_DEBUG("module.AutoBalance", "AutoBalance::InitializeMap: Map {} ({}{}) | is a Heroic dungeon that is not in LFG. Using non-heroic LFG levels.", + map->GetMapName(), + map->GetId(), + map->GetInstanceId() ? "-" + std::to_string(map->GetInstanceId()) : "" + ); + + if (nonHeroicDungeon) + { + mapABInfo->lfgMinLevel = nonHeroicDungeon->MinLevel; + mapABInfo->lfgMaxLevel = nonHeroicDungeon->MaxLevel; + mapABInfo->lfgTargetLevel = nonHeroicDungeon->TargetLevel; + } + else + { + LOG_ERROR("module.AutoBalance", "AutoBalance::InitializeMap: Map {} ({}{}) | Could not determine LFG level ranges for this map. Level will bet set to 0.", + map->GetMapName(), + map->GetId(), + map->GetInstanceId() ? "-" + std::to_string(map->GetInstanceId()) : "" + ); + } + } + + LOG_DEBUG("module.AutoBalance", "AutoBalance::InitializeMap: Map {} ({}{}) | LFG levels ({}-{}) (target {}). {} for AutoBalancing.", + map->GetMapName(), + map->GetId(), + map->GetInstanceId() ? "-" + std::to_string(map->GetInstanceId()) : "", + mapABInfo->lfgMinLevel ? std::to_string(mapABInfo->lfgMinLevel) : "?", + mapABInfo->lfgMaxLevel ? std::to_string(mapABInfo->lfgMaxLevel) : "?", + mapABInfo->lfgTargetLevel ? std::to_string(mapABInfo->lfgTargetLevel) : "?", + mapABInfo->enabled ? "Enabled" : "Disabled" + ); + + return mapABInfo; +} diff --git a/src/ABUtils.h b/src/ABUtils.h index 269afbd..7e9124b 100644 --- a/src/ABUtils.h +++ b/src/ABUtils.h @@ -7,6 +7,7 @@ #include "ABInflectionPointSettings.h" #include "ABLevelScalingDynamicLevelSettings.h" +#include "ABMapInfo.h" #include "ABStatModifiers.h" #include "AutoBalance.h" @@ -58,5 +59,6 @@ void UpdateMapPlayerStats (Map* map); void AddPlayerToMap(Map* map, Player* player); bool RemovePlayerFromMap(Map* map, Player* player); bool UpdateMapDataIfNeeded(Map* map, bool force = false); +AutoBalanceMapInfo* GetMapInfo(Map* map); #endif