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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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 ||
|
2011-08-14 17:47:51 +00:00
|
|
|
((~v->flags & fIgnoreHorizon) && 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;
|
|
|
|
}
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// MH1 needs this, but it breaks LSL1
|
2009-06-06 17:45:52 +00:00
|
|
|
if (getVersion() >= 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
|
|
|
|
2011-08-14 17:47:51 +00:00
|
|
|
if (v->flags & fIgnoreObjects)
|
2006-05-23 23:43:52 +00:00
|
|
|
return 0;
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
for (u = _game.viewTable; u < &_game.viewTable[MAX_VIEWTABLE]; u++) {
|
2011-08-14 17:47:51 +00:00
|
|
|
if ((u->flags & (fAnimated | fDrawn)) != (fAnimated | fDrawn))
|
2006-05-23 23:43:52 +00:00
|
|
|
continue;
|
|
|
|
|
2011-08-14 17:47:51 +00:00
|
|
|
if (u->flags & fIgnoreObjects)
|
2006-05-23 23:43:52 +00:00
|
|
|
continue;
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// Same object, check next
|
2006-05-23 23:43:52 +00:00
|
|
|
if (v->entry == u->entry)
|
|
|
|
continue;
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// 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;
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// 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
|
|
|
|
2009-06-06 17:39:13 +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;
|
|
|
|
|
2011-08-14 17:47:51 +00:00
|
|
|
if (~v->flags & fFixedPriority) {
|
2009-06-06 17:39:13 +00:00
|
|
|
// 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;
|
|
|
|
|
2012-01-07 16:55:11 +00:00
|
|
|
// Check if any picture is loaded before checking for priority below.
|
|
|
|
// If no picture has been loaded, the priority buffer won't be initialized,
|
|
|
|
// thus the check below will always fail. This case causes an infinite loop
|
|
|
|
// in the fanmade game Nick's Quest (bug #3451122), as the game attempts to
|
|
|
|
// draw a sprite (view 4, floating Nick) before it loads any picture. This
|
|
|
|
// causes the checks below to always fail, and the engine keeps readjusting
|
|
|
|
// the sprite's position in fixPosition() forever, as there is no valid
|
|
|
|
// position to place it (the default visual and priority screen is set to
|
|
|
|
// zero, i.e. unconditional black). To remedy this situation, we always
|
|
|
|
// return true here if no picture has been loaded and no priority screen
|
|
|
|
// has been set up.
|
|
|
|
if (!_game._vm->_picture->isPictureLoaded()) {
|
|
|
|
warning("checkPriority: no picture loaded");
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
if (pri == 0) { // unconditional black. no go at all!
|
2006-05-23 23:43:52 +00:00
|
|
|
pass = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
if (pri == 3) // water surface
|
2006-05-23 23:43:52 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
water = 0;
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
if (pri == 1) { // conditional blue
|
2011-08-14 17:47:51 +00:00
|
|
|
if (v->flags & fIgnoreBlocks)
|
2006-05-23 23:43:52 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
debugC(4, kDebugLevelSprites, "Blocks observed!");
|
|
|
|
pass = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
if (pri == 2) { // trigger
|
2006-05-23 23:43:52 +00:00
|
|
|
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) {
|
2011-08-14 17:47:51 +00:00
|
|
|
if (!water && v->flags & fOnWater)
|
2006-05-23 23:43:52 +00:00
|
|
|
pass = 0;
|
2011-08-14 17:47:51 +00:00
|
|
|
if (water && v->flags & fOnLand)
|
2006-05-23 23:43:52 +00:00
|
|
|
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++) {
|
2011-08-14 17:47:51 +00:00
|
|
|
if ((v->flags & (fAnimated | fUpdate | fDrawn)) != (fAnimated | fUpdate | fDrawn)) {
|
2006-05-23 23:43:52 +00:00
|
|
|
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
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// If object has moved, update its position
|
2011-08-14 17:47:51 +00:00
|
|
|
if (~v->flags & fUpdatePos) {
|
2006-05-23 23:43:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// Now check if it touched the borders
|
2006-05-23 23:43:52 +00:00
|
|
|
border = 0;
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// Check left/right borders
|
2006-05-23 23:43:52 +00:00
|
|
|
if (x < 0) {
|
|
|
|
x = 0;
|
|
|
|
border = 4;
|
2009-06-06 17:45:52 +00:00
|
|
|
} else if (x <= 0 && getVersion() == 0x3086) { // KQ4
|
2009-06-06 17:39:13 +00:00
|
|
|
x = 0; // See Sarien bug #590462
|
2006-05-23 23:43:52 +00:00
|
|
|
border = 4;
|
2011-08-14 17:47:51 +00:00
|
|
|
} else if (v->entry == 0 && x == 0 && v->flags & fAdjEgoXY) {
|
2009-06-06 17:39:13 +00:00
|
|
|
// Extra test to walk west clicking the mouse
|
2006-05-23 23:43:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// 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;
|
2011-08-14 17:47:51 +00:00
|
|
|
} else if ((~v->flags & fIgnoreHorizon) && y <= _game.horizon) {
|
2007-01-16 12:40:51 +00:00
|
|
|
debugC(4, kDebugLevelSprites, "y = %d, horizon = %d", y, _game.horizon);
|
|
|
|
y = _game.horizon + 1;
|
2006-05-23 23:43:52 +00:00
|
|
|
border = 1;
|
|
|
|
}
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// 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
|
|
|
}
|
2011-08-14 17:47:51 +00:00
|
|
|
if (v->motion == kMotionMoveObj) {
|
2007-01-16 12:40:51 +00:00
|
|
|
inDestination(v);
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-14 17:47:51 +00:00
|
|
|
v->flags &= ~fUpdatePos;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2011-05-25 14:31:37 +00:00
|
|
|
* This behavior is also known as the "Budin-Sonneveld effect".
|
2006-05-23 23:43:52 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// test horizon
|
2011-08-14 17:47:51 +00:00
|
|
|
if ((~v->flags & fIgnoreHorizon) && v->yPos <= _game.horizon)
|
2007-01-16 12:40:51 +00:00
|
|
|
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) {
|
2009-06-06 17:39:13 +00:00
|
|
|
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;
|
2009-06-06 17:39:13 +00:00
|
|
|
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;
|
2009-06-06 17:39:13 +00:00
|
|
|
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;
|
2009-06-06 17:39:13 +00:00
|
|
|
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
|