~ubuntu-branches/debian/jessie/scummvm/jessie

« back to all changes in this revision

Viewing changes to engines/mads/rails.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2014-08-10 00:50:36 UTC
  • mfrom: (1.2.22)
  • Revision ID: package-import@ubuntu.com-20140810005036-wls6i0dsxqfxu70g
Tags: 1.7.0+dfsg-1
* New upstream release [July 2014].
  - remove old/obsolete patches.
  + added new "drop1test.patch" to disable problematic test.
  + build with "--disable-eventrecorder" to avoid FTBFS in tests.
  + added "libjpeg-dev" and "libfaad-dev" to Build-Depends.
* Install all arch-independent files (themes, game data, etc.).
* Build-time re-compression of "classic" theme.
* Added "debian/gbp.conf".
* Standards-Version to 3.9.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ScummVM - Graphic Adventure Engine
 
2
 *
 
3
 * ScummVM is the legal property of its developers, whose names
 
4
 * are too numerous to list here. Please refer to the COPYRIGHT
 
5
 * file distributed with this source distribution.
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU General Public License
 
9
 * as published by the Free Software Foundation; either version 2
 
10
 * of the License, or (at your option) any later version.
 
11
 
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
20
 *
 
21
 */
 
22
 
 
23
#include "common/scummsys.h"
 
24
#include "mads/mads.h"
 
25
#include "mads/rails.h"
 
26
 
 
27
namespace MADS {
 
28
 
 
29
WalkNode::WalkNode() {
 
30
        _active = false;
 
31
        Common::fill(&_distances[0], &_distances[MAX_ROUTE_NODES], 0);
 
32
}
 
33
 
 
34
void WalkNode::load(Common::SeekableReadStream *f) {
 
35
        _walkPos.x = f->readSint16LE();
 
36
        _walkPos.y = f->readSint16LE();
 
37
        for (int i = 0; i < MAX_ROUTE_NODES; ++i)
 
38
                _distances[i] = f->readUint16LE();
 
39
}
 
40
 
 
41
/*------------------------------------------------------------------------*/
 
42
 
 
43
Rails::Rails() {
 
44
        _depthSurface = nullptr;
 
45
        _routeLength = 0;
 
46
        _depthStyle = 0;
 
47
        _next = 0;
 
48
}
 
49
 
 
50
void Rails::load(const WalkNodeList &nodes, DepthSurface *depthSurface, int depthStyle) {
 
51
        // Store the depth surface and depth style to use
 
52
        _depthSurface = depthSurface;
 
53
        _depthStyle = depthStyle;
 
54
 
 
55
        // Load the passed node list
 
56
        _nodes.clear();
 
57
 
 
58
        for (uint i = 0; i < nodes.size(); ++i)
 
59
                _nodes.push_back(nodes[i]);
 
60
 
 
61
        // Add two more empty nodes for the start and end points of any walk sequence
 
62
        _nodes.push_back(WalkNode());
 
63
        _nodes.push_back(WalkNode());
 
64
}
 
65
 
 
66
 
 
67
void Rails::setupRoute(bool bitFlag, const Common::Point &srcPos, const Common::Point &destPos) {
 
68
        // Reset the nodes in as being inactive
 
69
        for (uint i = 0; i < _nodes.size(); ++i)
 
70
                _nodes[i]._active = false;
 
71
 
 
72
        // Set the two extra walk nodes to the start and destination positions
 
73
        setNodePosition(_nodes.size() - 2, srcPos);
 
74
        setNodePosition(_nodes.size() - 1, destPos);
 
75
 
 
76
        // Start constructing route node list
 
77
        _routeLength = 0x3FFF;
 
78
        _routeIndexes.clear();
 
79
 
 
80
        // Recursively form a route from the destination walk node back to the player's position
 
81
        setupRouteNode(&_tempRoute[0], _nodes.size() - 1, bitFlag ? 0xC000 : 0x8000, 0);
 
82
 
 
83
        _next = 0;
 
84
        if (_routeIndexes.size() > 0) {
 
85
                Common::Point currPos = srcPos;
 
86
                for (int routeCtr = size() - 1; (routeCtr >= 0) && !_next; --routeCtr) {
 
87
                        int idx = _routeIndexes[routeCtr];
 
88
                        const Common::Point &pt = _nodes[idx]._walkPos;
 
89
 
 
90
                        _next = scanPath(currPos, pt);
 
91
                        currPos = pt;
 
92
                }
 
93
        }
 
94
}
 
95
 
 
96
void Rails::setupRouteNode(int *routeIndexP, int nodeIndex, int flags, int routeLength) {
 
97
        WalkNode &currentNode = _nodes[nodeIndex];
 
98
        currentNode._active = true;
 
99
 
 
100
        *routeIndexP++ = nodeIndex;
 
101
 
 
102
        // Get the index of the ultimate source position (the player)
 
103
        int subIndex = _nodes.size() - 2;
 
104
 
 
105
        int distanceVal = _nodes[nodeIndex]._distances[subIndex];
 
106
        if (distanceVal & flags) {
 
107
                routeLength += distanceVal & 0x3FFF;
 
108
                if (routeLength < _routeLength) {
 
109
                        // Found a new shorter route to destination, so set up the route with the found one
 
110
                        _routeIndexes.clear();
 
111
                        for (int i = 0; routeIndexP != &_tempRoute[i]; ++i)
 
112
                                _routeIndexes.push(_tempRoute[i]);
 
113
                        _routeLength = routeLength;
 
114
                }
 
115
        } else {
 
116
                for (int idx = _nodes.size() - 2; idx > 0; --idx) {
 
117
                        int nodePos = idx - 1;
 
118
                        if (!_nodes[nodePos]._active && ((currentNode._distances[nodePos] & flags) != 0))
 
119
                                setupRouteNode(routeIndexP, nodePos, 0x8000, routeLength + (distanceVal & 0x3fff));
 
120
                }
 
121
        }
 
122
 
 
123
        currentNode._active = false;
 
124
}
 
125
 
 
126
 
 
127
int Rails::scanPath(const Common::Point &srcPos, const Common::Point &destPos) {
 
128
        // For compressed depth surfaces, always return 0
 
129
        if (_depthStyle == 2)
 
130
                return 0;
 
131
 
 
132
        int yDiff = destPos.y - srcPos.y;
 
133
        int yAmount = MADS_SCREEN_WIDTH;
 
134
 
 
135
        if (yDiff < 0) {
 
136
                yDiff = -yDiff;
 
137
                yAmount = -yAmount;
 
138
        }
 
139
 
 
140
        int xDiff = destPos.x - srcPos.x;
 
141
        int xDirection = 1;
 
142
        int xAmount = 0;
 
143
        if (xDiff < 0) {
 
144
                xDiff = -xDiff;
 
145
                xDirection = -xDirection;
 
146
                xAmount = MIN(yDiff, xDiff);
 
147
        }
 
148
 
 
149
        ++xDiff;
 
150
        ++yDiff;
 
151
 
 
152
        const byte *srcP = _depthSurface->getBasePtr(srcPos.x, srcPos.y);
 
153
        int index = xAmount;
 
154
 
 
155
        // Outer loop
 
156
        for (int xCtr = 0; xCtr < xDiff; ++xCtr, srcP += xDirection) {
 
157
                index += yDiff;
 
158
                int v = (*srcP & 0x7F) >> 4;
 
159
                if (v)
 
160
                        return v;
 
161
 
 
162
                // Inner loop for handling vertical movement
 
163
                while (index >= xDiff) {
 
164
                        index -= xDiff;
 
165
 
 
166
                        v = (*srcP & 0x7F) >> 4;
 
167
                        if (v)
 
168
                                return v;
 
169
 
 
170
                        srcP += yAmount;
 
171
                }
 
172
        }
 
173
 
 
174
        return 0;
 
175
}
 
176
 
 
177
void Rails::resetRoute() {
 
178
        _routeIndexes.clear();
 
179
        _next = 0;
 
180
}
 
181
 
 
182
const WalkNode &Rails::popNode() {
 
183
        assert(!_routeIndexes.empty());
 
184
 
 
185
        return _nodes[_routeIndexes.pop()];
 
186
}
 
187
 
 
188
void Rails::setNodePosition(int nodeIndex, const Common::Point &pt) {
 
189
        int flags, hypotenuse;
 
190
 
 
191
        _nodes[nodeIndex]._walkPos = pt;
 
192
 
 
193
        // Recalculate inter-node lengths
 
194
        for (uint idx = 0; idx < _nodes.size(); ++idx) {
 
195
                int entry;
 
196
                if (idx == (uint)nodeIndex) {
 
197
                        entry = 0x3FFF;
 
198
                } else {
 
199
                        // Process the node
 
200
                        flags = getRouteFlags(pt, _nodes[idx]._walkPos);
 
201
 
 
202
                        int xDiff = ABS(_nodes[idx]._walkPos.x - pt.x);
 
203
                        int yDiff = ABS(_nodes[idx]._walkPos.y - pt.y);
 
204
                        hypotenuse = (int)sqrt((double)(xDiff * xDiff + yDiff * yDiff));
 
205
 
 
206
                        if (hypotenuse >= 0x3FFF)
 
207
                                // Shouldn't ever be this large
 
208
                                hypotenuse = 0x3FFF;
 
209
 
 
210
                        entry = hypotenuse | flags;
 
211
                }
 
212
 
 
213
                _nodes[idx]._distances[nodeIndex] = entry;
 
214
                _nodes[nodeIndex]._distances[idx] = entry;
 
215
        }
 
216
}
 
217
 
 
218
int Rails::getRouteFlags(const Common::Point &src, const Common::Point &dest) {
 
219
        int result = 0x8000;
 
220
        bool flag = false;
 
221
 
 
222
        int xDiff = ABS(dest.x - src.x);
 
223
        int yDiff = ABS(dest.y - src.y);
 
224
        int xDirection = dest.x >= src.x ? 1 : -1;
 
225
        int yDirection = dest.y >= src.y ? _depthSurface->w : -_depthSurface->w;
 
226
        int minorDiff = 0;
 
227
        if (dest.x < src.x)
 
228
                minorDiff = MIN(xDiff, yDiff);
 
229
        ++xDiff;
 
230
        ++yDiff;
 
231
 
 
232
        byte *srcP = _depthSurface->getBasePtr(src.x, src.y);
 
233
 
 
234
        int totalCtr = minorDiff;
 
235
        for (int xCtr = 0; xCtr < xDiff; ++xCtr, srcP += xDirection) {
 
236
                totalCtr += yDiff;
 
237
 
 
238
                if ((*srcP & 0x80) == 0)
 
239
                        flag = false;
 
240
                else if (!flag) {
 
241
                        flag = true;
 
242
                        result -= 0x4000;
 
243
                        if (result == 0)
 
244
                                break;
 
245
                }
 
246
 
 
247
                while (totalCtr >= xDiff) {
 
248
                        totalCtr -= xDiff;
 
249
 
 
250
                        if ((*srcP & 0x80) == 0)
 
251
                                flag = false;
 
252
                        else if (!flag) {
 
253
                                flag = true;
 
254
                                result -= 0x4000;
 
255
                                if (result == 0)
 
256
                                        break;
 
257
                        }
 
258
 
 
259
                        srcP += yDirection;
 
260
                }
 
261
                if (result == 0)
 
262
                        break;
 
263
        }
 
264
 
 
265
        return result;
 
266
}
 
267
 
 
268
void Rails::synchronize(Common::Serializer &s) {
 
269
        s.syncAsSint16LE(_routeLength);
 
270
        s.syncAsSint16LE(_next);
 
271
 
 
272
        if (s.isLoading()) {
 
273
                _routeIndexes.clear();
 
274
        }
 
275
}
 
276
 
 
277
} // End of namespace MADS