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
|
/* 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.
*
*/
/*
* This code is based on Labyrinth of Time code with assistance of
*
* Copyright (c) 1993 Terra Nova Development
* Copyright (c) 2004 The Wyrmkeep Entertainment Co.
*
*/
#include "lab/lab.h"
#include "lab/dispman.h"
#include "lab/music.h"
#include "lab/processroom.h"
#include "lab/resource.h"
namespace Lab {
Resource::Resource(LabEngine *vm) : _vm(vm) {
readStaticText();
}
void Resource::readStaticText() {
Common::File *labTextFile = openDataFile("Lab:Rooms/LabText");
for (int i = 0; i < 48; i++)
_staticText[i] = labTextFile->readLine();
delete labTextFile;
}
TextFont *Resource::getFont(const Common::String fileName) {
// TODO: Add support for the font format of the Amiga version
Common::File *dataFile = openDataFile(fileName, MKTAG('V', 'G', 'A', 'F'));
uint32 headerSize = 4 + 2 + 256 * 3 + 4;
uint32 fileSize = dataFile->size();
if (fileSize <= headerSize)
return nullptr;
_vm->updateMusicAndEvents();
TextFont *textfont = new TextFont();
textfont->_dataLength = fileSize - headerSize;
textfont->_height = dataFile->readUint16LE();
dataFile->read(textfont->_widths, 256);
for (int i = 0; i < 256; i++)
textfont->_offsets[i] = dataFile->readUint16LE();
dataFile->skip(4);
textfont->_data = new byte[textfont->_dataLength + 4];
dataFile->read(textfont->_data, textfont->_dataLength);
return textfont;
}
Common::String Resource::getText(const Common::String fileName) {
Common::File *dataFile = openDataFile(fileName);
_vm->updateMusicAndEvents();
uint32 count = dataFile->size();
byte *buffer = new byte[count];
byte *text = buffer;
dataFile->read(buffer, count);
while (text && (*text != '\0'))
*text++ -= (byte)95;
delete dataFile;
Common::String str = (char *)buffer;
delete[] buffer;
return str;
}
void Resource::readRoomData(const Common::String fileName) {
Common::File *dataFile = openDataFile(fileName, MKTAG('D', 'O', 'R', '1'));
_vm->_manyRooms = dataFile->readUint16LE();
_vm->_highestCondition = dataFile->readUint16LE();
_vm->_rooms = new RoomData[_vm->_manyRooms + 1];
memset(_vm->_rooms, 0, (_vm->_manyRooms + 1) * sizeof(RoomData));
for (int i = 1; i <= _vm->_manyRooms; i++) {
RoomData *curRoom = &_vm->_rooms[i];
curRoom->_doors[kDirectionNorth] = dataFile->readUint16LE();
curRoom->_doors[kDirectionSouth] = dataFile->readUint16LE();
curRoom->_doors[kDirectionEast] = dataFile->readUint16LE();
curRoom->_doors[kDirectionWest] = dataFile->readUint16LE();
curRoom->_transitionType = dataFile->readByte();
curRoom->_view[kDirectionNorth] = nullptr;
curRoom->_view[kDirectionSouth] = nullptr;
curRoom->_view[kDirectionEast] = nullptr;
curRoom->_view[kDirectionWest] = nullptr;
curRoom->_rules = nullptr;
curRoom->_roomMsg = "";
}
delete dataFile;
}
InventoryData *Resource::readInventory(const Common::String fileName) {
Common::File *dataFile = openDataFile(fileName, MKTAG('I', 'N', 'V', '1'));
_vm->_numInv = dataFile->readUint16LE();
InventoryData *inventory = new InventoryData[_vm->_numInv + 1];
for (int i = 1; i <= _vm->_numInv; i++) {
inventory[i]._quantity = dataFile->readUint16LE();
inventory[i]._name = readString(dataFile);
inventory[i]._bitmapName = readString(dataFile);
}
delete dataFile;
return inventory;
}
void Resource::readViews(uint16 roomNum) {
Common::String fileName = "LAB:Rooms/" + Common::String::format("%d", roomNum);
Common::File *dataFile = openDataFile(fileName, MKTAG('R', 'O', 'M', '4'));
freeViews(roomNum);
RoomData *curRoom = &_vm->_rooms[roomNum];
curRoom->_roomMsg = readString(dataFile);
curRoom->_view[kDirectionNorth] = readView(dataFile);
curRoom->_view[kDirectionSouth] = readView(dataFile);
curRoom->_view[kDirectionEast] = readView(dataFile);
curRoom->_view[kDirectionWest] = readView(dataFile);
curRoom->_rules = readRule(dataFile);
_vm->updateMusicAndEvents();
delete dataFile;
}
void Resource::freeViews(uint16 roomNum) {
if (!_vm->_rooms)
return;
for (int i = 0; i < 4; i++)
freeView(_vm->_rooms[roomNum]._view[i]);
freeRule(_vm->_rooms[roomNum]._rules);
}
Common::String Resource::translateFileName(const Common::String filename) {
Common::String upperFilename = filename;
upperFilename.toUppercase();
Common::String fileNameStrFinal;
if (upperFilename.hasPrefix("P:") || upperFilename.hasPrefix("F:")) {
if (_vm->_isHiRes)
fileNameStrFinal = "GAME/SPICT/";
else
fileNameStrFinal = "GAME/PICT/";
if (_vm->getPlatform() == Common::kPlatformAmiga) {
if (upperFilename.hasPrefix("P:")) {
fileNameStrFinal = "PICT/";
} else {
fileNameStrFinal = "LABFONTS/";
upperFilename += "T"; // all the Amiga fonts have a ".FONT" suffix
}
}
} else if (upperFilename.hasPrefix("LAB:")) {
if (_vm->getPlatform() != Common::kPlatformAmiga)
fileNameStrFinal = "GAME/";
} else if (upperFilename.hasPrefix("MUSIC:")) {
if (_vm->getPlatform() != Common::kPlatformAmiga)
fileNameStrFinal = "GAME/MUSIC/";
else
fileNameStrFinal = "MUSIC/";
}
if (upperFilename.contains(':')) {
while (upperFilename[0] != ':') {
upperFilename.deleteChar(0);
}
upperFilename.deleteChar(0);
}
fileNameStrFinal += upperFilename;
return fileNameStrFinal;
}
Common::File *Resource::openDataFile(const Common::String fileName, uint32 fileHeader) {
Common::File *dataFile = new Common::File();
dataFile->open(translateFileName(fileName));
if (!dataFile->isOpen())
error("openDataFile: Couldn't open %s (%s)", translateFileName(fileName).c_str(), fileName.c_str());
if (fileHeader > 0) {
uint32 headerTag = dataFile->readUint32BE();
if (headerTag != fileHeader) {
dataFile->close();
error("openDataFile: Unexpected header in %s (%s) - expected: %d, got: %d", translateFileName(fileName).c_str(), fileName.c_str(), fileHeader, headerTag);
}
}
return dataFile;
}
Common::String Resource::readString(Common::File *file) {
byte size = file->readByte();
if (!size)
return Common::String("");
char *str = new char[size];
for (int i = 0; i < size; i++) {
char c = file->readByte();
// Decrypt char
c = (i < size - 1) ? c - 95 : '\0';
str[i] = c;
}
Common::String result = str;
delete[] str;
return result;
}
int16 *Resource::readConditions(Common::File *file) {
int16 i = 0, cond;
int16 *list = new int16[25];
memset(list, 0, 25 * sizeof(int16));
do {
cond = file->readUint16LE();
if (i < 25)
list[i++] = cond;
} while (cond);
return list;
}
RuleList *Resource::readRule(Common::File *file) {
RuleList *rules = new RuleList();
while (file->readByte() == 1) {
Rule rule;
rule._ruleType = (RuleType)file->readSint16LE();
rule._param1 = file->readSint16LE();
rule._param2 = file->readSint16LE();
rule._condition = readConditions(file);
rule._actionList = readAction(file);
rules->push_back(rule);
}
return rules;
}
void Resource::freeRule(RuleList *ruleList) {
if (!ruleList)
return;
for (RuleList::iterator rule = ruleList->begin(); rule != ruleList->end(); ++rule) {
freeAction(rule->_actionList);
delete[] rule->_condition;
}
delete ruleList;
ruleList = nullptr;
}
Action *Resource::readAction(Common::File *file) {
Action *action = nullptr;
Action *prev = nullptr;
Action *head = nullptr;
while (file->readByte() == 1) {
action = new Action();
if (!head)
head = action;
if (prev)
prev->_nextAction = action;
action->_actionType = (ActionType)file->readSint16LE();
action->_param1 = file->readSint16LE();
action->_param2 = file->readSint16LE();
action->_param3 = file->readSint16LE();
if (action->_actionType == kActionShowMessages) {
action->_messages = new Common::String[action->_param1];
for (int i = 0; i < action->_param1; i++)
action->_messages[i] = readString(file);
} else {
action->_messages = new Common::String[1];
action->_messages[0] = readString(file);
}
action->_nextAction = nullptr;
prev = action;
}
return head;
}
void Resource::freeAction(Action *action) {
while (action) {
Action *nextAction = action->_nextAction;
delete[] action->_messages;
delete action;
action = nextAction;
}
}
CloseData *Resource::readCloseUps(uint16 depth, Common::File *file) {
CloseData *closeup = nullptr;
CloseData *prev = nullptr;
CloseData *head = nullptr;
while (file->readByte() != '\0') {
closeup = new CloseData();
if (!head)
head = closeup;
if (prev)
prev->_nextCloseUp = closeup;
closeup->_x1 = file->readUint16LE();
closeup->_y1 = file->readUint16LE();
closeup->_x2 = file->readUint16LE();
closeup->_y2 = file->readUint16LE();
closeup->_closeUpType = file->readSint16LE();
closeup->_depth = depth;
closeup->_graphicName = readString(file);
closeup->_message = readString(file);
closeup->_subCloseUps = readCloseUps(depth + 1, file);
closeup->_nextCloseUp = nullptr;
prev = closeup;
}
return head;
}
void Resource::freeCloseUps(CloseData *closeUps) {
while (closeUps) {
CloseData *nextCloseUp = closeUps->_nextCloseUp;
freeCloseUps(closeUps->_subCloseUps);
delete closeUps;
closeUps = nextCloseUp;
}
}
ViewData *Resource::readView(Common::File *file) {
ViewData *view = nullptr;
ViewData *prev = nullptr;
ViewData *head = nullptr;
while (file->readByte() == 1) {
view = new ViewData();
if (!head)
head = view;
if (prev)
prev->_nextCondition = view;
view->_condition = readConditions(file);
view->_graphicName = readString(file);
view->_closeUps = readCloseUps(0, file);
view->_nextCondition = nullptr;
prev = view;
}
return head;
}
void Resource::freeView(ViewData *view) {
while (view) {
ViewData *nextView = view->_nextCondition;
delete[] view->_condition;
freeCloseUps(view->_closeUps);
delete view;
view = nextView;
}
}
} // End of namespace Lab
|