TrinityCore
Loading...
Searching...
No Matches
ConfusedMovementGenerator.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 "MovementDefines.h"
21#include "MoveSpline.h"
22#include "MoveSplineInit.h"
23#include "PathGenerator.h"
24#include "Player.h"
25#include "Random.h"
26
27template<class T>
35
36template<class T>
41
42template<class T>
44{
47
48 if (!owner->IsAlive())
49 return;
50
51 // TODO: UNIT_FIELD_FLAGS should not be handled by generators
52 owner->SetUnitFlag(UNIT_FLAG_CONFUSED);
53 owner->StopMoving();
54
55 _timer.Reset(0);
56 owner->GetPosition(_reference.m_positionX, _reference.m_positionY, _reference.m_positionZ);
57 _path = nullptr;
58}
59
60template<class T>
62{
64
65 DoInitialize(owner);
66}
67
68template<class T>
70{
71 if (!owner->IsAlive())
72 return false;
73
74 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || owner->IsMovementPreventedByCasting())
75 {
77 owner->StopMoving();
78 _path = nullptr;
79 return true;
80 }
81 else
82 this->RemoveFlag(MOVEMENTGENERATOR_FLAG_INTERRUPTED);
83
84 // waiting for next move
85 _timer.Update(diff);
86 if ((this->HasFlag(MOVEMENTGENERATOR_FLAG_SPEED_UPDATE_PENDING) && !owner->movespline->Finalized()) || (_timer.Passed() && owner->movespline->Finalized()))
87 {
88 this->RemoveFlag(MOVEMENTGENERATOR_FLAG_TRANSITORY);
89
90 Position destination(_reference);
91 float distance = 4.0f * frand(0.0f, 1.0f) - 2.0f;
92 float angle = frand(0.0f, 1.0f) * float(M_PI) * 2.0f;
93 owner->MovePositionToFirstCollision(destination, distance, angle);
94
95 // Check if the destination is in LOS
96 if (!owner->IsWithinLOS(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ()))
97 {
98 // Retry later on
99 _timer.Reset(200);
100 return true;
101 }
102
103 if (!_path)
104 {
105 _path = std::make_unique<PathGenerator>(owner);
106 _path->SetPathLengthLimit(30.0f);
107 }
108
109 bool result = _path->CalculatePath(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ());
110 if (!result || (_path->GetPathType() & PATHFIND_NOPATH)
111 || (_path->GetPathType() & PATHFIND_SHORTCUT)
112 || (_path->GetPathType() & PATHFIND_FARFROMPOLY))
113 {
114 _timer.Reset(100);
115 return true;
116 }
117
118 owner->AddUnitState(UNIT_STATE_CONFUSED_MOVE);
119
120 Movement::MoveSplineInit init(owner);
121 init.MovebyPath(_path->GetPath());
122 init.SetWalk(true);
123 int32 traveltime = init.Launch();
124 _timer.Reset(traveltime + urand(800, 1500));
125 }
126
127 return true;
128}
129
130template<class T>
132{
134 owner->ClearUnitState(UNIT_STATE_CONFUSED_MOVE);
135}
136
137template<class T>
138void ConfusedMovementGenerator<T>::DoFinalize(T* owner, bool active, bool/* movementInform*/)
139{
141
142 if (active)
143 {
144 owner->RemoveUnitFlag(UNIT_FLAG_CONFUSED);
145 owner->ClearUnitState(UNIT_STATE_CONFUSED_MOVE);
146
147 if constexpr (std::is_base_of_v<Creature, T>)
148 {
149 if (Unit* victim = owner->GetVictim())
150 owner->SetTarget(victim->GetGUID());
151 }
152 else if constexpr (std::is_base_of_v<Player, T>)
153 owner->StopMoving();
154 }
155}
156
#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_HIGHEST
MovementGeneratorType
@ CONFUSED_MOTION_TYPE
@ MOVEMENTGENERATOR_FLAG_INITIALIZATION_PENDING
@ MOVEMENTGENERATOR_FLAG_DEACTIVATED
@ MOVEMENTGENERATOR_FLAG_FINALIZED
@ MOVEMENTGENERATOR_FLAG_TRANSITORY
@ MOVEMENTGENERATOR_FLAG_INTERRUPTED
@ MOVEMENTGENERATOR_FLAG_INITIALIZED
@ MOVEMENTGENERATOR_FLAG_SPEED_UPDATE_PENDING
@ PATHFIND_NOPATH
@ PATHFIND_FARFROMPOLY
@ PATHFIND_SHORTCUT
float frand(float min, float max)
Definition Random.cpp:55
uint32 urand(uint32 min, uint32 max)
Definition Random.cpp:42
@ UNIT_FLAG_CONFUSED
@ UNIT_STATE_NOT_MOVE
Definition Unit.h:306
@ UNIT_STATE_CONFUSED
Definition Unit.h:272
@ UNIT_STATE_CONFUSED_MOVE
Definition Unit.h:285
MovementGeneratorType GetMovementGeneratorType() const override
void SetWalk(bool enable)
void MovebyPath(std::span< Vector3 const > path, int32 pointId=0)
Definition Unit.h:635
Unit * GetVictim() const
Definition Unit.h:726
constexpr float GetPositionX() const
Definition Position.h:87
constexpr float GetPositionY() const
Definition Position.h:88
constexpr float GetPositionZ() const
Definition Position.h:89