Skip to content

Commit

Permalink
Add proper bot count calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
sokie committed Nov 30, 2023
1 parent b82e8d1 commit 96ee144
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/AutoBalance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2551,6 +2551,19 @@ void UpdateMapPlayerStats(Map* map)
// minimum of 1 to prevent scaling weirdness when only GMs are in the instnace
mapABInfo->playerCount = mapABInfo->allMapPlayers.size() ? mapABInfo->allMapPlayers.size() : 1;

uint8 botCount = 0;

for (std::vector<Player*>::const_iterator playerIterator = mapABInfo->allMapPlayers.begin(); playerIterator != mapABInfo->allMapPlayers.end(); ++playerIterator)
{
Player* thisPlayer = *playerIterator;
if (thisPlayer->HaveBot()) {
botCount += thisPlayer->GetNpcBotsCount();
}
}

// add bots to player count
mapABInfo->playerCount += botCount;

LOG_DEBUG("module.AutoBalance", "AutoBalance::UpdateMapPlayerStats: Map {} ({}{}) | playerCount = ({}).",
instanceMap->GetMapName(),
instanceMap->GetId(),
Expand Down Expand Up @@ -4487,12 +4500,53 @@ class AutoBalance_GameObjectScript : public AllGameObjectScript

class AutoBalance_AllMapScript : public AllMapScript
{


//npcbot
class PlayersCountRecheckEvent : public BasicEvent
{
public:
explicit PlayersCountRecheckEvent(AutoBalance_AllMapScript* script, Map* map, Player const* player)
: _script(script), _map(map), _player(player) {}
PlayersCountRecheckEvent(PlayersCountRecheckEvent const&) = delete;

bool Execute(uint64 /*e_time*/, uint32 /*p_time*/)
{
if (_player->HaveBot())
_script->AfterBotsEnter(_map, _player);
return true;
}

private:
AutoBalance_AllMapScript* _script;
Map* _map;
Player const* _player;
};
//end npcbot

public:
AutoBalance_AllMapScript()
: AllMapScript("AutoBalance_AllMapScript")
{
}

//npcbot
void AfterBotsEnter(Map* map, Player const* player)
{
AutoBalanceMapInfo* mapABInfo = map->CustomData.GetDefault<AutoBalanceMapInfo>("AutoBalanceMapInfo");
uint32 old_player_count = mapABInfo->playerCount;
UpdateMapPlayerStats(map);
mapABInfo->mapConfigTime = GetCurrentConfigTime();
if (mapABInfo->enabled && PlayerChangeNotify && EnableGlobal && old_player_count != mapABInfo->playerCount) {
for (MapReference const& ref : map->GetPlayers()) {
if (Player const* playerHandle = ref.GetSource()) {
ChatHandler(playerHandle->GetSession()).PSendSysMessage("|cffFF0000 [AutoBalance+NPCBots]|r|cffFF8000 %s's bots entered %s. Auto setting player count to %i (Player Difficulty Offset = %i) |r", player->GetName().c_str(), map->GetMapName(), mapABInfo->playerCount + PlayerCountDifficultyOffset, PlayerCountDifficultyOffset);
}
}
}
}
//end npcbot

void OnCreateMap(Map* map)
{
LOG_DEBUG("module.AutoBalance", "AutoBalance_AllMapScript::OnCreateMap(): Map {} ({}{})",
Expand Down Expand Up @@ -4646,6 +4700,12 @@ class AutoBalance_AllMapScript : public AllMapScript
AddCreatureToMapCreatureList(*creatureIterator, false, true);
}

//npcbot: recalculate players count once all bots are teleported
//event will be automatically deleted if player teleports out of the map before execution
//max teleport delay for bot is 8000ms
player->m_Events.AddEvent(new PlayersCountRecheckEvent(this, map, player), player->m_Events.CalculateTime(8500));
//end npcbot

// Notify players of the change
if (PlayerChangeNotify && mapABInfo->enabled)
{
Expand Down

0 comments on commit 96ee144

Please sign in to comment.