aboutsummaryrefslogtreecommitdiff
path: root/engines/toon/path.h
blob: 59f74ef286f5459a3eff6423be1af524ca4dc1d1 (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
/* 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.
*
*/

#ifndef TOON_PATH_H
#define TOON_PATH_H

#include "common/array.h"
#include "common/rect.h"

#include "toon/toon.h"

namespace Toon {

// binary heap system for fast A*
class PathFindingHeap {
public:
	PathFindingHeap();
	~PathFindingHeap();

	void push(int16 x, int16 y, uint16 weight);
	void pop(int16 *x, int16 *y, uint16 *weight);
	void init(int32 size);
	void clear();
	void unload();
	uint32 getCount() { return _count; }

private:
	struct HeapDataGrid {
		int16 _x, _y;
		uint16 _weight;
	};

	HeapDataGrid *_data;

	uint32 _size;
	uint32 _count;
};

class PathFinding {
public:
	PathFinding();
	~PathFinding();

	void init(Picture *mask);

	bool findPath(int16 x, int16 y, int16 destX, int16 destY);
	bool findClosestWalkingPoint(int16 xx, int16 yy, int16 *fxx, int16 *fyy, int16 origX = -1, int16 origY = -1);
	bool isWalkable(int16 x, int16 y);
	bool isLikelyWalkable(int16 x, int16 y);
	bool lineIsWalkable(int16 x, int16 y, int16 x2, int16 y2);
	void walkLine(int16 x, int16 y, int16 x2, int16 y2);

	void resetBlockingRects() { _numBlockingRects = 0; }
	void addBlockingRect(int16 x1, int16 y1, int16 x2, int16 y2);
	void addBlockingEllipse(int16 x1, int16 y1, int16 w, int16 h);

	uint32 getPathNodeCount() const { return _tempPath.size(); }
	int16 getPathNodeX(uint32 nodeId) const { return _tempPath[(_tempPath.size() - 1) - nodeId].x; }
	int16 getPathNodeY(uint32 nodeId) const { return _tempPath[(_tempPath.size() - 1) - nodeId].y; }

private:
	static const uint8 kMaxBlockingRects = 16;

	Picture *_currentMask;

	PathFindingHeap *_heap;

	uint16 *_sq;
	int16 _width;
	int16 _height;

	Common::Array<Common::Point> _tempPath;

	int16 _blockingRects[kMaxBlockingRects][5];
	uint8 _numBlockingRects;
};

} // End of namespace Toon

#endif