2010-07-31 09:53:02 +00:00
|
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
|
*
|
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
* $URL$
|
|
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2010-08-06 13:13:25 +00:00
|
|
|
|
/*
|
2010-07-31 09:53:02 +00:00
|
|
|
|
* This code is based on Broken Sword 2.5 engine
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
|
|
|
|
|
*
|
|
|
|
|
* Licensed under GNU GPL v2
|
|
|
|
|
*
|
|
|
|
|
*/
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
#include "sword25/kernel/kernel.h"
|
2010-07-29 19:55:28 +00:00
|
|
|
|
#include "sword25/kernel/inputpersistenceblock.h"
|
|
|
|
|
#include "sword25/kernel/outputpersistenceblock.h"
|
|
|
|
|
#include "sword25/math/walkregion.h"
|
|
|
|
|
#include "sword25/math/line.h"
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
#define BS_LOG_PREFIX "WALKREGION"
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
namespace Sword25 {
|
|
|
|
|
|
2010-07-29 19:53:02 +00:00
|
|
|
|
// -----------------------------------------------------------------------------
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Constants
|
2010-07-29 19:53:02 +00:00
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-23 23:04:08 +00:00
|
|
|
|
static const int infinity = 0x7fffffff;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Constructor / Destructor
|
2010-07-29 19:53:02 +00:00
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-18 12:57:47 +00:00
|
|
|
|
WalkRegion::WalkRegion() {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
m_Type = RT_WALKREGION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-09-02 12:14:04 +00:00
|
|
|
|
WalkRegion::WalkRegion(InputPersistenceBlock &Reader, uint Handle) :
|
2010-08-18 12:57:47 +00:00
|
|
|
|
Region(Reader, Handle) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
m_Type = RT_WALKREGION;
|
|
|
|
|
Unpersist(Reader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-18 12:57:47 +00:00
|
|
|
|
WalkRegion::~WalkRegion() {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-18 12:57:47 +00:00
|
|
|
|
bool WalkRegion::Init(const Polygon &Contour, const Common::Array<Polygon> *pHoles) {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Default initialisation of the region
|
2010-08-18 12:57:47 +00:00
|
|
|
|
if (!Region::Init(Contour, pHoles)) return false;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Prepare structures for pathfinding
|
2010-07-29 19:53:02 +00:00
|
|
|
|
InitNodeVector();
|
|
|
|
|
ComputeVisibilityMatrix();
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Signal success
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-18 12:57:47 +00:00
|
|
|
|
bool WalkRegion::QueryPath(Vertex StartPoint, Vertex EndPoint, BS_Path &Path) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
BS_ASSERT(Path.empty());
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// If the start and finish are identical, no path can be found trivially
|
2010-07-29 19:53:02 +00:00
|
|
|
|
if (StartPoint == EndPoint) return true;
|
|
|
|
|
|
2010-08-06 13:13:25 +00:00
|
|
|
|
// Ensure that the start and finish are valid and find new start points if either
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// are outside the polygon
|
2010-07-29 19:53:02 +00:00
|
|
|
|
if (!CheckAndPrepareStartAndEnd(StartPoint, EndPoint)) return false;
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// If between the start and point a line of sight exists, then it can be returned.
|
2010-08-06 13:13:25 +00:00
|
|
|
|
if (IsLineOfSight(StartPoint, EndPoint)) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Path.push_back(StartPoint);
|
|
|
|
|
Path.push_back(EndPoint);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FindPath(StartPoint, EndPoint, Path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
struct DijkstraNode {
|
|
|
|
|
typedef Common::Array<DijkstraNode> Container;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
typedef Container::iterator Iter;
|
|
|
|
|
typedef Container::const_iterator ConstIter;
|
|
|
|
|
|
2010-07-30 12:19:13 +00:00
|
|
|
|
DijkstraNode() : Cost(infinity), Chosen(false) {};
|
2010-08-06 13:13:25 +00:00
|
|
|
|
ConstIter ParentIter;
|
|
|
|
|
int Cost;
|
|
|
|
|
bool Chosen;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
};
|
|
|
|
|
|
2010-08-18 12:57:47 +00:00
|
|
|
|
static void InitDijkstraNodes(DijkstraNode::Container &DijkstraNodes, const Region &Region,
|
|
|
|
|
const Vertex &Start, const Common::Array<Vertex> &Nodes) {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Allocate sufficient space in the array
|
2010-07-29 19:53:02 +00:00
|
|
|
|
DijkstraNodes.resize(Nodes.size());
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Initialise all the nodes which are visible from the starting node
|
2010-07-29 19:53:02 +00:00
|
|
|
|
DijkstraNode::Iter DijkstraIter = DijkstraNodes.begin();
|
2010-08-18 12:57:47 +00:00
|
|
|
|
for (Common::Array<Vertex>::const_iterator NodesIter = Nodes.begin();
|
2010-08-06 13:13:25 +00:00
|
|
|
|
NodesIter != Nodes.end(); NodesIter++, DijkstraIter++) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
(*DijkstraIter).ParentIter = DijkstraNodes.end();
|
2010-08-06 13:13:25 +00:00
|
|
|
|
if (Region.IsLineOfSight(*NodesIter, Start))(*DijkstraIter).Cost = (*NodesIter).Distance(Start);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
BS_ASSERT(DijkstraIter == DijkstraNodes.end());
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-06 13:13:25 +00:00
|
|
|
|
static DijkstraNode::Iter ChooseClosestNode(DijkstraNode::Container &Nodes) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
DijkstraNode::Iter ClosestNodeInter = Nodes.end();
|
2010-07-30 12:19:13 +00:00
|
|
|
|
int MinCost = infinity;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
for (DijkstraNode::Iter iter = Nodes.begin(); iter != Nodes.end(); iter++) {
|
|
|
|
|
if (!(*iter).Chosen && (*iter).Cost < MinCost) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
MinCost = (*iter).Cost;
|
|
|
|
|
ClosestNodeInter = iter;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ClosestNodeInter;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
static void RelaxNodes(DijkstraNode::Container &Nodes,
|
2010-08-06 13:13:25 +00:00
|
|
|
|
const Common::Array< Common::Array<int> > &VisibilityMatrix,
|
|
|
|
|
const DijkstraNode::ConstIter &CurNodeIter) {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// All the successors of the current node that have not been chosen will be
|
|
|
|
|
// inserted into the boundary node list, and the cost will be updated if
|
|
|
|
|
// a shorter path has been found to them.
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
int CurNodeIndex = CurNodeIter - Nodes.begin();
|
2010-09-02 12:14:04 +00:00
|
|
|
|
for (uint i = 0; i < Nodes.size(); i++) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
int Cost = VisibilityMatrix[CurNodeIndex][i];
|
2010-08-01 08:31:50 +00:00
|
|
|
|
if (!Nodes[i].Chosen && Cost != infinity) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
int TotalCost = (*CurNodeIter).Cost + Cost;
|
2010-08-01 08:31:50 +00:00
|
|
|
|
if (TotalCost < Nodes[i].Cost) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Nodes[i].ParentIter = CurNodeIter;
|
|
|
|
|
Nodes[i].Cost = TotalCost;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-18 12:57:47 +00:00
|
|
|
|
static void RelaxEndPoint(const Vertex &CurNodePos,
|
2010-08-06 13:13:25 +00:00
|
|
|
|
const DijkstraNode::ConstIter &CurNodeIter,
|
2010-08-18 12:57:47 +00:00
|
|
|
|
const Vertex &EndPointPos,
|
2010-08-06 13:13:25 +00:00
|
|
|
|
DijkstraNode &EndPoint,
|
2010-08-18 12:57:47 +00:00
|
|
|
|
const Region &Region) {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
if (Region.IsLineOfSight(CurNodePos, EndPointPos)) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
int TotalCost = (*CurNodeIter).Cost + CurNodePos.Distance(EndPointPos);
|
2010-08-01 08:31:50 +00:00
|
|
|
|
if (TotalCost < EndPoint.Cost) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
EndPoint.ParentIter = CurNodeIter;
|
|
|
|
|
EndPoint.Cost = TotalCost;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-18 12:57:47 +00:00
|
|
|
|
bool WalkRegion::FindPath(const Vertex &Start, const Vertex &End, BS_Path &Path) const {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// This is an implementation of Dijkstra's algorithm
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Initialise edge node list
|
2010-07-29 19:53:02 +00:00
|
|
|
|
DijkstraNode::Container DijkstraNodes;
|
|
|
|
|
InitDijkstraNodes(DijkstraNodes, *this, Start, m_Nodes);
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// The end point is treated separately, since it does not exist in the visibility graph
|
2010-07-29 19:53:02 +00:00
|
|
|
|
DijkstraNode EndPoint;
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Since a node is selected each round from the node list, and can never be selected again
|
|
|
|
|
// after that, the maximum number of loop iterations is limited by the number of nodes
|
2010-09-02 12:14:04 +00:00
|
|
|
|
for (uint i = 0; i < m_Nodes.size(); i++) {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Determine the nearest edge node in the node list
|
2010-07-29 19:53:02 +00:00
|
|
|
|
DijkstraNode::Iter NodeInter = ChooseClosestNode(DijkstraNodes);
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// If no free nodes are absent from the edge node list, there is no path from start
|
|
|
|
|
// to end node. This case should never occur, since the number of loop passes is
|
|
|
|
|
// limited, but etter safe than sorry
|
2010-07-29 19:53:02 +00:00
|
|
|
|
if (NodeInter == DijkstraNodes.end()) return false;
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// If the destination point is closer than the point cost, scan can stop
|
2010-08-23 10:01:43 +00:00
|
|
|
|
(*NodeInter).Chosen = true;
|
2010-08-01 08:31:50 +00:00
|
|
|
|
if (EndPoint.Cost <= (*NodeInter).Cost) {
|
|
|
|
|
// Insert the end point in the list
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Path.push_back(End);
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// The list is done in reverse order and inserted into the path
|
2010-07-29 19:53:02 +00:00
|
|
|
|
DijkstraNode::ConstIter CurNode = EndPoint.ParentIter;
|
2010-08-01 08:31:50 +00:00
|
|
|
|
while (CurNode != DijkstraNodes.end()) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
BS_ASSERT((*CurNode).Chosen);
|
|
|
|
|
Path.push_back(m_Nodes[CurNode - DijkstraNodes.begin()]);
|
|
|
|
|
CurNode = (*CurNode).ParentIter;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// The starting point is inserted into the path
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Path.push_back(Start);
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// The nodes of the path must be untwisted, as they were extracted in reverse order.
|
|
|
|
|
// This step could be saved if the path from end to the beginning was desired
|
2010-08-18 12:57:47 +00:00
|
|
|
|
ReverseArray<Vertex>(Path);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Relaxation step for nodes of the graph, and perform the end nodes
|
2010-07-29 19:53:02 +00:00
|
|
|
|
RelaxNodes(DijkstraNodes, m_VisibilityMatrix, NodeInter);
|
|
|
|
|
RelaxEndPoint(m_Nodes[NodeInter - DijkstraNodes.begin()], NodeInter, End, EndPoint, *this);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// If the loop has been completely run through, all the nodes have been chosen, and still
|
|
|
|
|
// no path was found. There is therefore no path available
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-18 12:57:47 +00:00
|
|
|
|
void WalkRegion::InitNodeVector() {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Empty the Node list
|
2010-07-29 19:53:02 +00:00
|
|
|
|
m_Nodes.clear();
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Determine the number of nodes
|
2010-07-29 19:53:02 +00:00
|
|
|
|
int NodeCount = 0;
|
|
|
|
|
{
|
2010-09-02 12:14:04 +00:00
|
|
|
|
for (uint i = 0; i < m_Polygons.size(); i++)
|
2010-07-29 19:53:02 +00:00
|
|
|
|
NodeCount += m_Polygons[i].VertexCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Knoten-Vector f<>llen
|
|
|
|
|
m_Nodes.reserve(NodeCount);
|
|
|
|
|
{
|
2010-09-02 12:14:04 +00:00
|
|
|
|
for (uint j = 0; j < m_Polygons.size(); j++)
|
2010-07-29 19:53:02 +00:00
|
|
|
|
for (int i = 0; i < m_Polygons[j].VertexCount; i++)
|
|
|
|
|
m_Nodes.push_back(m_Polygons[j].Vertecies[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-18 12:57:47 +00:00
|
|
|
|
void WalkRegion::ComputeVisibilityMatrix() {
|
2010-08-06 13:13:25 +00:00
|
|
|
|
// Initialise visibility matrix
|
2010-08-01 08:31:50 +00:00
|
|
|
|
m_VisibilityMatrix = Common::Array< Common::Array <int> >();
|
|
|
|
|
for (uint idx = 0; idx < m_Nodes.size(); ++idx) {
|
|
|
|
|
Common::Array<int> arr;
|
|
|
|
|
for (uint idx2 = 0; idx2 < m_Nodes.size(); ++idx2)
|
|
|
|
|
arr.push_back(infinity);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
m_VisibilityMatrix.push_back(arr);
|
|
|
|
|
}
|
2010-08-06 13:13:25 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Calculate visibility been vertecies
|
2010-09-02 12:14:04 +00:00
|
|
|
|
for (uint j = 0; j < m_Nodes.size(); ++j) {
|
|
|
|
|
for (uint i = j; i < m_Nodes.size(); ++i) {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
if (IsLineOfSight(m_Nodes[i], m_Nodes[j])) {
|
|
|
|
|
// There is a line of sight, so save the distance between the two
|
2010-07-29 19:53:02 +00:00
|
|
|
|
int Distance = m_Nodes[i].Distance(m_Nodes[j]);
|
|
|
|
|
m_VisibilityMatrix[i][j] = Distance;
|
|
|
|
|
m_VisibilityMatrix[j][i] = Distance;
|
2010-08-01 08:31:50 +00:00
|
|
|
|
} else {
|
|
|
|
|
// There is no line of sight, so save infinity as the distance
|
2010-07-30 12:19:13 +00:00
|
|
|
|
m_VisibilityMatrix[i][j] = infinity;
|
|
|
|
|
m_VisibilityMatrix[j][i] = infinity;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-18 12:57:47 +00:00
|
|
|
|
bool WalkRegion::CheckAndPrepareStartAndEnd(Vertex &Start, Vertex &End) const {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
if (!IsPointInRegion(Start)) {
|
2010-08-18 12:57:47 +00:00
|
|
|
|
Vertex NewStart = FindClosestRegionPoint(Start);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Check to make sure the point is really in the region. If not, stop with an error
|
|
|
|
|
if (!IsPointInRegion(NewStart)) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
BS_LOG_ERRORLN("Constructed startpoint ((%d,%d) from (%d,%d)) is not inside the region.",
|
2010-08-06 13:13:25 +00:00
|
|
|
|
NewStart.X, NewStart.Y,
|
|
|
|
|
Start.X, Start.Y);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Start = NewStart;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// If the destination is outside the region, a point is determined that is within the region,
|
|
|
|
|
// and that is used as an endpoint instead
|
|
|
|
|
if (!IsPointInRegion(End)) {
|
2010-08-18 12:57:47 +00:00
|
|
|
|
Vertex NewEnd = FindClosestRegionPoint(End);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Make sure that the determined point is really within the region
|
|
|
|
|
if (!IsPointInRegion(NewEnd)) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
BS_LOG_ERRORLN("Constructed endpoint ((%d,%d) from (%d,%d)) is not inside the region.",
|
2010-08-06 13:13:25 +00:00
|
|
|
|
NewEnd.X, NewEnd.Y,
|
|
|
|
|
End.X, End.Y);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
End = NewEnd;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Signal success
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-18 12:57:47 +00:00
|
|
|
|
void WalkRegion::SetPos(int X, int Y) {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Calculate the difference between old and new position
|
2010-08-18 12:57:47 +00:00
|
|
|
|
Vertex Delta(X - m_Position.X, Y - m_Position.Y);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Move all the nodes
|
2010-09-02 12:14:04 +00:00
|
|
|
|
for (uint i = 0; i < m_Nodes.size(); i++) m_Nodes[i] += Delta;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Move regions
|
2010-08-18 12:57:47 +00:00
|
|
|
|
Region::SetPos(X, Y);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-18 12:58:22 +00:00
|
|
|
|
bool WalkRegion::Persist(OutputPersistenceBlock &Writer) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
bool Result = true;
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Persist the parent region
|
2010-08-18 12:57:47 +00:00
|
|
|
|
Result &= Region::Persist(Writer);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Persist the nodes
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Writer.Write(m_Nodes.size());
|
2010-08-18 12:57:47 +00:00
|
|
|
|
Common::Array<Vertex>::const_iterator It = m_Nodes.begin();
|
2010-08-01 08:31:50 +00:00
|
|
|
|
while (It != m_Nodes.end()) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Writer.Write(It->X);
|
|
|
|
|
Writer.Write(It->Y);
|
|
|
|
|
++It;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Persist the visibility matrix
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Writer.Write(m_VisibilityMatrix.size());
|
2010-08-01 08:31:50 +00:00
|
|
|
|
Common::Array< Common::Array<int> >::const_iterator RowIter = m_VisibilityMatrix.begin();
|
|
|
|
|
while (RowIter != m_VisibilityMatrix.end()) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Writer.Write(RowIter->size());
|
2010-08-01 08:31:50 +00:00
|
|
|
|
Common::Array<int>::const_iterator ColIter = RowIter->begin();
|
|
|
|
|
while (ColIter != RowIter->end()) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Writer.Write(*ColIter);
|
|
|
|
|
++ColIter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++RowIter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2010-08-18 12:58:22 +00:00
|
|
|
|
bool WalkRegion::Unpersist(InputPersistenceBlock &Reader) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
bool Result = true;
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// The parent object was already loaded in the constructor of BS_Region, so at
|
|
|
|
|
// this point only the additional data from BS_WalkRegion needs to be loaded
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Node load
|
2010-09-02 12:14:04 +00:00
|
|
|
|
uint NodeCount;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Reader.Read(NodeCount);
|
|
|
|
|
m_Nodes.clear();
|
|
|
|
|
m_Nodes.resize(NodeCount);
|
2010-08-18 12:57:47 +00:00
|
|
|
|
Common::Array<Vertex>::iterator It = m_Nodes.begin();
|
2010-08-01 08:31:50 +00:00
|
|
|
|
while (It != m_Nodes.end()) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Reader.Read(It->X);
|
|
|
|
|
Reader.Read(It->Y);
|
|
|
|
|
++It;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Visibility matrix load
|
2010-09-02 12:14:04 +00:00
|
|
|
|
uint RowCount;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Reader.Read(RowCount);
|
|
|
|
|
m_VisibilityMatrix.clear();
|
|
|
|
|
m_VisibilityMatrix.resize(RowCount);
|
2010-08-01 08:31:50 +00:00
|
|
|
|
Common::Array< Common::Array<int> >::iterator RowIter = m_VisibilityMatrix.begin();
|
|
|
|
|
while (RowIter != m_VisibilityMatrix.end()) {
|
2010-09-02 12:14:04 +00:00
|
|
|
|
uint ColCount;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Reader.Read(ColCount);
|
|
|
|
|
RowIter->resize(ColCount);
|
2010-08-01 08:31:50 +00:00
|
|
|
|
Common::Array<int>::iterator ColIter = RowIter->begin();
|
|
|
|
|
while (ColIter != RowIter->end()) {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
Reader.Read(*ColIter);
|
|
|
|
|
++ColIter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++RowIter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result && Reader.IsGood();
|
|
|
|
|
}
|
2010-08-01 08:31:50 +00:00
|
|
|
|
|
|
|
|
|
} // End of namespace Sword25
|