ScummVM API documentation
myst_scripts.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 MYST_SCRIPTS_H
23 #define MYST_SCRIPTS_H
24 
25 #include "common/ptr.h"
26 #include "common/scummsys.h"
27 #include "common/util.h"
28 
29 #include "mohawk/myst_state.h"
30 
31 namespace Mohawk {
32 
33 typedef Common::Array<uint16> ArgumentsArray;
34 
35 #define DECLARE_OPCODE(x) void x(uint16 var, const ArgumentsArray &args)
36 
37 class MohawkEngine_Myst;
38 class MystArea;
39 
40 enum MystScriptType {
41  kMystScriptNone,
42  kMystScriptNormal,
43  kMystScriptInit,
44  kMystScriptExit
45 };
46 
49 
50  MystScriptType type;
51  uint16 resourceId;
52  uint16 opcode;
53  uint16 var;
54  ArgumentsArray args;
55  uint16 u1;
56 };
57 
59 
61 public:
62  MystScriptParser(MohawkEngine_Myst *vm, MystStack stackId);
63  virtual ~MystScriptParser();
64 
65  void runScript(const MystScript &script, MystArea *invokingResource = nullptr);
66  void runOpcode(uint16 op, uint16 var = 0, const ArgumentsArray &args = ArgumentsArray());
67  const Common::String getOpcodeDesc(uint16 op);
68  MystScript readScript(Common::SeekableReadStream *stream, MystScriptType type);
69  void setInvokingResource(MystArea *resource) { _invokingResource = resource; }
70 
76  bool isScriptRunning() const;
77 
78  virtual void disablePersistentScripts() = 0;
79  virtual void runPersistentScripts() = 0;
80 
81  virtual uint16 getVar(uint16 var);
82  virtual void toggleVar(uint16 var);
83  virtual bool setVarValue(uint16 var, uint16 value);
84 
85  MystStack getStackId() const { return _stackId; }
86  virtual uint16 getMap() { return 0; }
87  void showMap();
88 
89  void animatedUpdate(const ArgumentsArray &args, uint16 delay);
90  void soundWaitStop() const;
91 
92  // Common opcodes
93  DECLARE_OPCODE(o_toggleVar);
94  DECLARE_OPCODE(o_setVar);
95  DECLARE_OPCODE(o_changeCardSwitch4);
96  DECLARE_OPCODE(o_changeCardSwitchLtR);
97  DECLARE_OPCODE(o_changeCardSwitchRtL);
98  DECLARE_OPCODE(o_takePage);
99  DECLARE_OPCODE(o_redrawCard);
100  DECLARE_OPCODE(o_goToDest);
101  DECLARE_OPCODE(o_goToDestForward);
102  DECLARE_OPCODE(o_goToDestRight);
103  DECLARE_OPCODE(o_goToDestLeft);
104  DECLARE_OPCODE(o_goToDestUp);
105  DECLARE_OPCODE(o_triggerMovie);
106  DECLARE_OPCODE(o_toggleVarNoRedraw);
107  DECLARE_OPCODE(o_drawAreaState);
108  DECLARE_OPCODE(o_redrawAreaForVar);
109  DECLARE_OPCODE(o_changeCardDirectional);
110  DECLARE_OPCODE(o_changeCardPush);
111  DECLARE_OPCODE(o_changeCardPop);
112  DECLARE_OPCODE(o_enableAreas);
113  DECLARE_OPCODE(o_disableAreas);
114  DECLARE_OPCODE(o_directionalUpdate);
115  DECLARE_OPCODE(o_toggleAreasActivation);
116  DECLARE_OPCODE(o_playSound);
117  DECLARE_OPCODE(o_stopSoundBackground);
118  DECLARE_OPCODE(o_playSoundBlocking);
119  DECLARE_OPCODE(o_copyBackBufferToScreen);
120  DECLARE_OPCODE(o_copyImageToBackBuffer);
121  DECLARE_OPCODE(o_changeBackgroundSound);
122  DECLARE_OPCODE(o_soundPlaySwitch);
123  DECLARE_OPCODE(o_copyImageToScreen);
124  DECLARE_OPCODE(o_soundResumeBackground);
125  DECLARE_OPCODE(o_changeCard);
126  DECLARE_OPCODE(o_drawImageChangeCard);
127  DECLARE_OPCODE(o_changeMainCursor);
128  DECLARE_OPCODE(o_hideCursor);
129  DECLARE_OPCODE(o_showCursor);
130  DECLARE_OPCODE(o_delay);
131  DECLARE_OPCODE(o_changeStack);
132  DECLARE_OPCODE(o_changeCardPlaySoundDirectional);
133  DECLARE_OPCODE(o_directionalUpdatePlaySound);
134  DECLARE_OPCODE(o_saveMainCursor);
135  DECLARE_OPCODE(o_restoreMainCursor);
136  DECLARE_OPCODE(o_soundWaitStop);
137  DECLARE_OPCODE(o_exitMap);
138 
139  // Used in multiple stacks
140  DECLARE_OPCODE(o_quit);
141 
142  DECLARE_OPCODE(NOP);
143 
144 protected:
145  MohawkEngine_Myst *_vm;
146  MystGameState::Globals &_globals;
147 
149 
150 #define REGISTER_OPCODE(op, cls, method) \
151  registerOpcode( \
152  op, #method, new Common::Functor2Mem<uint16, const ArgumentsArray &, void, cls>(this, &cls::method) \
153  )
154 
155 #define OVERRIDE_OPCODE(op, cls, method) \
156  overrideOpcode( \
157  op, #method, new Common::Functor2Mem<uint16, const ArgumentsArray &, void, cls>(this, &cls::method) \
158  )
159 
160  void registerOpcode(uint16 op, const char *name, OpcodeProcMyst *command);
161  void overrideOpcode(uint16 op, const char *name, OpcodeProcMyst *command);
162 
163  uint16 _savedCardId;
164  uint16 _savedMapCardId;
165  uint16 _savedCursorId;
166  int16 _tempVar; // Generic temp var used by the scripts
167  uint32 _startTime; // Generic start time used by the scripts
168 
169  static const MystStack _stackMap[];
170  static const uint16 _startCard[];
171 
172  void setupCommonOpcodes();
173 
174  template<class T>
175  T *getInvokingResource() const;
176 
177 private:
178  struct MystOpcode {
179  MystOpcode(uint16 o, OpcodeProcMyst *p, const char *d) : op(o), proc(p), desc(d) {}
180 
181  uint16 op;
183  const char *desc;
184  };
185 
186  Common::Array<MystOpcode> _opcodes;
187 
188  MystArea *_invokingResource;
189  int32 _scriptNestingLevel;
190 
191  const MystStack _stackId;
192 
193  Common::String describeCommand(const MystOpcode &command, uint16 var, const ArgumentsArray &args);
194 };
195 
196 template<class T>
197 T *MystScriptParser::getInvokingResource() const {
198  T *resource = dynamic_cast<T *>(_invokingResource);
199 
200  if (!resource) {
201  error("Invoking resource has unexpected type");
202  }
203 
204  return resource;
205 }
206 
207 } // End of namespace Mohawk
208 
209 #undef DECLARE_OPCODE
210 
211 #endif
Definition: str.h:59
Definition: myst_state.h:128
Definition: stream.h:745
Definition: myst_scripts.h:47
Definition: myst_scripts.h:60
Definition: myst.h:129
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: myst_areas.h:59
Definition: ptr.h:159
Definition: func.h:473
Definition: bitmap.h:32