fix(Core/Movement): add creature formation movement generator (#25030)

Co-authored-by: Shauren <shauren.trinity@gmail.com>
Co-authored-by: Rocco Silipo <108557877+Rorschach91@users.noreply.github.com>
This commit is contained in:
sogladev
2026-03-08 17:41:42 +01:00
committed by GitHub
parent 04fbeec3c2
commit 4f3e5ca591
19 changed files with 402 additions and 86 deletions
+14 -2
View File
@@ -22,6 +22,7 @@
#include "CreatureAI.h"
#include "CreatureAISelector.h"
#include "CreatureGroups.h"
#include "MoveSpline.h"
#include "DatabaseEnv.h"
#include "Formulas.h"
#include "GameEventMgr.h"
@@ -1093,8 +1094,7 @@ bool Creature::AIM_Initialize(CreatureAI* ai)
UnitAI* oldAI = i_AI;
// Xinef: called in add to world
//Motion_Initialize();
Motion_Initialize();
i_AI = ai ? ai : FactorySelector::SelectAI(this);
delete oldAI;
@@ -1117,7 +1117,19 @@ void Creature::Motion_Initialize()
GetMotionMaster()->Initialize();
}
else if (m_formation->IsFormed())
{
// If the leader is already moving, start following immediately
// instead of waiting for the next waypoint signal.
if (Creature* leader = m_formation->GetLeader())
{
if (leader->IsAlive() && !leader->movespline->Finalized())
{
m_formation->LeaderStartedMoving();
return;
}
}
GetMotionMaster()->MoveIdle(); //wait the order of leader
}
else
GetMotionMaster()->Initialize();
}
@@ -186,6 +186,7 @@ void CreatureGroup::RemoveMember(Creature* member)
{
if (m_leader == member)
{
RemoveFormationMovement();
m_leader = nullptr;
}
@@ -333,7 +334,7 @@ void CreatureGroup::FormationReset(bool dismiss, bool initMotionMaster)
if (initMotionMaster)
{
if (dismiss)
member->GetMotionMaster()->Initialize();
member->GetMotionMaster()->MovementExpiredOnSlot(MOTION_SLOT_IDLE, false);
else
member->GetMotionMaster()->MoveIdle();
@@ -344,16 +345,11 @@ void CreatureGroup::FormationReset(bool dismiss, bool initMotionMaster)
m_Formed = !dismiss;
}
void CreatureGroup::LeaderMoveTo(float x, float y, float z, uint32 move_type)
void CreatureGroup::LeaderStartedMoving()
{
//! To do: This should probably get its own movement generator or use WaypointMovementGenerator.
//! If the leader's path is known, member's path can be plotted as well using formation offsets.
if (!m_leader)
return;
float pathDist = m_leader->GetExactDist(x, y, z);
float pathAngle = std::atan2(m_leader->GetPositionY() - y, m_leader->GetPositionX() - x);
for (auto const& itr : m_members)
{
Creature* member = itr.first;
@@ -361,59 +357,39 @@ void CreatureGroup::LeaderMoveTo(float x, float y, float z, uint32 move_type)
if (member == m_leader || !member->IsAlive() || member->GetVictim() || !pFormationInfo.HasGroupFlag(std::underlying_type_t<GroupAIFlags>(GroupAIFlags::GROUP_AI_FLAG_FOLLOW_LEADER)))
continue;
// If member is stunned / rooted etc don't allow to move him
// Or if charmed/controlled
if (member->HasUnitState(UNIT_STATE_NOT_MOVE) || member->isPossessed() || member->HasUnitFlag(UNIT_FLAG_PLAYER_CONTROLLED))
continue;
// Xinef: this should be automatized, if turn angle is greater than PI/2 (90�) we should swap formation angle
float followAngle = pFormationInfo.follow_angle;
if (static_cast<float>(M_PI) - std::fabs(std::fabs(m_leader->GetOrientation() - pathAngle) - static_cast<float>(M_PI)) > static_cast<float>(M_PI)* 0.5f)
{
// pussywizard: in both cases should be 2*M_PI - follow_angle
// pussywizard: also, GetCurrentWaypointID() returns 0..n-1, while point_1 must be > 0, so +1
// pussywizard: db table waypoint_data shouldn't have point id 0 and shouldn't have any gaps for this to work!
// if (m_leader->GetCurrentWaypointID()+1 == pFormationInfo->point_1 || m_leader->GetCurrentWaypointID()+1 == itr->second->point_2)
followAngle = Position::NormalizeOrientation(pFormationInfo.follow_angle + static_cast<float>(M_PI)); //(2 * M_PI) - itr->second->follow_angle;
}
float const followAngle = pFormationInfo.follow_angle;
float const followDist = pFormationInfo.follow_dist;
float dx = x + std::cos(followAngle + pathAngle) * followDist;
float dy = y + std::sin(followAngle + pathAngle) * followDist;
float dz = z;
if (!member->HasUnitState(UNIT_STATE_FOLLOW_MOVE))
member->GetMotionMaster()->MoveFormation(m_leader, followDist, followAngle, pFormationInfo.point_1, pFormationInfo.point_2);
}
}
Acore::NormalizeMapCoord(dx);
Acore::NormalizeMapCoord(dy);
if (move_type < 2)
member->UpdateGroundPositionZ(dx, dy, dz);
bool CreatureGroup::CanLeaderStartMoving() const
{
for (auto const& itr : m_members)
{
if (itr.first && itr.first != m_leader && itr.first->IsAlive())
if (itr.first->IsEngaged() || itr.first->IsInEvadeMode())
return false;
}
// pussywizard: setting the same movementflags is not enough, spline decides whether leader walks/runs, so spline param is now passed as "run" parameter to this function
member->SetUnitMovementFlags(m_leader->GetUnitMovementFlags());
switch (move_type)
{
case WAYPOINT_MOVE_TYPE_WALK:
member->AddUnitMovementFlag(MOVEMENTFLAG_WALKING);
break;
case WAYPOINT_MOVE_TYPE_RUN:
member->RemoveUnitMovementFlag(MOVEMENTFLAG_WALKING);
break;
case WAYPOINT_MOVE_TYPE_LAND:
member->AddUnitMovementFlag(MOVEMENTFLAG_DISABLE_GRAVITY);
break;
}
return true;
}
// xinef: if we move members to position without taking care of sizes, we should compare distance without sizes
// xinef: change members speed basing on distance - if too far speed up, if too close slow down
UnitMoveType const mtype = Movement::SelectSpeedType(member->GetUnitMovementFlags());
float const speedRate = m_leader->GetSpeedRate(mtype) * member->GetExactDist(dx, dy, dz) / pathDist;
void CreatureGroup::RemoveFormationMovement()
{
for (auto const& itr : m_members)
{
Creature* member = itr.first;
if (!member || member == m_leader)
continue;
if (speedRate > 0.01f) // don't move if speed rate is too low
{
member->SetSpeedRate(mtype, speedRate);
member->GetMotionMaster()->MovePoint(0, dx, dy, dz);
member->SetHomePosition(dx, dy, dz, pathAngle);
}
if (member->GetMotionMaster()->GetMotionSlotType(MOTION_SLOT_IDLE) == FORMATION_MOTION_TYPE)
member->GetMotionMaster()->MovementExpiredOnSlot(MOTION_SLOT_IDLE, false);
}
}
@@ -108,7 +108,9 @@ public:
void RemoveMember(Creature* member);
void FormationReset(bool dismiss, bool initMotionMaster);
void LeaderMoveTo(float x, float y, float z, uint32 move_type);
void LeaderStartedMoving();
[[nodiscard]] bool CanLeaderStartMoving() const;
void RemoveFormationMovement();
void MemberEngagingTarget(Creature* member, Unit* target);
Unit* GetNewTargetForMember(Creature* member);
void MemberEvaded(Creature* member);