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
|
/* 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 "dreamweb/dreamweb.h"
namespace DreamGen {
void DreamGenContext::turnPathOn() {
turnPathOn(al);
}
void DreamGenContext::turnPathOn(uint8 param) {
findOrMake(param, 0xff, data.byte(kRoomnum) + 100);
PathNode *roomsPaths = getRoomsPaths()->nodes;
if (param == 0xff)
return;
roomsPaths[param].on = 0xff;
}
void DreamGenContext::turnPathOff() {
turnPathOff(al);
}
void DreamGenContext::turnPathOff(uint8 param) {
findOrMake(param, 0x00, data.byte(kRoomnum) + 100);
PathNode *roomsPaths = getRoomsPaths()->nodes;
if (param == 0xff)
return;
roomsPaths[param].on = 0x00;
}
void DreamGenContext::turnAnyPathOn(uint8 param, uint8 room) {
findOrMake(param, 0xff, room + 100);
PathNode *paths = (PathNode *)getSegment(data.word(kReels)).ptr(kPathdata + 144 * room, 0);
paths[param].on = 0xff;
}
void DreamGenContext::turnAnyPathOn() {
turnAnyPathOn(al, ah);
}
void DreamGenContext::turnAnyPathOff(uint8 param, uint8 room) {
findOrMake(param, 0x00, room + 100);
PathNode *paths = (PathNode *)getSegment(data.word(kReels)).ptr(kPathdata + 144 * room, 0);
paths[param].on = 0x00;
}
void DreamGenContext::turnAnyPathOff() {
turnAnyPathOff(al, ah);
}
RoomPaths *DreamBase::getRoomsPaths() {
void *result = getSegment(data.word(kReels)).ptr(data.byte(kRoomnum) * 144, 144);
return (RoomPaths *)result;
}
void DreamBase::faceRightWay() {
PathNode *paths = getRoomsPaths()->nodes;
uint8 dir = paths[data.byte(kManspath)].dir;
data.byte(kTurntoface) = dir;
data.byte(kLeavedirection) = dir;
}
void DreamBase::setWalk() {
if (data.byte(kLinepointer) != 254) {
// Already walking
data.byte(kFinaldest) = data.byte(kPointerspath);
} else if (data.byte(kPointerspath) == data.byte(kManspath)) {
// Can't walk
faceRightWay();
} else if (data.byte(kWatchmode) == 1) {
// Holding reel
data.byte(kDestafterhold) = data.byte(kPointerspath);
data.byte(kWatchmode) = 2;
} else if (data.byte(kWatchmode) == 2) {
// Can't walk
} else {
data.byte(kDestination) = data.byte(kPointerspath);
data.byte(kFinaldest) = data.byte(kPointerspath);
if (data.word(kMousebutton) != 2 || data.word(kCommandtype) == 3) {
autoSetWalk();
} else {
data.byte(kWalkandexam) = 1;
data.byte(kWalkexamtype) = data.byte(kCommandtype);
data.byte(kWalkexamnum) = data.byte(kCommand);
autoSetWalk();
}
}
}
void DreamBase::autoSetWalk() {
if (data.byte(kFinaldest) == data.byte(kManspath))
return;
const RoomPaths *roomsPaths = getRoomsPaths();
checkDest(roomsPaths);
data.word(kLinestartx) = roomsPaths->nodes[data.byte(kManspath)].x - 12;
data.word(kLinestarty) = roomsPaths->nodes[data.byte(kManspath)].y - 12;
data.word(kLineendx) = roomsPaths->nodes[data.byte(kDestination)].x - 12;
data.word(kLineendy) = roomsPaths->nodes[data.byte(kDestination)].y - 12;
bresenhams();
if (data.byte(kLinedirection) != 0) {
data.byte(kLinepointer) = data.byte(kLinelength) - 1;
data.byte(kLinedirection) = 1;
return;
}
data.byte(kLinepointer) = 0;
}
void DreamBase::checkDest(const RoomPaths *roomsPaths) {
const PathSegment *segments = roomsPaths->segments;
const uint8 tmp = data.byte(kManspath) << 4;
uint8 destination = data.byte(kDestination);
for (size_t i = 0; i < 24; ++i) {
if ((segments[i].b0 & 0xf0) == tmp &&
(segments[i].b0 & 0x0f) == data.byte(kDestination)) {
data.byte(kDestination) = segments[i].b1 & 0x0f;
return;
}
if (((segments[i].b0 & 0x0f) << 4) == tmp &&
((segments[i].b0 & 0xf0) >> 4) == data.byte(kDestination)) {
destination = segments[i].b1 & 0x0f;
}
}
data.byte(kDestination) = destination;
}
void DreamBase::findXYFromPath() {
const PathNode *roomsPaths = getRoomsPaths()->nodes;
data.byte(kRyanx) = roomsPaths[data.byte(kManspath)].x - 12;
data.byte(kRyany) = roomsPaths[data.byte(kManspath)].y - 12;
}
void DreamGenContext::checkIfPathIsOn() {
flags._z = checkIfPathIsOn(al);
}
bool DreamGenContext::checkIfPathIsOn(uint8 index) {
RoomPaths *roomsPaths = getRoomsPaths();
uint8 pathOn = roomsPaths->nodes[index].on;
return pathOn == 0xff;
}
void DreamBase::bresenhams() {
workoutFrames();
Common::Point *lineData = &_lineData[0];
int16 startX = (int16)data.word(kLinestartx);
int16 startY = (int16)data.word(kLinestarty);
int16 endX = (int16)data.word(kLineendx);
int16 endY = (int16)data.word(kLineendy);
if (endX == startX) {
uint16 deltaY;
int8 y;
if (endY < startY) {
deltaY = startY - endY;
y = (int8)endY;
data.byte(kLinedirection) = 1;
} else {
deltaY = endY - startY;
y = (int8)startY;
data.byte(kLinedirection) = 0;
}
++deltaY;
int8 x = (int8)startX;
data.byte(kLinelength) = deltaY;
for (; deltaY; --deltaY) {
lineData->x = x;
lineData->y = y;
++lineData;
++y;
}
return;
}
uint16 deltaX;
if (endX < startX) {
deltaX = startX - endX;
SWAP(startX, endX);
SWAP(startY, endY);
data.word(kLinestartx) = (uint16)startX;
data.word(kLinestarty) = (uint16)startY;
data.word(kLineendx) = (uint16)endX;
data.word(kLineendy) = (uint16)endY;
data.byte(kLinedirection) = 1;
} else {
deltaX = endX - startX;
data.byte(kLinedirection) = 0;
}
int16 increment;
if (endY == startY) {
int8 x = (int8)startX;
int8 y = (int8)startY;
++deltaX;
data.byte(kLinelength) = deltaX;
for (; deltaX; --deltaX) {
lineData->x = x;
lineData->y = y;
++lineData;
++x;
}
return;
}
uint16 deltaY;
if (startY > endY) {
deltaY = startY - endY;
increment = -1;
} else {
deltaY = endY - startY;
increment = 1;
}
uint16 delta1, delta2;
if (deltaY > deltaX) {
data.byte(kLineroutine) = 1;
delta1 = deltaY;
delta2 = deltaX;
} else {
data.byte(kLineroutine) = 0;
delta1 = deltaX;
delta2 = deltaY;
}
data.word(kIncrement1) = delta2 * 2;
int16 remainder = delta2 * 2 - delta1;
data.word(kIncrement2) = delta2 * 2 - delta1 * 2;
++delta1;
int8 x = (int8)startX;
int8 y = (int8)startY;
data.byte(kLinelength) = delta1;
if (data.byte(kLineroutine) != 1) {
for (; delta1; --delta1) {
lineData->x = x;
lineData->y = y;
++lineData;
++x;
if (remainder < 0) {
remainder += data.word(kIncrement1);
} else {
remainder += data.word(kIncrement2);
y += increment;
}
}
} else {
for (; delta1; --delta1) {
lineData->x = x;
lineData->y = y;
++lineData;
y += increment;
if (remainder < 0) {
remainder += data.word(kIncrement1);
} else {
remainder += data.word(kIncrement2);
++x;
}
}
}
}
void DreamBase::workoutFrames() {
byte tmp;
int diffx, diffy;
// We have to use signed arithmetic here because these values can
// be slightly negative when walking off-screen
int lineStartX = (int16)data.word(kLinestartx);
int lineStartY = (int16)data.word(kLinestarty);
int lineEndX = (int16)data.word(kLineendx);
int lineEndY = (int16)data.word(kLineendy);
diffx = ABS(lineStartX - lineEndX);
diffy = ABS(lineStartY - lineEndY);
if (diffx < diffy) {
tmp = 2;
if (diffx >= (diffy >> 1))
tmp = 1;
} else {
// tendstohoriz
tmp = 0;
if (diffy >= (diffx >> 1))
tmp = 1;
}
if (lineStartX >= lineEndX) {
// isinleft
if (lineStartY < lineEndY) {
if (tmp != 1)
tmp ^= 2;
tmp += 4;
} else {
// topleft
tmp += 6;
}
} else {
// isinright
if (lineStartY < lineEndY) {
tmp += 2;
} else {
// botright
if (tmp != 1)
tmp ^= 2;
}
}
data.byte(kTurntoface) = tmp & 7;
data.byte(kTurndirection) = 0;
}
} // End of namespace DreamGen
|