aboutsummaryrefslogtreecommitdiff
path: root/engines/illusions/pathfinder.cpp
blob: 777cb4d4bde5dd27c65a18e3dd520f77c21a9fe0 (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
/* 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 "illusions/illusions.h"
#include "illusions/pathfinder.h"
#include "camera.h"

namespace Illusions {

PointArray *PathFinder::findPath(Camera *camera, Common::Point sourcePt, Common::Point destPt,
	PointArray *walkPoints, PathLines *walkRects, WidthHeight bgDimensions) {
	Common::Point cameraPt = camera->getScreenOffset();
	_screenRect.p0 = cameraPt;
	_screenRect.p1.x = cameraPt.x + 320; //TODO fix me get screen dimensions here.
	_screenRect.p1.y = cameraPt.y + 200;
	_walkPoints = walkPoints;
	_walkRects = walkRects;
	_bgDimensions = bgDimensions;
	return findPathInternal(sourcePt, destPt);
}

PointArray *PathFinder::findPathInternal(Common::Point sourcePt, Common::Point destPt) {
	PathLine line;
	PointArray *foundPath = new PointArray();
	line.p0 = sourcePt;
	line.p1 = destPt;

	if (_walkRects && _walkPoints && isLineBlocked(line)) {
		Common::Point nextStartPt = sourcePt, outPt;

		if (!findValidDestLine(destPt)) {
			findValidDestPt(destPt);
			line.p1 = destPt;
		}

		_pathBytes = (byte*)calloc(1, _walkPoints->size());

		bool done = false;
		while (!done) {
			line.p0 = nextStartPt;
			if (!isLineBlocked(line)) {
				foundPath->push_back(destPt);
				done = true;
			} else {
				if (foundPath->size() < _walkPoints->size() + 2 && findClosestPt(nextStartPt, outPt, destPt)) {
					foundPath->push_back(outPt);
					nextStartPt = outPt;
				} else {
					if (foundPath->size() == 0)
						foundPath->push_back(sourcePt);
					done = true;
				}
			}
		}

		free(_pathBytes);
		postProcess(sourcePt, foundPath);

	} else {
		foundPath->push_back(destPt);
	}
	return foundPath;
}

void PathFinder::postProcess(Common::Point sourcePt, PointArray *foundPath) {
	// For each three points A, B and C, removes B if the line between A and C is not blocked
	for (uint index = 0; index + 2 < foundPath->size(); ++index) {
		PathLine line;
		line.p0 = index == 0 ? sourcePt : (*foundPath)[index - 1];
		line.p1 = (*foundPath)[index + 1];
		if (!isLineBlocked(line)) {
			debug("remove point");
			foundPath->remove_at(index);
		}
	}
}

bool PathFinder::isLineBlocked(PathLine &line) {
	for (uint i = 0; i < _walkRects->size(); ++i) {
		if (calcLineStatus(line, (*_walkRects)[i], 0) != 3)
			return true;
	}
	return false;
}

int PathFinder::calcLineDistance(PathLine &line) {
	int16 deltaX = line.p0.x - line.p1.x;
	int16 deltaY = line.p0.y - line.p1.y;
	if (deltaX != 0 || deltaY != 0)
		return sqrt(deltaX * deltaX + deltaY * deltaY);
	return 0;
}

bool PathFinder::findClosestPt(Common::Point &sourcePt, Common::Point &closestPt, Common::Point &destPt) {
	PathLine sourceLine, destLine;
	uint minIndex = 0;
	int minDistance = 0xFFFF;
	sourceLine.p0 = sourcePt;
	destLine.p1 = destPt;
	for (uint i = 0; i < _walkPoints->size(); ++i) {
		sourceLine.p1 = (*_walkPoints)[i];
		destLine.p0 = (*_walkPoints)[i];
		if (!_pathBytes[i] && !isLineBlocked(sourceLine)) {
			int currDistance = calcLineDistance(destLine);
			if (currDistance <= minDistance) {
				minDistance = currDistance;
				minIndex = i + 1;
			}
		}
	}
	if (minIndex) {
		closestPt = (*_walkPoints)[minIndex - 1];
		_pathBytes[minIndex - 1] = 1;
		return true;
	}
	return false;
}

bool PathFinder::findValidDestLine(Common::Point &destPt) {
	PathLine destLine;
	destLine.p0 = destPt;
	for (uint i = 0; i < _walkPoints->size(); ++i) {
		destLine.p1 = (*_walkPoints)[i];
		if (!isLineBlocked(destLine))
			return true;
	}
	return false;
}

void PathFinder::findValidDestPt(Common::Point &destPt) {
	Common::Point minPt, outPt, deltaPt;
	int minDistance = 0xFFFF, currDistance;
	PathLine destLine;
	for (uint i = 0; i < _walkRects->size(); ++i) {
		PathLine currRect = (*_walkRects)[i];
		//TODO fix this hack. Used here to get xmas tree scene to work.
		if (currRect.p1.x > _screenRect.p1.x) {
			currRect.p1.x = _screenRect.p1.x;
		}
		if (currRect.p0.x < _screenRect.p0.x) {
			currRect.p0.x = _screenRect.p0.x;
		}
		WidthHeight rectDimensions = calcRectDimensions(currRect);

		adjustRectDimensions(rectDimensions);
		clipLineToBg(destPt, rectDimensions, destLine);
		if (calcLineStatus(destLine, currRect, &outPt) == 3) {
			destLine.p0 = destPt;
			destLine.p1 = currRect.p0;
			currDistance = calcLineDistance(destLine);
			if (currDistance < minDistance) {
				minDistance = currDistance;
				minPt = currRect.p0;
			}
			destLine.p0 = destPt;
			destLine.p1 = currRect.p1;
			currDistance = calcLineDistance(destLine);
			if (currDistance < minDistance) {
				minDistance = currDistance;
				minPt = currRect.p1;
			}
		} else {
			destLine.p0 = destPt;
			destLine.p1 = outPt;
			currDistance = calcLineDistance(destLine);
			if (currDistance < minDistance) {
				minDistance = currDistance;
				minPt = outPt;
			}
		}
	}
	findDeltaPt(minPt, deltaPt);
	destPt.x = deltaPt.x + minPt.x;
	destPt.y = deltaPt.y + minPt.y;
}

WidthHeight PathFinder::calcRectDimensions(PathLine &rect) {
	WidthHeight dimensions;
	dimensions._width = rect.p1.x - rect.p0.x;
	dimensions._height = rect.p1.y - rect.p0.y;
	swapDimensions(dimensions);
	return dimensions;
}

void PathFinder::adjustRectDimensions(WidthHeight &dimensions) {
	dimensions._width = ABS(dimensions._height) * (dimensions._width < 0 ? -1 : 1);
	dimensions._height = ABS(dimensions._width) * (dimensions._height < 0 ? -1 : 1);
	if (dimensions._width)
		dimensions._width = -dimensions._width;
	else
		dimensions._height = -dimensions._height;
	swapDimensions(dimensions);
}

void PathFinder::swapDimensions(WidthHeight &dimensions) {
	if (dimensions._width < 0) {
		dimensions._width = -dimensions._width;
		dimensions._height = -dimensions._height;
	} else if (dimensions._width == 0)
		dimensions._height = abs(dimensions._height);
	else if (dimensions._height == 0)
		dimensions._width = abs(dimensions._width);
}

void PathFinder::clipLineToBg(Common::Point &destPt, WidthHeight &rectDimensions, PathLine &outDestLine) {
	if (rectDimensions._height == 0) {
		outDestLine.p0.x = 0;
		outDestLine.p0.y = destPt.y;
		outDestLine.p1.x = _bgDimensions._width;
		outDestLine.p1.y = destPt.y;
	} else if (rectDimensions._width == 0) {
		outDestLine.p0.y = 0;
		outDestLine.p0.x = destPt.x;
		outDestLine.p1.x = destPt.x;
		outDestLine.p1.y = _bgDimensions._height;
	} else {
		outDestLine.p0 = destPt;
		outDestLine.p1.x = destPt.x + rectDimensions._width;
		outDestLine.p1.y = destPt.y + rectDimensions._height;
		int16 y1 = destPt.y + (rectDimensions._height * -destPt.x / rectDimensions._width);
		int16 y2 = destPt.y + (rectDimensions._height * (_bgDimensions._width - destPt.x) / rectDimensions._width);
		int16 x1 = destPt.x + (rectDimensions._width * -destPt.y / rectDimensions._height);
		int16 x2 = destPt.x + (rectDimensions._width * (_bgDimensions._height - destPt.y) / rectDimensions._height);
		if (ABS(rectDimensions._height) <= ABS(rectDimensions._width)) {
			outDestLine.p0.y = 0;
			outDestLine.p0.x = _bgDimensions._width;
			if (x1 < 0 || _bgDimensions._width < x1)
				outDestLine.p0.y = y2;
			else
				outDestLine.p0.x = x1;
			outDestLine.p1.x = 0;
			outDestLine.p1.y = _bgDimensions._height;
			if (x2 < 0 || _bgDimensions._width < x2)
				outDestLine.p1.y = y1;
			else
				outDestLine.p1.x = x2;
		} else {
			outDestLine.p0.y = 0;
			outDestLine.p0.x = 0;
			if (x1 < 0 || _bgDimensions._width < x1)
				outDestLine.p0.y = y1;
			else
				outDestLine.p0.x = x1;
			outDestLine.p1.x = _bgDimensions._width;
			outDestLine.p1.y = _bgDimensions._height;
			if (x2 < 0 || _bgDimensions._width < x2)
				outDestLine.p1.y = y2;
			else
				outDestLine.p1.x = x2;
		}
	}
}

void PathFinder::findDeltaPt(Common::Point pt, Common::Point &outDeltaPt) {
	static const struct { int16 x, y; } kDeltaPoints[] = {
		{ 0, -4}, {0, 4}, {-4, 0}, { 4,  0}, {-3, -3}, {3, 3}, {-3, 3}, { 3, -3},
		{-2, -4}, {2, 4}, {-2, 4}, { 2, -4}, {-4, -2}, {4, 2}, {-4, 2}, { 4, -2},
		{-1, -4}, {1, 4}, {-1, 4}, { 1, -4}, {-4, -1}, {4, 1}, {-4, 1}, { 4, -1},
		{-2, -3}, {2, 3}, {-2, 3}, { 2, -3}, {-3, -2}, {3, 2}, {-3, 2}, { 3, -2}
	};
	Common::Point testPt;
	for (uint i = 0; i < 32; ++i) {
		testPt.x = pt.x + kDeltaPoints[i].x;
		testPt.y = pt.y + kDeltaPoints[i].y;
		if (findValidDestLine(testPt)) {
			outDeltaPt.x = kDeltaPoints[i].x;
			outDeltaPt.y = kDeltaPoints[i].y;
			break;
		}
	}
}
/**
 * returns true if line is contained within rect.
 */
bool PathFinder::isLineWithinRectangle(PathLine &line, PathLine &rect) {
	return line.p0.x <= rect.p1.x && line.p1.x >= rect.p0.x &&
		line.p0.y <= rect.p1.y && line.p1.y >= rect.p0.y;
}

/**
 * flip line coordinates so it starts top left and finishes bottom right
 */
void PathFinder::swapLine(PathLine &line, PathLine &outLine) {
	if (line.p1.x <= line.p0.x) {
		outLine.p1.x = line.p0.x;
		outLine.p0.x = line.p1.x;
	} else {
		outLine.p0.x = line.p0.x;
		outLine.p1.x = line.p1.x;
	}
	if (line.p1.y <= line.p0.y) {
		outLine.p1.y = line.p0.y;
		outLine.p0.y = line.p1.y;
	} else {
		outLine.p0.y = line.p0.y;
		outLine.p1.y = line.p1.y;
	}
}

int PathFinder::calcLineStatus(PathLine &sourceLine, PathLine &destRect, Common::Point *outPoint) {
	PathLine sourceLine1, destRect1;
	swapLine(sourceLine, sourceLine1);
	swapLine(destRect, destRect1);

	if (!isLineWithinRectangle(sourceLine1, destRect1))
		return 3;

	int sourceDeltaX = sourceLine.p1.x - sourceLine.p0.x;
	int sourceDeltaY = sourceLine.p1.y - sourceLine.p0.y;
	int destDeltaX = destRect.p0.x - destRect.p1.x;
	int destDeltaY = destRect.p0.y - destRect.p1.y;
	int sdDeltaX = sourceLine.p0.x - destRect.p0.x;
	int sdDeltaY = sourceLine.p0.y - destRect.p0.y;
	int delta1 = destDeltaY * sdDeltaX - destDeltaX * sdDeltaY;
	int delta2 = sourceDeltaY * destDeltaX - sourceDeltaX * destDeltaY;
	int delta3 = sourceDeltaX * sdDeltaY - sourceDeltaY * sdDeltaX;

	if ((delta2 <= 0 && (delta1 > 0 || delta2 > delta1)) ||
		(delta2 > 0 && (delta1 < 0 || delta2 < delta1)) ||
		(delta2 <= 0 && (delta3 > 0 || delta2 > delta3)) ||
		(delta2 > 0 && (delta3 < 0 || delta2 < delta3)))
		return 3;

	if (!outPoint)
		return 1;

	if (delta2 == 0)
		return 2;

	int v15 = sourceDeltaX * delta1, v18 = sourceDeltaY * delta1;
	int v16 = 0;
	int v17 = 0;

	if ((v15 >= 0 && delta2 >= 0) || (v15 < 0 && delta2 < 0)) {
		v16 = delta2 / 2;
		v17 = delta2 / 2;
	} else if ((v15 < 0 && delta2 >= 0) || (v15 >= 0 && delta2 < 0)) {
		v17 = delta2 / 2;
		v16 = delta2 / -2;
	}

	outPoint->x = sourceLine.p0.x + (v15 + v16) / delta2;

	if ((v18 >= 0 && delta2 < 0) || (v18 < 0 && delta2 >= 0))
		v17 = -v17;

	outPoint->y = sourceLine.p0.y + (v18 + v17) / delta2;

	return 1;
}

} // End of namespace Illusions