1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-29 04:20:29 +00:00

Merge remote-tracking branch 'gus/AIFix2'

This commit is contained in:
Marc Zinnschlag 2014-02-17 10:54:07 +01:00
commit ea16f79d77
5 changed files with 111 additions and 23 deletions

View File

@ -115,21 +115,11 @@ namespace MWClass
if (ref->mRef.mTeleport) if (ref->mRef.mTeleport)
{ {
// teleport door boost::shared_ptr<MWWorld::Action> action(new MWWorld::ActionTeleport (ref->mRef.mDestCell, ref->mRef.mDoorDest));
/// \todo remove this if clause once ActionTeleport can also support other actors
if (MWBase::Environment::get().getWorld()->getPlayerPtr()==actor)
{
boost::shared_ptr<MWWorld::Action> action(new MWWorld::ActionTeleport (ref->mRef.mDestCell, ref->mRef.mDoorDest));
action->setSound(openSound); action->setSound(openSound);
return action; return action;
}
else
{
// another NPC or a creature is using the door
return boost::shared_ptr<MWWorld::Action> (new MWWorld::FailedAction);
}
} }
else else
{ {

View File

@ -1,8 +1,27 @@
#include "aiactivate.hpp" #include "aiactivate.hpp"
#include <iostream> #include <iostream>
#include "movement.hpp"
#include "../mwbase/world.hpp"
#include "../mwbase/environment.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/action.hpp"
#include "steering.hpp"
namespace
{
float sgn(float a)
{
if(a > 0)
return 1.0;
return -1.0;
}
}
MWMechanics::AiActivate::AiActivate(const std::string &objectId) MWMechanics::AiActivate::AiActivate(const std::string &objectId)
: mObjectId(objectId) : mObjectId(objectId)
{ {
} }
MWMechanics::AiActivate *MWMechanics::AiActivate::clone() const MWMechanics::AiActivate *MWMechanics::AiActivate::clone() const
@ -11,8 +30,81 @@ MWMechanics::AiActivate *MWMechanics::AiActivate::clone() const
} }
bool MWMechanics::AiActivate::execute (const MWWorld::Ptr& actor,float duration) bool MWMechanics::AiActivate::execute (const MWWorld::Ptr& actor,float duration)
{ {
std::cout << "AiActivate completed.\n"; MWBase::World *world = MWBase::Environment::get().getWorld();
return true; ESM::Position pos = actor.getRefData().getPosition();
Movement &movement = actor.getClass().getMovementSettings(actor);
const ESM::Cell *cell = actor.getCell()->mCell;
MWWorld::Ptr player = world->getPlayerPtr();
if(cell->mData.mX != player.getCell()->mCell->mData.mX)
{
int sideX = sgn(cell->mData.mX - player.getCell()->mCell->mData.mX);
//check if actor is near the border of an inactive cell. If so, stop walking.
if(sideX * (pos.pos[0] - cell->mData.mX*ESM::Land::REAL_SIZE) >
sideX * (ESM::Land::REAL_SIZE/2.0f - 200.0f))
{
movement.mPosition[1] = 0;
return false;
}
}
if(cell->mData.mY != player.getCell()->mCell->mData.mY)
{
int sideY = sgn(cell->mData.mY - player.getCell()->mCell->mData.mY);
//check if actor is near the border of an inactive cell. If so, stop walking.
if(sideY * (pos.pos[1] - cell->mData.mY*ESM::Land::REAL_SIZE) >
sideY * (ESM::Land::REAL_SIZE/2.0f - 200.0f))
{
movement.mPosition[1] = 0;
return false;
}
}
MWWorld::Ptr target = world->getPtr(mObjectId,false);
ESM::Position targetPos = target.getRefData().getPosition();
bool cellChange = cell->mData.mX != mCellX || cell->mData.mY != mCellY;
if(!mPathFinder.isPathConstructed() || cellChange)
{
mCellX = cell->mData.mX;
mCellY = cell->mData.mY;
ESM::Pathgrid::Point dest;
dest.mX = targetPos.pos[0];
dest.mY = targetPos.pos[1];
dest.mZ = targetPos.pos[2];
ESM::Pathgrid::Point start;
start.mX = pos.pos[0];
start.mY = pos.pos[1];
start.mZ = pos.pos[2];
mPathFinder.buildPath(start, dest, actor.getCell(), true);
}
if((pos.pos[0]-targetPos.pos[0])*(pos.pos[0]-targetPos.pos[0])+
(pos.pos[1]-targetPos.pos[1])*(pos.pos[1]-targetPos.pos[1])+
(pos.pos[2]-targetPos.pos[2])*(pos.pos[2]-targetPos.pos[2]) < 200*200)
{
movement.mPosition[1] = 0;
MWWorld::Ptr target = world->getPtr(mObjectId,false);
MWWorld::Class::get(target).activate(target,actor).get()->execute(actor);
return true;
}
if(mPathFinder.checkPathCompleted(pos.pos[0], pos.pos[1], pos.pos[2]))
{
movement.mPosition[1] = 0;
MWWorld::Ptr target = world->getPtr(mObjectId,false);
MWWorld::Class::get(target).activate(target,actor).get()->execute(actor);
return true;
}
float zAngle = mPathFinder.getZAngleToNext(pos.pos[0], pos.pos[1]);
zTurn(actor, Ogre::Degree(zAngle));
MWWorld::Class::get(actor).getMovementSettings(actor).mPosition[1] = 1;
movement.mPosition[1] = 1;
return false;
} }
int MWMechanics::AiActivate::getTypeId() const int MWMechanics::AiActivate::getTypeId() const

View File

@ -4,6 +4,8 @@
#include "aipackage.hpp" #include "aipackage.hpp"
#include <string> #include <string>
#include "pathfinding.hpp"
namespace MWMechanics namespace MWMechanics
{ {
@ -18,6 +20,10 @@ namespace MWMechanics
private: private:
std::string mObjectId; std::string mObjectId;
PathFinder mPathFinder;
int mCellX;
int mCellY;
}; };
} }
#endif // GAME_MWMECHANICS_AIACTIVATE_H #endif // GAME_MWMECHANICS_AIACTIVATE_H

View File

@ -21,8 +21,8 @@ namespace MWMechanics
{ {
AiTravel::AiTravel(float x, float y, float z) AiTravel::AiTravel(float x, float y, float z)
: mX(x),mY(y),mZ(z),mPathFinder() : mX(x),mY(y),mZ(z),mPathFinder()
, cellX(std::numeric_limits<int>::max()) , mCellX(std::numeric_limits<int>::max())
, cellY(std::numeric_limits<int>::max()) , mCellY(std::numeric_limits<int>::max())
{ {
} }
@ -62,11 +62,11 @@ namespace MWMechanics
} }
} }
bool cellChange = cell->mData.mX != cellX || cell->mData.mY != cellY; bool cellChange = cell->mData.mX != mCellX || cell->mData.mY != mCellY;
if(!mPathFinder.isPathConstructed() || cellChange) if(!mPathFinder.isPathConstructed() || cellChange)
{ {
cellX = cell->mData.mX; mCellX = cell->mData.mX;
cellY = cell->mData.mY; mCellY = cell->mData.mY;
ESM::Pathgrid::Point dest; ESM::Pathgrid::Point dest;
dest.mX = mX; dest.mX = mX;

View File

@ -23,8 +23,8 @@ namespace MWMechanics
float mY; float mY;
float mZ; float mZ;
int cellX; int mCellX;
int cellY; int mCellY;
PathFinder mPathFinder; PathFinder mPathFinder;
}; };