TrinityCore
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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
64{
65 static IdleMovementGenerator instance;
66 return &instance;
67}
68
69//----------------------------------------------------//
70
72 Optional<float> turnSpeed, Optional<float> totalTurnAngle,
73 Optional<Scripting::v2::ActionResultSetter<MovementStopReason>>&& scriptResult) : _id(id), _duration(duration),
74 _turnSpeed(turnSpeed), _totalTurnAngle(totalTurnAngle),
75 _direction(direction), _diffSinceLastUpdate(0)
76{
81 ScriptResult = std::move(scriptResult);
82}
83
85{
88
89 owner->StopMoving();
90
91 /*
92 * TODO: This code should be handled somewhere else, like MovementInform
93 *
94 * if (owner->GetVictim())
95 * owner->SetInFront(owner->GetVictim());
96 *
97 * owner->AttackStop();
98 */
99}
101{
103
104 Initialize(owner);
105}
106
108{
109 _diffSinceLastUpdate += diff;
110
111 float currentAngle = owner->GetOrientation();
112 float angleDelta = _turnSpeed.value_or(owner->GetSpeed(MOVE_TURN_RATE)) * (float(_diffSinceLastUpdate) / float(IN_MILLISECONDS));
113
114 if (_duration)
115 _duration->Update(diff);
116
117 if (_totalTurnAngle)
118 _totalTurnAngle = *_totalTurnAngle - angleDelta;
119
120 bool expired = (_duration && _duration->Passed()) || (_totalTurnAngle && _totalTurnAngle < 0.0f);
121
122 if (angleDelta >= MIN_ANGLE_DELTA_FOR_FACING_UPDATE || expired)
123 {
124 float newAngle = Position::NormalizeOrientation(currentAngle + angleDelta * (_direction == ROTATE_DIRECTION_LEFT ? 1.0f : -1.0f));
125
126 Movement::MoveSplineInit init(owner);
127 init.MoveTo(PositionToVector3(owner->GetPosition()), false);
128 if (!owner->GetTransGUID().IsEmpty())
130 init.SetFacing(newAngle);
131 init.Launch();
132
134 }
135
136 if (expired)
137 {
139 return false;
140 }
141
142 return true;
143}
144
146{
148}
149
150void RotateMovementGenerator::Finalize(Unit* owner, bool/* active*/, bool movementInform)
151{
153
154 if (movementInform)
155 {
157 if (owner->IsCreature())
159 }
160}
161
163{
164 return ROTATE_MOTION_TYPE;
165}
166
167//----------------------------------------------------//
168
169DistractMovementGenerator::DistractMovementGenerator(uint32 timer, float orientation) : _timer(timer), _orientation(orientation)
170{
175}
176
178{
181
182 // Distracted creatures stand up if not standing
183 if (!owner->IsStandState())
185
186 Movement::MoveSplineInit init(owner);
187 init.MoveTo(PositionToVector3(*owner), false);
188 if (!owner->GetTransGUID().IsEmpty())
191 init.Launch();
192}
193
195{
197
198 Initialize(owner);
199}
200
202{
203 if (!owner)
204 return false;
205
206 if (diff > _timer)
207 {
209 return false;
210 }
211
212 _timer -= diff;
213 return true;
214}
215
217{
219}
220
221void DistractMovementGenerator::Finalize(Unit* owner, bool/* active*/, bool movementInform)
222{
224
225 // TODO: This code should be handled somewhere else
226 // If this is a creature, then return orientation to original position (for idle movement creatures)
227 if (movementInform && HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED) && owner->GetTypeId() == TYPEID_UNIT)
228 {
229 float angle = owner->ToCreature()->GetHomePosition().GetOrientation();
230 owner->SetFacingTo(angle);
231 }
232}
233
235{
237}
238
239//----------------------------------------------------//
240
242{
244}
245
246void AssistanceDistractMovementGenerator::Finalize(Unit* owner, bool/* active*/, bool movementInform)
247{
249
250 if (movementInform && HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED) && owner->GetTypeId() == TYPEID_UNIT)
252}
253
255{
257}
@ IN_MILLISECONDS
Definition: Common.h:38
uint32_t uint32
Definition: Define.h:148
@ 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:42
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:542
@ UNIT_STAND_STATE_STAND
Definition: UnitDefines.h:42
@ UNIT_STATE_DISTRACTED
Definition: Unit.h:271
@ UNIT_STATE_ROTATING
Definition: Unit.h:280
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:388
void SetReactState(ReactStates st)
Definition: Creature.h:174
CreatureAI * AI() const
Definition: Creature.h:228
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 SetScriptResult(MovementStopReason reason)
void AddFlag(uint16 const flag)
Optional< Scripting::v2::ActionResultSetter< MovementStopReason > > ScriptResult
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:322
static Creature * ToCreature(Object *o)
Definition: Object.h:255
TypeID GetTypeId() const
Definition: Object.h:209
bool IsCreature() const
Definition: Object.h:254
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
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, Optional< Scripting::v2::ActionResultSetter< MovementStopReason > > &&scriptResult)
void Deactivate(Unit *) override
Definition: Unit.h:632
float GetSpeed(UnitMoveType mtype) const
Definition: Unit.cpp:8740
void SetStandState(UnitStandStateType state, uint32 animKitID=0)
Definition: Unit.cpp:10505
void StopMoving()
Definition: Unit.cpp:10454
bool IsStandState() const
Definition: Unit.cpp:10499
void SetFacingTo(float const ori, bool force=true)
Definition: Unit.cpp:13074
ObjectGuid GetTransGUID() const override
Definition: Unit.cpp:11915
MovementGenerator * Create(Unit *object) const override
static float NormalizeOrientation(float o)
Definition: Position.cpp:204
constexpr void GetPosition(float &x, float &y) const
Definition: Position.h:91
constexpr float GetOrientation() const
Definition: Position.h:89