forked from mirror/openmw-tes3mp
attempt to solve Bug #1009 by not building graph every frame
This commit is contained in:
parent
0e46c40cb5
commit
5d038423ec
7 changed files with 48 additions and 23 deletions
|
@ -49,8 +49,6 @@ namespace MWMechanics
|
||||||
//MWWorld::Class::get(actor).getCreatureStats(actor).setAttackingOrSpell(true);
|
//MWWorld::Class::get(actor).getCreatureStats(actor).setAttackingOrSpell(true);
|
||||||
}
|
}
|
||||||
ESM::Position pos = actor.getRefData().getPosition();
|
ESM::Position pos = actor.getRefData().getPosition();
|
||||||
const ESM::Pathgrid *pathgrid =
|
|
||||||
MWBase::Environment::get().getWorld()->getStore().get<ESM::Pathgrid>().search(*actor.getCell()->mCell);
|
|
||||||
|
|
||||||
float xCell = 0;
|
float xCell = 0;
|
||||||
float yCell = 0;
|
float yCell = 0;
|
||||||
|
@ -74,20 +72,19 @@ namespace MWMechanics
|
||||||
mTimer2 = mTimer2 + duration;
|
mTimer2 = mTimer2 + duration;
|
||||||
|
|
||||||
if(!mPathFinder.isPathConstructed())
|
if(!mPathFinder.isPathConstructed())
|
||||||
mPathFinder.buildPath(start, dest, pathgrid, xCell, yCell, true);
|
mPathFinder.buildPath(start, dest, actor.getCell(), true);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(mTimer2 > 0.25)
|
if(mTimer2 > 0.25)
|
||||||
{
|
{
|
||||||
mTimer2 = 0;
|
mTimer2 = 0;
|
||||||
mPathFinder2.buildPath(start, dest, pathgrid, xCell, yCell, true);
|
mPathFinder2.buildPath(start, dest, actor.getCell(), true);
|
||||||
ESM::Pathgrid::Point lastPt = mPathFinder.getPath().back();
|
ESM::Pathgrid::Point lastPt = mPathFinder.getPath().back();
|
||||||
if(mPathFinder2.getPathSize() < mPathFinder.getPathSize() ||
|
if(mPathFinder2.getPathSize() < mPathFinder.getPathSize() ||
|
||||||
(dest.mX - lastPt.mX)*(dest.mX - lastPt.mX)+(dest.mY - lastPt.mY)*(dest.mY - lastPt.mY)+(dest.mZ - lastPt.mZ)*(dest.mZ - lastPt.mZ) > 200*200)
|
(dest.mX - lastPt.mX)*(dest.mX - lastPt.mX)+(dest.mY - lastPt.mY)*(dest.mY - lastPt.mY)+(dest.mZ - lastPt.mZ)*(dest.mZ - lastPt.mZ) > 200*200)
|
||||||
mPathFinder = mPathFinder2;
|
mPathFinder = mPathFinder2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ESM::Pathgrid::Point lastPt = mPathFinder.getPath().back();
|
|
||||||
|
|
||||||
mPathFinder.checkPathCompleted(pos.pos[0],pos.pos[1],pos.pos[2]);
|
mPathFinder.checkPathCompleted(pos.pos[0],pos.pos[1],pos.pos[2]);
|
||||||
|
|
||||||
|
|
|
@ -85,8 +85,6 @@ namespace MWMechanics
|
||||||
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayerPtr();
|
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayerPtr();
|
||||||
ESM::Position pos = actor.getRefData().getPosition();
|
ESM::Position pos = actor.getRefData().getPosition();
|
||||||
bool cellChange = actor.getCell()->mCell->mData.mX != cellX || actor.getCell()->mCell->mData.mY != cellY;
|
bool cellChange = actor.getCell()->mCell->mData.mX != cellX || actor.getCell()->mCell->mData.mY != cellY;
|
||||||
const ESM::Pathgrid *pathgrid =
|
|
||||||
MWBase::Environment::get().getWorld()->getStore().get<ESM::Pathgrid>().search(*actor.getCell()->mCell);
|
|
||||||
|
|
||||||
if(actor.getCell()->mCell->mData.mX != player.getCell()->mCell->mData.mX)
|
if(actor.getCell()->mCell->mData.mX != player.getCell()->mCell->mData.mX)
|
||||||
{
|
{
|
||||||
|
@ -136,7 +134,7 @@ namespace MWMechanics
|
||||||
start.mY = pos.pos[1];
|
start.mY = pos.pos[1];
|
||||||
start.mZ = pos.pos[2];
|
start.mZ = pos.pos[2];
|
||||||
|
|
||||||
mPathFinder.buildPath(start, dest, pathgrid, xCell, yCell, true);
|
mPathFinder.buildPath(start, dest, actor.getCell(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mPathFinder.checkPathCompleted(pos.pos[0],pos.pos[1],pos.pos[2]))
|
if(mPathFinder.checkPathCompleted(pos.pos[0],pos.pos[1],pos.pos[2]))
|
||||||
|
|
|
@ -83,9 +83,6 @@ bool MWMechanics::AiFollow::execute (const MWWorld::Ptr& actor,float duration)
|
||||||
start.mY = pos.pos[1];
|
start.mY = pos.pos[1];
|
||||||
start.mZ = pos.pos[2];
|
start.mZ = pos.pos[2];
|
||||||
|
|
||||||
const ESM::Pathgrid *pathgrid =
|
|
||||||
MWBase::Environment::get().getWorld()->getStore().get<ESM::Pathgrid>().search(*actor.getCell()->mCell);
|
|
||||||
|
|
||||||
float xCell = 0;
|
float xCell = 0;
|
||||||
float yCell = 0;
|
float yCell = 0;
|
||||||
|
|
||||||
|
@ -94,7 +91,7 @@ bool MWMechanics::AiFollow::execute (const MWWorld::Ptr& actor,float duration)
|
||||||
xCell = actor.getCell()->mCell->mData.mX * ESM::Land::REAL_SIZE;
|
xCell = actor.getCell()->mCell->mData.mX * ESM::Land::REAL_SIZE;
|
||||||
yCell = actor.getCell()->mCell->mData.mY * ESM::Land::REAL_SIZE;
|
yCell = actor.getCell()->mCell->mData.mY * ESM::Land::REAL_SIZE;
|
||||||
}
|
}
|
||||||
mPathFinder.buildPath(start, dest, pathgrid, xCell, yCell, true);
|
mPathFinder.buildPath(start, dest, actor.getCell(), true);
|
||||||
}
|
}
|
||||||
mStuckTimer = 0;
|
mStuckTimer = 0;
|
||||||
mStuckPos = pos;
|
mStuckPos = pos;
|
||||||
|
|
|
@ -61,7 +61,6 @@ namespace MWMechanics
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const ESM::Pathgrid *pathgrid = world->getStore().get<ESM::Pathgrid>().search(*cell);
|
|
||||||
bool cellChange = cell->mData.mX != cellX || cell->mData.mY != cellY;
|
bool cellChange = cell->mData.mX != cellX || cell->mData.mY != cellY;
|
||||||
if(!mPathFinder.isPathConstructed() || cellChange)
|
if(!mPathFinder.isPathConstructed() || cellChange)
|
||||||
{
|
{
|
||||||
|
@ -86,7 +85,7 @@ namespace MWMechanics
|
||||||
start.mY = pos.pos[1];
|
start.mY = pos.pos[1];
|
||||||
start.mZ = pos.pos[2];
|
start.mZ = pos.pos[2];
|
||||||
|
|
||||||
mPathFinder.buildPath(start, dest, pathgrid, xCell, yCell, true);
|
mPathFinder.buildPath(start, dest, actor.getCell(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mPathFinder.checkPathCompleted(pos.pos[0], pos.pos[1], pos.pos[2]))
|
if(mPathFinder.checkPathCompleted(pos.pos[0], pos.pos[1], pos.pos[2]))
|
||||||
|
|
|
@ -213,7 +213,7 @@ namespace MWMechanics
|
||||||
start.mY = pos.pos[1];
|
start.mY = pos.pos[1];
|
||||||
start.mZ = pos.pos[2];
|
start.mZ = pos.pos[2];
|
||||||
|
|
||||||
mPathFinder.buildPath(start, dest, mPathgrid, mXCell, mYCell, false);
|
mPathFinder.buildPath(start, dest, actor.getCell(), false);
|
||||||
|
|
||||||
if(mPathFinder.isPathConstructed())
|
if(mPathFinder.isPathConstructed())
|
||||||
{
|
{
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
#include "OgreMath.h"
|
#include "OgreMath.h"
|
||||||
|
|
||||||
#include <boost/graph/dijkstra_shortest_paths.hpp>
|
#include <boost/graph/dijkstra_shortest_paths.hpp>
|
||||||
#include <boost/graph/adjacency_list.hpp>
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
@ -136,8 +135,8 @@ namespace
|
||||||
namespace MWMechanics
|
namespace MWMechanics
|
||||||
{
|
{
|
||||||
PathFinder::PathFinder()
|
PathFinder::PathFinder()
|
||||||
|
:mIsPathConstructed(false),mIsGraphConstructed(false)
|
||||||
{
|
{
|
||||||
mIsPathConstructed = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PathFinder::clearPath()
|
void PathFinder::clearPath()
|
||||||
|
@ -147,10 +146,19 @@ namespace MWMechanics
|
||||||
mIsPathConstructed = false;
|
mIsPathConstructed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PathFinder::buildPathgridGraph(const ESM::Pathgrid* pathGrid,float xCell, float yCell)
|
||||||
|
{
|
||||||
|
mGraph = buildGraph(pathGrid, xCell, yCell);
|
||||||
|
mIsGraphConstructed = true;
|
||||||
|
}
|
||||||
|
|
||||||
void PathFinder::buildPath(const ESM::Pathgrid::Point &startPoint, const ESM::Pathgrid::Point &endPoint,
|
void PathFinder::buildPath(const ESM::Pathgrid::Point &startPoint, const ESM::Pathgrid::Point &endPoint,
|
||||||
const ESM::Pathgrid *pathGrid, float xCell, float yCell, bool allowShortcuts)
|
const MWWorld::CellStore* cell, bool allowShortcuts)
|
||||||
{
|
{
|
||||||
mPath.clear();
|
mPath.clear();
|
||||||
|
if(mCell != cell) mIsGraphConstructed = false;
|
||||||
|
mCell = cell;
|
||||||
|
|
||||||
if(allowShortcuts)
|
if(allowShortcuts)
|
||||||
{
|
{
|
||||||
if(MWBase::Environment::get().getWorld()->castRay(startPoint.mX, startPoint.mY, startPoint.mZ,
|
if(MWBase::Environment::get().getWorld()->castRay(startPoint.mX, startPoint.mY, startPoint.mZ,
|
||||||
|
@ -160,13 +168,24 @@ namespace MWMechanics
|
||||||
|
|
||||||
if(!allowShortcuts)
|
if(!allowShortcuts)
|
||||||
{
|
{
|
||||||
|
const ESM::Pathgrid *pathGrid =
|
||||||
|
MWBase::Environment::get().getWorld()->getStore().get<ESM::Pathgrid>().search(*mCell->mCell);
|
||||||
|
float xCell = 0;
|
||||||
|
float yCell = 0;
|
||||||
|
|
||||||
|
if (mCell->isExterior())
|
||||||
|
{
|
||||||
|
xCell = mCell->mCell->mData.mX * ESM::Land::REAL_SIZE;
|
||||||
|
yCell = mCell->mCell->mData.mY * ESM::Land::REAL_SIZE;
|
||||||
|
}
|
||||||
int startNode = getClosestPoint(pathGrid, startPoint.mX - xCell, startPoint.mY - yCell,startPoint.mZ);
|
int startNode = getClosestPoint(pathGrid, startPoint.mX - xCell, startPoint.mY - yCell,startPoint.mZ);
|
||||||
int endNode = getClosestPoint(pathGrid, endPoint.mX - xCell, endPoint.mY - yCell, endPoint.mZ);
|
int endNode = getClosestPoint(pathGrid, endPoint.mX - xCell, endPoint.mY - yCell, endPoint.mZ);
|
||||||
|
|
||||||
if(startNode != -1 && endNode != -1)
|
if(startNode != -1 && endNode != -1)
|
||||||
{
|
{
|
||||||
PathGridGraph graph = buildGraph(pathGrid, xCell, yCell);
|
if(!mIsGraphConstructed) buildPathgridGraph(pathGrid, xCell, yCell);
|
||||||
mPath = findPath(startNode, endNode, graph);
|
|
||||||
|
mPath = findPath(startNode, endNode, mGraph);
|
||||||
|
|
||||||
if(!mPath.empty())
|
if(!mPath.empty())
|
||||||
{
|
{
|
||||||
|
|
|
@ -3,6 +3,12 @@
|
||||||
|
|
||||||
#include <components/esm/loadpgrd.hpp>
|
#include <components/esm/loadpgrd.hpp>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <boost/graph/adjacency_list.hpp>
|
||||||
|
|
||||||
|
namespace MWWorld
|
||||||
|
{
|
||||||
|
class CellStore;
|
||||||
|
}
|
||||||
|
|
||||||
namespace MWMechanics
|
namespace MWMechanics
|
||||||
{
|
{
|
||||||
|
@ -12,9 +18,11 @@ namespace MWMechanics
|
||||||
PathFinder();
|
PathFinder();
|
||||||
|
|
||||||
void clearPath();
|
void clearPath();
|
||||||
|
|
||||||
|
void buildPathgridGraph(const ESM::Pathgrid* pathGrid,float xCell = 0, float yCell = 0);
|
||||||
|
|
||||||
void buildPath(const ESM::Pathgrid::Point &startPoint, const ESM::Pathgrid::Point &endPoint,
|
void buildPath(const ESM::Pathgrid::Point &startPoint, const ESM::Pathgrid::Point &endPoint,
|
||||||
const ESM::Pathgrid* pathGrid, float xCell = 0, float yCell = 0,
|
const MWWorld::CellStore* cell, bool allowShortcuts = true);
|
||||||
bool allowShortcuts = true);
|
|
||||||
|
|
||||||
bool checkPathCompleted(float x, float y, float z);
|
bool checkPathCompleted(float x, float y, float z);
|
||||||
///< \Returns true if the last point of the path has been reached.
|
///< \Returns true if the last point of the path has been reached.
|
||||||
|
@ -45,6 +53,13 @@ namespace MWMechanics
|
||||||
private:
|
private:
|
||||||
std::list<ESM::Pathgrid::Point> mPath;
|
std::list<ESM::Pathgrid::Point> mPath;
|
||||||
bool mIsPathConstructed;
|
bool mIsPathConstructed;
|
||||||
|
|
||||||
|
typedef boost::adjacency_list< boost::vecS, boost::vecS, boost::undirectedS,
|
||||||
|
boost::property<boost::vertex_index_t, int, ESM::Pathgrid::Point>, boost::property<boost::edge_weight_t, float> >
|
||||||
|
PathGridGraph;
|
||||||
|
PathGridGraph mGraph;
|
||||||
|
bool mIsGraphConstructed;
|
||||||
|
const MWWorld::CellStore* mCell;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue