From 2d66b2c4fa7ca03a096a3be54fc6e19d6cdc8f83 Mon Sep 17 00:00:00 2001 From: gus Date: Sat, 11 Jan 2014 12:06:36 +0100 Subject: [PATCH] AiFollow. Npc get stuck often (no stuck dtection yet) --- apps/openmw/mwmechanics/aicombat.cpp | 3 +- apps/openmw/mwmechanics/aifollow.cpp | 44 ++++++++++++++++++++++++++-- apps/openmw/mwmechanics/aifollow.hpp | 7 +++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/apps/openmw/mwmechanics/aicombat.cpp b/apps/openmw/mwmechanics/aicombat.cpp index 3ca522dc7..6a1ab68d8 100644 --- a/apps/openmw/mwmechanics/aicombat.cpp +++ b/apps/openmw/mwmechanics/aicombat.cpp @@ -3,7 +3,7 @@ #include "movement.hpp" #include "../mwworld/class.hpp" -#include "../mwworld/timestamp.hpp" + #include "../mwbase/world.hpp" #include "../mwbase/environment.hpp" #include "../mwbase/mechanicsmanager.hpp" @@ -87,6 +87,7 @@ namespace MWMechanics mPathFinder = mPathFinder2; } } + ESM::Pathgrid::Point lastPt = mPathFinder.getPath().back(); mPathFinder.checkPathCompleted(pos.pos[0],pos.pos[1],pos.pos[2]); diff --git a/apps/openmw/mwmechanics/aifollow.cpp b/apps/openmw/mwmechanics/aifollow.cpp index 73bf9259a..2f9a7a843 100644 --- a/apps/openmw/mwmechanics/aifollow.cpp +++ b/apps/openmw/mwmechanics/aifollow.cpp @@ -1,12 +1,16 @@ #include "aifollow.hpp" #include +#include "../mwbase/world.hpp" +#include "../mwbase/environment.hpp" +#include "../mwworld/class.hpp" +#include "movement.hpp" MWMechanics::AiFollow::AiFollow(const std::string &actorId,float duration, float x, float y, float z) : mDuration(duration), mX(x), mY(y), mZ(z), mActorId(actorId) { } MWMechanics::AiFollow::AiFollow(const std::string &actorId,const std::string &cellId,float duration, float x, float y, float z) -: mDuration(duration), mX(x), mY(y), mZ(z), mActorId(actorId), mCellId(cellId) +: mDuration(duration), mX(x), mY(y), mZ(z), mActorId(actorId), mCellId(cellId), mTimer(0), mStuckTimer(0) { } @@ -17,8 +21,44 @@ MWMechanics::AiFollow *MWMechanics::AiFollow::clone() const bool MWMechanics::AiFollow::execute (const MWWorld::Ptr& actor,float duration) { + const MWWorld::Ptr target = MWBase::Environment::get().getWorld()->getPtr(mActorId, false); + + mTimer = mTimer + duration; + mStuckTimer = mStuckTimer + duration; + mTotalTime = mTotalTime + duration; + + if(mTotalTime > mDuration) return true; + + ESM::Pathgrid::Point dest; + dest.mX = target.getRefData().getPosition().pos[0]; + dest.mY = target.getRefData().getPosition().pos[1]; + dest.mZ = target.getRefData().getPosition().pos[2]; + + if(mTimer > 0.25) + { + ESM::Pathgrid::Point lastPos = mPathFinder.getPath().back(); + + if((dest.mX - lastPos.mX)*(dest.mX - lastPos.mX) + +(dest.mY - lastPos.mY)*(dest.mY - lastPos.mY) + +(dest.mZ - lastPos.mZ)*(dest.mZ - lastPos.mZ) + > 100*100) + mPathFinder.getPath().push_back(dest); + + mTimer = 0; + } + + ESM::Position pos = actor.getRefData().getPosition(); + float zAngle = mPathFinder.getZAngleToNext(pos.pos[0], pos.pos[1]); + MWBase::Environment::get().getWorld()->rotateObject(actor, 0, 0, zAngle, false); + + if((dest.mX - pos.pos[0])*(dest.mX - pos.pos[0])+(dest.mY - pos.pos[1])*(dest.mY - pos.pos[1])+(dest.mZ - pos.pos[2])*(dest.mZ - pos.pos[2]) + < 100*100) + MWWorld::Class::get(actor).getMovementSettings(actor).mPosition[1] = 0; + else + MWWorld::Class::get(actor).getMovementSettings(actor).mPosition[1] = 1; + std::cout << "AiFollow completed.\n"; - return true; + return false; } int MWMechanics::AiFollow::getTypeId() const diff --git a/apps/openmw/mwmechanics/aifollow.hpp b/apps/openmw/mwmechanics/aifollow.hpp index 39df024e4..b149c55f5 100644 --- a/apps/openmw/mwmechanics/aifollow.hpp +++ b/apps/openmw/mwmechanics/aifollow.hpp @@ -3,6 +3,7 @@ #include "aipackage.hpp" #include +#include "pathfinding.hpp" namespace MWMechanics { @@ -24,6 +25,12 @@ namespace MWMechanics float mZ; std::string mActorId; std::string mCellId; + + float mTimer; + float mStuckTimer; + float mTotalTime; + + PathFinder mPathFinder; }; } #endif