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 || !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(_x, _y, _z);
55 _path = nullptr;
56 return true;
57}
58
59template<class T>
66
67template<class T>
69{
70 if (!owner || !owner->IsAlive())
71 return false;
72
73 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || owner->IsMovementPreventedByCasting())
74 {
76 owner->StopMoving();
77 _path = nullptr;
78 return true;
79 }
80 else
82
83 // waiting for next move
84 _timer.Update(diff);
85 if ((MovementGenerator::HasFlag(MOVEMENTGENERATOR_FLAG_SPEED_UPDATE_PENDING) && !owner->movespline->Finalized()) || (_timer.Passed() && owner->movespline->Finalized()))
86 {
88
89 Position destination(_x, _y, _z);
90 float distance = 4.0f * frand(0.0f, 1.0f) - 2.0f;
91 float angle = frand(0.0f, 1.0f) * float(M_PI) * 2.0f;
92 owner->MovePositionToFirstCollision(destination, distance, angle);
93
94 // Check if the destination is in LOS
95 if (!owner->IsWithinLOS(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ()))
96 {
97 // Retry later on
98 _timer.Reset(200);
99 return true;
100 }
101
102 if (!_path)
103 {
104 _path = std::make_unique<PathGenerator>(owner);
105 _path->SetPathLengthLimit(30.0f);
106 }
107
108 bool result = _path->CalculatePath(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ());
109 if (!result || (_path->GetPathType() & PATHFIND_NOPATH)
110 || (_path->GetPathType() & PATHFIND_SHORTCUT)
111 || (_path->GetPathType() & PATHFIND_FARFROMPOLY))
112 {
113 _timer.Reset(100);
114 return true;
115 }
116
117 owner->AddUnitState(UNIT_STATE_CONFUSED_MOVE);
118
119 Movement::MoveSplineInit init(owner);
120 init.MovebyPath(_path->GetPath());
121 init.SetWalk(true);
122 int32 traveltime = init.Launch();
123 _timer.Reset(traveltime + urand(800, 1500));
124 }
125
126 return true;
127}
128
129template<class T>
135
136template<class T>
138
139template<>
140void ConfusedMovementGenerator<Player>::DoFinalize(Player* owner, bool active, bool/* movementInform*/)
141{
143
144 if (active)
145 {
147 owner->StopMoving();
148 }
149}
150
151template<>
152void ConfusedMovementGenerator<Creature>::DoFinalize(Creature* owner, bool active, bool/* movementInform*/)
153{
155
156 if (active)
157 {
160 if (owner->GetVictim())
161 owner->SetTarget(owner->EnsureVictim()->GetGUID());
162 }
163}
164
#define M_PI
Definition Common.h:72
int32_t int32
Definition Define.h:129
uint32_t uint32
Definition Define.h:133
@ 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:265
@ UNIT_STATE_CONFUSED
Definition Unit.h:231
@ UNIT_STATE_CONFUSED_MOVE
Definition Unit.h:244
MovementGeneratorType GetMovementGeneratorType() const override
void SetTarget(ObjectGuid guid) override
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:78
void ClearUnitState(uint32 f)
Definition Unit.h:877
void StopMoving(bool force=false)
Definition Unit.cpp:10312
Unit * EnsureVictim() const
Definition Unit.h:861
Unit * GetVictim() const
Definition Unit.h:859
void RemoveUnitFlag(UnitFlags flags)
Definition Unit.h:955
float GetPositionZ() const
Definition Position.h:81
float GetPositionX() const
Definition Position.h:79
float GetPositionY() const
Definition Position.h:80