aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/true_talk/true_talk_manager.cpp
blob: ed45ab26a0f93fb9eac8036b4fa5727c1e3acf38 (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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/* 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.
 *
 */

#include "titanic/true_talk/true_talk_manager.h"
#include "titanic/core/tree_item.h"
#include "titanic/npcs/true_talk_npc.h"
#include "titanic/game_manager.h"
#include "titanic/titanic.h"

#define MKTAG_BE(a3,a2,a1,a0) ((uint32)((a3) | ((a2) << 8) | ((a1) << 16) | ((a0) << 24)))

namespace Titanic {

int CTrueTalkManager::_v1;
int CTrueTalkManager::_v2;
int CTrueTalkManager::_v3;
bool CTrueTalkManager::_v4;
bool CTrueTalkManager::_v5;
int CTrueTalkManager::_v6;
int CTrueTalkManager::_v7;
bool CTrueTalkManager::_v8;
int CTrueTalkManager::_v9;
bool CTrueTalkManager::_v10;
int CTrueTalkManager::_v11[41];
CTrueTalkNPC *CTrueTalkManager::_currentNPC;

/*------------------------------------------------------------------------*/

CTrueTalkManager::CTrueTalkManager(CGameManager *owner) :
		_gameManager(owner), _scripts(&_titleEngine), _currentCharId(0),
		_dialogueFile(nullptr), _dialogueId(0) {
	_titleEngine.setup(3, 3);
	_quotes.load();
	_quotesTree.load();

	_currentNPC = nullptr;
	g_vm->_trueTalkManager = this;
}

CTrueTalkManager::~CTrueTalkManager() {
	clear();
	g_vm->_trueTalkManager = nullptr;
}

void CTrueTalkManager::save(SimpleFile *file) const {
	saveStatics(file);

	saveNPC(file, 101);
	saveNPC(file, 103);
	saveNPC(file, 104);
	saveNPC(file, 105);
	saveNPC(file, 111);
	saveNPC(file, 100);
	saveNPC(file, 112);
	saveNPC(file, 107);
	file->writeNumber(0);
}

void CTrueTalkManager::load(SimpleFile *file) {
	loadStatics(file);

	// Iterate through loading characters
	int charId = file->readNumber();
	while (charId) {
		loadNPC(file, charId);

		int ident1 = file->readNumber();
		int ident2 = file->readNumber();

		if (ident1 != MKTAG_BE('U', 'R', 'A', 'H')) {
			while (ident2 != MKTAG_BE('A', 'K', 'E', 'R')) {
				ident1 = ident2;
				ident2 = file->readNumber();

				if (!ident1)
					break;
			}
		}

		// Get start of next character
		charId = file->readNumber();
	}
}

void CTrueTalkManager::loadStatics(SimpleFile *file) {
	int count = file->readNumber();
	_v1 = file->readNumber();
	_v2 = file->readNumber();
	_v3 = file->readNumber();
	_v4 = file->readNumber() != 0;
	_v5 = file->readNumber() != 0;
	_v6 = file->readNumber();
	_v7 = file->readNumber();
	_v8 = file->readNumber() != 0;
	_v9 = file->readNumber();
	_v10 = file->readNumber() != 0;

	for (int idx = count; idx > 10; --idx)
		file->readNumber();

	int count2 = file->readNumber();
	for (int idx = 0; idx < count2; ++idx) {
		int v = file->readNumber();
		if (idx < 41)
			_v11[idx] = v;
	}
}

void CTrueTalkManager::saveStatics(SimpleFile *file) {
	file->writeNumber(10);
	file->writeNumber(_v1);
	file->writeNumber(_v2);
	file->writeNumber(_v3);
	file->writeNumber(_v4 ? 1 : 0);
	file->writeNumber(_v5 ? 1 : 0);
	file->writeNumber(_v6);
	file->writeNumber(_v7);
	file->writeNumber(_v8 ? 1 : 0);
	file->writeNumber(_v9);
	file->writeNumber(_v10 ? 1 : 0);

	file->writeNumber(41);
	for (int idx = 0; idx < 41; ++idx)
		file->writeNumber(_v11[idx]);
}

void CTrueTalkManager::clear() {
	delete _dialogueFile;
	_dialogueFile = nullptr;
	_currentCharId = 0;
}

void CTrueTalkManager::setFlags(int index, int val) {
	switch (index) {
	case 1:
		if (val >= 1 && val <= 3)
			_v3 = val;
		break;

	case 2:
		_v4 = !val;
		break;

	case 3:
		_v5 = val != 0;
		break;

	case 4:
		if (val >= 0 && val <= 3)
			_v6 = val;
		break;

	case 5:
		_v7 = val;
		break;

	case 6:
		_v8 = val != 0;
		break;

	default:
		if (index < 41)
			_v11[index] = val;
		break;
	}
}

void CTrueTalkManager::loadNPC(SimpleFile *file, int charId) {
	TTnpcScript *script = _scripts.getNpcScript(charId);
	if (script)
		script->load(file);
}

void CTrueTalkManager::saveNPC(SimpleFile *file, int charId) const {
	TTnpcScript *script = _scripts.getNpcScript(charId);
	if (script) {
		script->save(file);
		file->writeNumber(MKTAG_BE('U', 'R', 'A', 'H'));
		file->writeNumber(MKTAG_BE('A', 'K', 'E', 'R'));
	}
}

void CTrueTalkManager::preLoad() {
	// Delete any previous talkers
	for (TTtalkerList::iterator i = _talkers.begin(); i != _talkers.end(); ++i)
		delete *i;
	_talkers.clear();
}

void CTrueTalkManager::removeCompleted() {
	for (TTtalkerList::iterator i = _talkers.begin(); i != _talkers.end(); ) {
		TTtalker *talker = *i;

		if (talker->_done) {
			i = _talkers.erase(i);
			delete talker;
		} else {
			++i;
		}
	}
}

void CTrueTalkManager::start(CTrueTalkNPC *npc, uint id, CViewItem *view) {
	TTnpcScript *npcScript = getNpcScript(npc);
	TTroomScript *roomScript = getRoomScript();

	_titleEngine.reset();
	uint charId = npcScript->charId();
	loadAssets(npc, charId);

	_currentNPC = npc;
	_titleEngine._scriptHandler->scriptChanged(roomScript, npcScript, id);
	_currentNPC = nullptr;

	setDialogue(npc, roomScript, view);
}

void CTrueTalkManager::start3(CTrueTalkNPC *npc, CViewItem *view) {
	start(npc, 3, view);
}

void CTrueTalkManager::start4(CTrueTalkNPC *npc, CViewItem *view) {
	start(npc, 4, view);
}

TTnpcScript *CTrueTalkManager::getTalker(const CString &name) const {
	if (name.contains("Doorbot"))
		return _scripts.getNpcScript(104);
	else if (name.contains("DeskBot"))
		return _scripts.getNpcScript(103);
	else if (name.contains("LiftBot"))
		return _scripts.getNpcScript(105);
	else if (name.contains("Parrot"))
		return _scripts.getNpcScript(107);
	else if (name.contains("BarBot"))
		return _scripts.getNpcScript(100);
	else if (name.contains("ChatterBot"))
		return _scripts.getNpcScript(102);
	else if (name.contains("BellBot"))
		return _scripts.getNpcScript(101);
	else if (name.contains("MaitreD"))
		return _scripts.getNpcScript(112);
	else if (name.contains("Succubus") || name.contains("Sub"))
		return _scripts.getNpcScript(111);

	return nullptr;
}

TTnpcScript *CTrueTalkManager::getNpcScript(CTrueTalkNPC *npc) const {
	CString npcName = npc->getName();
	TTnpcScript *script = getTalker(npcName);

	if (!script) {
		// Fall back on the default NPC script
		script = _scripts.getNpcScript(101);
	}

	return script;
}

TTroomScript *CTrueTalkManager::getRoomScript() const {
	CRoomItem *room = _gameManager->getRoom();
	TTroomScript *script = nullptr;
	if (room) {
		int scriptId = room->getScriptId();
		if (scriptId)
			script = _scripts.getRoomScript(scriptId);
	}

	if (!script) {
		// Fall back on the default Room script
		script = _scripts.getRoomScript(110);
	}

	return script;
}

TTroomScript *CTrueTalkManager::getRoomScript(int roomId) const {
	TTroomScript *script = nullptr;
	if (roomId)
		script = _scripts.getRoomScript(roomId);

	if (!script)
		// Fall back on the default Room script
		script = _scripts.getRoomScript(110);

	return script;
}

void CTrueTalkManager::loadAssets(CTrueTalkNPC *npc, int charId) {
	// If assets for the character are already loaded, simply exit
	if (_currentCharId == charId)
		return;

	// Clear any previously loaded data
	clear();

	// Signal the NPC to get the asset details
	CTrueTalkGetAssetDetailsMsg detailsMsg;
	detailsMsg.execute(npc);

	if (!detailsMsg._filename.empty()) {
		_dialogueFile = new CDialogueFile(detailsMsg._filename, 20);
		_dialogueId = detailsMsg._numValue + 1;
	}
}

void CTrueTalkManager::processInput(CTrueTalkNPC *npc, CTextInputMsg *msg, CViewItem *view) {
	TTnpcScript *npcScript = getNpcScript(npc);
	TTroomScript *roomScript = getRoomScript();
	_titleEngine.reset();

	if (npcScript && roomScript) {
		_currentNPC = npc;
		_titleEngine._scriptHandler->processInput(roomScript, npcScript, TTstring(msg->_input));
		_currentNPC = nullptr;

		loadAssets(npc, npcScript->charId());
		setDialogue(npc, roomScript, view);
	}

	_currentNPC = nullptr;
}

void CTrueTalkManager::setDialogue(CTrueTalkNPC *npc, TTroomScript *roomScript, CViewItem *view) {
	// Get the dialog text
	CString dialogueStr = readDialogueString();
	if (dialogueStr.empty())
		return;

	uint speechDuration = readDialogueSpeech();
	TTtalker *talker = new TTtalker(this, npc);
	_talkers.push_back(talker);

	bool isParrot = npc->getName().contains("parrot");
	triggerNPC(npc);
	playSpeech(talker, roomScript, view, isParrot);
	talker->speechStarted(dialogueStr, _titleEngine._indexes[0], speechDuration);
}

#define STRING_BUFFER_SIZE 2048

CString CTrueTalkManager::readDialogueString() {
	byte buffer[STRING_BUFFER_SIZE];
	CString result;

	for (uint idx = 0; idx < _titleEngine._indexes.size(); ++idx) {
		if (idx != 0)
			result += " ";

		// Open a text entry from the dialogue file for access
		DialogueResource *textRes = _dialogueFile->openTextEntry(
			_titleEngine._indexes[idx] - _dialogueId);
		if (!textRes)
			continue;

		size_t entrySize = textRes->size();
		byte *tempBuffer = (entrySize < STRING_BUFFER_SIZE) ? buffer :
			new byte[entrySize + 1];

		_dialogueFile->read(textRes, tempBuffer, entrySize);
		buffer[entrySize] = '\0';

		// Close the resource
		_dialogueFile->closeEntry(textRes);

		// Strip off any non-printable characters
		for (byte *p = buffer; *p != '\0'; ++p) {
			if (*p < 32 || *p > 127)
				*p = ' ';
		}

		// Add string to result
		result += CString((const char *)buffer);

		// Free buffer if one was allocated
		if (entrySize >= STRING_BUFFER_SIZE)
			delete[] tempBuffer;
	}

	return result;
}

uint CTrueTalkManager::readDialogueSpeech() {
	_speechDuration = 0;

	for (uint idx = 0; idx < _titleEngine._indexes.size(); ++idx) {
		CWaveFile *waveFile = _gameManager->_sound.getTrueTalkSound(
			_dialogueFile, _titleEngine._indexes[idx] - _dialogueId);
		if (waveFile) {
			_speechDuration += waveFile->getDuration();
		}
	}

	return _speechDuration;
}

void CTrueTalkManager::triggerNPC(CTrueTalkNPC *npc) {
	CTrueTalkSelfQueueAnimSetMsg queueSetMsg;
	if (queueSetMsg.execute(npc)) {
		if (_speechDuration > 300) {
			CTrueTalkQueueUpAnimSetMsg upMsg(_speechDuration);
			upMsg.execute(npc);
		}
	} else {
		CTrueTalkGetAnimSetMsg getAnimMsg;
		if (_speechDuration > 300) {
			do {
				getAnimMsg.execute(npc);
				if (!getAnimMsg._endFrame)
					break;

				npc->playMovie(getAnimMsg._startFrame, getAnimMsg._endFrame, 0);
				getAnimMsg._endFrame = 0;

				uint numFrames = getAnimMsg._endFrame - getAnimMsg._startFrame;
				int64 val = (numFrames * 1000) * 0x88888889;
				uint diff = (val >> (32 + 5)) - 500;
				_speechDuration += diff;

				getAnimMsg._index++;
			} while (_speechDuration > 0);
		}
	}
}

void CTrueTalkManager::playSpeech(TTtalker *talker, TTroomScript *roomScript, CViewItem *view, bool isParrot) {
	uint milli, index;
	switch (roomScript->_scriptId) {
	case 101:
		milli = 300;
		index = 16;
		break;
	case 106:
	case 107:
	case 110:
	case 114:
	case 115:
	case 122:
		milli = 130;
		index = 10;
		break;
	case 108:
	case 109:
		milli = 200;
		index = 10;
		break;
	case 111:
	case 116:
	case 121:
		milli = 80;
		index = 12;
		break;
	case 112:
	case 124:
	case 128:
	case 130:
		milli = 80;
		index = 4;
		break;
	case 132:
		milli = 60;
		index = 4;
		break;
	default:
		milli = 0;
		index = 4;
		break;
	}

	// Setup proximities
	CProximity p1, p2, p3;
	if (isParrot) {
		p1._channelMode = 3;
		p2._channelMode = 5;
		p3._channelMode = 4;
	} else {
		p1._channelMode = 0;
		p2._channelMode = 1;
		p3._channelMode = 2;
	}

	if (milli > 0) {
		p3._channelVolume = (index * 3) / 2;
		p3._positioningMode = POSMODE_POLAR;
		p3._azimuth = -135.0;
		p3._range = 1.0;
		p3._elevation = 0;

		p2._channelVolume = (index * 3) / 4;
		p2._positioningMode = POSMODE_NONE;
		p2._azimuth = 135.0;
		p2._range = 1.0;
		p2._elevation = 0;
	}

	_gameManager->_sound.stopChannel(p1._channelMode);
	if (view) {
		p1._positioningMode = POSMODE_VECTOR;
		view->getPosition(p1._posX, p1._posY, p1._posZ);
	}

	// Loop through adding each of the speech portions in. We use the
	// _priorSoundHandle of CProximity to chain each successive speech
	// to start when the prior one finishes
	for (uint idx = 0; idx < _titleEngine._indexes.size(); ++idx) {
		uint id = _titleEngine._indexes[idx];
		if (id > 100000)
			continue;

		if (idx == (_titleEngine._indexes.size() - 1)) {
			// Final iteration of speech segments to play
			p1._endTalkerFn = &talkerEnd;
			p1._talker = talker;
		}

		// Start the speech
		p1._priorSoundHandle = _gameManager->_sound.playSpeech(_dialogueFile, id - _dialogueId, p1);
		if (!milli)
			continue;

		if (idx == 0)
			g_vm->_events->sleep(milli);

		p3._priorSoundHandle = _gameManager->_sound.playSpeech(_dialogueFile, id - _dialogueId, p3);
		if (idx == 0)
			g_vm->_events->sleep(milli);

		p2._priorSoundHandle = _gameManager->_sound.playSpeech(_dialogueFile, id - _dialogueId, p2);
	}
}

int CTrueTalkManager::getStateValue(int stateNum) {
	if (!_currentNPC)
		return -1000;

	CTrueTalkGetStateValueMsg msg(stateNum, -1000);
	msg.execute(_currentNPC);
	return msg._stateVal;
}

bool CTrueTalkManager::triggerAction(int action, int param) {
	if (!_currentNPC)
		return false;

	CTrueTalkTriggerActionMsg msg(action, param, 0);
	msg.execute(_currentNPC);
	return true;
}

void CTrueTalkManager::talkerEnd(TTtalker *talker) {
	if (talker)
		talker->endSpeech(0);
}

CGameManager *CTrueTalkManager::getGameManager() const {
	return _gameManager;
}

CGameState *CTrueTalkManager::getGameState() const {
	return _gameManager ? &_gameManager->_gameState : nullptr;
}

int CTrueTalkManager::getPassengerClass() const {
	CGameState *gameState = getGameState();
	return gameState ? gameState->_passengerClass : 4;
}

Season CTrueTalkManager::getCurrentSeason() const {
	CGameState *gameState = getGameState();
	return gameState ? gameState->_seasonNum : SEASON_SUMMER;
}

} // End of namespace Titanic