forked from TrinityCore/TrinityCore
/
ConfusedMovementGenerator.cpp
executable file
·129 lines (111 loc) · 4.13 KB
/
ConfusedMovementGenerator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright (C) 2008-2012 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Creature.h"
#include "MapManager.h"
#include "ConfusedMovementGenerator.h"
#include "PathGenerator.h"
#include "VMapFactory.h"
#include "MoveSplineInit.h"
#include "MoveSpline.h"
#include "Player.h"
#ifdef MAP_BASED_RAND_GEN
#define rand_norm() unit.rand_norm()
#define urand(a, b) unit.urand(a, b)
#endif
template<class T>
void ConfusedMovementGenerator<T>::Initialize(T* unit)
{
unit->GetPosition(i_x, i_y, i_z);
unit->StopMoving();
unit->SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_CONFUSED);
unit->AddUnitState(UNIT_STATE_CONFUSED | UNIT_STATE_CONFUSED_MOVE);
}
template<class T>
void ConfusedMovementGenerator<T>::Reset(T* unit)
{
i_nextMoveTime.Reset(0);
unit->StopMoving();
unit->AddUnitState(UNIT_STATE_CONFUSED | UNIT_STATE_CONFUSED_MOVE);
}
template<class T>
bool ConfusedMovementGenerator<T>::Update(T* unit, const uint32& diff)
{
if (unit->HasUnitState(UNIT_STATE_ROOT | UNIT_STATE_STUNNED | UNIT_STATE_DISTRACTED))
return true;
if (i_nextMoveTime.Passed())
{
// currently moving, update location
unit->AddUnitState(UNIT_STATE_CONFUSED_MOVE);
if (unit->movespline->Finalized())
i_nextMoveTime.Reset(urand(800, 1500));
}
else
{
// waiting for next move
i_nextMoveTime.Update(diff);
if (i_nextMoveTime.Passed())
{
// start moving
unit->AddUnitState(UNIT_STATE_CONFUSED_MOVE);
float x = i_x + (4.0f * (float)rand_norm() - 2.0f);
float y = i_y + (4.0f * (float)rand_norm() - 2.0f);
Trinity::NormalizeMapCoord(x);
Trinity::NormalizeMapCoord(y);
float z = unit->GetBaseMap()->GetHeight(unit->GetPhaseMask(), x, y, 10.0f, true);
if (z <= INVALID_HEIGHT || fabs(i_z - z) > 10.0f || !unit->IsWithinLOS(x, y, z))
{
i_nextMoveTime.Reset(100);
return true;
}
PathGenerator path(unit);
path.SetPathLengthLimit(20.0f);
bool result = path.CalculatePath(x, y, z);
if (!result || (path.GetPathType() & PATHFIND_NOPATH))
{
i_nextMoveTime.Reset(100); // short reset
return true;
}
Movement::MoveSplineInit init(unit);
init.MovebyPath(path.GetPath());
init.SetWalk(true);
init.Launch();
}
}
return true;
}
template<>
void ConfusedMovementGenerator<Player>::Finalize(Player* unit)
{
unit->RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_CONFUSED);
unit->ClearUnitState(UNIT_STATE_CONFUSED | UNIT_STATE_CONFUSED_MOVE);
unit->StopMoving();
}
template<>
void ConfusedMovementGenerator<Creature>::Finalize(Creature* unit)
{
unit->RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_CONFUSED);
unit->ClearUnitState(UNIT_STATE_CONFUSED | UNIT_STATE_CONFUSED_MOVE);
if (unit->getVictim())
unit->SetTarget(unit->getVictim()->GetGUID());
}
template void ConfusedMovementGenerator<Player>::Initialize(Player*);
template void ConfusedMovementGenerator<Creature>::Initialize(Creature*);
template void ConfusedMovementGenerator<Player>::Reset(Player*);
template void ConfusedMovementGenerator<Creature>::Reset(Creature*);
template bool ConfusedMovementGenerator<Player>::Update(Player*, const uint32&);
template bool ConfusedMovementGenerator<Creature>::Update(Creature*, const uint32&);