aboutsummaryrefslogtreecommitdiff
path: root/engines/startrek/startrek.cpp
blob: 3acd3d9c8a24882501b8372250a470d768c3a732 (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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/* 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 "base/plugins.h"
#include "base/version.h"
#include "common/archive.h"
#include "common/config-manager.h"
#include "common/debug-channels.h"
#include "common/error.h"
#include "common/events.h"
#include "common/file.h"
#include "common/macresman.h"
#include "graphics/surface.h"
#include "engines/util.h"
#include "video/qt_decoder.h"

#include "startrek/filestream.h"
#include "startrek/lzss.h"
#include "startrek/startrek.h"

namespace StarTrek {

StarTrekEngine::StarTrekEngine(OSystem *syst, const StarTrekGameDescription *gamedesc) :
	Engine(syst),
	_gameDescription(gamedesc),
	_kirkObject(&_objectList[0]),
	_spockObject(&_objectList[1]),
	_mccoyObject(&_objectList[2]),
	_redshirtObject(&_objectList[3]) {

	DebugMan.addDebugChannel(kDebugSound, "sound", "Sound");
	DebugMan.addDebugChannel(kDebugGraphics, "graphics", "Graphics");

	_gfx = nullptr;
	_sound = nullptr;
	_macResFork = nullptr;
	_room = nullptr;

	_redshirtDead = false;

	_clockTicks = 0;

	_musicEnabled = true;
	_sfxEnabled = true;
	_word_467a6 = true;
	_musicWorking = true;
	_sfxWorking = true;
	_finishedPlayingSpeech = false;

	_mouseControllingShip = false;
	_keyboardControlsMouse = true;

	_inQuitGameMenu = false;
}

StarTrekEngine::~StarTrekEngine() {
	delete _gfx;
	delete _sound;
	delete _macResFork;

	delete _room;
}

Common::Error StarTrekEngine::run() {
	_gfx = new Graphics(this);
	_sound = new Sound(this);

	if (getPlatform() == Common::kPlatformMacintosh) {
		_macResFork = new Common::MacResManager();
		if (!_macResFork->open("Star Trek Data"))
			error("Could not load Star Trek Data");
		assert(_macResFork->hasDataFork() && _macResFork->hasResFork());
	}

	initGraphics(SCREEN_WIDTH, SCREEN_HEIGHT);
	initializeEventsAndMouse();

	_frameIndex = 0;

	_gameMode = -1;
	_lastGameMode = -1;

	runGameMode(GAMEMODE_BEAMDOWN);
	return Common::kNoError;


#if 1
	_room = new Room(this, "DEMON0");
	_gfx->loadPalette("PALETTE");
	_gfx->loadPri("DEMON0.PRI");

	_sound->loadMusicFile("GROUND");


	while (true) {
		_gfx->showOptionsMenu(0, 0);
	}
	
	while (!shouldQuit()) {
		pollSystemEvents();
	}

	return Common::kNoError;
#endif
}

Common::Error StarTrekEngine::runGameMode(int mode) {
	_gameMode = mode;

	_sound->stopAllVocSounds();
	if (!_sound->_loopingAudioName.empty())
		_sound->playVoc(_sound->_loopingAudioName);

	if (_gameMode == GAMEMODE_START)
		_gameMode = GAMEMODE_BRIDGE;

	while (true) {
		TrekEvent event;

		if (_gameMode != _lastGameMode) {
			// Cleanup previous game mode
			switch (_lastGameMode) {
			case GAMEMODE_BRIDGE:
				//cleanupBridgeMode();
				break;

			case GAMEMODE_AWAYMISSION:
				//cleanupAwayMissionMode();
				break;

			case GAMEMODE_BEAMDOWN:
			case GAMEMODE_BEAMUP:
				break;
			}

			_lastGameMode = _gameMode;

			// Load next game mode
			switch (_gameMode) {
			case GAMEMODE_BRIDGE:
				_sound->loadMusicFile("bridge");
				//initBridge();
				break;

			case GAMEMODE_AWAYMISSION:
				//initAwayMission();
				break;

			case GAMEMODE_BEAMDOWN:
				_redshirtDead = false;
				_sound->loadMusicFile("ground");
				runTransportSequence("teled");
				_gameMode = GAMEMODE_AWAYMISSION;
				continue; // Back to start of loop

			case GAMEMODE_BEAMUP:
				runTransportSequence("teleb");
				_gameMode = GAMEMODE_BRIDGE;
				//sub_15c61();
				_sound->stopAllVocSounds();
				_sound->playVoc("bridloop");
				continue; // Back to start of loop
			}
		}

		// Run current game mode
		switch (_gameMode) {
		case GAMEMODE_BRIDGE:
			popNextEvent(&event);
			//runBridge();
			break;

		case GAMEMODE_AWAYMISSION:
			popNextEvent(&event);
			_system->updateScreen();
			//runAwayMission();
			break;

		case GAMEMODE_BEAMDOWN:
		case GAMEMODE_BEAMUP:
			error("Can't be here.");
			break;
		}
	}

	return Common::kNoError;
}

void StarTrekEngine::runTransportSequence(const Common::String &name) {
	const uint16 crewmanTransportPositions[][2] = {
		{ 0x8e, 0x7c },
		{ 0xbe, 0x7c },
		{ 0x7e, 0x72 },
		{ 0xaa, 0x72 }
	};

	_sound->stopAllVocSounds();
	// sub_1e70d();
	objectFunc1();
	initObjects();

	SharedPtr<Bitmap> bgImage = _gfx->loadBitmap("transprt");
	_gfx->setBackgroundImage(bgImage);
	_gfx->clearPri();
	_gfx->loadPalette("palette");
	_gfx->drawDirectToScreen(bgImage);
	_system->updateScreen();

	for (int i = 0; i < (_redshirtDead ? 3 : 4); i++) {
		Common::String filename = getCrewmanAnimFilename(i, name);
		int x = crewmanTransportPositions[i][0];
		int y = crewmanTransportPositions[i][1];
		loadAnimationForObject(i, filename, x, y, 256);
		_objectList[i].animationString[0] = '\0';
	}

	if (_missionToLoad.equalsIgnoreCase("feather") && name[4] == 'b') {
		loadAnimationForObject(9, "qteleb", 0x61, 0x79, 0x100);
	}
	else if (_missionToLoad.equalsIgnoreCase("trial")) {
		if (name[4] == 'd') {
			loadAnimationForObject(9, "qteled", 0x61, 0x79, 0x100);
		}
		/* TODO
		else if (word_51156 >= 3) {
			loadAnimationForObject(9, "qteleb", 0x61, 0x79, 0x100);
		}
		*/
	}

	loadAnimationForObject(8, "transc", 0, 0, 0x100);

	// TODO: redraw mouse and sprite_52c4e?

	_gfx->drawAllSprites();
	// sub_1e6ab();

	playSoundEffectIndex(0x0a);

	if (name.equalsIgnoreCase("teled"))
		playSoundEffectIndex(0x08);
	else
		playSoundEffectIndex(0x09);

	while (_objectList[0].field62 == 0) {
		TrekEvent event;
		if (popNextEvent(&event)) {
			if (event.type == TREKEVENT_TICK) {
				// TODO: redraw sprite_52c4e?
				_frameIndex++;
				updateObjectAnimations();
				_gfx->drawAllSprites();
			}
		}
	}

	// TODO: redraw sprite_52c4e?

	_gfx->drawAllSprites();
	// sub_1e70d();
	objectFunc1();
	initObjects();
}

Room *StarTrekEngine::getRoom() {
	return _room;
}

void StarTrekEngine::playSoundEffectIndex(int index) {
	switch (index) {
	case 0x04:
		_sound->playVoc("tricorde");
		break;
	case 0x05:
		_sound->playVoc("STDOOR1");
		break;
	case 0x06:
		_sound->playVoc("PHASSHOT");
		break;
	case 0x07:
		_sound->playMidiTrack(index);
		break;
	case 0x08:
		_sound->playVoc("TRANSDEM");
		break;
	case 0x09:
		_sound->playVoc("TRANSMAT");
		break;
	case 0x0a:
		_sound->playVoc("TRANSENE");
		break;
	case 0x10: // Menu selection sound
		_sound->playMidiTrack(index);
		break;
	case 0x22:
		_sound->playVoc("HAILING");
		break;
	case 0x24:
		_sound->playVoc("PHASSHOT");
		break;
	case 0x25:
		_sound->playVoc("PHOTSHOT");
		break;
	case 0x26:
		_sound->playVoc("HITSHIEL");
		break;
	case 0x27:
		_sound->playMidiTrack(index);
		break;
	case 0x28:
		_sound->playVoc("REDALERT");
		break;
	case 0x29:
		_sound->playVoc("WARP");
		break;
	default:
	    break;
	}
}

void StarTrekEngine::playSpeech(const Common::String &filename) {
	_sound->playSpeech(filename.c_str());
}

void StarTrekEngine::stopPlayingSpeech() {
	_sound->stopPlayingSpeech();
}

void StarTrekEngine::initObjects() {
	for (int i = 0; i < MAX_OBJECTS; i++) {
		_objectList[i] = Object();
	}
	for (int i = 0; i < MAX_OBJECTS / 2; i++)
		_objectBanFiles[i].reset();

	strcpy(_kirkObject->animationString, "kstnd");
	strcpy(_spockObject->animationString, "sstnd");
	strcpy(_mccoyObject->animationString, "mstnd");
	strcpy(_redshirtObject->animationString, "rstnd");
}

int StarTrekEngine::loadAnimationForObject(int objectIndex, const Common::String &animName, uint16 x, uint16 y, uint16 arg8) {
	debugC(6, kDebugGraphics, "Load animation '%s' on object %d", animName.c_str(), objectIndex);

	Object *object;

	if (objectIndex == -1) {
		// TODO
	}
	else
		object = &_objectList[objectIndex];

	if (object->spriteDrawn) {
		releaseAnim(object);
		drawObjectToScreen(object, animName, x, y, arg8, false);
	}
	else {
		drawObjectToScreen(object, animName, x, y, arg8, true);
	}

	object->field64 = 0;
	object->field66 = 0;

	return objectIndex;
}

void StarTrekEngine::updateObjectAnimations() {
	for (int i = 0; i < MAX_OBJECTS; i++) {
		Object *object = &_objectList[i];
		if (!object->spriteDrawn)
			continue;

		switch (object->animType) {
		case 0:
		case 2:
			if (_frameIndex >= object->frameToStartNextAnim) {
				int nextAnimIndex = 0; // TODO: "chooseNextAnimFrame" function
				object->animFile->seek(18 + nextAnimIndex + object->animFrame * 22, SEEK_SET);
				byte nextAnimFrame = object->animFile->readByte();

				debugC(7, kDebugGraphics, "Object %d animation frame %d", i, nextAnimFrame);

				if (object->animFrame != nextAnimFrame) {
					if (nextAnimFrame == object->numAnimFrames - 1) {
						object->field62++;
						if (object->field64 != 0) {
							// sub_20099(10, object->field66, 0, 0);
						}
					}
				}

				object->animFrame = nextAnimFrame;
				if (object->animFrame >= object->numAnimFrames) {
					if (object->animationString[0] == '\0')
						removeObjectFromScreen(i);
					/*
					else // TODO
						initStandAnim(i);
						*/
				}
				else {
					Sprite *sprite = &object->sprite;

					object->animFile->seek(object->animFrame * 22, SEEK_SET);
					char animFrameFilename[16];
					object->animFile->read(animFrameFilename, 16);
					sprite->setBitmap(loadAnimationFrame(animFrameFilename, object->scale));

					memset(object->animationString4, 0, 16);
					strncpy(object->animationString4, animFrameFilename, 15);

					object->animFile->seek(10 + object->animFrame * 22, SEEK_SET);
					uint16 xOffset = object->animFile->readUint16();
					uint16 yOffset = object->animFile->readUint16();
					uint16 basePriority = object->animFile->readUint16();
					uint16 frames = object->animFile->readUint16();

					sprite->pos.x = xOffset + object->field5e;
					sprite->pos.y = yOffset + object->field60;
					sprite->drawPriority = _gfx->getPriValue(0, yOffset + object->field60) + basePriority;
					sprite->bitmapChanged = true;

					object->frameToStartNextAnim = frames + _frameIndex;
				}
			}
			break;
		case 1: // TODO
			warning("Unimplemented anim type %d", object->animType);
			break;
		default:
			error("Invalid anim type.");
			break;
		}
	}
}

void StarTrekEngine::removeObjectFromScreen(int objectIndex) {
	Object *object = &_objectList[objectIndex];

	if (object->spriteDrawn != 1)
		return;

	debugC(6, kDebugGraphics, "Stop drawing object %d", objectIndex);

	Sprite *sprite = &object->sprite;
	sprite->field16 = true;
	sprite->bitmapChanged = true;
	_gfx->drawAllSprites();
	_gfx->delSprite(sprite);
	releaseAnim(object);
}

void StarTrekEngine::objectFunc1() {
	for (int i = 0; i < MAX_OBJECTS; i++) {
		if (_objectList[i].spriteDrawn == 1) {
			removeObjectFromScreen(i);
		}
	}

	for (int i = 0; i < MAX_OBJECTS / 2; i++) {
		_objectBanFiles[i].reset();
	}
}

void StarTrekEngine::drawObjectToScreen(Object *object, const Common::String &_animName, uint16 x, uint16 y, uint16 arg8, bool addSprite) {
	Common::String animFilename = _animName;
	if (_animName.hasPrefixIgnoreCase("stnd") /* && word_45d20 == -1 */) // TODO
		animFilename += 'j';
	memcpy(object->animationString3, _animName.c_str(), sizeof(object->animationString3));

	object->animType = 2;
	object->animFile = loadFile(animFilename + ".anm");
	object->numAnimFrames = object->animFile->size() / 22;
	object->animFrame = 0;
	object->field5e = x;
	object->field60 = y;
	object->field62 = 0;
	object->scale = arg8;

	object->animFile->seek(16, SEEK_SET);
	object->frameToStartNextAnim = object->animFile->readUint16() + _frameIndex;

	char firstFrameFilename[10];
	object->animFile->seek(0, SEEK_SET);
	object->animFile->read(firstFrameFilename, 10);

	Sprite *sprite = &object->sprite;
	if (addSprite)
		_gfx->addSprite(sprite);

	sprite->setBitmap(loadAnimationFrame(firstFrameFilename, arg8));
	memset(object->animationString4, 0, sizeof(char) * 10);
	strncpy(object->animationString4, firstFrameFilename, sizeof(char) * 9);

	object->scale = arg8;

	object->animFile->seek(10, SEEK_SET);
	uint16 xOffset = object->animFile->readUint16();
	uint16 yOffset = object->animFile->readUint16();
	uint16 basePriority = object->animFile->readUint16();

	sprite->pos.x = xOffset + object->field5e;
	sprite->pos.y = yOffset + object->field60;
	sprite->drawPriority = _gfx->getPriValue(0, yOffset + object->field60) + basePriority;
	sprite->bitmapChanged = true;

	object->spriteDrawn = 1;
}

void StarTrekEngine::releaseAnim(Object *object) {
	switch (object->animType) {
	case 0:
	case 2:
		object->sprite.bitmap.reset();
		object->animFile.reset();
		break;
	case 1: // TODO
		warning("Unimplemented anim type %d", object->animType);
		break;
	default:
		error("Invalid anim type.");
		break;
	}

	object->spriteDrawn = 0;
}

SharedPtr<Bitmap> StarTrekEngine::loadAnimationFrame(const Common::String &filename, uint16 arg2) {
	SharedPtr<Bitmap> bitmapToReturn;

	char basename[5];
	strncpy(basename, filename.c_str()+1, 4);
	basename[4] = '\0';

	char c = filename[0];
	if ((strcmp(basename, "stnd") == 0 || strcmp(basename, "tele") == 0)
			&& (c == 'm' || c == 's' || c == 'k' || c == 'r')) {
		if (c == 'm') {
			// Mccoy has the "base" animations for all crewmen
			bitmapToReturn = _gfx->loadBitmap(filename);
		}
		else {
			// All crewman other than mccoy copy the animation frames from mccoy, change
			// the colors of the uniforms, and load an "xor" file to redraw the face.

			// TODO: The ".$bm" extension is a "virtual file"? Caches the changes to the
			// file made here?
			// bitmapToReturn = _gfx->loadBitmap(filename + ".$bm");

			if (bitmapToReturn == nullptr) {
				Common::String mccoyFilename = filename;
				mccoyFilename.setChar('m', 0);
				SharedPtr<Bitmap> bitmap = _gfx->loadBitmap(mccoyFilename);

				uint16 width = bitmap->width;
				uint16 height = bitmap->height;

				bitmapToReturn = SharedPtr<Bitmap>(new Bitmap(width, height));
				bitmapToReturn->xoffset = bitmap->xoffset;
				bitmapToReturn->yoffset = bitmap->yoffset;

				// Change uniform color
				int16 colorShift;
				switch (c) {
				case 'k':
					colorShift = 8;
					break;
				case 'r':
					colorShift = -8;
					break;
				case 's':
					colorShift = 0;
					break;
				}

				if (colorShift == 0) {
					memcpy(bitmapToReturn->pixels, bitmap->pixels, width * height);
				}
				else {
					byte *src = bitmap->pixels;
					byte *dest = bitmapToReturn->pixels;
					byte baseUniformColor = 0xa8;

					for (int i = 0; i < width * height; i++) {
						byte b = *src++;
						if (b >= baseUniformColor && b < baseUniformColor + 8)
							*dest++ = b + colorShift;
						else
							*dest++ = b;
					}
				}

				// Redraw face with xor file
				SharedPtr<FileStream> xorFile = loadFile(filename + ".xor");
				xorFile->seek(0, SEEK_SET);
				uint16 xoffset = bitmap->xoffset - xorFile->readUint16();
				uint16 yoffset = bitmap->yoffset - xorFile->readUint16();
				uint16 xorWidth = xorFile->readUint16();
				uint16 xorHeight = xorFile->readUint16();

				byte *dest = bitmapToReturn->pixels + yoffset * bitmap->width + xoffset;

				for (int i = 0; i < xorHeight; i++) {
					for (int j = 0; j < xorWidth; j++)
						*dest++ ^= xorFile->readByte();
					dest += (bitmap->width - xorWidth);
				}
			}
		}
	}
	else {
		// TODO: when loading a bitmap, it passes a different argument than is standard to
		// the "file loading with cache" function...
		bitmapToReturn = _gfx->loadBitmap(filename);
	}

	if (arg2 != 256) {
		// TODO
		// bitmapToReturn = scaleBitmap(bitmapToReturn, arg2);
	}

	return bitmapToReturn;
}

Common::String StarTrekEngine::getCrewmanAnimFilename(int objectIndex, const Common::String &basename) {
	const char *crewmanChars = "ksmr";
	assert(objectIndex >= 0 && objectIndex < 4);
	return crewmanChars[objectIndex] + basename;
}

void StarTrekEngine::updateClockTicks() {
	// TODO (based on DOS interrupt 1A, AH=0; read system clock counter)

	_clockTicks = 0;
}

/**
 * TODO:
 *   - Should return nullptr on failure to open a file?
 *   - This is supposed to cache results, return same FileStream on multiple accesses.
 *   - This is supposed to read from a "patches" folder which overrides files in the
 *     packed blob.
 */
SharedPtr<FileStream> StarTrekEngine::loadFile(Common::String filename, int fileIndex) {
	filename.toUppercase();

	Common::String basename, extension;

	bool bigEndian = getPlatform() == Common::kPlatformAmiga;

	for (int i=filename.size()-1; ; i--) {
		if (filename[i] == '.') {
			basename = filename;
			extension = filename;
			basename.replace(i, filename.size()-i, "");
			extension.replace(0, i+1, "");
			break;
		}
	}

	// FIXME: don't know if this is right, or if it goes here
	while (!basename.empty() && basename.lastChar() == ' ') {
		basename.erase(basename.size() - 1, 1);
	}

	filename = basename + '.' + extension;

	// The Judgment Rites demo has its files not in the standard archive
	if (getGameType() == GType_STJR && (getFeatures() & GF_DEMO)) {
		Common::File *file = new Common::File();
		if (!file->open(filename.c_str()))
			error("Could not find file \'%s\'", filename.c_str());
		return SharedPtr<FileStream>(new FileStream(file, bigEndian));
	}

	Common::SeekableReadStream *indexFile = 0;

	if (getPlatform() == Common::kPlatformAmiga) {
		indexFile = SearchMan.createReadStreamForMember("data000.dir");
		if (!indexFile)
			error("Could not open data000.dir");
	} else if (getPlatform() == Common::kPlatformMacintosh) {
		indexFile = _macResFork->getResource("Directory");
		if (!indexFile)
			error("Could not find 'Directory' resource in 'Star Trek Data'");
	} else {
		indexFile = SearchMan.createReadStreamForMember("data.dir");
		if (!indexFile)
			error("Could not open data.dir");
	}
	
	uint32 indexOffset = 0;
	bool foundData = false;
	uint16 fileCount = 1;
	uint16 uncompressedSize = 0;
	
	while (!indexFile->eos() && !indexFile->err()) {
		Common::String testfile;
		for (byte i = 0; i < 8; i++) {
			char c = indexFile->readByte();
			if (c)
				testfile += c;
		}
		testfile += '.';

		for (byte i = 0; i < 3; i++)
			testfile += indexFile->readByte();

		if (getFeatures() & GF_DEMO && getPlatform() == Common::kPlatformDOS) {
			indexFile->readByte(); // Always 0?
			fileCount = indexFile->readUint16LE(); // Always 1
			indexOffset = indexFile->readUint32LE();
			uncompressedSize = indexFile->readUint16LE();
		} else {
			if (getPlatform() == Common::kPlatformAmiga)
				indexOffset = (indexFile->readByte() << 16) + (indexFile->readByte() << 8) + indexFile->readByte();	
			else 
				indexOffset = indexFile->readByte() + (indexFile->readByte() << 8) + (indexFile->readByte() << 16);
			
			if (indexOffset & (1 << 23)) {
				fileCount = (indexOffset >> 16) & 0x7F;
				indexOffset = indexOffset & 0xFFFF;
				assert(fileCount > 1);
			} else {
				fileCount = 1;
			}
		}

		if (filename.matchString(testfile)) {
			foundData = true;
			break;
		}
	}

	delete indexFile;

	if (!foundData) {
		// Files can be accessed "sequentially" if their filenames are the same except for
		// the last character being incremented by one.
		if ((basename.lastChar() >= '1' && basename.lastChar() <= '9') ||
				(basename.lastChar() >= 'B' && basename.lastChar() <= 'Z')) {
			basename.setChar(basename.lastChar()-1, basename.size()-1);
			return loadFile(basename + "." + extension, fileIndex+1);
		} else
			error("Could not find file \'%s\'", filename.c_str());
	}

	if (fileIndex >= fileCount)
		error("Tried to access file index %d for file '%s' which doesn't exist.", fileIndex, filename.c_str());

	Common::SeekableReadStream *dataFile = 0;
	Common::SeekableReadStream *dataRunFile = 0; // FIXME: Amiga & Mac need this implemented

	if (getPlatform() == Common::kPlatformAmiga) {
		dataFile = SearchMan.createReadStreamForMember("data.000");
		if (!dataFile)
			error("Could not open data.000");
	} else if (getPlatform() == Common::kPlatformMacintosh) {
		dataFile = _macResFork->getDataFork();
		if (!dataFile)
			error("Could not get 'Star Trek Data' data fork");
	} else {
		dataFile = SearchMan.createReadStreamForMember("data.001");
		if (!dataFile)
			error("Could not open data.001");
		dataRunFile = SearchMan.createReadStreamForMember("data.run");
		if (!dataFile)
			error("Could not open data.run");
	}

	if (getFeatures() & GF_DEMO && getPlatform() == Common::kPlatformDOS) {
		assert(fileCount == 1); // Sanity check...
		Common::SeekableReadStream *stream = dataFile->readStream(uncompressedSize);
		delete dataFile;
		delete dataRunFile;
		return SharedPtr<FileStream>(new FileStream(stream, bigEndian));
	} else {
		if (fileCount != 1) {
			dataRunFile->seek(indexOffset);

			indexOffset = dataRunFile->readByte() + (dataRunFile->readByte() << 8) + (dataRunFile->readByte() << 16);
			//indexOffset &= 0xFFFFFE;

			for (uint16 i = 0; i < fileIndex; i++) {
				uint16 size = dataRunFile->readUint16LE();
				indexOffset += size;
			}
		}
		dataFile->seek(indexOffset);

		uncompressedSize = (getPlatform() == Common::kPlatformAmiga) ? dataFile->readUint16BE() : dataFile->readUint16LE();
		uint16 compressedSize = (getPlatform() == Common::kPlatformAmiga) ? dataFile->readUint16BE() : dataFile->readUint16LE();

		Common::SeekableReadStream *stream = decodeLZSS(dataFile->readStream(compressedSize), uncompressedSize);

		delete dataFile;
		delete dataRunFile;
		return SharedPtr<FileStream>(new FileStream(stream, bigEndian));
	}

	// We should not get to this point...
	error("Could not find data for \'%s\'", filename.c_str());

	return SharedPtr<FileStream>();
}

void StarTrekEngine::playMovie(Common::String filename) {
	if (getPlatform() == Common::kPlatformMacintosh)
		playMovieMac(filename);
	else
		error("Interplay MVE not yet supported");
}

void StarTrekEngine::playMovieMac(Common::String filename) {
	// Swap to 16bpp mode
	initGraphics(512, 384, NULL);

	Video::QuickTimeDecoder *qtDecoder = new Video::QuickTimeDecoder();

	if (!qtDecoder->loadFile(filename))
		error("Could not open '%s'", filename.c_str());

	bool continuePlaying = true;

	qtDecoder->start();

	while (!qtDecoder->endOfVideo() && !shouldQuit() && continuePlaying) {
		if (qtDecoder->needsUpdate()) {
			const ::Graphics::Surface *frame = qtDecoder->decodeNextFrame();

			if (frame) {
				::Graphics::Surface *convertedFrame = frame->convertTo(_system->getScreenFormat());
				_system->copyRectToScreen(convertedFrame->getPixels(), convertedFrame->pitch, 0, 0, convertedFrame->w, convertedFrame->h);
				_system->updateScreen();
				convertedFrame->free();
				delete convertedFrame;
			}
		}

		Common::Event event;
		while (g_system->getEventManager()->pollEvent(event))
			if (event.type == Common::EVENT_KEYDOWN && event.kbd.keycode == Common::KEYCODE_ESCAPE)
				continuePlaying = false;

		g_system->delayMillis(10);
	}

	delete qtDecoder;

	// Swap back to 8bpp mode
	initGraphics(320, 200);
}

} // End of namespace StarTrek