File: PathfindingGrid.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (279 lines) | stat: -rw-r--r-- 8,339 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * This code is based on the CRAB engine
 *
 * Copyright (c) Arvind Raja Yadav
 *
 * Licensed under MIT
 *
 */

#include "crab/PathfindingGrid.h"
#include "crab/TMX/TMXMap.h"

namespace Crab {

using namespace TMX;

PathfindingGrid::PathfindingGrid() {
	_blockedCost = BLOCKED;
	_openCost = OPEN;
	_stairsCost = STAIRS;
	_nodes = nullptr;

	_dimensions.x = 0;
	_dimensions.y = 0;

	_cellSize.x = 0.0;
	_cellSize.y = 0.0;
}

PathfindingGrid::~PathfindingGrid() {
	reset();
}

void PathfindingGrid::reset() {
	for (int x = 0; x < _dimensions.x; ++x) {
		delete[] _nodes[x];
	}

	delete[] _nodes;
	_nodes = nullptr;

	_dimensions.x = 0;
	_dimensions.y = 0;

	_cellSize.x = 0.0;
	_cellSize.y = 0.0;
}

void PathfindingGrid::setupNodes(const TMX::TMXMap &map) {
	// delete nodes if they exist
	reset();

	_dimensions.x = map._pathRows; // Logically, this is incorrect but it matches the format of cols and rows used elsewhere (SZ)
	_dimensions.y = map._pathCols;

	_cellSize.x = (float)map._pathSize.x;
	_cellSize.y = (float)map._pathSize.y;

	// Check to see if the costs have been loaded from the level file.
	// If not, assign to defaults.
	if (map._movementCosts._noWalk != 0) {
		_blockedCost = map._movementCosts._noWalk;
	}
	if (map._movementCosts._open != 0) {
		_openCost = map._movementCosts._open;
	}
	if (map._movementCosts._stairs != 0) {
		_stairsCost = map._movementCosts._stairs;
	}

	_nodes = new PathfindingGraphNode *[_dimensions.x];

	// Allocate some nodes!
	// TODO: probably want to change this to a one chunk allocation...
	for (int i = 0; i < _dimensions.x; ++i) {
		_nodes[i] = new PathfindingGraphNode[_dimensions.y];
	}

	// Fill up those nodes!
	int idCounter = 0;

	Vector2f pos = Vector2f(0.0f, 0.0f);
	Vector2f topLeftPos = pos;

	// Initialize the nodes
	for (int x = 0; x < _dimensions.x; ++x) {
		for (int y = 0; y < _dimensions.y; ++y) {
			// PathfindingGraphNode* newNode = new PathfindingGraphNode(pos, idCounter++);

			// nodes[x][y] = *newNode;
			_nodes[x][y]._collisionRect = Rect(pos.x, pos.y, _cellSize.x, _cellSize.y);

			_nodes[x][y]._position.x = pos.x + _cellSize.x / 2.0f;
			_nodes[x][y]._position.y = pos.y + _cellSize.y / 2.0f;
			_nodes[x][y]._id = idCounter++;

			_nodes[x][y]._movementCost = _openCost;
			_nodes[x][y]._neighborCosts.reserve(4); // since its a square based grid, 4 is the greatest number of costs and nodes possible.
			_nodes[x][y]._neighborNodes.reserve(4);

			pos.y += _cellSize.y;

			const Common::Array<Shape> &noWalk = map.areaNoWalk();

			// Check if the square should count as blocked
			for (const auto &i : noWalk) {
				if (i.collide(_nodes[x][y]._collisionRect)._intersect) {
					_nodes[x][y]._movementCost = (float)_blockedCost;
					break;
				}
			}

			// Check for stairs if the cell isn't blocked
			if (_nodes[x][y]._movementCost >= 0.0f) {
				const Common::Array<pyrodactyl::level::Stairs> &stairs = map.areaStairs();

				for (const auto &i : stairs) {
					if (i.collide(_nodes[x][y]._collisionRect)._intersect) {
						_nodes[x][y]._movementCost = (float)_stairsCost;
						break;
					}
				}
			}

			// More collision checks can be added for the node as long as it checks for the high cost objects first
			// since the highest cost collider in any given tile would be used for the path cost. (SZ)
		}

		pos.x += _cellSize.x;
		pos.y = topLeftPos.y;
	}

	// Connect the nodes
	for (int x = 0; x < _dimensions.x; ++x) {
		for (int y = 0; y < _dimensions.y; ++y) {
			// Check horizontal
			if (x < _dimensions.x - 1) {
				connectNodes(&_nodes[x][y], &_nodes[x + 1][y]);

				// Check diagonals
				// This causes hangups since the collider has a greater width to take into account when traveling
				// diagonally compared to horizontal or vertical. (SZ)
				/*if( y < dimensions.y - 2)
				{
				ConnectNodes(&nodes[x][y], &nodes[x + 1][y + 1]);

				nodes[x][y].neighborCosts[nodes[x][y].neighborCosts.size() - 1] *= 1.41f;
				nodes[x + 1][y + 1].movementCost *= 1.41f;
				}

				if(y > 0)
				{
				ConnectNodes(&nodes[x][y], &nodes[x + 1][y - 1]);

				nodes[x][y].neighborCosts[nodes[x][y].neighborCosts.size() - 1] *= 1.41f;
				nodes[x + 1][y - 1].movementCost *= 1.41f;
				}*/
			}
			// Check vertical
			if (y < _dimensions.y - 1) {
				connectNodes(&_nodes[x][y], &_nodes[x][y + 1]);
			}
		}
	}

	////Check for adjacencies
	////This could be used if additional weight should be applied to nodes adjacent to blocked nodes.
	// for(int x = 0; x < dimensions.x; ++x)
	//{
	//	for(int y = 0; y < dimensions.y; ++y)
	//	{
	//		for(int i = 0; i < nodes[x][y].neighborNodes.size(); ++i)
	//		{
	//			if(nodes[x][y].neighborNodes[i]->movementCost == blockedCost)
	//			{
	//				nodes[x][y].movementCost *= 2.0f;
	//				break;
	//			}
	//		}
	//	}
	// }
}

void PathfindingGrid::connectNodes(PathfindingGraphNode *node1, PathfindingGraphNode *node2) {
	node1->addNeighbor(node2, true);
	node2->addNeighbor(node1, true);
}

PathfindingGraphNode *PathfindingGrid::getNodeAtPoint(Vector2f point) {
	int x = (int)floor(point.x / _cellSize.x);
	int y = (int)floor(point.y / _cellSize.y);

	return &_nodes[x][y];
}

Common::Array<PathfindingGraphNode *> PathfindingGrid::cornerCheck(const PathfindingGraphNode *node1, const PathfindingGraphNode *node2) {
	Common::Array<PathfindingGraphNode *> returnNodes;

	// Iterat through both nodes neighbors. If a blocked neighbor is found that is shared between the two,
	// It is a corner to them.
	for (auto iter : node1->_neighborNodes) {
		for (auto iter2 : node2->_neighborNodes) {
			if (iter == iter2 && iter->_movementCost < 0) {
				if (returnNodes.size() == 0 || (*(Common::find(returnNodes.begin(), returnNodes.end(), iter))) == nullptr)
					returnNodes.push_back(iter);
			}
		}
	}

	return returnNodes;
}

PathfindingGraphNode *PathfindingGrid::getNearestOpenNode(Vector2f nodePos, Vector2f comparePos) {
	PathfindingGraphNode *startNode = getNodeAtPoint(nodePos);

	if (startNode->getMovementCost() > 0) // If the clicked node is open, we're done!
		return startNode;

	PathfindingGraphNode *returnNode = nullptr;

	float shortestDistance = 0.0f;

	Common::List<PathfindingGraphNode *> checkNodes;
	checkNodes.push_back(startNode);

	Common::Array<PathfindingGraphNode *> allUsedNodes;
	allUsedNodes.push_back(startNode);

	// Iterate through the nodes, check if they are open then check their distance from the compare point.
	while (!checkNodes.empty()) {
		if (checkNodes.front()->getMovementCost() > 0) {
			float distance = (comparePos - checkNodes.front()->getPosition()).magSqr();

			if (shortestDistance == 0.0f || distance) { // If this is the new shortest distance, this becomes the new return.
				shortestDistance = distance;

				returnNode = checkNodes.front();
			}
		} else {
			for (uint i = 0; i < checkNodes.front()->_neighborNodes.size(); ++i) {
				// If the neighbor hasn't been checked yet, add it to the list to check.
				if (Common::find(allUsedNodes.begin(), allUsedNodes.end(), checkNodes.front()->_neighborNodes[i]) == allUsedNodes.end()) {
					allUsedNodes.push_back(checkNodes.front()->_neighborNodes[i]);
					checkNodes.push_back(checkNodes.front()->_neighborNodes[i]);
				}
			}
		}

		if (returnNode != nullptr) // If a node has been found, we are done. We don't want to continue iterating through neighbors since it would take us further from the clicked node.
			return returnNode;

		checkNodes.pop_front();
	}

	return nullptr;
}

} // End of namespace Crab