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
|
/* 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/scummsys.h"
#include "mads/mads.h"
#include "mads/rails.h"
namespace MADS {
WalkNode::WalkNode() {
_active = false;
Common::fill(&_distances[0], &_distances[MAX_ROUTE_NODES], 0);
}
void WalkNode::load(Common::SeekableReadStream *f) {
_walkPos.x = f->readSint16LE();
_walkPos.y = f->readSint16LE();
for (int i = 0; i < MAX_ROUTE_NODES; ++i)
_distances[i] = f->readUint16LE();
}
/*------------------------------------------------------------------------*/
Rails::Rails() {
_depthSurface = nullptr;
_routeLength = 0;
}
void Rails::load(const WalkNodeList &nodes, DepthSurface *depthSurface, int depthStyle) {
// Store the depth surface and depth style to use
_depthSurface = depthSurface;
_depthStyle = depthStyle;
// Load the passed node list
_nodes.clear();
for (uint i = 0; i < nodes.size(); ++i)
_nodes.push_back(nodes[i]);
// Add two more empty nodes for the start and end points of any walk sequence
_nodes.push_back(WalkNode());
_nodes.push_back(WalkNode());
}
void Rails::setupRoute(bool bitFlag, const Common::Point &srcPos, const Common::Point &destPos) {
// Reset the nodes in as being inactive
for (uint i = 0; i < _nodes.size(); ++i)
_nodes[i]._active = false;
// Set the two extra walk nodes to the start and destination positions
setNodePosition(_nodes.size() - 2, srcPos);
setNodePosition(_nodes.size() - 1, destPos);
// Start constructing route node list
_routeLength = 0x3FFF;
_routeIndexes.clear();
// Recursively form a route from the destination walk node back to the player's position
setupRouteNode(&_tempRoute[0], _nodes.size() - 1, bitFlag ? 0xC000 : 0x8000, 0);
_next = 0;
if (_routeIndexes.size() > 0) {
Common::Point currPos = srcPos;
for (int routeCtr = size() - 1; (routeCtr >= 0) && (_next == 0); --routeCtr) {
int idx = _routeIndexes[routeCtr];
const Common::Point &pt = _nodes[idx]._walkPos;
_next = scanPath(currPos, pt);
currPos = pt;
}
}
}
void Rails::setupRouteNode(int *routeIndexP, int nodeIndex, int flags, int routeLength) {
WalkNode ¤tNode = _nodes[nodeIndex];
currentNode._active = true;
*routeIndexP++ = nodeIndex;
// Get the index of the ultimate source position (the player)
int subIndex = _nodes.size() - 2;
int distanceVal = _nodes[nodeIndex]._distances[subIndex];
if (distanceVal & flags) {
routeLength += distanceVal & 0x3FFF;
if (routeLength < _routeLength) {
// Found a new shorter route to destination, so set up the route with the found one
_routeIndexes.clear();
for (int i = 0; routeIndexP != &_tempRoute[i]; ++i)
_routeIndexes.push(_tempRoute[i]);
_routeLength = routeLength;
}
} else {
for (int idx = _nodes.size() - 2; idx > 0; --idx) {
int nodePos = idx - 1;
if (!_nodes[nodePos]._active && ((currentNode._distances[nodePos] & flags) != 0))
setupRouteNode(routeIndexP, nodePos, 0x8000, routeLength + (distanceVal & 0x3fff));
}
}
currentNode._active = false;
}
int Rails::scanPath(const Common::Point &srcPos, const Common::Point &destPos) {
// For compressed depth surfaces, always return 0
if (_depthStyle != 2)
return 0;
int yDiff = destPos.y - srcPos.y;
int yAmount = MADS_SCREEN_WIDTH;
if (yDiff < 0) {
yDiff = -yDiff;
yAmount = -yAmount;
}
int xDiff = destPos.x - srcPos.y;
int xDirection = 1;
int xAmount = 0;
if (xDiff < 0) {
xDiff = -xDiff;
xDirection = -xDirection;
xAmount = MIN(yDiff, xDiff);
}
++xDiff;
++yDiff;
const byte *srcP = _depthSurface->getBasePtr(srcPos.x, srcPos.y);
int index = xAmount;
// Outer horizontal movement loop
for (int yIndex = 0; yIndex < yDiff; ++yIndex) {
index += yDiff;
int v = (*srcP & 0x7F) >> 4;
if (v)
return v;
// Inner loop for handling vertical movement
while (index >= xDiff) {
index -= xDiff;
v = (*srcP & 0x7F) >> 4;
if (v)
return v;
srcP += yAmount;
}
srcP += xDirection;
}
return 0;
}
void Rails::resetRoute() {
_routeIndexes.clear();
_next = 0;
}
const WalkNode &Rails::popNode() {
assert(!_routeIndexes.empty());
return _nodes[_routeIndexes.pop()];
}
void Rails::setNodePosition(int nodeIndex, const Common::Point &pt) {
int flags, hypotenuse;
_nodes[nodeIndex]._walkPos = pt;
// Recalculate inter-node lengths
for (uint idx = 0; idx < _nodes.size(); ++idx) {
int entry;
if (idx == (uint)nodeIndex) {
entry = 0x3FFF;
}
else {
// Process the node
flags = getRouteFlags(pt, _nodes[idx]._walkPos);
int xDiff = ABS(_nodes[idx]._walkPos.x - pt.x);
int yDiff = ABS(_nodes[idx]._walkPos.y - pt.y);
hypotenuse = sqrt((double)(xDiff * xDiff + yDiff * yDiff));
if (hypotenuse >= 0x3FFF)
// Shouldn't ever be this large
hypotenuse = 0x3FFF;
entry = hypotenuse | flags;
}
_nodes[idx]._distances[nodeIndex] = entry;
_nodes[nodeIndex]._distances[idx] = entry;
}
}
int Rails::getRouteFlags(const Common::Point &src, const Common::Point &dest) {
int result = 0x8000;
bool flag = false;
int xDiff = ABS(dest.x - src.x);
int yDiff = ABS(dest.y - src.y);
int xDirection = dest.x >= src.x ? 1 : -1;
int yDirection = dest.y >= src.y ? _depthSurface->w : -_depthSurface->w;
int minorDiff = 0;
if (dest.x < src.x)
minorDiff = MIN(xDiff, yDiff);
++xDiff;
++yDiff;
byte *srcP = _depthSurface->getBasePtr(src.x, src.y);
int totalCtr = minorDiff;
for (int xCtr = 0; xCtr < xDiff; ++xCtr, srcP += xDirection) {
totalCtr += yDiff;
if ((*srcP & 0x80) == 0)
flag = false;
else if (!flag) {
flag = true;
result -= 0x4000;
if (result == 0)
break;
}
while (totalCtr >= xDiff) {
totalCtr -= xDiff;
if ((*srcP & 0x80) == 0)
flag = false;
else if (!flag) {
flag = true;
result -= 0x4000;
if (result == 0)
break;
}
srcP += yDirection;
}
if (result == 0)
break;
}
return result;
}
} // End of namespace MADS
|