aboutsummaryrefslogtreecommitdiff
path: root/engines/m4/globals.cpp
blob: 12d9a24d37c270ea91ab30639e089b57fcf23c42 (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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "m4/m4.h"
#include "m4/globals.h"
#include "m4/graphics.h"
#include "m4/gui.h"
#include "m4/viewmgr.h"
#include "m4/script.h"
#include "m4/m4_views.h"
#include "m4/compression.h"

namespace M4 {

Kernel::Kernel(M4Engine *vm) : _vm(vm) {
	daemonTriggerAvailable = true;
	firstFadeColorIndex = 0;
	paused = false;
	betweenRooms = false;
	currentSection = 0;
	newSection = 0;
	previousSection = 0;
	currentRoom = 0;
	newRoom = 0;
	previousRoom = 0;
	trigger = 0;
	triggerMode = KT_DAEMON;
	
	_globalDaemonFn = NULL;
	_globalParserFn = NULL;

	_sectionInitFn = NULL;
	_sectionDaemonFn = NULL;
	_sectionParserFn = NULL;

	_roomInitFn = NULL;
	_roomDaemonFn = NULL;
	_roomPreParserFn = NULL;
	_roomParserFn = NULL;

}

int32 Kernel::createTrigger(int32 triggerNum) {
	if (triggerNum < 0)
		return triggerNum;
	else
		return triggerNum | (currentRoom << 16) | (triggerMode << 28);
}

bool Kernel::sendTrigger(int32 triggerNum) {
	return handleTrigger(createTrigger(triggerNum));
}

bool Kernel::handleTrigger(int32 triggerNum) {

	printf("betweenRooms = %d; triggerNum = %08X\n", betweenRooms, triggerNum);

	if (betweenRooms)
		return true;

	if (triggerNum < 0)
		return false;

	KernelTriggerType saveTriggerMode = triggerMode;
	int32 saveTrigger = trigger;
	bool result = false;

	int room = (triggerNum >> 16) & 0xFFF;
	
	printf("room = %d; currentRoom = %d\n", room, currentRoom); fflush(stdout);
	
	if (room != currentRoom) {
		printf("Kernel::handleTrigger() Trigger from another room\n");
		return false;
	}
	
	trigger = triggerNum & 0xFFFF;
	KernelTriggerType mode = (KernelTriggerType)(triggerNum >> 28);
	
	switch (mode) {

	case KT_PREPARSE:
		if (trigger < 32000) {
			triggerMode = KT_PREPARSE;
			roomPreParser();
			result = true;
		}
		break;

	case KT_PARSE:
		if (trigger < 32000) {
			triggerMode = KT_PARSE;
			// TODO player.commandReady = TRUE;
			roomParser();
			/* TODO
			if (player.commandReady)
				globalParser();
			*/
			result = true;
		}
		break;

	case KT_DAEMON:
		printf("KT_DAEMON\n");
		fflush(stdout);
		triggerMode = KT_DAEMON;
		daemonTriggerAvailable = false;
		roomDaemon();
		if (daemonTriggerAvailable) {
			daemonTriggerAvailable = false;
			sectionDaemon();
		}
		if (daemonTriggerAvailable) {
			daemonTriggerAvailable = false;
			globalDaemon();
		}

		break;

	default:
		printf("Kernel::handleTrigger() Unknown trigger mode %d\n", mode);

	}

	triggerMode = saveTriggerMode;
	trigger = saveTrigger;

	return result;
}

void Kernel::loadGlobalScriptFunctions() {
	_globalDaemonFn = _vm->_script->loadFunction("global_daemon");
	_globalParserFn = _vm->_script->loadFunction("global_parser");
}

void Kernel::loadSectionScriptFunctions() {
	char tempFnName[128];
	snprintf(tempFnName, 128, "section_init_%d", currentSection);
	_sectionInitFn = _vm->_script->loadFunction(tempFnName);
	snprintf(tempFnName, 128, "section_daemon_%d", currentSection);
	_sectionDaemonFn = _vm->_script->loadFunction(tempFnName);
	snprintf(tempFnName, 128, "section_parser_%d", currentSection);
	_sectionParserFn = _vm->_script->loadFunction(tempFnName);
}

void Kernel::loadRoomScriptFunctions() {
	char tempFnName[128];
	snprintf(tempFnName, 128, "room_init_%d", currentRoom);
	_roomInitFn = _vm->_script->loadFunction(tempFnName);
	snprintf(tempFnName, 128, "room_daemon_%d", currentRoom);
	_roomDaemonFn = _vm->_script->loadFunction(tempFnName);
	snprintf(tempFnName, 128, "room_pre_parser_%d", currentRoom);
	_roomPreParserFn = _vm->_script->loadFunction(tempFnName);
	snprintf(tempFnName, 128, "room_parser_%d", currentRoom);
	_roomParserFn = _vm->_script->loadFunction(tempFnName);
}

void Kernel::globalDaemon() {
	if (_globalDaemonFn)
		_vm->_script->runFunction(_globalDaemonFn);
	else {
		printf("Kernel::globalDaemon() _globalDaemonFn is NULL\n");
	}
}

void Kernel::globalParser() {
	if (_globalParserFn)
		_vm->_script->runFunction(_globalParserFn);
	else {
		printf("Kernel::globalParser() _globalParserFn is NULL\n");
	}
}

void Kernel::sectionInit() {
	if (_sectionInitFn)
		_vm->_script->runFunction(_sectionInitFn);
	else {
		printf("Kernel::sectionInit() _sectionInitFn is NULL\n");
	}
}

void Kernel::sectionDaemon() {
	if (_sectionDaemonFn)
		_vm->_script->runFunction(_sectionDaemonFn);
	else {
		printf("Kernel::sectionDaemon() _sectionDaemonFn is NULL\n");
	}
}

void Kernel::sectionParser() {
	if (_sectionParserFn)
		_vm->_script->runFunction(_sectionParserFn);
	else {
		printf("Kernel::sectionParser() _sectionParserFn is NULL\n");
	}
}

void Kernel::roomInit() {
	if (_roomInitFn)
		_vm->_script->runFunction(_roomInitFn);
	else {
		printf("Kernel::roomInit() _roomInitFn is NULL\n");
	}
}

void Kernel::roomDaemon() {
	if (_roomDaemonFn)
		_vm->_script->runFunction(_roomDaemonFn);
	else {
		printf("Kernel::roomDaemon() _roomDaemonFn is NULL\n");
	}
}

void Kernel::roomPreParser() {
	if (_roomPreParserFn)
		_vm->_script->runFunction(_roomPreParserFn);
	else {
		printf("Kernel::roomPreParser() _roomPreParserFn is NULL\n");
	}
}

void Kernel::roomParser() {
	if (_roomParserFn)
		_vm->_script->runFunction(_roomParserFn);
	else {
		printf("Kernel::roomParser() _roomParserFn is NULL\n");
	}
}

void Kernel::pauseGame(bool value) {
	paused = value;

	if (paused) pauseEngines();
	else unpauseEngines();
}

void Kernel::pauseEngines() {
	// TODO: A proper implementation of game pausing. At the moment I'm using a hard-coded
	// check in events.cpp on Kernel::paused to prevent any events going to the scene
}

void Kernel::unpauseEngines() {
	// TODO: A proper implementation of game unpausing
}

//--------------------------------------------------------------------------

Globals::Globals(M4Engine *vm): _vm(vm) {
}

Globals::~Globals() {
	for(uint32 i = 0; i < _madsVocab.size(); i++)
		free(_madsVocab[i]);
	_madsVocab.clear();

	for(uint32 i = 0; i < _madsQuotes.size(); i++)
		free(_madsQuotes[i]);
	_madsQuotes.clear();

	_madsMessages.clear();
}

bool Globals::isInterfaceVisible() {
	return _vm->_interfaceView->isVisible();
}

void Globals::loadMadsVocab() {
	Common::SeekableReadStream *vocabS = _vm->res()->get("vocab.dat");
	int curPos = 0;

	char buffer[30];
	strcpy(buffer, "");

	while(!vocabS->eos()) {
		buffer[curPos++] = vocabS->readByte();
		if (buffer[curPos - 1] == '\0') {
			// end of string, add it to the strings list
			_madsVocab.push_back(strdup(buffer));
			curPos = 0;
			strcpy(buffer, "");
		}
	}

	_vm->res()->toss("vocab.dat");
}

void Globals::loadMadsQuotes() {
	Common::SeekableReadStream *quoteS = _vm->res()->get("quotes.dat");
	int curPos = 0;

	char buffer[128];
	strcpy(buffer, "");

	while(!quoteS->eos()) {
		buffer[curPos++] = quoteS->readByte();
		if (buffer[curPos - 1] == '\0') {
			// end of string, add it to the strings list
			_madsQuotes.push_back(strdup(buffer));
			curPos = 0;
			strcpy(buffer, "");
		}
	}

	_vm->res()->toss("quotes.dat");
}

void Globals::loadMadsMessagesInfo() {
	Common::SeekableReadStream *messageS = _vm->res()->get("messages.dat");

	int16 count = messageS->readUint16LE();
	//printf("%i messages\n", count);

	for (int i = 0; i < count; i++) {
		MessageItem *curMessage = new MessageItem();
		curMessage->id = messageS->readUint32LE();
		curMessage->offset = messageS->readUint32LE();
		curMessage->uncompSize = messageS->readUint16LE();

		if (i > 0)
			_madsMessages[i - 1]->compSize = curMessage->offset - _madsMessages[i - 1]->offset;

		if (i == count - 1)
			curMessage->compSize = messageS->size() - curMessage->offset;

		//printf("id: %i, offset: %i, uncomp size: %i\n", curMessage->id, curMessage->offset, curMessage->uncompSize);
		_madsMessages.push_back(curMessage);
	}

	_vm->res()->toss("messages.dat");
}

char* Globals::loadMessage(uint32 index) {
	if (index > _madsMessages.size() - 1) {
		warning("Invalid message index: %i", index);
		return NULL;
	}

	FabDecompressor fab;
	byte *compData = new byte[_madsMessages[index]->compSize];
	byte *buffer = new byte[_madsMessages[index]->uncompSize];

	Common::SeekableReadStream *messageS = _vm->res()->get("messages.dat");
	messageS->seek(_madsMessages[index]->offset, SEEK_SET);
	messageS->read(compData, _madsMessages[index]->compSize);
	fab.decompress(compData, _madsMessages[index]->compSize, buffer, _madsMessages[index]->uncompSize);

	for (int i = 0; i < _madsMessages[index]->uncompSize - 1; i++)
		if (buffer[i] == '\0') buffer[i] = '\n';

	_vm->res()->toss("messages.dat");

	return (char*)buffer;
}

//--------------------------------------------------------------------------

Player::Player(M4Engine *vm) : _vm(vm) {
	commandsAllowed = true;
	needToWalk = false;
	readyToWalk = false;
	waitingForWalk = false;
	commandReady = false;
	strcpy(verb, "");
	strcpy(noun, "");
	strcpy(prep, "");
	strcpy(object, "");
}

void Player::setCommandsAllowed(bool value) {
	setCommandsAllowedFlag = true;
	commandsAllowed = value;
	if (value) {
		// Player commands are enabled again
		_vm->_mouse->lockCursor(CURSOR_ARROW);
		//_vm->_interfaceView->cancelSentence();
	} else {
		// Player commands are disabled, so show hourglass cursor
		_vm->_mouse->lockCursor(CURSOR_HOURGLASS);
	}
}

bool Player::said(const char *word1, const char *word2, const char *word3) {
	const char *words[3];
	words[0] = word1;
	words[1] = word2;
	words[2] = word2;
	for (int i = 0; i < 3; i++) {
		if (words[i])
			if ((scumm_stricmp(noun, words[i])) &&
				(scumm_stricmp(object, words[i])) &&
				(scumm_stricmp(verb, words[i])))
				return false;
	}
	return true;
}

bool Player::saidAny(const char *word1, const char *word2, const char *word3,
	const char *word4, const char *word5, const char *word6, const char *word7,
	const char *word8, const char *word9, const char *word10) {
	const char *words[10];
	words[0] = word1;
	words[1] = word2;
	words[2] = word3;
	words[3] = word4;
	words[4] = word5;
	words[5] = word6;
	words[6] = word7;
	words[7] = word8;
	words[8] = word9;
	words[9] = word10;
	for (int i = 0; i < 10; i++) {
		if (words[i]) {
			if (!scumm_stricmp(noun, words[i]))
				return true;
			if (!scumm_stricmp(object, words[i]))
				return true;
			if (!scumm_stricmp(verb, words[i]))
				return true;
		}
	}
	return false;
}


} // End of namespace M4