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 ULTIMA4_GAME_SCRIPT_H
23#define ULTIMA4_GAME_SCRIPT_H
24
25#include "ultima/ultima4/core/types.h"
26#include "ultima/shared/conf/xml_node.h"
27#include "common/file.h"
28#include "common/list.h"
29
30namespace Ultima {
31namespace Ultima4 {
32
46class Script {
47public:
55 class Provider {
56 public:
57 virtual ~Provider() {}
58 virtual Common::String translate(Common::Array<Common::String> &parts) = 0;
59 };
60
61private:
65 class Variable {
66 public:
67 Variable();
68 Variable(const Common::String &v);
69 Variable(const int &v);
70
71 int &getInt();
72 Common::String &getString();
73
74 void setValue(const int &v);
75 void setValue(const Common::String &v);
76 void unset();
77
78 bool isInt() const;
79 bool isString() const;
80 bool isSet() const;
81
82 private:
83 int _iVal;
84 Common::String _sVal;
85 bool _set;
86 };
87
88public:
93 RET_OK,
94 RET_REDIRECTED,
95 RET_STOP
96 };
97
101 enum State {
102 STATE_UNLOADED,
103 STATE_NORMAL,
104 STATE_DONE,
105 STATE_INPUT
106 };
107
112 INPUT_CHOICE,
113 INPUT_NUMBER,
114 INPUT_STRING,
115 INPUT_DIRECTION,
116 INPUT_PLAYER,
117 INPUT_KEYPRESS
118 };
119
123 enum Action {
124 ACTION_SET_CONTEXT,
125 ACTION_UNSET_CONTEXT,
126 ACTION_END,
127 ACTION_REDIRECT,
128 ACTION_WAIT_FOR_KEY,
129 ACTION_WAIT,
130 ACTION_STOP,
131 ACTION_INCLUDE,
132 ACTION_FOR_LOOP,
133 ACTION_RANDOM,
134 ACTION_MOVE,
135 ACTION_SLEEP,
136 ACTION_CURSOR,
137 ACTION_PAY,
138 ACTION_IF,
139 ACTION_INPUT,
140 ACTION_ADD,
141 ACTION_LOSE,
142 ACTION_HEAL,
143 ACTION_CAST_SPELL,
144 ACTION_DAMAGE,
145 ACTION_KARMA,
146 ACTION_MUSIC,
147 ACTION_SET_VARIABLE,
148 ACTION_ZTATS
149 };
150
155 ~Script();
156
160 void addProvider(const Common::String &name, Provider *p);
161
165 bool load(const Common::String &filename, const Common::String &baseId, const Common::String &subNodeName = "", const Common::String &subNodeId = "");
166
170 void unload();
171
175 void run(const Common::String &script);
176
180 ReturnCode execute(Shared::XMLNode *script, Shared::XMLNode *currentItem = nullptr, Common::String *output = nullptr);
181
185 void _continue();
186
191 void setState(State state);
192 State getState();
193
194 void setTarget(const Common::String &val);
195 void setChoices(const Common::String &val);
196 void setVar(const Common::String &name, const Common::String &val);
197 void setVar(const Common::String &name, int val);
198 void unsetVar(const Common::String &name);
199
200 Common::String getTarget();
201 InputType getInputType();
202 Common::String getInputName();
203 Common::String getChoices();
204 int getInputMaxLen();
205
206private:
210 void translate(Common::String *script);
211
215 Shared::XMLNode *find(Shared::XMLNode *node, const Common::String &script, const Common::String &choice = "", bool _default = false);
216
221 Common::String getPropAsStr(Common::List<Shared::XMLNode *> &nodes, const Common::String &prop, bool recursive);
222
223 Common::String getPropAsStr(Shared::XMLNode *node, const Common::String &prop, bool recursive = false);
224
228 int getPropAsInt(Common::List<Shared::XMLNode *> &nodes, const Common::String &prop, bool recursive);
229
230 int getPropAsInt(Shared::XMLNode *node, const Common::String &prop, bool recursive = false);
231
235 Common::String getContent(Shared::XMLNode *node);
236
237 /*
238 * Action Functions
239 */
243 ReturnCode pushContext(Shared::XMLNode *script, Shared::XMLNode *current);
244
248 ReturnCode popContext(Shared::XMLNode *script, Shared::XMLNode *current);
249
253 ReturnCode end(Shared::XMLNode *script, Shared::XMLNode *current);
254
258 ReturnCode waitForKeypress(Shared::XMLNode *script, Shared::XMLNode *current);
259
263 ReturnCode redirect(Shared::XMLNode *script, Shared::XMLNode *current);
264
268 ReturnCode include(Shared::XMLNode *script, Shared::XMLNode *current);
269
273 ReturnCode wait(Shared::XMLNode *script, Shared::XMLNode *current);
274
278 ReturnCode forLoop(Shared::XMLNode *script, Shared::XMLNode *current);
279
283 ReturnCode randomScript(Shared::XMLNode *script, Shared::XMLNode *current);
284
288 ReturnCode move(Shared::XMLNode *script, Shared::XMLNode *current);
289
293 ReturnCode sleep(Shared::XMLNode *script, Shared::XMLNode *current);
294
298 ReturnCode cursor(Shared::XMLNode *script, Shared::XMLNode *current);
299
303 ReturnCode pay(Shared::XMLNode *script, Shared::XMLNode *current);
304
308 ReturnCode _if(Shared::XMLNode *script, Shared::XMLNode *current);
309
313 ReturnCode input(Shared::XMLNode *script, Shared::XMLNode *current);
314
318 ReturnCode add(Shared::XMLNode *script, Shared::XMLNode *current);
319
323 ReturnCode lose(Shared::XMLNode *script, Shared::XMLNode *current);
324
328 ReturnCode heal(Shared::XMLNode *script, Shared::XMLNode *current);
329
333 ReturnCode castSpell(Shared::XMLNode *script, Shared::XMLNode *current);
334
338 ReturnCode damage(Shared::XMLNode *script, Shared::XMLNode *current);
339
343 ReturnCode karma(Shared::XMLNode *script, Shared::XMLNode *current);
344
348 ReturnCode music(Shared::XMLNode *script, Shared::XMLNode *current);
349
353 ReturnCode setVar(Shared::XMLNode *script, Shared::XMLNode *current);
354
358 ReturnCode ztats(Shared::XMLNode *script, Shared::XMLNode *current);
359
360 /*
361 * Math and comparison functions
362 */
363
370 void mathParseChildren(Shared::XMLNode *math, Common::String *result);
371
375 int mathValue(const Common::String &str);
376
380 int math(int lval, int rval, Common::String &op);
381
387 bool mathParse(const Common::String &str, int *lval, int *rval, Common::String *op);
388
393 void parseOperation(const Common::String &str, Common::String *lval, Common::String *rval, Common::String *op);
394
399 bool compare(const Common::String &str);
400
404 void funcParse(const Common::String &str, Common::String *funcName, Common::String *contents);
405
406 /*
407 * Static variables
408 */
409private:
411 ActionMap _actionMap;
412
413private:
414 void removeCurrentVariable(const Common::String &name);
415 Shared::XMLNode *_vendorScriptDoc;
416 Shared::XMLNode *_scriptNode;
417 bool _debug;
418
419 State _state;
420 Shared::XMLNode *_currentScript;
421 Shared::XMLNode *_currentItem;
422 Common::List<Shared::XMLNode *> _translationContext;
423 Common::String _target;
424 InputType _inputType;
425 Common::String _inputName;
426 int _inputMaxLen;
428 Common::String _nounName;
429 Common::String _idPropName;
432 Common::String _choices;
433 int _iterator;
434
437};
438
439} // End of namespace Ultima4
440} // End of namespace Ultima
441
442#endif
Definition: list.h:44
Definition: str.h:59
Definition: xml_node.h:36
Definition: script.h:55
Definition: script.h:46
State
Definition: script.h:101
bool load(const Common::String &filename, const Common::String &baseId, const Common::String &subNodeName="", const Common::String &subNodeId="")
Action
Definition: script.h:123
ReturnCode
Definition: script.h:92
ReturnCode execute(Shared::XMLNode *script, Shared::XMLNode *currentItem=nullptr, Common::String *output=nullptr)
InputType
Definition: script.h:111
void addProvider(const Common::String &name, Provider *p)
void run(const Common::String &script)