2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2004-04-12 21:40:49 +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.
|
2004-04-12 21:40:49 +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
|
2008-01-05 12:45:14 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2004-04-12 21:40:49 +00:00
|
|
|
* 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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
2006-02-11 12:44:16 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
// Object map / Object click-area module
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 13:04:31 +00:00
|
|
|
// Polygon Hit Test code ( HitTestPoly() ) adapted from code (C) Eric Haines
|
|
|
|
// appearing in Graphics Gems IV, "Point in Polygon Strategies."
|
|
|
|
// p. 24-46, code: p. 34-45
|
2007-07-31 18:08:40 +00:00
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/saga.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/gfx.h"
|
2004-08-10 18:31:33 +00:00
|
|
|
#include "saga/console.h"
|
2004-08-03 00:06:18 +00:00
|
|
|
#include "saga/font.h"
|
2005-01-27 20:07:04 +00:00
|
|
|
#include "saga/interface.h"
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/objectmap.h"
|
2005-03-18 17:11:37 +00:00
|
|
|
#include "saga/actor.h"
|
|
|
|
#include "saga/scene.h"
|
|
|
|
#include "saga/isomap.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
namespace Saga {
|
|
|
|
|
2006-11-09 13:19:23 +00:00
|
|
|
HitZone::HitZone(MemoryReadStreamEndian *readStream, int index, int sceneNumber): _index(index) {
|
2004-12-24 20:44:39 +00:00
|
|
|
int i, j;
|
|
|
|
HitZone::ClickArea *clickArea;
|
|
|
|
Point *point;
|
|
|
|
|
|
|
|
_flags = readStream->readByte();
|
|
|
|
_clickAreasCount = readStream->readByte();
|
2005-01-15 20:12:49 +00:00
|
|
|
_rightButtonVerb = readStream->readByte();
|
2004-12-24 20:44:39 +00:00
|
|
|
readStream->readByte(); // pad
|
2005-01-18 15:01:21 +00:00
|
|
|
_nameIndex = readStream->readUint16();
|
2004-12-24 20:44:39 +00:00
|
|
|
_scriptNumber = readStream->readUint16();
|
|
|
|
|
2005-01-02 14:52:11 +00:00
|
|
|
_clickAreas = (HitZone::ClickArea *)malloc(_clickAreasCount * sizeof(*_clickAreas));
|
2004-12-24 20:44:39 +00:00
|
|
|
|
|
|
|
if (_clickAreas == NULL) {
|
2005-01-21 21:55:54 +00:00
|
|
|
memoryError("HitZone::HitZone");
|
2004-12-24 20:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < _clickAreasCount; i++) {
|
|
|
|
clickArea = &_clickAreas[i];
|
2005-03-18 17:11:37 +00:00
|
|
|
clickArea->pointsCount = readStream->readUint16LE();
|
2005-07-29 17:58:00 +00:00
|
|
|
|
2004-12-24 20:44:39 +00:00
|
|
|
assert(clickArea->pointsCount);
|
|
|
|
|
2005-01-02 14:52:11 +00:00
|
|
|
clickArea->points = (Point *)malloc(clickArea->pointsCount * sizeof(*(clickArea->points)));
|
2004-12-24 20:44:39 +00:00
|
|
|
if (clickArea->points == NULL) {
|
2005-01-21 21:55:54 +00:00
|
|
|
memoryError("HitZone::HitZone");
|
2004-12-24 20:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < clickArea->pointsCount; j++) {
|
|
|
|
point = &clickArea->points[j];
|
|
|
|
point->x = readStream->readSint16();
|
|
|
|
point->y = readStream->readSint16();
|
2006-11-09 13:19:23 +00:00
|
|
|
|
|
|
|
// WORKAROUND: bug #1259608: "ITE: Riff ignores command in Ferret merchant center"
|
|
|
|
// Apparently ITE Mac version has bug in game data. Both ObjectMap and ActionMap
|
2008-01-27 19:47:41 +00:00
|
|
|
// for exit area are little taller (y = 123) and thus Riff goes to exit
|
2006-11-09 13:19:23 +00:00
|
|
|
// when clicked on barrel of nails.
|
|
|
|
if (sceneNumber == 18 && index == 0 && i == 0 && j == 0 && point->y == 123)
|
|
|
|
point->y = 129;
|
2004-12-24 20:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HitZone::~HitZone() {
|
|
|
|
for (int i = 0; i < _clickAreasCount; i++) {
|
|
|
|
free(_clickAreas[i].points);
|
|
|
|
}
|
|
|
|
free(_clickAreas);
|
|
|
|
}
|
|
|
|
|
2005-01-18 15:01:21 +00:00
|
|
|
bool HitZone::getSpecialPoint(Point &specialPoint) const {
|
|
|
|
int i, pointsCount;
|
|
|
|
HitZone::ClickArea *clickArea;
|
|
|
|
Point *points;
|
|
|
|
|
|
|
|
for (i = 0; i < _clickAreasCount; i++) {
|
|
|
|
clickArea = &_clickAreas[i];
|
|
|
|
pointsCount = clickArea->pointsCount;
|
|
|
|
points = clickArea->points;
|
|
|
|
if (pointsCount == 1) {
|
|
|
|
specialPoint = points[0];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2004-12-24 20:44:39 +00:00
|
|
|
bool HitZone::hitTest(const Point &testPoint) {
|
|
|
|
int i, pointsCount;
|
|
|
|
HitZone::ClickArea *clickArea;
|
|
|
|
Point *points;
|
|
|
|
|
|
|
|
if (_flags & kHitZoneEnabled) {
|
|
|
|
for (i = 0; i < _clickAreasCount; i++) {
|
|
|
|
clickArea = &_clickAreas[i];
|
|
|
|
pointsCount = clickArea->pointsCount;
|
|
|
|
points = clickArea->points;
|
|
|
|
|
|
|
|
if (pointsCount == 2) {
|
|
|
|
// Hit-test a box region
|
2005-07-29 17:58:00 +00:00
|
|
|
if ((testPoint.x >= points[0].x) &&
|
2004-12-24 20:44:39 +00:00
|
|
|
(testPoint.x <= points[1].x) &&
|
|
|
|
(testPoint.y >= points[0].y) &&
|
|
|
|
(testPoint.y <= points[1].y)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pointsCount > 2) {
|
|
|
|
// Hit-test a polygon
|
|
|
|
if (hitTestPoly(points, pointsCount, testPoint)) {
|
|
|
|
return true;
|
2005-07-29 17:58:00 +00:00
|
|
|
}
|
2004-12-24 20:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
void HitZone::draw(SagaEngine *vm, Surface *ds, int color) {
|
2005-04-03 15:32:04 +00:00
|
|
|
int i, pointsCount, j;
|
|
|
|
Location location;
|
2004-12-24 20:44:39 +00:00
|
|
|
HitZone::ClickArea *clickArea;
|
|
|
|
Point *points;
|
2007-06-08 23:33:35 +00:00
|
|
|
Point specialPoint1;
|
|
|
|
Point specialPoint2;
|
2004-12-24 20:44:39 +00:00
|
|
|
for (i = 0; i < _clickAreasCount; i++) {
|
|
|
|
clickArea = &_clickAreas[i];
|
|
|
|
pointsCount = clickArea->pointsCount;
|
2005-07-19 19:05:52 +00:00
|
|
|
if (vm->_scene->getFlags() & kSceneFlagISO) {
|
2005-04-03 15:32:04 +00:00
|
|
|
points = (Point*)malloc(sizeof(Point) * pointsCount);
|
|
|
|
for (j = 0; j < pointsCount; j++) {
|
|
|
|
location.u() = clickArea->points[j].x;
|
|
|
|
location.v() = clickArea->points[j].y;
|
|
|
|
location.z = 0;
|
2005-07-19 19:05:52 +00:00
|
|
|
vm->_isoMap->tileCoordsToScreenPoint(location, points[j]);
|
2005-07-29 17:58:00 +00:00
|
|
|
}
|
2005-04-03 15:32:04 +00:00
|
|
|
} else {
|
|
|
|
points = clickArea->points;
|
|
|
|
}
|
|
|
|
|
2004-12-24 20:44:39 +00:00
|
|
|
if (pointsCount == 2) {
|
|
|
|
// 2 points represent a box
|
2005-07-09 16:23:45 +00:00
|
|
|
ds->drawFrame(points[0], points[1], color);
|
2004-12-24 20:44:39 +00:00
|
|
|
} else {
|
|
|
|
if (pointsCount > 2) {
|
|
|
|
// Otherwise draw a polyline
|
2005-07-09 16:23:45 +00:00
|
|
|
ds->drawPolyLine(points, pointsCount, color);
|
2004-12-24 20:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
2005-07-19 19:05:52 +00:00
|
|
|
if (vm->_scene->getFlags() & kSceneFlagISO) {
|
2005-04-03 15:32:04 +00:00
|
|
|
free(points);
|
|
|
|
}
|
|
|
|
|
2004-12-24 20:44:39 +00:00
|
|
|
}
|
2007-06-08 23:33:35 +00:00
|
|
|
if (getSpecialPoint(specialPoint1)) {
|
|
|
|
specialPoint2 = specialPoint1;
|
|
|
|
specialPoint1.x--;
|
|
|
|
specialPoint1.y--;
|
|
|
|
specialPoint2.x++;
|
|
|
|
specialPoint2.y++;
|
|
|
|
ds->drawFrame(specialPoint1, specialPoint2, color);
|
|
|
|
}
|
2004-12-24 20:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
// Loads an object map resource ( objects ( clickareas ( points ) ) )
|
2005-01-18 11:55:31 +00:00
|
|
|
void ObjectMap::load(const byte *resourcePointer, size_t resourceLength) {
|
2004-04-12 21:40:49 +00:00
|
|
|
int i;
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (resourceLength == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-01-18 11:55:31 +00:00
|
|
|
if (resourceLength < 4) {
|
|
|
|
error("ObjectMap::load wrong resourceLength");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
MemoryReadStreamEndian readS(resourcePointer, resourceLength, _vm->isBigEndian());
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-18 11:55:31 +00:00
|
|
|
_hitZoneListCount = readS.readSint16();
|
|
|
|
if (_hitZoneListCount < 0) {
|
|
|
|
error("ObjectMap::load _hitZoneListCount < 0");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-01-18 11:55:31 +00:00
|
|
|
if (_hitZoneList)
|
|
|
|
error("ObjectMap::load _hitZoneList != NULL");
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-18 11:55:31 +00:00
|
|
|
_hitZoneList = (HitZone **) malloc(_hitZoneListCount * sizeof(HitZone *));
|
|
|
|
if (_hitZoneList == NULL) {
|
2005-01-21 21:55:54 +00:00
|
|
|
memoryError("ObjectMap::load");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-01-18 11:55:31 +00:00
|
|
|
for (i = 0; i < _hitZoneListCount; i++) {
|
2006-11-09 13:19:23 +00:00
|
|
|
_hitZoneList[i] = new HitZone(&readS, i, _vm->_scene->currentSceneNumber());
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-18 11:55:31 +00:00
|
|
|
void ObjectMap::freeMem() {
|
2004-04-12 21:40:49 +00:00
|
|
|
int i;
|
|
|
|
|
2005-01-18 11:55:31 +00:00
|
|
|
if (_hitZoneList) {
|
|
|
|
for (i = 0; i < _hitZoneListCount; i++) {
|
|
|
|
delete _hitZoneList[i];
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2004-12-24 20:44:39 +00:00
|
|
|
|
2005-01-18 11:55:31 +00:00
|
|
|
free(_hitZoneList);
|
|
|
|
_hitZoneList = NULL;
|
|
|
|
}
|
2005-03-18 17:11:37 +00:00
|
|
|
_hitZoneListCount = 0;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-07-09 16:23:45 +00:00
|
|
|
void ObjectMap::draw(Surface *ds, const Point& testPoint, int color, int color2) {
|
2005-01-18 11:55:31 +00:00
|
|
|
int i;
|
|
|
|
int hitZoneIndex;
|
|
|
|
char txtBuf[32];
|
2005-03-18 17:11:37 +00:00
|
|
|
Point pickPoint;
|
2005-07-14 17:46:21 +00:00
|
|
|
Point textPoint;
|
2005-03-18 17:11:37 +00:00
|
|
|
Location pickLocation;
|
|
|
|
pickPoint = testPoint;
|
|
|
|
if (_vm->_scene->getFlags() & kSceneFlagISO) {
|
|
|
|
assert(_vm->_actor->_protagonist);
|
2005-08-10 14:53:17 +00:00
|
|
|
pickPoint.y -= _vm->_actor->_protagonist->_location.z;
|
2005-03-18 17:11:37 +00:00
|
|
|
_vm->_isoMap->screenPointToTileCoords(pickPoint, pickLocation);
|
|
|
|
pickLocation.toScreenPointUV(pickPoint);
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-03-18 17:11:37 +00:00
|
|
|
hitZoneIndex = hitTest(pickPoint);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
for (i = 0; i < _hitZoneListCount; i++) {
|
2005-07-19 19:05:52 +00:00
|
|
|
_hitZoneList[i]->draw(_vm, ds, (hitZoneIndex == i) ? color2 : color);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
if (hitZoneIndex != -1) {
|
2005-01-18 11:55:31 +00:00
|
|
|
snprintf(txtBuf, sizeof(txtBuf), "hitZone %d", hitZoneIndex);
|
2005-07-14 17:46:21 +00:00
|
|
|
textPoint.x = 2;
|
|
|
|
textPoint.y = 2;
|
2005-11-03 18:20:12 +00:00
|
|
|
_vm->_font->textDraw(kKnownFontSmall, ds, txtBuf, textPoint, kITEColorBrightWhite, kITEColorBlack, kFontOutline);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-18 11:55:31 +00:00
|
|
|
int ObjectMap::hitTest(const Point& testPoint) {
|
|
|
|
int i;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 13:04:31 +00:00
|
|
|
// Loop through all scene objects
|
2005-01-18 11:55:31 +00:00
|
|
|
for (i = 0; i < _hitZoneListCount; i++) {
|
|
|
|
if (_hitZoneList[i]->hitTest(testPoint)) {
|
|
|
|
return i;
|
2004-05-01 13:04:31 +00:00
|
|
|
}
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-07 23:26:41 +00:00
|
|
|
return -1;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-12-24 20:44:39 +00:00
|
|
|
void ObjectMap::cmdInfo(void) {
|
2005-01-18 11:55:31 +00:00
|
|
|
_vm->_console->DebugPrintf("%d zone(s) loaded.\n\n", _hitZoneListCount);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace Saga
|