mirror of
https://github.com/mod-playerbots/mod-playerbots.git
synced 2026-03-15 21:35:09 +00:00
Fix transport boarding when master is on a transport (Zep/Boats) (#1830)
Summary This PR improves Follow related behaviour when the master is on a transport (zeppelin/boat). It makes follow actions safer and less disruptive by: Detecting when the master is on a transport and handling boarding correctly Avoiding teleport-under-floor issues by using a small positional offset when teleporting the bot near the master Preventing movement conflicts between MoveSpline/MotionMaster and the transport driver by forcing a MotionMaster cleanup and MoveIdle after boarding Clearing movement flags (forward / walking) after boarding so the bot does not remain in a walking/march state Next-check delay after boarding to allow the server to update transport/position state Before this change, bots get stuck when attempting to board Fight the server-side transport movement because local MoveSpline/MotionMaster was still active Repeatedly attempt movement on every follow tick while already a passenger, causing jitter and CPU/noise This PR reduces stuck/jitter cases, avoids conflicting movement commands, and makes boarding more robust. **Key changes** Check master->GetTransport() and handle three main cases: If bot already passenger of same transport: stabilize (StopMoving, Clear(true), MoveIdle, StopMovingOnCurrentPos) and set a longer next-check delay; return false (no new movement in theory). If bot passenger of another transport: do nothing (avoid conflicting behaviour). If bot not a passenger of master transport: teleport bot near master (with offsets) and call Transport::AddPassenger(bot, true), then force: bot->StopMoving() bot->GetMotionMaster()->Clear(true) bot->GetMotionMaster()->MoveIdle() Remove movement flags MOVEMENTFLAG_FORWARD and MOVEMENTFLAG_WALKING SetNextCheckDelay to random 1000–2500 ms Log boarding with bot name, transport GUID and coordinates Preserve earlier follow logic when master is not on a transport Tests performed Manual tests on a local server: Master on boat/zeppelin -> bot teleports to a safe offset position and becomes a passenger without getting stuck Bot already passenger on same transport -> bot no longer issues movement commands and stabilizes Bot on a different transport -> no boarding attempt for master's transport (no interference) Movement flags cleared after boarding; bot stops local movement and does not fight server transport movement Now the bots follow their masters in the zeppelins and boats, although sometimes they move around a bit inside when the zeppelin starts (they must have smoked something bad). --------- Co-authored-by: Keleborn <22352763+Celandriel@users.noreply.github.com> Co-authored-by: bash <hermensb@gmail.com> Co-authored-by: bashermens <31279994+hermensbas@users.noreply.github.com>
This commit is contained in:
@@ -5,18 +5,211 @@
|
||||
|
||||
#include "FollowActions.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <array>
|
||||
|
||||
#include "Event.h"
|
||||
#include "Formations.h"
|
||||
#include "LastMovementValue.h"
|
||||
#include "MotionMaster.h"
|
||||
#include "PlayerbotAI.h"
|
||||
#include "Playerbots.h"
|
||||
#include "ServerFacade.h"
|
||||
#include "Transport.h"
|
||||
#include "Map.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
Transport* GetTransportForPosTolerant(Map* map, WorldObject* ref, uint32 phaseMask, float x, float y, float z)
|
||||
{
|
||||
if (!map || !ref)
|
||||
return nullptr;
|
||||
|
||||
std::array<float, 4> const probes = { z, z + 0.5f, z + 1.5f, z - 0.5f };
|
||||
for (float const pz : probes)
|
||||
{
|
||||
if (Transport* t = map->GetTransportForPos(phaseMask, x, y, pz, ref))
|
||||
return t;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Attempts to find a point on the leader's transport that is closer to the bot,
|
||||
// by probing along the segment from master -> bot and returning the last point
|
||||
// that is still detected as being on the expected transport.
|
||||
bool FindBoardingPointOnTransport(Map* map, Transport* expectedTransport, WorldObject* ref,
|
||||
float masterX, float masterY, float masterZ,
|
||||
float botX, float botY, float botZ,
|
||||
float& outX, float& outY, float& outZ)
|
||||
{
|
||||
if (!map || !expectedTransport || !ref)
|
||||
return false;
|
||||
|
||||
uint32 const phaseMask = ref->GetPhaseMask();
|
||||
|
||||
// Ensure master is actually detected on that transport (tolerant).
|
||||
if (GetTransportForPosTolerant(map, ref, phaseMask, masterX, masterY, masterZ) != expectedTransport)
|
||||
return false;
|
||||
|
||||
// The raycast in GetTransportForPos starts at (z + 2). Probe with a safe Z.
|
||||
float const probeZ = std::max(masterZ, botZ);
|
||||
|
||||
// Adaptive step count: small platforms need tighter sampling.
|
||||
float const dx2 = botX - masterX;
|
||||
float const dy2 = botY - masterY;
|
||||
float const dist2d = std::sqrt(dx2 * dx2 + dy2 * dy2);
|
||||
int32 const steps = std::clamp(static_cast<int32>(dist2d / 0.75f), 10, 28);
|
||||
|
||||
float const dx = (botX - masterX) / static_cast<float>(steps);
|
||||
float const dy = (botY - masterY) / static_cast<float>(steps);
|
||||
|
||||
// Master must actually be on the expected transport for this to work.
|
||||
if (map->GetTransportForPos(ref->GetPhaseMask(), masterX, masterY, probeZ, ref) != expectedTransport)
|
||||
return false;
|
||||
|
||||
float lastX = masterX;
|
||||
float lastY = masterY;
|
||||
bool found = false;
|
||||
|
||||
for (int32 i = 1; i <= steps; ++i)
|
||||
{
|
||||
float const px = masterX + dx * i;
|
||||
float const py = masterY + dy * i;
|
||||
|
||||
Transport* const t = GetTransportForPosTolerant(map, ref, phaseMask, px, py, probeZ);
|
||||
if (t != expectedTransport)
|
||||
break;
|
||||
|
||||
lastX = px;
|
||||
lastY = py;
|
||||
found = true;
|
||||
}
|
||||
|
||||
if (!found)
|
||||
return false;
|
||||
|
||||
outX = lastX;
|
||||
outY = lastY;
|
||||
outZ = masterZ; // keep deck-level Z to encourage stepping onto the platform/boat
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool FollowAction::Execute(Event /*event*/)
|
||||
{
|
||||
Formation* formation = AI_VALUE(Formation*, "formation");
|
||||
std::string const target = formation->GetTargetName();
|
||||
|
||||
// Transport handling for moving transports only (boats/zeppelins).
|
||||
Player* master = botAI->GetMaster();
|
||||
if (master && master->IsInWorld() && bot->IsInWorld() && bot->GetMapId() == master->GetMapId())
|
||||
{
|
||||
Map* map = master->GetMap();
|
||||
uint32 const mapId = bot->GetMapId();
|
||||
Transport* transport = nullptr;
|
||||
bool masterOnTransport = false;
|
||||
|
||||
if (master->GetTransport())
|
||||
{
|
||||
transport = master->GetTransport();
|
||||
masterOnTransport = true;
|
||||
}
|
||||
else if (map)
|
||||
{
|
||||
transport = GetTransportForPosTolerant(map, master, master->GetPhaseMask(),
|
||||
master->GetPositionX(), master->GetPositionY(), master->GetPositionZ());
|
||||
masterOnTransport = (transport != nullptr);
|
||||
}
|
||||
|
||||
// Ignore static transports (elevators/trams): only keep boats/zeppelins here.
|
||||
if (transport && transport->IsStaticTransport())
|
||||
transport = nullptr;
|
||||
|
||||
if (transport && map && bot->GetTransport() != transport)
|
||||
{
|
||||
float const botProbeZ = std::max(bot->GetPositionZ(), transport->GetPositionZ());
|
||||
Transport* botSurfaceTransport = GetTransportForPosTolerant(map, bot, bot->GetPhaseMask(),
|
||||
bot->GetPositionX(), bot->GetPositionY(), botProbeZ);
|
||||
|
||||
if (botSurfaceTransport == transport)
|
||||
{
|
||||
transport->AddPassenger(bot, true);
|
||||
bot->StopMovingOnCurrentPos();
|
||||
return true;
|
||||
}
|
||||
|
||||
float const boardingAssistDistance = 60.0f;
|
||||
float const dist2d = ServerFacade::instance().GetDistance2d(bot, master);
|
||||
bool const inAssist = ServerFacade::instance().IsDistanceLessOrEqualThan(dist2d, boardingAssistDistance);
|
||||
|
||||
if (inAssist)
|
||||
{
|
||||
float destX = masterOnTransport ? master->GetPositionX() : transport->GetPositionX();
|
||||
float destY = masterOnTransport ? master->GetPositionY() : transport->GetPositionY();
|
||||
float destZ = masterOnTransport ? master->GetPositionZ() : transport->GetPositionZ();
|
||||
float edgeX = 0.0f;
|
||||
float edgeY = 0.0f;
|
||||
float edgeZ = 0.0f;
|
||||
|
||||
if (masterOnTransport &&
|
||||
FindBoardingPointOnTransport(map, transport, master,
|
||||
master->GetPositionX(), master->GetPositionY(), master->GetPositionZ(),
|
||||
bot->GetPositionX(), bot->GetPositionY(), bot->GetPositionZ(),
|
||||
edgeX, edgeY, edgeZ))
|
||||
{
|
||||
destX = edgeX;
|
||||
destY = edgeY;
|
||||
destZ = edgeZ;
|
||||
}
|
||||
|
||||
MovementPriority const priority = botAI->GetState() == BOT_STATE_COMBAT
|
||||
? MovementPriority::MOVEMENT_COMBAT
|
||||
: MovementPriority::MOVEMENT_NORMAL;
|
||||
|
||||
bool const movingAllowed = IsMovingAllowed(mapId, destX, destY, destZ);
|
||||
bool const dupMove = IsDuplicateMove(mapId, destX, destY, destZ);
|
||||
bool const waiting = IsWaitingForLastMove(priority);
|
||||
|
||||
if (movingAllowed && !dupMove && !waiting)
|
||||
{
|
||||
if (bot->IsSitState())
|
||||
bot->SetStandState(UNIT_STAND_STATE_STAND);
|
||||
|
||||
if (bot->IsNonMeleeSpellCast(true))
|
||||
{
|
||||
bot->CastStop();
|
||||
botAI->InterruptSpell();
|
||||
}
|
||||
|
||||
if (MotionMaster* mm = bot->GetMotionMaster())
|
||||
{
|
||||
mm->MovePoint(
|
||||
/*id*/ 0,
|
||||
/*coords*/ destX, destY, destZ,
|
||||
/*forcedMovement*/ FORCED_MOVEMENT_NONE,
|
||||
/*speed*/ 0.0f,
|
||||
/*orientation*/ 0.0f,
|
||||
/*generatePath*/ false,
|
||||
/*forceDestination*/ false);
|
||||
}
|
||||
else
|
||||
return false;
|
||||
|
||||
float delay = 1000.0f * MoveDelay(bot->GetExactDist(destX, destY, destZ));
|
||||
delay = std::clamp(delay, 0.0f, static_cast<float>(sPlayerbotAIConfig.maxWaitForMove));
|
||||
|
||||
AI_VALUE(LastMovement&, "last movement")
|
||||
.Set(mapId, destX, destY, destZ, bot->GetOrientation(), delay, priority);
|
||||
ClearIdleState();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// end unified transport handling
|
||||
|
||||
bool moved = false;
|
||||
if (!target.empty())
|
||||
{
|
||||
|
||||
@@ -1753,190 +1753,75 @@ void TravelNodeMap::generateTransportNodes()
|
||||
for (auto const& itr : *sObjectMgr->GetGameObjectTemplates())
|
||||
{
|
||||
GameObjectTemplate const* data = &itr.second;
|
||||
if (data && (data->type == GAMEOBJECT_TYPE_TRANSPORT || data->type == GAMEOBJECT_TYPE_MO_TRANSPORT))
|
||||
if (!data || (data->type != GAMEOBJECT_TYPE_TRANSPORT && data->type != GAMEOBJECT_TYPE_MO_TRANSPORT))
|
||||
continue;
|
||||
|
||||
uint32 pathId = data->moTransport.taxiPathId;
|
||||
float moveSpeed = data->moTransport.moveSpeed;
|
||||
if (pathId >= sTaxiPathNodesByPath.size())
|
||||
continue;
|
||||
|
||||
TaxiPathNodeList const& path = sTaxiPathNodesByPath[pathId];
|
||||
|
||||
// Keep only transports with taxi paths (boats/zeppelins).
|
||||
if (path.empty())
|
||||
continue;
|
||||
|
||||
std::vector<WorldPosition> ppath;
|
||||
TravelNode* prevNode = nullptr;
|
||||
|
||||
// Loop over the path and connect stop locations.
|
||||
for (auto& p : path)
|
||||
{
|
||||
TransportAnimation const* animation = sTransportMgr->GetTransportAnimInfo(itr.first);
|
||||
WorldPosition pos = WorldPosition(p->mapid, p->x, p->y, p->z, 0);
|
||||
|
||||
uint32 pathId = data->moTransport.taxiPathId;
|
||||
float moveSpeed = data->moTransport.moveSpeed;
|
||||
if (pathId >= sTaxiPathNodesByPath.size())
|
||||
continue;
|
||||
if (prevNode)
|
||||
ppath.push_back(pos);
|
||||
|
||||
TaxiPathNodeList const& path = sTaxiPathNodesByPath[pathId];
|
||||
|
||||
std::vector<WorldPosition> ppath;
|
||||
TravelNode* prevNode = nullptr;
|
||||
|
||||
// Elevators/Trams
|
||||
if (path.empty())
|
||||
if (p->delay > 0)
|
||||
{
|
||||
if (animation)
|
||||
TravelNode* node = TravelNodeMap::instance().addNode(pos, data->name, true, true, true, itr.first);
|
||||
|
||||
if (!prevNode)
|
||||
{
|
||||
TransportPathContainer aPath = animation->Path;
|
||||
float timeStart;
|
||||
|
||||
for (auto& transport : WorldPosition().getGameObjectsNear(0, itr.first))
|
||||
{
|
||||
prevNode = nullptr;
|
||||
WorldPosition basePos(transport->mapid, transport->posX, transport->posY, transport->posZ,
|
||||
transport->orientation);
|
||||
WorldPosition lPos = WorldPosition();
|
||||
|
||||
for (auto& p : aPath)
|
||||
{
|
||||
float dx = -1 * p.second->X;
|
||||
float dy = -1 * p.second->Y;
|
||||
|
||||
WorldPosition pos =
|
||||
WorldPosition(basePos.GetMapId(), basePos.GetPositionX() + dx,
|
||||
basePos.GetPositionY() + dy, basePos.GetPositionZ() + p.second->Z,
|
||||
basePos.GetOrientation());
|
||||
|
||||
if (prevNode)
|
||||
{
|
||||
ppath.push_back(pos);
|
||||
}
|
||||
|
||||
if (pos.distance(&lPos) == 0)
|
||||
{
|
||||
TravelNode* node =
|
||||
TravelNodeMap::instance().addNode(pos, data->name, true, true, true, itr.first);
|
||||
|
||||
if (!prevNode)
|
||||
{
|
||||
ppath.push_back(pos);
|
||||
timeStart = p.second->TimeSeg;
|
||||
}
|
||||
else
|
||||
{
|
||||
float totalTime = (p.second->TimeSeg - timeStart) / 1000.0f;
|
||||
|
||||
TravelNodePath travelPath(0.1f, totalTime, (uint8)TravelNodePathType::transport,
|
||||
itr.first, true);
|
||||
node->setPathTo(prevNode, travelPath);
|
||||
ppath.clear();
|
||||
ppath.push_back(pos);
|
||||
timeStart = p.second->TimeSeg;
|
||||
}
|
||||
|
||||
prevNode = node;
|
||||
}
|
||||
|
||||
lPos = pos;
|
||||
}
|
||||
|
||||
if (prevNode)
|
||||
{
|
||||
for (auto& p : aPath)
|
||||
{
|
||||
float dx = -1 * p.second->X;
|
||||
float dy = -1 * p.second->Y;
|
||||
WorldPosition pos =
|
||||
WorldPosition(basePos.GetMapId(), basePos.GetPositionX() + dx,
|
||||
basePos.GetPositionY() + dy, basePos.GetPositionZ() + p.second->Z,
|
||||
basePos.GetOrientation());
|
||||
|
||||
ppath.push_back(pos);
|
||||
|
||||
if (pos.distance(&lPos) == 0)
|
||||
{
|
||||
TravelNode* node =
|
||||
TravelNodeMap::instance().addNode(pos, data->name, true, true, true, itr.first);
|
||||
if (node != prevNode)
|
||||
{
|
||||
if (p.second->TimeSeg < timeStart)
|
||||
timeStart = 0;
|
||||
|
||||
float totalTime = (p.second->TimeSeg - timeStart) / 1000.0f;
|
||||
|
||||
TravelNodePath travelPath(0.1f, totalTime, (uint8)TravelNodePathType::transport,
|
||||
itr.first, true);
|
||||
travelPath.setPath(ppath);
|
||||
node->setPathTo(prevNode, travelPath);
|
||||
ppath.clear();
|
||||
ppath.push_back(pos);
|
||||
timeStart = p.second->TimeSeg;
|
||||
}
|
||||
}
|
||||
|
||||
lPos = pos;
|
||||
}
|
||||
}
|
||||
|
||||
ppath.clear();
|
||||
}
|
||||
ppath.push_back(pos);
|
||||
}
|
||||
}
|
||||
else // Boats/Zepelins
|
||||
{
|
||||
// Loop over the path and connect stop locations.
|
||||
for (auto& p : path)
|
||||
else
|
||||
{
|
||||
WorldPosition pos = WorldPosition(p->mapid, p->x, p->y, p->z, 0);
|
||||
|
||||
// if (data->displayId == 3015)
|
||||
// pos.setZ(pos.getZ() + 6.0f);
|
||||
// else if (data->displayId == 3031)
|
||||
// pos.setZ(pos.getZ() - 17.0f);
|
||||
|
||||
if (prevNode)
|
||||
{
|
||||
ppath.push_back(pos);
|
||||
}
|
||||
|
||||
if (p->delay > 0)
|
||||
{
|
||||
TravelNode* node = TravelNodeMap::instance().addNode(pos, data->name, true, true, true, itr.first);
|
||||
|
||||
if (!prevNode)
|
||||
{
|
||||
ppath.push_back(pos);
|
||||
}
|
||||
else
|
||||
{
|
||||
TravelNodePath travelPath(0.1f, 0.0, (uint8)TravelNodePathType::transport, itr.first, true);
|
||||
travelPath.setPathAndCost(ppath, moveSpeed);
|
||||
node->setPathTo(prevNode, travelPath);
|
||||
ppath.clear();
|
||||
ppath.push_back(pos);
|
||||
}
|
||||
|
||||
prevNode = node;
|
||||
}
|
||||
TravelNodePath travelPath(0.1f, 0.0, (uint8)TravelNodePathType::transport, itr.first, true);
|
||||
travelPath.setPathAndCost(ppath, moveSpeed);
|
||||
node->setPathTo(prevNode, travelPath);
|
||||
ppath.clear();
|
||||
ppath.push_back(pos);
|
||||
}
|
||||
|
||||
if (prevNode)
|
||||
{
|
||||
// Continue from start until first stop and connect to end.
|
||||
for (auto& p : path)
|
||||
{
|
||||
WorldPosition pos = WorldPosition(p->mapid, p->x, p->y, p->z, 0);
|
||||
|
||||
// if (data->displayId == 3015)
|
||||
// pos.setZ(pos.getZ() + 6.0f);
|
||||
// else if (data->displayId == 3031)
|
||||
// pos.setZ(pos.getZ() - 17.0f);
|
||||
|
||||
ppath.push_back(pos);
|
||||
|
||||
if (p->delay > 0)
|
||||
{
|
||||
TravelNode* node = TravelNodeMap::instance().getNode(pos, nullptr, 5.0f);
|
||||
|
||||
if (node != prevNode)
|
||||
{
|
||||
TravelNodePath travelPath(0.1f, 0.0, (uint8)TravelNodePathType::transport, itr.first,
|
||||
true);
|
||||
travelPath.setPathAndCost(ppath, moveSpeed);
|
||||
|
||||
node->setPathTo(prevNode, travelPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ppath.clear();
|
||||
prevNode = node;
|
||||
}
|
||||
}
|
||||
|
||||
if (!prevNode)
|
||||
continue;
|
||||
|
||||
// Continue from start until first stop and connect to end.
|
||||
for (auto& p : path)
|
||||
{
|
||||
WorldPosition pos = WorldPosition(p->mapid, p->x, p->y, p->z, 0);
|
||||
ppath.push_back(pos);
|
||||
|
||||
if (p->delay > 0)
|
||||
{
|
||||
TravelNode* node = TravelNodeMap::instance().getNode(pos, nullptr, 5.0f);
|
||||
|
||||
if (node != prevNode)
|
||||
{
|
||||
TravelNodePath travelPath(0.1f, 0.0, (uint8)TravelNodePathType::transport, itr.first, true);
|
||||
travelPath.setPathAndCost(ppath, moveSpeed);
|
||||
|
||||
node->setPathTo(prevNode, travelPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
ppath.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user