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
|
/* 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 "agi/agi.h"
#include "agi/graphics.h"
namespace Agi {
bool AgiEngine::checkPosition(ScreenObjEntry *screenObj) {
bool result = true; // position is fine
debugC(4, kDebugLevelSprites, "check position @ %d, %d", screenObj->xPos, screenObj->yPos);
if (screenObj->xPos < 0) {
result = false;
} else {
if (screenObj->xPos + screenObj->xSize > SCRIPT_WIDTH) {
result = false;
} else {
if (screenObj->yPos - screenObj->ySize < -1) {
result = false;
} else {
if (screenObj->yPos >= SCRIPT_HEIGHT) {
result = false;
} else {
if (((!(screenObj->flags & fIgnoreHorizon)) && screenObj->yPos <= _game.horizon)) {
result = false;
}
}
}
}
}
// MH1 needs this, but it breaks LSL1
// TODO: *NOT* in disassembly of AGI3 .149, why was this needed?
// if (getVersion() >= 0x3000) {
// if (screenObj->yPos < screenObj->ySize)
// result = false;
// }
if (!result) {
debugC(4, kDebugLevelSprites, "check position failed: x=%d, y=%d, h=%d, w=%d",
screenObj->xPos, screenObj->yPos, screenObj->xSize, screenObj->ySize);
}
return result;
}
/**
* Check if there's another object on the way
*/
bool AgiEngine::checkCollision(ScreenObjEntry *screenObj) {
ScreenObjEntry *checkObj;
if (screenObj->flags & fIgnoreObjects)
return false;
for (checkObj = _game.screenObjTable; checkObj < &_game.screenObjTable[SCREENOBJECTS_MAX]; checkObj++) {
if ((checkObj->flags & (fAnimated | fDrawn)) != (fAnimated | fDrawn))
continue;
if (checkObj->flags & fIgnoreObjects)
continue;
// Same object, check next
if (screenObj->objectNr == checkObj->objectNr)
continue;
// No horizontal overlap, check next
if (screenObj->xPos + screenObj->xSize < checkObj->xPos || screenObj->xPos > checkObj->xPos + checkObj->xSize)
continue;
// Same y, return error!
if (screenObj->yPos == checkObj->yPos) {
debugC(4, kDebugLevelSprites, "check returns 1 (object %d)", screenObj->objectNr);
return true;
}
// Crossed the baseline, return error!
if ((screenObj->yPos > checkObj->yPos && screenObj->yPos_prev < checkObj->yPos_prev) ||
(screenObj->yPos < checkObj->yPos && screenObj->yPos_prev > checkObj->yPos_prev)) {
debugC(4, kDebugLevelSprites, "check returns 1 (object %d)", screenObj->objectNr);
return true;
}
}
return false;
}
bool AgiEngine::checkPriority(ScreenObjEntry *screenObj) {
bool touchedWater = false;
bool touchedTrigger = false;
bool touchedControl = true;
int16 curX;
int16 curY;
int16 celX;
byte screenPriority = 0;
if (!(screenObj->flags & fFixedPriority)) {
// Priority bands
screenObj->priority = _gfx->priorityFromY(screenObj->yPos);
}
if (screenObj->priority != 0x0f) {
touchedWater = true;
curX = screenObj->xPos;
curY = screenObj->yPos;
for (celX = 0; celX < screenObj->xSize; celX++, curX++) {
screenPriority = _gfx->getPriority(curX, curY);
if (screenPriority == 0) { // unconditional black. no go at all!
touchedControl = false;
break;
}
if (screenPriority != 3) { // not water surface
touchedWater = false;
if (screenPriority == 1) { // conditional blue
if (!(screenObj->flags & fIgnoreBlocks)) {
debugC(4, kDebugLevelSprites, "Blocks observed!");
touchedControl = false;
break;
}
} else if (screenPriority == 2) {
debugC(4, kDebugLevelSprites, "stepped on trigger");
if (!_debug.ignoretriggers)
touchedTrigger = true;
}
}
}
if (touchedControl) {
if (!touchedWater) {
if (screenObj->flags & fOnWater)
touchedControl = false;
} else {
if (screenObj->flags & fOnLand)
touchedControl = false;
}
}
}
// Check ego
if (screenObj->objectNr == 0) {
setFlag(VM_FLAG_EGO_TOUCHED_P2, touchedTrigger ? true : false);
setFlag(VM_FLAG_EGO_WATER, touchedWater ? true : false);
}
return touchedControl;
}
/*
* Public functions
*/
/**
* Update position of objects
* This function updates the position of all animated, updating view
* table entries according to its motion type, step size, etc. The
* new position must be valid according to the sprite positioning
* rules, otherwise the previous position will be kept.
*/
void AgiEngine::updatePosition() {
ScreenObjEntry *screenObj;
int x, y, oldX, oldY, border;
setVar(VM_VAR_BORDER_CODE, 0);
setVar(VM_VAR_BORDER_TOUCH_EGO, 0);
setVar(VM_VAR_BORDER_TOUCH_OBJECT, 0);
for (screenObj = _game.screenObjTable; screenObj < &_game.screenObjTable[SCREENOBJECTS_MAX]; screenObj++) {
if ((screenObj->flags & (fAnimated | fUpdate | fDrawn)) != (fAnimated | fUpdate | fDrawn)) {
continue;
}
if (screenObj->stepTimeCount > 1) {
screenObj->stepTimeCount--;
continue;
}
screenObj->stepTimeCount = screenObj->stepTime;
x = oldX = screenObj->xPos;
y = oldY = screenObj->yPos;
// If object has moved, update its position
if (!(screenObj->flags & fUpdatePos)) {
int dx[9] = { 0, 0, 1, 1, 1, 0, -1, -1, -1 };
int dy[9] = { 0, -1, -1, 0, 1, 1, 1, 0, -1 };
x += screenObj->stepSize * dx[screenObj->direction];
y += screenObj->stepSize * dy[screenObj->direction];
}
// Now check if it touched the borders
border = 0;
// Check left/right borders
if (getVersion() == 0x3086) {
// KQ4 interpreter does a different comparison on x
// The interpreter before (2.917) and after that (3.098) don't do them that way
// This difference is required for at least Sarien bug #192
// KQ4: room 135, hen moves from the center of the screen to the left border,
// but it doesn't disappear.
if (x <= 0) {
x = 0;
border = 4;
}
} else {
// regular comparison
if (x < 0) {
x = 0;
border = 4;
}
}
// } else if (v->entry == 0 && x == 0 && v->flags & fAdjEgoXY) { // should not be required
// // Extra test to walk west clicking the mouse
// x = 0;
// border = 4;
if (!border) {
if (x + screenObj->xSize > SCRIPT_WIDTH) {
x = SCRIPT_WIDTH - screenObj->xSize;
border = 2;
}
}
// Check top/bottom borders.
if (y - screenObj->ySize < -1) {
y = screenObj->ySize - 1;
border = 1;
} else if (y > SCRIPT_HEIGHT - 1) {
y = SCRIPT_HEIGHT - 1;
border = 3;
} else if ((!(screenObj->flags & fIgnoreHorizon)) && y <= _game.horizon) {
debugC(4, kDebugLevelSprites, "y = %d, horizon = %d", y, _game.horizon);
y = _game.horizon + 1;
border = 1;
}
// Test new position. rollback if test fails
screenObj->xPos = x;
screenObj->yPos = y;
if (checkCollision(screenObj) || !checkPriority(screenObj)) {
screenObj->xPos = oldX;
screenObj->yPos = oldY;
border = 0;
fixPosition(screenObj->objectNr);
}
if (border) {
if (isEgoView(screenObj)) {
setVar(VM_VAR_BORDER_TOUCH_EGO, border);
} else {
setVar(VM_VAR_BORDER_CODE, screenObj->objectNr);
setVar(VM_VAR_BORDER_TOUCH_OBJECT, border);
}
if (screenObj->motionType == kMotionMoveObj) {
motionMoveObjStop(screenObj);
}
}
screenObj->flags &= ~fUpdatePos;
}
}
/**
* Adjust position of a sprite
* This function adjusts the position of a sprite moving it until
* certain criteria is matched. According to priority and control line
* data, a sprite may not always appear at the location we specified.
* This behavior is also known as the "Budin-Sonneveld effect".
*
* @param n view table entry number
*/
void AgiEngine::fixPosition(int16 screenObjNr) {
ScreenObjEntry *screenObj = &_game.screenObjTable[screenObjNr];
fixPosition(screenObj);
}
void AgiEngine::fixPosition(ScreenObjEntry *screenObj) {
int count, dir, size;
debugC(4, kDebugLevelSprites, "adjusting view table entry #%d (%d,%d)", screenObj->objectNr, screenObj->xPos, screenObj->yPos);
// test horizon
if ((!(screenObj->flags & fIgnoreHorizon)) && screenObj->yPos <= _game.horizon)
screenObj->yPos = _game.horizon + 1;
dir = 0;
count = size = 1;
while (!checkPosition(screenObj) || checkCollision(screenObj) || !checkPriority(screenObj)) {
switch (dir) {
case 0: // west
screenObj->xPos--;
if (--count)
continue;
dir = 1;
break;
case 1: // south
screenObj->yPos++;
if (--count)
continue;
dir = 2;
size++;
break;
case 2: // east
screenObj->xPos++;
if (--count)
continue;
dir = 3;
break;
case 3: // north
screenObj->yPos--;
if (--count)
continue;
dir = 0;
size++;
break;
default:
break;
}
count = size;
}
debugC(4, kDebugLevelSprites, "view table entry #%d position adjusted to (%d,%d)", screenObj->objectNr, screenObj->xPos, screenObj->yPos);
}
} // End of namespace Agi
|