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{
38 this->ScriptResult = std::move(scriptResult);
39 if (duration)
40 _duration.emplace(*duration);
41}
42
43template<class T>
48
49template<class T>
51{
52 if (timer)
53 {
55 _timer.Reset(timer);
56 this->RemoveFlag(MOVEMENTGENERATOR_FLAG_PAUSED);
57 }
58 else
59 {
60 this->AddFlag(MOVEMENTGENERATOR_FLAG_PAUSED);
62 }
63}
64
65template<class T>
67{
68 if (overrideTimer)
69 _timer.Reset(overrideTimer);
70
71 this->RemoveFlag(MOVEMENTGENERATOR_FLAG_PAUSED);
72}
73
74template<class T>
76{
79
80 if (!owner->IsAlive())
81 return;
82
83 _reference = owner->GetPosition();
84 owner->StopMoving();
85
86 // Retail seems to let a creature walk 2 up to 10 splines before triggering a pause
87 _wanderSteps = urand(2, 10);
88
89 _timer.Reset(0);
90 _path = nullptr;
91}
92
93template<class T>
95{
97
98 DoInitialize(owner);
99}
100
101template<class T>
103{
104 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE | UNIT_STATE_LOST_CONTROL) || owner->IsMovementPreventedByCasting())
105 {
107 owner->StopMoving();
108 _path = nullptr;
109 return;
110 }
111
112 Position position(_reference);
113 float distance = _wanderDistance > 0.1f ? frand(0.1f, _wanderDistance) : _wanderDistance;
114 float angle = frand(0.f, static_cast<float>(M_PI * 2));
115 owner->MovePositionToFirstCollision(position, distance, angle);
116
117 // Check if the destination is in LOS
118 if (!owner->IsWithinLOS(position.GetPositionX(), position.GetPositionY(), position.GetPositionZ()))
119 {
120 // Retry later on
121 _timer.Reset(200);
122 return;
123 }
124
125 if (!_path)
126 {
127 _path = std::make_unique<PathGenerator>(owner);
128 _path->SetPathLengthLimit(30.0f);
129 }
130
131 bool result = _path->CalculatePath(position.GetPositionX(), position.GetPositionY(), position.GetPositionZ());
132 // PATHFIND_FARFROMPOLY shouldn't be checked as creatures in water are most likely far from poly
133 if (!result || (_path->GetPathType() & PATHFIND_NOPATH)
134 || (_path->GetPathType() & PATHFIND_SHORTCUT)
135 /*|| (_path->GetPathType() & PATHFIND_FARFROMPOLY)*/)
136 {
137 _timer.Reset(100);
138 return;
139 }
140
141 if (_path->GetPathLength() < 0.1f)
142 {
143 // the path is too short for the spline system to be accepted. Let's try again soon.
144 _timer.Reset(500);
145 return;
146 }
147
149
150 owner->AddUnitState(UNIT_STATE_ROAMING_MOVE);
151
152 Movement::MoveSplineInit init(owner);
153 init.MovebyPath(_path->GetPath());
154
155 switch (_speedSelectionMode)
156 {
158 break;
160 init.SetWalk(false);
161 break;
163 init.SetWalk(true);
164 break;
165 default:
166 break;
167 }
168
169 if (_speed)
170 init.SetVelocity(*_speed);
171
172 int32 splineDuration = init.Launch();
173
174 --_wanderSteps;
175 if (_wanderSteps) // Creature has yet to do steps before pausing
176 _timer.Reset(splineDuration);
177 else
178 {
179 // Creature has made all its steps, time for a little break
180 _timer.Reset(splineDuration + urand(4, 10) * IN_MILLISECONDS); // Retails seems to use rounded numbers so we do as well
181 _wanderSteps = urand(2, 10);
182 }
183
184 // Call for creature group update
185 if constexpr (std::is_base_of_v<Creature, T>)
186 owner->SignalFormationMovement();
187}
188
189template<class T>
191{
192 if (!owner->IsAlive())
193 return true;
194
196 return true;
197
198 if (_duration)
199 {
200 _duration->Update(diff);
201 if (_duration->Passed())
202 {
203 this->RemoveFlag(MOVEMENTGENERATOR_FLAG_TRANSITORY);
205 return false;
206 }
207 }
208
209 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || owner->IsMovementPreventedByCasting())
210 {
212 owner->StopMoving();
213 _path = nullptr;
214 return true;
215 }
216 else
217 this->RemoveFlag(MOVEMENTGENERATOR_FLAG_INTERRUPTED);
218
219 _timer.Update(diff);
220 if ((this->HasFlag(MOVEMENTGENERATOR_FLAG_SPEED_UPDATE_PENDING) && !owner->movespline->Finalized()) || (_timer.Passed() && owner->movespline->Finalized()))
221 SetRandomLocation(owner);
222
223 return true;
224}
225
226template<class T>
228{
230 owner->ClearUnitState(UNIT_STATE_ROAMING_MOVE);
231}
232
233template<class T>
234void RandomMovementGenerator<T>::DoFinalize(T* owner, bool active, bool movementInform)
235{
237 if (active)
238 {
239 owner->ClearUnitState(UNIT_STATE_ROAMING_MOVE);
240 owner->StopMoving();
241
242 // TODO: Research if this modification is needed, which most likely isnt
243 owner->SetWalk(false);
244 }
245
246 if (movementInform && this->HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED))
247 {
248 this->SetScriptResult(MovementStopReason::Finished);
249 if constexpr (std::is_base_of_v<Creature, T>)
250 if (owner->IsAIEnabled())
251 owner->AI()->MovementInform(RANDOM_MOTION_TYPE, 0);
252 }
253}
254
256{
257 Creature* owner = object->ToCreature();
259 switch (owner->GetMovementTemplate().GetRandom())
260 {
263 break;
265 break;
268 break;
269 default:
270 break;
271 }
272
273 return new RandomMovementGenerator<Creature>(object->ToCreature()->GetWanderDistance(), {}, {}, speedSelectionMode);
274}
275
@ 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_MODE_DEFAULT
@ 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:55
uint32 urand(uint32 min, uint32 max)
Definition Random.cpp:42
@ UNIT_STATE_NOT_MOVE
Definition Unit.h:306
@ UNIT_STATE_ROAMING_MOVE
Definition Unit.h:284
@ UNIT_STATE_LOST_CONTROL
Definition Unit.h:302
@ UNIT_STATE_ROAMING
Definition Unit.h:265
float GetWanderDistance() const
Definition Creature.h:362
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:635
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