2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2006-05-23 23:43:52 +00:00
|
|
|
*
|
2007-05-30 21:56:52 +00:00
|
|
|
* 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.
|
2006-05-23 23:43:52 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2006-05-23 23:43:52 +00:00
|
|
|
#include "agi/agi.h"
|
|
|
|
|
|
|
|
namespace Agi {
|
|
|
|
|
2007-02-03 21:37:52 +00:00
|
|
|
int AgiEngine::checkPosition(VtEntry *v) {
|
2007-01-16 12:40:51 +00:00
|
|
|
debugC(4, kDebugLevelSprites, "check position @ %d, %d", v->xPos, v->yPos);
|
|
|
|
|
|
|
|
if (v->xPos < 0 ||
|
|
|
|
v->xPos + v->xSize > _WIDTH ||
|
|
|
|
v->yPos - v->ySize + 1 < 0 ||
|
|
|
|
v->yPos >= _HEIGHT ||
|
|
|
|
((~v->flags & IGNORE_HORIZON) && v->yPos <= _game.horizon)) {
|
2006-05-23 23:43:52 +00:00
|
|
|
debugC(4, kDebugLevelSprites, "check position failed: x=%d, y=%d, h=%d, w=%d",
|
2007-01-16 12:40:51 +00:00
|
|
|
v->xPos, v->yPos, v->xSize, v->ySize);
|
2006-05-23 23:43:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* MH1 needs this, but it breaks LSL1 */
|
2006-12-06 19:27:02 +00:00
|
|
|
if (agiGetRelease() >= 0x3000) {
|
2007-01-16 12:40:51 +00:00
|
|
|
if (v->yPos < v->ySize)
|
2006-05-23 23:43:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if there's another object on the way
|
|
|
|
*/
|
2007-02-03 21:37:52 +00:00
|
|
|
int AgiEngine::checkCollision(VtEntry *v) {
|
|
|
|
VtEntry *u;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
|
|
|
if (v->flags & IGNORE_OBJECTS)
|
|
|
|
return 0;
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
for (u = _game.viewTable; u < &_game.viewTable[MAX_VIEWTABLE]; u++) {
|
2006-05-23 23:43:52 +00:00
|
|
|
if ((u->flags & (ANIMATED | DRAWN)) != (ANIMATED | DRAWN))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (u->flags & IGNORE_OBJECTS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Same object, check next */
|
|
|
|
if (v->entry == u->entry)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* No horizontal overlap, check next */
|
2007-01-16 12:40:51 +00:00
|
|
|
if (v->xPos + v->xSize < u->xPos || v->xPos > u->xPos + u->xSize)
|
2006-05-23 23:43:52 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Same y, return error! */
|
2007-05-06 10:35:47 +00:00
|
|
|
if (v->yPos == u->yPos) {
|
|
|
|
debugC(4, kDebugLevelSprites, "check returns 1 (object %d)", v->entry);
|
|
|
|
return 1;
|
|
|
|
}
|
2006-05-23 23:43:52 +00:00
|
|
|
|
|
|
|
/* Crossed the baseline, return error! */
|
2007-01-16 12:40:51 +00:00
|
|
|
if ((v->yPos > u->yPos && v->yPos2 < u->yPos2) ||
|
|
|
|
(v->yPos < u->yPos && v->yPos2 > u->yPos2)) {
|
2007-05-06 10:35:47 +00:00
|
|
|
debugC(4, kDebugLevelSprites, "check returns 1 (object %d)", v->entry);
|
|
|
|
return 1;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
int AgiEngine::checkPriority(VtEntry *v) {
|
2006-05-23 23:43:52 +00:00
|
|
|
int i, trigger, water, pass, pri;
|
|
|
|
uint8 *p0;
|
|
|
|
|
|
|
|
if (~v->flags & FIXED_PRIORITY) {
|
|
|
|
/* Priority bands */
|
2007-01-16 12:40:51 +00:00
|
|
|
v->priority = _game.priTable[v->yPos];
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trigger = 0;
|
|
|
|
water = 0;
|
|
|
|
pass = 1;
|
|
|
|
|
2007-05-06 10:35:47 +00:00
|
|
|
if (v->priority == 0x0f) {
|
|
|
|
// Check ego
|
|
|
|
if (v->entry == 0) {
|
|
|
|
setflag(fEgoTouchedP2, trigger ? true : false);
|
|
|
|
setflag(fEgoWater, water ? true : false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
2007-09-19 08:40:12 +00:00
|
|
|
}
|
2006-05-23 23:43:52 +00:00
|
|
|
|
|
|
|
water = 1;
|
|
|
|
|
2007-06-20 23:36:59 +00:00
|
|
|
p0 = &_game.sbuf16c[v->xPos + v->yPos * _WIDTH];
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
for (i = 0; i < v->xSize; i++, p0++) {
|
2006-05-23 23:43:52 +00:00
|
|
|
pri = *p0 >> 4;
|
|
|
|
|
|
|
|
if (pri == 0) { /* unconditional black. no go at all! */
|
|
|
|
pass = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pri == 3) /* water surface */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
water = 0;
|
|
|
|
|
|
|
|
if (pri == 1) { /* conditional blue */
|
|
|
|
if (v->flags & IGNORE_BLOCKS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
debugC(4, kDebugLevelSprites, "Blocks observed!");
|
|
|
|
pass = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pri == 2) { /* trigger */
|
|
|
|
debugC(4, kDebugLevelSprites, "stepped on trigger");
|
2006-12-06 19:27:02 +00:00
|
|
|
if (!_debug.ignoretriggers)
|
2006-05-23 23:43:52 +00:00
|
|
|
trigger = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pass) {
|
|
|
|
if (!water && v->flags & ON_WATER)
|
|
|
|
pass = 0;
|
|
|
|
if (water && v->flags & ON_LAND)
|
|
|
|
pass = 0;
|
|
|
|
}
|
|
|
|
|
2007-05-06 10:35:47 +00:00
|
|
|
// Check ego
|
2006-05-23 23:43:52 +00:00
|
|
|
if (v->entry == 0) {
|
2007-01-16 12:40:51 +00:00
|
|
|
setflag(fEgoTouchedP2, trigger ? true : false);
|
|
|
|
setflag(fEgoWater, water ? true : false);
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2007-01-16 12:40:51 +00:00
|
|
|
void AgiEngine::updatePosition() {
|
2007-02-03 21:37:52 +00:00
|
|
|
VtEntry *v;
|
2007-01-16 12:40:51 +00:00
|
|
|
int x, y, oldX, oldY, border;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
_game.vars[vBorderCode] = 0;
|
|
|
|
_game.vars[vBorderTouchEgo] = 0;
|
|
|
|
_game.vars[vBorderTouchObj] = 0;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
for (v = _game.viewTable; v < &_game.viewTable[MAX_VIEWTABLE]; v++) {
|
2006-05-23 23:43:52 +00:00
|
|
|
if ((v->flags & (ANIMATED | UPDATE | DRAWN)) != (ANIMATED | UPDATE | DRAWN)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
if (v->stepTimeCount != 0) {
|
|
|
|
if (--v->stepTimeCount != 0)
|
2006-05-23 23:43:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
v->stepTimeCount = v->stepTime;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
x = oldX = v->xPos;
|
|
|
|
y = oldY = v->yPos;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
|
|
|
/* If object has moved, update its position */
|
|
|
|
if (~v->flags & UPDATE_POS) {
|
|
|
|
int dx[9] = { 0, 0, 1, 1, 1, 0, -1, -1, -1 };
|
|
|
|
int dy[9] = { 0, -1, -1, 0, 1, 1, 1, 0, -1 };
|
2007-01-16 12:40:51 +00:00
|
|
|
x += v->stepSize * dx[v->direction];
|
|
|
|
y += v->stepSize * dy[v->direction];
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now check if it touched the borders */
|
|
|
|
border = 0;
|
|
|
|
|
|
|
|
/* Check left/right borders */
|
|
|
|
if (x < 0) {
|
|
|
|
x = 0;
|
|
|
|
border = 4;
|
2006-12-06 19:27:02 +00:00
|
|
|
} else if (x <= 0 && agiGetRelease() == 0x3086) { /* KQ4 */
|
2007-05-07 05:06:22 +00:00
|
|
|
x = 0; /* See Sarien bug #590462 */
|
2006-05-23 23:43:52 +00:00
|
|
|
border = 4;
|
|
|
|
} else if (v->entry == 0 && x == 0 && v->flags & ADJ_EGO_XY) {
|
|
|
|
/* Extra test to walk west clicking the mouse */
|
|
|
|
x = 0;
|
|
|
|
border = 4;
|
2007-01-16 12:40:51 +00:00
|
|
|
} else if (x + v->xSize > _WIDTH) {
|
|
|
|
x = _WIDTH - v->xSize;
|
2006-05-23 23:43:52 +00:00
|
|
|
border = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check top/bottom borders. */
|
2007-01-16 12:40:51 +00:00
|
|
|
if (y - v->ySize + 1 < 0) {
|
|
|
|
y = v->ySize - 1;
|
2006-05-23 23:43:52 +00:00
|
|
|
border = 1;
|
|
|
|
} else if (y > _HEIGHT - 1) {
|
|
|
|
y = _HEIGHT - 1;
|
|
|
|
border = 3;
|
2007-01-16 12:40:51 +00:00
|
|
|
} else if ((~v->flags & IGNORE_HORIZON) && y <= _game.horizon) {
|
|
|
|
debugC(4, kDebugLevelSprites, "y = %d, horizon = %d", y, _game.horizon);
|
|
|
|
y = _game.horizon + 1;
|
2006-05-23 23:43:52 +00:00
|
|
|
border = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test new position. rollback if test fails */
|
2007-01-16 12:40:51 +00:00
|
|
|
v->xPos = x;
|
|
|
|
v->yPos = y;
|
|
|
|
if (checkCollision(v) || !checkPriority(v)) {
|
|
|
|
v->xPos = oldX;
|
|
|
|
v->yPos = oldY;
|
2006-05-23 23:43:52 +00:00
|
|
|
border = 0;
|
2007-01-16 12:40:51 +00:00
|
|
|
fixPosition(v->entry);
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (border != 0) {
|
2007-01-16 12:40:51 +00:00
|
|
|
if (isEgoView(v)) {
|
|
|
|
_game.vars[vBorderTouchEgo] = border;
|
2006-05-23 23:43:52 +00:00
|
|
|
} else {
|
2007-01-16 12:40:51 +00:00
|
|
|
_game.vars[vBorderCode] = v->entry;
|
|
|
|
_game.vars[vBorderTouchObj] = border;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
if (v->motion == MOTION_MOVE_OBJ) {
|
2007-01-16 12:40:51 +00:00
|
|
|
inDestination(v);
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v->flags &= ~UPDATE_POS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 behaviour is also known as the "Budin-Sonneveld effect".
|
|
|
|
*
|
|
|
|
* @param n view table entry number
|
|
|
|
*/
|
2007-01-16 12:40:51 +00:00
|
|
|
void AgiEngine::fixPosition(int n) {
|
|
|
|
VtEntry *v = &_game.viewTable[n];
|
2006-05-23 23:43:52 +00:00
|
|
|
int count, dir, size;
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
debugC(4, kDebugLevelSprites, "adjusting view table entry #%d (%d,%d)", n, v->xPos, v->yPos);
|
2006-05-23 23:43:52 +00:00
|
|
|
|
|
|
|
/* test horizon */
|
2007-01-16 12:40:51 +00:00
|
|
|
if ((~v->flags & IGNORE_HORIZON) && v->yPos <= _game.horizon)
|
|
|
|
v->yPos = _game.horizon + 1;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
|
|
|
dir = 0;
|
|
|
|
count = size = 1;
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
while (!checkPosition(v) || checkCollision(v) || !checkPriority(v)) {
|
2006-05-23 23:43:52 +00:00
|
|
|
switch (dir) {
|
|
|
|
case 0: /* west */
|
2007-01-16 12:40:51 +00:00
|
|
|
v->xPos--;
|
2006-05-23 23:43:52 +00:00
|
|
|
if (--count)
|
|
|
|
continue;
|
|
|
|
dir = 1;
|
|
|
|
break;
|
|
|
|
case 1: /* south */
|
2007-01-16 12:40:51 +00:00
|
|
|
v->yPos++;
|
2006-05-23 23:43:52 +00:00
|
|
|
if (--count)
|
|
|
|
continue;
|
|
|
|
dir = 2;
|
|
|
|
size++;
|
|
|
|
break;
|
|
|
|
case 2: /* east */
|
2007-01-16 12:40:51 +00:00
|
|
|
v->xPos++;
|
2006-05-23 23:43:52 +00:00
|
|
|
if (--count)
|
|
|
|
continue;
|
|
|
|
dir = 3;
|
|
|
|
break;
|
|
|
|
case 3: /* north */
|
2007-01-16 12:40:51 +00:00
|
|
|
v->yPos--;
|
2006-05-23 23:43:52 +00:00
|
|
|
if (--count)
|
|
|
|
continue;
|
|
|
|
dir = 0;
|
|
|
|
size++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = size;
|
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
debugC(4, kDebugLevelSprites, "view table entry #%d position adjusted to (%d,%d)", n, v->xPos, v->yPos);
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace Agi
|