TrinityCore
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>
28ConfusedMovementGenerator<T>::ConfusedMovementGenerator() : _timer(0), _reference(0.f, 0.f, 0.f)
29{
34}
35
36template<class T>
38{
40}
41
42template<class T>
44{
47
48 if (!owner || !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 || !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
83
84 // waiting for next move
85 _timer.Update(diff);
86 if ((MovementGenerator::HasFlag(MOVEMENTGENERATOR_FLAG_SPEED_UPDATE_PENDING) && !owner->movespline->Finalized()) || (_timer.Passed() && owner->movespline->Finalized()))
87 {
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>
139
140template<>
141void ConfusedMovementGenerator<Player>::DoFinalize(Player* owner, bool active, bool/* movementInform*/)
142{
144
145 if (active)
146 {
148 owner->StopMoving();
149 }
150}
151
152template<>
153void ConfusedMovementGenerator<Creature>::DoFinalize(Creature* owner, bool active, bool/* movementInform*/)
154{
156
157 if (active)
158 {
161 if (owner->GetVictim())
162 owner->SetTarget(owner->EnsureVictim()->GetGUID());
163 }
164}
165
#define M_PI
Definition: Common.h:115
int32_t int32
Definition: Define.h:138
uint32_t uint32
Definition: Define.h:142
@ 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
Definition: PathGenerator.h:47
@ PATHFIND_FARFROMPOLY
Definition: PathGenerator.h:52
@ PATHFIND_SHORTCUT
Definition: PathGenerator.h:45
float frand(float min, float max)
Definition: Random.cpp:55
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:42
@ UNIT_FLAG_CONFUSED
Definition: UnitDefines.h:166
@ UNIT_STATE_NOT_MOVE
Definition: Unit.h:300
@ UNIT_STATE_CONFUSED
Definition: Unit.h:266
@ UNIT_STATE_CONFUSED_MOVE
Definition: Unit.h:279
MovementGeneratorType GetMovementGeneratorType() const override
void SetTarget(ObjectGuid const &guid) override
Definition: Creature.cpp:3419
void AddFlag(uint16 const flag)
bool HasFlag(uint16 const flag) const
void RemoveFlag(uint16 const flag)
void SetWalk(bool enable)
void MovebyPath(PointsArray const &path, int32 pointId=0)
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:159
void ClearUnitState(uint32 f)
Definition: Unit.h:733
void StopMoving()
Definition: Unit.cpp:10049
Unit * EnsureVictim() const
Definition: Unit.h:717
Unit * GetVictim() const
Definition: Unit.h:715
void RemoveUnitFlag(UnitFlags flags)
Definition: Unit.h:834
constexpr float GetPositionX() const
Definition: Position.h:76
constexpr float GetPositionY() const
Definition: Position.h:77
constexpr float GetPositionZ() const
Definition: Position.h:78