ScummVM API documentation
adl.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef ADL_ADL_H
23 #define ADL_ADL_H
24 
25 #include "common/debug-channels.h"
26 #include "common/array.h"
27 #include "common/rect.h"
28 #include "common/str.h"
29 #include "common/hashmap.h"
30 #include "common/hash-str.h"
31 #include "common/func.h"
32 #include "common/ptr.h"
33 #include "common/scummsys.h"
34 #include "common/keyboard.h"
35 
36 #include "engines/engine.h"
37 
38 #include "audio/mixer.h"
39 #include "audio/softsynth/pcspk.h"
40 
41 #include "adl/console.h"
42 #include "adl/disk.h"
43 #include "adl/sound.h"
44 #include "adl/detection.h"
45 
46 namespace Common {
47 class ReadStream;
48 class WriteStream;
49 class SeekableReadStream;
50 class File;
51 struct Event;
52 class RandomSource;
53 }
54 
55 namespace Adl {
56 
57 Common::Path getDiskImageName(const AdlGameDescription &adlDesc, byte volume);
58 GameType getGameType(const AdlGameDescription &desc);
59 GameVersion getGameVersion(const AdlGameDescription &desc);
60 Common::Language getLanguage(const AdlGameDescription &desc);
61 Common::Platform getPlatform(const AdlGameDescription &desc);
62 
63 class Console;
64 class Display;
65 class GraphicsMan;
66 class ScriptEnv;
67 
68 enum kDebugChannels {
69  kDebugChannelScript = 1 << 0
70 };
71 
72 enum ADLAction {
73  kADLActionNone,
74  kADLActionQuit,
75 
76  kADLActionCount
77 };
78 
79 // Save and restore opcodes
80 #define IDO_ACT_SAVE 0x0f
81 #define IDO_ACT_LOAD 0x10
82 
83 #define IDI_CUR_ROOM 0xfc
84 #define IDI_VOID_ROOM 0xfd
85 #define IDI_ANY 0xfe
86 
87 #define IDI_WORD_SIZE 8
88 
89 enum Direction {
90  IDI_DIR_NORTH,
91  IDI_DIR_SOUTH,
92  IDI_DIR_EAST,
93  IDI_DIR_WEST,
94  IDI_DIR_UP,
95  IDI_DIR_DOWN,
96  IDI_DIR_TOTAL
97 };
98 
99 struct Room {
100  Room() :
101  description(0),
102  picture(0),
103  curPicture(0),
104  isFirstTime(true) {
105  memset(connections, 0, sizeof(connections));
106  }
107 
108  byte description;
109  byte connections[IDI_DIR_TOTAL];
110  DataBlockPtr data;
111  byte picture;
112  byte curPicture;
113  bool isFirstTime;
114 };
115 
117 
119 
120 struct Command {
121  byte room;
122  byte verb, noun;
123  byte numCond, numAct;
124  Script script;
125 };
126 
127 class ScriptEnv {
128 public:
129  ScriptEnv(const Command &cmd, byte room, byte verb, byte noun) :
130  _cmd(cmd), _room(room), _verb(verb), _noun(noun), _ip(0) { }
131 
132  virtual ~ScriptEnv() { }
133 
134  enum kOpType {
135  kOpTypeDone,
136  kOpTypeCond,
137  kOpTypeAct
138  };
139 
140  byte op() const { return _cmd.script[_ip]; }
141  virtual kOpType getOpType() const = 0;
142  // We keep this 1-based for easier comparison with the original engine
143  byte arg(uint i) const { return _cmd.script[_ip + i]; }
144  virtual void next(uint numArgs) = 0;
145 
146  bool isMatch() const {
147  return (_cmd.room == IDI_ANY || _cmd.room == _room) &&
148  (_cmd.verb == IDI_ANY || _cmd.verb == _verb) &&
149  (_cmd.noun == IDI_ANY || _cmd.noun == _noun);
150  }
151 
152  byte getNoun() const { return _noun; }
153  const Command &getCommand() const { return _cmd; }
154 
155 protected:
156  byte _ip;
157 
158 private:
159  const Command &_cmd;
160  const byte _room, _verb, _noun;
161 };
162 
163 enum {
164  IDI_ITEM_NOT_MOVED,
165  IDI_ITEM_DROPPED,
166  IDI_ITEM_DOESNT_MOVE
167 };
168 
169 struct Item {
170  byte id;
171  byte noun;
172  byte region;
173  byte room;
174  byte picture;
175  bool isShape;
176  Common::Point position;
177  int state;
178  byte description;
179  Common::Array<byte> roomPictures;
180  bool isOnScreen;
181 
182  Item() : id(0), noun(0), region(0), room(0), picture(0), isShape(false), state(0), description(0), isOnScreen(false) { }
183 };
184 
185 struct Time {
186  byte hours, minutes;
187 
188  Time() : hours(12), minutes(0) { }
189 };
190 
191 struct RoomState {
192  byte picture;
193  byte isFirstTime;
194 };
195 
196 struct Region {
197  Common::Array<byte> vars;
199 };
200 
201 struct State {
202  Common::Array<Region> regions;
203  Common::Array<Room> rooms;
204  Common::List<Item> items;
205  Common::Array<byte> vars;
206 
207  byte region, prevRegion;
208  byte room;
209  byte curPicture;
210  uint16 moves;
211  bool isDark;
212  Time time;
213 
214  State() : region(0), prevRegion(0), room(1), curPicture(0), moves(1), isDark(false) { }
215 };
216 
219 
220 struct RoomData {
221  Common::String description;
222  PictureMap pictures;
223  Commands commands;
224 };
225 
226 // Opcode debugging macros
227 #define OP_DEBUG_0(F) do { \
228  if (DebugMan.isDebugChannelEnabled(kDebugChannelScript) && op_debug(F)) \
229  return 0; \
230 } while (0)
231 
232 #define OP_DEBUG_1(F, P1) do { \
233  if (DebugMan.isDebugChannelEnabled(kDebugChannelScript) && op_debug(F, P1)) \
234  return 1; \
235 } while (0)
236 
237 #define OP_DEBUG_2(F, P1, P2) do { \
238  if (DebugMan.isDebugChannelEnabled(kDebugChannelScript) && op_debug(F, P1, P2)) \
239  return 2; \
240 } while (0)
241 
242 #define OP_DEBUG_3(F, P1, P2, P3) do { \
243  if (DebugMan.isDebugChannelEnabled(kDebugChannelScript) && op_debug(F, P1, P2, P3)) \
244  return 3; \
245 } while (0)
246 
247 #define OP_DEBUG_4(F, P1, P2, P3, P4) do { \
248  if (DebugMan.isDebugChannelEnabled(kDebugChannelScript) && op_debug(F, P1, P2, P3, P4)) \
249  return 4; \
250 } while (0)
251 
252 class AdlEngine : public Engine {
253 friend class Console;
254 public:
255  ~AdlEngine() override;
256 
257  bool pollEvent(Common::Event &event) const;
258  void bell(uint count = 1) const;
259 
260 protected:
261  AdlEngine(OSystem *syst, const AdlGameDescription *gd);
262 
263  // Engine
264  Common::Error loadGameState(int slot) override;
265  Common::Error saveGameState(int slot, const Common::String &desc, bool isAutosave = false) override;
266  bool canSaveGameStateCurrently(Common::U32String *msg = nullptr) override;
267  Common::String getSaveStateName(int slot) const override;
268  int getAutosaveSlot() const override { return 15; }
269 
270  Common::Path getDiskImageName(byte volume) const { return Adl::getDiskImageName(*_gameDescription, volume); }
271  GameType getGameType() const { return Adl::getGameType(*_gameDescription); }
272  GameVersion getGameVersion() const { return Adl::getGameVersion(*_gameDescription); }
273  Common::Language getLanguage() const { return Adl::getLanguage(*_gameDescription); }
274  virtual void gameLoop();
275  virtual void loadState(Common::ReadStream &stream);
276  virtual void saveState(Common::WriteStream &stream);
277  Common::String readString(Common::ReadStream &stream, byte until = 0) const;
278  Common::String readStringAt(Common::SeekableReadStream &stream, uint offset, byte until = 0) const;
279  void extractExeStrings(Common::ReadStream &stream, uint16 printAddr, Common::StringArray &strings) const;
280 
281  virtual void printString(const Common::String &str) = 0;
282  virtual Common::String loadMessage(uint idx) const = 0;
283  virtual void printMessage(uint idx);
284  virtual Common::String getItemDescription(const Item &item) const;
285  void delay(uint32 ms) const;
286 
287  virtual Common::String getLine();
288  Common::String inputString(byte prompt = 0) const;
289  byte inputKey(bool showCursor = true) const;
290  void waitKey(uint32 ms = 0, Common::KeyCode keycode = Common::KEYCODE_INVALID) const;
291  virtual void getInput(uint &verb, uint &noun);
292  Common::String getWord(const Common::String &line, uint &index) const;
293 
294  virtual Common::String formatVerbError(const Common::String &verb) const;
295  virtual Common::String formatNounError(const Common::String &verb, const Common::String &noun) const;
296  void loadWords(Common::ReadStream &stream, WordMap &map, Common::StringArray &pri, uint count = 0) const;
297  void readCommands(Common::ReadStream &stream, Commands &commands);
298  void removeCommand(Commands &commands, uint idx);
299  Command &getCommand(Commands &commands, uint idx);
300  void checkInput(byte verb, byte noun);
301  virtual bool isInputValid(byte verb, byte noun, bool &is_any);
302  virtual bool isInputValid(const Commands &commands, byte verb, byte noun, bool &is_any);
303  virtual void applyRoomWorkarounds(byte roomNr) { }
304  virtual void applyRegionWorkarounds() { }
305 
306  virtual void setupOpcodeTables();
307  virtual void initState();
308  virtual void switchRoom(byte roomNr);
309  virtual byte roomArg(byte room) const;
310  virtual void advanceClock() { }
311  void loadDroppedItemOffsets(Common::ReadStream &stream, byte count);
312 
313  // Opcodes
315 
316  template <class T>
317  Opcode opcode(int (T::*f)(ScriptEnv &)) {
318  return Opcode(new Common::Functor1Mem<ScriptEnv &, int, T>(static_cast<T *>(this), f));
319  }
320 
321  virtual int o_isItemInRoom(ScriptEnv &e);
322  virtual int o_isMovesGT(ScriptEnv &e);
323  virtual int o_isVarEQ(ScriptEnv &e);
324  virtual int o_isCurPicEQ(ScriptEnv &e);
325  virtual int o_isItemPicEQ(ScriptEnv &e);
326 
327  virtual int o_varAdd(ScriptEnv &e);
328  virtual int o_varSub(ScriptEnv &e);
329  virtual int o_varSet(ScriptEnv &e);
330  virtual int o_listInv(ScriptEnv &e);
331  virtual int o_moveItem(ScriptEnv &e);
332  virtual int o_setRoom(ScriptEnv &e);
333  virtual int o_setCurPic(ScriptEnv &e);
334  virtual int o_setPic(ScriptEnv &e);
335  virtual int o_printMsg(ScriptEnv &e);
336  virtual int o_setLight(ScriptEnv &e);
337  virtual int o_setDark(ScriptEnv &e);
338  virtual int o_save(ScriptEnv &e);
339  virtual int o_restore(ScriptEnv &e);
340  virtual int o_restart(ScriptEnv &e);
341  virtual int o_quit(ScriptEnv &e);
342  virtual int o_placeItem(ScriptEnv &e);
343  virtual int o_setItemPic(ScriptEnv &e);
344  virtual int o_resetPic(ScriptEnv &e);
345  virtual int o_takeItem(ScriptEnv &e);
346  virtual int o_dropItem(ScriptEnv &e);
347  virtual int o_setRoomPic(ScriptEnv &e);
348 
349  virtual int goDirection(ScriptEnv &e, Direction D);
350  int o_goNorth(ScriptEnv &e) { return goDirection(e, IDI_DIR_NORTH); }
351  int o_goSouth(ScriptEnv &e) { return goDirection(e, IDI_DIR_SOUTH); }
352  int o_goEast(ScriptEnv &e) { return goDirection(e, IDI_DIR_EAST); }
353  int o_goWest(ScriptEnv &e) { return goDirection(e, IDI_DIR_WEST); }
354  int o_goUp(ScriptEnv &e) { return goDirection(e, IDI_DIR_UP); }
355  int o_goDown(ScriptEnv &e) { return goDirection(e, IDI_DIR_DOWN); }
356 
357  // Graphics
358  void drawPic(byte pic, Common::Point pos = Common::Point()) const;
359 
360  // Sound
361  bool playTones(const Tones &tones, bool isMusic, bool allowSkip = false) const;
362 
363  // Game state functions
364  const Region &getRegion(uint i) const;
365  Region &getRegion(uint i);
366  const Room &getRoom(uint i) const;
367  Room &getRoom(uint i);
368  const Region &getCurRegion() const;
369  Region &getCurRegion();
370  const Room &getCurRoom() const;
371  Room &getCurRoom();
372  const Item &getItem(uint i) const;
373  Item &getItem(uint i);
374  byte getVar(uint i) const;
375  void setVar(uint i, byte value);
376  virtual void takeItem(byte noun);
377  virtual void dropItem(byte noun);
378  bool matchCommand(ScriptEnv &env) const;
379  void doActions(ScriptEnv &env);
380  bool doOneCommand(const Commands &commands, byte verb, byte noun);
381  void doAllCommands(const Commands &commands, byte verb, byte noun);
382  virtual ScriptEnv *createScriptEnv(const Command &cmd, byte room, byte verb, byte noun);
383 
384  // Debug functions
385  static Common::String toAscii(const Common::String &str);
386  Common::String itemStr(uint i) const;
387  Common::String roomStr(uint i) const;
388  Common::String itemRoomStr(uint i) const;
389  Common::String verbStr(uint i) const;
390  Common::String nounStr(uint i) const;
391  Common::String msgStr(uint i) const;
392  Common::String dirStr(Direction dir) const;
393  bool op_debug(const char *fmt, ...) const;
394  Common::DumpFile *_dumpFile;
395 
396  Display *_display;
397  GraphicsMan *_graphics;
398  bool _textMode;
399 
400  // Opcodes
401  Common::Array<Opcode> _condOpcodes, _actOpcodes;
402  // Message strings in data file
403  Common::Array<DataBlockPtr> _messages;
404  // Picture data
405  PictureMap _pictures;
406  // Dropped item screen offsets
407  Common::Array<Common::Point> _itemOffsets;
408  // <room, verb, noun, script> lists
409  Commands _roomCommands;
410  Commands _globalCommands;
411  // Data related to the current room
412  RoomData _roomData;
413 
414  WordMap _verbs;
415  WordMap _nouns;
416  Common::StringArray _priVerbs;
417  Common::StringArray _priNouns;
418 
419  struct {
420  Common::String enterCommand;
421  Common::String verbError;
422  Common::String nounError;
423  Common::String playAgain;
424  Common::String pressReturn;
425  Common::String lineFeeds;
426  } _strings;
427 
428  uint32 _verbErrorPos, _nounErrorPos;
429 
430  struct {
431  uint cantGoThere;
432  uint dontUnderstand;
433  uint itemDoesntMove;
434  uint itemNotHere;
435  uint thanksForPlaying;
436  } _messageIds;
437 
438  // Game state
439  State _state;
440 
441  uint _linesPrinted;
442  bool _isRestarting, _isRestoring, _isQuitting;
443  bool _canSaveNow, _canRestoreNow;
444  bool _abortScript;
445  Common::RandomSource *_random;
446 
447  const AdlGameDescription *_gameDescription;
448 
449  mutable Common::File *_inputScript;
450  mutable uint _scriptDelay;
451  mutable bool _scriptPaused;
452 
453 private:
454  virtual void runIntro() { }
455  virtual void init() = 0;
456  virtual void initGameState() = 0;
457  virtual void drawItems() = 0;
458  virtual void drawItem(Item &item, const Common::Point &pos) = 0;
459  virtual void loadRoom(byte roomNr) = 0;
460  virtual void showRoom() = 0;
461  virtual void switchRegion(byte region) { }
462  void runScript(const char *filename) const;
463  void stopScript() const;
464  void setScriptDelay(uint scriptDelay) const { _scriptDelay = scriptDelay; }
465  Common::String getScriptLine() const;
466  // Engine
467  Common::Error run() override;
468  bool hasFeature(EngineFeature f) const override;
469  bool canLoadGameStateCurrently(Common::U32String *msg = nullptr) override;
470 
471  // Text input
472  byte convertKey(uint16 ascii) const;
473 
474  byte _saveVerb, _saveNoun, _restoreVerb, _restoreNoun;
475 };
476 
477 } // End of namespace Adl
478 
479 #endif
Definition: console.h:38
Definition: adl.h:191
Definition: str.h:59
EngineFeature
Definition: engine.h:250
Definition: stream.h:77
Definition: error.h:84
Definition: adl.h:201
Definition: random.h:44
Definition: list.h:44
Definition: adl.h:220
Definition: path.h:52
int getAutosaveSlot() const override
Definition: adl.h:268
Definition: adl.h:55
Definition: detection.h:71
Definition: graphics.h:32
Definition: stream.h:745
Definition: file.h:145
Definition: adl.h:196
Definition: adl.h:252
Definition: display.h:34
Definition: adl.h:127
Definition: ustr.h:57
Definition: file.h:47
Definition: adl.h:185
Definition: events.h:198
Definition: algorithm.h:29
Definition: func.h:452
Definition: adl.h:120
Definition: rect.h:45
Definition: adl.h:169
Definition: stream.h:385
Definition: system.h:167
Definition: engine.h:143
Platform
Definition: platform.h:46
Definition: adl.h:99
Language
Definition: language.h:45