aboutsummaryrefslogtreecommitdiff
path: root/engines/director/director.cpp
blob: 2f77234dcd9e8034948adce8aa4f4e8bff400d2d (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
/* 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 "audio/mixer.h"

#include "common/config-manager.h"
#include "common/debug.h"
#include "common/scummsys.h"
#include "common/error.h"
#include "common/events.h"
#include "common/macresman.h"
#include "common/stream.h"
#include "common/system.h"
#include "common/textconsole.h"
#include "common/fs.h"

#include "engines/util.h"

#include "graphics/surface.h"

#include "director/director.h"
#include "director/dib.h"
#include "director/resource.h"
#include "director/score.h"
#include "director/lingo/lingo.h"
#include "director/sound.h"

namespace Director {

DirectorEngine::DirectorEngine(OSystem *syst, const DirectorGameDescription *gameDesc) : Engine(syst), _gameDescription(gameDesc),
		_rnd("director") {
	if (!_mixer->isReady())
		error("Sound initialization failed");

	// Setup mixer
	syncSoundSettings();
	_sharedCasts = new Common::HashMap<int, Cast *>;
	_sharedDIB = new Common::HashMap<int, Common::SeekableSubReadStreamEndian *>;
	_sharedBMP = new Common::HashMap<int, Common::SeekableSubReadStreamEndian *>;
	_sharedSTXT = new Common::HashMap<int, Common::SeekableSubReadStreamEndian *>;
	_sharedSound = new Common::HashMap<int, Common::SeekableSubReadStreamEndian *>;

	_mainArchive = 0;
	_macBinary = 0;
	//FIXME
	_sharedMMM = "SHARDCST.MMM";
	_movies = new Common::HashMap<Common::String, Score *>;

	const Common::FSNode gameDataDir(ConfMan.get("path"));
	SearchMan.addSubDirectoryMatching(gameDataDir, "data");
	SearchMan.addSubDirectoryMatching(gameDataDir, "install");
}

DirectorEngine::~DirectorEngine() {
	delete _sharedCasts;
	delete _sharedSound;
	delete _sharedBMP;
	delete _sharedSTXT;
	delete _sharedDIB;
	delete _movies;

	delete _mainArchive;
	delete _macBinary;
	delete _soundManager;
	delete _lingo;
	delete _currentScore;
	delete _currentPalette;
}

Common::Error DirectorEngine::run() {
	debug("Starting v%d Director game", getVersion());

	_sharedCasts = nullptr;
	_sharedSound = nullptr;
	_sharedBMP = nullptr;
	_sharedSTXT = nullptr;
	_sharedDIB = nullptr;

	_movies = nullptr;
	_currentPalette = nullptr;

	_macBinary = nullptr;
	_soundManager = nullptr;

	_lingo = new Lingo(this);
	_soundManager = new DirectorSound();

#if 0
	_mainArchive = nullptr;
	_currentScore = nullptr;

	_lingo->addCode("--\n\
	set x = 1\n\
	if x = 5 then exit\n\
	else put 10.0\n\
	repeat with x = 1 to 5\n\
		if x = 3 then put 30\n\
		else if x = 4 then put 40\n\
		else if x = 5 then put 50\n\
		else put 10.0\n\
		if x = 1 then\n\
		  put 1\n\
		else if x = 2 then\n\
		  put 2.1\n\
		  put 2.2\n\
		else if x = 3 then put 3\n\
		end if\n\
		if x = 4 then put 4\n\
		else put 5\n\
	end repeat\n\
	set z = \"foo bar baz\"\n\
	set z1 = z & \" meow\"\n\
	set z1 = z1 && \"woof\"\n\
	put z\n\
	put z1\n\
", kMovieScript, 2);

_lingo->executeScript(kMovieScript, 2);

return Common::kNoError;

	_lingo->addCode("--\n\
macro SHIPX\n\
global x, y\n\
set x = Random(5)\n\
if x = 1 then\n\
go \"Zoom\"\n\
exit\n\
end if\n\
if x >1 then\n\
set y = 10\n\
exit\n\
end if\n\
put 100\n\
\n\
--\n\
macro ZIPX\n\
set x = Random(5)\n\
if x = 1 then\n\
go \"ZIP\"\n\
exit\n\
end if\n\
if x >1 then\n\
put x\n\
exit\n\
end if\n\
\n\
--\n\
macro check par1, par2		\n\
, par3\n\
if par1 = 3 then\n\
  put -3\n\
else\n\
  put 0\n\
end if  \n\
if par2 = 2 then	\n\
  put 2		\n\
else	\n\
  put 0\n\
end if\n\
put par1\n\
put par2\n\
put par3\n\
", kMovieScript, 1);

_lingo->addCode("check(2, 3)\n\
global x, y\n\
set y = 8\n\
shipx\n\
put x\n\
zipx\n\
put x\n\
put y\n\
check(1, 2, 3)\n\
check 4, 5, 6\n\
check 7, 8\n\
if random(4) > 2 then put 1000\n\
set z = 5.5\n\
set z1 = 2\n\
set z2 = z / z1\n\
put z\n\
put z1\n\
put z2\n\
put integer(z2)\n\
put cos(z2)\n\
", kMovieScript, 2);

_lingo->executeScript(kMovieScript, 2);

return Common::kNoError;


	_lingo->addCode("mci \"open MM\\T005045a.wav type WaveAudio alias T005045a\"\n\
	mci \"play T005045a from 22710 to 32872\"", kMovieScript, 1);

	_lingo->addCode("go to frame \"Open23\" of movie \"OpenCabin23\"\n\
go \"CARDBACK\"\n\
go movie \"BAR 1\"\n\
go to \"Open23\" of movie \"OpenCabin23\"\n\
go to \"Chair\"\n\
set x = 2 + 3 * (4 / 2)\n\
put x\n", kMovieScript, 2);

_lingo->addCode("set x = 5\n\
if x <= 5 then set x = 6 end if\n\
if (x = 5) then\n\
   set x = 7 -- this is comment\n\
else\n\
	set x = 8\n\
	-- this is another comment\n\
end if\n\
put x\n\
-- this is more comment\n\
set y = 1\n\
repeat while (y < 5)\n\
  set y = y + 1\n\
  put y\n\
end repeat\n\
\n\
repeat with z = 10 to 15\n\
	put z\n\
end repeat\n\
repeat with y = 5 down to 1\n\
put y\n\
end repeat\n\
", kMovieScript, 3);

	_lingo->executeScript(kMovieScript, 3);

	return Common::kNoError;
#endif

	//FIXME
	_mainArchive = new RIFFArchive();
	_mainArchive->openFile("bookshelf_example.mmm");
	_currentScore = new Score(this);
	debug(0, "Score name %s", _currentScore->getMacName().c_str());

	_currentScore->loadArchive();
	_currentScore->startLoop();

	if (getPlatform() == Common::kPlatformWindows)
		loadEXE();
	else
		loadMac();

	return Common::kNoError;
}

Common::HashMap<Common::String, Score *> DirectorEngine::loadMMMNames(Common::String folder) {
	Common::FSNode directory(folder);
	Common::FSList movies;

	Common::HashMap<Common::String, Score *> nameMap;
	directory.getChildren(movies, Common::FSNode::kListFilesOnly);

	if (!movies.empty()) {
		for (Common::FSList::const_iterator i = movies.begin(); i != movies.end(); ++i) {
			if (i->getName() == _sharedMMM) {
				loadSharedCastsFrom(i->getPath());
				continue;
			}

			RIFFArchive *arc = new RIFFArchive();
			arc->openFile(i->getPath());
			Score *sc = new Score(this);
			nameMap[sc->getMacName()] = sc;
		}
	}

	return nameMap;
}

void DirectorEngine::loadEXE() {
	Common::SeekableReadStream *exeStream = SearchMan.createReadStreamForMember(getEXEName());
	if (!exeStream)
		error("Failed to open EXE '%s'", getEXEName().c_str());

	_lingo->processEvent(kEventStart, 0);

	exeStream->seek(-4, SEEK_END);
	exeStream->seek(exeStream->readUint32LE());

	switch (getVersion()) {
	case 3:
		loadEXEv3(exeStream);
		break;
	case 4:
		loadEXEv4(exeStream);
		break;
	case 5:
		loadEXEv5(exeStream);
		break;
	case 7:
		loadEXEv7(exeStream);
		break;
	default:
		error("Unhandled Windows EXE version %d", getVersion());
	}
}

void DirectorEngine::loadEXEv3(Common::SeekableReadStream *stream) {
	uint16 entryCount = stream->readUint16LE();
	if (entryCount != 1)
		error("Unhandled multiple entry v3 EXE");

	stream->skip(5); // unknown

	stream->readUint32LE(); // Main MMM size
	Common::String mmmFileName = readPascalString(*stream);
	Common::String directoryName = readPascalString(*stream);

	debug("Main MMM: '%s'", mmmFileName.c_str());
	debug("Directory Name: '%s'", directoryName.c_str());

	_mainArchive = new RIFFArchive();

	if (!_mainArchive->openFile(mmmFileName))
		error("Could not open '%s'", mmmFileName.c_str());

	delete stream;
}

void DirectorEngine::loadEXEv4(Common::SeekableReadStream *stream) {
	if (stream->readUint32BE() != MKTAG('P', 'J', '9', '3'))
		error("Invalid projector tag found in v4 EXE");

	uint32 rifxOffset = stream->readUint32LE();
	/* uint32 fontMapOffset = */ stream->readUint32LE();
	/* uint32 resourceForkOffset1 = */ stream->readUint32LE();
	/* uint32 resourceForkOffset2 = */ stream->readUint32LE();
	stream->readUint32LE(); // graphics DLL offset
	stream->readUint32LE(); // sound DLL offset
	/* uint32 rifxOffsetAlt = */ stream->readUint32LE(); // equivalent to rifxOffset

	loadEXERIFX(stream, rifxOffset);
}

void DirectorEngine::loadEXEv5(Common::SeekableReadStream *stream) {
	if (stream->readUint32LE() != MKTAG('P', 'J', '9', '5'))
		error("Invalid projector tag found in v5 EXE");

	uint32 rifxOffset = stream->readUint32LE();
	stream->readUint32LE(); // unknown
	stream->readUint32LE(); // unknown
	stream->readUint32LE(); // unknown
	/* uint16 screenWidth = */ stream->readUint16LE();
	/* uint16 screenHeight = */ stream->readUint16LE();
	stream->readUint32LE(); // unknown
	stream->readUint32LE(); // unknown
	/* uint32 fontMapOffset = */ stream->readUint32LE();

	loadEXERIFX(stream, rifxOffset);
}

void DirectorEngine::loadEXEv7(Common::SeekableReadStream *stream) {
	if (stream->readUint32LE() != MKTAG('P', 'J', '0', '0'))
		error("Invalid projector tag found in v7 EXE");

	uint32 rifxOffset = stream->readUint32LE();
	stream->readUint32LE(); // unknown
	stream->readUint32LE(); // unknown
	stream->readUint32LE(); // unknown
	stream->readUint32LE(); // unknown
	stream->readUint32LE(); // some DLL offset

	loadEXERIFX(stream, rifxOffset);
}

void DirectorEngine::loadEXERIFX(Common::SeekableReadStream *stream, uint32 offset) {
	_mainArchive = new RIFXArchive();

	if (!_mainArchive->openStream(stream, offset))
		error("Failed to load RIFX from EXE");
}

void DirectorEngine::loadMac() {
	if (getVersion() < 4) {
		// The data is part of the resource fork of the executable
		_mainArchive = new MacArchive();

		if (!_mainArchive->openFile(getEXEName()))
			error("Failed to open Mac binary '%s'", getEXEName().c_str());
	} else {
		// The RIFX is located in the data fork of the executable
		_macBinary = new Common::MacResManager();

		if (!_macBinary->open(getEXEName()) || !_macBinary->hasDataFork())
			error("Failed to open Mac binary '%s'", getEXEName().c_str());

		Common::SeekableReadStream *dataFork = _macBinary->getDataFork();
		_mainArchive = new RIFXArchive();

		// First we need to detect PPC vs. 68k

		uint32 tag = dataFork->readUint32BE();
		uint32 startOffset;

		if (SWAP_BYTES_32(tag) == MKTAG('P', 'J', '9', '3') || tag == MKTAG('P', 'J', '9', '5') || tag == MKTAG('P', 'J', '0', '0')) {
			// PPC: The RIFX shares the data fork with the binary
			startOffset = dataFork->readUint32BE();
		} else {
			// 68k: The RIFX is the only thing in the data fork
			startOffset = 0;
		}

		if (!_mainArchive->openStream(dataFork, startOffset))
			error("Failed to load RIFX from Mac binary");
	}
}

Common::String DirectorEngine::readPascalString(Common::SeekableReadStream &stream) {
	byte length = stream.readByte();
	Common::String x;

	while (length--)
		x += (char)stream.readByte();

	return x;
}

void DirectorEngine::setPalette(byte *palette, uint16 count) {
	_currentPalette = palette;
	_currentPaletteLength = count;
}

void DirectorEngine::loadSharedCastsFrom(Common::String filename) {
	Archive *shardcst;

	if (getVersion() < 4) {
		shardcst = new RIFFArchive();
	} else {
		shardcst = new RIFXArchive();
	}

	shardcst->openFile(filename);

	Score *castScore = new Score(this);
	Common::SeekableSubReadStreamEndian *castStream = shardcst->getResource(MKTAG('V','W','C','R'), 1024);

	castScore->loadCastData(*castStream);
	*_sharedCasts = castScore->_casts;

	Common::Array<uint16> dib = shardcst->getResourceIDList(MKTAG('D','I','B',' '));

	if (dib.size() != 0) {
		Common::Array<uint16>::iterator iterator;
		for (iterator = dib.begin(); iterator != dib.end(); ++iterator) {
			debug(3, "Shared DIB %d", *iterator);
			_sharedDIB->setVal(*iterator, shardcst->getResource(MKTAG('D','I','B',' '), *iterator));
		}
	}

	Common::Array<uint16> stxt = shardcst->getResourceIDList(MKTAG('S','T','X','T'));

	if (stxt.size() != 0) {
		Common::Array<uint16>::iterator iterator;
		for (iterator = stxt.begin(); iterator != stxt.end(); ++iterator) {
			debug(3, "Shared STXT %d", *iterator);
			_sharedSTXT->setVal(*iterator, shardcst->getResource(MKTAG('S','T','X','T'), *iterator));
		}
	}

	Common::Array<uint16> bmp = shardcst->getResourceIDList(MKTAG('B','I','T','D'));

	if (bmp.size() != 0) {
		Common::Array<uint16>::iterator iterator;
		for (iterator = bmp.begin(); iterator != bmp.end(); ++iterator) {
			_sharedBMP->setVal(*iterator, shardcst->getResource(MKTAG('B','I','T','D'), *iterator));
		}
	}

	Common::Array<uint16> sound = shardcst->getResourceIDList(MKTAG('S','N','D',' '));

	if (stxt.size() != 0) {
		Common::Array<uint16>::iterator iterator;
		for (iterator = sound.begin(); iterator != sound.end(); ++iterator) {
			_sharedSound->setVal(*iterator, shardcst->getResource(MKTAG('S','N','D',' '), *iterator));
		}
	}
}

} // End of namespace Director