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
|
/* 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/random.h"
#include "common/error.h"
#include "adl/adl_v2.h"
#include "adl/display.h"
#include "adl/graphics.h"
namespace Adl {
AdlEngine_v2::~AdlEngine_v2() {
delete _random;
delete _disk;
}
AdlEngine_v2::AdlEngine_v2(OSystem *syst, const AdlGameDescription *gd) :
AdlEngine(syst, gd),
_linesPrinted(0),
_disk(nullptr),
_itemRemoved(false),
_roomOnScreen(0),
_picOnScreen(0),
_itemsOnScreen(0) {
_random = new Common::RandomSource("adl");
}
typedef Common::Functor1Mem<ScriptEnv &, int, AdlEngine_v2> OpcodeV2;
#define SetOpcodeTable(x) table = &x;
#define Opcode(x) table->push_back(new OpcodeV2(this, &AdlEngine_v2::x))
#define OpcodeUnImpl() table->push_back(new OpcodeV2(this, 0))
void AdlEngine_v2::setupOpcodeTables() {
Common::Array<const Opcode *> *table = 0;
SetOpcodeTable(_condOpcodes);
// 0x00
OpcodeUnImpl();
Opcode(o2_isFirstTime);
Opcode(o2_isRandomGT);
Opcode(o1_isItemInRoom);
// 0x04
Opcode(o2_isNounNotInRoom);
Opcode(o1_isMovesGT);
Opcode(o1_isVarEQ);
Opcode(o2_isCarryingSomething);
// 0x08
OpcodeUnImpl();
Opcode(o1_isCurPicEQ);
Opcode(o1_isItemPicEQ);
SetOpcodeTable(_actOpcodes);
// 0x00
OpcodeUnImpl();
Opcode(o1_varAdd);
Opcode(o1_varSub);
Opcode(o1_varSet);
// 0x04
Opcode(o1_listInv);
Opcode(o2_moveItem);
Opcode(o1_setRoom);
Opcode(o2_setCurPic);
// 0x08
Opcode(o2_setPic);
Opcode(o1_printMsg);
Opcode(o1_setLight);
Opcode(o1_setDark);
// 0x0c
Opcode(o2_moveAllItems);
Opcode(o1_quit);
OpcodeUnImpl();
Opcode(o2_save);
// 0x10
Opcode(o2_restore);
Opcode(o1_restart);
Opcode(o2_placeItem);
Opcode(o1_setItemPic);
// 0x14
Opcode(o1_resetPic);
Opcode(o1_goDirection<IDI_DIR_NORTH>);
Opcode(o1_goDirection<IDI_DIR_SOUTH>);
Opcode(o1_goDirection<IDI_DIR_EAST>);
// 0x18
Opcode(o1_goDirection<IDI_DIR_WEST>);
Opcode(o1_goDirection<IDI_DIR_UP>);
Opcode(o1_goDirection<IDI_DIR_DOWN>);
Opcode(o1_takeItem);
// 0x1c
Opcode(o1_dropItem);
Opcode(o1_setRoomPic);
Opcode(o2_tellTime);
Opcode(o2_setRoomFromVar);
// 0x20
Opcode(o2_initDisk);
}
void AdlEngine_v2::initState() {
AdlEngine::initState();
_linesPrinted = 0;
_picOnScreen = 0;
_roomOnScreen = 0;
_itemRemoved = false;
_itemsOnScreen = 0;
}
byte AdlEngine_v2::roomArg(byte room) const {
if (room == IDI_CUR_ROOM)
return _state.room;
return room;
}
void AdlEngine_v2::advanceClock() {
Time &time = _state.time;
time.minutes += 5;
if (time.minutes == 60) {
time.minutes = 0;
++time.hours;
if (time.hours == 13)
time.hours = 1;
}
}
void AdlEngine_v2::checkTextOverflow(char c) {
if (c != APPLECHAR('\r'))
return;
++_linesPrinted;
if (_linesPrinted < 4)
return;
_linesPrinted = 0;
_display->updateTextScreen();
bell();
while (true) {
char key = inputKey(false);
if (shouldQuit())
return;
if (key == APPLECHAR('\r'))
break;
bell(3);
}
}
Common::String AdlEngine_v2::loadMessage(uint idx) const {
if (_messages[idx]) {
StreamPtr strStream(_messages[idx]->createReadStream());
return readString(*strStream, 0xff);
}
return Common::String();
}
void AdlEngine_v2::printString(const Common::String &str) {
Common::String s(str);
uint endPos = TEXT_WIDTH - 1;
uint pos = 0;
while (true) {
while (pos <= endPos && pos != s.size()) {
s.setChar(APPLECHAR(s[pos]), pos);
++pos;
}
if (pos == s.size())
break;
while (s[pos] != APPLECHAR(' ') && s[pos] != APPLECHAR('\r'))
--pos;
s.setChar(APPLECHAR('\r'), pos);
endPos = pos + TEXT_WIDTH;
++pos;
}
pos = 0;
while (pos != s.size()) {
checkTextOverflow(s[pos]);
_display->printChar(s[pos]);
++pos;
}
checkTextOverflow(APPLECHAR('\r'));
_display->printChar(APPLECHAR('\r'));
_display->updateTextScreen();
}
void AdlEngine_v2::drawItem(Item &item, const Common::Point &pos) {
item.isOnScreen = true;
StreamPtr stream(_itemPics[item.picture - 1]->createReadStream());
stream->readByte(); // Skip clear opcode
_graphics->drawPic(*stream, pos);
}
void AdlEngine_v2::loadRoom(byte roomNr) {
Room &room = getRoom(roomNr);
StreamPtr stream(room.data->createReadStream());
uint16 descOffset = stream->readUint16LE();
uint16 commandOffset = stream->readUint16LE();
_roomData.pictures.clear();
// There's no picture count. The original engine always checks at most
// five pictures. We use the description offset to bound our search.
uint16 picCount = (descOffset - 4) / 5;
for (uint i = 0; i < picCount; ++i) {
byte nr = stream->readByte();
_roomData.pictures[nr] = readDataBlockPtr(*stream);
}
_roomData.description = readStringAt(*stream, descOffset, 0xff);
_roomData.commands.clear();
if (commandOffset != 0) {
stream->seek(commandOffset);
readCommands(*stream, _roomData.commands);
}
applyRoomWorkarounds(roomNr);
}
void AdlEngine_v2::showRoom() {
bool redrawPic = false;
_state.curPicture = getCurRoom().curPicture;
if (_state.room != _roomOnScreen) {
loadRoom(_state.room);
clearScreen();
if (!_state.isDark)
redrawPic = true;
} else {
if (_state.curPicture != _picOnScreen || _itemRemoved)
redrawPic = true;
}
if (redrawPic) {
_roomOnScreen = _state.room;
_picOnScreen = _state.curPicture;
drawPic(_state.curPicture);
_itemRemoved = false;
_itemsOnScreen = 0;
Common::List<Item>::iterator item;
for (item = _state.items.begin(); item != _state.items.end(); ++item)
item->isOnScreen = false;
}
if (!_state.isDark)
drawItems();
_display->updateHiResScreen();
printString(_roomData.description);
// FIXME: move to main loop?
_linesPrinted = 0;
}
// TODO: Merge this into AdlEngine?
void AdlEngine_v2::takeItem(byte noun) {
Common::List<Item>::iterator item;
for (item = _state.items.begin(); item != _state.items.end(); ++item) {
if (item->noun == noun && item->room == _state.room && item->region == _state.region) {
if (item->state == IDI_ITEM_DOESNT_MOVE) {
printMessage(_messageIds.itemDoesntMove);
return;
}
if (item->state == IDI_ITEM_DROPPED) {
item->room = IDI_ANY;
_itemRemoved = true;
return;
}
Common::Array<byte>::const_iterator pic;
for (pic = item->roomPictures.begin(); pic != item->roomPictures.end(); ++pic) {
if (*pic == getCurRoom().curPicture || *pic == IDI_ANY) {
if (!isInventoryFull()) {
item->room = IDI_ANY;
_itemRemoved = true;
item->state = IDI_ITEM_DROPPED;
}
return;
}
}
}
}
printMessage(_messageIds.itemNotHere);
}
void AdlEngine_v2::drawItems() {
Common::List<Item>::iterator item;
for (item = _state.items.begin(); item != _state.items.end(); ++item) {
// Skip items not in this room
if (item->region == _state.region && item->room == _state.room && !item->isOnScreen) {
if (item->state == IDI_ITEM_DROPPED) {
// Draw dropped item if in normal view
if (getCurRoom().picture == getCurRoom().curPicture)
drawItem(*item, _itemOffsets[_itemsOnScreen++]);
} else {
// Draw fixed item if current view is in the pic list
Common::Array<byte>::const_iterator pic;
for (pic = item->roomPictures.begin(); pic != item->roomPictures.end(); ++pic) {
if (*pic == _state.curPicture || *pic == IDI_ANY) {
drawItem(*item, item->position);
break;
}
}
}
}
}
}
DataBlockPtr AdlEngine_v2::readDataBlockPtr(Common::ReadStream &f) const {
byte track = f.readByte();
byte sector = f.readByte();
byte offset = f.readByte();
byte size = f.readByte();
if (f.eos() || f.err())
error("Error reading DataBlockPtr");
if (track == 0 && sector == 0 && offset == 0 && size == 0)
return DataBlockPtr();
adjustDataBlockPtr(track, sector, offset, size);
return _disk->getDataBlock(track, sector, offset, size);
}
void AdlEngine_v2::loadItems(Common::ReadStream &stream) {
byte id;
while ((id = stream.readByte()) != 0xff && !stream.eos() && !stream.err()) {
Item item = Item();
item.id = id;
item.noun = stream.readByte();
item.room = stream.readByte();
item.picture = stream.readByte();
item.region = stream.readByte();
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);
}
if (stream.eos() || stream.err())
error("Error loading items");
}
void AdlEngine_v2::loadRooms(Common::ReadStream &stream, byte count) {
for (uint i = 0; i < count; ++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);
}
if (stream.eos() || stream.err())
error("Error loading rooms");
}
void AdlEngine_v2::loadMessages(Common::ReadStream &stream, byte count) {
for (uint i = 0; i < count; ++i)
_messages.push_back(readDataBlockPtr(stream));
}
void AdlEngine_v2::loadPictures(Common::ReadStream &stream) {
byte picNr;
while ((picNr = stream.readByte()) != 0xff) {
if (stream.eos() || stream.err())
error("Error reading global pic list");
_pictures[picNr] = readDataBlockPtr(stream);
}
}
void AdlEngine_v2::loadItemPictures(Common::ReadStream &stream, byte count) {
for (uint i = 0; i < count; ++i) {
stream.readByte(); // number
_itemPics.push_back(readDataBlockPtr(stream));
}
}
int AdlEngine_v2::o2_isFirstTime(ScriptEnv &e) {
OP_DEBUG_0("\t&& IS_FIRST_TIME()");
bool oldFlag = getCurRoom().isFirstTime;
getCurRoom().isFirstTime = false;
if (!oldFlag)
return -1;
return 0;
}
int AdlEngine_v2::o2_isRandomGT(ScriptEnv &e) {
OP_DEBUG_1("\t&& RAND() > %d", e.arg(1));
byte rnd = _random->getRandomNumber(255);
if (rnd > e.arg(1))
return 1;
return -1;
}
int AdlEngine_v2::o2_isNounNotInRoom(ScriptEnv &e) {
OP_DEBUG_1("\t&& NO_SUCH_ITEMS_IN_ROOM(%s)", itemRoomStr(e.arg(1)).c_str());
Common::List<Item>::const_iterator item;
for (item = _state.items.begin(); item != _state.items.end(); ++item)
if (item->noun == e.getNoun() && (item->room == roomArg(e.arg(1))))
return -1;
return 1;
}
int AdlEngine_v2::o2_isCarryingSomething(ScriptEnv &e) {
OP_DEBUG_0("\t&& IS_CARRYING_SOMETHING()");
Common::List<Item>::const_iterator item;
for (item = _state.items.begin(); item != _state.items.end(); ++item)
if (item->room == IDI_ANY)
return 0;
return -1;
}
int AdlEngine_v2::o2_moveItem(ScriptEnv &e) {
OP_DEBUG_2("\tSET_ITEM_ROOM(%s, %s)", itemStr(e.arg(1)).c_str(), itemRoomStr(e.arg(2)).c_str());
byte room = roomArg(e.arg(2));
Item &item = getItem(e.arg(1));
if (item.room == _roomOnScreen)
_picOnScreen = 0;
// Set items that move from inventory to a room to state "dropped"
if (item.room == IDI_ANY && room != IDI_VOID_ROOM)
item.state = IDI_ITEM_DROPPED;
item.room = room;
return 2;
}
int AdlEngine_v2::o2_setCurPic(ScriptEnv &e) {
OP_DEBUG_1("\tSET_CURPIC(%d)", e.arg(1));
getCurRoom().curPicture = _state.curPicture = e.arg(1);
return 1;
}
int AdlEngine_v2::o2_setPic(ScriptEnv &e) {
OP_DEBUG_1("\tSET_PIC(%d)", e.arg(1));
getCurRoom().picture = getCurRoom().curPicture = _state.curPicture = e.arg(1);
return 1;
}
int AdlEngine_v2::o2_moveAllItems(ScriptEnv &e) {
OP_DEBUG_2("\tMOVE_ALL_ITEMS(%s, %s)", itemRoomStr(e.arg(1)).c_str(), itemRoomStr(e.arg(2)).c_str());
byte room1 = roomArg(e.arg(1));
if (room1 == _state.room)
_picOnScreen = 0;
byte room2 = roomArg(e.arg(2));
Common::List<Item>::iterator item;
for (item = _state.items.begin(); item != _state.items.end(); ++item)
if (item->room == room1) {
item->room = room2;
if (room1 == IDI_ANY)
item->state = IDI_ITEM_DROPPED;
}
return 2;
}
int AdlEngine_v2::o2_save(ScriptEnv &e) {
OP_DEBUG_0("\tSAVE_GAME()");
int slot = askForSlot(_strings_v2.saveInsert);
if (slot < 0)
return -1;
saveGameState(slot, "");
_display->printString(_strings_v2.saveReplace);
inputString();
return 0;
}
int AdlEngine_v2::o2_restore(ScriptEnv &e) {
OP_DEBUG_0("\tRESTORE_GAME()");
int slot = askForSlot(_strings_v2.restoreInsert);
if (slot < 0)
return -1;
loadGameState(slot);
_isRestoring = false;
_display->printString(_strings_v2.restoreReplace);
inputString();
_picOnScreen = 0;
_roomOnScreen = 0;
return 0;
}
int AdlEngine_v2::o2_placeItem(ScriptEnv &e) {
OP_DEBUG_4("\tPLACE_ITEM(%s, %s, (%d, %d))", itemStr(e.arg(1)).c_str(), itemRoomStr(e.arg(2)).c_str(), e.arg(3), e.arg(4));
Item &item = getItem(e.arg(1));
item.room = roomArg(e.arg(2));
item.position.x = e.arg(3);
item.position.y = e.arg(4);
item.state = IDI_ITEM_NOT_MOVED;
return 4;
}
int AdlEngine_v2::o2_tellTime(ScriptEnv &e) {
OP_DEBUG_0("\tTELL_TIME()");
Common::String time = _strings_v2.time;
time.setChar(APPLECHAR('0') + _state.time.hours / 10, 12);
time.setChar(APPLECHAR('0') + _state.time.hours % 10, 13);
time.setChar(APPLECHAR('0') + _state.time.minutes / 10, 15);
time.setChar(APPLECHAR('0') + _state.time.minutes % 10, 16);
printString(time);
return 0;
}
int AdlEngine_v2::o2_setRoomFromVar(ScriptEnv &e) {
OP_DEBUG_1("\tROOM = VAR[%d]", e.arg(1));
getCurRoom().curPicture = getCurRoom().picture;
_state.room = getVar(e.arg(1));
return 1;
}
int AdlEngine_v2::o2_initDisk(ScriptEnv &e) {
OP_DEBUG_0("\tINIT_DISK()");
_display->printAsciiString("NOT REQUIRED\r");
return 0;
}
int AdlEngine_v2::askForSlot(const Common::String &question) {
while (1) {
_display->printString(question);
Common::String input = inputString();
if (shouldQuit())
return -1;
if (input.size() > 0 && input[0] >= APPLECHAR('A') && input[0] <= APPLECHAR('O'))
return input[0] - APPLECHAR('A');
}
}
} // End of namespace Adl
|