TrinityCore
FormationMovementGenerator.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
19#include "Creature.h"
20#include "CreatureAI.h"
21#include "CreatureGroups.h"
22#include "G3DPosition.hpp"
23#include "MovementDefines.h"
24#include "MoveSpline.h"
25#include "MoveSplineInit.h"
26
28 _range(range), _angle(angle), _point1(point1), _point2(point2), _lastLeaderSplineID(0), _hasPredictedDestination(false)
29{
34}
35
37{
39}
40
42{
45
47 {
49 owner->StopMoving();
50 return;
51 }
52
54}
55
57{
59
60 DoInitialize(owner);
61}
62
64{
65 Unit* target = GetTarget();
66
67 if (!owner || !target)
68 return false;
69
70 // Owner cannot move. Reset all fields and wait for next action
72 {
74 owner->StopMoving();
77 return true;
78 }
79
80 // If target is not moving and destination has been predicted and if we are on the same spline, we stop as well
81 if (target->movespline->Finalized() && target->movespline->GetId() == _lastLeaderSplineID && _hasPredictedDestination)
82 {
84 owner->StopMoving();
87 return true;
88 }
89
90 if (!owner->movespline->Finalized())
91 owner->SetHomePosition(owner->GetPosition());
92
93 // Formation leader has launched a new spline, launch a new one for our member as well
94 // This action does not reset the regular movement launch cycle interval
95 if (!target->movespline->Finalized() && target->movespline->GetId() != _lastLeaderSplineID)
96 {
97 // Update formation angle
98 if (_point1 && target->GetTypeId() == TYPEID_UNIT)
99 {
100 if (CreatureGroup* formation = target->ToCreature()->GetFormation())
101 {
102 if (Creature* leader = formation->GetLeader())
103 {
104 uint8 currentWaypoint = leader->GetCurrentWaypointInfo().first;
105 if (currentWaypoint == _point1 || currentWaypoint == _point2)
106 _angle = float(M_PI) * 2 - _angle;
107 }
108 }
109 }
110
111 LaunchMovement(owner, target);
112 _lastLeaderSplineID = target->movespline->GetId();
113 return true;
114 }
115
118 {
120
121 // Our leader has a different position than on our last check, launch movement.
122 if (_lastLeaderPosition != target->GetPosition())
123 {
124 LaunchMovement(owner, target);
125 return true;
126 }
127 }
128
129 // We have reached our destination before launching a new movement. Alling facing with leader
130 if (owner->HasUnitState(UNIT_STATE_FOLLOW_FORMATION_MOVE) && owner->movespline->Finalized())
131 {
133 owner->SetFacingTo(target->GetOrientation());
134 MovementInform(owner);
135 }
136
137 return true;
138}
139
141{
142 float relativeAngle = 0.f;
143
144 // Determine our relative angle to our current spline destination point
145 if (!target->movespline->Finalized())
146 relativeAngle = target->GetRelativeAngle(Vector3ToPosition(target->movespline->CurrentDestination()));
147
148 // Destination calculation
149 /*
150 According to sniff data, formation members have a periodic move interal of 1,2s.
151 Each of these splines has a exact duration of 1650ms +- 1ms when no pathfinding is involved.
152 To get a representative result like that we have to predict our formation leader's path
153 and apply our formation shape based on that destination.
154 */
155 Position dest = target->GetPosition();
156 float velocity = 0.f;
157
158 // Formation leader is moving. Predict our destination
159 if (!target->movespline->Finalized())
160 {
161 // Pick up leader's spline velocity
162 velocity = target->movespline->Velocity();
163
164 // Calculate travel distance to get a 1650ms result
165 float travelDist = velocity * 1.65f;
166
167 // Move destination ahead...
168 target->MovePositionToFirstCollision(dest, travelDist, relativeAngle);
169 // ... and apply formation shape
170 target->MovePositionToFirstCollision(dest, _range, _angle + relativeAngle);
171
172 float distance = owner->GetExactDist(dest);
173
174 // Calculate catchup speed mod (Limit to a maximum of 50% of our original velocity
175 float velocityMod = std::min<float>(distance / travelDist, 1.5f);
176
177 // Now we will always stay synch with our leader
178 velocity *= velocityMod;
180 }
181 else
182 {
183 // Formation leader is not moving. Just apply the base formation shape on his position.
184 target->MovePositionToFirstCollision(dest, _range, _angle + relativeAngle);
186 }
187
188 // Leader is not moving, so just pick up his default walk speed
189 if (velocity == 0.f)
190 velocity = target->GetSpeed(MOVE_WALK);
191
192 Movement::MoveSplineInit init(owner);
193 init.MoveTo(PositionToVector3(dest));
194 init.SetVelocity(velocity);
195 init.Launch();
196
200}
201
203{
206}
207
208void FormationMovementGenerator::DoFinalize(Creature* owner, bool active, bool movementInform)
209{
211 if (active)
213
214 if (movementInform && HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED))
215 MovementInform(owner);
216}
217
219{
220 if (owner->AI())
222}
#define M_PI
Definition: Common.h:115
uint8_t uint8
Definition: Define.h:144
uint32_t uint32
Definition: Define.h:142
#define ASSERT_NOTNULL(pointer)
Definition: Errors.h:84
@ MOTION_MODE_DEFAULT
@ MOTION_PRIORITY_NORMAL
MovementGeneratorType
@ FORMATION_MOTION_TYPE
@ MOVEMENTGENERATOR_FLAG_INITIALIZATION_PENDING
@ MOVEMENTGENERATOR_FLAG_DEACTIVATED
@ MOVEMENTGENERATOR_FLAG_FINALIZED
@ MOVEMENTGENERATOR_FLAG_TRANSITORY
@ MOVEMENTGENERATOR_FLAG_INTERRUPTED
@ MOVEMENTGENERATOR_FLAG_INFORM_ENABLED
@ MOVEMENTGENERATOR_FLAG_INITIALIZED
@ TYPEID_UNIT
Definition: ObjectGuid.h:40
@ MOVE_WALK
Definition: UnitDefines.h:117
@ UNIT_STATE_NOT_MOVE
Definition: Unit.h:300
@ UNIT_STATE_FOLLOW_FORMATION
Definition: Unit.h:274
@ UNIT_STATE_FOLLOW_FORMATION_MOVE
Definition: Unit.h:284
virtual void MovementInform(uint32, uint32)
Definition: CreatureAI.h:154
void SetHomePosition(float x, float y, float z, float o)
Definition: Creature.h:371
CreatureGroup * GetFormation()
Definition: Creature.h:391
CreatureAI * AI() const
Definition: Creature.h:214
bool IsMovementPreventedByCasting() const override
Definition: Creature.cpp:3551
FormationMovementGenerator(Unit *leader, float range, float angle, uint32 point1, uint32 point2)
void DoFinalize(Creature *, bool, bool)
MovementGeneratorType GetMovementGeneratorType() const override
static constexpr uint32 FORMATION_MOVEMENT_INTERVAL
void LaunchMovement(Creature *owner, Unit *target)
void AddFlag(uint16 const flag)
bool HasFlag(uint16 const flag) const
void RemoveFlag(uint16 const flag)
void MoveTo(Vector3 const &destination, bool generatePath=true, bool forceDestination=false)
void SetVelocity(float velocity)
static Creature * ToCreature(Object *o)
Definition: Object.h:219
TypeID GetTypeId() const
Definition: Object.h:173
Definition: Unit.h:627
void ClearUnitState(uint32 f)
Definition: Unit.h:733
float GetSpeed(UnitMoveType mtype) const
Definition: Unit.cpp:8515
void StopMoving()
Definition: Unit.cpp:10049
void AddUnitState(uint32 f)
Definition: Unit.h:731
void SetFacingTo(float const ori, bool force=true)
Definition: Unit.cpp:12653
bool HasUnitState(const uint32 f) const
Definition: Unit.h:732
std::unique_ptr< Movement::MoveSpline > movespline
Definition: Unit.h:1766
void MovePositionToFirstCollision(Position &pos, float dist, float angle)
Definition: Object.cpp:3482
Unit * GetTarget() const
float GetRelativeAngle(float x, float y) const
Definition: Position.h:136
float GetExactDist(float x, float y, float z) const
Definition: Position.h:118
constexpr void GetPosition(float &x, float &y) const
Definition: Position.h:81
constexpr float GetOrientation() const
Definition: Position.h:79
void Update(int32 diff)
Definition: Timer.h:121
bool Passed() const
Definition: Timer.h:131
void Reset(int32 expiry)
Definition: Timer.h:136