[7915] Implement more stricted checks and limitations at loading creature addon data.
[getmangos.git] / src / game / ConfusedMovementGenerator.cpp
blobd591d151f6ecc4de7dadc2729980204e11d069ee
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();
34 uint32 mapid=unit.GetMapId();
36 Map const* map = MapManager::Instance().GetBaseMap(mapid);
38 i_nextMove = 1;
40 bool is_water_ok, is_land_ok;
41 _InitSpecific(unit, is_water_ok, is_land_ok);
43 for(unsigned int idx=0; idx < MAX_CONF_WAYPOINTS+1; ++idx)
45 const float wanderX=wander_distance*rand_norm() - wander_distance/2;
46 const float wanderY=wander_distance*rand_norm() - wander_distance/2;
48 i_waypoints[idx][0] = x + wanderX;
49 i_waypoints[idx][1] = y + wanderY;
51 // prevent invalid coordinates generation
52 MaNGOS::NormalizeMapCoord(i_waypoints[idx][0]);
53 MaNGOS::NormalizeMapCoord(i_waypoints[idx][1]);
55 bool is_water = map->IsInWater(i_waypoints[idx][0],i_waypoints[idx][1],z);
56 // if generated wrong path just ignore
57 if ((is_water && !is_water_ok) || (!is_water && !is_land_ok))
59 i_waypoints[idx][0] = idx > 0 ? i_waypoints[idx-1][0] : x;
60 i_waypoints[idx][1] = idx > 0 ? i_waypoints[idx-1][1] : y;
63 unit.UpdateGroundPositionZ(i_waypoints[idx][0],i_waypoints[idx][1],z);
64 i_waypoints[idx][2] = z;
67 unit.StopMoving();
68 unit.RemoveUnitMovementFlag(MOVEMENTFLAG_WALK_MODE);
69 unit.addUnitState(UNIT_STAT_CONFUSED);
72 template<>
73 void
74 ConfusedMovementGenerator<Creature>::_InitSpecific(Creature &creature, bool &is_water_ok, bool &is_land_ok)
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<class T>
143 void
144 ConfusedMovementGenerator<T>::Finalize(T &unit)
146 unit.clearUnitState(UNIT_STAT_CONFUSED);
149 template void ConfusedMovementGenerator<Player>::Initialize(Player &player);
150 template void ConfusedMovementGenerator<Creature>::Initialize(Creature &creature);
151 template void ConfusedMovementGenerator<Player>::Finalize(Player &player);
152 template void ConfusedMovementGenerator<Creature>::Finalize(Creature &creature);
153 template void ConfusedMovementGenerator<Player>::Reset(Player &player);
154 template void ConfusedMovementGenerator<Creature>::Reset(Creature &creature);
155 template bool ConfusedMovementGenerator<Player>::Update(Player &player, const uint32 &diff);
156 template bool ConfusedMovementGenerator<Creature>::Update(Creature &creature, const uint32 &diff);