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];
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 char *key = "") 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 removeMessage(uint idx);
301  void checkInput(byte verb, byte noun);
302  virtual bool isInputValid(byte verb, byte noun, bool &is_any);
303  virtual bool isInputValid(const Commands &commands, byte verb, byte noun, bool &is_any);
304  virtual void applyRoomWorkarounds(byte roomNr) { }
305  virtual void applyRegionWorkarounds() { }
306 
307  virtual void setupOpcodeTables();
308  virtual void initState();
309  virtual void switchRoom(byte roomNr);
310  virtual byte roomArg(byte room) const;
311  virtual void advanceClock() { }
312  void loadDroppedItemOffsets(Common::ReadStream &stream, byte count);
313 
314  // Opcodes
316 
317  template <class T>
318  Opcode opcode(int (T::*f)(ScriptEnv &)) {
319  return Opcode(new Common::Functor1Mem<ScriptEnv &, int, T>(static_cast<T *>(this), f));
320  }
321 
322  virtual int o_isItemInRoom(ScriptEnv &e);
323  virtual int o_isMovesGT(ScriptEnv &e);
324  virtual int o_isVarEQ(ScriptEnv &e);
325  virtual int o_isCurPicEQ(ScriptEnv &e);
326  virtual int o_isItemPicEQ(ScriptEnv &e);
327 
328  virtual int o_varAdd(ScriptEnv &e);
329  virtual int o_varSub(ScriptEnv &e);
330  virtual int o_varSet(ScriptEnv &e);
331  virtual int o_listInv(ScriptEnv &e);
332  virtual int o_moveItem(ScriptEnv &e);
333  virtual int o_setRoom(ScriptEnv &e);
334  virtual int o_setCurPic(ScriptEnv &e);
335  virtual int o_setPic(ScriptEnv &e);
336  virtual int o_printMsg(ScriptEnv &e);
337  virtual int o_setLight(ScriptEnv &e);
338  virtual int o_setDark(ScriptEnv &e);
339  virtual int o_save(ScriptEnv &e);
340  virtual int o_restore(ScriptEnv &e);
341  virtual int o_restart(ScriptEnv &e);
342  virtual int o_quit(ScriptEnv &e);
343  virtual int o_placeItem(ScriptEnv &e);
344  virtual int o_setItemPic(ScriptEnv &e);
345  virtual int o_resetPic(ScriptEnv &e);
346  virtual int o_takeItem(ScriptEnv &e);
347  virtual int o_dropItem(ScriptEnv &e);
348  virtual int o_setRoomPic(ScriptEnv &e);
349 
350  virtual int goDirection(ScriptEnv &e, Direction D);
351  int o_goNorth(ScriptEnv &e) { return goDirection(e, IDI_DIR_NORTH); }
352  int o_goSouth(ScriptEnv &e) { return goDirection(e, IDI_DIR_SOUTH); }
353  int o_goEast(ScriptEnv &e) { return goDirection(e, IDI_DIR_EAST); }
354  int o_goWest(ScriptEnv &e) { return goDirection(e, IDI_DIR_WEST); }
355  int o_goUp(ScriptEnv &e) { return goDirection(e, IDI_DIR_UP); }
356  int o_goDown(ScriptEnv &e) { return goDirection(e, IDI_DIR_DOWN); }
357 
358  // Graphics
359  void drawPic(byte pic, Common::Point pos = Common::Point()) const;
360 
361  // Sound
362  bool playTones(const Tones &tones, bool isMusic, bool allowSkip = false) const;
363 
364  // Game state functions
365  const Region &getRegion(uint i) const;
366  Region &getRegion(uint i);
367  const Room &getRoom(uint i) const;
368  Room &getRoom(uint i);
369  const Region &getCurRegion() const;
370  Region &getCurRegion();
371  const Room &getCurRoom() const;
372  Room &getCurRoom();
373  const Item &getItem(uint i) const;
374  Item &getItem(uint i);
375  byte getVar(uint i) const;
376  void setVar(uint i, byte value);
377  virtual void takeItem(byte noun);
378  virtual void dropItem(byte noun);
379  bool matchCommand(ScriptEnv &env) const;
380  void doActions(ScriptEnv &env);
381  bool doOneCommand(const Commands &commands, byte verb, byte noun);
382  void doAllCommands(const Commands &commands, byte verb, byte noun);
383  virtual ScriptEnv *createScriptEnv(const Command &cmd, byte room, byte verb, byte noun);
384 
385  // Debug functions
386  static Common::String toAscii(const Common::String &str);
387  Common::String itemStr(uint i) const;
388  Common::String roomStr(uint i) const;
389  Common::String itemRoomStr(uint i) const;
390  Common::String verbStr(uint i) const;
391  Common::String nounStr(uint i) const;
392  Common::String msgStr(uint i) const;
393  Common::String dirStr(Direction dir) const;
394  bool op_debug(const char *fmt, ...) const;
395  Common::DumpFile *_dumpFile;
396 
397  Display *_display;
398  GraphicsMan *_graphics;
399  bool _textMode;
400 
401  // Opcodes
402  Common::Array<Opcode> _condOpcodes, _actOpcodes;
403  // Message strings in data file
405  // Picture data
406  PictureMap _pictures;
407  // Dropped item screen offsets
408  Common::Array<Common::Point> _itemOffsets;
409  // <room, verb, noun, script> lists
410  Commands _roomCommands;
411  Commands _globalCommands;
412  // Data related to the current room
413  RoomData _roomData;
414 
415  WordMap _verbs;
416  WordMap _nouns;
417  Common::StringArray _priVerbs;
418  Common::StringArray _priNouns;
419 
420  struct {
421  Common::String enterCommand;
422  Common::String verbError;
423  Common::String nounError;
424  Common::String playAgain;
425  Common::String pressReturn;
426  Common::String lineFeeds;
427  } _strings;
428 
429  uint32 _verbErrorPos, _nounErrorPos;
430 
431  struct {
432  uint cantGoThere;
433  uint dontUnderstand;
434  uint itemDoesntMove;
435  uint itemNotHere;
436  uint thanksForPlaying;
437  } _messageIds;
438 
439  // Game state
440  State _state;
441 
442  uint _linesPrinted;
443  bool _isRestarting, _isRestoring, _isQuitting;
444  bool _canSaveNow, _canRestoreNow;
445  bool _abortScript;
446  Common::RandomSource *_random;
447 
448  const AdlGameDescription *_gameDescription;
449 
450  mutable Common::File *_inputScript;
451  mutable uint _scriptDelay;
452  mutable bool _scriptPaused;
453 
454 private:
455  virtual void runIntro() { }
456  virtual void init() = 0;
457  virtual void initGameState() = 0;
458  virtual void drawItems() = 0;
459  virtual void drawItem(Item &item, const Common::Point &pos) = 0;
460  virtual void loadRoom(byte roomNr) = 0;
461  virtual void showRoom() = 0;
462  virtual void switchRegion(byte region) { }
463  void runScript(const char *filename) const;
464  void stopScript() const;
465  void setScriptDelay(uint scriptDelay) const { _scriptDelay = scriptDelay; }
466  Common::String getScriptLine() const;
467  // Engine
468  Common::Error run() override;
469  bool hasFeature(EngineFeature f) const override;
470  bool canLoadGameStateCurrently(Common::U32String *msg = nullptr) override;
471 
472  // Text input
473  byte convertKey(uint16 ascii) const;
474 
475  byte _saveVerb, _saveNoun, _restoreVerb, _restoreNoun;
476 };
477 
478 } // End of namespace Adl
479 
480 #endif
Definition: console.h:39
Definition: adl.h:191
Definition: str.h:59
EngineFeature
Definition: engine.h:253
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:199
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:161
Definition: engine.h:144
Platform
Definition: platform.h:46
Definition: adl.h:99
Language
Definition: language.h:45