aboutsummaryrefslogtreecommitdiff
path: root/engines/lure/game.cpp
blob: 32f68780258f8ed62e1aa5511d30d383118c0539 (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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2005-2006 The ScummVM project
 *
 * 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "lure/game.h"
#include "lure/strings.h"
#include "lure/room.h"
#include "lure/system.h"
#include "lure/debug-input.h"
#include "lure/debug-methods.h"
#include "lure/scripts.h"
#include "lure/res_struct.h"

namespace Lure {

static Game *int_game = NULL;

Game &Game::getReference() {
	return *int_game;
}

Game::Game() {
	int_game = this;
	_slowSpeedFlag = true;
	_soundFlag = true;
}

void Game::nextFrame() {
	Resources &res = Resources::getReference();
	Room &room = Room::getReference();
	HotspotList::iterator i;

	room.checkCursor();
	room.update();

	// Call the tick method for each hotspot - this is somewaht complicated
	// by the fact that a tick proc can unload both itself and/or others,
	// so we first get a list of the Ids, and call the tick proc for each 
	// id in sequence if it's still active

	uint16 *idList = new uint16[res.activeHotspots().size()];
	int idSize = 0;
	for (i = res.activeHotspots().begin(); i != res.activeHotspots().end(); ++i) {
		Hotspot *hotspot = *i;
		idList[idSize++] = hotspot->hotspotId();
	}

	for (int idCtr = 0; idCtr < idSize; ++idCtr) {
		Hotspot *hotspot = res.getActiveHotspot(idList[idCtr]);
		if (hotspot)
			hotspot->tick();
	}

	delete[] idList;
	Screen::getReference().update();
}

void Game::execute() {
	OSystem &system = System::getReference();
	Room &r = Room::getReference();
	Resources &res = Resources::getReference();
	Events &events = Events::getReference();
	Mouse &mouse = Mouse::getReference();
	Screen &screen = Screen::getReference();
	//Menu &menu = Menu::getReference();
	ValueTableData &fields = res.fieldList();

	uint32 timerVal = system.getMillis();

	screen.empty();
	//_screen.resetPalette();
	screen.setPaletteEmpty();

	Script::execute(STARTUP_SCRIPT);

	// Load the first room
	r.setRoomNumber(1);

	// Set the player direction
	res.getActiveHotspot(PLAYER_ID)->setDirection(UP);

	r.update();
	mouse.setCursorNum(CURSOR_ARROW);
	mouse.cursorOn();
	
	while (!events.quitFlag) {
		// If time for next frame, allow everything to update
		if (system.getMillis() > timerVal + GAME_FRAME_DELAY) {
			timerVal = system.getMillis();
			nextFrame();			
		}
		res.delayList().tick();

		while (events.pollEvent()) {
			if (events.type() == OSystem::EVENT_KEYDOWN) {
				uint16 roomNum = r.roomNumber();

#ifdef LURE_DEBUG
				if (events.event().kbd.keycode == 282) {
					doDebugMenu();
					continue;
				}
#endif

				switch (events.event().kbd.ascii) {
				case 27:
					events.quitFlag = true;
					break;

#ifdef LURE_DEBUG
				case '+':
					while (++roomNum <= 51) 
						if (res.getRoom(roomNum) != NULL) break; 
					if (roomNum == 52) roomNum = 1;

					r.leaveRoom();
					r.setRoomNumber(roomNum);
					break;

				case '-':
					if (roomNum == 1) roomNum = 55;
					while (res.getRoom(--roomNum) == NULL) ;

					r.leaveRoom();
					r.setRoomNumber(roomNum);
					break;

				case '*':
					res.getActiveHotspot(PLAYER_ID)->setRoomNumber(
						r.roomNumber());
					break;
#endif
				default:
					break;
				}
			}

			if ((events.type() == OSystem::EVENT_LBUTTONDOWN) ||
				(events.type() == OSystem::EVENT_RBUTTONDOWN)) 
				handleClick();
		}

		uint16 destRoom;
		destRoom = fields.getField(NEW_ROOM_NUMBER);
		if (destRoom != 0) {
			// Need to change the current room
			bool remoteFlag = fields.getField(OLD_ROOM_NUMBER) != 0;
			r.setRoomNumber(destRoom, remoteFlag);
			fields.setField(NEW_ROOM_NUMBER, 0);
		}

		destRoom = fields.playerNewPos().roomNumber;
		if (destRoom != 0) {
			playerChangeRoom();
		}

		system.delayMillis(10);
	}

	r.leaveRoom();
}

#ifdef LURE_DEBUG

#define NUM_DEBUG_ITEMS 4
const char *debugItems[NUM_DEBUG_ITEMS] = 
		{"Toggle Info", "Set Room", "Show Active HS", "Show Room HS"};

void Game::doDebugMenu() {
	uint16 index = PopupMenu::Show(NUM_DEBUG_ITEMS, debugItems);
	Room &r = Room::getReference();
	Resources &res = Resources::getReference();
	
	switch (index) {
	case 0:
		// Toggle co-ordinates
		r.setShowInfo(!r.showInfo());
		break;

	case 1:
		// Set room number:
		uint32 roomNumber;
		if (!input_integer("Enter room number:", roomNumber)) return;
		if (res.getRoom(roomNumber))
			r.setRoomNumber(roomNumber);
		else
			Dialog::show("The room does not exist");
		break;

	case 2:
		// Show active hotspots
        showActiveHotspots();
		break;

	case 3:
		// Show hotspots in room
		showRoomHotspots();
		break;

	default:
		break;
	}
}

#endif

void Game::handleMenuResponse(uint8 selection) {
	switch (selection) {
	case MENUITEM_CREDITS:
		doShowCredits();
		break;

	case MENUITEM_RESTART_GAME: 
	case MENUITEM_SAVE_GAME:
	case MENUITEM_RESTORE_GAME: 
		break;

	case MENUITEM_QUIT:
		doQuit();
		break;

	case MENUITEM_TEXT_SPEED:
		doTextSpeed();
		break;

	case MENUITEM_SOUND:
		doSound();
	}
}

void Game::playerChangeRoom() {
	Resources &res = Resources::getReference();
	Room &room = Room::getReference();
	ValueTableData &fields = res.fieldList();
	uint16 roomNum = fields.playerNewPos().roomNumber;
	fields.playerNewPos().roomNumber = 0;
	Point &newPos = fields.playerNewPos().position;

	Hotspot *player = res.getActiveHotspot(PLAYER_ID);
	player->currentActions().clear();
	player->setRoomNumber(roomNum);
	//player->setPosition((newPos.x & 0xfff8) || 5, newPos.y & 0xfff8);
	player->setPosition(newPos.x, newPos.y);
	room.setRoomNumber(roomNum, false);
}

void Game::handleClick() {
	Resources &res = Resources::getReference();
	Room &room = Room::getReference();
	ValueTableData &fields = res.fieldList();
	Mouse &mouse = Mouse::getReference();
	uint16 oldRoomNumber = fields.getField(OLD_ROOM_NUMBER);

	if (room.checkInTalkDialog()) {
		// Close the active talk dialog
		room.setTalkDialog(0, 0);
	} else if (oldRoomNumber != 0) {
		// Viewing a room remotely - handle returning to prior room
		if ((room.roomNumber() != 35) || (fields.getField(87) == 0)) {
			// Reset player tick proc and signal to change back to the old room
			res.getActiveHotspot(PLAYER_ID)->setTickProc(PLAYER_TICK_PROC_ID); 
			fields.setField(NEW_ROOM_NUMBER, oldRoomNumber);
			fields.setField(OLD_ROOM_NUMBER, 0);
		}
	} else if (res.getTalkState() != TALK_NONE) {
		// Currently talking, so let it's tick proc handle it
	} else if (mouse.y() < MENUBAR_Y_SIZE) {
		uint8 response = Menu::getReference().execute();
		if (response != MENUITEM_NONE)
			handleMenuResponse(response);
	} else {
		if (mouse.lButton())
			handleLeftClick();
		else
			handleRightClickMenu();
	}
}

void Game::handleRightClickMenu() {
	Room &room = Room::getReference();
	Resources &res = Resources::getReference();
	ValueTableData &fields = res.fieldList();
	Hotspot *player = res.getActiveHotspot(PLAYER_ID);
	HotspotData *hotspot;
	Action action;
	uint32 actions;
	uint16 itemId;

	if (room.hotspotId() != 0) {
		// Get hotspot actions
		actions = room.hotspotActions();
	} else {
		// Standard actions - drink, examine, look, status
		actions = 0x1184000;
	}

	// If no inventory items remove entries that require them
	if (res.numInventoryItems() == 0) 
		actions &= 0xFEF3F9FD;

	// If the player hasn't any money, remove any bribe entry
	if (res.fieldList().numGroats() == 0)
		actions &= 0xFF7FFFFF;

	action = NONE;
	hotspot = NULL;

	bool breakFlag = false;
	while (!breakFlag) {
		action = PopupMenu::Show(actions);

		switch (action) {
		case LOOK:
		case STATUS:
			breakFlag = true;
			break;

		case GIVE:
		case USE:
		case EXAMINE:
		case DRINK:
			if (action != DRINK)
				hotspot = res.getHotspot(room.hotspotId());
			itemId = PopupMenu::ShowInventory();
			breakFlag = (itemId != 0xffff);
			if (breakFlag) 
				fields.setField(USE_HOTSPOT_ID, itemId);
			break;

		default:
			hotspot = res.getHotspot(room.hotspotId());
			breakFlag = true;
			break;
		}
	}

	// Set fields used by the script interpreter
	fields.setField(CHARACTER_HOTSPOT_ID, PLAYER_ID);
	if (hotspot) {
		fields.setField(ACTIVE_HOTSPOT_ID, hotspot->hotspotId);
		if ((action != USE) && (action != GIVE)) {
			fields.setField(USE_HOTSPOT_ID, 0xffff);
		} 
	}

	if (action != NONE) {
		res.setCurrentAction(action);
		room.update();
		Screen::getReference().update();
		player->doAction(action, hotspot);

		if (action != TALK_TO)
			res.setCurrentAction(NONE);
	}
}

void Game::handleLeftClick() {
	Room &room = Room::getReference();
	Mouse &mouse = Mouse::getReference();
	Resources &res = Resources::getReference();
	ValueTableData &fields = res.fieldList();
	Hotspot *player = res.getActiveHotspot(PLAYER_ID);

	if ((room.destRoomNumber() == 0) && (room.hotspotId() != 0)) {	
		// Handle look at hotspot
		HotspotData *hs = res.getHotspot(room.hotspotId());

		fields.setField(CHARACTER_HOTSPOT_ID, PLAYER_ID);
		fields.setField(ACTIVE_HOTSPOT_ID, hs->hotspotId);
		fields.setField(USE_HOTSPOT_ID, 0xffff);

		res.setCurrentAction(LOOK_AT);
		room.update();
		Screen::getReference().update();
		player->doAction(LOOK_AT, hs);
		res.setCurrentAction(NONE);
	} else if (room.destRoomNumber() != 0) {
		// Walk to another room
		RoomExitCoordinateData &exitData = 
			res.coordinateList().getEntry(room.roomNumber()).getData(room.destRoomNumber());

		player->walkTo((exitData.x & 0xfff8) | 5, (exitData.y & 0xfff8), 
			room.hotspotId() == 0 ? 0xffff : room.hotspotId());
	} else {
		// Walking within room
		player->walkTo(mouse.x(), mouse.y(), 0);
	}
}

void Game::doShowCredits() {
	Events &events = Events::getReference();
	Mouse &mouse = Mouse::getReference();
	Screen &screen = Screen::getReference();

	mouse.cursorOff();
	Palette p(CREDITS_RESOURCE_ID - 1);
	Surface *s = Surface::getScreen(CREDITS_RESOURCE_ID);
	screen.setPalette(&p);	
	s->copyToScreen(0, 0);
	delete s;

	events.waitForPress();

	screen.resetPalette();
	screen.update();
	mouse.cursorOn();
}

void Game::doQuit() {
	Mouse &mouse = Mouse::getReference();
	Events &events = Events::getReference();
	Screen &screen = Screen::getReference();

	mouse.cursorOff();
	Surface *s = Surface::newDialog(190, "Are you sure (y/n)?");
	s->centerOnScreen();
	delete s;

	char key = '\0';
	do {
		if (events.pollEvent()) {
			if (events.event().type == OSystem::EVENT_KEYDOWN) {
				key = events.event().kbd.ascii;
				if ((key >= 'A') && (key <= 'Z')) key += 'a' - 'A';
			}
		}
	} while (((uint8) key != 27) && (key != 'y') && (key != 'n'));

	events.quitFlag = key == 'y';
	if (!events.quitFlag) {
		screen.update();
		mouse.cursorOn();
	}
}

void Game::doTextSpeed() {
	Menu &menu = Menu::getReference();

	_slowSpeedFlag = !_slowSpeedFlag;
	const char *pSrc = _slowSpeedFlag ? "Slow" : "Fast";
	char *pDest = menu.getMenu(2).getEntry(1);
	memcpy(pDest, pSrc, 4);
}

void Game::doSound() {
	Menu &menu = Menu::getReference();

	_soundFlag = !_soundFlag;
	const char *pSrc = _soundFlag ? "on " : "off";
	char *pDest = menu.getMenu(2).getEntry(2) + 6;
	memcpy(pDest, pSrc, 3);
}

} // end of namespace Lure