Create Flightmastercache to handle finding the nearest flight master. (#1979)

Refactor the flightmastercache the bots use for finding the nearest
available flight master.

I made a small change to how the original function worked by storing the
database position for the flightmaster in the cache itself. This allows
us to calculate the distance from bot before accessing the creature
object, should be faster overall.
This commit is contained in:
Keleborn
2026-01-23 11:36:57 -08:00
committed by GitHub
parent bf456ee07f
commit 3e21563669
5 changed files with 74 additions and 24 deletions

View File

@@ -3,6 +3,7 @@
#include "BroadcastHelper.h"
#include "ChatHelper.h"
#include "Creature.h"
#include "FlightMasterCache.h"
#include "G3D/Vector2.h"
#include "GameObject.h"
#include "GossipDef.h"
@@ -968,22 +969,7 @@ WorldPosition NewRpgBaseAction::SelectRandomCampPos(Player* bot)
bool NewRpgBaseAction::SelectRandomFlightTaxiNode(ObjectGuid& flightMaster, uint32& fromNode, uint32& toNode)
{
const std::vector<uint32>& flightMasters = IsAlliance(bot->getRace())
? sRandomPlayerbotMgr->allianceFlightMasterCache
: sRandomPlayerbotMgr->hordeFlightMasterCache;
Creature* nearestFlightMaster = nullptr;
for (const uint32& guid : flightMasters)
{
Creature* flightMaster = ObjectAccessor::GetSpawnedCreatureByDBGUID(bot->GetMapId(), guid);
if (!flightMaster)
continue;
if (bot->GetMapId() != flightMaster->GetMapId())
continue;
if (!nearestFlightMaster || bot->GetDistance(nearestFlightMaster) > bot->GetDistance(flightMaster))
nearestFlightMaster = flightMaster;
}
Creature* nearestFlightMaster = sFlightMasterCache->GetNearestFlightMaster(bot);
if (!nearestFlightMaster || bot->GetDistance(nearestFlightMaster) > 500.0f)
return false;

View File

@@ -26,6 +26,7 @@
#include "DatabaseEnv.h"
#include "Define.h"
#include "FleeManager.h"
#include "FlightMasterCache.h"
#include "GridNotifiers.h"
#include "GridNotifiersImpl.h"
#include "GuildMgr.h"
@@ -2002,14 +2003,12 @@ void RandomPlayerbotMgr::PrepareTeleportCache()
bool forAlliance = !(entry->hostileMask & 2);
if (tNpcflag & UNIT_NPC_FLAG_FLIGHTMASTER)
{
WorldPosition pos(mapId, x, y, z, orient);
if (forHorde)
{
hordeFlightMasterCache.push_back(guid);
}
sFlightMasterCache->AddHordeFlightMaster(guid, pos);
if (forAlliance)
{
allianceFlightMasterCache.push_back(guid);
}
sFlightMasterCache->AddAllianceFlightMaster(guid, pos);
}
const AreaTableEntry* area = sAreaTableStore.LookupEntry(map->GetAreaId(PHASEMASK_NORMAL, x, y, z));
uint32 zoneId = area->zone ? area->zone : area->ID;

View File

@@ -171,8 +171,7 @@ public:
std::map<uint8, std::vector<WorldLocation>> locsPerLevelCache;
std::map<uint8, std::vector<WorldLocation>> allianceStarterPerLevelCache;
std::map<uint8, std::vector<WorldLocation>> hordeStarterPerLevelCache;
std::vector<uint32> allianceFlightMasterCache;
std::vector<uint32> hordeFlightMasterCache;
struct LevelBracket {
uint32 low;
uint32 high;

View File

@@ -0,0 +1,39 @@
#include "FlightMasterCache.h"
void FlightMasterCache::AddHordeFlightMaster(uint32 entry, WorldPosition pos)
{
hordeFlightMasterCache[entry] = pos;
}
void FlightMasterCache::AddAllianceFlightMaster(uint32 entry, WorldPosition pos)
{
allianceFlightMasterCache[entry] = pos;
}
Creature* FlightMasterCache::GetNearestFlightMaster(Player* bot)
{
std::map<uint32, WorldPosition>& flightMasterCache =
(bot->GetTeamId() == ALLIANCE) ? allianceFlightMasterCache : hordeFlightMasterCache;
Creature* nearestFlightMaster = nullptr;
float nearestDistance = std::numeric_limits<float>::max();
for (auto const& [entry, pos] : flightMasterCache)
{
if (pos.GetMapId() == bot->GetMapId())
{
float distance = bot->GetExactDist2dSq(pos);
if (distance < nearestDistance)
{
Creature* flightMaster = ObjectAccessor::GetSpawnedCreatureByDBGUID(bot->GetMapId(), entry);
if (flightMaster)
{
nearestDistance = distance;
nearestFlightMaster = flightMaster;
}
}
}
}
return nearestFlightMaster;
}

View File

@@ -0,0 +1,27 @@
#ifndef _PLAYERBOT_FLIGHTMASTER_H
#define _PLAYERBOT_FLIGHTMASTER_H
#include "Creature.h"
#include "Player.h"
#include "TravelMgr.h"
class FlightMasterCache
{
public:
static FlightMasterCache* Instance()
{
static FlightMasterCache instance;
return &instance;
}
Creature* GetNearestFlightMaster(Player* bot);
void AddHordeFlightMaster(uint32 entry, WorldPosition pos);
void AddAllianceFlightMaster(uint32 entry, WorldPosition pos);
private:
std::map<uint32, WorldPosition> allianceFlightMasterCache;
std::map<uint32, WorldPosition> hordeFlightMasterCache;
};
#define sFlightMasterCache FlightMasterCache::Instance()
#endif