aboutsummaryrefslogtreecommitdiff
path: root/engines/mads/conversations.cpp
blob: 53b8e7900daa37563745e793787d0c364900a01a (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
/* 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 "mads/conversations.h"
#include "mads/mads.h"
#include "mads/compression.h"
#include "common/file.h"
#include "common/util.h"	// for Common::hexdump

namespace MADS {

#define MAX_SPEAKERS 5

enum DialogCommands {
	cmdNodeEnd = 0,
	//
	cmdHide = 2,
	cmdUnhide = 3,
	cmdMessage = 4,
	//
	//
	cmdGoto = 7,
	//
	cmdAssign = 9,
	cmdDialogEnd = 255
};

struct ConvDialog {
	int16 textLineIndex;	// 0-based
	int16 speechIndex;		// 1-based
	uint16 nodeOffset;		// offset in section 6
	uint16 nodeSize;		// size in section 6
};

struct ConvNode {
	uint16 index;
	uint16 dialogCount;
	int16 unk1;
	int16 unk2;
	int16 unk3;

	Common::Array<ConvDialog> dialogs;
};

struct ConvData {
	uint16 nodeCount;		// conversation nodes, each one containing several dialog options and messages
	uint16 dialogCount;		// messages (non-selectable) + texts (selectable)
	uint16 messageCount;	// messages (non-selectable)
	uint16 textLineCount;
	uint16 unk2;
	uint16 importCount;
	uint16 speakerCount;
	Common::String portraits[MAX_SPEAKERS];
	bool speakerExists[MAX_SPEAKERS];
	Common::String speechFile;
	Common::Array<Common::String> textLines;
	Common::Array<ConvNode> convNodes;
};

GameConversation::GameConversation(MADSEngine *vm)
	: _vm(vm) {
	_running = _restoreRunning = 0;
	_nextStartNode = nullptr;
}

GameConversation::~GameConversation() {
}

void GameConversation::get(int id) {
	Common::File inFile;
	Common::String fileName = Common::String::format("CONV%03d.CNV", id);
	// TODO: Also handle the .CND file

	inFile.open(fileName);
	MadsPack convFileUnpacked(&inFile);
	Common::SeekableReadStream *convFile = convFileUnpacked.getItemStream(0);

	char buffer[16];

	ConvData conv;

	// **** Section 0: Header *************************************************
	conv.nodeCount = convFile->readUint16LE();
	conv.dialogCount = convFile->readUint16LE();
	conv.messageCount = convFile->readUint16LE();
	conv.textLineCount = convFile->readUint16LE();
	conv.unk2 = convFile->readUint16LE();
	conv.importCount = convFile->readUint16LE();
	conv.speakerCount = convFile->readUint16LE();

	//debug("Conv %d has %d nodes, %d dialogs, %d messages, %d text lines, %d unk2, %d imports and %d speakers",
	//		id, conv.nodeCount, conv.dialogCount, conv.messageCount, conv.textLineCount, conv.unk2, conv.importCount, conv.speakerCount);

	for (uint16 i = 0; i < MAX_SPEAKERS; i++) {
		convFile->read(buffer, 16);
		conv.portraits[i] = buffer;
		//debug("Speaker %d, portrait %s", i, conv.portraits[i].c_str());
	}

	for (uint16 i = 0; i < MAX_SPEAKERS; i++) {
		conv.speakerExists[i] = convFile->readUint16LE();
		//debug("Speaker %d exists: %d", i, conv.speakerExists[i]);
	}

	convFile->read(buffer, 14);
	conv.speechFile = Common::String(buffer);
	//debug("Speech file %s", conv.speechFile.c_str());

	uint16 textLength = convFile->readUint16LE();	// Total text length in section 5
	convFile->skip(2);	// TODO: unknown
	uint16 commandLength = convFile->readUint16LE();	// Total length of commands in section 6
	//debug("Node entry commands length: %d", commandLength);

#if 0
	debug("Section 0 unknown bytes");
	byte *tmp0 = new byte[26];
	convFile->read(tmp0, 26);
	Common::hexdump(tmp0, 26);
	delete[] tmp0;
	delete convFile;
#else
	warning("Section 0 unknown bytes");
#endif
	// **** Section 1: Nodes **************************************************
	convFile = convFileUnpacked.getItemStream(1);

	for (uint16 i = 0; i < conv.nodeCount; i++) {
		ConvNode node;
		node.index = convFile->readUint16LE();
		node.dialogCount = convFile->readUint16LE();
		node.unk1 = convFile->readSint16LE();	// TODO
		node.unk2 = convFile->readSint16LE();	// TODO
		node.unk3 = convFile->readSint16LE();	// TODO
		conv.convNodes.push_back(node);
		//debug("Node %d, index %d, entries %d - %d, %d, %d", i, node.index, node.dialogCount, node.unk1, node.unk2, node.unk3);
	}
	delete convFile;

	// **** Section 2: Dialogs ************************************************
	convFile = convFileUnpacked.getItemStream(2);
	assert(convFile->size() == conv.dialogCount * 8);

	for (uint16 i = 0; i < conv.nodeCount; i++) {
		uint16 dialogCount = conv.convNodes[i].dialogCount;

		for (uint16 j = 0; j < dialogCount; j++) {
			ConvDialog dialog;
			dialog.textLineIndex = convFile->readSint16LE();
			dialog.speechIndex = convFile->readSint16LE();
			dialog.nodeOffset = convFile->readUint16LE();
			dialog.nodeSize = convFile->readUint16LE();
			conv.convNodes[i].dialogs.push_back(dialog);
			//debug("Node %d, dialog %d: text line %d, speech index %d, node offset %d, node size %d", j, i, dialog.textLineIndex, dialog.speechIndex, dialog.nodeOffset, dialog.nodeSize);
		}
	}
	delete convFile;

	// **** Section 3: ???? ***************************************************
#if 0
	debug("Section 3");
	convFile = convFileUnpacked.getItemStream(3);
	byte *tmp1 = new byte[convFile->size()];
	convFile->read(tmp1, convFile->size());
	Common::hexdump(tmp1, convFile->size());
	delete[] tmp1;
	delete convFile;
#else
	warning("Section 3 - TODO");
#endif
	// **** Section 4: Text line offsets **************************************
	convFile = convFileUnpacked.getItemStream(4);
	assert(convFile->size() == conv.textLineCount * 2);

	uint16 *textLineOffsets = new uint16[conv.textLineCount];	// deleted below in section 5
	for (uint16 i = 0; i < conv.textLineCount; i++)
		textLineOffsets[i] = convFile->readUint16LE();

	delete convFile;

	// **** Section 5: Text lines *********************************************
	convFile = convFileUnpacked.getItemStream(5);
	assert(convFile->size() == textLength);

	Common::String textLine;
	conv.textLines.resize(conv.textLineCount);
	char textLineBuffer[256];
	uint16 nextOffset;
	for (uint16 i = 0; i < conv.textLineCount; i++) {
		nextOffset = (i != conv.textLineCount - 1) ? textLineOffsets[i + 1] : convFile->size();
		convFile->read(textLineBuffer, nextOffset - textLineOffsets[i]);
		conv.textLines[i] = Common::String(textLineBuffer);	
		//debug("Text line %d: %s", i, conv.textLines[i].c_str());
	}

	delete[] textLineOffsets;
	delete convFile;

	// **** Section 6: Node entry commands ************************************
	convFile = convFileUnpacked.getItemStream(6);
	assert(convFile->size() == commandLength);

	for (uint16 i = 0; i < conv.nodeCount; i++) {
		uint16 dialogCount = conv.convNodes[i].dialogCount;

		for (uint16 j = 0; j < dialogCount; j++) {
			//ConvDialog dialog = conv.convNodes[i].dialogs[j];
			byte command;
			uint16 chk;

			do {
				command = convFile->readByte();
				chk = convFile->readUint16BE();
				if (chk != 0xFF00 && chk != 0x0000) {
					warning("Error while reading conversation node entries - bailing out");
					break;
				}

				switch (command) {
				case cmdNodeEnd:
					//debug("Node end");
					break;
				case cmdDialogEnd:
					//debug("Dialog end");
					break;
				case cmdHide: {
					byte count = convFile->readByte();
					for (byte k = 0; k < count; k++) {
						/*uint16 nodeRef = */convFile->readUint16LE();
						//debug("Hide node %d", nodeRef);
					}

					}
					break;
				case cmdUnhide: {
					byte count = convFile->readByte();
					for (byte k = 0; k < count; k++) {
						/*uint16 nodeRef = */convFile->readUint16LE();
						//debug("Unhide node %d", nodeRef);
					}

					}
					break;
				case cmdMessage:
					//debug("Message");
					convFile->skip(7);	// TODO
					break;
				case cmdGoto: {
					convFile->skip(3);	// unused?
					/*byte nodeRef = */convFile->readByte();
					//debug("Goto %d", nodeRef);
					}
					break;
				case cmdAssign: {
					convFile->skip(3);	// unused?
					/*uint16 value = */convFile->readUint16LE();
					/*uint16 variable = */convFile->readUint16LE();
					//debug("Variable %d = %d", variable, value);
					}
					break;
				default:
					error("Unknown conversation command %d", command);
					break;
				}
			} while (command != cmdNodeEnd && command != cmdDialogEnd);
		}
	}

	delete convFile;
	inFile.close();

	/*
	// DEBUG: Show the very first message, and play the very first speech
	_vm->_audio->setSoundGroup(conv.speechFile);
	uint16 firstText = 0, firstSpeech = 1;

	for (uint16 i = 0; i < conv.convNodes.size(); i++) {
		for (uint16 k = 0; k < conv.convNodes[i].dialogs.size(); k++) {
			if (conv.convNodes[i].dialogs[k].textLineIndex >= 0) {
				firstText = conv.convNodes[i].dialogs[k].textLineIndex;
				firstSpeech = conv.convNodes[i].dialogs[k].speechIndex;
				break;
			}
		}
	}

	_vm->_audio->playSound(firstSpeech - 1);

	TextDialog *dialog = new TextDialog(_vm, FONT_INTERFACE, Common::Point(0, 100), 30);
	dialog->addLine(conv.textLines[firstText]);
	dialog->show();
	delete dialog;
	*/

	warning("TODO GameConversation::get");
}

void GameConversation::run(int id) {
	warning("TODO GameConversation::run");
}

void GameConversation::stop() {
	warning("TODO GameConversation::stop");
}

void GameConversation::exportPointer(int *val) {
	warning("TODO GameConversation::exportPointer");
}

void GameConversation::exportValue(int val) {
	warning("TODO GameConversation::exportValue");
}

void GameConversation::setHeroTrigger(int val) {
	_vm->_game->_trigger = val;	// HACK
	_running = -1;	// HACK
	warning("TODO: GameConversation::setHeroTrigger");
}

void GameConversation::setInterlocutorTrigger(int val) {
	warning("TODO: GameConversation::setInterlocutorTrigger");
}

int* GameConversation::getVariable(int idx) {
	warning("TODO: GameConversation::getVariable");
	return nullptr;
}

void GameConversation::hold() {
	warning("TODO: GameConversation::hold");
}

void GameConversation::release() {
	warning("TODO: GameConversation::release");
}

void GameConversation::reset(int id) {
	warning("TODO: GameConversation::reset");
}

void GameConversation::abortConv() {
	warning("TODO: GameConversation::abort");
}
} // End of namespace MADS