[9581] Fixed apply damage reduction to melee/ranged damage.
[getmangos.git] / src / game / ConfusedMovementGenerator.cpp
blobb44740640b2d38ca41013e9f3d6c9ed8c40fd94f
1 /*
2 * Copyright (C) 2005-2010 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_f() - wander_distance/2;
45 const float wanderY=wander_distance*rand_norm_f() - 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|UNIT_STAT_CONFUSED_MOVE);
70 template<>
71 void
72 ConfusedMovementGenerator<Creature>::_InitSpecific(Creature &creature, bool &is_water_ok, bool &is_land_ok)
74 creature.RemoveSplineFlag(SPLINEFLAG_WALKMODE);
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 ConfusedMovementGenerator<T>::Interrupt(T &unit)
91 // confused state still applied while movegen disabled
92 unit.clearUnitState(UNIT_STAT_CONFUSED_MOVE);
95 template<class T>
96 void ConfusedMovementGenerator<T>::Reset(T &unit)
98 i_nextMove = 1;
99 i_nextMoveTime.Reset(0);
100 i_destinationHolder.ResetUpdate();
101 unit.StopMoving();
102 unit.addUnitState(UNIT_STAT_CONFUSED|UNIT_STAT_CONFUSED_MOVE);
105 template<class T>
106 bool ConfusedMovementGenerator<T>::Update(T &unit, const uint32 &diff)
108 if(!&unit)
109 return true;
111 // ignore in case other no reaction state
112 if (unit.hasUnitState(UNIT_STAT_CAN_NOT_REACT & ~UNIT_STAT_CONFUSED))
113 return true;
115 if (i_nextMoveTime.Passed())
117 // currently moving, update location
118 unit.addUnitState(UNIT_STAT_CONFUSED_MOVE);
119 Traveller<T> traveller(unit);
120 if (i_destinationHolder.UpdateTraveller(traveller, diff, false))
122 if (!IsActive(unit)) // force stop processing (movement can move out active zone with cleanup movegens list)
123 return true; // not expire now, but already lost
125 if (i_destinationHolder.HasArrived())
127 // arrived, stop and wait a bit
128 unit.StopMoving();
130 i_nextMove = urand(1,MAX_CONF_WAYPOINTS);
131 i_nextMoveTime.Reset(urand(0, 1500-1)); // TODO: check the minimum reset time, should be probably higher
135 else
137 // waiting for next move
138 i_nextMoveTime.Update(diff);
139 if( i_nextMoveTime.Passed() )
141 // start moving
142 unit.addUnitState(UNIT_STAT_CONFUSED_MOVE);
143 assert( i_nextMove <= MAX_CONF_WAYPOINTS );
144 const float x = i_waypoints[i_nextMove][0];
145 const float y = i_waypoints[i_nextMove][1];
146 const float z = i_waypoints[i_nextMove][2];
147 Traveller<T> traveller(unit);
148 i_destinationHolder.SetDestination(traveller, x, y, z);
151 return true;
154 template<>
155 void ConfusedMovementGenerator<Player>::Finalize(Player &unit)
157 unit.clearUnitState(UNIT_STAT_CONFUSED|UNIT_STAT_CONFUSED_MOVE);
160 template<>
161 void ConfusedMovementGenerator<Creature>::Finalize(Creature &unit)
163 unit.clearUnitState(UNIT_STAT_CONFUSED|UNIT_STAT_CONFUSED_MOVE);
164 unit.AddSplineFlag(SPLINEFLAG_WALKMODE);
167 template void ConfusedMovementGenerator<Player>::Initialize(Player &player);
168 template void ConfusedMovementGenerator<Creature>::Initialize(Creature &creature);
169 template void ConfusedMovementGenerator<Player>::Interrupt(Player &player);
170 template void ConfusedMovementGenerator<Creature>::Interrupt(Creature &creature);
171 template void ConfusedMovementGenerator<Player>::Reset(Player &player);
172 template void ConfusedMovementGenerator<Creature>::Reset(Creature &creature);
173 template bool ConfusedMovementGenerator<Player>::Update(Player &player, const uint32 &diff);
174 template bool ConfusedMovementGenerator<Creature>::Update(Creature &creature, const uint32 &diff);