2007-07-29 16:33:11 +00:00
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-05-11 23:16:50 +00:00
|
|
|
#include "kyra/kyra_v1.h"
|
2007-07-29 16:33:11 +00:00
|
|
|
|
|
|
|
namespace Kyra {
|
|
|
|
|
2008-05-11 23:16:50 +00:00
|
|
|
int KyraEngine_v1::findWay(int x, int y, int toX, int toY, int *moveTable, int moveTableSize) {
|
2007-07-29 16:33:11 +00:00
|
|
|
x &= 0xFFFC; toX &= 0xFFFC;
|
|
|
|
y &= 0xFFFE; toY &= 0xFFFE;
|
|
|
|
x = (int16)x; y = (int16)y; toX = (int16)toX; toY = (int16)toY;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
if (x == toY && y == toY) {
|
|
|
|
moveTable[0] = 8;
|
|
|
|
return 0;
|
|
|
|
}
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
int curX = x;
|
|
|
|
int curY = y;
|
|
|
|
int tempValue = 0;
|
|
|
|
int lastUsedEntry = 0;
|
|
|
|
int *pathTable1 = new int[0x7D0];
|
|
|
|
int *pathTable2 = new int[0x7D0];
|
|
|
|
assert(pathTable1 && pathTable2);
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
while (true) {
|
|
|
|
int newFacing = getFacingFromPointToPoint(x, y, toX, toY);
|
|
|
|
changePosTowardsFacing(curX, curY, newFacing);
|
|
|
|
|
|
|
|
if (curX == toX && curY == toY) {
|
|
|
|
if (!lineIsPassable(curX, curY))
|
|
|
|
break;
|
|
|
|
moveTable[lastUsedEntry++] = newFacing;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineIsPassable(curX, curY)) {
|
|
|
|
if (lastUsedEntry == moveTableSize) {
|
2008-05-07 14:20:37 +00:00
|
|
|
delete[] pathTable1;
|
|
|
|
delete[] pathTable2;
|
2007-07-29 16:33:11 +00:00
|
|
|
return 0x7D00;
|
|
|
|
}
|
|
|
|
// debug drawing
|
2007-08-31 13:11:26 +00:00
|
|
|
/*if (curX >= 0 && curY >= 0 && curX < 320 && curY < 200) {
|
|
|
|
screen()->setPagePixel(0, curX, curY, 11);
|
|
|
|
screen()->updateScreen();
|
2008-05-11 16:48:54 +00:00
|
|
|
delayWithTicks(5);
|
2007-08-31 13:11:26 +00:00
|
|
|
}*/
|
2007-07-29 16:33:11 +00:00
|
|
|
moveTable[lastUsedEntry++] = newFacing;
|
|
|
|
x = curX;
|
|
|
|
y = curY;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int temp = 0;
|
|
|
|
while (true) {
|
|
|
|
newFacing = getFacingFromPointToPoint(curX, curY, toX, toY);
|
|
|
|
changePosTowardsFacing(curX, curY, newFacing);
|
|
|
|
// debug drawing
|
2007-08-31 13:11:26 +00:00
|
|
|
/*if (curX >= 0 && curY >= 0 && curX < 320 && curY < 200) {
|
|
|
|
screen()->setPagePixel(0, curX, curY, 8);
|
|
|
|
screen()->updateScreen();
|
2008-05-11 16:48:54 +00:00
|
|
|
delayWithTicks(5);
|
2007-08-31 13:11:26 +00:00
|
|
|
}*/
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
if (!lineIsPassable(curX, curY)) {
|
|
|
|
if (curX != toX || curY != toY)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curX == toX && curY == toY) {
|
|
|
|
if (!lineIsPassable(curX, curY)) {
|
|
|
|
tempValue = 0;
|
|
|
|
temp = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
temp = findSubPath(x, y, curX, curY, pathTable1, 1, 0x7D0);
|
|
|
|
tempValue = findSubPath(x, y, curX, curY, pathTable2, 0, 0x7D0);
|
|
|
|
if (curX == toX && curY == toY) {
|
|
|
|
if (temp == 0x7D00 && tempValue == 0x7D00) {
|
2008-05-07 14:20:37 +00:00
|
|
|
delete[] pathTable1;
|
|
|
|
delete[] pathTable2;
|
2007-07-29 16:33:11 +00:00
|
|
|
return 0x7D00;
|
|
|
|
}
|
|
|
|
}
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
if (temp != 0x7D00 || tempValue != 0x7D00)
|
|
|
|
break;
|
|
|
|
}
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
if (temp < tempValue) {
|
|
|
|
if (lastUsedEntry + temp > moveTableSize) {
|
2008-05-07 14:20:37 +00:00
|
|
|
delete[] pathTable1;
|
|
|
|
delete[] pathTable2;
|
2007-07-29 16:33:11 +00:00
|
|
|
return 0x7D00;
|
|
|
|
}
|
2011-12-26 20:19:29 +00:00
|
|
|
memcpy(&moveTable[lastUsedEntry], pathTable1, temp * sizeof(int));
|
2007-07-29 16:33:11 +00:00
|
|
|
lastUsedEntry += temp;
|
|
|
|
} else {
|
|
|
|
if (lastUsedEntry + tempValue > moveTableSize) {
|
2008-05-07 14:20:37 +00:00
|
|
|
delete[] pathTable1;
|
|
|
|
delete[] pathTable2;
|
2007-07-29 16:33:11 +00:00
|
|
|
return 0x7D00;
|
|
|
|
}
|
2011-12-26 20:19:29 +00:00
|
|
|
memcpy(&moveTable[lastUsedEntry], pathTable2, tempValue * sizeof(int));
|
2007-07-29 16:33:11 +00:00
|
|
|
lastUsedEntry += tempValue;
|
|
|
|
}
|
|
|
|
x = curX;
|
|
|
|
y = curY;
|
|
|
|
if (curX == toX && curY == toY)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-05-07 14:20:37 +00:00
|
|
|
delete[] pathTable1;
|
|
|
|
delete[] pathTable2;
|
2007-07-29 16:33:11 +00:00
|
|
|
moveTable[lastUsedEntry] = 8;
|
|
|
|
return lastUsedEntry;
|
|
|
|
}
|
|
|
|
|
2008-05-11 23:16:50 +00:00
|
|
|
int KyraEngine_v1::findSubPath(int x, int y, int toX, int toY, int *moveTable, int start, int end) {
|
2007-07-29 16:33:11 +00:00
|
|
|
// only used for debug specific code
|
|
|
|
//static uint16 unkTable[] = { 8, 5 };
|
|
|
|
static const int8 facingTable1[] = { 7, 0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 0 };
|
|
|
|
static const int8 facingTable2[] = { -1, 0, -1, 2, -1, 4, -1, 6, -1, 2, -1, 4, -1, 6, -1, 0 };
|
|
|
|
static const int8 facingTable3[] = { 2, 4, 4, 6, 6, 0, 0, 2, 6, 6, 0, 0, 2, 2, 4, 4 };
|
|
|
|
static const int8 addPosTableX[] = { -1, 0, -1, 4, -1, 0, -1, -4, -1, -4, -1, 0, -1, 4, -1, 0 };
|
|
|
|
static const int8 addPosTableY[] = { -1, 2, -1, 0, -1, -2, -1, 0, -1, 0, -1, 2, -1, 0, -1, -2 };
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
// debug specific
|
|
|
|
/*++unkTable[start];
|
|
|
|
while (screen()->getPalette(0)[unkTable[start]] != 0x0F) {
|
|
|
|
++unkTable[start];
|
|
|
|
}*/
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
int xpos1 = x, xpos2 = x;
|
|
|
|
int ypos1 = y, ypos2 = y;
|
|
|
|
int newFacing = getFacingFromPointToPoint(x, y, toX, toY);
|
|
|
|
int position = 0;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
while (position != end) {
|
|
|
|
int newFacing2 = newFacing;
|
|
|
|
while (true) {
|
2011-12-26 20:19:29 +00:00
|
|
|
changePosTowardsFacing(xpos1, ypos1, facingTable1[start * 8 + newFacing2]);
|
2007-07-29 16:33:11 +00:00
|
|
|
if (!lineIsPassable(xpos1, ypos1)) {
|
2011-12-26 20:19:29 +00:00
|
|
|
if (facingTable1[start * 8 + newFacing2] == newFacing)
|
2007-07-29 16:33:11 +00:00
|
|
|
return 0x7D00;
|
2011-12-26 20:19:29 +00:00
|
|
|
newFacing2 = facingTable1[start * 8 + newFacing2];
|
2007-07-29 16:33:11 +00:00
|
|
|
xpos1 = x;
|
|
|
|
ypos1 = y;
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-26 20:19:29 +00:00
|
|
|
newFacing = facingTable1[start * 8 + newFacing2];
|
2007-07-29 16:33:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// debug drawing
|
|
|
|
/*if (xpos1 >= 0 && ypos1 >= 0 && xpos1 < 320 && ypos1 < 200) {
|
|
|
|
screen()->setPagePixel(0, xpos1, ypos1, unkTable[start]);
|
|
|
|
screen()->updateScreen();
|
2008-05-11 16:48:54 +00:00
|
|
|
delayWithTicks(5);
|
2007-07-29 16:33:11 +00:00
|
|
|
}*/
|
|
|
|
if (newFacing & 1) {
|
|
|
|
int temp = xpos1 + addPosTableX[newFacing + start * 8];
|
|
|
|
if (toX == temp) {
|
|
|
|
temp = ypos1 + addPosTableY[newFacing + start * 8];
|
|
|
|
if (toY == temp) {
|
|
|
|
moveTable[position++] = facingTable2[newFacing + start * 8];
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
moveTable[position++] = newFacing;
|
|
|
|
x = xpos1;
|
|
|
|
y = ypos1;
|
|
|
|
|
|
|
|
if (x == toX && y == toY)
|
|
|
|
return position;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
if (xpos1 == xpos2 && ypos1 == ypos2)
|
|
|
|
break;
|
|
|
|
|
2011-12-26 20:19:29 +00:00
|
|
|
newFacing = facingTable3[start * 8 + newFacing];
|
2007-07-29 16:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0x7D00;
|
|
|
|
}
|
|
|
|
|
2008-05-11 23:16:50 +00:00
|
|
|
int KyraEngine_v1::getFacingFromPointToPoint(int x, int y, int toX, int toY) {
|
2007-07-29 16:33:11 +00:00
|
|
|
static const int facingTable[] = {
|
|
|
|
1, 0, 1, 2, 3, 4, 3, 2, 7, 0, 7, 6, 5, 4, 5, 6
|
|
|
|
};
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
int facingEntry = 0;
|
|
|
|
int ydiff = y - toY;
|
|
|
|
if (ydiff < 0) {
|
|
|
|
++facingEntry;
|
|
|
|
ydiff = -ydiff;
|
2008-01-27 19:47:41 +00:00
|
|
|
}
|
2007-07-29 16:33:11 +00:00
|
|
|
facingEntry <<= 1;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
int xdiff = toX - x;
|
|
|
|
if (xdiff < 0) {
|
|
|
|
++facingEntry;
|
|
|
|
xdiff = -xdiff;
|
|
|
|
}
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
if (xdiff >= ydiff) {
|
|
|
|
int temp = ydiff;
|
|
|
|
ydiff = xdiff;
|
|
|
|
xdiff = temp;
|
|
|
|
|
|
|
|
facingEntry <<= 1;
|
|
|
|
} else {
|
|
|
|
facingEntry <<= 1;
|
|
|
|
facingEntry += 1;
|
|
|
|
}
|
|
|
|
int temp = (ydiff + 1) >> 1;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
if (xdiff < temp) {
|
|
|
|
facingEntry <<= 1;
|
|
|
|
facingEntry += 1;
|
|
|
|
} else {
|
|
|
|
facingEntry <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(facingEntry < ARRAYSIZE(facingTable));
|
|
|
|
return facingTable[facingEntry];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-11 23:16:50 +00:00
|
|
|
int KyraEngine_v1::getOppositeFacingDirection(int dir) {
|
2007-07-29 16:33:11 +00:00
|
|
|
switch (dir) {
|
|
|
|
case 0:
|
|
|
|
return 2;
|
|
|
|
case 1:
|
|
|
|
return 1;
|
|
|
|
case 3:
|
|
|
|
return 7;
|
|
|
|
case 4:
|
|
|
|
return 6;
|
|
|
|
case 5:
|
|
|
|
return 5;
|
|
|
|
case 6:
|
|
|
|
return 4;
|
|
|
|
case 7:
|
|
|
|
return 3;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-11 23:16:50 +00:00
|
|
|
void KyraEngine_v1::changePosTowardsFacing(int &x, int &y, int facing) {
|
2007-07-29 16:33:11 +00:00
|
|
|
x += _addXPosTable[facing];
|
|
|
|
y += _addYPosTable[facing];
|
|
|
|
}
|
|
|
|
|
2008-05-11 23:16:50 +00:00
|
|
|
int KyraEngine_v1::getMoveTableSize(int *moveTable) {
|
2011-11-04 19:01:57 +00:00
|
|
|
int tableSize = 0;
|
2007-07-29 16:33:11 +00:00
|
|
|
if (moveTable[0] == 8)
|
|
|
|
return 0;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
static const int facingTable[] = {
|
|
|
|
4, 5, 6, 7, 0, 1, 2, 3
|
|
|
|
};
|
|
|
|
static const int unkTable[] = {
|
|
|
|
-1, -1, 1, 2, -1, 6, 7, -1,
|
|
|
|
-1, -1, -1, -1, 2, -1, 0, -1,
|
|
|
|
1, -1, -1, -1, 3, 4, -1, 0,
|
|
|
|
2, -1, -1, -1, -1, -1, 4, -1,
|
|
|
|
-1, 2, 3, -1, -1, -1, 5, 6,
|
|
|
|
6, -1, 4, -1, -1, -1, -1, -1,
|
|
|
|
7, 0, -1, 4, 5, -1, -1, -1,
|
|
|
|
-1, -1, 0, -1, 6, -1, -1, -1
|
|
|
|
};
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
int *oldPosition = moveTable;
|
|
|
|
int *tempPosition = moveTable;
|
|
|
|
int *curPosition = moveTable + 1;
|
2011-11-04 19:01:57 +00:00
|
|
|
tableSize = 1;
|
2007-07-29 16:33:11 +00:00
|
|
|
|
|
|
|
while (*curPosition != 8) {
|
|
|
|
if (*oldPosition == facingTable[*curPosition]) {
|
2011-11-04 19:01:57 +00:00
|
|
|
tableSize -= 2;
|
2007-07-29 16:33:11 +00:00
|
|
|
*oldPosition = 9;
|
|
|
|
*curPosition = 9;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
while (tempPosition != moveTable) {
|
|
|
|
--tempPosition;
|
|
|
|
if (*tempPosition != 9)
|
|
|
|
break;
|
|
|
|
}
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
if (tempPosition == moveTable && *tempPosition == 9) {
|
2011-11-04 19:01:57 +00:00
|
|
|
while (*tempPosition == 9)
|
2007-07-29 16:33:11 +00:00
|
|
|
++tempPosition;
|
|
|
|
|
|
|
|
if (*tempPosition == 8)
|
|
|
|
return 0;
|
|
|
|
}
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
oldPosition = tempPosition;
|
2011-11-04 19:01:57 +00:00
|
|
|
curPosition = oldPosition + 1;
|
2007-07-29 16:33:11 +00:00
|
|
|
|
2011-11-04 19:01:57 +00:00
|
|
|
while (*curPosition == 9)
|
2007-07-29 16:33:11 +00:00
|
|
|
++curPosition;
|
2011-11-04 19:01:57 +00:00
|
|
|
} else if (unkTable[*curPosition + *oldPosition * 8] != -1) {
|
|
|
|
--tableSize;
|
|
|
|
*oldPosition = unkTable[*curPosition + *oldPosition * 8];
|
2007-07-29 16:33:11 +00:00
|
|
|
*curPosition = 9;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-29 16:33:11 +00:00
|
|
|
if (tempPosition != oldPosition) {
|
|
|
|
curPosition = oldPosition;
|
|
|
|
oldPosition = tempPosition;
|
2011-11-04 19:01:57 +00:00
|
|
|
while (tempPosition != moveTable) {
|
2007-07-29 16:33:11 +00:00
|
|
|
--tempPosition;
|
|
|
|
if (*tempPosition != 9)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2011-11-04 19:01:57 +00:00
|
|
|
do {
|
2007-07-29 16:33:11 +00:00
|
|
|
++curPosition;
|
2011-11-04 19:01:57 +00:00
|
|
|
} while (*curPosition == 9);
|
2007-07-29 16:33:11 +00:00
|
|
|
}
|
2011-11-04 19:01:57 +00:00
|
|
|
} else {
|
|
|
|
tempPosition = oldPosition;
|
|
|
|
oldPosition = curPosition;
|
|
|
|
++tableSize;
|
2007-07-29 16:33:11 +00:00
|
|
|
|
2011-11-04 19:01:57 +00:00
|
|
|
do {
|
|
|
|
++curPosition;
|
|
|
|
} while (*curPosition == 9);
|
2007-07-29 16:33:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 19:01:57 +00:00
|
|
|
return tableSize;
|
2007-07-29 16:33:11 +00:00
|
|
|
}
|
|
|
|
|
2009-10-04 21:26:33 +00:00
|
|
|
} // End of namespace Kyra
|