aboutsummaryrefslogtreecommitdiff
path: root/engines/saga/objectmap.cpp
blob: ad5167209cc4833a6f89b80d8274673ebbe9e0b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* 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.
 *
 */

// Object map / Object click-area module

// 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

#include "saga/saga.h"

#include "saga/gfx.h"
#include "saga/console.h"
#include "saga/font.h"
#include "saga/interface.h"
#include "saga/objectmap.h"
#include "saga/actor.h"
#include "saga/scene.h"
#include "saga/isomap.h"
#ifdef SAGA_DEBUG
#include "saga/render.h"
#endif

namespace Saga {

void HitZone::load(SagaEngine *vm, Common::MemoryReadStreamEndian *readStream, int index, int sceneNumber) {
	_index = index;
	_flags = readStream->readByte();
	_clickAreas.resize(readStream->readByte());
	_rightButtonVerb = readStream->readByte();
	readStream->readByte(); // pad
	_nameIndex = readStream->readUint16();
	_scriptNumber = readStream->readUint16();

	for (ClickAreas::iterator i = _clickAreas.begin(); i != _clickAreas.end(); ++i) {
		i->resize(readStream->readUint16LE());

		assert(!i->empty());

		for (ClickArea::iterator j = i->begin(); j != i->end(); ++j) {
			j->x = readStream->readSint16();
			j->y = readStream->readSint16();

			// WORKAROUND: bug #1259608: "ITE: Riff ignores command in Ferret merchant center"
			// Apparently ITE Mac version has bug in game data. Both ObjectMap and ActionMap
			// for exit area are little taller (y = 123) and thus Riff goes to exit
			// when clicked on barrel of nails.
			if (vm->getGameId() == GID_ITE) {
				if (sceneNumber == 18 && index == 0 && (i == _clickAreas.begin()) && (j == i->begin()) && j->y == 123) {
					j->y = 129;
				}
			}
		}
	}
}

bool HitZone::getSpecialPoint(Point &specialPoint) const {
	for (ClickAreas::const_iterator i = _clickAreas.begin(); i != _clickAreas.end(); ++i) {
		if (i->size() == 1) {
			specialPoint = (*i)[0];
			return true;
		}
	}
	return false;
}

bool HitZone::hitTest(const Point &testPoint) {
	const Point *points;
	uint pointsCount;

	if (_flags & kHitZoneEnabled) {
		for (ClickAreas::const_iterator i = _clickAreas.begin(); i != _clickAreas.end(); ++i) {
			pointsCount = i->size();
			if (pointsCount < 2) {
				continue;
			}
			points = &i->front();
			if (pointsCount == 2) {
				// Hit-test a box region
				if ((testPoint.x >= points[0].x) &&
					(testPoint.x <= points[1].x) &&
					(testPoint.y >= points[0].y) &&
					(testPoint.y <= points[1].y)) {
						return true;
					}
			} else {
				// Hit-test a polygon
				if (hitTestPoly(points, pointsCount, testPoint)) {
					return true;
				}
			}
		}
	}
	return false;
}

#ifdef SAGA_DEBUG
void HitZone::draw(SagaEngine *vm, int color) {
	int pointsCount, j;
	Location location;
	HitZone::ClickArea tmpPoints;
	const Point *points;
	Point specialPoint1;
	Point specialPoint2;

	for (ClickAreas::const_iterator i = _clickAreas.begin(); i != _clickAreas.end(); ++i) {
		pointsCount = i->size();
		points = &i->front();
		if (vm->_scene->getFlags() & kSceneFlagISO) {
			tmpPoints.resize(pointsCount);
			for (j = 0; j < pointsCount; j++) {
				location.u() = points[j].x;
				location.v() = points[j].y;
				location.z = 0;
				vm->_isoMap->tileCoordsToScreenPoint(location, tmpPoints[j]);
			}
			points = &tmpPoints.front();
		}

		if (pointsCount == 2) {
			// 2 points represent a box
			vm->_gfx->drawFrame(points[0], points[1], color);
		} else {
			if (pointsCount > 2) {
				// Otherwise draw a polyline
				// Do a full refresh so that the polyline can be shown
				vm->_render->setFullRefresh(true);
				vm->_gfx->drawPolyLine(points, pointsCount, color);
			}
		}
	}
	if (getSpecialPoint(specialPoint1)) {
		specialPoint2 = specialPoint1;
		specialPoint1.x--;
		specialPoint1.y--;
		specialPoint2.x++;
		specialPoint2.y++;
		vm->_gfx->drawFrame(specialPoint1, specialPoint2, color);
	}
}
#endif

// Loads an object map resource ( objects ( clickareas ( points ) ) )
void ObjectMap::load(const ByteArray &resourceData) {

	if (!_hitZoneList.empty()) {
		error("ObjectMap::load _hitZoneList not empty");
	}

	if (resourceData.empty()) {
		return;
	}

	if (resourceData.size() < 4) {
		error("ObjectMap::load wrong resourceLength");
	}

	ByteArrayReadStreamEndian readS(resourceData, _vm->isBigEndian());

	_hitZoneList.resize(readS.readUint16());

	int idx = 0;
	for (HitZoneArray::iterator i = _hitZoneList.begin(); i != _hitZoneList.end(); ++i) {
		i->load(_vm, &readS, idx++, _vm->_scene->currentSceneNumber());
	}
}

void ObjectMap::clear() {
	_hitZoneList.clear();
}

#ifdef SAGA_DEBUG
void ObjectMap::draw(const Point& testPoint, int color, int color2) {
	int hitZoneIndex;
	Common::String txtBuf;
	Point pickPoint;
	Point textPoint;
	Location pickLocation;
	pickPoint = testPoint;
	if (_vm->_scene->getFlags() & kSceneFlagISO) {
		assert(_vm->_actor->_protagonist);
		pickPoint.y -= _vm->_actor->_protagonist->_location.z;
		_vm->_isoMap->screenPointToTileCoords(pickPoint, pickLocation);
		pickLocation.toScreenPointUV(pickPoint);
	}

	hitZoneIndex = hitTest(pickPoint);

	for (HitZoneArray::iterator i = _hitZoneList.begin(); i != _hitZoneList.end(); ++i) {
		i->draw(_vm, (hitZoneIndex == i->getIndex()) ? color2 : color);
	}

	if (hitZoneIndex != -1) {
		txtBuf = Common::String::format("hitZone %d", hitZoneIndex);
		textPoint.x = 2;
		textPoint.y = 2;
		_vm->_font->textDraw(kKnownFontSmall, txtBuf.c_str(), textPoint, kITEColorBrightWhite, kITEColorBlack, kFontOutline);
	}
}
#endif

int ObjectMap::hitTest(const Point& testPoint) {

	// Loop through all scene objects
	for (HitZoneArray::iterator i = _hitZoneList.begin(); i != _hitZoneList.end(); ++i) {
		if (i->hitTest(testPoint)) {
			return i->getIndex();
		}
	}

	return -1;
}

void ObjectMap::cmdInfo() {
	_vm->_console->debugPrintf("%d zone(s) loaded.\n\n", _hitZoneList.size());
}

} // End of namespace Saga