TrinityCore
Loading...
Searching...
No Matches
RandomMovementGenerator.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 "MoveSpline.h"
22#include "MoveSplineInit.h"
23#include "MovementDefines.h"
24#include "PathGenerator.h"
25#include "Player.h"
26#include "Random.h"
27
28template<class T>
30 MovementWalkRunSpeedSelectionMode speedSelectionMode,
32 : _timer(0), _speed(speed), _speedSelectionMode(speedSelectionMode), _wanderDistance(distance), _wanderSteps(0)
33{
37 this->ScriptResult = std::move(scriptResult);
38 if (duration)
39 _duration.emplace(*duration);
40}
41
42template<class T>
47
48template<class T>
50{
51 if (timer)
52 {
54 _timer.Reset(timer);
55 this->RemoveFlag(MOVEMENTGENERATOR_FLAG_PAUSED);
56 }
57 else
58 {
59 this->AddFlag(MOVEMENTGENERATOR_FLAG_PAUSED);
61 }
62}
63
64template<class T>
66{
67 if (overrideTimer)
68 _timer.Reset(overrideTimer);
69
70 this->RemoveFlag(MOVEMENTGENERATOR_FLAG_PAUSED);
71}
72
73template<class T>
75{
78
79 if (!owner->IsAlive())
80 return false;
81
82 _reference = owner->GetPosition();
83
84 // Retail seems to let a creature walk 2 up to 10 splines before triggering a pause
85 _wanderSteps = urand(2, 10);
86
87 _timer.Reset(0);
88 _path = nullptr;
89 return true;
90}
91
92template<class T>
94{
96
97 return DoInitialize(owner);
98}
99
100template<class T>
102{
103 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE | UNIT_STATE_LOST_CONTROL) || owner->IsMovementPreventedByCasting())
104 {
106 owner->StopMoving();
107 _path = nullptr;
108 return;
109 }
110
111 Position position(_reference);
112 float distance = _wanderDistance > 0.1f ? frand(0.1f, _wanderDistance) : _wanderDistance;
113 float angle = frand(0.f, static_cast<float>(M_PI * 2));
114 owner->MovePositionToFirstCollision(position, distance, angle);
115
116 // Check if the destination is in LOS
117 if (!owner->IsWithinLOS(position.GetPositionX(), position.GetPositionY(), position.GetPositionZ()))
118 {
119 // Retry later on
120 _timer.Reset(200);
121 return;
122 }
123
124 if (!_path)
125 {
126 _path = std::make_unique<PathGenerator>(owner);
127 _path->SetPathLengthLimit(30.0f);
128 }
129
130 bool result = _path->CalculatePath(position.GetPositionX(), position.GetPositionY(), position.GetPositionZ());
131 // PATHFIND_FARFROMPOLY shouldn't be checked as creatures in water are most likely far from poly
132 if (!result || (_path->GetPathType() & PATHFIND_NOPATH)
133 || (_path->GetPathType() & PATHFIND_SHORTCUT)
134 /*|| (_path->GetPathType() & PATHFIND_FARFROMPOLY)*/)
135 {
136 _timer.Reset(100);
137 return;
138 }
139
140 if (_path->GetPathLength() < 0.1f)
141 {
142 // the path is too short for the spline system to be accepted. Let's try again soon.
143 _timer.Reset(500);
144 return;
145 }
146
148
149 owner->AddUnitState(UNIT_STATE_ROAMING_MOVE);
150
151 Movement::MoveSplineInit init(owner);
152 init.MovebyPath(_path->GetPath());
153
154 switch (_speedSelectionMode)
155 {
157 break;
159 init.SetWalk(false);
160 break;
162 init.SetWalk(true);
163 break;
164 default:
165 break;
166 }
167
168 if (_speed)
169 init.SetVelocity(*_speed);
170
171 int32 splineDuration = init.Launch();
172
173 --_wanderSteps;
174 if (_wanderSteps) // Creature has yet to do steps before pausing
175 _timer.Reset(splineDuration);
176 else
177 {
178 // Creature has made all its steps, time for a little break
179 _timer.Reset(splineDuration + urand(4, 10) * IN_MILLISECONDS); // Retails seems to use rounded numbers so we do as well
180 _wanderSteps = urand(2, 10);
181 }
182
183 // Call for creature group update
184 if constexpr (std::is_base_of_v<Creature, T>)
185 owner->SignalFormationMovement();
186}
187
188template<class T>
190{
191 if (!owner->IsAlive())
192 return true;
193
195 return true;
196
197 if (_duration)
198 {
199 _duration->Update(diff);
200 if (_duration->Passed())
201 {
202 this->RemoveFlag(MOVEMENTGENERATOR_FLAG_TRANSITORY);
204 return false;
205 }
206 }
207
208 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || owner->IsMovementPreventedByCasting())
209 {
211 owner->StopMoving();
212 _path = nullptr;
213 return true;
214 }
215 else
216 this->RemoveFlag(MOVEMENTGENERATOR_FLAG_INTERRUPTED);
217
218 _timer.Update(diff);
219 if ((this->HasFlag(MOVEMENTGENERATOR_FLAG_SPEED_UPDATE_PENDING) && !owner->movespline->Finalized()) || (_timer.Passed() && owner->movespline->Finalized()))
220 SetRandomLocation(owner);
221
222 return true;
223}
224
225template<class T>
227{
229 owner->ClearUnitState(UNIT_STATE_ROAMING_MOVE);
230}
231
232template<class T>
233void RandomMovementGenerator<T>::DoFinalize(T* owner, bool active, bool movementInform)
234{
236 if (active)
237 {
238 owner->ClearUnitState(UNIT_STATE_ROAMING_MOVE);
239 owner->StopMoving();
240
241 // TODO: Research if this modification is needed, which most likely isnt
242 owner->SetWalk(false);
243 }
244
245 if (movementInform && this->HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED))
246 {
247 this->SetScriptResult(MovementStopReason::Finished);
248 if constexpr (std::is_base_of_v<Creature, T>)
249 if (owner->IsAIEnabled())
250 owner->AI()->MovementInform(RANDOM_MOTION_TYPE, 0);
251 }
252}
253
255{
256 Creature* owner = object->ToCreature();
258 switch (owner->GetMovementTemplate().GetRandom())
259 {
262 break;
264 break;
267 break;
268 default:
269 break;
270 }
271
272 return new RandomMovementGenerator<Creature>(object->ToCreature()->GetWanderDistance(), {}, {}, speedSelectionMode);
273}
274
@ IN_MILLISECONDS
Definition Common.h:38
#define M_PI
Definition Common.h:118
int32_t int32
Definition Define.h:150
uint32_t uint32
Definition Define.h:154
@ MOTION_PRIORITY_NORMAL
MovementWalkRunSpeedSelectionMode
MovementGeneratorType
@ RANDOM_MOTION_TYPE
@ MOVEMENTGENERATOR_FLAG_TIMED_PAUSED
@ MOVEMENTGENERATOR_FLAG_INITIALIZATION_PENDING
@ MOVEMENTGENERATOR_FLAG_PAUSED
@ MOVEMENTGENERATOR_FLAG_DEACTIVATED
@ MOVEMENTGENERATOR_FLAG_FINALIZED
@ MOVEMENTGENERATOR_FLAG_TRANSITORY
@ MOVEMENTGENERATOR_FLAG_INTERRUPTED
@ MOVEMENTGENERATOR_FLAG_INFORM_ENABLED
@ MOVEMENTGENERATOR_FLAG_INITIALIZED
@ MOVEMENTGENERATOR_FLAG_SPEED_UPDATE_PENDING
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
@ PATHFIND_NOPATH
@ PATHFIND_SHORTCUT
float frand(float min, float max)
Definition Random.cpp:56
uint32 urand(uint32 min, uint32 max)
Definition Random.cpp:43
@ UNIT_STATE_NOT_MOVE
Definition Unit.h:320
@ UNIT_STATE_ROAMING_MOVE
Definition Unit.h:298
@ UNIT_STATE_LOST_CONTROL
Definition Unit.h:316
@ UNIT_STATE_ROAMING
Definition Unit.h:279
float GetWanderDistance() const
Definition Creature.h:370
CreatureMovementData const & GetMovementTemplate() const
Scripting::v2::ActionResultSetter< MovementStopReason > ScriptResult
void SetWalk(bool enable)
void MovebyPath(std::span< Vector3 const > path, int32 pointId=0)
void SetVelocity(float velocity)
Creature * ToCreature()
Definition Object.h:121
RandomMovementGenerator(float distance, Optional< Milliseconds > duration={}, Optional< float > speed={}, MovementWalkRunSpeedSelectionMode speedSelectionMode=MovementWalkRunSpeedSelectionMode::Default, Scripting::v2::ActionResultSetter< MovementStopReason > &&scriptResult={})
Optional< TimeTracker > _duration
void Pause(uint32 timer) override
MovementGeneratorType GetMovementGeneratorType() const override
void Resume(uint32 overrideTimer) override
Definition Unit.h:647
CreatureRandomMovementType GetRandom() const
constexpr float GetPositionX() const
Definition Position.h:87
constexpr float GetPositionY() const
Definition Position.h:88
constexpr float GetPositionZ() const
Definition Position.h:89
MovementGenerator * Create(Unit *object) const override