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 PRINCE_SCRIPT_H
23 #define PRINCE_SCRIPT_H
24 
25 #include "common/random.h"
26 #include "common/endian.h"
27 #include "common/array.h"
28 #include "common/stream.h"
29 
30 #include "prince/flags.h"
31 
32 namespace Prince {
33 
34 class PrinceEngine;
35 class Animation;
36 class Object;
37 struct Anim;
38 struct BackgroundAnim;
39 struct Mask;
40 
41 class Room {
42 public:
43  Room();
44  int _mobs; // mob flag offset
45  int _backAnim; // offset to array of animation numbers
46  int _obj; // offset to array of object numbers
47  int _nak; // offset to array of masks
48  int _itemUse;
49  int _itemGive;
50  int _walkTo; // offset to array of WALKTO events or 0
51  int _examine; // offset to array of EXAMINE events or 0
52  int _pickup;
53  int _use;
54  int _pushOpen;
55  int _pullClose;
56  int _talk;
57  int _give;
58 
59  bool loadStream(Common::SeekableReadStream &stream);
60  bool loadRoom(byte *roomData);
61  int getOptionOffset(int option);
62 
63 private:
64 
65  typedef void (Room::*LoadingStep)(Common::SeekableReadStream &stream);
66 
67  void nextLoadStep(Common::SeekableReadStream &stream, LoadingStep step);
68 
69  void loadMobs(Common::SeekableReadStream &stream);
70  void loadBackAnim(Common::SeekableReadStream &stream);
71  void loadObj(Common::SeekableReadStream &stream);
72  void loadNak(Common::SeekableReadStream &stream);
73  void loadItemUse(Common::SeekableReadStream &stream);
74  void loadItemGive(Common::SeekableReadStream &stream);
75  void loadWalkTo(Common::SeekableReadStream &stream);
76  void loadExamine(Common::SeekableReadStream &stream);
77  void loadPickup(Common::SeekableReadStream &stream);
78  void loadUse(Common::SeekableReadStream &stream);
79  void loadPushOpen(Common::SeekableReadStream &stream);
80  void loadPullClose(Common::SeekableReadStream &stream);
81  void loadTalk(Common::SeekableReadStream &stream);
82  void loadGive(Common::SeekableReadStream &stream);
83 };
84 
85 
86 class Script {
87 public:
88  static const int16 kMaxRooms = 60;
89 
90  Script(PrinceEngine *vm);
91  ~Script();
92 
93  struct ScriptInfo {
94  int rooms;
95  int startGame;
96  int restoreGame;
97  int stdExamine;
98  int stdPickup;
99  int stdUse;
100  int stdOpen;
101  int stdClose;
102  int stdTalk;
103  int stdGive;
104  int usdCode;
105  int invObjExam;
106  int invObjUse;
107  int invObjUU;
108  int stdUseItem;
109  int lightSources;
110  int specRout;
111  int invObjGive;
112  int stdGiveItem;
113  int goTester;
114  };
115 
116  ScriptInfo _scriptInfo;
117 
118  bool loadStream(Common::SeekableReadStream &stream);
119 
120  uint16 readScript16(uint32 address);
121  uint32 readScript32(uint32 address);
122 
123  uint32 getStartGameOffset();
124  uint32 getLocationInitScript(int initRoomTableOffset, int roomNr);
125  int16 getLightX(int locationNr);
126  int16 getLightY(int locationNr);
127  int32 getShadowScale(int locationNr);
128  uint8 *getRoomOffset(int locationNr);
129  int32 getOptionStandardOffset(int option);
130  uint8 *getHeroAnimName(int offset);
131 
132  void installBackAnims(Common::Array<BackgroundAnim> &backAnimList, int roomBackAnimOffset);
133  void installSingleBackAnim(Common::Array<BackgroundAnim> &backAnimList, int slot, int roomBackAnimOffset);
134  void installObjects(int offset);
135  bool loadAllMasks(Common::Array<Mask> &maskList, int offset);
136 
137  int scanMobEvents(int mobMask, int dataEventOffset);
138  int scanMobEventsWithItem(int mobMask, int dataEventOffset, int itemMask);
139 
140  byte getMobVisible(int roomMobOffset, uint16 mob);
141  void setMobVisible(int roomMobOffset, uint16 mob, byte value);
142 
143  uint32 getBackAnimId(int roomBackAnimOffset, int slot);
144  void setBackAnimId(int roomBackAnimOffset, int slot, int animId);
145 
146  byte getObjId(int roomObjOffset, int slot);
147  void setObjId(int roomObjOffset, int slot, byte objectId);
148 
149  const char *getString(uint32 offset) {
150  return (const char *)(&_data[offset]);
151  }
152 
153 private:
154  PrinceEngine *_vm;
155  uint8 *_data;
156  uint32 _dataSize;
157  Common::Array<Room> _roomList;
158 };
159 
161 public:
163 
164  void setFlagValue(Flags::Id flag, int32 value);
165  int32 getFlagValue(Flags::Id flag);
166 
167  void resetAllFlags();
168 
169  static const uint16 kFlagMask = 0x8000;
170  static const uint16 kMaxFlags = 2000;
171 private:
172  int32 _flags[kMaxFlags];
173 };
174 
175 class Interpreter {
176 public:
177  Interpreter(PrinceEngine *vm, Script *script, InterpreterFlags *flags);
178 
179  void stopBg() { _bgOpcodePC = 0; }
180 
181  void stepBg();
182  void stepFg();
183  void storeNewPC(int opcodePC);
184  int getLastOPCode();
185  int getFgOpcodePC();
186 
187  void setBgOpcodePC(uint32 value);
188  void setFgOpcodePC(uint32 value);
189 
190  uint32 getCurrentString();
191  void setCurrentString(uint32 value);
192 
193  byte *getString();
194  void setString(byte *newString);
195  void increaseString();
196 
197  void setResult(byte value);
198 
199 private:
200  PrinceEngine *_vm;
201  Script *_script;
202  InterpreterFlags *_flags;
203 
204  uint32 _currentInstruction;
205 
206  uint32 _bgOpcodePC;
207  uint32 _fgOpcodePC;
208 
209  uint16 _lastOpcode;
210  uint32 _lastInstruction;
211  byte _result;
212 
213  bool _opcodeNF; // break interpreter loop
214  bool _opcodeEnd; // end of a game flag
215 
216  static const uint32 _STACK_SIZE = 500;
217  uint32 _stack[_STACK_SIZE];
218  struct stringStack {
219  byte *string;
220  byte *dialogData;
221  uint32 currentString;
222  } _stringStack;
223  uint8 _stacktop;
224  uint32 _waitFlag;
225 
226  byte *_string;
227  byte _stringBuf[1024];
228  uint32 _currentString;
229  const char *_mode;
230 
231  Flags _flagMap;
232 
233  // Helper functions
234  uint32 step(uint32 opcodePC);
235  uint16 readScript16();
236  uint32 readScript32();
237  int32 readScriptFlagValue();
238  Flags::Id readScriptFlagId();
239  int checkSeq(byte *string);
240 
241  void debugInterpreter(const char *s, ...);
242 
243  typedef void (Interpreter::*OpcodeFunc)();
244  static OpcodeFunc _opcodes[];
245 
246  static const uint kGiveLetterScriptFix = 79002;
247  static const uint kSecondBirdAnimationScriptFix = 45658;
248 
249  // Keep opcode handlers names as they are in original code
250  // making it easier to switch back and forth
251  void O_WAITFOREVER();
252  void O_BLACKPALETTE();
253  void O_SETUPPALETTE();
254  void O_INITROOM();
255  void O_SETSAMPLE();
256  void O_FREESAMPLE();
257  void O_PLAYSAMPLE();
258  void O_PUTOBJECT();
259  void O_REMOBJECT();
260  void O_SHOWANIM();
261  void O_CHECKANIMEND();
262  void O_FREEANIM();
263  void O_CHECKANIMFRAME();
264  void O_PUTBACKANIM();
265  void O_REMBACKANIM();
266  void O_CHECKBACKANIMFRAME();
267  void O_FREEALLSAMPLES();
268  void O_SETMUSIC();
269  void O_STOPMUSIC();
270  void O__WAIT();
271  void O_UPDATEOFF();
272  void O_UPDATEON();
273  void O_UPDATE ();
274  void O_CLS();
275  void O__CALL();
276  void O_RETURN();
277  void O_GO();
278  void O_BACKANIMUPDATEOFF();
279  void O_BACKANIMUPDATEON();
280  void O_CHANGECURSOR();
281  void O_CHANGEANIMTYPE();
282  void O__SETFLAG();
283  void O_COMPARE();
284  void O_JUMPZ();
285  void O_JUMPNZ();
286  void O_EXIT();
287  void O_ADDFLAG();
288  void O_TALKANIM();
289  void O_SUBFLAG();
290  void O_SETSTRING();
291  void O_ANDFLAG();
292  void O_GETMOBDATA();
293  void O_ORFLAG();
294  void O_SETMOBDATA();
295  void O_XORFLAG();
296  void O_GETMOBTEXT();
297  void O_MOVEHERO();
298  void O_WALKHERO();
299  void O_SETHERO();
300  void O_HEROOFF();
301  void O_HEROON();
302  void O_CLSTEXT();
303  void O_CALLTABLE();
304  void O_CHANGEMOB();
305  void O_ADDINV();
306  void O_REMINV();
307  void O_REPINV();
308  void O_OBSOLETE_GETACTION();
309  void O_ADDWALKAREA();
310  void O_REMWALKAREA();
311  void O_RESTOREWALKAREA();
312  void O_WAITFRAME();
313  void O_SETFRAME();
314  void O_RUNACTION();
315  void O_COMPAREHI();
316  void O_COMPARELO();
317  void O_PRELOADSET();
318  void O_FREEPRELOAD();
319  void O_CHECKINV();
320  void O_TALKHERO();
321  void O_WAITTEXT();
322  void O_SETHEROANIM();
323  void O_WAITHEROANIM();
324  void O_GETHERODATA();
325  void O_GETMOUSEBUTTON();
326  void O_CHANGEFRAMES();
327  void O_CHANGEBACKFRAMES();
328  void O_GETBACKANIMDATA();
329  void O_GETANIMDATA();
330  void O_SETBGCODE();
331  void O_SETBACKFRAME();
332  void O_GETRND();
333  void O_TALKBACKANIM();
334  void O_LOADPATH();
335  void O_GETCHAR();
336  void O_SETDFLAG();
337  void O_CALLDFLAG();
338  void O_PRINTAT();
339  void O_ZOOMIN();
340  void O_ZOOMOUT();
341  void O_SETSTRINGOFFSET();
342  void O_GETOBJDATA();
343  void O_SETOBJDATA();
344  void O_SWAPOBJECTS();
345  void O_CHANGEHEROSET();
346  void O_ADDSTRING();
347  void O_SUBSTRING();
348  void O_INITDIALOG();
349  void O_ENABLEDIALOGOPT();
350  void O_DISABLEDIALOGOPT();
351  void O_SHOWDIALOGBOX();
352  void O_STOPSAMPLE();
353  void O_BACKANIMRANGE();
354  void O_CLEARPATH();
355  void O_SETPATH();
356  void O_GETHEROX();
357  void O_GETHEROY();
358  void O_GETHEROD();
359  void O_PUSHSTRING();
360  void O_POPSTRING();
361  void O_SETFGCODE();
362  void O_STOPHERO();
363  void O_ANIMUPDATEOFF();
364  void O_ANIMUPDATEON();
365  void O_FREECURSOR();
366  void O_ADDINVQUIET();
367  void O_RUNHERO();
368  void O_SETBACKANIMDATA();
369  void O_VIEWFLC();
370  void O_CHECKFLCFRAME();
371  void O_CHECKFLCEND();
372  void O_FREEFLC();
373  void O_TALKHEROSTOP();
374  void O_HEROCOLOR();
375  void O_GRABMAPA();
376  void O_ENABLENAK();
377  void O_DISABLENAK();
378  void O_GETMOBNAME();
379  void O_SWAPINVENTORY();
380  void O_CLEARINVENTORY();
381  void O_SKIPTEXT();
382  void O_SETVOICEH();
383  void O_SETVOICEA();
384  void O_SETVOICEB();
385  void O_SETVOICEC();
386  void O_VIEWFLCLOOP();
387  void O_FLCSPEED();
388  void O_OPENINVENTORY();
389  void O_KRZYWA();
390  void O_GETKRZYWA();
391  void O_GETMOB();
392  void O_INPUTLINE();
393  void O_SETVOICED();
394  void O_BREAK_POINT();
395 
396 };
397 
398 } // End of namespace Prince
399 
400 #endif
Definition: prince.h:270
Definition: script.h:93
Definition: array.h:52
Definition: stream.h:745
Definition: animation.h:30
Definition: script.h:86
Definition: script.h:41
Definition: script.h:175
Definition: script.h:160
Definition: flags.h:30