ScummVM API documentation
script.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 CINE_SCRIPT_H
23 #define CINE_SCRIPT_H
24 
25 #include "common/savefile.h"
26 #include "common/array.h"
27 #include "common/list.h"
28 #include "common/ptr.h"
29 
30 namespace Cine {
31 
32 #define SCRIPT_STACK_SIZE 50
33 #define LOCAL_VARS_SIZE 50
34 
42 class FWScript;
43 
44 typedef int (FWScript::*OpFunc)();
45 
46 struct Opcode {
47  OpFunc proc;
48  const char *args;
49 };
50 
54 class ScriptVars {
55 private:
56  unsigned int _size;
57  int16 *_vars;
58 
59 public:
60  // Explicit to prevent var=0 instead of var[i]=0 typos.
61  explicit ScriptVars(unsigned int len = 50);
62  ScriptVars(Common::SeekableReadStream &fHandle, unsigned int len = 50);
63  ScriptVars(const ScriptVars &src);
64  ~ScriptVars();
65 
66  void reinit(unsigned int len);
67  ScriptVars &operator=(const ScriptVars &src);
68  int16 &operator[](unsigned int idx);
69  int16 operator[](unsigned int idx) const;
70 
71  void save(Common::OutSaveFile &fHandle) const;
72  void save(Common::OutSaveFile &fHandle, unsigned int len) const;
73  void load(Common::SeekableReadStream &fHandle);
74  void load(Common::SeekableReadStream &fHandle, unsigned int len);
75  void reset();
76 };
77 
78 class FWScriptInfo;
79 
86 class RawScript {
87 private:
88  byte *_data;
89  ScriptVars _labels;
90 
91 protected:
92  void computeLabels(const FWScriptInfo &info);
93  int getNextLabel(const FWScriptInfo &info, int offset) const;
94 
95 public:
96  uint16 _size;
97 
98  explicit RawScript(uint16 size);
99  RawScript(const FWScriptInfo &info, const byte *data, uint16 size);
100  RawScript(const RawScript &src);
101  ~RawScript();
102 
103  RawScript &operator=(const RawScript &src);
104 
105  void setData(const FWScriptInfo &info, const byte *data);
106  const ScriptVars &labels() const;
107  byte getByte(unsigned int pos) const;
108  uint16 getWord(unsigned int pos) const;
109  const char *getString(unsigned int pos) const;
110  uint16 getLabel(const FWScriptInfo &info, byte index, uint16 offset) const;
111 };
112 
119 class RawObjectScript : public RawScript {
120 public:
121  int16 _runCount;
122  uint16 _param1;
123  uint16 _param2;
124  uint16 _param3;
125 
126  RawObjectScript(uint16 size, uint16 p1, uint16 p2, uint16 p3);
127  RawObjectScript(const FWScriptInfo &info, const byte *data, uint16 size, uint16 p1, uint16 p2, uint16 p3);
128 
133  int16 run() { return _runCount++; }
134 };
135 
140 class FWScript {
141 private:
142  const RawScript &_script;
143  uint16 _pos;
144  uint16 _line;
145  uint16 _compare;
146  ScriptVars _labels;
147  ScriptVars _localVars;
148  ScriptVars &_globalVars;
149  FWScriptInfo *_info;
150 
151 protected:
152  static const Opcode *_opcodeTable;
153  static unsigned int _numOpcodes;
154 
155  int o1_modifyObjectParam();
156  int o1_getObjectParam();
157  int o1_addObjectParam();
158  int o1_subObjectParam();
159  int o1_mulObjectParam();
160  int o1_divObjectParam();
161  int o1_compareObjectParam();
162  int o1_setupObject();
163  int o1_checkCollision();
164  int o1_loadVar();
165  int o1_addVar();
166  int o1_subVar();
167  int o1_mulVar();
168  int o1_divVar();
169  int o1_compareVar();
170  int o1_modifyObjectParam2();
171  int o1_loadMask0();
172  int o1_unloadMask0();
173  int o1_addToBgList();
174  int o1_loadMask1();
175  int o1_unloadMask1();
176  int o1_loadMask4();
177  int o1_unloadMask4();
178  int o1_addSpriteFilledToBgList();
179  int o1_clearBgIncrustList();
180  int o1_label();
181  int o1_goto();
182  int o1_gotoIfSup();
183  int o1_gotoIfSupEqu();
184  int o1_gotoIfInf();
185  int o1_gotoIfInfEqu();
186  int o1_gotoIfEqu();
187  int o1_gotoIfDiff();
188  int o1_removeLabel();
189  int o1_loop();
190  int o1_startGlobalScript();
191  int o1_endGlobalScript();
192  int o1_loadAnim();
193  int o1_loadBg();
194  int o1_loadCt();
195  int o1_loadPart();
196  int o1_closePart();
197  int o1_loadNewPrcName();
198  int o1_requestCheckPendingDataLoad();
199  int o1_blitAndFade();
200  int o1_fadeToBlack();
201  int o1_transformPaletteRange();
202  int o1_setDefaultMenuBgColor();
203  int o1_palRotate();
204  int o1_break();
205  int o1_endScript();
206  int o1_message();
207  int o1_loadGlobalVar();
208  int o1_compareGlobalVar();
209  int o1_declareFunctionName();
210  int o1_freePartRange();
211  int o1_unloadAllMasks();
212  int o1_setScreenDimensions();
213  int o1_displayBackground();
214  int o1_initializeZoneData();
215  int o1_setZoneDataEntry();
216  int o1_getZoneDataEntry();
217  int o1_setPlayerCommandPosY();
218  int o1_allowPlayerInput();
219  int o1_disallowPlayerInput();
220  int o1_changeDataDisk();
221  int o1_loadMusic();
222  int o1_playMusic();
223  int o1_fadeOutMusic();
224  int o1_stopSample();
225  int o1_op71();
226  int o1_op72();
227  int o1_op73();
228  int o1_playSample();
229  int o1_playSampleSwapped();
230  int o1_disableSystemMenu();
231  int o1_loadMask5();
232  int o1_unloadMask5();
233 
234  // pointers to member functions in C++ suck...
235  int o2_loadCt();
236  int o2_loadPart();
237  int o2_addSeqListElement();
238  int o2_removeSeq();
239  int o2_playSample();
240  int o2_playSampleAlt();
241  int o2_clearSeqList();
242  int o2_modifySeqListElement();
243  int o2_isSeqRunning();
244  int o2_gotoIfSupNearest();
245  int o2_gotoIfSupEquNearest();
246  int o2_gotoIfInfNearest();
247  int o2_gotoIfInfEquNearest();
248  int o2_gotoIfEquNearest();
249  int o2_gotoIfDiffNearest();
250  int o2_startObjectScript();
251  int o2_stopObjectScript();
252  int o2_op8D();
253  int o2_addBackground();
254  int o2_removeBackground();
255  int o2_loadAbs();
256  int o2_loadBg();
257  int o2_wasZoneChecked();
258  int o2_op9B();
259  int o2_op9C();
260  int o2_useBgScroll();
261  int o2_setAdditionalBgVScroll();
262  int o2_op9F();
263  int o2_addGfxElementType20();
264  int o2_removeGfxElementType20();
265  int o2_addGfxElementType21();
266  int o2_removeGfxElementType21();
267  int o2_loadMask22();
268  int o2_unloadMask22();
269 
270  byte getNextByte();
271  uint16 getNextWord();
272  const char *getNextString();
273 
274  void load(const ScriptVars &labels, const ScriptVars &local, uint16 compare, uint16 pos);
275 
276  FWScript(const RawScript &script, int16 index, FWScriptInfo *info);
277  FWScript(RawObjectScript &script, int16 index, FWScriptInfo *info);
278  FWScript(const FWScript &src, FWScriptInfo *info);
279 
280 public:
281  int16 _index;
282 
283  static void setupTable();
284 
285  FWScript(const RawScript &script, int16 index);
286 // FWScript(const RawObjectScript &script, int16 index);
287  FWScript(const FWScript &src);
288  ~FWScript();
289 
290  int execute();
291  void save(Common::OutSaveFile &fHandle) const;
292 
293  friend class FWScriptInfo;
294 
295  // workaround for bug in g++ which prevents protected member functions
296  // of FWScript from being used in OSScript::_opcodeTable[]
297  // initialization ("error: protected within this context")
298  friend class OSScript;
299 };
300 
304 class OSScript : public FWScript {
305 private:
306  static const Opcode *_opcodeTable;
307  static unsigned int _numOpcodes;
308 
309 protected:
310  void load(const ScriptVars &labels, const ScriptVars &local, uint16 compare, uint16 pos);
311 
312 public:
313  static void setupTable();
314 
315  OSScript(const RawScript &script, int16 index);
316  OSScript(RawObjectScript &script, int16 index);
317  OSScript(const OSScript &src);
318 
319  friend class OSScriptInfo;
320 };
321 
326 protected:
327  virtual OpFunc opcodeHandler(byte opcode) const;
328 
329 public:
330  virtual ~FWScriptInfo() {}
331 
332  virtual const char *opcodeInfo(byte opcode) const;
333  virtual FWScript *create(const RawScript &script, int16 index) const;
334  virtual FWScript *create(const RawObjectScript &script, int16 index) const;
335  virtual FWScript *create(const RawScript &script, int16 index, const ScriptVars &labels, const ScriptVars &local, uint16 compare, uint16 pos) const;
336  virtual FWScript *create(const RawObjectScript &script, int16 index, const ScriptVars &labels, const ScriptVars &local, uint16 compare, uint16 pos) const;
337 
338  friend class FWScript;
339 };
340 
344 class OSScriptInfo : public FWScriptInfo {
345 protected:
346  OpFunc opcodeHandler(byte opcode) const override;
347 
348 public:
349  ~OSScriptInfo() override {}
350 
351  const char *opcodeInfo(byte opcode) const override;
352  FWScript *create(const RawScript &script, int16 index) const override;
353  FWScript *create(const RawObjectScript &script, int16 index) const override;
354  FWScript *create(const RawScript &script, int16 index, const ScriptVars &labels, const ScriptVars &local, uint16 compare, uint16 pos) const override;
355  FWScript *create(const RawObjectScript &script, int16 index, const ScriptVars &labels, const ScriptVars &local, uint16 compare, uint16 pos) const override;
356 
357  friend class FWScript;
358 };
359 
366 
367 #define NUM_MAX_SCRIPT 50
368 
369 FWScriptInfo *setupOpcodes();
370 
371 void decompileScript(const byte *scriptPtr, uint16 scriptSize, uint16 scriptIdx);
372 void dumpScript(char *dumpName);
373 
374 #define OP_loadPart 0x3F
375 #define OP_loadNewPrcName 0x41
376 #define OP_requestCheckPendingDataLoad 0x42
377 #define OP_endScript 0x50
378 
379 void addScriptToGlobalScripts(uint16 idx);
380 int16 checkCollision(int16 objIdx, int16 x, int16 y, int16 numZones, int16 zoneIdx);
381 
382 void runObjectScript(int16 entryIdx);
383 
384 void executeObjectScripts();
385 void executeGlobalScripts();
386 
387 void purgeObjectScripts();
388 void purgeGlobalScripts();
389 
390 } // End of namespace Cine
391 
392 #endif
Definition: script.h:54
Definition: script.h:119
Definition: script.h:46
uint16 _param2
Additional parameter not used at the moment.
Definition: script.h:123
Definition: savefile.h:54
Definition: script.h:304
Definition: anim.h:29
int16 run()
Definition: script.h:133
Definition: stream.h:745
Definition: script.h:140
Definition: script.h:86
int16 _index
Index in script table.
Definition: script.h:281
uint16 _param3
Additional parameter not used at the moment.
Definition: script.h:124
uint16 _param1
Additional parameter not used at the moment.
Definition: script.h:122
Definition: script.h:344
Definition: ptr.h:159
int16 _runCount
How many times the script was used.
Definition: script.h:121
uint16 _size
Bytecode length.
Definition: script.h:96
Definition: script.h:325