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
56 _path = nullptr;
57 SetTargetLocation(owner);
58 return true;
59}
60
61template<class T>
68
69template<class T>
71{
72 if (!owner || !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
84
85 // waiting for next move
86 _timer.Update(diff);
87 if ((MovementGenerator::HasFlag(MOVEMENTGENERATOR_FLAG_SPEED_UPDATE_PENDING) && !owner->movespline->Finalized()) || (_timer.Passed() && owner->movespline->Finalized()))
88 {
90 SetTargetLocation(owner);
91 }
92
93 return true;
94}
95
96template<class T>
102
103template<class T>
105
106template<>
107void ConfusedMovementGenerator<Player>::DoFinalize(Player* owner, bool active, bool/* movementInform*/)
108{
110
111 if (active)
112 {
114 owner->StopMoving();
115 }
116}
117
118template<>
119void ConfusedMovementGenerator<Creature>::DoFinalize(Creature* owner, bool active, bool/* movementInform*/)
120{
122
123 if (active)
124 {
127 if (owner->GetVictim())
128 owner->SetTarget(owner->EnsureVictim()->GetGUID());
129 }
130}
131
132template<class T>
134{
135 if (!owner)
136 return;
137
138 Position destination(_x, _y, _z);
139 float distance = 4.0f * frand(0.0f, 1.0f) - 2.0f;
140 float angle = frand(0.0f, 1.0f) * float(M_PI) * 2.0f;
141 owner->MovePositionToFirstCollision(destination, distance, angle);
142
143 // Check if the destination is in LOS
144 if (!owner->IsWithinLOS(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ()))
145 {
146 // Retry later on
147 _timer.Reset(200);
148 return;
149 }
150
151 if (!_path)
152 {
153 _path = std::make_unique<PathGenerator>(owner);
154 _path->SetPathLengthLimit(30.0f);
155 }
156
157 bool result = _path->CalculatePath(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ());
158 if (!result || (_path->GetPathType() & PATHFIND_NOPATH)
159 || (_path->GetPathType() & PATHFIND_SHORTCUT)
160 || (_path->GetPathType() & PATHFIND_FARFROMPOLY))
161 {
162 _timer.Reset(100);
163 return;
164 }
165
166 owner->AddUnitState(UNIT_STATE_CONFUSED_MOVE);
167
168 Movement::MoveSplineInit init(owner);
169 init.MovebyPath(_path->GetPath());
170 init.SetWalk(true);
171 int32 traveltime = init.Launch();
172 _timer.Reset(traveltime + urand(800, 1500));
173}
174
#define M_PI
Definition Common.h:71
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:279
@ UNIT_STATE_CONFUSED
Definition Unit.h:245
@ UNIT_STATE_CONFUSED_MOVE
Definition Unit.h:258
MovementGeneratorType GetMovementGeneratorType() const override
void SetTarget(ObjectGuid const &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:891
void StopMoving(bool force=false)
Definition Unit.cpp:10289
Unit * EnsureVictim() const
Definition Unit.h:875
Unit * GetVictim() const
Definition Unit.h:873
void RemoveUnitFlag(UnitFlags flags)
Definition Unit.h:969
float GetPositionZ() const
Definition Position.h:81
float GetPositionX() const
Definition Position.h:79
float GetPositionY() const
Definition Position.h:80