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 SNATCHER_SCRIPT_H
23#define SNATCHER_SCRIPT_H
24
25#include "snatcher/saveflags.h"
26#include "snatcher/state.h"
27#include "common/array.h"
28#include "common/func.h"
29#include "common/ptr.h"
30
31
32namespace Common {
33class SeekableReadStream;
34class SeekableWriteStream;
35} // namespace Common
36
37namespace Snatcher {
38
39class ActionSequenceHandler;
40class ResourcePointer;
41class SnatcherEngine;
42class UI;
43
44class CmdQueue {
45public:
47 ~CmdQueue();
48 void reset();
49 void writeUInt16(uint16 val);
50 void writeUInt32(uint32 val);
51 void start() { _enable = (_writePos != _data); }
52 bool enabled() const { return _enable; }
53 void run(GameState &state);
54
55private:
56 const uint16 *_readPos;
57 uint16 *_data;
58 uint16 *_writePos;
59 uint16 _currentOpcode;
60 int16 _progress;
61 uint16 _counter;
62 bool _enable;
63
64 SnatcherEngine *_vm;
65 GameState *_state;
66
67private:
68 void makeFunctions();
69
70 void m_end(const uint16 *&data);
71 void m_initAnimations(const uint16 *&data);
72 void m_animPauseAll(const uint16 *&data);
73 void m_drawCommands(const uint16 *&data);
74 void m_fmSfxBlock(const uint16 *&data);
75 void m_printText(const uint16 *&data);
76 void m_06(const uint16 *&data);
77 void m_fmMusicStart(const uint16 *&data);
78 void m_displayDialog(const uint16 *&data);
79 void m_animOps(const uint16 *&data);
80 void m_pcmWait(const uint16 *&data);
81 void m_cdaWait(const uint16 *&data);
82 void m_pcmSound(const uint16 *&data);
83 void m_chatWithPortraitAnim(const uint16 *&data);
84 void m_cdaPlay(const uint16 *&data);
85 void m_gfxReset(const uint16 *&data);
86 void m_waitFrames(const uint16 *&data);
87 void m_loadResource(const uint16 *&data);
88 void m_gfxStart(const uint16 *&data);
89 void m_fmMusicWait(const uint16 *&data);
90 void m_nop(const uint16 *&data);
91 void m_invItemCloseUp(const uint16 *&data);
92 void m_resetTextFields(const uint16 *&data);
93 void m_unused(const uint16 *&data);
94 void m_shooterSequenceRun(const uint16 *&data);
95 void m_fmSfxWait(const uint16 *&data);
96 void m_nop2(const uint16 *&data);
97 void m_cdaSync(const uint16 *&data);
98 void m_fmSoundEffect(const uint16 *&data);
99 void m_pcmBlock(const uint16 *&data);
100 void m_saveGame(const uint16 *&data);
101 void m_shooterSequenceResetScore(const uint16 *&data);
102 void m_gfxPostLoadProcess(const uint16 *&data);
103 void m_waitForStartButton(const uint16 *&data);
104 void m_palOps(const uint16 *&data);
105 void m_clearTextInputLine(const uint16 *&data);
106
109};
110
112public:
113 ScriptArray() : _data(nullptr) {}
114 ScriptArray(uint8 *data, uint8 size) : _data(data) {
115 *data = size;
116 }
117 uint8 size() const { return _data[0]; }
118 uint8 pos() const { return _data[1]; }
119 uint8 &pos() { return _data[1]; }
120 uint16 read(uint8 entry) const {
121 if (entry >= size())
122 error("ScriptArrayEntry::read: entry %d out of bounds", entry);
123 return *((uint16*)&_data[2 + entry * 2]);
124 }
125 void write(uint8 entry, uint16 val) {
126 if (entry >= size())
127 error("ScriptArrayEntry::write: entry %d out of bounds", entry);
128 *((uint16*)&_data[2 + entry * 2]) = val;
129 }
130private:
131 uint8 *_data;
132};
133
134class MemAccessHandler;
136public:
139
140 void resetArrays();
141 void run(Script &script);
142 bool postProcess(Script &script);
143 void processInput();
144
145 void loadState(Common::SeekableReadStream *in, Script &script, SaveFlags flags);
146 void saveState(Common::SeekableWriteStream *out, Script &script, SaveFlags flags);
147
148private:
149 CmdQueue *_que;
150 MemAccessHandler *_mem;
152 UI *_ui;
153 Script *_script;
154
155 uint8 *_arrayData;
156 ScriptArray _arrays[6];
157 uint8 *_flagsTable;
158 int _pos1;
159 int _pos2;
160 uint16 _result;
161
162private:
163 void runOpcode();
164 void getOpcodeProperties(uint16 &m1, uint16 &m2, uint16 &u);
165 void runOpcodeOrReadVar(uint16 mode, uint16 &result);
166 void getFlags(uint16 sel, uint16 &result);
167 void setFlags(uint16 sel, uint32 flags);
168
169 uint8 countFunctionOps();
170 void seekToFunctionOp(int num);
171 bool isExecFuncOpcode();
172 bool stackOps_checkFor52(uint16 &result);
173 bool opcodeHasFlag8();
174 bool evalFinished();
175 void skipOnFlag8();
176 void jump();
177
178 bool getArrayLastEntry(uint16 arrNo, uint16 &result);
179 bool popArrayLastEntry(uint16 arrNo, uint16 &result);
180 bool getArrayEntry(uint16 arrNo, uint8 pos, uint16 &result);
181 bool setArrayLastEntry(uint16 arrNo, uint16 val);
182
183 uint8 getOpcode(int offset) const;
184 uint16 safeReadUINT16(uint16 pos, const char *caller) const;
185
186private:
187 void makeOpcodeTable(ResourcePointer *scd);
188
189 void o_nop();
190 void o_animOps();
191 void o_evalAdd();
192 void o_chatWithPortraitAnim();
193 void o_pcmSound();
194 void o_evalAnd();
195 void o_fmMusicStart();
196 void o_runSubScript();
197 void o_callSubroutine();
198 void o_cdaPlay();
199 void o_incrCounter();
200 void o_fmSoundEffect();
201 void o_evalEquals();
202 void o_clearFlags();
203 void o_setFlags();
204 void o_getPrevPosition();
205 void o_evalLess();
206 void o_evalLessOrEqual();
207 void o_doEitherOr();
208 void o_doIf();
209 void o_setResult();
210 void o_runFunction();
211 void o_evalGreater();
212 void o_evalGreaterOrEqual();
213 void o_break();
214 void o_evalTrue();
215 void o_start();
216 void o_evalOr();
217 void o_condExec();
218 void o_sentenceRecurseBegin();
219 void o_verbOps();
220 void o_sentenceRecurseEnd();
221 void o_returnFromSubScript();
222 void o_checkSubRecurseState();
223 void o_loadModuleAndStartGfx();
224 void o_randomSwitch();
225 void o_sequentialSwitch();
226 void o_64();
227 void o_sysOps();
228 void o_displayDialog();
229 void o_animWait();
230 void o_pcmSoundWait();
231 void o_fmMusicWait();
232 void o_cdaWait();
233 void o_fmSfxWait();
234 void o_waitFrames();
235
236#ifndef SNATCHER_SCRIPT_DEBUG
238#else
239 class ScriptEngineProc : public Common::Functor0Mem<void, ScriptEngine> {
240 public:
241 typedef void (ScriptEngine::*ScrFunc)();
242 ScriptEngineProc(ScriptEngine *se, ScrFunc func, const char *desc, uint8 len, uint8 flg) : Common::Functor0Mem<void, ScriptEngine>(se, func), _desc(Common::String("o_") + desc) {
243 _desc += Common::String::format("() [len: %d, flags: 0x%02x]", len, flg);
244 _recursion = 0;
245 }
246 void operator()(int pos) {
247 Common::String spc;
248 for (int i = 0; i < _recursion; ++i)
249 spc += "|--";
250 debug("0x%04x: %s%s", pos, spc.c_str(), _desc.c_str());
251 ++_recursion;
252 Functor0Mem<void, ScriptEngine>::operator()();
253 --_recursion;
254 }
255 private:
256 static int _recursion;
257 Common::String _desc;
258 };
259#endif
260
261 struct ScriptEngineOpcode {
262 ScriptEngineOpcode() : len(0), flags(0), proc(nullptr) {}
263 ScriptEngineOpcode(uint8 v1, uint8 v2, ScriptEngineProc *p) : len(v1), flags(v2), proc(Common::SharedPtr<ScriptEngineProc>(p)) {}
264 ~ScriptEngineOpcode() {}
265 uint8 len;
266 uint8 flags;
268 };
270};
271
272} // End of namespace Snatcher
273
274#endif // SNATCHER_SCRIPT
Definition: array.h:52
Definition: func.h:389
Definition: func.h:452
Definition: stream.h:745
Definition: stream.h:351
Definition: ptr.h:159
Definition: str.h:59
static String format(MSVC_PRINTF const char *fmt,...) GCC_PRINTF(1
Definition: action.h:36
Definition: script.h:44
Definition: memory.h:42
Definition: resource.h:52
Definition: script.h:111
Definition: script.h:135
Definition: snatcher.h:50
Definition: ui.h:42
void debug(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: algorithm.h:29
Definition: state.h:94
Definition: state.h:65