TrinityCore
Loading...
Searching...
No Matches
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
33
34/*
35 * TODO: "if (!owner->IsStopped())" is useless, each generator cleans their own STATE_MOVE, the result is that StopMoving is almost never called
36 * 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"
37 */
39{
40 owner->StopMoving();
41 return true;
42}
43
45{
46 owner->StopMoving();
47 return true;
48}
49
51{
52}
53
54void IdleMovementGenerator::Finalize(Unit* /*owner*/, bool/* active*/, bool/* movementInform*/)
55{
57}
58
63
65{
66 static IdleMovementGenerator instance;
67 return &instance;
68}
69
70//----------------------------------------------------//
71
73 Optional<float> turnSpeed, Optional<float> totalTurnAngle,
74 Scripting::v2::ActionResultSetter<MovementStopReason>&& scriptResult) : _id(id), _duration(duration),
75 _turnSpeed(turnSpeed), _totalTurnAngle(totalTurnAngle),
76 _direction(direction), _diffSinceLastUpdate(0)
77{
81 ScriptResult = std::move(scriptResult);
82}
83
85{
88
89 return true;
90 /*
91 * TODO: This code should be handled somewhere else, like MovementInform
92 *
93 * if (owner->GetVictim())
94 * owner->SetInFront(owner->GetVictim());
95 *
96 * owner->AttackStop();
97 */
98}
105
107{
108 _diffSinceLastUpdate += diff;
109
110 float currentAngle = owner->GetOrientation();
111 float angleDelta = _turnSpeed.value_or(owner->GetSpeed(MOVE_TURN_RATE)) * (float(_diffSinceLastUpdate) / float(IN_MILLISECONDS));
112
113 if (_duration)
114 _duration->Update(diff);
115
116 if (_totalTurnAngle)
117 _totalTurnAngle = *_totalTurnAngle - angleDelta;
118
119 bool expired = (_duration && _duration->Passed()) || (_totalTurnAngle && _totalTurnAngle < 0.0f);
120
121 if (angleDelta >= MIN_ANGLE_DELTA_FOR_FACING_UPDATE || expired)
122 {
123 float newAngle = Position::NormalizeOrientation(currentAngle + angleDelta * (_direction == ROTATE_DIRECTION_LEFT ? 1.0f : -1.0f));
124
125 Movement::MoveSplineInit init(owner);
126 init.MoveTo(PositionToVector3(owner->GetPosition()), false);
127 if (!owner->GetTransGUID().IsEmpty())
129 init.SetFacing(newAngle);
130 init.Launch();
131
133 }
134
135 if (expired)
136 {
138 return false;
139 }
140
141 return true;
142}
143
148
149void RotateMovementGenerator::Finalize(Unit* owner, bool/* active*/, bool movementInform)
150{
152
153 if (movementInform)
154 {
156 if (owner->IsCreature())
158 }
159}
160
165
166//----------------------------------------------------//
167
168DistractMovementGenerator::DistractMovementGenerator(uint32 timer, float orientation) : _timer(timer), _orientation(orientation), _originalOrientation(0.f)
169{
173}
174
176{
179
180 // Distracted creatures stand up if not standing
181 if (!owner->IsStandState())
183
185
186 Movement::MoveSplineInit init(owner);
187 init.MoveTo(PositionToVector3(*owner), false);
188 if (!owner->GetTransGUID().IsEmpty())
191 init.Launch();
192 return true;
193}
194
201
203{
204 if (!owner)
205 return false;
206
207 if (diff > _timer)
208 {
210 return false;
211 }
212
213 _timer -= diff;
214 return true;
215}
216
221
222void DistractMovementGenerator::Finalize(Unit* owner, bool/* active*/, bool movementInform)
223{
225
226 if (movementInform && HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED) && owner->GetTypeId() == TYPEID_UNIT)
227 owner->SetFacingTo(_originalOrientation, true);
228}
229
234
235//----------------------------------------------------//
236
241
242void AssistanceDistractMovementGenerator::Finalize(Unit* owner, bool/* active*/, bool movementInform)
243{
245
246 if (movementInform && HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED) && owner->GetTypeId() == TYPEID_UNIT)
248}
249
@ IN_MILLISECONDS
Definition Common.h:38
uint32_t uint32
Definition Define.h:154
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:43
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
@ MOVE_TURN_RATE
@ REACT_AGGRESSIVE
@ UNIT_STAND_STATE_STAND
Definition UnitDefines.h:42
@ UNIT_STATE_DISTRACTED
Definition Unit.h:287
@ UNIT_STATE_ROTATING
Definition Unit.h:296
MovementGeneratorType GetMovementGeneratorType() const override
void Finalize(Unit *, bool, bool) override
AssistanceDistractMovementGenerator(uint32 timer, float orientation)
bool IsCreature() const
Definition BaseEntity.h:172
TypeID GetTypeId() const
Definition BaseEntity.h:166
virtual void MovementInform(uint32, uint32)
Definition CreatureAI.h:162
void SetReactState(ReactStates st)
Definition Creature.h:180
CreatureAI * AI() const
Definition Creature.h:235
void Finalize(Unit *, bool, bool) override
MovementGeneratorType GetMovementGeneratorType() const override
bool Update(Unit *, uint32) override
DistractMovementGenerator(uint32 timer, float orientation)
bool Reset(Unit *) override
void Deactivate(Unit *) override
MovementGeneratorType GetMovementGeneratorType() const override
void Finalize(Unit *, bool, bool) override
bool Initialize(Unit *) override
void SetScriptResult(MovementStopReason reason)
void AddFlag(uint16 const flag)
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 SetFacing(float angle)
bool IsEmpty() const
Definition ObjectGuid.h:362
Creature * ToCreature()
Definition Object.h:121
Optional< float > _turnSpeed
radians per sec
bool Update(Unit *, uint32) override
static constexpr float MIN_ANGLE_DELTA_FOR_FACING_UPDATE
Optional< TimeTracker > _duration
MovementGeneratorType GetMovementGeneratorType() const override
RotateMovementGenerator(uint32 id, RotateDirection direction, Optional< Milliseconds > duration, Optional< float > turnSpeed, Optional< float > totalTurnAngle, Scripting::v2::ActionResultSetter< MovementStopReason > &&scriptResult)
void Finalize(Unit *, bool, bool) override
Optional< float > _totalTurnAngle
void Deactivate(Unit *) override
bool Initialize(Unit *) override
Definition Unit.h:647
float GetSpeed(UnitMoveType mtype) const
Definition Unit.cpp:8951
void SetStandState(UnitStandStateType state, uint32 animKitID=0)
Definition Unit.cpp:10804
void StopMoving(bool force=false)
Definition Unit.cpp:10753
bool IsStandState() const
Definition Unit.cpp:10798
ObjectGuid GetTransGUID() const override
Definition Unit.cpp:12236
void SetFacingTo(float ori, bool force=true, uint32 movementId=EVENT_FACE)
Definition Unit.cpp:13385
MovementGenerator * Create(Unit *object) const override
static float NormalizeOrientation(float o)
Definition Position.cpp:207
constexpr void GetPosition(float &x, float &y) const
Definition Position.h:92
constexpr float GetOrientation() const
Definition Position.h:90