2002-08-12 14:54:57 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2001-10-09 14:30:12 +00:00
|
|
|
* Copyright (C) 2001 Ludvig Strigeus
|
2003-03-06 21:46:56 +00:00
|
|
|
* Copyright (C) 2001-2003 The ScummVM project
|
2001-10-09 14:30:12 +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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
2001-11-06 20:00:47 +00:00
|
|
|
* $Header$
|
2001-10-09 14:30:12 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "scumm.h"
|
2002-05-14 23:32:34 +00:00
|
|
|
#include "actor.h"
|
2003-07-02 13:47:03 +00:00
|
|
|
#include "boxes.h"
|
2002-09-08 01:08:12 +00:00
|
|
|
#include "common/util.h"
|
2002-07-08 01:04:29 +00:00
|
|
|
|
2002-05-15 07:38:33 +00:00
|
|
|
#include <math.h>
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-07-16 17:59:28 +00:00
|
|
|
#if !defined(__GNUC__)
|
|
|
|
#pragma START_PACK_STRUCTS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct Box { /* Internal walkbox file format */
|
2002-12-30 01:17:41 +00:00
|
|
|
union {
|
2003-05-09 21:46:34 +00:00
|
|
|
struct {
|
|
|
|
byte uy;
|
|
|
|
byte ly;
|
|
|
|
byte ulx;
|
|
|
|
byte urx;
|
|
|
|
byte llx;
|
|
|
|
byte lrx;
|
|
|
|
byte mask;
|
|
|
|
byte flags;
|
|
|
|
} GCC_PACK v2;
|
|
|
|
|
2002-12-30 01:17:41 +00:00
|
|
|
struct {
|
|
|
|
int16 ulx, uly;
|
|
|
|
int16 urx, ury;
|
|
|
|
int16 lrx, lry;
|
|
|
|
int16 llx, lly;
|
|
|
|
byte mask;
|
|
|
|
byte flags;
|
|
|
|
uint16 scale;
|
|
|
|
} GCC_PACK old;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
int32 ulx, uly;
|
|
|
|
int32 urx, ury;
|
|
|
|
int32 lrx, lry;
|
|
|
|
int32 llx, lly;
|
|
|
|
uint32 mask; // FIXME - is 'mask' really here?
|
|
|
|
uint32 flags; // FIXME - is 'flags' really here?
|
2003-01-13 01:29:45 +00:00
|
|
|
uint32 scaleSlot;
|
2002-12-30 01:17:41 +00:00
|
|
|
uint32 scale;
|
|
|
|
uint32 unk2;
|
|
|
|
uint32 unk3;
|
|
|
|
} GCC_PACK v8;
|
|
|
|
} GCC_PACK;
|
2002-07-16 17:59:28 +00:00
|
|
|
} GCC_PACK;
|
|
|
|
|
|
|
|
#if !defined(__GNUC__)
|
|
|
|
#pragma END_PACK_STRUCTS
|
|
|
|
#endif
|
|
|
|
|
2002-08-15 16:46:29 +00:00
|
|
|
#define BOX_MATRIX_SIZE 2000
|
2003-06-08 17:59:09 +00:00
|
|
|
#define BOX_DEBUG 0
|
2002-08-15 16:46:29 +00:00
|
|
|
|
2002-07-16 17:59:28 +00:00
|
|
|
|
2003-05-26 02:26:13 +00:00
|
|
|
static bool compareSlope(int X1, int Y1, int X2, int Y2, int X3, int Y3);
|
|
|
|
static ScummVM::Point closestPtOnLine(int ulx, int uly, int llx, int lly, int x, int y);
|
2002-07-16 17:59:28 +00:00
|
|
|
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
byte Scumm::getMaskFromBox(int box) {
|
2003-06-30 22:22:04 +00:00
|
|
|
// Fix for bug #740244 and #755863. This appears to have been a
|
2003-06-30 20:12:34 +00:00
|
|
|
// long standing bug in the original engine?
|
|
|
|
if (_version <= 3 && box == 255)
|
|
|
|
return 1;
|
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
Box *ptr = getBoxBaseAddr(box);
|
2002-03-26 05:23:06 +00:00
|
|
|
if (!ptr)
|
|
|
|
return 0;
|
2002-04-20 04:09:02 +00:00
|
|
|
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8)
|
2002-12-30 14:22:23 +00:00
|
|
|
return (byte) FROM_LE_32(ptr->v8.mask);
|
2003-06-07 00:13:26 +00:00
|
|
|
else if (_version <= 2)
|
2003-05-09 21:46:34 +00:00
|
|
|
return ptr->v2.mask;
|
2002-12-30 01:17:41 +00:00
|
|
|
else
|
|
|
|
return ptr->old.mask;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
void Scumm::setBoxFlags(int box, int val) {
|
2002-08-20 09:58:35 +00:00
|
|
|
debug(2, "setBoxFlags(%d, 0x%02x)", box, val);
|
2002-08-15 16:46:29 +00:00
|
|
|
|
|
|
|
/* FULL_THROTTLE stuff */
|
|
|
|
if (val & 0xC000) {
|
|
|
|
assert(box >= 0 && box < 65);
|
|
|
|
_extraBoxFlags[box] = val;
|
|
|
|
} else {
|
2003-05-09 21:46:34 +00:00
|
|
|
Box *ptr = getBoxBaseAddr(box);
|
|
|
|
assert(ptr);
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8)
|
2003-05-09 21:46:34 +00:00
|
|
|
ptr->v8.flags = TO_LE_32(val);
|
2003-06-07 00:13:26 +00:00
|
|
|
else if (_version <= 2)
|
2003-05-09 21:46:34 +00:00
|
|
|
ptr->v2.flags = val;
|
2002-12-30 01:17:41 +00:00
|
|
|
else
|
2003-05-09 21:46:34 +00:00
|
|
|
ptr->old.flags = val;
|
2002-08-15 16:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
byte Scumm::getBoxFlags(int box) {
|
2001-10-09 14:30:12 +00:00
|
|
|
Box *ptr = getBoxBaseAddr(box);
|
2002-03-26 05:23:06 +00:00
|
|
|
if (!ptr)
|
|
|
|
return 0;
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8)
|
2002-12-30 14:22:23 +00:00
|
|
|
return (byte) FROM_LE_32(ptr->v8.flags);
|
2003-06-07 00:13:26 +00:00
|
|
|
else if (_version <= 2)
|
2003-05-09 21:46:34 +00:00
|
|
|
return ptr->v2.flags;
|
2002-12-30 01:17:41 +00:00
|
|
|
else
|
|
|
|
return ptr->old.flags;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
void Scumm::setBoxScale(int box, int scale) {
|
2003-05-09 21:46:34 +00:00
|
|
|
Box *ptr = getBoxBaseAddr(box);
|
2003-05-18 19:44:22 +00:00
|
|
|
assert(ptr);
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8)
|
2003-05-09 21:46:34 +00:00
|
|
|
ptr->v8.scale = TO_LE_32(scale);
|
2003-06-07 00:13:26 +00:00
|
|
|
else if (_version <= 2)
|
2003-05-09 21:46:34 +00:00
|
|
|
error("This should not ever be called!");
|
2002-12-30 01:17:41 +00:00
|
|
|
else
|
2003-05-09 21:46:34 +00:00
|
|
|
ptr->old.scale = TO_LE_16(scale);
|
2002-08-15 16:46:29 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
void Scumm::setBoxScaleSlot(int box, int slot) {
|
2003-05-18 19:44:22 +00:00
|
|
|
Box *ptr = getBoxBaseAddr(box);
|
|
|
|
assert(ptr);
|
|
|
|
ptr->v8.scaleSlot = TO_LE_32(slot);
|
2003-01-13 14:04:41 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
int Scumm::getScale(int box, int x, int y) {
|
2003-05-18 19:44:22 +00:00
|
|
|
if (_features & GF_NO_SCALLING)
|
|
|
|
return 255;
|
|
|
|
|
2003-01-13 01:29:45 +00:00
|
|
|
Box *ptr = getBoxBaseAddr(box);
|
2003-05-18 19:44:22 +00:00
|
|
|
if (!ptr)
|
|
|
|
return 255;
|
2003-01-13 01:29:45 +00:00
|
|
|
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8) {
|
2003-01-13 01:29:45 +00:00
|
|
|
int slot = FROM_LE_32(ptr->v8.scaleSlot);
|
|
|
|
if (slot) {
|
2003-01-13 13:37:43 +00:00
|
|
|
assert(1 <= slot && slot <= 20);
|
2003-01-13 01:29:45 +00:00
|
|
|
int scaleX = 0, scaleY = 0;
|
2003-01-13 13:37:43 +00:00
|
|
|
ScaleSlot &s = _scaleSlots[slot-1];
|
2003-01-13 01:29:45 +00:00
|
|
|
|
|
|
|
if (s.y1 == s.y2 && s.x1 == s.x2)
|
|
|
|
error("Invalid scale slot %d", slot);
|
2003-03-06 17:58:13 +00:00
|
|
|
|
2003-01-13 01:29:45 +00:00
|
|
|
if (s.y1 != s.y2) {
|
|
|
|
if (y < 0)
|
|
|
|
y = 0;
|
2003-03-06 17:58:13 +00:00
|
|
|
|
2003-01-13 01:29:45 +00:00
|
|
|
scaleY = (s.scale2 - s.scale1) * (y - s.y1) / (s.y2 - s.y1) + s.scale1;
|
|
|
|
if (s.x1 == s.x2) {
|
|
|
|
return scaleY;
|
|
|
|
}
|
|
|
|
}
|
2003-03-06 17:58:13 +00:00
|
|
|
|
2003-01-13 01:29:45 +00:00
|
|
|
scaleX = (s.scale2 - s.scale1) * (x - s.x1) / (s.x2 - s.x1) + s.scale1;
|
2003-03-06 17:58:13 +00:00
|
|
|
|
2003-01-13 01:29:45 +00:00
|
|
|
if (s.y1 == s.y2) {
|
|
|
|
return scaleX;
|
|
|
|
} else {
|
|
|
|
return (scaleX + scaleY - s.x1) / 2;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
return FROM_LE_32(ptr->v8.scale);
|
|
|
|
} else {
|
2003-04-30 12:57:46 +00:00
|
|
|
uint16 scale = READ_LE_UINT16(&ptr->old.scale);
|
2003-01-13 01:29:45 +00:00
|
|
|
|
|
|
|
if (scale & 0x8000) {
|
|
|
|
scale = (scale & 0x7FFF) + 1;
|
|
|
|
byte *resptr = getResourceAddress(rtScaleTable, scale);
|
|
|
|
if (resptr == NULL)
|
|
|
|
error("Scale table %d not defined", scale);
|
2003-05-10 21:49:59 +00:00
|
|
|
if (y >= _screenHeight)
|
|
|
|
y = _screenHeight - 1;
|
2003-01-13 01:29:45 +00:00
|
|
|
else if (y < 0)
|
|
|
|
y = 0;
|
|
|
|
scale = resptr[y];
|
|
|
|
}
|
|
|
|
|
|
|
|
return scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
int Scumm::getBoxScale(int box) {
|
2002-04-11 17:19:16 +00:00
|
|
|
if (_features & GF_NO_SCALLING)
|
2002-12-30 01:17:41 +00:00
|
|
|
return 255;
|
2001-10-09 14:30:12 +00:00
|
|
|
Box *ptr = getBoxBaseAddr(box);
|
2002-05-09 20:04:53 +00:00
|
|
|
if (!ptr)
|
2002-03-26 05:23:06 +00:00
|
|
|
return 255;
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8)
|
2002-12-30 01:17:41 +00:00
|
|
|
return FROM_LE_32(ptr->v8.scale);
|
|
|
|
else
|
2003-04-30 12:57:46 +00:00
|
|
|
return READ_LE_UINT16(&ptr->old.scale);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-07-19 18:18:01 +00:00
|
|
|
/*
|
|
|
|
FIXME: It seems that scale items and scale slots are the same thing after
|
|
|
|
all - they only differ in some details (scale item is used to precompute
|
|
|
|
a scale table, while for the scale slots the computations are done on the
|
|
|
|
fly; also for scale slots, the scale along the x axis can vary, too).
|
|
|
|
|
|
|
|
Now, there are various known scale glitches in FT (and apparently also
|
|
|
|
in The Dig, see FIXME comments in Actor::setupActorScale). In this context
|
|
|
|
it is very interesting that for V5, there is an opcode which invokes
|
|
|
|
setScaleItem, and for V8 one that invokes setScaleSlot. But there is no
|
|
|
|
such opcode to be found for V6/V7 games.
|
|
|
|
|
|
|
|
Hypothesis: we simple are missing this opcode, and implementing it might
|
|
|
|
fix some or all of the Dig/FT scaling issues.
|
|
|
|
*/
|
|
|
|
void Scumm::setScaleItem(int slot, int y1, int scale1, int y2, int scale2) {
|
|
|
|
byte *ptr;
|
|
|
|
int y, tmp;
|
|
|
|
|
|
|
|
if (y1 == y2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ptr = createResource(rtScaleTable, slot, 200);
|
|
|
|
|
|
|
|
for (y = 0; y < 200; y++) {
|
|
|
|
tmp = ((scale2 - scale1) * (y - y1)) / (y2 - y1) + scale1;
|
|
|
|
if (tmp < 1)
|
|
|
|
tmp = 1;
|
|
|
|
if (tmp > 255)
|
|
|
|
tmp = 255;
|
|
|
|
*ptr++ = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scumm::setScaleSlot(int slot, int x1, int y1, int scale1, int x2, int y2, int scale2) {
|
|
|
|
assert(1 <= slot && slot <= 20);
|
|
|
|
_scaleSlots[slot-1].x2 = x2;
|
|
|
|
_scaleSlots[slot-1].y2 = y2;
|
|
|
|
_scaleSlots[slot-1].scale2 = scale2;
|
|
|
|
_scaleSlots[slot-1].x1 = x1;
|
|
|
|
_scaleSlots[slot-1].y1 = y1;
|
|
|
|
_scaleSlots[slot-1].scale1 = scale1;
|
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
byte Scumm::getNumBoxes() {
|
2001-11-05 19:21:49 +00:00
|
|
|
byte *ptr = getResourceAddress(rtMatrix, 2);
|
2002-04-11 17:19:16 +00:00
|
|
|
if (!ptr)
|
|
|
|
return 0;
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8)
|
2002-12-30 14:22:23 +00:00
|
|
|
return (byte) READ_LE_UINT32(ptr);
|
2002-12-30 01:17:41 +00:00
|
|
|
else
|
|
|
|
return ptr[0];
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
Box *Scumm::getBoxBaseAddr(int box) {
|
2002-04-11 17:19:16 +00:00
|
|
|
byte *ptr = getResourceAddress(rtMatrix, 2);
|
2003-05-18 19:44:22 +00:00
|
|
|
if (!ptr || box == 255)
|
2002-04-11 17:19:16 +00:00
|
|
|
return NULL;
|
2003-05-18 19:44:22 +00:00
|
|
|
|
2003-04-27 22:21:22 +00:00
|
|
|
// FIXME: In "pass to adventure", the loom demo, when bobbin enters
|
|
|
|
// the tent to the elders, box = 2, but ptr[0] = 2 -> errors out.
|
|
|
|
// Hence we disable the check for now. Maybe in PASS (and other old games)
|
|
|
|
// we shouldn't subtract 1 from ptr[0] when performing the check?
|
2003-05-09 03:02:49 +00:00
|
|
|
// this also seems to be incorrect for atari st demo of zak
|
|
|
|
// and assumingly other v2 games
|
2003-07-13 19:50:35 +00:00
|
|
|
// The same happens in Indy3EGA (see bug #770351)
|
2003-07-15 19:20:40 +00:00
|
|
|
// Also happend in ZakEGA (see bug #771803).
|
|
|
|
//
|
|
|
|
// This *might* mean that we have a bug in our box implementation
|
|
|
|
// OTOH, the original engine, unlike ScummVM, performed no bound
|
|
|
|
// checking at all. All the problems so far have been cases where
|
|
|
|
// the value was exactly one more than what we consider the maximum.
|
|
|
|
// So it's very well possible that all of these are script errors.
|
|
|
|
if (_gameId == GID_MONKEY_EGA || _gameId == GID_INDY3 || _gameId == GID_ZAK) {
|
|
|
|
checkRange(ptr[0], 0, box, "Illegal box %d");
|
2003-05-18 19:44:22 +00:00
|
|
|
} else
|
2003-04-11 12:57:41 +00:00
|
|
|
checkRange(ptr[0] - 1, 0, box, "Illegal box %d");
|
2003-04-27 22:21:22 +00:00
|
|
|
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version <= 2)
|
2003-05-09 21:46:34 +00:00
|
|
|
return (Box *)(ptr + box * SIZEOF_BOX_V2 + 1);
|
2003-06-07 00:13:26 +00:00
|
|
|
else if (_version == 3)
|
2003-05-09 21:46:34 +00:00
|
|
|
return (Box *)(ptr + box * SIZEOF_BOX_V3 + 1);
|
|
|
|
else if (_features & GF_SMALL_HEADER)
|
|
|
|
return (Box *)(ptr + box * SIZEOF_BOX + 1);
|
2003-06-07 00:13:26 +00:00
|
|
|
else if (_version == 8)
|
2003-05-09 21:46:34 +00:00
|
|
|
return (Box *)(ptr + box * SIZEOF_BOX_V8 + 4);
|
|
|
|
else
|
2002-07-07 20:25:23 +00:00
|
|
|
return (Box *)(ptr + box * SIZEOF_BOX + 2);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
int Scumm::getSpecialBox(int x, int y) {
|
2002-04-25 08:53:10 +00:00
|
|
|
int i;
|
|
|
|
int numOfBoxes;
|
|
|
|
byte flag;
|
2002-07-07 20:25:23 +00:00
|
|
|
|
2002-04-25 08:53:10 +00:00
|
|
|
numOfBoxes = getNumBoxes() - 1;
|
|
|
|
|
2002-07-07 20:25:23 +00:00
|
|
|
for (i = numOfBoxes; i >= 0; i--) {
|
2002-04-25 08:53:10 +00:00
|
|
|
flag = getBoxFlags(i);
|
|
|
|
|
2002-08-15 16:46:29 +00:00
|
|
|
if (!(flag & kBoxInvisible) && (flag & kBoxPlayerOnly))
|
2002-07-07 20:25:23 +00:00
|
|
|
return (-1);
|
2002-04-25 08:53:10 +00:00
|
|
|
|
2002-12-30 01:40:12 +00:00
|
|
|
if (checkXYInBoxBounds(i, x, y))
|
2002-07-07 20:25:23 +00:00
|
|
|
return (i);
|
2002-04-25 08:53:10 +00:00
|
|
|
}
|
|
|
|
|
2002-07-07 20:25:23 +00:00
|
|
|
return (-1);
|
2002-04-25 08:53:10 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
bool Scumm::checkXYInBoxBounds(int b, int x, int y) {
|
2001-11-26 19:57:57 +00:00
|
|
|
BoxCoords box;
|
|
|
|
|
2003-05-20 20:42:28 +00:00
|
|
|
if (b < 0 || b == Actor::kInvalidBox)
|
2002-08-12 16:05:14 +00:00
|
|
|
return false;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
getBoxCoordinates(b, &box);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-07-16 17:59:28 +00:00
|
|
|
if (x < box.ul.x && x < box.ur.x && x < box.lr.x && x < box.ll.x)
|
2002-08-12 16:05:14 +00:00
|
|
|
return false;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-07-16 17:59:28 +00:00
|
|
|
if (x > box.ul.x && x > box.ur.x && x > box.lr.x && x > box.ll.x)
|
2002-08-12 16:05:14 +00:00
|
|
|
return false;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-07-16 17:59:28 +00:00
|
|
|
if (y < box.ul.y && y < box.ur.y && y < box.lr.y && y < box.ll.y)
|
2002-08-12 16:05:14 +00:00
|
|
|
return false;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-07-16 17:59:28 +00:00
|
|
|
if (y > box.ul.y && y > box.ur.y && y > box.lr.y && y > box.ll.y)
|
2002-08-12 16:05:14 +00:00
|
|
|
return false;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-07-16 17:59:28 +00:00
|
|
|
if (box.ul.x == box.ur.x && box.ul.y == box.ur.y && box.lr.x == box.ll.x && box.lr.y == box.ll.y ||
|
|
|
|
box.ul.x == box.ll.x && box.ul.y == box.ll.y && box.ur.x == box.lr.x && box.ur.y == box.lr.y) {
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2003-05-15 21:57:38 +00:00
|
|
|
ScummVM::Point pt;
|
2002-07-16 17:59:28 +00:00
|
|
|
pt = closestPtOnLine(box.ul.x, box.ul.y, box.lr.x, box.lr.y, x, y);
|
2002-04-11 17:19:16 +00:00
|
|
|
if (distanceFromPt(x, y, pt.x, pt.y) <= 4)
|
2002-08-12 16:05:14 +00:00
|
|
|
return true;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-05-23 21:22:08 +00:00
|
|
|
if (!compareSlope(box.ul.x, box.ul.y, box.ur.x, box.ur.y, x, y))
|
2002-08-12 16:05:14 +00:00
|
|
|
return false;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-07-16 17:59:28 +00:00
|
|
|
if (!compareSlope(box.ur.x, box.ur.y, box.lr.x, box.lr.y, x, y))
|
2002-08-12 16:05:14 +00:00
|
|
|
return false;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-07-16 17:59:28 +00:00
|
|
|
if (!compareSlope(box.ll.x, box.ll.y, x, y, box.lr.x, box.lr.y))
|
2002-08-12 16:05:14 +00:00
|
|
|
return false;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-07-16 17:59:28 +00:00
|
|
|
if (!compareSlope(box.ul.x, box.ul.y, x, y, box.ll.x, box.ll.y))
|
2002-08-12 16:05:14 +00:00
|
|
|
return false;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-08-12 16:05:14 +00:00
|
|
|
return true;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
void Scumm::getBoxCoordinates(int boxnum, BoxCoords *box) {
|
2001-11-26 19:57:57 +00:00
|
|
|
Box *bp = getBoxBaseAddr(boxnum);
|
2003-05-18 19:44:22 +00:00
|
|
|
assert(bp);
|
2002-03-07 05:18:55 +00:00
|
|
|
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version == 8) {
|
2003-05-16 00:07:47 +00:00
|
|
|
box->ul.x = (short)FROM_LE_32(bp->v8.ulx);
|
|
|
|
box->ul.y = (short)FROM_LE_32(bp->v8.uly);
|
|
|
|
box->ur.x = (short)FROM_LE_32(bp->v8.urx);
|
|
|
|
box->ur.y = (short)FROM_LE_32(bp->v8.ury);
|
2002-12-30 01:17:41 +00:00
|
|
|
|
2003-05-16 00:07:47 +00:00
|
|
|
box->ll.x = (short)FROM_LE_32(bp->v8.llx);
|
|
|
|
box->ll.y = (short)FROM_LE_32(bp->v8.lly);
|
|
|
|
box->lr.x = (short)FROM_LE_32(bp->v8.lrx);
|
|
|
|
box->lr.y = (short)FROM_LE_32(bp->v8.lry);
|
2003-02-16 16:54:17 +00:00
|
|
|
|
|
|
|
// FIXME: Some walkboxes in CMI appear to have been flipped,
|
|
|
|
// in the sense that for instance the lower boundary is above
|
|
|
|
// the upper one. Can that really be the case, or is there
|
|
|
|
// some more sinister problem afoot?
|
|
|
|
//
|
|
|
|
// Is this fix sufficient, or will we need something more
|
|
|
|
// elaborate?
|
|
|
|
|
|
|
|
if (box->ul.y > box->ll.y && box->ur.y > box->lr.y) {
|
|
|
|
SWAP(box->ul.x, box->ll.x);
|
|
|
|
SWAP(box->ul.y, box->ll.y);
|
|
|
|
SWAP(box->ur.x, box->lr.x);
|
|
|
|
SWAP(box->ur.y, box->lr.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (box->ul.x > box->ur.x && box->ll.x > box->lr.x) {
|
|
|
|
SWAP(box->ul.x, box->ur.x);
|
|
|
|
SWAP(box->ul.y, box->ur.y);
|
|
|
|
SWAP(box->ll.x, box->lr.x);
|
|
|
|
SWAP(box->ll.y, box->lr.y);
|
|
|
|
}
|
2003-06-07 00:13:26 +00:00
|
|
|
} else if (_version <= 2) {
|
2003-05-10 23:47:39 +00:00
|
|
|
box->ul.x = bp->v2.ulx * 8;
|
|
|
|
box->ul.y = bp->v2.uy * 2;
|
2003-07-12 18:46:33 +00:00
|
|
|
box->ur.x = bp->v2.urx * 8;
|
2003-05-10 23:47:39 +00:00
|
|
|
box->ur.y = bp->v2.uy * 2;
|
2003-05-09 21:46:34 +00:00
|
|
|
|
2003-05-10 23:47:39 +00:00
|
|
|
box->ll.x = bp->v2.llx * 8;
|
2003-07-12 18:46:33 +00:00
|
|
|
box->ll.y = bp->v2.ly * 2;
|
|
|
|
box->lr.x = bp->v2.lrx * 8;
|
|
|
|
box->lr.y = bp->v2.ly * 2;
|
2002-12-30 01:17:41 +00:00
|
|
|
} else {
|
2003-04-30 12:57:46 +00:00
|
|
|
box->ul.x = (int16)READ_LE_UINT16(&bp->old.ulx);
|
|
|
|
box->ul.y = (int16)READ_LE_UINT16(&bp->old.uly);
|
|
|
|
box->ur.x = (int16)READ_LE_UINT16(&bp->old.urx);
|
|
|
|
box->ur.y = (int16)READ_LE_UINT16(&bp->old.ury);
|
2002-12-30 01:17:41 +00:00
|
|
|
|
2003-04-30 12:57:46 +00:00
|
|
|
box->ll.x = (int16)READ_LE_UINT16(&bp->old.llx);
|
|
|
|
box->ll.y = (int16)READ_LE_UINT16(&bp->old.lly);
|
|
|
|
box->lr.x = (int16)READ_LE_UINT16(&bp->old.lrx);
|
|
|
|
box->lr.y = (int16)READ_LE_UINT16(&bp->old.lry);
|
2002-12-30 01:17:41 +00:00
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
uint Scumm::distanceFromPt(int x, int y, int ptx, int pty) {
|
2001-10-09 14:30:12 +00:00
|
|
|
int diffx, diffy;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
|
|
|
diffx = abs(ptx - x);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2003-07-02 13:47:03 +00:00
|
|
|
if (diffx >= 0x1000)
|
|
|
|
return 0xFFFFFF;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2001-10-11 10:45:39 +00:00
|
|
|
diffy = abs(pty - y);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2003-07-02 13:47:03 +00:00
|
|
|
if (diffy >= 0x1000)
|
|
|
|
return 0xFFFFFF;
|
2001-10-09 14:30:12 +00:00
|
|
|
diffx *= diffx;
|
|
|
|
diffy *= diffy;
|
|
|
|
return diffx + diffy;
|
|
|
|
}
|
|
|
|
|
2003-05-26 02:26:13 +00:00
|
|
|
|
|
|
|
bool compareSlope(int X1, int Y1, int X2, int Y2, int X3, int Y3) {
|
|
|
|
return (Y2 - Y1) * (X3 - X1) <= (Y3 - Y1) * (X2 - X1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ScummVM::Point closestPtOnLine(int ulx, int uly, int llx, int lly, int x, int y) {
|
2002-04-11 17:19:16 +00:00
|
|
|
int lydiff, lxdiff;
|
|
|
|
int32 dist, a, b, c;
|
|
|
|
int x2, y2;
|
2003-05-15 21:57:38 +00:00
|
|
|
ScummVM::Point pt;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-07-16 14:54:49 +00:00
|
|
|
if (llx == ulx) { // Vertical line?
|
2001-10-09 14:30:12 +00:00
|
|
|
x2 = ulx;
|
|
|
|
y2 = y;
|
2002-07-16 14:54:49 +00:00
|
|
|
} else if (lly == uly) { // Horizontal line?
|
2001-10-09 14:30:12 +00:00
|
|
|
x2 = x;
|
|
|
|
y2 = uly;
|
|
|
|
} else {
|
|
|
|
lydiff = lly - uly;
|
|
|
|
|
|
|
|
lxdiff = llx - ulx;
|
|
|
|
|
|
|
|
if (abs(lxdiff) > abs(lydiff)) {
|
|
|
|
dist = lxdiff * lxdiff + lydiff * lydiff;
|
|
|
|
|
|
|
|
a = ulx * lydiff / lxdiff;
|
|
|
|
b = x * lxdiff / lydiff;
|
|
|
|
|
|
|
|
c = (a + b - uly + y) * lydiff * lxdiff / dist;
|
|
|
|
|
|
|
|
x2 = c;
|
|
|
|
y2 = c * lydiff / lxdiff - a + uly;
|
|
|
|
} else {
|
|
|
|
dist = lydiff * lydiff + lxdiff * lxdiff;
|
|
|
|
|
|
|
|
a = uly * lxdiff / lydiff;
|
|
|
|
b = y * lydiff / lxdiff;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
c = (a + b - ulx + x) * lydiff * lxdiff / dist;
|
|
|
|
|
|
|
|
y2 = c;
|
|
|
|
x2 = c * lxdiff / lydiff - a + ulx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lxdiff = llx - ulx;
|
|
|
|
lydiff = lly - uly;
|
|
|
|
|
|
|
|
if (abs(lydiff) < abs(lxdiff)) {
|
|
|
|
if (lxdiff > 0) {
|
|
|
|
if (x2 < ulx) {
|
2002-04-11 17:19:16 +00:00
|
|
|
type1:;
|
2001-10-09 14:30:12 +00:00
|
|
|
x2 = ulx;
|
|
|
|
y2 = uly;
|
|
|
|
} else if (x2 > llx) {
|
2002-04-11 17:19:16 +00:00
|
|
|
type2:;
|
2001-10-09 14:30:12 +00:00
|
|
|
x2 = llx;
|
|
|
|
y2 = lly;
|
|
|
|
}
|
|
|
|
} else {
|
2002-04-11 17:19:16 +00:00
|
|
|
if (x2 > ulx)
|
|
|
|
goto type1;
|
|
|
|
if (x2 < llx)
|
|
|
|
goto type2;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (lydiff > 0) {
|
2002-04-11 17:19:16 +00:00
|
|
|
if (y2 < uly)
|
|
|
|
goto type1;
|
|
|
|
if (y2 > lly)
|
|
|
|
goto type2;
|
2001-10-09 14:30:12 +00:00
|
|
|
} else {
|
2002-04-11 17:19:16 +00:00
|
|
|
if (y2 > uly)
|
|
|
|
goto type1;
|
|
|
|
if (y2 < lly)
|
|
|
|
goto type2;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pt.x = x2;
|
|
|
|
pt.y = y2;
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
bool Scumm::inBoxQuickReject(int b, int x, int y, int threshold) {
|
2001-10-09 14:30:12 +00:00
|
|
|
int t;
|
2001-11-26 19:57:57 +00:00
|
|
|
BoxCoords box;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
getBoxCoordinates(b, &box);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
|
|
|
t = x - threshold;
|
2002-07-16 17:59:28 +00:00
|
|
|
if (t > box.ul.x && t > box.ur.x && t > box.lr.x && t > box.ll.x)
|
2003-07-02 13:47:03 +00:00
|
|
|
return true;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
t = x + threshold;
|
2002-07-16 17:59:28 +00:00
|
|
|
if (t < box.ul.x && t < box.ur.x && t < box.lr.x && t < box.ll.x)
|
2003-07-02 13:47:03 +00:00
|
|
|
return true;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
t = y - threshold;
|
2002-07-16 17:59:28 +00:00
|
|
|
if (t > box.ul.y && t > box.ur.y && t > box.lr.y && t > box.ll.y)
|
2003-07-02 13:47:03 +00:00
|
|
|
return true;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
t = y + threshold;
|
2002-07-16 17:59:28 +00:00
|
|
|
if (t < box.ul.y && t < box.ur.y && t < box.lr.y && t < box.ll.y)
|
2003-07-02 13:47:03 +00:00
|
|
|
return true;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2003-07-02 13:47:03 +00:00
|
|
|
return false;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-07-02 13:47:03 +00:00
|
|
|
int Scumm::getClosestPtOnBox(int b, int x, int y, int16& outX, int16& outY) {
|
2003-05-15 21:57:38 +00:00
|
|
|
ScummVM::Point pt;
|
2001-10-09 14:30:12 +00:00
|
|
|
uint dist;
|
2003-07-02 13:47:03 +00:00
|
|
|
uint bestdist = 0xFFFFFF;
|
2001-11-26 19:57:57 +00:00
|
|
|
BoxCoords box;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
getBoxCoordinates(b, &box);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
pt = closestPtOnLine(box.ul.x, box.ul.y, box.ur.x, box.ur.y, x, y);
|
2001-10-09 14:30:12 +00:00
|
|
|
dist = distanceFromPt(x, y, pt.x, pt.y);
|
|
|
|
if (dist < bestdist) {
|
|
|
|
bestdist = dist;
|
2003-07-02 13:47:03 +00:00
|
|
|
outX = pt.x;
|
|
|
|
outY = pt.y;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 17:59:28 +00:00
|
|
|
pt = closestPtOnLine(box.ur.x, box.ur.y, box.lr.x, box.lr.y, x, y);
|
2001-10-09 14:30:12 +00:00
|
|
|
dist = distanceFromPt(x, y, pt.x, pt.y);
|
|
|
|
if (dist < bestdist) {
|
|
|
|
bestdist = dist;
|
2003-07-02 13:47:03 +00:00
|
|
|
outX = pt.x;
|
|
|
|
outY = pt.y;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 17:59:28 +00:00
|
|
|
pt = closestPtOnLine(box.lr.x, box.lr.y, box.ll.x, box.ll.y, x, y);
|
2001-10-09 14:30:12 +00:00
|
|
|
dist = distanceFromPt(x, y, pt.x, pt.y);
|
|
|
|
if (dist < bestdist) {
|
|
|
|
bestdist = dist;
|
2003-07-02 13:47:03 +00:00
|
|
|
outX = pt.x;
|
|
|
|
outY = pt.y;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 17:59:28 +00:00
|
|
|
pt = closestPtOnLine(box.ll.x, box.ll.y, box.ul.x, box.ul.y, x, y);
|
2001-10-09 14:30:12 +00:00
|
|
|
dist = distanceFromPt(x, y, pt.x, pt.y);
|
|
|
|
if (dist < bestdist) {
|
|
|
|
bestdist = dist;
|
2003-07-02 13:47:03 +00:00
|
|
|
outX = pt.x;
|
|
|
|
outY = pt.y;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2003-07-02 13:47:03 +00:00
|
|
|
return bestdist;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
byte *Scumm::getBoxMatrixBaseAddr() {
|
2001-12-27 17:51:58 +00:00
|
|
|
byte *ptr = getResourceAddress(rtMatrix, 1);
|
2003-07-21 21:59:07 +00:00
|
|
|
assert(ptr);
|
2002-04-11 17:19:16 +00:00
|
|
|
if (*ptr == 0xFF)
|
|
|
|
ptr++;
|
2001-10-09 14:30:12 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2002-08-12 16:05:14 +00:00
|
|
|
/*
|
|
|
|
* Compute if there is a way that connects box 'from' with box 'to'.
|
|
|
|
* Returns the number of a box adjactant to 'from' that is the next on the
|
|
|
|
* way to 'to' (this can be 'to' itself or a third box).
|
|
|
|
* If there is no connection -1 is return.
|
|
|
|
*/
|
2003-03-06 17:58:13 +00:00
|
|
|
int Scumm::getPathToDestBox(byte from, byte to) {
|
2003-07-21 21:59:07 +00:00
|
|
|
const byte *boxm;
|
2001-11-26 19:57:57 +00:00
|
|
|
byte i;
|
2003-05-19 00:17:34 +00:00
|
|
|
const int numOfBoxes = getNumBoxes();
|
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
if (from == to)
|
2001-10-09 14:30:12 +00:00
|
|
|
return to;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2003-05-20 20:42:28 +00:00
|
|
|
if (to == Actor::kInvalidBox)
|
2003-05-20 12:54:04 +00:00
|
|
|
return -1;
|
|
|
|
|
2003-05-20 20:42:28 +00:00
|
|
|
if (from == Actor::kInvalidBox)
|
2003-05-20 12:54:04 +00:00
|
|
|
return to;
|
|
|
|
|
|
|
|
assert(from < numOfBoxes);
|
2003-05-19 00:17:34 +00:00
|
|
|
assert(to < numOfBoxes);
|
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
boxm = getBoxMatrixBaseAddr();
|
|
|
|
|
2003-06-07 00:13:26 +00:00
|
|
|
if (_version <= 2) {
|
2003-05-19 20:36:39 +00:00
|
|
|
// The v2 box matrix is a real matrix with numOfBoxes rows and columns.
|
|
|
|
// The first numOfBoxes bytes contain indices to the start of the corresponding
|
|
|
|
// row (although that seems unnecessary to me - the value is easily computable.
|
2003-05-19 00:25:26 +00:00
|
|
|
boxm += numOfBoxes + boxm[from];
|
2003-07-21 21:59:07 +00:00
|
|
|
return (int8)boxm[to];
|
2003-05-19 00:17:34 +00:00
|
|
|
}
|
|
|
|
|
2003-07-21 21:59:07 +00:00
|
|
|
// WORKAROUND #1: It seems that in some cases, the box matrix is corrupt
|
|
|
|
// (more precisely, is too short) in the datafiles already. In
|
|
|
|
// particular this seems to be the case in room 46 of Indy3 EGA (see
|
|
|
|
// also bug #770690). This didn't cause problems in the original
|
|
|
|
// engine, because there, the memory layout is different. After the
|
|
|
|
// walkbox would follow the rest of the room file, thus the program
|
|
|
|
// always behaved the same (and by chance, correct). Not so for us,
|
|
|
|
// since random data may follow after the resource in ScummVM.
|
|
|
|
//
|
|
|
|
// As a workaround, we add a check for the end of the box matrix
|
|
|
|
// resource, and abort the search once we reach the end.
|
|
|
|
const byte *end = boxm + getResourceSize(rtMatrix, 1);
|
|
|
|
|
|
|
|
// WORKAROUND #2: In addition to the above, we have to add this special
|
|
|
|
// case to fix the scene in Indy3 where Indy meets Hitler in Berlin.
|
|
|
|
// It's one of the places (or maybe even the only one?). See bug #770690
|
|
|
|
if (_gameId == GID_INDY3 && _roomResource == 46 && from == 1 && to == 0)
|
|
|
|
return 0;
|
|
|
|
|
2003-05-19 20:36:39 +00:00
|
|
|
// Skip up to the matrix data for box 'from'
|
2003-07-21 21:59:07 +00:00
|
|
|
for (i = 0; i < from && boxm < end; i++) {
|
2001-10-09 14:30:12 +00:00
|
|
|
while (*boxm != 0xFF)
|
|
|
|
boxm += 3;
|
|
|
|
boxm++;
|
|
|
|
}
|
|
|
|
|
2003-05-19 20:36:39 +00:00
|
|
|
// Now search for the entry for box 'to'
|
2003-07-21 21:59:07 +00:00
|
|
|
while (boxm < end && boxm[0] != 0xFF) {
|
2002-12-29 21:14:28 +00:00
|
|
|
if (boxm[0] <= to && to <= boxm[1])
|
2003-07-21 21:59:07 +00:00
|
|
|
return (int8)boxm[2];
|
2002-04-11 17:19:16 +00:00
|
|
|
boxm += 3;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2003-07-21 21:59:07 +00:00
|
|
|
|
|
|
|
if (boxm >= end)
|
|
|
|
warning("The box matrix apparently is truncated (room %d)", _roomResource);
|
2003-05-19 20:36:39 +00:00
|
|
|
|
2003-07-21 21:59:07 +00:00
|
|
|
return -1;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-08-12 16:05:14 +00:00
|
|
|
/*
|
|
|
|
* Computes the next point actor a has to walk towards in a straight
|
|
|
|
* line in order to get from box1 to box3 via box2.
|
|
|
|
*/
|
2003-05-26 02:26:13 +00:00
|
|
|
bool Actor::findPathTowards(byte box1nr, byte box2nr, byte box3nr, int16 &foundPathX, int16 &foundPathY) {
|
2001-11-26 19:57:57 +00:00
|
|
|
BoxCoords box1;
|
|
|
|
BoxCoords box2;
|
2003-05-15 21:57:38 +00:00
|
|
|
ScummVM::Point tmp;
|
2002-04-11 17:19:16 +00:00
|
|
|
int i, j;
|
2001-11-26 19:57:57 +00:00
|
|
|
int flag;
|
2002-04-11 17:19:16 +00:00
|
|
|
int q, pos;
|
2001-11-26 19:57:57 +00:00
|
|
|
|
2003-05-26 02:26:13 +00:00
|
|
|
_vm->getBoxCoordinates(box1nr, &box1);
|
|
|
|
_vm->getBoxCoordinates(box2nr, &box2);
|
2001-11-26 19:57:57 +00:00
|
|
|
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
for (j = 0; j < 4; j++) {
|
2002-07-07 20:25:23 +00:00
|
|
|
if (box1.ul.x == box1.ur.x && box1.ul.x == box2.ul.x && box1.ul.x == box2.ur.x) {
|
2001-11-26 19:57:57 +00:00
|
|
|
flag = 0;
|
|
|
|
if (box1.ul.y > box1.ur.y) {
|
|
|
|
SWAP(box1.ul.y, box1.ur.y);
|
|
|
|
flag |= 1;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
|
|
|
|
if (box2.ul.y > box2.ur.y) {
|
|
|
|
SWAP(box2.ul.y, box2.ur.y);
|
|
|
|
flag |= 2;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
|
|
|
|
if (box1.ul.y > box2.ur.y || box2.ul.y > box1.ur.y ||
|
2002-04-11 17:19:16 +00:00
|
|
|
(box1.ur.y == box2.ul.y || box2.ur.y == box1.ul.y) &&
|
|
|
|
box1.ul.y != box1.ur.y && box2.ul.y != box2.ur.y) {
|
|
|
|
if (flag & 1)
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box1.ul.y, box1.ur.y);
|
2002-04-11 17:19:16 +00:00
|
|
|
if (flag & 2)
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box2.ul.y, box2.ur.y);
|
|
|
|
} else {
|
2003-05-26 02:26:13 +00:00
|
|
|
pos = y;
|
2001-11-26 19:57:57 +00:00
|
|
|
if (box2nr == box3nr) {
|
2003-05-26 02:26:13 +00:00
|
|
|
int diffX = walkdata.destx - x;
|
|
|
|
int diffY = walkdata.desty - y;
|
|
|
|
int boxDiffX = box1.ul.x - x;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
|
|
|
if (diffX != 0) {
|
2001-11-26 19:57:57 +00:00
|
|
|
int t;
|
|
|
|
|
|
|
|
diffY *= boxDiffX;
|
|
|
|
t = diffY / diffX;
|
2002-04-11 17:19:16 +00:00
|
|
|
if (t == 0 && (diffY <= 0 || diffX <= 0)
|
|
|
|
&& (diffY >= 0 || diffX >= 0))
|
2001-11-26 19:57:57 +00:00
|
|
|
t = -1;
|
2003-05-26 02:26:13 +00:00
|
|
|
pos = y + t;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
|
|
|
|
q = pos;
|
|
|
|
if (q < box2.ul.y)
|
2002-04-11 17:19:16 +00:00
|
|
|
q = box2.ul.y;
|
2001-11-26 19:57:57 +00:00
|
|
|
if (q > box2.ur.y)
|
|
|
|
q = box2.ur.y;
|
|
|
|
if (q < box1.ul.y)
|
|
|
|
q = box1.ul.y;
|
|
|
|
if (q > box1.ur.y)
|
|
|
|
q = box1.ur.y;
|
2002-04-11 17:19:16 +00:00
|
|
|
if (q == pos && box2nr == box3nr)
|
2002-08-12 16:05:14 +00:00
|
|
|
return true;
|
|
|
|
foundPathY = q;
|
|
|
|
foundPathX = box1.ul.x;
|
|
|
|
return false;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
|
2002-07-07 20:25:23 +00:00
|
|
|
if (box1.ul.y == box1.ur.y && box1.ul.y == box2.ul.y && box1.ul.y == box2.ur.y) {
|
2001-11-26 19:57:57 +00:00
|
|
|
flag = 0;
|
|
|
|
if (box1.ul.x > box1.ur.x) {
|
|
|
|
SWAP(box1.ul.x, box1.ur.x);
|
|
|
|
flag |= 1;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
|
|
|
|
if (box2.ul.x > box2.ur.x) {
|
|
|
|
SWAP(box2.ul.x, box2.ur.x);
|
|
|
|
flag |= 2;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
|
|
|
|
if (box1.ul.x > box2.ur.x || box2.ul.x > box1.ur.x ||
|
2002-04-11 17:19:16 +00:00
|
|
|
(box1.ur.x == box2.ul.x || box2.ur.x == box1.ul.x) &&
|
|
|
|
box1.ul.x != box1.ur.x && box2.ul.x != box2.ur.x) {
|
|
|
|
if (flag & 1)
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box1.ul.x, box1.ur.x);
|
2002-04-11 17:19:16 +00:00
|
|
|
if (flag & 2)
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box2.ul.x, box2.ur.x);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (box2nr == box3nr) {
|
2003-05-26 02:26:13 +00:00
|
|
|
int diffX = walkdata.destx - x;
|
|
|
|
int diffY = walkdata.desty - y;
|
|
|
|
int boxDiffY = box1.ul.y - y;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2003-05-26 02:26:13 +00:00
|
|
|
pos = x;
|
2002-04-11 17:19:16 +00:00
|
|
|
if (diffY != 0) {
|
2001-11-26 19:57:57 +00:00
|
|
|
pos += diffX * boxDiffY / diffY;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
2003-05-26 02:26:13 +00:00
|
|
|
pos = x;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
|
|
|
|
q = pos;
|
|
|
|
if (q < box2.ul.x)
|
|
|
|
q = box2.ul.x;
|
|
|
|
if (q > box2.ur.x)
|
|
|
|
q = box2.ur.x;
|
|
|
|
if (q < box1.ul.x)
|
|
|
|
q = box1.ul.x;
|
|
|
|
if (q > box1.ur.x)
|
|
|
|
q = box1.ur.x;
|
2002-04-11 17:19:16 +00:00
|
|
|
if (q == pos && box2nr == box3nr)
|
2002-08-12 16:05:14 +00:00
|
|
|
return true;
|
|
|
|
foundPathX = q;
|
|
|
|
foundPathY = box1.ul.y;
|
|
|
|
return false;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
tmp = box1.ul;
|
|
|
|
box1.ul = box1.ur;
|
2002-07-16 17:59:28 +00:00
|
|
|
box1.ur = box1.lr;
|
|
|
|
box1.lr = box1.ll;
|
|
|
|
box1.ll = tmp;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
tmp = box2.ul;
|
|
|
|
box2.ul = box2.ur;
|
2002-07-16 17:59:28 +00:00
|
|
|
box2.ur = box2.lr;
|
|
|
|
box2.lr = box2.ll;
|
|
|
|
box2.ll = tmp;
|
2001-11-26 19:57:57 +00:00
|
|
|
}
|
2002-08-12 16:05:14 +00:00
|
|
|
return false;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2002-08-12 16:05:14 +00:00
|
|
|
|
2003-06-08 17:59:09 +00:00
|
|
|
#if BOX_DEBUG
|
|
|
|
static void printMatrix(byte *boxm, int num) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
printf("%d: ", i);
|
|
|
|
while (*boxm != 0xFF) {
|
|
|
|
printf("%d, ", *boxm);
|
|
|
|
boxm++;
|
2003-05-31 16:20:20 +00:00
|
|
|
}
|
2003-06-08 17:59:09 +00:00
|
|
|
boxm++;
|
|
|
|
printf("\n");
|
2003-06-08 12:49:34 +00:00
|
|
|
}
|
2003-06-08 17:59:09 +00:00
|
|
|
}
|
2003-06-08 12:49:34 +00:00
|
|
|
|
2003-06-08 17:59:09 +00:00
|
|
|
static void printMatrix2(byte *matrix, int num) {
|
|
|
|
int i, j;
|
|
|
|
printf(" ");
|
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
printf("%2d ", i);
|
|
|
|
printf("\n");
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
printf("%2d: ", i);
|
|
|
|
for (j = 0; j < num; j++) {
|
|
|
|
int val = matrix[i * 64 + j];
|
2003-06-11 21:34:34 +00:00
|
|
|
if (val == Actor::kInvalidBox)
|
2003-06-08 17:59:09 +00:00
|
|
|
printf(" ? ");
|
|
|
|
else
|
|
|
|
printf("%2d ", val);
|
|
|
|
}
|
|
|
|
printf("\n");
|
2003-06-08 12:49:34 +00:00
|
|
|
}
|
2003-05-31 16:20:20 +00:00
|
|
|
}
|
2003-06-08 17:59:09 +00:00
|
|
|
#endif
|
2003-05-31 16:20:20 +00:00
|
|
|
|
2003-03-06 17:58:13 +00:00
|
|
|
void Scumm::createBoxMatrix() {
|
2003-06-08 17:59:09 +00:00
|
|
|
int num, i, j, k;
|
|
|
|
byte *adjacentMatrix, *itineraryMatrix;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2003-06-08 17:59:09 +00:00
|
|
|
// The total number of boxes
|
2001-10-09 14:30:12 +00:00
|
|
|
num = getNumBoxes();
|
2003-06-08 17:59:09 +00:00
|
|
|
assert(num <= 64);
|
|
|
|
|
|
|
|
// Allocate the adjacent & itinerary matrices
|
|
|
|
adjacentMatrix = (byte *)malloc(64 * 64);
|
|
|
|
itineraryMatrix = (byte *)malloc(64 * 64);
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2003-06-08 17:59:09 +00:00
|
|
|
// Initialise the adjacent matrix: each box has distance 0 to itself,
|
2002-12-29 21:14:28 +00:00
|
|
|
// and distance 1 to its direct neighbors. Initially, it has distance
|
2003-06-08 17:59:09 +00:00
|
|
|
// 255 (= infinity) to all other boxes.
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
for (j = 0; j < num; j++) {
|
|
|
|
if (i == j) {
|
2003-06-08 17:59:09 +00:00
|
|
|
adjacentMatrix[i * 64 + j] = 0;
|
|
|
|
itineraryMatrix[i * 64 + j] = j;
|
2001-10-09 14:30:12 +00:00
|
|
|
} else if (areBoxesNeighbours(i, j)) {
|
2003-06-08 17:59:09 +00:00
|
|
|
adjacentMatrix[i * 64 + j] = 1;
|
|
|
|
itineraryMatrix[i * 64 + j] = j;
|
2001-10-09 14:30:12 +00:00
|
|
|
} else {
|
2003-06-08 17:59:09 +00:00
|
|
|
adjacentMatrix[i * 64 + j] = 255;
|
2003-06-11 21:34:34 +00:00
|
|
|
itineraryMatrix[i * 64 + j] = Actor::kInvalidBox;
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2003-06-08 17:59:09 +00:00
|
|
|
// Compute the shortest routes between boxes via Kleene's algorithm.
|
|
|
|
// The original code used some kind of mangled Dijkstra's algorithm;
|
|
|
|
// while that might in theory be slightly faster, it was
|
|
|
|
// a) extremly obfuscated
|
|
|
|
// b) incorrect: it didn't always find the shortest paths
|
|
|
|
// c) not any faster in reality for our sparse & small adjacent matrices
|
2003-06-11 21:34:34 +00:00
|
|
|
for (k = 0; k < num; k++) {
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
for (j = 0; j < num; j++) {
|
2003-06-08 17:59:09 +00:00
|
|
|
if (i == j)
|
|
|
|
continue;
|
|
|
|
byte distIK = adjacentMatrix[64 * i + k];
|
|
|
|
byte distKJ = adjacentMatrix[64 * k + j];
|
|
|
|
if (adjacentMatrix[64 * i + j] > distIK + distKJ) {
|
|
|
|
adjacentMatrix[64 * i + j] = distIK + distKJ;
|
|
|
|
itineraryMatrix[64 * i + j] = itineraryMatrix[64 * i + k];
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
2003-06-08 17:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2003-06-08 17:59:09 +00:00
|
|
|
// "Compress" the distance matrix into the box matrix format used
|
|
|
|
// by the engine. The format is like this:
|
|
|
|
// For each box (from 0 to num) there is first a byte with value 0xFF,
|
|
|
|
// followed by an arbitrary number of byte triples; the end is marked
|
|
|
|
// again by the lead 0xFF for the next "row". The meaning of the
|
|
|
|
// byte triples is as follows: the first two bytes define a range
|
|
|
|
// of box numbers (e.g. 7-11), while the third byte defines an
|
|
|
|
// itineray box. Assuming we are in the 5th "row" and encounter
|
|
|
|
// the triplet 7,11,15: this means to get from box 5 to any of
|
|
|
|
// the boxes 7,8,9,10,11 the shortest way is to go via box 15.
|
|
|
|
// See also getPathToDestBox.
|
|
|
|
|
|
|
|
byte *matrixStart = createResource(rtMatrix, 1, BOX_MATRIX_SIZE);
|
|
|
|
const byte *matrixEnd = matrixStart + BOX_MATRIX_SIZE;
|
|
|
|
|
|
|
|
#define addToMatrix(b) do { *matrixStart++ = (b); assert(matrixStart < matrixEnd); } while (0)
|
|
|
|
|
2003-06-11 21:34:34 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
2003-06-08 17:59:09 +00:00
|
|
|
addToMatrix(0xFF);
|
2003-06-11 21:34:34 +00:00
|
|
|
for (j = 0; j < num; j++) {
|
2003-06-08 17:59:09 +00:00
|
|
|
byte itinerary = itineraryMatrix[64 * i + j];
|
2003-06-11 21:34:34 +00:00
|
|
|
if (itinerary != Actor::kInvalidBox) {
|
2003-06-08 17:59:09 +00:00
|
|
|
addToMatrix(j);
|
2003-06-12 06:53:05 +00:00
|
|
|
while (j < num - 1 && itinerary == itineraryMatrix[64 * i + (j + 1)])
|
2003-06-08 17:59:09 +00:00
|
|
|
j++;
|
|
|
|
addToMatrix(j);
|
|
|
|
addToMatrix(itinerary);
|
2002-12-29 21:14:28 +00:00
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
2003-06-08 17:59:09 +00:00
|
|
|
addToMatrix(0xFF);
|
2003-05-31 16:20:20 +00:00
|
|
|
|
2003-06-08 17:59:09 +00:00
|
|
|
|
|
|
|
#if BOX_DEBUG
|
|
|
|
printf("Itinerary matrix:\n");
|
|
|
|
printMatrix2(itineraryMatrix, num);
|
|
|
|
printf("compressed matrix:\n");
|
|
|
|
printMatrix(getBoxMatrixBaseAddr(), num);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
free(adjacentMatrix);
|
|
|
|
free(itineraryMatrix);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2003-06-08 12:49:34 +00:00
|
|
|
/** Check if two boxes are neighbours. */
|
2003-03-06 17:58:13 +00:00
|
|
|
bool Scumm::areBoxesNeighbours(int box1nr, int box2nr) {
|
2002-04-11 17:19:16 +00:00
|
|
|
int j, k, m, n;
|
2001-10-09 14:30:12 +00:00
|
|
|
int tmp_x, tmp_y;
|
|
|
|
bool result;
|
2001-11-26 19:57:57 +00:00
|
|
|
BoxCoords box;
|
|
|
|
BoxCoords box2;
|
2001-10-09 14:30:12 +00:00
|
|
|
|
2002-08-15 16:46:29 +00:00
|
|
|
if (getBoxFlags(box1nr) & kBoxInvisible || getBoxFlags(box2nr) & kBoxInvisible)
|
2001-10-09 14:30:12 +00:00
|
|
|
return false;
|
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
getBoxCoordinates(box1nr, &box2);
|
|
|
|
getBoxCoordinates(box2nr, &box);
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
result = false;
|
|
|
|
j = 4;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2001-10-09 14:30:12 +00:00
|
|
|
do {
|
|
|
|
k = 4;
|
|
|
|
do {
|
2002-07-07 20:25:23 +00:00
|
|
|
if (box2.ur.x == box2.ul.x && box.ul.x == box2.ul.x && box.ur.x == box2.ur.x) {
|
2001-10-09 14:30:12 +00:00
|
|
|
n = m = 0;
|
2001-11-26 19:57:57 +00:00
|
|
|
if (box2.ur.y < box2.ul.y) {
|
2001-10-09 14:30:12 +00:00
|
|
|
n = 1;
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box2.ur.y, box2.ul.y);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
if (box.ur.y < box.ul.y) {
|
2001-10-09 14:30:12 +00:00
|
|
|
m = 1;
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box.ur.y, box.ul.y);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
if (box.ur.y < box2.ul.y ||
|
2002-04-11 17:19:16 +00:00
|
|
|
box.ul.y > box2.ur.y ||
|
2001-11-26 19:57:57 +00:00
|
|
|
(box.ul.y == box2.ur.y ||
|
2002-07-07 20:25:23 +00:00
|
|
|
box.ur.y == box2.ul.y) && box2.ur.y != box2.ul.y && box.ul.y != box.ur.y) {
|
2001-10-09 14:30:12 +00:00
|
|
|
if (n) {
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box2.ur.y, box2.ul.y);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
if (m) {
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box.ur.y, box.ul.y);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (n) {
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box2.ur.y, box2.ul.y);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
if (m) {
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box.ur.y, box.ul.y);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
result = true;
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2002-07-07 20:25:23 +00:00
|
|
|
if (box2.ur.y == box2.ul.y && box.ul.y == box2.ul.y && box.ur.y == box2.ur.y) {
|
2001-10-09 14:30:12 +00:00
|
|
|
n = m = 0;
|
2001-11-26 19:57:57 +00:00
|
|
|
if (box2.ur.x < box2.ul.x) {
|
2001-10-09 14:30:12 +00:00
|
|
|
n = 1;
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box2.ur.x, box2.ul.x);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
if (box.ur.x < box.ul.x) {
|
2001-10-09 14:30:12 +00:00
|
|
|
m = 1;
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box.ur.x, box.ul.x);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
2001-11-26 19:57:57 +00:00
|
|
|
if (box.ur.x < box2.ul.x ||
|
2002-04-11 17:19:16 +00:00
|
|
|
box.ul.x > box2.ur.x ||
|
2001-11-26 19:57:57 +00:00
|
|
|
(box.ul.x == box2.ur.x ||
|
2002-07-07 20:25:23 +00:00
|
|
|
box.ur.x == box2.ul.x) && box2.ur.x != box2.ul.x && box.ul.x != box.ur.x) {
|
2001-10-09 14:30:12 +00:00
|
|
|
|
|
|
|
if (n) {
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box2.ur.x, box2.ul.x);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
if (m) {
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box.ur.x, box.ul.x);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (n) {
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box2.ur.x, box2.ul.x);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
if (m) {
|
2001-11-26 19:57:57 +00:00
|
|
|
SWAP(box.ur.x, box.ul.x);
|
2001-10-09 14:30:12 +00:00
|
|
|
}
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
tmp_x = box2.ul.x;
|
|
|
|
tmp_y = box2.ul.y;
|
|
|
|
box2.ul.x = box2.ur.x;
|
|
|
|
box2.ul.y = box2.ur.y;
|
2002-07-16 17:59:28 +00:00
|
|
|
box2.ur.x = box2.lr.x;
|
|
|
|
box2.ur.y = box2.lr.y;
|
|
|
|
box2.lr.x = box2.ll.x;
|
|
|
|
box2.lr.y = box2.ll.y;
|
|
|
|
box2.ll.x = tmp_x;
|
|
|
|
box2.ll.y = tmp_y;
|
2001-10-09 14:30:12 +00:00
|
|
|
} while (--k);
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2001-11-26 19:57:57 +00:00
|
|
|
tmp_x = box.ul.x;
|
|
|
|
tmp_y = box.ul.y;
|
|
|
|
box.ul.x = box.ur.x;
|
|
|
|
box.ul.y = box.ur.y;
|
2002-07-16 17:59:28 +00:00
|
|
|
box.ur.x = box.lr.x;
|
|
|
|
box.ur.y = box.lr.y;
|
|
|
|
box.lr.x = box.ll.x;
|
|
|
|
box.lr.y = box.ll.y;
|
|
|
|
box.ll.x = tmp_x;
|
|
|
|
box.ll.y = tmp_y;
|
2001-10-09 14:30:12 +00:00
|
|
|
} while (--j);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-05-26 02:26:13 +00:00
|
|
|
void Actor::findPathTowardsOld(byte trap1, byte trap2, byte final_trap, ScummVM::Point gateLoc[5]) {
|
2003-05-15 21:57:38 +00:00
|
|
|
ScummVM::Point pt;
|
|
|
|
ScummVM::Point gateA[2];
|
|
|
|
ScummVM::Point gateB[2];
|
2002-07-15 21:50:51 +00:00
|
|
|
|
2003-05-26 02:26:13 +00:00
|
|
|
_vm->getGates(trap1, trap2, gateA, gateB);
|
2002-03-11 13:15:07 +00:00
|
|
|
|
2003-05-26 02:26:13 +00:00
|
|
|
gateLoc[1].x = x;
|
|
|
|
gateLoc[1].y = y;
|
2002-05-14 19:11:20 +00:00
|
|
|
gateLoc[2].x = 32000;
|
|
|
|
gateLoc[3].x = 32000;
|
|
|
|
gateLoc[4].x = 32000;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
|
|
|
if (trap2 == final_trap) { /* next = final box? */
|
2003-05-26 02:26:13 +00:00
|
|
|
gateLoc[4].x = walkdata.destx;
|
|
|
|
gateLoc[4].y = walkdata.desty;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2003-05-26 02:26:13 +00:00
|
|
|
if (_vm->getMaskFromBox(trap1) == _vm->getMaskFromBox(trap2) || 1) {
|
2002-07-15 21:50:51 +00:00
|
|
|
if (compareSlope(gateLoc[1].x, gateLoc[1].y, gateLoc[4].x, gateLoc[4].y, gateA[0].x, gateA[0].y) !=
|
|
|
|
compareSlope(gateLoc[1].x, gateLoc[1].y, gateLoc[4].x, gateLoc[4].y, gateB[0].x, gateB[0].y) &&
|
|
|
|
compareSlope(gateLoc[1].x, gateLoc[1].y, gateLoc[4].x, gateLoc[4].y, gateA[1].x, gateA[1].y) !=
|
|
|
|
compareSlope(gateLoc[1].x, gateLoc[1].y, gateLoc[4].x, gateLoc[4].y, gateB[1].x, gateB[1].y)) {
|
2002-08-12 16:05:14 +00:00
|
|
|
return; /* same zplane and between both gates? */
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-15 21:50:51 +00:00
|
|
|
pt = closestPtOnLine(gateA[1].x, gateA[1].y, gateB[1].x, gateB[1].y, gateLoc[1].x, gateLoc[1].y);
|
2002-05-14 19:11:20 +00:00
|
|
|
gateLoc[3].x = pt.x;
|
|
|
|
gateLoc[3].y = pt.y;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2002-07-15 21:50:51 +00:00
|
|
|
if (compareSlope(gateLoc[1].x, gateLoc[1].y, gateLoc[3].x, gateLoc[3].y, gateA[0].x, gateA[0].y) ==
|
|
|
|
compareSlope(gateLoc[1].x, gateLoc[1].y, gateLoc[3].x, gateLoc[3].y, gateB[0].x, gateB[0].y)) {
|
|
|
|
closestPtOnLine(gateA[0].x, gateA[0].y, gateB[0].x, gateB[0].y, gateLoc[1].x, gateLoc[1].y);
|
2002-07-07 20:25:23 +00:00
|
|
|
gateLoc[2].x = pt.x; /* if point 2 between gates, ignore! */
|
2002-05-14 19:11:20 +00:00
|
|
|
gateLoc[2].y = pt.y;
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
2002-08-12 16:05:14 +00:00
|
|
|
return;
|
2002-03-11 13:15:07 +00:00
|
|
|
}
|
2002-03-18 22:48:57 +00:00
|
|
|
|
2003-05-15 21:57:38 +00:00
|
|
|
void Scumm::getGates(int trap1, int trap2, ScummVM::Point gateA[2], ScummVM::Point gateB[2]) {
|
2002-07-15 21:50:51 +00:00
|
|
|
int i, j;
|
2002-12-30 21:56:56 +00:00
|
|
|
int dist[8];
|
|
|
|
int minDist[3];
|
|
|
|
int closest[3];
|
|
|
|
int box[3];
|
|
|
|
BoxCoords coords;
|
2003-05-15 21:57:38 +00:00
|
|
|
ScummVM::Point Clo[8];
|
|
|
|
ScummVM::Point poly[8];
|
2002-07-15 21:50:51 +00:00
|
|
|
int line1, line2;
|
2002-04-11 17:19:16 +00:00
|
|
|
|
2003-07-02 13:47:03 +00:00
|
|
|
// For all corner coordinates of the first box, compute the point closest
|
2002-07-15 21:50:51 +00:00
|
|
|
// to them on the second box (and also compute the distance of these points).
|
2002-12-30 21:56:56 +00:00
|
|
|
getBoxCoordinates(trap1, &coords);
|
|
|
|
poly[0] = coords.ul;
|
|
|
|
poly[1] = coords.ur;
|
|
|
|
poly[2] = coords.lr;
|
|
|
|
poly[3] = coords.ll;
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2003-07-02 13:47:03 +00:00
|
|
|
dist[i] = getClosestPtOnBox(trap2, poly[i].x, poly[i].y, Clo[i].x, Clo[i].y);
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 21:50:51 +00:00
|
|
|
// Now do the same but with the roles of the first and second box swapped.
|
2002-12-30 21:56:56 +00:00
|
|
|
getBoxCoordinates(trap2, &coords);
|
|
|
|
poly[4] = coords.ul;
|
|
|
|
poly[5] = coords.ur;
|
|
|
|
poly[6] = coords.lr;
|
|
|
|
poly[7] = coords.ll;
|
2002-04-11 17:19:16 +00:00
|
|
|
for (i = 4; i < 8; i++) {
|
2003-07-02 13:47:03 +00:00
|
|
|
dist[i] = getClosestPtOnBox(trap1, poly[i].x, poly[i].y, Clo[i].x, Clo[i].y);
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 21:50:51 +00:00
|
|
|
// Find the three closest "close" points between the two boxes.
|
|
|
|
for (j = 0; j < 3; j++) {
|
2002-12-30 21:56:56 +00:00
|
|
|
minDist[j] = 0xFFFF;
|
2002-07-15 21:50:51 +00:00
|
|
|
for (i = 0; i < 8; i++) {
|
2002-12-30 21:56:56 +00:00
|
|
|
if (dist[i] < minDist[j]) {
|
|
|
|
minDist[j] = dist[i];
|
|
|
|
closest[j] = i;
|
2002-07-15 21:50:51 +00:00
|
|
|
}
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
2002-12-30 21:56:56 +00:00
|
|
|
dist[closest[j]] = 0xFFFF;
|
|
|
|
minDist[j] = (int)sqrt((double)minDist[j]);
|
2003-07-02 13:47:03 +00:00
|
|
|
box[j] = (closest[j] > 3); // Is the point on the first or on the second box?
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-15 21:50:51 +00:00
|
|
|
// Finally, compute the "gate". That's a pair of two points that are
|
|
|
|
// in the same box (actually, on the border of that box), which both have
|
|
|
|
// "minimal" distance to the other box in a certain sense.
|
|
|
|
|
2002-12-30 21:56:56 +00:00
|
|
|
if (box[0] == box[1] && abs(minDist[0] - minDist[1]) < 4) {
|
|
|
|
line1 = closest[0];
|
|
|
|
line2 = closest[1];
|
|
|
|
|
|
|
|
} else if (box[0] == box[1] && minDist[0] == minDist[1]) { /* parallel */
|
|
|
|
line1 = closest[0];
|
|
|
|
line2 = closest[1];
|
|
|
|
} else if (box[0] == box[2] && minDist[0] == minDist[2]) { /* parallel */
|
|
|
|
line1 = closest[0];
|
|
|
|
line2 = closest[2];
|
|
|
|
} else if (box[1] == box[2] && minDist[1] == minDist[2]) { /* parallel */
|
|
|
|
line1 = closest[1];
|
|
|
|
line2 = closest[2];
|
|
|
|
|
|
|
|
} else if (box[0] == box[2] && abs(minDist[0] - minDist[2]) < 4) {
|
|
|
|
line1 = closest[0];
|
|
|
|
line2 = closest[2];
|
|
|
|
} else if (abs(minDist[0] - minDist[2]) < 4) { /* if 1 close to 3 then use 2-3 */
|
|
|
|
line1 = closest[1];
|
|
|
|
line2 = closest[2];
|
|
|
|
} else if (abs(minDist[0] - minDist[1]) < 4) {
|
|
|
|
line1 = closest[0];
|
|
|
|
line2 = closest[1];
|
2002-04-11 17:19:16 +00:00
|
|
|
} else {
|
2002-12-30 21:56:56 +00:00
|
|
|
line1 = closest[0];
|
|
|
|
line2 = closest[0];
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
2002-03-18 22:48:57 +00:00
|
|
|
|
2002-07-15 21:50:51 +00:00
|
|
|
// Set the gate
|
2002-04-11 17:19:16 +00:00
|
|
|
if (line1 < 4) { /* from box 1 to box 2 */
|
2002-07-15 21:50:51 +00:00
|
|
|
gateA[0] = poly[line1];
|
|
|
|
gateA[1] = Clo[line1];
|
2002-04-11 17:19:16 +00:00
|
|
|
} else {
|
2002-07-15 21:50:51 +00:00
|
|
|
gateA[1] = poly[line1];
|
|
|
|
gateA[0] = Clo[line1];
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (line2 < 4) { /* from box */
|
2002-07-15 21:50:51 +00:00
|
|
|
gateB[0] = poly[line2];
|
|
|
|
gateB[1] = Clo[line2];
|
2002-04-11 17:19:16 +00:00
|
|
|
} else {
|
2002-07-15 21:50:51 +00:00
|
|
|
gateB[1] = poly[line2];
|
|
|
|
gateB[0] = Clo[line2];
|
2002-04-11 17:19:16 +00:00
|
|
|
}
|
2002-03-18 22:48:57 +00:00
|
|
|
}
|
2002-07-15 21:50:51 +00:00
|
|
|
|