aboutsummaryrefslogtreecommitdiff
path: root/saga/objectmap.cpp
blob: 23fc284396264721b8c5823553774ecd5c468e01 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004 The ScummVM project
 *
 * The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
 *
 * 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.
 *
 * $Header$
 *
 */

// 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.h"
#include "reinherit.h"

#include "cvar_mod.h"
#include "console_mod.h"
#include "gfx_mod.h"
#include "font_mod.h"

#include "objectmap_mod.h"
#include "objectmap.h"

namespace Saga {

static R_OBJECTMAP_INFO OMInfo;

int OBJECTMAP_Register() {
	CVAR_RegisterFunc(CF_object_info, "object_info", NULL, R_CVAR_NONE, 0, 0);

	return R_SUCCESS;
}

// Initializes the object map module, creates module allocation context
int OBJECTMAP_Init() {
	debug(0, "OBJECTMAP Module: Initializing...");

	OMInfo.initialized = 1;
	return R_SUCCESS;
}

// Shuts down the object map module, destroys module allocation context
int OBJECTMAP_Shutdown() {
	if (!OMInfo.initialized) {
		return R_FAILURE;
	}

	debug(0, "OBJECTMAP Module: Shutting down...");

	OBJECTMAP_Free();
	OBJECTMAP_FreeNames();

	debug(0, "OBJECTMAP Module: Shutdown AOK.");

	OMInfo.initialized = 0;
	return R_SUCCESS;
}

// Loads an object map resource ( objects ( clickareas ( points ) ) ) 
int OBJECTMAP_Load(const byte *om_res, size_t om_res_len) {
	R_OBJECTMAP_ENTRY *object_map;
	R_CLICKAREA *clickarea;
	R_POINT *point;

	int i, k, m;

	MemoryReadStream *readS = new MemoryReadStream(om_res, om_res_len);

	if (!OMInfo.initialized) {
		warning("Error: Object map module not initialized");
		return R_FAILURE;
	}

	if (OMInfo.objects_loaded) {
		OBJECTMAP_Free();
	}

	// Obtain object count N and allocate space for N objects
	OMInfo.n_objects = readS->readUint16LE();

	OMInfo.object_maps = (R_OBJECTMAP_ENTRY *)malloc(OMInfo.n_objects * sizeof *OMInfo.object_maps);

	if (OMInfo.object_maps == NULL) {
		warning("Error: Memory allocation failed");
		return R_MEM;
	}

	// Load all N objects
	for (i = 0; i < OMInfo.n_objects; i++) {
		object_map = &OMInfo.object_maps[i];
		object_map->unknown0 = readS->readByte();
		object_map->n_clickareas = readS->readByte();
		object_map->flags = readS->readUint16LE();
		object_map->object_num = readS->readUint16LE();
		object_map->script_num = readS->readUint16LE();
		object_map->clickareas = (R_CLICKAREA *)malloc(object_map->n_clickareas * sizeof *(object_map->clickareas));

		if (object_map->clickareas == NULL) {
			warning("Error: Memory allocation failed");
			return R_MEM;
		}

		// Load all clickareas for this object
		for (k = 0; k < object_map->n_clickareas; k++) {
			clickarea = &object_map->clickareas[k];
			clickarea->n_points = readS->readUint16LE();
			assert(clickarea->n_points != 0);

			clickarea->points = (R_POINT *)malloc(clickarea->n_points * sizeof *(clickarea->points));
			if (clickarea->points == NULL) {
				warning("Error: Memory allocation failed");
				return R_MEM;
			}

			// Load all points for this clickarea
			for (m = 0; m < clickarea->n_points; m++) {
				point = &clickarea->points[m];
				point->x = readS->readSint16LE();
				point->y = readS->readSint16LE();
			}
			debug(2, "OBJECTMAP_Load(): Read %d points for clickarea %d in object %d.",
					clickarea->n_points, k, object_map->object_num);
		}
	}

	OMInfo.objects_loaded = 1;

	return R_SUCCESS;
}

// Frees all storage allocated for the current object map data
int OBJECTMAP_Free() {
	R_OBJECTMAP_ENTRY *object_map;
	R_CLICKAREA *clickarea;

	int i, k;

	if (!OMInfo.objects_loaded) {
		return R_FAILURE;
	}

	for (i = 0; i < OMInfo.n_objects; i++) {
		object_map = &OMInfo.object_maps[i];
		for (k = 0; k < object_map->n_clickareas; k++) {
			clickarea = &object_map->clickareas[k];
			free(clickarea->points);
		}
		free(object_map->clickareas);
	}

	if (OMInfo.n_objects) {
		free(OMInfo.object_maps);
	}

	OMInfo.objects_loaded = 0;

	return R_SUCCESS;
}

// Loads an object name list resource
int OBJECTMAP_LoadNames(const unsigned char *onl_res, size_t onl_res_len) {
	int table_len;
	int n_names;
	size_t name_offset;

	int i;

	MemoryReadStream *readS = new MemoryReadStream(onl_res, onl_res_len);

	if (OMInfo.names_loaded) {
		OBJECTMAP_FreeNames();
	}

	table_len = readS->readUint16LE();

	n_names = table_len / 2 - 2;
	OMInfo.n_names = n_names;

	debug(2, "OBJECTMAP_LoadNames: Loading %d object names.", n_names);
	OMInfo.names = (const char **)malloc(n_names * sizeof *OMInfo.names);

	if (OMInfo.names == NULL) {
		warning("Error: Memory allocation failed");
		return R_MEM;
	}

	for (i = 0; i < n_names; i++) {
		name_offset = readS->readUint16LE();
		OMInfo.names[i] = (const char *)(onl_res + name_offset);

		debug(3, "Loaded object name string: %s", OMInfo.names[i]);
	}

	OMInfo.names_loaded = 1;

	return R_SUCCESS;
}

// Frees all storage allocated for the current object name list data
int OBJECTMAP_FreeNames() {
	if (!OMInfo.names_loaded) {
		return R_FAILURE;
	}

	if (OMInfo.n_names) {
		free(OMInfo.names);
	}

	OMInfo.names_loaded = 0;
	return R_SUCCESS;
}

// If 'object' is a valid object number in the currently loaded object 
// name list resource, the funciton sets '*name' to the descriptive string
// corresponding to 'object' and returns R_SUCCESS. Otherwise it returns
// R_FAILURE.
int OBJECTMAP_GetName(int object, const char **name) {
	if (!OMInfo.names_loaded) {
		return R_FAILURE;
	}

	if ((object <= 0) || (object > OMInfo.n_names)) {
		return R_FAILURE;
	}

	*name = OMInfo.names[object - 1];

	return R_SUCCESS;
}

int OBJECTMAP_GetFlags(int object, uint16 *flags) {
	int i;

	if (!OMInfo.names_loaded) {
		return R_FAILURE;
	}

	if ((object <= 0) || (object > OMInfo.n_names)) {
		return R_FAILURE;
	}

	for (i = 0; i < OMInfo.n_objects; i++) {
		if (OMInfo.object_maps[i].object_num == object) {
			*flags = OMInfo.object_maps[i].flags;
			return R_SUCCESS;
		}
	}

	return R_FAILURE;
}

// If 'object' is a valid object number in the currently loaded object 
// name list resource, the funciton sets '*ep_num' to the entrypoint number
// corresponding to 'object' and returns R_SUCCESS. Otherwise, it returns
// R_FAILURE.
int OBJECTMAP_GetEPNum(int object, int *ep_num) {
	int i;

	if (!OMInfo.names_loaded) {
		return R_FAILURE;
	}

	if ((object < 0) || (object > (OMInfo.n_objects + 1))) {
		return R_FAILURE;
	}

	for (i = 0; i < OMInfo.n_objects; i++) {

		if (OMInfo.object_maps[i].object_num == object) {

			*ep_num = OMInfo.object_maps[i].script_num;
			return R_SUCCESS;
		}
	}

	return R_FAILURE;
}

// Uses GFX_DrawLine to display all clickareas for each object in the 
// currently loaded object map resource.
int OBJECTMAP_Draw(R_SURFACE *ds, R_POINT *imouse_pt, int color, int color2) {
	R_OBJECTMAP_ENTRY *object_map;
	R_CLICKAREA *clickarea;

	char txt_buf[32];

	int draw_color = color;
	int draw_txt = 0;

	int hit_object = 0;
	int object_num = 0;

	int pointcount = 0;
	int i, k;

	assert(OMInfo.initialized);

	if (!OMInfo.objects_loaded) {
		return R_FAILURE;
	}

	if (imouse_pt != NULL) {
		if (OBJECTMAP_HitTest(imouse_pt, &object_num) == R_SUCCESS) {
			hit_object = 1;
		}
	}

	for (i = 0; i < OMInfo.n_objects; i++) {
		draw_color = color;
		if (hit_object && (object_num == OMInfo.object_maps[i].object_num)) {
			snprintf(txt_buf, sizeof txt_buf, "obj %d: ? %d, f %X",
					OMInfo.object_maps[i].object_num,
					OMInfo.object_maps[i].unknown0,
					OMInfo.object_maps[i].flags);
			draw_txt = 1;
			draw_color = color2;
		}

		object_map = &OMInfo.object_maps[i];

		for (k = 0; k < object_map->n_clickareas; k++) {
			clickarea = &object_map->clickareas[k];
			pointcount = 0;
			if (clickarea->n_points == 2) {
				// 2 points represent a box
				GFX_DrawFrame(ds, &clickarea->points[0], &clickarea->points[1], draw_color);
			} else if (clickarea->n_points > 2) {
				// Otherwise draw a polyline
				GFX_DrawPolyLine(ds, clickarea->points, clickarea->n_points, draw_color);
			}
		}
	}

	if (draw_txt) {
		FONT_Draw(SMALL_FONT_ID, ds, txt_buf, 0, 2, 2, GFX_GetWhite(), GFX_GetBlack(), FONT_OUTLINE);
	}

	return R_SUCCESS;
}

static bool MATH_HitTestPoly(R_POINT *points, unsigned int npoints, R_POINT test_point) {
	int yflag0;
	int yflag1;
	bool inside_flag = false;
	unsigned int pt;

	R_POINT *vtx0 = &points[npoints - 1];
	R_POINT *vtx1 = &points[0];

	yflag0 = (vtx0->y >= test_point.y);
	for (pt = 0; pt < npoints; pt++, vtx1++) {
		yflag1 = (vtx1->y >= test_point.y);
		if (yflag0 != yflag1) {
			if (((vtx1->y - test_point.y) * (vtx0->x - vtx1->x) >=
				(vtx1->x - test_point.x) * (vtx0->y - vtx1->y)) == yflag1) {
				inside_flag = !inside_flag;
			}
		}
		yflag0 = yflag1;
		vtx0 = vtx1;
	}

	return inside_flag;
}

int OBJECTMAP_HitTest(R_POINT * imouse_pt, int *object_num) {
	R_POINT imouse;
	R_OBJECTMAP_ENTRY *object_map;
	R_CLICKAREA *clickarea;
	R_POINT *points;
	int n_points;

	int i, k;

	assert((imouse_pt != NULL) && (object_num != NULL));

	imouse.x = imouse_pt->x;
	imouse.y = imouse_pt->y;

	// Loop through all scene objects
	for (i = 0; i < OMInfo.n_objects; i++) {
		object_map = &OMInfo.object_maps[i];

		// Hit-test all clickareas for this object
		for (k = 0; k < object_map->n_clickareas; k++) {
			clickarea = &object_map->clickareas[k];
			n_points = clickarea->n_points;
			points = clickarea->points;

			if (n_points == 2) {
				// Hit-test a box region
				if ((imouse.x > points[0].x) && (imouse.x <= points[1].x) &&
					(imouse.y > points[0].y) &&
					(imouse.y <= points[1].y)) {
						*object_num = object_map->object_num;
						return R_SUCCESS;
				}
			} else if (n_points > 2) {
				// Hit-test a polygon
				if (MATH_HitTestPoly(points, n_points, imouse)) {
					*object_num = object_map->object_num;
					return R_SUCCESS;
				}
			}
		}
	}

	*object_num = 0;

	return R_FAILURE;
}

static void CF_object_info(int argc, char *argv[]) {
	int i;

	(void)(argc);
	(void)(argv);

	if (!OMInfo.initialized) {
		return;
	}

	CON_Print("%d objects loaded.", OMInfo.n_objects);

	for (i = 0; i < OMInfo.n_objects; i++) {
		CON_Print("%s:", OMInfo.names[i]);
		CON_Print("%d. Unk1: %d, flags: %X, name_i: %d, scr_n: %d, ca_ct: %d", i, OMInfo.object_maps[i].unknown0,
					OMInfo.object_maps[i].flags,
					OMInfo.object_maps[i].object_num,
					OMInfo.object_maps[i].script_num,
					OMInfo.object_maps[i].n_clickareas);
	}

	return;
}

} // End of namespace Saga