Backing off closed doors working, needs cleanup and tweaking.

actorid
cc9cii 11 years ago
parent 7eb6a2e52d
commit 479a94b35d

@ -35,6 +35,9 @@ namespace
namespace MWMechanics
{
static const float DOOR_CHECK_INTERVAL = 1.5f;
// NOTE: MIN_DIST_TO_DOOR_SQUARED is defined in obstacle.hpp
AiCombat::AiCombat(const MWWorld::Ptr& actor) :
mTarget(actor),
mTimerAttack(0),
@ -44,12 +47,104 @@ namespace MWMechanics
mReadyToAttack(false),
mStrike(false),
mCombatMove(false),
mBackOffDoor(false),
mRotate(false),
mMovement(),
mDoorIter(actor.getCell()->get<ESM::Door>().mList.end()),
mDoors(actor.getCell()->get<ESM::Door>().mList),
mDoorCheckDuration(0),
mTargetAngle(0)
{
}
/*
* The 'pursuit' part of AiCombat now has some memory to allow backtracking.
* The intention is to allow actors to detect being 'stuck' somewhere, whether
* in a river or facing a door, to go back to a known 'good' position.
*
* Each actor goes through these states of movement once alerted and an AiCombat
* package is queued (FIXME: below is a DRAFT proposal only):
*
* - Maybe remember the starting position so that the actor can return if
* lose sight of the target? Last Known Target Location Tracking - keep track
* of target positions every sec, paired with actor's location at the time
*
* - Get to the target if far away (need path finding), how close depends on
* the current best (selected?) weapon. As the actor moves, some breadcrumb
* is left (e.g. every second store a position in a FIFO) so that the actor
* can back track if necessary.
*
* - If the actor gets stuck (need a way of detecting this) then decide what
* to do next. For example, if next to a door then maybe back track one or
* two positions and check LOS of the target every x frames (reaction time?).
* Or maybe back track then look for a new path.
*
* - Once in weapon range, may need a strategy to get to the target for a
* strike (maybe there are others nearby attacking the same target).
*
*
* Current AiCombat movement states (as of 0.29.0), ignoring the details of the
* attack states such as CombatMove, Strike and ReadyToAttack:
*
* +-----(within strike range)----->attack--(beyond strike range)-->follow
* | | ^ | |
* | | | | |
* pursue<----(beyond follow range)-----+ +----(within strike range)---+ |
* ^ |
* | |
* +--------------------------(beyond follow range)--------------------+
*
*
* Below diagram is high level only, FIXME: code detail may be different:
*
* +-----------(same)-------------->attack---------(same)---------->follow
* | |^^ | |
* | ||| | |
* | +--(same)-----------------+|+----------(same)------------+ |
* | | | in range |
* | | too far | |
* | | +---------------got hit or LOS<---+ |
* | <----+ | | |
* pursue<---------+ door | |
* ^^ <--> maybe stuck, check --------------> back up and wait |
* || | ^ | | ^ | |
* || | | | stuck | | | waited |
* || +---+ | +---+ | too long |
* || backtrack | | |
* |+----------------------+ go back? <-----------+ |
* | |
* +----------------------------(same)---------------------------------+
*
* FIXME:
*
* The new scheme is way too complicated, should really be implemented as a
* proper state machine.
*
* TODO:
*
* Use the Observer Pattern to co-ordinate attacks, provide intelligence on
* whether the target was hit, etc.
*
* TODO:
*
* Auto-generate large cubes or squares as a poor-man's navmesh? Many
* external cells do not have any pathgrids, and certainly none for flying
* or swimming.
*
*
* Called from: (check Doxygen as this comment could be out of date)
*
* OMW::Engine::go() |
* Ogre::Root::renderOneFrame() |
* Ogre::Root::... | ... detail omitted
* Ogre::Root::fireFrameRenderingQueued() |
* OMW::Engine::frameRenderingQueued() | virtual Ogre::FrameListener
* MWMechanics::MechanicsManager::update() |
* MWMechanics::Actors::update() |
* MWMechanics::Actors::updateActor() |
* MWMechanics::AiSequence::execute() | from priority queue mPackages
* MWMechanics::AiCombat::execute() | virtual MWMechanics::AiPackage
*/
bool AiCombat::execute (const MWWorld::Ptr& actor,float duration)
{
//General description
@ -168,6 +263,43 @@ namespace MWMechanics
ESM::Position pos = actor.getRefData().getPosition();
/*
* Some notes on meanings of variables:
*
* rangeMelee:
*
* - Distance where attack using the actor's weapon is possible
* - longer for ranged weapons (obviously?) vs. melee weapons
* - Once within this distance mFollowTarget is triggered
* (TODO: check whether the follow logic still works for ranged
* weapons, since rangeCloseup is set to zero)
* - TODO: The variable name is confusing. It was ok when AiCombat only
* had melee weapons but now that ranged weapons are supported that is
* no longer the case. It should really be renamed to something
* like rangeStrike - alternatively, keep this name for melee
* weapons and use a different variable for tracking ranged weapon
* distance (rangeRanged maybe?)
*
* rangeCloseup:
*
* - Applies to melee weapons or hand to hand only (or creatures without
* weapons)
* - Distance a little further away from the actor's weapon strike
* i.e. rangeCloseup > rangeMelee for melee weapons
* (the variable names make this simple concept counter-intuitive,
* something like rangeMelee > rangeStrike may be better)
* - Once the target gets beyond this distance mFollowTarget is cleared
* and a path to the target needs to be found
* - TODO: Possibly rename this variable to rangeMelee or even rangeFollow
*
* mFollowTarget:
*
* - Once triggered, the actor follows the target with LOS shortcut
* (the shortcut really only applies to cells where pathgrids are
* available, since the default path without pathgrids is direct to
* target even if LOS is not achieved)
*
*/
float rangeMelee;
float rangeCloseUp;
bool distantCombat = false;
@ -189,6 +321,7 @@ namespace MWMechanics
Ogre::Vector3 vDir = vDest - vStart;
float distBetween = vDir.length();
// (within strike dist) || (not quite strike dist while following)
if(distBetween < rangeMelee || (distBetween <= rangeCloseUp && mFollowTarget) )
{
//Melee and Close-up combat
@ -198,12 +331,13 @@ namespace MWMechanics
mRotate = true;
//bool LOS = MWBase::Environment::get().getWorld()->getLOS(actor, mTarget);
// (not quite strike dist while following)
if (mFollowTarget && distBetween > rangeMelee)
{
//Close-up combat: just run up on target
mMovement.mPosition[1] = 1;
}
else
else // (within strike dist)
{
//Melee: stop running and attack
mMovement.mPosition[1] = 0;
@ -240,7 +374,7 @@ namespace MWMechanics
}
else
{
//target is at far distance: build path to target OR follow target (if previously actor had reached it once)
//target is at far distance: build path to target
mFollowTarget = false;
buildNewPath(actor); //may fail to build a path, check before use
@ -261,6 +395,55 @@ namespace MWMechanics
mMovement.mPosition[1] = 1;
mReadyToAttack = false;
// remember that this section gets updated every tReaction, which is
// currently hard coded at 250ms or 1/4 second
if(mObstacleCheck.check(actor, tReaction)) // true if evasive action needed
{
std::cout<<"found obstacle"<<std::endl;
// first check if we're walking into a door
bool foundClosedDoor = false;
MWWorld::CellStore *cell = actor.getCell();
if(!cell->getCell()->isExterior())
{
// Check all the doors in this cell
mDoors = cell->get<ESM::Door>().mList; // update list member
mDoorIter = mDoors.begin();
Ogre::Vector3 actorPos(actor.getRefData().getPosition().pos);
for (; mDoorIter != mDoors.end(); ++mDoorIter)
{
MWWorld::LiveCellRef<ESM::Door>& ref = *mDoorIter;
if(actorPos.squaredDistance(Ogre::Vector3(ref.mRef.mPos.pos)) < 1.3*1.3*MIN_DIST_TO_DOOR_SQUARED &&
ref.mData.getLocalRotation().rot[2] == 0)
{
std::cout<<"closed door id \""<<ref.mRef.mRefID<<"\""<<std::endl;
foundClosedDoor = true;
break;
}
}
}
if(foundClosedDoor)
{
mBackOffDoor = true;
mObstacleCheck.clear();
if(mFollowTarget) // FIXME: probably not needed
{
std::cout<<"follow door"<<std::endl;
mFollowTarget = false;
}
}
else // probably walking into another NPC
{
// TODO: diagonal should have same animation as walk forward
// but doesn't seem to do that?
actor.getClass().getMovementSettings(actor).mPosition[0] = 1;
actor.getClass().getMovementSettings(actor).mPosition[1] = 0.1f;
// change the angle a bit, too
if(mPathFinder.isPathConstructed())
zTurn(actor, Ogre::Degree(mPathFinder.getZAngleToNext(pos.pos[0] + 1, pos.pos[1])));
}
}
}
if(distBetween > rangeMelee)
@ -293,6 +476,28 @@ namespace MWMechanics
}
}
MWWorld::LiveCellRef<ESM::Door>& ref = *mDoorIter;
Ogre::Vector3 actorPos(actor.getRefData().getPosition().pos);
if(mBackOffDoor &&
actorPos.squaredDistance(Ogre::Vector3(ref.mRef.mPos.pos)) < 1.5*1.5*MIN_DIST_TO_DOOR_SQUARED)
{
mMovement.mPosition[1] = -0.2; // back off, but slowly
if(mDoorIter != mDoors.end() && ref.mData.getLocalRotation().rot[2] >= 1) // open
{
mDoorIter = mDoors.end();
mBackOffDoor = false;
std::cout<<"open door id \""<<ref.mRef.mRefID<<"\""<<std::endl;
}
}
else
{
if(mReadyToAttack && !mBackOffDoor)
{
std::cout<<"ready to attack, move forward"<<std::endl;
mMovement.mPosition[1] = 1; // FIXME: oscillates?
}
}
actor.getClass().getMovementSettings(actor) = mMovement;
return false;

@ -6,6 +6,9 @@
#include "pathfinding.hpp"
#include "movement.hpp"
#include "obstacle.hpp"
#include "../mwworld/cellstore.hpp" // for Doors
#include "../mwbase/world.hpp"
@ -40,14 +43,21 @@ namespace MWMechanics
// used every frame when mRotate is true
float mTargetAngle;
// AiCombat states
bool mReadyToAttack, mStrike;
bool mFollowTarget;
bool mCombatMove;
bool mBackOffDoor;
bool mRotate;
MWMechanics::Movement mMovement;
MWWorld::Ptr mTarget;
ObstacleCheck mObstacleCheck;
float mDoorCheckDuration;
MWWorld::CellRefList<ESM::Door>::List::iterator mDoorIter;
MWWorld::CellRefList<ESM::Door>::List& mDoors;
void buildNewPath(const MWWorld::Ptr& actor);
};
}

@ -83,8 +83,7 @@ namespace MWMechanics
*
* New high level states. Not exactly as per vanilla (e.g. door stuff)
* but the differences are required because our physics does not work like
* vanilla and therefore have to compensate/work around. Note also many of
* the actions now have reaction times.
* vanilla and therefore have to compensate/work around.
*
* [select node, [if stuck evade
* build path] or remove nodes if near door]
@ -467,6 +466,10 @@ namespace MWMechanics
const PathFinder& pathfinder)
{
// TODO: how to add these back in once the door opens?
// Idea: keep a list of detected closed doors (see aicombat.cpp)
// Every now and then check whether one of the doors is opened. (maybe
// at the end of playing idle?) If the door is opened then re-calculate
// allowed nodes starting from the spawn point.
std::list<ESM::Pathgrid::Point> paths = pathfinder.getPath();
while(paths.size() >= 2)
{

@ -42,9 +42,12 @@ namespace MWMechanics
for (; it != refList.end(); ++it)
{
MWWorld::LiveCellRef<ESM::Door>& ref = *it;
if(pos.squaredDistance(Ogre::Vector3(ref.mRef.mPos.pos)) < minSqr &&
ref.mData.getLocalRotation().rot[2] == (closed ? 0 : 1))
return true; // found, stop searching
if(pos.squaredDistance(Ogre::Vector3(ref.mRef.mPos.pos)) < minSqr)
if(closed && ref.mData.getLocalRotation().rot[2] == 0 ||
!closed && ref.mData.getLocalRotation().rot[2] >= 1)
{
return true; // found, stop searching
}
}
return false; // none found
}

@ -197,11 +197,11 @@ namespace MWMechanics
// both of these are set to zero in the constructor
//mSCCId = 0; // how many strongly connected components in this cell
//mSCCIndex = 0;
int pointsSize = mPathgrid->mPoints.size();
int pointsSize = static_cast<int> (mPathgrid->mPoints.size());
mSCCPoint.resize(pointsSize, std::pair<int, int> (-1, -1));
mSCCStack.reserve(pointsSize);
for(int v = 0; v < static_cast<int> (pointsSize); v++)
for(int v = 0; v < pointsSize; v++)
{
if(mSCCPoint[v].first == -1) // undefined (haven't visited)
recursiveStrongConnect(v);
@ -249,7 +249,7 @@ namespace MWMechanics
return path; // there is no path, return an empty path
}
int graphSize = mGraph.size();
int graphSize = static_cast<int> (mGraph.size());
std::vector<float> gScore (graphSize, -1);
std::vector<float> fScore (graphSize, -1);
std::vector<int> graphParent (graphSize, -1);

Loading…
Cancel
Save