2014-05-13 13:07:27 -04:00
|
|
|
#include "aiavoiddoor.hpp"
|
2015-07-25 01:48:34 +02:00
|
|
|
|
2019-08-25 17:21:14 +02:00
|
|
|
#include <components/misc/rng.hpp>
|
|
|
|
|
2014-05-13 13:07:27 -04:00
|
|
|
#include "../mwbase/world.hpp"
|
|
|
|
#include "../mwbase/environment.hpp"
|
2015-05-28 15:44:58 +02:00
|
|
|
#include "../mwbase/mechanicsmanager.hpp"
|
2016-06-17 23:07:16 +09:00
|
|
|
|
2014-05-13 13:07:27 -04:00
|
|
|
#include "../mwworld/class.hpp"
|
2016-06-17 23:07:16 +09:00
|
|
|
|
2014-05-13 13:07:27 -04:00
|
|
|
#include "creaturestats.hpp"
|
|
|
|
#include "movement.hpp"
|
2015-08-21 21:12:39 +12:00
|
|
|
#include "actorutil.hpp"
|
2014-05-13 13:07:27 -04:00
|
|
|
#include "steering.hpp"
|
|
|
|
|
2019-08-25 17:21:14 +02:00
|
|
|
static const int MAX_DIRECTIONS = 4;
|
|
|
|
|
2015-12-19 15:57:37 +01:00
|
|
|
MWMechanics::AiAvoidDoor::AiAvoidDoor(const MWWorld::ConstPtr& doorPtr)
|
2020-05-17 22:10:36 +02:00
|
|
|
: mDuration(1), mDoorPtr(doorPtr), mDirection(0)
|
2014-05-13 13:07:27 -04:00
|
|
|
{
|
2014-05-14 13:38:10 -04:00
|
|
|
|
2014-05-13 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2015-06-26 17:47:04 +02:00
|
|
|
bool MWMechanics::AiAvoidDoor::execute (const MWWorld::Ptr& actor, CharacterController& characterController, AiState& state, float duration)
|
2014-05-13 13:07:27 -04:00
|
|
|
{
|
2014-05-14 13:38:10 -04:00
|
|
|
|
|
|
|
ESM::Position pos = actor.getRefData().getPosition();
|
2014-05-14 14:11:34 -04:00
|
|
|
if(mDuration == 1) //If it just started, get the actor position as the stuck detection thing
|
2019-08-25 17:21:14 +02:00
|
|
|
mLastPos = pos.asVec3();
|
2014-05-14 13:38:10 -04:00
|
|
|
|
2014-05-13 13:07:27 -04:00
|
|
|
mDuration -= duration; //Update timer
|
|
|
|
|
2019-08-25 17:21:14 +02:00
|
|
|
if (mDuration < 0)
|
|
|
|
{
|
|
|
|
if (isStuck(pos.asVec3()))
|
|
|
|
{
|
|
|
|
adjustDirection();
|
2014-05-14 13:38:10 -04:00
|
|
|
mDuration = 1; //reset timer
|
|
|
|
}
|
2019-08-25 17:21:14 +02:00
|
|
|
else
|
2014-05-14 13:38:10 -04:00
|
|
|
return true; // We have tried backing up for more than one second, we've probably cleared it
|
|
|
|
}
|
2014-05-13 13:07:27 -04:00
|
|
|
|
2019-08-25 15:20:14 +02:00
|
|
|
if (mDoorPtr.getClass().getDoorState(mDoorPtr) == MWWorld::DoorState::Idle)
|
2014-05-13 13:07:27 -04:00
|
|
|
return true; //Door is no longer opening
|
|
|
|
|
|
|
|
ESM::Position tPos = mDoorPtr.getRefData().getPosition(); //Position of the door
|
|
|
|
float x = pos.pos[0] - tPos.pos[0];
|
|
|
|
float y = pos.pos[1] - tPos.pos[1];
|
|
|
|
|
2014-08-11 04:21:04 +02:00
|
|
|
actor.getClass().getCreatureStats(actor).setMovementFlag(CreatureStats::Flag_Run, true);
|
2014-05-13 13:07:27 -04:00
|
|
|
|
2014-08-11 04:21:04 +02:00
|
|
|
// Turn away from the door and move when turn completed
|
2019-08-25 17:21:14 +02:00
|
|
|
if (zTurn(actor, std::atan2(x,y) + getAdjustedAngle(), osg::DegreesToRadians(5.f)))
|
2014-08-11 04:21:04 +02:00
|
|
|
actor.getClass().getMovementSettings(actor).mPosition[1] = 1;
|
|
|
|
else
|
|
|
|
actor.getClass().getMovementSettings(actor).mPosition[1] = 0;
|
|
|
|
actor.getClass().getMovementSettings(actor).mPosition[0] = 0;
|
|
|
|
|
|
|
|
// Make all nearby actors also avoid the door
|
2014-05-14 04:05:18 -04:00
|
|
|
std::vector<MWWorld::Ptr> actors;
|
2015-06-01 21:41:13 +02:00
|
|
|
MWBase::Environment::get().getMechanicsManager()->getActorsInRange(pos.asVec3(),100,actors);
|
2014-09-26 17:12:48 +02:00
|
|
|
for(std::vector<MWWorld::Ptr>::iterator it = actors.begin(); it != actors.end(); ++it) {
|
2015-08-21 21:12:39 +12:00
|
|
|
if(*it != getPlayer()) { //Not the player
|
2014-05-22 20:37:22 +02:00
|
|
|
MWMechanics::AiSequence& seq = it->getClass().getCreatureStats(*it).getAiSequence();
|
2020-05-16 21:52:16 +02:00
|
|
|
if(seq.getTypeId() != MWMechanics::AiPackageTypeId::AvoidDoor) { //Only add it once
|
2014-05-14 04:05:18 -04:00
|
|
|
seq.stack(MWMechanics::AiAvoidDoor(mDoorPtr),*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 13:07:27 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-25 17:21:14 +02:00
|
|
|
bool MWMechanics::AiAvoidDoor::isStuck(const osg::Vec3f& actorPos) const
|
|
|
|
{
|
|
|
|
return (actorPos - mLastPos).length2() < 10 * 10;
|
|
|
|
}
|
2014-06-12 23:27:04 +02:00
|
|
|
|
2019-08-25 17:21:14 +02:00
|
|
|
void MWMechanics::AiAvoidDoor::adjustDirection()
|
|
|
|
{
|
|
|
|
mDirection = Misc::Rng::rollDice(MAX_DIRECTIONS);
|
|
|
|
}
|
|
|
|
|
|
|
|
float MWMechanics::AiAvoidDoor::getAdjustedAngle() const
|
|
|
|
{
|
|
|
|
return 2 * osg::PI / MAX_DIRECTIONS * mDirection;
|
|
|
|
}
|