TrinityCore
IdleMovementGenerator.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 "G3DPosition.hpp"
22#include "MovementDefines.h"
23#include "MoveSpline.h"
24#include "MoveSplineInit.h"
25#include "Unit.h"
26
28{
32 BaseUnitState = 0;
33}
34
35/*
36 * TODO: "if (!owner->IsStopped())" is useless, each generator cleans their own STATE_MOVE, the result is that StopMoving is almost never called
37 * Old comment: "StopMoving is needed to make unit stop if its last movement generator expires but it should not be sent otherwise there are many redundent packets"
38 */
40{
41 owner->StopMoving();
42}
43
45{
46 owner->StopMoving();
47}
48
50{
51}
52
53void IdleMovementGenerator::Finalize(Unit* /*owner*/, bool/* active*/, bool/* movementInform*/)
54{
56}
57
59{
60 return IDLE_MOTION_TYPE;
61}
62
63//----------------------------------------------------//
64
66 Optional<float> turnSpeed, Optional<float> totalTurnAngle) : _id(id), _duration(duration), _turnSpeed(turnSpeed), _totalTurnAngle(totalTurnAngle),
67 _direction(direction), _diffSinceLastUpdate(0)
68{
73}
74
76{
79
80 owner->StopMoving();
81
82 /*
83 * TODO: This code should be handled somewhere else, like MovementInform
84 *
85 * if (owner->GetVictim())
86 * owner->SetInFront(owner->GetVictim());
87 *
88 * owner->AttackStop();
89 */
90}
92{
94
95 Initialize(owner);
96}
97
99{
100 _diffSinceLastUpdate += diff;
101
102 float currentAngle = owner->GetOrientation();
103 float angleDelta = _turnSpeed.value_or(owner->GetSpeed(MOVE_TURN_RATE)) * (float(_diffSinceLastUpdate) / float(IN_MILLISECONDS));
104
105 if (_duration)
106 _duration->Update(diff);
107
108 if (_totalTurnAngle)
109 _totalTurnAngle = *_totalTurnAngle - angleDelta;
110
111 bool expired = (_duration && _duration->Passed()) || (_totalTurnAngle && _totalTurnAngle < 0.0f);
112
113 if (angleDelta >= MIN_ANGLE_DELTA_FOR_FACING_UPDATE || expired)
114 {
115 float newAngle = Position::NormalizeOrientation(currentAngle + angleDelta * (_direction == ROTATE_DIRECTION_LEFT ? 1.0f : -1.0f));
116
117 Movement::MoveSplineInit init(owner);
118 init.MoveTo(PositionToVector3(owner->GetPosition()), false);
119 if (!owner->GetTransGUID().IsEmpty())
121 init.SetFacing(newAngle);
122 init.Launch();
123
125 }
126
127 if (expired)
128 {
130 return false;
131 }
132
133 return true;
134}
135
137{
139}
140
141void RotateMovementGenerator::Finalize(Unit* owner, bool/* active*/, bool movementInform)
142{
144
145 if (movementInform && owner->GetTypeId() == TYPEID_UNIT)
147}
148
150{
151 return ROTATE_MOTION_TYPE;
152}
153
154//----------------------------------------------------//
155
156DistractMovementGenerator::DistractMovementGenerator(uint32 timer, float orientation) : _timer(timer), _orientation(orientation)
157{
162}
163
165{
168
169 // Distracted creatures stand up if not standing
170 if (!owner->IsStandState())
172
173 Movement::MoveSplineInit init(owner);
174 init.MoveTo(PositionToVector3(*owner), false);
175 if (!owner->GetTransGUID().IsEmpty())
178 init.Launch();
179}
180
182{
184
185 Initialize(owner);
186}
187
189{
190 if (!owner)
191 return false;
192
193 if (diff > _timer)
194 {
196 return false;
197 }
198
199 _timer -= diff;
200 return true;
201}
202
204{
206}
207
208void DistractMovementGenerator::Finalize(Unit* owner, bool/* active*/, bool movementInform)
209{
211
212 // TODO: This code should be handled somewhere else
213 // If this is a creature, then return orientation to original position (for idle movement creatures)
214 if (movementInform && HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED) && owner->GetTypeId() == TYPEID_UNIT)
215 {
216 float angle = owner->ToCreature()->GetHomePosition().GetOrientation();
217 owner->SetFacingTo(angle);
218 }
219}
220
222{
224}
225
226//----------------------------------------------------//
227
229{
231}
232
233void AssistanceDistractMovementGenerator::Finalize(Unit* owner, bool/* active*/, bool movementInform)
234{
236
237 if (movementInform && HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED) && owner->GetTypeId() == TYPEID_UNIT)
239}
240
242{
244}
@ IN_MILLISECONDS
Definition: Common.h:35
uint32_t uint32
Definition: Define.h:142
@ MOTION_MODE_DEFAULT
RotateDirection
@ ROTATE_DIRECTION_LEFT
@ MOTION_PRIORITY_HIGHEST
@ MOTION_PRIORITY_NORMAL
MovementGeneratorType
@ DISTRACT_MOTION_TYPE
@ IDLE_MOTION_TYPE
@ ROTATE_MOTION_TYPE
@ ASSISTANCE_DISTRACT_MOTION_TYPE
@ MOVEMENTGENERATOR_FLAG_INITIALIZATION_PENDING
@ MOVEMENTGENERATOR_FLAG_DEACTIVATED
@ MOVEMENTGENERATOR_FLAG_FINALIZED
@ MOVEMENTGENERATOR_FLAG_INFORM_ENABLED
@ MOVEMENTGENERATOR_FLAG_INITIALIZED
@ TYPEID_UNIT
Definition: ObjectGuid.h:40
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
@ MOVE_TURN_RATE
Definition: UnitDefines.h:122
@ REACT_AGGRESSIVE
Definition: UnitDefines.h:508
@ UNIT_STAND_STATE_STAND
Definition: UnitDefines.h:42
@ UNIT_STATE_DISTRACTED
Definition: Unit.h:267
@ UNIT_STATE_ROTATING
Definition: Unit.h:276
MovementGeneratorType GetMovementGeneratorType() const override
void Finalize(Unit *, bool, bool) override
AssistanceDistractMovementGenerator(uint32 timer, float orientation)
virtual void MovementInform(uint32, uint32)
Definition: CreatureAI.h:154
void GetHomePosition(float &x, float &y, float &z, float &ori) const
Definition: Creature.h:373
void SetReactState(ReactStates st)
Definition: Creature.h:160
CreatureAI * AI() const
Definition: Creature.h:214
void Finalize(Unit *, bool, bool) override
void Deactivate(Unit *) override
void Initialize(Unit *) override
MovementGeneratorType GetMovementGeneratorType() const override
bool Update(Unit *, uint32) override
DistractMovementGenerator(uint32 timer, float orientation)
void Reset(Unit *) override
void Deactivate(Unit *) override
void Initialize(Unit *) override
MovementGeneratorType GetMovementGeneratorType() const override
void Finalize(Unit *, bool, bool) override
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 DisableTransportPathTransformations()
void SetFacing(float angle)
bool IsEmpty() const
Definition: ObjectGuid.h:319
static Creature * ToCreature(Object *o)
Definition: Object.h:219
TypeID GetTypeId() const
Definition: Object.h:173
Optional< float > _turnSpeed
radians per sec
bool Update(Unit *, uint32) override
static constexpr float MIN_ANGLE_DELTA_FOR_FACING_UPDATE
void Initialize(Unit *) override
Optional< TimeTracker > _duration
void Reset(Unit *) override
MovementGeneratorType GetMovementGeneratorType() const override
void Finalize(Unit *, bool, bool) override
Optional< float > _totalTurnAngle
RotateMovementGenerator(uint32 id, RotateDirection direction, Optional< Milliseconds > duration, Optional< float > turnSpeed, Optional< float > totalTurnAngle)
void Deactivate(Unit *) override
Definition: Unit.h:627
float GetSpeed(UnitMoveType mtype) const
Definition: Unit.cpp:8515
void SetStandState(UnitStandStateType state, uint32 animKitID=0)
Definition: Unit.cpp:10100
void StopMoving()
Definition: Unit.cpp:10049
bool IsStandState() const
Definition: Unit.cpp:10094
void SetFacingTo(float const ori, bool force=true)
Definition: Unit.cpp:12653
ObjectGuid GetTransGUID() const override
Definition: Unit.cpp:11510
static float NormalizeOrientation(float o)
Definition: Position.cpp:135
constexpr void GetPosition(float &x, float &y) const
Definition: Position.h:81
constexpr float GetOrientation() const
Definition: Position.h:79