aboutsummaryrefslogtreecommitdiff
path: root/engines/adl/hires6.cpp
blob: e15cf43e5bd0300189492b7fe8dae47a9bae89b4 (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
/* 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 "common/system.h"
#include "common/debug.h"
#include "common/error.h"
#include "common/file.h"
#include "common/stream.h"
#include "common/memstream.h"

#include "adl/hires6.h"
#include "adl/display.h"
#include "adl/graphics.h"
#include "adl/disk.h"

namespace Adl {

static const char *disks[] = { "DARK1A.DSK", "DARK1B.NIB", "DARK2A.NIB", "DARK2B.NIB" };

#define SECTORS_PER_TRACK 16
#define BYTES_PER_SECTOR 256

static Common::MemoryReadStream *loadSectors(DiskImage *disk, byte track, byte sector = SECTORS_PER_TRACK - 1, byte count = SECTORS_PER_TRACK) {
	const int bufSize = count * BYTES_PER_SECTOR;
	byte *const buf = (byte *)malloc(bufSize);
	byte *p = buf;

	while (count-- > 0) {
		StreamPtr stream(disk->createReadStream(track, sector, 0, 0));
		stream->read(p, BYTES_PER_SECTOR);

		if (stream->err() || stream->eos())
			error("Error loading from disk image");

		p += BYTES_PER_SECTOR;
		if (sector > 0)
			--sector;
		else {
			++track;

			// Skip VTOC track
			if (track == 17)
				++track;

			sector = SECTORS_PER_TRACK - 1;
		}
	}

	return new Common::MemoryReadStream(buf, bufSize, DisposeAfterUse::YES);
}

void HiRes6Engine::runIntro() const {
	DiskImage_DSK *boot(new DiskImage_DSK());

	if (!boot->open(disks[0]))
		error("Failed to open disk image '%s'", disks[0]);

	StreamPtr stream(loadSectors(boot, 11, 1, 96));

	_display->setMode(DISPLAY_MODE_HIRES);
	_display->loadFrameBuffer(*stream);
	_display->updateHiResScreen();
	delay(256 * 8609 / 1000);

	_display->loadFrameBuffer(*stream);
	_display->updateHiResScreen();
	delay(256 * 8609 / 1000);

	_display->loadFrameBuffer(*stream);

	delete boot;

	// Load copyright string from boot file
	Files_DOS33 *files(new Files_DOS33());

	if (!files->open(disks[0]))
		error("Failed to open disk image '%s'", disks[0]);

	stream.reset(files->createReadStream("\010\010\010\010\010\010"));
	Common::String copyright(readStringAt(*stream, 0x103, APPLECHAR('\r')));

	delete files;

	_display->updateHiResScreen();
	_display->home();
	_display->setMode(DISPLAY_MODE_MIXED);
	_display->moveCursorTo(Common::Point(0, 21));
	_display->printString(copyright);
	delay(256 * 8609 / 1000);
}

void HiRes6Engine::init() {
	DiskImage_DSK *boot(new DiskImage_DSK());
	_graphics = new Graphics_v2(*_display);

	if (!boot->open(disks[0]))
		error("Failed to open disk image '%s'", disks[0]);

	StreamPtr stream(loadSectors(boot, 7));

	// Read parser messages
	_strings.verbError = readStringAt(*stream, 0x666);
	_strings.nounError = readStringAt(*stream, 0x6bd);
	_strings.enterCommand = readStringAt(*stream, 0x6e9);

	// Read line feeds
	_strings.lineFeeds = readStringAt(*stream, 0x408);

	// Read opcode strings (TODO)
	_strings_v2.saveInsert = readStringAt(*stream, 0xad8);
	readStringAt(*stream, 0xb95); // Confirm save
	// _strings_v2.saveReplace
	_strings_v2.restoreInsert = readStringAt(*stream, 0xc07);
	// _strings_v2.restoreReplace
	_strings.playAgain = readStringAt(*stream, 0xcdf, 0xff);

	_messageIds.cantGoThere = IDI_HR6_MSG_CANT_GO_THERE;
	_messageIds.dontUnderstand = IDI_HR6_MSG_DONT_UNDERSTAND;
	_messageIds.itemDoesntMove = IDI_HR6_MSG_ITEM_DOESNT_MOVE;
	_messageIds.itemNotHere = IDI_HR6_MSG_ITEM_NOT_HERE;
	_messageIds.thanksForPlaying = IDI_HR6_MSG_THANKS_FOR_PLAYING;

	// Load dropped item offsets
	stream.reset(boot->createReadStream(0x8, 0x9, 0x16));
	for (uint i = 0; i < IDI_HR6_NUM_ITEM_OFFSETS; ++i) {
		Common::Point p;
		p.x = stream->readByte();
		p.y = stream->readByte();
		_itemOffsets.push_back(p);
	}

	_disk = new DiskImage_NIB();

	if (!_disk->open(disks[1]))
		error("Failed to open disk image '%s'", disks[1]);

	// Load item picture data
	stream.reset(boot->createReadStream(0xb, 0xd, 0x08));
	for (uint i = 0; i < IDI_HR6_NUM_ITEM_PICS; ++i) {
		stream->readByte();
		_itemPics.push_back(readDataBlockPtr(*stream));
	}

	// Load global picture data
	stream.reset(_disk->createReadStream(0x1f, 0xf, 0x16));
	byte picNr;
	while ((picNr = stream->readByte()) != 0xff) {
		if (stream->eos() || stream->err())
			error("Error reading global pic list");
		_pictures[picNr] = readDataBlockPtr(*stream);
	}

	// Load message offsets
	stream.reset(_disk->createReadStream(0x1f, 0xb, 0x16, 4));
	for (uint i = 0; i < IDI_HR6_NUM_MESSAGES; ++i)
		_messages.push_back(readDataBlockPtr(*stream));

	// Load commands
	stream.reset(_disk->createReadStream(0x21, 0x4, 0x85, 7));
	readCommands(*stream, _roomCommands);

	stream.reset(_disk->createReadStream(0x20, 0xf, 0x82, 5));
	readCommands(*stream, _globalCommands);

	// Load verbs
	stream.reset(_disk->createReadStream(0x1f, 0xf, 0x56, 6));
	loadWords(*stream, _verbs, _priVerbs);

	// Load nouns
	stream.reset(_disk->createReadStream(0x20, 0x5, 0x8e, 8));
	loadWords(*stream, _nouns, _priNouns);

	delete boot;
}

void HiRes6Engine::initState() {
	_linesPrinted = 0;

	_state = State();
	_state.vars.resize(IDI_HR6_NUM_VARS);

	StreamPtr stream(_disk->createReadStream(0x20, 0xd, 0x94, 2));

	for (uint i = 0; i < IDI_HR6_NUM_ROOMS; ++i) {
		Room room;
		stream->readByte(); // number
		for (uint j = 0; j < 6; ++j)
			room.connections[j] = stream->readByte();
		room.data = readDataBlockPtr(*stream);
		room.picture = stream->readByte();
		room.curPicture = stream->readByte();
		room.isFirstTime = stream->readByte();
		_state.rooms.push_back(room);
	}

	stream.reset(_disk->createReadStream(0x22, 0x0, 0x07, 0));

	byte id;
	while ((id = stream->readByte()) != 0xff) {
		Item item = { };
		item.id = id;
		item.noun = stream->readByte();
		item.room = stream->readByte();
		item.picture = stream->readByte();
		item.isLineArt = stream->readByte(); // Now seems to be disk number
		item.position.x = stream->readByte();
		item.position.y = stream->readByte();
		item.state = stream->readByte();
		item.description = stream->readByte();

		stream->readByte(); // Struct size

		byte picListSize = stream->readByte();

		// Flag to keep track of what has been drawn on the screen
		stream->readByte();

		for (uint i = 0; i < picListSize; ++i)
			item.roomPictures.push_back(stream->readByte());

		_state.items.push_back(item);
	}
}

void HiRes6Engine::applyDataBlockOffset(byte &track, byte &sector) const {
	// FIXME: this uses a table
	++track;
}

Engine *HiRes6Engine_create(OSystem *syst, const AdlGameDescription *gd) {
	return new HiRes6Engine(syst, gd);
}

} // End of namespace Adl