server/src/game/RandomMovementGenerator.cpp
2012-02-06 23:18:34 +01:00

114 lines
3.2 KiB
C++

/*
* Copyright (C) 2005-2012 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "Creature.h"
#include "MapManager.h"
#include "RandomMovementGenerator.h"
#include "Map.h"
#include "Util.h"
#include "movement/MoveSplineInit.h"
#include "movement/MoveSpline.h"
template<>
void RandomMovementGenerator<Creature>::_setRandomLocation(Creature &creature)
{
float respX, respY, respZ, respO, wander_distance;
creature.GetRespawnCoord(respX, respY, respZ, &respO, &wander_distance);
const float angle = rand_norm_f() * (M_PI_F*2.0f);
const float range = rand_norm_f() * wander_distance;
float destX = respX + range * cos(angle);
float destY = respY + range * sin(angle);
float destZ = creature.GetPositionZ();
creature.UpdateAllowedPositionZ(destX, destY, destZ);
creature.addUnitState(UNIT_STAT_ROAMING_MOVE);
Movement::MoveSplineInit init(creature);
init.MoveTo(destX, destY, destZ, true);
init.SetWalk(true);
init.Launch();
if (creature.CanFly())
i_nextMoveTime.Reset(0);
else
i_nextMoveTime.Reset(urand(500, 10000));
}
template<>
void RandomMovementGenerator<Creature>::Initialize(Creature &creature)
{
if (!creature.isAlive())
return;
creature.addUnitState(UNIT_STAT_ROAMING|UNIT_STAT_ROAMING_MOVE);
_setRandomLocation(creature);
}
template<>
void RandomMovementGenerator<Creature>::Reset(Creature &creature)
{
Initialize(creature);
}
template<>
void RandomMovementGenerator<Creature>::Interrupt(Creature &creature)
{
creature.clearUnitState(UNIT_STAT_ROAMING|UNIT_STAT_ROAMING_MOVE);
creature.SetWalk(false);
}
template<>
void RandomMovementGenerator<Creature>::Finalize(Creature &creature)
{
creature.clearUnitState(UNIT_STAT_ROAMING|UNIT_STAT_ROAMING_MOVE);
creature.SetWalk(false);
}
template<>
bool RandomMovementGenerator<Creature>::Update(Creature &creature, const uint32 &diff)
{
if (creature.hasUnitState(UNIT_STAT_NOT_MOVE))
{
i_nextMoveTime.Reset(0); // Expire the timer
creature.clearUnitState(UNIT_STAT_ROAMING_MOVE);
return true;
}
if (creature.movespline->Finalized())
{
i_nextMoveTime.Update(diff);
if (i_nextMoveTime.Passed())
_setRandomLocation(creature);
}
return true;
}
template<>
bool RandomMovementGenerator<Creature>::GetResetPosition(Creature& c, float& x, float& y, float& z)
{
float radius;
c.GetRespawnCoord(x, y, z, NULL, &radius);
// use current if in range
if (c.IsWithinDist2d(x,y,radius))
c.GetPosition(x,y,z);
return true;
}