aboutsummaryrefslogtreecommitdiff
path: root/engines/m4/rails.cpp
blob: f51d81c8f406c08dc09ddcb1746c3b37f0151dbc (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
/* 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.
 *
 */

/*
	TODO:
		- rewrite functions (GetShortestPath etc.)
*/

#include "graphics/primitives.h"
#include "common/list.h"
#include "common/rect.h"
#include "common/util.h"

#include "m4/rails.h"
#include "m4/m4.h"

namespace M4 {

#define TOP_EDGE	1 << 0
#define LEFT_EDGE	1 << 1
#define BOTTOM_EDGE	1 << 2
#define RIGHT_EDGE	1 << 3


Rails::Rails() {
}


Rails::~Rails() {
	clearRails();
}


void Rails::clearRails() {
	uint32 i;
	Common::List<NoWalkRect *>::iterator j;
	RailNode *tempNode;

	for (i = 0; i < _nodes.size(); i++) {
		tempNode = _nodes[i];
		_nodes.remove_at(i);
		delete tempNode;
	}

	_edges.clear();

	for (j = _noWalkRects.begin(); j != _noWalkRects.end(); ++j)
		delete (*j);
	_noWalkRects.clear();
}

static void checkPoint(int x, int y, int color, void *data) {
	IsWalkableData *isWalkableData = (IsWalkableData*)data;
	if (!isWalkableData->result)
		return;
	else {
	   M4Surface *codes = isWalkableData->codes;
	   if (x >= 0 && x < codes->width() && y >= 0 && y < codes->height()) {
			isWalkableData->result = !((*((uint8*)codes->getBasePtr(x, y))) & 0x10);
		} else {
			isWalkableData->result = false;
		}
	}
}

bool Rails::isLineWalkable(int x0, int y0, int x1, int y1) {
	IsWalkableData isWalkableData;
	isWalkableData.codes = _walkCodes;
	isWalkableData.result = true;
	Graphics::drawLine(x0, y0, x1, y1, 0, &checkPoint, &isWalkableData);
	return isWalkableData.result;
}

uint8 Rails::getDepth(const Common::Point &pt) {
	// TODO: Check based on sceneResources
	const byte *b = _walkCodes->getBasePtr(pt.x, pt.y);
	return *b & 0xf;
}

// helper function
uint8 getEndCode(int32 x, int32 y, Common::Rect rect) {
	uint8 endCode = 0;
	endCode = (x < rect.left) ? LEFT_EDGE : endCode;
	endCode = (x > rect.right) ? RIGHT_EDGE : endCode;
	endCode = (y < rect.top) ? endCode | TOP_EDGE : endCode;
	endCode = (y > rect.bottom) ? endCode | BOTTOM_EDGE : endCode;
	return endCode;
}

bool Rails::lineCrossesRect(int32 x1, int32 y1, int32 x2, int32 y2, Common::Rect rect) {
	int32 mX, mY;
	int32 pX1 = x1, pX2 = x2, pY1 = y1, pY2 = y2;
	uint8 endCode1, endCode2, midCode;

	if (rect.left > rect.right || rect.top > rect.bottom)
		return false;

	// Cohen-Sutherland line clipping algorithm

	endCode1 = getEndCode(pX1, pY1, rect);
	endCode2 = getEndCode(pX2, pY2, rect);

	if (!endCode1 || !endCode2)	// if both endcodes are zero
		return true;	// point is inside the rectangle, therefore the line intersects

	while (true) {
		if (endCode1 & endCode2)	// if both endcodes have a common bitset
			return false;	// line is completely off one edge

		// calculate midpoint
		mX = (pX1 + pX2)>>1;
		mY = (pY1 + pY2)>>1;

		// avoid round-off error: make sure that the midpoint isn't the same as one of the
		// two endpoints
		if (((mX == pX1) && (mY == pY1)) || ((mX == pX2) && (mY == pY2)))
			return false;

		midCode = getEndCode(mX, mY, rect);

		if (!midCode) {
			return true;
		} else if (midCode & endCode1) {
			// the midCode and an end point form a line segment completely off one edge, so
			// remove that half of the line segment
			pX1 = mX;
			pY1 = mY;
			endCode1 = midCode;
		} else {
			pX2 = mX;
			pY2 = mY;
			endCode2 = midCode;
		}
	}
}


bool Rails::linePassesThroughRect(int32 x1, int32 y1, int32 x2, int32 y2) {
	if (_noWalkRects.empty())
		return false;

	bool intersected = false;
	Common::List<NoWalkRect *>::iterator i;

	for (i = _noWalkRects.begin(); i != _noWalkRects.end(); ++i) {
		intersected = lineCrossesRect(x1, y1, x2, y2, Common::Rect((*i)->x1, (*i)->y1, (*i)->x2, (*i)->y2));
		if (intersected)
			break;
	}

	return intersected;
}

long SqrtF16(long n) {
	uint32 r = 0, s;
	uint32 v = (uint32)n;

	for (int i = 15; i >= 0; --i) {
		s = r + (1L << i * 2);
		r >>= 1;
		if (s <= v) {
			v -= s;
			r |= (1L << i * 2);
		}
	}

	return (long)r;
}

void Rails::createEdge(int32 node1, int32 node2) {
	uint32		index;
	int32		x1, y1, x2, y2;
	bool		valid;
	long		deltaX, deltaY, distance;

	if ((node1 < 0) || (node1 >= MAXRAILNODES) || (node2 < 0) || (node2 >= MAXRAILNODES))
		return;

	if (node1 == node2)
		return;

	if (node2 < node1)
		SWAP(node1, node2);		// ensure node1 < node2

	// Find the table entry i.e. tableWidth * node1 + node2 and then subtract
	// n(n+1)/2, since only the upper triangle of the table is stored
	index = (MAXRAILNODES-1) * node1 + node2 - 1 - (node1*(node1+1)>>1);
	if (index > _edges.size() - 1)
		_edges.resize(index + 1);
	_edges.insert_at(index, 0);
	valid = true;

	if (_nodes.size() <= (uint32)node1 || _nodes.size() <= (uint32)node2)
		return;

	x1 = _nodes[node1]->x;
	y1 = _nodes[node1]->y;
	x2 = _nodes[node2]->x;
	y2 = _nodes[node2]->y;

	// Make sure that the algorithm is symmetric
	if (x2 < x1) {
		SWAP(x1, x2);
		SWAP(y1, y2);
	}

	valid = isLineWalkable(_nodes[node1]->x, _nodes[node1]->y,
		_nodes[node2]->x, _nodes[node2]->y);
	debugCN(kDebugCore, "test code says: %d\n", valid);

	// Check if the line passes through a forbidden rectangle
	if (valid) {
		if (linePassesThroughRect(x1, y1, x2, y2)) {
			valid = false;
		}
	}

	if (valid) {
		deltaX = ABS(((long)(x2 - x1)) << 16);
		deltaY = ABS(((long)(y2 - y1)) << 16);
		if ((deltaX >= 0x800000) || (deltaY >= 0x800000)) {
			deltaX >>= 16;
			deltaY >>= 16;
			distance = (long)(SqrtF16(deltaX * deltaX + deltaY * deltaY) << 16);
		} else {
			distance = SqrtF16(FixedMul(deltaX, deltaX) + FixedMul(deltaY, deltaY)) << 8;
		}
		_edges.insert_at(index, distance >> 16);
	}

	debugCN(kDebugCore, "node1 = %d, node2 = %d, valid = %d\n", node1, node2, valid);

}


void Rails::restoreNodeEdges(int32 nodeID) {
	for (int32 i = 0; i < MAXRAILNODES; i++) {
		createEdge(i, nodeID);
	}
}

void Rails::restoreEdgeList() {
	int32 j;
	for (int32 i = 0; i < MAXRAILNODES; i++) {
		for (j = i + 1; j < MAXRAILNODES; j++) {
			createEdge(i, j);
		}
	}
}

int32 Rails::addRailNode(int32 x, int32 y, bool restoreEdges) {
	uint32 i = _nodes.size();
	if (i >= MAXRAILNODES)
		return -1;

	RailNode *newNode = new RailNode();
	newNode->nodeID = i;
	newNode->x = x;
	newNode->y = y;
	_nodes.insert_at(i, newNode);
	if (restoreEdges) {
		for (uint32 j=0; j<_nodes.size(); j++)
			createEdge(i, j);
	}
	return i;
}

bool Rails::removeRailNode(int32 nodeID, bool restoreEdges) {
	if (nodeID < 0 || nodeID >= MAXRAILNODES)
		return false;

	if (_nodes.empty() || _edges.empty())
		return false;

	RailNode *tempNode = _nodes[nodeID];
	_nodes.remove_at(nodeID);
	delete tempNode;

	if (restoreEdges) {
		restoreNodeEdges(nodeID);
	}
	return true;
}

int16 Rails::getEdgeLength(int32 node1, int32 node2) {
	int32		index;
	if (_edges.empty() || node1 == node2)
		return 0;
	if (node2 < node1)
		SWAP(node1, node2);
	// Find the table entry i.e. tableWidth * node1 + node2 and then subtract
	// n(n+1)/2, since only the upper triangle of the table is stored
	index = (MAXRAILNODES-1)*node1 + node2 - 1 - (node1*(node1+1)>>1);
	return _edges[index];
}

void Rails::disposePath(RailNode *pathStart) {
	RailNode *tempNode = pathStart;
	while (tempNode) {
		pathStart = pathStart->shortPath;
		delete tempNode;
		tempNode = pathStart;
	}
}

/*
static RailNode* duplicatePath(RailNode *pathStart) {
	RailNode *newNode = NULL;
	RailNode *firstNode = NULL;
	RailNode *prevNode = NULL;
	// A valid path is assumed
	RailNode *pathNode = pathStart;

	while (pathNode) {
		newNode = new RailNode();
		newNode->x = pathNode->x;
		newNode->y = pathNode->y;
		newNode->shortPath = NULL;

		if (!firstNode)
			firstNode = newNode;
		else
			prevNode->shortPath = newNode;

		prevNode = newNode;
		// Get the next node
		pathNode = pathNode->shortPath;
	}

	return firstNode;
}
*/

bool Rails::getShortestPath(int32 origID, int32 destID, RailNode **shortPath) {
	// TODO
	return true;
}

} // End of namespace M4