[8483] Implement glyph 43361.
[getmangos.git] / src / game / ConfusedMovementGenerator.cpp
blob84ebdfc6bca299c3f9f64452081efadb2e9bb3fd
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Creature.h"
20 #include "MapManager.h"
21 #include "Opcodes.h"
22 #include "ConfusedMovementGenerator.h"
23 #include "DestinationHolderImp.h"
25 template<class T>
26 void
27 ConfusedMovementGenerator<T>::Initialize(T &unit)
29 const float wander_distance=11;
30 float x,y,z;
31 x = unit.GetPositionX();
32 y = unit.GetPositionY();
33 z = unit.GetPositionZ();
35 Map const* map = unit.GetBaseMap();
37 i_nextMove = 1;
39 bool is_water_ok, is_land_ok;
40 _InitSpecific(unit, is_water_ok, is_land_ok);
42 for(unsigned int idx=0; idx < MAX_CONF_WAYPOINTS+1; ++idx)
44 const float wanderX=wander_distance*rand_norm() - wander_distance/2;
45 const float wanderY=wander_distance*rand_norm() - wander_distance/2;
47 i_waypoints[idx][0] = x + wanderX;
48 i_waypoints[idx][1] = y + wanderY;
50 // prevent invalid coordinates generation
51 MaNGOS::NormalizeMapCoord(i_waypoints[idx][0]);
52 MaNGOS::NormalizeMapCoord(i_waypoints[idx][1]);
54 bool is_water = map->IsInWater(i_waypoints[idx][0],i_waypoints[idx][1],z);
55 // if generated wrong path just ignore
56 if ((is_water && !is_water_ok) || (!is_water && !is_land_ok))
58 i_waypoints[idx][0] = idx > 0 ? i_waypoints[idx-1][0] : x;
59 i_waypoints[idx][1] = idx > 0 ? i_waypoints[idx-1][1] : y;
62 unit.UpdateGroundPositionZ(i_waypoints[idx][0],i_waypoints[idx][1],z);
63 i_waypoints[idx][2] = z;
66 unit.StopMoving();
67 unit.addUnitState(UNIT_STAT_CONFUSED);
70 template<>
71 void
72 ConfusedMovementGenerator<Creature>::_InitSpecific(Creature &creature, bool &is_water_ok, bool &is_land_ok)
74 creature.RemoveMonsterMoveFlag(MONSTER_MOVE_WALK);
76 is_water_ok = creature.canSwim();
77 is_land_ok = creature.canWalk();
80 template<>
81 void
82 ConfusedMovementGenerator<Player>::_InitSpecific(Player &, bool &is_water_ok, bool &is_land_ok)
84 is_water_ok = true;
85 is_land_ok = true;
88 template<class T>
89 void
90 ConfusedMovementGenerator<T>::Reset(T &unit)
92 i_nextMove = 1;
93 i_nextMoveTime.Reset(0);
94 i_destinationHolder.ResetUpdate();
95 unit.StopMoving();
98 template<class T>
99 bool
100 ConfusedMovementGenerator<T>::Update(T &unit, const uint32 &diff)
102 if(!&unit)
103 return true;
105 if(unit.hasUnitState(UNIT_STAT_ROOT | UNIT_STAT_STUNNED | UNIT_STAT_DISTRACTED))
106 return true;
108 if( i_nextMoveTime.Passed() )
110 // currently moving, update location
111 Traveller<T> traveller(unit);
112 if( i_destinationHolder.UpdateTraveller(traveller, diff, false))
114 if( i_destinationHolder.HasArrived())
116 // arrived, stop and wait a bit
117 unit.StopMoving();
119 i_nextMove = urand(1,MAX_CONF_WAYPOINTS);
120 i_nextMoveTime.Reset(urand(0, 1500-1)); // TODO: check the minimum reset time, should be probably higher
124 else
126 // waiting for next move
127 i_nextMoveTime.Update(diff);
128 if( i_nextMoveTime.Passed() )
130 // start moving
131 assert( i_nextMove <= MAX_CONF_WAYPOINTS );
132 const float x = i_waypoints[i_nextMove][0];
133 const float y = i_waypoints[i_nextMove][1];
134 const float z = i_waypoints[i_nextMove][2];
135 Traveller<T> traveller(unit);
136 i_destinationHolder.SetDestination(traveller, x, y, z);
139 return true;
142 template<>
143 void ConfusedMovementGenerator<Player>::Finalize(Player &unit)
145 unit.clearUnitState(UNIT_STAT_CONFUSED);
148 template<>
149 void ConfusedMovementGenerator<Creature>::Finalize(Creature &unit)
151 unit.clearUnitState(UNIT_STAT_CONFUSED);
152 unit.AddMonsterMoveFlag(MONSTER_MOVE_WALK);
155 template void ConfusedMovementGenerator<Player>::Initialize(Player &player);
156 template void ConfusedMovementGenerator<Creature>::Initialize(Creature &creature);
157 template void ConfusedMovementGenerator<Player>::Reset(Player &player);
158 template void ConfusedMovementGenerator<Creature>::Reset(Creature &creature);
159 template bool ConfusedMovementGenerator<Player>::Update(Player &player, const uint32 &diff);
160 template bool ConfusedMovementGenerator<Creature>::Update(Creature &creature, const uint32 &diff);