Implement dynamic clientless free-flight travel

Integrate transient flight alternatives with graph routing, native flying mounts, phased terrain and collision checks, bounded avoidance and confirmed landing. Add safe-return recovery, node and location integration, profile settings, diagnostic commands, regression tests and flight documentation.
This commit is contained in:
Nico committed 2026-10-26 17:35:58 +01:00
1 parent 4474714c00
commit 8df9591c99
21 files changed
+1536 -16

No files matched your search

@@ -0,0 +1,501 @@
#include "ClientlessFlightMgr.h"
#include "ClientlessLuaEngine.h"
#include "ClientlessTravelMgr.h"
#include "ClientlessActionExecutorMgr.h"
#include "ClientlessPlayerMgr.h"
#include "ClientlessRouteRecorderMgr.h"
#include "DatabaseEnv.h"
#include "BattlefieldMgr.h"
#include "Battlefield.h"
#include "Chat.h"
#include "Config.h"
#include "DBCStores.h"
#include "Log.h"
#include "Map.h"
#include "MoveSplineInit.h"
#include "PhasingHandler.h"
#include "Player.h"
#include "PathGenerator.h"
#include "Random.h"
#include "SpellInfo.h"
#include "SpellMgr.h"
#include "TerrainMgr.h"
#include "VMapFactory.h"
#include "IVMapManager.h"
#include "VMapManager2.h"
#include "World.h"
#include "WorldSession.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <sstream>
namespace
{
using namespace ClientlessFlightPath;
Point PointOf(Position const & p)
{
return
{
p.GetPositionX(), p.GetPositionY(), p.GetPositionZ()
};
}
float Infinity()
{
return std::numeric_limits < float >::infinity();
}
bool ClearRay(Player * player, Point const & a, Point const & b)
{
Map * map = player->GetMap();
float x, y, z;
return map->isInLineOfSight(player->GetPhaseShift(), a.X, a.Y, a.Z, b.X, b.Y, b.Z, LINEOFSIGHT_ALL_CHECKS, VMAP::ModelIgnoreFlags(0)) && ! map->getObjectHitPos(player->GetPhaseShift(), a.X, a.Y, a.Z, b.X, b.Y, b.Z, x, y, z, 0);
}
char const * StateName(ClientlessFlightState state)
{
switch (state)
{
case ClientlessFlightState::Mounting : return "Mounting";
case ClientlessFlightState::Flying : return "Flying";
case ClientlessFlightState::Arrived : return "Arrived";
case ClientlessFlightState::Failed : return "Failed";
}
return "Unknown";
}
uint32 FlightType(SpellInfo const * spell)
{
if (spell) for (auto const & effect : spell->Effects) if (effect.ApplyAuraName == SPELL_AURA_MOUNTED) return uint32(effect.MiscValueB);
return 0;
}
}
ClientlessFlightMgr * ClientlessFlightMgr::instance()
{
static ClientlessFlightMgr mgr;
return & mgr;
}
void ClientlessFlightMgr::BeginPlanning()
{
_planningAreas.clear();
_planningTerrain.clear();
}
std::pair < uint32, uint32 > ClientlessFlightMgr::AreaZone(Player const * player, uint32 map, Position const & point) const
{
std::string key = std::to_string(map) + ":" + std::to_string(point.GetPositionX()) + ":" + std::to_string(point.GetPositionY()) + ":" + std::to_string(point.GetPositionZ());
auto found = _planningAreas.find(key);
if (found != _planningAreas.end()) return found->second;
auto & terrain = _planningTerrain[map];
if (! terrain) terrain = sTerrainMgr.LoadTerrain(map);
uint32 zone = 0, area = 0;
if (terrain) terrain->GetZoneAndAreaId(player->GetPhaseShift(), zone, area, map, point);
return _planningAreas.emplace(key, std::make_pair(area, zone)).first->second;
}
bool ClientlessFlightMgr::Enabled(Player const * player) const
{
auto const * profile = sClientlessLuaEngine->GetAdventureProfile(player->GetGUID().GetCounter());
if (! sConfigMgr->GetBoolDefault("Clientless.Flight.Enabled", true) || ! profile || ! profile->CanFreeFly || player->GetSkillValue(SKILL_RIDING) < 225) return false;
for (auto const & mount : profile->FlightMounts) if (player->HasSpell(mount.DisplayId) && FlightType(sSpellMgr->GetSpellInfo(mount.DisplayId))) return true;
return false;
}
bool ClientlessFlightMgr::Preferred(Player const * player, uint32 map, Position const & position) const
{
std::istringstream list(sConfigMgr->GetStringDefault("Clientless.Flight.PreferredZones", "210 4197"));
uint32 zone = AreaZone(player, map, position).second, candidate;
while (list >> candidate) if (zone == candidate) return true;
return false;
}
float ClientlessFlightMgr::Estimate(Player const * player, uint32 map, Position const & from, Position const & to) const
{
auto const * entry = sMapStore.LookupEntry(map);
if (! Enabled(player) || ! entry || entry->MapType != MAP_COMMON) return Infinity();
if ((map == 0 || map == 1) && ! player->HasSpell(90267)) return Infinity();
if (map == 571 && ! player->HasSpell(54197)) return Infinity();
uint32 const area = AreaZone(player, map, from).first;
if (auto const * data = sAreaTableStore.LookupEntry(area))
{
bool const dynamicRules = data->World_pvp_ID != 0 ||(map == player->GetMapId() && area == player->GetAreaId() && player->HasAuraType(SPELL_AURA_MOUNT_RESTRICTIONS));
if (! dynamicRules && ! data->GetMountFlags().HasFlag(AreaMountFlags::AllowFlyingMounts)) return Infinity();
}
// Unknown terrain permission is an estimate only; native mount capability
// and spell checks at the actual departure are mandatory.
bool const preferred = Preferred(player, map, from) || Preferred(player, map, to);
float const distance = Distance(PointOf(from), PointOf(to));
float minimum = preferred ? 60.0f : std::max(100.0f, sConfigMgr->GetFloatDefault("Clientless.Flight.MinDistance", 600));
if (! std::isfinite(distance) || distance < minimum || distance > 20000) return Infinity();
return preferred ? 40 + distance * 0.4f : 180 + distance * 1.5f;
}
ClientlessFlightStatus const * ClientlessFlightMgr::GetStatus(uint32 guid) const
{
auto i = _records.find(guid);
return i == _records.end() ? nullptr : & i->second.Status;
}
void ClientlessFlightMgr::Clear(uint32 guid)
{
_records.erase(guid);
}
Surface ClientlessFlightMgr::Sample(Player * player, Record & record, float x, float y) const
{
Map * map = player->GetMap();
uint32 terrain = PhasingHandler::GetTerrainMapId(player->GetPhaseShift(), map->GetId(), map->GetTerrain(), x, y);
std::string key = std::to_string(terrain) + ":" + std::to_string(int(std::floor(x / 533.33333f))) + ":" + std::to_string(int(std::floor(y / 533.33333f)));
auto found = record.Tiles.find(key);
if (found == record.Tiles.end()) found = record.Tiles.emplace(key, TerrainMgr::ExistMapAndVMap(terrain, x, y)).first;
if (! found->second) return
{
};
map->LoadGrid(x, y);
// terrain/VMAP and dynamic objects before ray queries
float ground = map->GetHeight(player->GetPhaseShift(), x, y, MAX_HEIGHT, true, MAX_HEIGHT);
float water = map->GetWaterLevel(player->GetPhaseShift(), x, y);
if (std::isfinite(water) && water > ground) ground = water;
float ceiling = map->GetTerrain()->GetMaxHeight(player->GetPhaseShift(), map->GetId(), x, y);
return
{
ground, ceiling, std::isfinite(ground) && std::isfinite(ceiling) && ground > INVALID_HEIGHT + 1 && ceiling > ground + 5
};
}
bool ClientlessFlightMgr::Start(Player * player, Position const & target, std::string & error)
{
if (! Trinity::IsValidMapCoord(target.GetPositionX(), target.GetPositionY(), target.GetPositionZ()))
{
error = "invalid free-flight destination coordinates";
return false;
}
if (! player || ! player->IsInWorld() || ! Enabled(player) || ! player->IsAlive() || player->GetMap()->GetEntry()->MapType != MAP_COMMON || player->GetTransport() || player->IsInFlight() || player->IsBeingTeleported() || player->IsInCombat() || ! player->IsOutdoors() || player->IsInWater() || player->IsFalling())
{
error = "free flight unavailable in current player/location state";
return false;
}
auto * vmaps = VMAP::VMapFactory::createOrGetVMapManager();
if (! vmaps->isLineOfSightCalcEnabled() || ! vmaps->isHeightCalcEnabled())
{
error = "free flight requires native VMAP height and collision checks";
return false;
}
if (auto * battlefield = sBattlefieldMgr->GetBattlefieldToZoneId(player->GetMap(), player->GetZoneId())) if (! battlefield->CanFlyIn())
{
error = "battlefield currently forbids flying";
return false;
}
auto const * profile = sClientlessLuaEngine->GetAdventureProfile(player->GetGUID().GetCounter());
std::vector < std::pair < uint32, uint32 >> candidates;
uint32 total = 0;
for (auto const & mount : profile->FlightMounts)
{
if (! player->HasSpell(mount.DisplayId)) continue;
uint32 type = FlightType(sSpellMgr->GetSpellInfo(mount.DisplayId));
auto const * capability = player->GetMountCapability(type);
if (! capability || !(capability->Flags & MOUNT_CAPABILITY_FLAG_FLYING)) continue;
uint32 weight = std::min(mount.Weight, uint32(10000));
total += weight;
candidates.emplace_back(mount.DisplayId, weight);
}
if (! total)
{
error = "no learned profile mount with native flying capability here";
return false;
}
uint32 roll = urand(1, total), spell = 0;
for (auto const & candidate : candidates)
{
spell = candidate.first;
if (roll <= candidate.second) break;
roll -= candidate.second;
}
Record record;
record.Status.MapId = player->GetMapId();
record.Status.Target = target;
record.Start.Relocate(player);
record.Settings.Clearance = std::clamp(sConfigMgr->GetFloatDefault("Clientless.Flight.Clearance", 60), 20.0f, 200.0f);
auto origin = Sample(player, record, player->GetPositionX(), player->GetPositionY());
if (! origin.Valid || std::fabs(origin.Ground - player->GetPositionZ()) > 3)
{
error = "unknown flight bounds or no safe ground departure";
return false;
}
bool foundLanding = false;
for (float radius :
{
0.0f, 6.0f, 12.0f, 20.0f
})
{
for (unsigned i = 0; i <(radius == 0 ? 1u : 12u); ++ i)
{
float angle = float(i) * 6.283185307f / 12;
float x = target.GetPositionX() + radius * std::cos(angle), y = target.GetPositionY() + radius * std::sin(angle);
auto landing = Sample(player, record, x, y);
if (! landing.Valid || std::fabs(landing.Ground - target.GetPositionZ()) > 5) continue;
float solid = player->GetMap()->GetHeight(player->GetPhaseShift(), x, y, MAX_HEIGHT, true, MAX_HEIGHT);
if (landing.Ground > solid + 0.7f) continue;
// water is no solid touchdown
Point bottom
{
x, y, landing.Ground + 1
}, top = bottom;
top.Z += record.Settings.Clearance;
if (! ClearRay(player, bottom, top)) continue;
Position candidate;
candidate.Relocate(x, y, landing.Ground, target.GetOrientation());
if (radius > 0 || std::fabs(landing.Ground - target.GetPositionZ()) > 0.7f)
{
auto path = PathGenerator::AnalyzePositions(player->GetMapId(), player->GetMapId(), player->GetInstanceId(), candidate, target, NAV_GROUND);
if (! path.IsComplete() || path.FinalDistance > 1.5f || path.PathLength > 120) continue;
}
record.Status.Landing = candidate;
foundLanding = true;
break;
}
if (foundLanding) break;
}
if (! foundLanding)
{
error = "no free solid landing with a validated short target approach";
return false;
}
sClientlessTravelMgr->CancelTravel(player->GetGUID().GetCounter());
player->StopMoving();
sClientlessLuaEngine->DismountGroundMount(player, "free-flight-takeoff");
player->RemoveAurasByType(SPELL_AURA_MOUNTED);
record.Status.State = ClientlessFlightState::Mounting;
record.Status.MountSpell = spell;
record.Status.Detail = "waiting for native flying mount";
_records[player->GetGUID().GetCounter()] = std::move(record);
if (player->CastSpell(player, spell, false) != SPELL_CAST_OK)
{
Fail(player, _records.at(player->GetGUID().GetCounter()), "native mount spell rejected");
error = "native mount spell rejected";
return false;
}
TC_LOG_INFO("server.adventurebot.navigation", "FLIGHT_START guid=%u map=%u spell=%u target=(%.2f %.2f %.2f)", player->GetGUID().GetCounter(), player->GetMapId(), spell, target.GetPositionX(), target.GetPositionY(), target.GetPositionZ());
return true;
}
void ClientlessFlightMgr::Fail(Player * player, Record & record, std::string const & reason)
{
player->StopMoving();
record.Moving = false;
if (record.Status.State == ClientlessFlightState::Mounting) player->InterruptNonMeleeSpells(false);
if (! record.Returning && record.Status.State == ClientlessFlightState::Flying && player->CanFly() && player->IsAlive() && player->GetMapId() == record.Status.MapId && ! record.Breadcrumbs.empty())
{
record.Returning = true;
record.AbortReason = reason;
record.Status.Detail = "aborting: returning along checked flight trail";
TC_LOG_WARN("server.adventurebot.navigation", "FLIGHT_RETURN guid=%u reason='%s'", player->GetGUID().GetCounter(), reason.c_str());
return;
}
record.Status.State = ClientlessFlightState::Failed;
record.Status.Detail = reason;
// If even the reverse corridor is blocked, remain mounted and stopped;
// never fabricate a landing or drop the player from an unknown altitude.
TC_LOG_WARN("server.adventurebot.navigation", "FLIGHT_FAILED guid=%u map=%u pos=(%.2f %.2f %.2f) reason='%s'", player->GetGUID().GetCounter(), player->GetMapId(), player->GetPositionX(), player->GetPositionY(), player->GetPositionZ(), reason.c_str());
}
bool ClientlessFlightMgr::Advance(Player * player, Record & record)
{
Point current = PointOf(* player), goal = PointOf(record.Returning ? record.Start : record.Status.Landing);
auto sample =[&](float x, float y)
{
return Sample(player, record, x, y);
};
auto ray =[&](Point const & a, Point const & b)
{
return ClearRay(player, a, b);
};
if (Horizontal(current, goal) < 1.5f && std::fabs(current.Z - goal.Z) < 0.7f)
{
auto floor = sample(current.X, current.Y);
if (! floor.Valid || std::fabs(current.Z - floor.Ground) > 0.7f)
{
Fail(player, record, "touchdown ground not confirmed");
return false;
}
player->StopMoving();
player->RemoveUnitMovementFlag(MOVEMENTFLAG_FLYING | MOVEMENTFLAG_ASCENDING | MOVEMENTFLAG_DESCENDING);
player->RemoveAurasByType(SPELL_AURA_MOUNTED);
record.Status.State = record.Returning ? ClientlessFlightState::Failed : ClientlessFlightState::Arrived;
record.Status.Detail = record.Returning ? "safely returned after abort: " + record.AbortReason : "landed on validated destination ground";
TC_LOG_INFO("server.adventurebot.navigation", "FLIGHT_LANDED guid=%u segments=%u avoidances=%u travelled=%.2f error=%.2f", player->GetGUID().GetCounter(), record.Status.Segments, record.Status.Avoidances, record.Status.Travelled, Distance(current, goal));
return true;
}
ClientlessFlightPath::Result next;
if (record.Returning)
{
while (! record.Breadcrumbs.empty() && Distance(current, PointOf(record.Breadcrumbs.back())) < 0.7f) record.Breadcrumbs.pop_back();
Point back = record.Breadcrumbs.empty() ? goal : PointOf(record.Breadcrumbs.back());
next =
{
back, Corridor(current, back, goal, record.Settings, sample, ray, true), true, false
};
}
else next = Next(current, goal, record.Settings, record.Memory, sample, ray);
if (! next.Valid ||(! record.Returning && record.Status.Travelled > Distance(PointOf(record.Start), goal) * 2 + 500))
{
Fail(player, record, "no safe bounded flight corridor; holding position mounted");
return false;
}
if (! record.Returning) record.Breadcrumbs.push_back(* player);
record.Segment.Relocate(next.Next.X, next.Next.Y, next.Next.Z, player->GetOrientation());
player->AddUnitMovementFlag(MOVEMENTFLAG_FLYING);
Movement::MoveSplineInit init(player);
init.MoveTo(next.Next.X, next.Next.Y, next.Next.Z, false);
init.SetFly();
init.SetWalk(false);
init.SetVelocity(player->GetSpeed(MOVE_FLIGHT));
if (init.Launch() <= 0)
{
Fail(player, record, "flight spline rejected");
return false;
}
record.Moving = true;
record.SegmentElapsed = 0;
++ record.Status.Segments;
record.Status.Avoidances += next.Avoiding ? 1 : 0;
record.Status.Travelled += Distance(current, next.Next);
record.Status.Detail = next.Avoiding ? "obstacle avoidance" : next.Landing ? "landing approach" : next.Next.Z < current.Z - 0.25f ? "diagonal descent" : next.Next.Z > current.Z + 0.25f ? "diagonal climb" : "terrain-smoothed cruise";
TC_LOG_INFO("server.adventurebot.navigation", "FLIGHT_SEGMENT guid=%u n=%u avoid=%u target=(%.2f %.2f %.2f) detail='%s'", player->GetGUID().GetCounter(), record.Status.Segments, next.Avoiding ? 1 : 0, next.Next.X, next.Next.Y, next.Next.Z, record.Status.Detail.c_str());
return true;
}
void ClientlessFlightMgr::Update(uint32 diff)
{
for (auto &[guid, record] : _records)
{
if (record.Status.State == ClientlessFlightState::Arrived || record.Status.State == ClientlessFlightState::Failed) continue;
auto * session = sWorld->FindClientlessSession(guid);
auto * player = session ? session->GetPlayer() : nullptr;
if (! player || ! player->IsInWorld() || sWorld->GetClientlessPlayerState(guid) != ClientlessPlayerState::Online) continue;
record.Elapsed += std::min(diff, uint32(30000));
record.SegmentElapsed += std::min(diff, uint32(30000));
if (player->GetMapId() != record.Status.MapId || ! player->IsAlive() || player->IsBeingTeleported())
{
Fail(player, record, "flight player/map state changed");
continue;
}
if (record.Status.State == ClientlessFlightState::Mounting)
{
if (player->IsMounted() && player->CanFly()) record.Status.State = ClientlessFlightState::Flying;
else
{
if (record.Elapsed > 8000) Fail(player, record, "native mount did not grant flight");
continue;
}
}
if (! player->CanFly() || record.Elapsed >(record.Returning ? 2100000u : 1800000u))
{
Fail(player, record, "flight permission lost or total timeout");
continue;
}
if (record.CheckTimer > diff)
{
record.CheckTimer -= diff;
continue;
}
record.CheckTimer = 200;
if (record.Moving && player->IsSplineEnabled())
{
if (record.SegmentElapsed > 10000)
{
Fail(player, record, "flight segment made no confirmed progress");
continue;
}
auto ray =[&](Point const & a, Point const & b)
{
return ClearRay(player, a, b);
};
auto sample =[&](float x, float y)
{
return Sample(player, record, x, y);
};
// Recheck the remaining corridor as moving GO geometry changes.
if (! Corridor(PointOf(* player), PointOf(record.Segment), PointOf(record.Returning ? record.Start : record.Status.Landing), record.Settings, sample, ray, true))
{
player->StopMoving();
record.Moving = false;
Advance(player, record);
}
continue;
}
record.Moving = false;
Advance(player, record);
}
}
bool ClientlessFlightMgr::Command(ChatHandler * handler, char const * args)
{
std::istringstream in(args ? args : "");
std::string op;
uint32 guid = 0;
in >> op >> guid;
auto const * status = GetStatus(guid);
if (op == "abort")
{
auto * session = sWorld->FindClientlessSession(guid);
auto * player = session ? session->GetPlayer() : nullptr;
auto found = _records.find(guid);
if (player && found != _records.end() &&(found->second.Status.State == ClientlessFlightState::Mounting || found->second.Status.State == ClientlessFlightState::Flying))
{
if (found->second.Status.State == ClientlessFlightState::Mounting) player->InterruptNonMeleeSpells(false);
Fail(player, found->second, "operator requested safe return");
handler->SendSysMessage("Flight abort requested. An airborne player attempts the checked reverse trail before dismounting.");
}
else handler->SendSysMessage("No active free flight to abort.");
return true;
}
if (op == "probe")
{
float x, y;
auto * session = sWorld->FindClientlessSession(guid);
auto * player = session ? session->GetPlayer() : nullptr;
if (! player || !(in >> x >> y) || ! std::isfinite(x) || ! std::isfinite(y) || std::fabs(x) > 17000 || std::fabs(y) > 17000)
{
handler->SendSysMessage("Usage: .clientless flight probe <onlineGuid> <x> <y>");
return true;
}
Record probe;
auto surface = Sample(player, probe, x, y);
handler->PSendSysMessage("Flight surface map=%u x=%.3f y=%.3f valid=%u ground=%.3f ceiling=%.3f", player->GetMapId(), x, y, surface.Valid ? 1u : 0u, surface.Ground, surface.Ceiling);
return true;
}
if (op == "test_node")
{
std::string node;
in >> node;
auto * session = sWorld->FindClientlessSession(guid);
auto * player = session ? session->GetPlayer() : nullptr;
auto const * action = sClientlessActionExecutorMgr->GetStatus(guid);
if (! player || ! sClientlessPlayerMgr->IsManaged(guid) || ! ClientlessRouteRecorderMgr::IsSafeKey(node) ||(action &&(action->State == ClientlessActionExecutionState::Moving || action->State == ClientlessActionExecutionState::SelectingTarget)) ||(status &&(status->State == ClientlessFlightState::Mounting || status->State == ClientlessFlightState::Flying)))
{
handler->SendSysMessage("Use an idle online CPC player and a valid destination node.");
return true;
}
QueryResult target = WorldDatabase.PQuery("SELECT w.map,w.position_x,w.position_y,w.position_z FROM vcore_clientless_travel_node n JOIN vcore_clientless_world_point w ON w.point_key=n.world_point_key WHERE n.node_key='%s' AND n.enabled=1 AND w.enabled=1", node.c_str());
QueryResult origin = WorldDatabase.PQuery("SELECT n.node_key FROM vcore_clientless_travel_node n JOIN vcore_clientless_world_point w ON w.point_key=n.world_point_key WHERE n.enabled=1 AND w.enabled=1 AND w.map=%u AND POW(w.position_x-%.6f,2)+POW(w.position_y-%.6f,2)+POW(w.position_z-%.6f,2)<=9 LIMIT 1", player->GetMapId(), player->GetPositionX(), player->GetPositionY(), player->GetPositionZ());
if (! target || ! origin || target->Fetch()[0].GetUInt32() != player->GetMapId())
{
handler->SendSysMessage("Flight test requires a real departure node within 3 yards and a destination node on the same map.");
return true;
}
auto * fields = target->Fetch();
Position point;
point.Relocate(fields[1].GetFloat(), fields[2].GetFloat(), fields[3].GetFloat());
std::string error;
bool ok = Start(player, point, error);
handler->PSendSysMessage("Isolated free-flight test: %s %s", ok ? "started" : "rejected", error.c_str());
return true;
}
if (op != "status" || ! guid)
{
handler->SendSysMessage("Usage: .clientless flight status <guid> OR test_node <guid> <node>. test_node isolates flight execution; normal trips use the planner.");
return false;
}
if (! status) handler->SendSysMessage("No free-flight state for this character.");
else handler->PSendSysMessage("Flight GUID %u: state=%s map=%u mountSpell=%u segments=%u avoidances=%u travelled=%.1f detail='%s'", guid, StateName(status->State), status->MapId, status->MountSpell, status->Segments, status->Avoidances, status->Travelled, status->Detail.c_str());
return true;
}