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>
34
35template<class T>
40
41template<class T>
43{
46
47 if (!owner->IsAlive())
48 return false;
49
50 // TODO: UNIT_FIELD_FLAGS should not be handled by generators
51 owner->SetUnitFlag(UNIT_FLAG_CONFUSED);
52
53 _timer.Reset(0);
54 owner->GetPosition(_reference.m_positionX, _reference.m_positionY, _reference.m_positionZ);
55
56 _path = nullptr;
57 SetTargetLocation(owner);
58 return true;
59}
60
61template<class T>
63{
65
66 return DoInitialize(owner);
67}
68
69template<class T>
71{
72 if (!owner->IsAlive())
73 return false;
74
75 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || owner->IsMovementPreventedByCasting())
76 {
78 owner->StopMoving();
79 _path = nullptr;
80 return true;
81 }
82 else
83 this->RemoveFlag(MOVEMENTGENERATOR_FLAG_INTERRUPTED);
84
85 // waiting for next move
86 _timer.Update(diff);
87 if ((this->HasFlag(MOVEMENTGENERATOR_FLAG_SPEED_UPDATE_PENDING) && !owner->movespline->Finalized()) || (_timer.Passed() && owner->movespline->Finalized()))
88 {
89 this->RemoveFlag(MOVEMENTGENERATOR_FLAG_TRANSITORY);
90 SetTargetLocation(owner);
91 }
92
93 return true;
94}
95
96template<class T>
98{
100 owner->ClearUnitState(UNIT_STATE_CONFUSED_MOVE);
101}
102
103template<class T>
104void ConfusedMovementGenerator<T>::DoFinalize(T* owner, bool active, bool/* movementInform*/)
105{
107
108 if (active)
109 {
110 owner->RemoveUnitFlag(UNIT_FLAG_CONFUSED);
111 owner->ClearUnitState(UNIT_STATE_CONFUSED_MOVE);
112
113 if constexpr (std::is_base_of_v<Creature, T>)
114 {
115 if (Unit* victim = owner->GetVictim())
116 owner->SetTarget(victim->GetGUID());
117 }
118 else if constexpr (std::is_base_of_v<Player, T>)
119 owner->StopMoving();
120 }
121}
122
123template<class T>
125{
126 if (!owner)
127 return;
128
129 Position destination(_reference);
130 float distance = 4.0f * frand(0.0f, 1.0f) - 2.0f;
131 float angle = frand(0.0f, 1.0f) * float(M_PI) * 2.0f;
132 owner->MovePositionToFirstCollision(destination, distance, angle);
133
134 // Check if the destination is in LOS
135 if (!owner->IsWithinLOS(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ()))
136 {
137 // Retry later on
138 _timer.Reset(200);
139 return;
140 }
141
142 if (!_path)
143 {
144 _path = std::make_unique<PathGenerator>(owner);
145 _path->SetPathLengthLimit(30.0f);
146 }
147
148 bool result = _path->CalculatePath(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ());
149 if (!result || (_path->GetPathType() & PATHFIND_NOPATH)
150 || (_path->GetPathType() & PATHFIND_SHORTCUT)
151 || (_path->GetPathType() & PATHFIND_FARFROMPOLY))
152 {
153 _timer.Reset(100);
154 return;
155 }
156
157 owner->AddUnitState(UNIT_STATE_CONFUSED_MOVE);
158
159 Movement::MoveSplineInit init(owner);
160 init.MovebyPath(_path->GetPath());
161 init.SetWalk(true);
162 int32 traveltime = init.Launch();
163 _timer.Reset(traveltime + urand(800, 1500));
164}
165
#define M_PI
Definition Common.h:118
int32_t int32
Definition Define.h:150
uint32_t uint32
Definition Define.h:154
@ 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:56
uint32 urand(uint32 min, uint32 max)
Definition Random.cpp:43
@ UNIT_FLAG_CONFUSED
@ UNIT_STATE_NOT_MOVE
Definition Unit.h:320
@ UNIT_STATE_CONFUSED
Definition Unit.h:286
@ UNIT_STATE_CONFUSED_MOVE
Definition Unit.h:299
MovementGeneratorType GetMovementGeneratorType() const override
void SetWalk(bool enable)
void MovebyPath(std::span< Vector3 const > path, int32 pointId=0)
Definition Unit.h:647
Unit * GetVictim() const
Definition Unit.h:738
constexpr float GetPositionX() const
Definition Position.h:87
constexpr float GetPositionY() const
Definition Position.h:88
constexpr float GetPositionZ() const
Definition Position.h:89