aboutsummaryrefslogtreecommitdiff
path: root/engines/toon/path.cpp
blob: 5aae523455598446e10a83578ae7b30d133b1fb5 (plain)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* 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.
*
*/

#include "common/debug.h"

#include "toon/path.h"

namespace Toon {

PathFindingHeap::PathFindingHeap() {
	_count = 0;
	_size = 0;
	_data = NULL;
}

PathFindingHeap::~PathFindingHeap() {
	free(_data);
}

void PathFindingHeap::init(int32 size) {
	debugC(1, kDebugPath, "init(%d)", size);
	_size = size;

	free(_data);
	_data = (HeapDataGrid *)malloc(sizeof(HeapDataGrid) * _size);
	memset(_data, 0, sizeof(HeapDataGrid) * _size);
	_count = 0;
}

void PathFindingHeap::unload() {
	_count = 0;
	_size = 0;
	free(_data);
	_data = NULL;
}

void PathFindingHeap::clear() {
	debugC(1, kDebugPath, "clear()");

	_count = 0;
	memset(_data, 0, sizeof(HeapDataGrid) * _size);
}

void PathFindingHeap::push(int16 x, int16 y, int16 weight) {
	debugC(2, kDebugPath, "push(%d, %d, %d)", x, y, weight);

	if (_count == _size) {
		// Increase size by 50%
		int newSize = _size + (_size >> 1) + 1;
		HeapDataGrid *newData;

		newData = (HeapDataGrid *)realloc(_data, sizeof(HeapDataGrid) * newSize);
		if (newData == NULL) {
			warning("Aborting attempt to push onto PathFindingHeap at maximum size: %d", _count);
			return;
		}

		memset(newData + _size, 0, sizeof(HeapDataGrid) * (newSize - _size));
		_data = newData;
		_size = newSize;
	}

	_data[_count]._x = x;
	_data[_count]._y = y;
	_data[_count]._weight = weight;
	_count++;

	int32 lMax = _count-1;
	int32 lT = 0;

	while (true) {
		if (lMax <= 0)
			break;
		lT = (lMax-1) / 2;

		if (_data[lT]._weight > _data[lMax]._weight) {
			HeapDataGrid temp;
			temp = _data[lT];
			_data[lT] = _data[lMax];
			_data[lMax] = temp;
			lMax = lT;
		} else {
			break;
		}
	}
}

void PathFindingHeap::pop(int16 *x, int16 *y, int16 *weight) {
	debugC(2, kDebugPath, "pop(x, y, weight)");

	if (!_count) {
		warning("Attempt to pop empty PathFindingHeap!");
		return;
	}

	*x = _data[0]._x;
	*y = _data[0]._y;
	*weight = _data[0]._weight;

	_data[0] = _data[--_count];
	if (!_count)
		return;

	int32 lMin = 0;
	int32 lT = 0;

	while (true) {
		lT = (lMin << 1) + 1;
		if (lT < _count) {
			if (lT < _count-1) {
				if (_data[lT + 1]._weight < _data[lT]._weight)
					lT++;
			}
			if (_data[lT]._weight <= _data[lMin]._weight) {
				HeapDataGrid temp;
				temp = _data[lMin];
				_data[lMin] = _data[lT];
				_data[lT] = temp;

				lMin = lT;
			} else {
				break;
			}
		} else {
			break;
		}
	}
}

PathFinding::PathFinding() {
	_width = 0;
	_height = 0;
	_heap = new PathFindingHeap();
	_sq = NULL;
	_numBlockingRects = 0;
}

PathFinding::~PathFinding(void) {
	if (_heap)
		_heap->unload();
	delete _heap;
	delete[] _sq;
}

void PathFinding::init(Picture *mask) {
	debugC(1, kDebugPath, "init(mask)");

	_width = mask->getWidth();
	_height = mask->getHeight();
	_currentMask = mask;
	_heap->unload();
	_heap->init(500);
	delete[] _sq;
	_sq = new int32[_width * _height];
}

bool PathFinding::isLikelyWalkable(int16 x, int16 y) {
	for (uint8 i = 0; i < _numBlockingRects; i++) {
		if (_blockingRects[i][4] == 0) {
			if (x >= _blockingRects[i][0] && x <= _blockingRects[i][2] && y >= _blockingRects[i][1] && y < _blockingRects[i][3])
				return false;
		} else {
			int16 dx = abs(_blockingRects[i][0] - x);
			int16 dy = abs(_blockingRects[i][1] - y);
			if ((dx << 8) / _blockingRects[i][2] < (1 << 8) && (dy << 8) / _blockingRects[i][3] < (1 << 8)) {
				return false;
			}
		}
	}
	return true;
}

bool PathFinding::isWalkable(int16 x, int16 y) {
	debugC(2, kDebugPath, "isWalkable(%d, %d)", x, y);

	return (_currentMask->getData(x, y) & 0x1f) > 0;
}

bool PathFinding::findClosestWalkingPoint(int16 xx, int16 yy, int16 *fxx, int16 *fyy, int16 origX, int16 origY) {
	debugC(1, kDebugPath, "findClosestWalkingPoint(%d, %d, fxx, fyy, %d, %d)", xx, yy, origX, origY);

	int32 currentFound = -1;
	int32 dist = -1;
	int32 dist2 = -1;

	if (origX == -1)
		origX = xx;
	if (origY == -1)
		origY = yy;

	for (int16 y = 0; y < _height; y++) {
		for (int16 x = 0; x < _width; x++) {
			if (isWalkable(x, y) && isLikelyWalkable(x, y)) {
				int32 ndist = (x - xx) * (x - xx) + (y - yy) * (y - yy);
				int32 ndist2 = (x - origX) * (x - origX) + (y - origY) * (y - origY);
				if (currentFound < 0 || ndist < dist || (ndist == dist && ndist2 < dist2)) {
					dist = ndist;
					dist2 = ndist2;
					currentFound = y * _width + x;
				}
			}
		}
	}

	if (currentFound != -1) {
		*fxx = currentFound % _width;
		*fyy = currentFound / _width;
		return true;
	} else {
		*fxx = 0;
		*fyy = 0;
		return false;
	}
}

bool PathFinding::walkLine(int16 x, int16 y, int16 x2, int16 y2) {
	uint32 bx = x << 16;
	int32 dx = x2 - x;
	uint32 by = y << 16;
	int32 dy = y2 - y;
	uint32 adx = abs(dx);
	uint32 ady = abs(dy);
	int32 t = 0;
	if (adx <= ady)
		t = ady;
	else
		t = adx;

	int32 cdx = (dx << 16) / t;
	int32 cdy = (dy << 16) / t;

	_tempPath.clear();
	i32Point p;
	for (int32 i = t; i > 0; i--) {
		p.x = bx >> 16;
		p.y = by >> 16;
		_tempPath.insert_at(0, p);
		bx += cdx;
		by += cdy;
	}

	p.x = x2;
	p.y = y2;
	_tempPath.insert_at(0, p);

	return true;
}

bool PathFinding::lineIsWalkable(int16 x, int16 y, int16 x2, int16 y2) {
	uint32 bx = x << 16;
	int32 dx = x2 - x;
	uint32 by = y << 16;
	int32 dy = y2 - y;
	uint32 adx = abs(dx);
	uint32 ady = abs(dy);
	int32 t = 0;
	if (adx <= ady)
		t = ady;
	else
		t = adx;

	int32 cdx = (dx << 16) / t;
	int32 cdy = (dy << 16) / t;

	for (int32 i = t; i > 0; i--) {
		if (!isWalkable(bx >> 16, by >> 16))
			return false;
		bx += cdx;
		by += cdy;
	}
	return true;
}

bool PathFinding::findPath(int16 x, int16 y, int16 destx, int16 desty) {
	debugC(1, kDebugPath, "findPath(%d, %d, %d, %d)", x, y, destx, desty);

	if (x == destx && y == desty) {
		_tempPath.clear();
		return true;
	}

	// ignore path finding if the character is outside the screen
	if (x < 0 || x > 1280 || y < 0 || y > 400 || destx < 0 || destx > 1280 || desty < 0 || desty > 400) {
		_tempPath.clear();
		return true;
	}

	// first test direct line
	if (lineIsWalkable(x, y, destx, desty)) {
		walkLine(x, y, destx, desty);
		return true;
	}

	// no direct line, we use the standard A* algorithm
	memset(_sq , 0, _width * _height * sizeof(int32));
	_heap->clear();
	int16 curX = x;
	int16 curY = y;
	int16 curWeight = 0;

	_sq[curX + curY *_width] = 1;
	_heap->push(curX, curY, abs(destx - x) + abs(desty - y));
	int32 wei = 0;

	while (_heap->getCount()) {
		wei = 0;
		_heap->pop(&curX, &curY, &curWeight);
		int32 curNode = curX + curY * _width;

		int16 endX = MIN<int16>(curX + 1, _width - 1);
		int16 endY = MIN<int16>(curY + 1, _height - 1);
		int16 startX = MAX<int16>(curX - 1, 0);
		int16 startY = MAX<int16>(curY - 1, 0);
		bool next = false;

		for (int16 px = startX; px <= endX && !next; px++) {
			for (int16 py = startY; py <= endY && !next; py++) {
				if (px != curX || py != curY) {
					wei = ((abs(px - curX) + abs(py - curY)));

					int32 curPNode = px + py * _width;
					if (isWalkable(px, py)) { // walkable ?
						int32 sum = _sq[curNode] + wei * (1 + (isLikelyWalkable(px, py) ? 5 : 0));
						if (_sq[curPNode] > sum || !_sq[curPNode]) {
							int32 newWeight = abs(destx - px) + abs(desty - py);
							_sq[curPNode] = sum;
							_heap->push(px, py, _sq[curPNode] + newWeight);
							if (!newWeight)
								next = true; // we found it !
						}
					}
				}
			}
		}
	}

	// let's see if we found a result !
	if (!_sq[destx + desty * _width]) {
		// didn't find anything
		_tempPath.clear();
		return false;
	}

	curX = destx;
	curY = desty;

	Common::Array<i32Point> retPath;

	i32Point p;
	p.x = curX;
	p.y = curY;
	retPath.push_back(p);

	int32 bestscore = _sq[destx + desty * _width];

	bool retVal = false;
	while (true) {
		int16 bestX = -1;
		int16 bestY = -1;

		int16 endX = MIN<int16>(curX + 1, _width - 1);
		int16 endY = MIN<int16>(curY + 1, _height - 1);
		int16 startX = MAX<int16>(curX - 1, 0);
		int16 startY = MAX<int16>(curY - 1, 0);

		for (int16 px = startX; px <= endX; px++) {
			for (int16 py = startY; py <= endY; py++) {
				if (px != curX || py != curY) {
					wei = abs(px - curX) + abs(py - curY);

					int32 PNode = px + py * _width;
					if (_sq[PNode] && (isWalkable(px, py))) {
						if (_sq[PNode] < bestscore) {
							bestscore = _sq[PNode];
							bestX = px;
							bestY = py;
						}
					}
				}
			}
		}

		if (bestX < 0 || bestY < 0)
			break;

		i32Point pp;
		pp.x = bestX;
		pp.y = bestY;
		retPath.push_back(pp);

		if ((bestX == x && bestY == y)) {
			_tempPath.clear();
			for (uint32 i = 0; i < retPath.size(); i++)
				_tempPath.push_back(retPath[i]);

			retVal = true;
			break;
		}

		curX = bestX;
		curY = bestY;
	}

	return retVal;
}

void PathFinding::addBlockingRect(int16 x1, int16 y1, int16 x2, int16 y2) {
	debugC(1, kDebugPath, "addBlockingRect(%d, %d, %d, %d)", x1, y1, x2, y2);
	if (_numBlockingRects >= kMaxBlockingRects) {
		warning("Maximum number of %d Blocking Rects reached!", kMaxBlockingRects);
		return;
	}

	_blockingRects[_numBlockingRects][0] = x1;
	_blockingRects[_numBlockingRects][1] = y1;
	_blockingRects[_numBlockingRects][2] = x2;
	_blockingRects[_numBlockingRects][3] = y2;
	_blockingRects[_numBlockingRects][4] = 0;
	_numBlockingRects++;
}

void PathFinding::addBlockingEllipse(int16 x1, int16 y1, int16 w, int16 h) {
	debugC(1, kDebugPath, "addBlockingEllipse(%d, %d, %d, %d)", x1, y1, w, h);
	if (_numBlockingRects >= kMaxBlockingRects) {
		warning("Maximum number of %d Blocking Rects reached!", kMaxBlockingRects);
		return;
	}

	_blockingRects[_numBlockingRects][0] = x1;
	_blockingRects[_numBlockingRects][1] = y1;
	_blockingRects[_numBlockingRects][2] = w;
	_blockingRects[_numBlockingRects][3] = h;
	_blockingRects[_numBlockingRects][4] = 1;
	_numBlockingRects++;
}

} // End of namespace Toon