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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
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
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
namespace Sword25 {
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
static const int Infinity = 0x7fffffff;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-18 12:57:47 +00:00
|
|
|
|
WalkRegion::WalkRegion() {
|
2010-10-03 13:25:36 +00:00
|
|
|
|
_type = RT_WALKREGION;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
WalkRegion::WalkRegion(InputPersistenceBlock &reader, uint handle) :
|
|
|
|
|
Region(reader, handle) {
|
|
|
|
|
_type = RT_WALKREGION;
|
|
|
|
|
unpersist(reader);
|
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
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +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-10-03 13:25:36 +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-10-03 13:25:36 +00:00
|
|
|
|
initNodeVector();
|
|
|
|
|
computeVisibilityMatrix();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Signal success
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
bool WalkRegion::queryPath(Vertex startPoint, Vertex endPoint, BS_Path &path) {
|
2011-01-23 15:01:24 +00:00
|
|
|
|
assert(path.empty());
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// If the start and finish are identical, no path can be found trivially
|
2010-10-03 13:25:36 +00:00
|
|
|
|
if (startPoint == endPoint)
|
|
|
|
|
return true;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
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-10-03 13:25:36 +00:00
|
|
|
|
if (!checkAndPrepareStartAndEnd(startPoint, endPoint)) return false;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
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-10-03 13:25:36 +00:00
|
|
|
|
if (isLineOfSight(startPoint, endPoint)) {
|
|
|
|
|
path.push_back(startPoint);
|
|
|
|
|
path.push_back(endPoint);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
return findPath(startPoint, endPoint, path);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
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-11-28 21:33:27 +00:00
|
|
|
|
DijkstraNode() : parentIter(), cost(Infinity), chosen(false) {}
|
2010-10-03 13:25:36 +00:00
|
|
|
|
ConstIter parentIter;
|
|
|
|
|
int cost;
|
|
|
|
|
bool chosen;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
};
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
static void initDijkstraNodes(DijkstraNode::Container &dijkstraNodes, const Region ®ion,
|
|
|
|
|
const Vertex &start, const Common::Array<Vertex> &nodes) {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Allocate sufficient space in the array
|
2010-10-03 13:25:36 +00:00
|
|
|
|
dijkstraNodes.resize(nodes.size());
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2011-05-25 11:17:11 -04:00
|
|
|
|
// Initialize all the nodes which are visible from the starting node
|
2010-10-03 13:25:36 +00:00
|
|
|
|
DijkstraNode::Iter dijkstraIter = dijkstraNodes.begin();
|
|
|
|
|
for (Common::Array<Vertex>::const_iterator nodesIter = nodes.begin();
|
|
|
|
|
nodesIter != nodes.end(); nodesIter++, dijkstraIter++) {
|
|
|
|
|
(*dijkstraIter).parentIter = dijkstraNodes.end();
|
|
|
|
|
if (region.isLineOfSight(*nodesIter, start))(*dijkstraIter).cost = (*nodesIter).distance(start);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
2011-01-23 15:01:24 +00:00
|
|
|
|
assert(dijkstraIter == dijkstraNodes.end());
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
static DijkstraNode::Iter chooseClosestNode(DijkstraNode::Container &nodes) {
|
|
|
|
|
DijkstraNode::Iter closestNodeInter = nodes.end();
|
|
|
|
|
int minCost = Infinity;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
for (DijkstraNode::Iter iter = nodes.begin(); iter != nodes.end(); iter++) {
|
|
|
|
|
if (!(*iter).chosen && (*iter).cost < minCost) {
|
|
|
|
|
minCost = (*iter).cost;
|
|
|
|
|
closestNodeInter = iter;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
return closestNodeInter;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
static void relaxNodes(DijkstraNode::Container &nodes,
|
|
|
|
|
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
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
int curNodeIndex = curNodeIter - nodes.begin();
|
|
|
|
|
for (uint i = 0; i < nodes.size(); i++) {
|
|
|
|
|
int cost = visibilityMatrix[curNodeIndex][i];
|
|
|
|
|
if (!nodes[i].chosen && cost != Infinity) {
|
|
|
|
|
int totalCost = (*curNodeIter).cost + cost;
|
|
|
|
|
if (totalCost < nodes[i].cost) {
|
|
|
|
|
nodes[i].parentIter = curNodeIter;
|
|
|
|
|
nodes[i].cost = totalCost;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
static void relaxEndPoint(const Vertex &curNodePos,
|
|
|
|
|
const DijkstraNode::ConstIter &curNodeIter,
|
|
|
|
|
const Vertex &endPointPos,
|
|
|
|
|
DijkstraNode &endPoint,
|
|
|
|
|
const Region ®ion) {
|
|
|
|
|
if (region.isLineOfSight(curNodePos, endPointPos)) {
|
|
|
|
|
int totalCost = (*curNodeIter).cost + curNodePos.distance(endPointPos);
|
|
|
|
|
if (totalCost < endPoint.cost) {
|
|
|
|
|
endPoint.parentIter = curNodeIter;
|
|
|
|
|
endPoint.cost = totalCost;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-19 09:43:31 +00:00
|
|
|
|
template<class T>
|
2010-10-19 21:03:33 +00:00
|
|
|
|
void reverseArray(Common::Array<T> &arr) {
|
2010-10-19 09:43:31 +00:00
|
|
|
|
const uint size = arr.size();
|
|
|
|
|
if (size < 2)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (uint i = 0; i <= (size / 2 - 1); ++i) {
|
|
|
|
|
SWAP(arr[i], arr[size - i - 1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +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
|
|
|
|
|
2011-05-25 11:17:11 -04:00
|
|
|
|
// Initialize edge node list
|
2010-10-03 13:25:36 +00:00
|
|
|
|
DijkstraNode::Container dijkstraNodes;
|
|
|
|
|
initDijkstraNodes(dijkstraNodes, *this, start, _nodes);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// The end point is treated separately, since it does not exist in the visibility graph
|
2010-10-03 13:25:36 +00:00
|
|
|
|
DijkstraNode endPoint;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
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-10-03 13:25:36 +00:00
|
|
|
|
for (uint i = 0; i < _nodes.size(); i++) {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Determine the nearest edge node in the node list
|
2010-10-03 13:25:36 +00:00
|
|
|
|
DijkstraNode::Iter nodeInter = chooseClosestNode(dijkstraNodes);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
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-10-03 13:25:36 +00:00
|
|
|
|
if (nodeInter == dijkstraNodes.end())
|
|
|
|
|
return false;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// If the destination point is closer than the point cost, scan can stop
|
2010-10-03 13:25:36 +00:00
|
|
|
|
(*nodeInter).chosen = true;
|
|
|
|
|
if (endPoint.cost <= (*nodeInter).cost) {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Insert the end point in the list
|
2010-10-03 13:25:36 +00:00
|
|
|
|
path.push_back(end);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// The list is done in reverse order and inserted into the path
|
2010-10-03 13:25:36 +00:00
|
|
|
|
DijkstraNode::ConstIter curNode = endPoint.parentIter;
|
|
|
|
|
while (curNode != dijkstraNodes.end()) {
|
2011-01-23 15:01:24 +00:00
|
|
|
|
assert((*curNode).chosen);
|
2010-10-03 13:25:36 +00:00
|
|
|
|
path.push_back(_nodes[curNode - dijkstraNodes.begin()]);
|
|
|
|
|
curNode = (*curNode).parentIter;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// The starting point is inserted into the path
|
2010-10-03 13:25:36 +00:00
|
|
|
|
path.push_back(start);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
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-10-19 21:03:33 +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-10-03 13:25:36 +00:00
|
|
|
|
relaxNodes(dijkstraNodes, _visibilityMatrix, nodeInter);
|
|
|
|
|
relaxEndPoint(_nodes[nodeInter - dijkstraNodes.begin()], nodeInter, end, endPoint, *this);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
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-10-03 13:25:36 +00:00
|
|
|
|
void WalkRegion::initNodeVector() {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Empty the Node list
|
2010-10-03 13:25:36 +00:00
|
|
|
|
_nodes.clear();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Determine the number of nodes
|
2010-10-03 13:25:36 +00:00
|
|
|
|
int nodeCount = 0;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
{
|
2010-10-03 13:25:36 +00:00
|
|
|
|
for (uint i = 0; i < _polygons.size(); i++)
|
|
|
|
|
nodeCount += _polygons[i].vertexCount;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Knoten-Vector f<>llen
|
2010-10-03 13:25:36 +00:00
|
|
|
|
_nodes.reserve(nodeCount);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
{
|
2010-10-03 13:25:36 +00:00
|
|
|
|
for (uint j = 0; j < _polygons.size(); j++)
|
|
|
|
|
for (int i = 0; i < _polygons[j].vertexCount; i++)
|
|
|
|
|
_nodes.push_back(_polygons[j].vertices[i]);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
void WalkRegion::computeVisibilityMatrix() {
|
2011-05-25 11:17:11 -04:00
|
|
|
|
// Initialize visibility matrix
|
2010-10-03 13:25:36 +00:00
|
|
|
|
_visibilityMatrix = Common::Array< Common::Array <int> >();
|
|
|
|
|
for (uint idx = 0; idx < _nodes.size(); ++idx) {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
Common::Array<int> arr;
|
2010-10-03 13:25:36 +00:00
|
|
|
|
for (uint idx2 = 0; idx2 < _nodes.size(); ++idx2)
|
|
|
|
|
arr.push_back(Infinity);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
_visibilityMatrix.push_back(arr);
|
2010-08-01 08:31:50 +00:00
|
|
|
|
}
|
2010-08-06 13:13:25 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Calculate visibility been vertecies
|
2010-10-03 13:25:36 +00:00
|
|
|
|
for (uint j = 0; j < _nodes.size(); ++j) {
|
|
|
|
|
for (uint i = j; i < _nodes.size(); ++i) {
|
|
|
|
|
if (isLineOfSight(_nodes[i], _nodes[j])) {
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// There is a line of sight, so save the distance between the two
|
2010-10-03 13:25:36 +00:00
|
|
|
|
int distance = _nodes[i].distance(_nodes[j]);
|
|
|
|
|
_visibilityMatrix[i][j] = distance;
|
|
|
|
|
_visibilityMatrix[j][i] = distance;
|
2010-08-01 08:31:50 +00:00
|
|
|
|
} else {
|
2010-10-03 13:25:36 +00:00
|
|
|
|
// There is no line of sight, so save Infinity as the distance
|
|
|
|
|
_visibilityMatrix[i][j] = Infinity;
|
|
|
|
|
_visibilityMatrix[j][i] = Infinity;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
bool WalkRegion::checkAndPrepareStartAndEnd(Vertex &start, Vertex &end) const {
|
|
|
|
|
if (!isPointInRegion(start)) {
|
|
|
|
|
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
|
2010-10-03 13:25:36 +00:00
|
|
|
|
if (!isPointInRegion(newStart)) {
|
2011-01-23 14:49:50 +00:00
|
|
|
|
error("Constructed startpoint ((%d,%d) from (%d,%d)) is not inside the region.",
|
2010-10-03 13:25:36 +00:00
|
|
|
|
newStart.x, newStart.y,
|
|
|
|
|
start.x, start.y);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
start = newStart;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
2010-10-03 13:25:36 +00:00
|
|
|
|
if (!isPointInRegion(end)) {
|
|
|
|
|
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
|
2010-10-03 13:25:36 +00:00
|
|
|
|
if (!isPointInRegion(newEnd)) {
|
2011-01-23 14:49:50 +00:00
|
|
|
|
error("Constructed endpoint ((%d,%d) from (%d,%d)) is not inside the region.",
|
2010-10-03 13:25:36 +00:00
|
|
|
|
newEnd.x, newEnd.y,
|
|
|
|
|
end.x, end.y);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
end = newEnd;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Signal success
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +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-10-03 13:25:36 +00:00
|
|
|
|
Vertex Delta(x - _position.x, y - _position.y);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Move all the nodes
|
2010-10-03 13:25:36 +00:00
|
|
|
|
for (uint i = 0; i < _nodes.size(); i++)
|
|
|
|
|
_nodes[i] += Delta;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Move regions
|
2010-10-03 13:25:36 +00:00
|
|
|
|
Region::setPos(x, y);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-02 17:11:45 +00:00
|
|
|
|
bool WalkRegion::persist(OutputPersistenceBlock &writer) {
|
|
|
|
|
bool result = true;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Persist the parent region
|
2010-09-02 17:11:45 +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-10-03 13:25:36 +00:00
|
|
|
|
writer.write(_nodes.size());
|
|
|
|
|
Common::Array<Vertex>::const_iterator it = _nodes.begin();
|
|
|
|
|
while (it != _nodes.end()) {
|
|
|
|
|
writer.write(it->x);
|
|
|
|
|
writer.write(it->y);
|
|
|
|
|
++it;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Persist the visibility matrix
|
2010-10-03 13:25:36 +00:00
|
|
|
|
writer.write(_visibilityMatrix.size());
|
|
|
|
|
Common::Array< Common::Array<int> >::const_iterator rowIter = _visibilityMatrix.begin();
|
|
|
|
|
while (rowIter != _visibilityMatrix.end()) {
|
|
|
|
|
writer.write(rowIter->size());
|
|
|
|
|
Common::Array<int>::const_iterator colIter = rowIter->begin();
|
|
|
|
|
while (colIter != rowIter->end()) {
|
|
|
|
|
writer.write(*colIter);
|
|
|
|
|
++colIter;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
++rowIter;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-02 17:11:45 +00:00
|
|
|
|
return result;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-02 17:11:45 +00:00
|
|
|
|
bool WalkRegion::unpersist(InputPersistenceBlock &reader) {
|
|
|
|
|
bool result = true;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
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-10-03 13:25:36 +00:00
|
|
|
|
uint nodeCount;
|
|
|
|
|
reader.read(nodeCount);
|
|
|
|
|
_nodes.clear();
|
|
|
|
|
_nodes.resize(nodeCount);
|
|
|
|
|
Common::Array<Vertex>::iterator it = _nodes.begin();
|
|
|
|
|
while (it != _nodes.end()) {
|
|
|
|
|
reader.read(it->x);
|
|
|
|
|
reader.read(it->y);
|
|
|
|
|
++it;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-08-01 08:31:50 +00:00
|
|
|
|
// Visibility matrix load
|
2010-10-03 13:25:36 +00:00
|
|
|
|
uint rowCount;
|
|
|
|
|
reader.read(rowCount);
|
|
|
|
|
_visibilityMatrix.clear();
|
|
|
|
|
_visibilityMatrix.resize(rowCount);
|
|
|
|
|
Common::Array< Common::Array<int> >::iterator rowIter = _visibilityMatrix.begin();
|
|
|
|
|
while (rowIter != _visibilityMatrix.end()) {
|
|
|
|
|
uint colCount;
|
|
|
|
|
reader.read(colCount);
|
|
|
|
|
rowIter->resize(colCount);
|
|
|
|
|
Common::Array<int>::iterator colIter = rowIter->begin();
|
|
|
|
|
while (colIter != rowIter->end()) {
|
|
|
|
|
reader.read(*colIter);
|
|
|
|
|
++colIter;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-03 13:25:36 +00:00
|
|
|
|
++rowIter;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-02 17:11:45 +00:00
|
|
|
|
return result && reader.isGood();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
2010-08-01 08:31:50 +00:00
|
|
|
|
|
|
|
|
|
} // End of namespace Sword25
|