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 "ultima/shared/std/containers.h"
28 #include "common/file.h"
29 #include "common/list.h"
30 
31 namespace Ultima {
32 namespace Ultima4 {
33 
47 class Script {
48 public:
56  class Provider {
57  public:
58  virtual ~Provider() {}
59  virtual Common::String translate(Std::vector<Common::String> &parts) = 0;
60  };
61 
62 private:
66  class Variable {
67  public:
68  Variable();
69  Variable(const Common::String &v);
70  Variable(const int &v);
71 
72  int &getInt();
73  Common::String &getString();
74 
75  void setValue(const int &v);
76  void setValue(const Common::String &v);
77  void unset();
78 
79  bool isInt() const;
80  bool isString() const;
81  bool isSet() const;
82 
83  private:
84  int _iVal;
85  Common::String _sVal;
86  bool _set;
87  };
88 
89 public:
93  enum ReturnCode {
94  RET_OK,
95  RET_REDIRECTED,
96  RET_STOP
97  };
98 
102  enum State {
103  STATE_UNLOADED,
104  STATE_NORMAL,
105  STATE_DONE,
106  STATE_INPUT
107  };
108 
112  enum InputType {
113  INPUT_CHOICE,
114  INPUT_NUMBER,
115  INPUT_STRING,
116  INPUT_DIRECTION,
117  INPUT_PLAYER,
118  INPUT_KEYPRESS
119  };
120 
124  enum Action {
125  ACTION_SET_CONTEXT,
126  ACTION_UNSET_CONTEXT,
127  ACTION_END,
128  ACTION_REDIRECT,
129  ACTION_WAIT_FOR_KEY,
130  ACTION_WAIT,
131  ACTION_STOP,
132  ACTION_INCLUDE,
133  ACTION_FOR_LOOP,
134  ACTION_RANDOM,
135  ACTION_MOVE,
136  ACTION_SLEEP,
137  ACTION_CURSOR,
138  ACTION_PAY,
139  ACTION_IF,
140  ACTION_INPUT,
141  ACTION_ADD,
142  ACTION_LOSE,
143  ACTION_HEAL,
144  ACTION_CAST_SPELL,
145  ACTION_DAMAGE,
146  ACTION_KARMA,
147  ACTION_MUSIC,
148  ACTION_SET_VARIABLE,
149  ACTION_ZTATS
150  };
151 
155  Script();
156  ~Script();
157 
161  void addProvider(const Common::String &name, Provider *p);
162 
166  bool load(const Common::String &filename, const Common::String &baseId, const Common::String &subNodeName = "", const Common::String &subNodeId = "");
167 
171  void unload();
172 
176  void run(const Common::String &script);
177 
181  ReturnCode execute(Shared::XMLNode *script, Shared::XMLNode *currentItem = nullptr, Common::String *output = nullptr);
182 
186  void _continue();
187 
191  void resetState();
192  void setState(State state);
193  State getState();
194 
195  void setTarget(const Common::String &val);
196  void setChoices(const Common::String &val);
197  void setVar(const Common::String &name, const Common::String &val);
198  void setVar(const Common::String &name, int val);
199  void unsetVar(const Common::String &name);
200 
201  Common::String getTarget();
202  InputType getInputType();
203  Common::String getInputName();
204  Common::String getChoices();
205  int getInputMaxLen();
206 
207 private:
211  void translate(Common::String *script);
212 
216  Shared::XMLNode *find(Shared::XMLNode *node, const Common::String &script, const Common::String &choice = "", bool _default = false);
217 
222  Common::String getPropAsStr(Common::List<Shared::XMLNode *> &nodes, const Common::String &prop, bool recursive);
223 
224  Common::String getPropAsStr(Shared::XMLNode *node, const Common::String &prop, bool recursive = false);
225 
229  int getPropAsInt(Common::List<Shared::XMLNode *> &nodes, const Common::String &prop, bool recursive);
230 
231  int getPropAsInt(Shared::XMLNode *node, const Common::String &prop, bool recursive = false);
232 
236  Common::String getContent(Shared::XMLNode *node);
237 
238  /*
239  * Action Functions
240  */
244  ReturnCode pushContext(Shared::XMLNode *script, Shared::XMLNode *current);
245 
249  ReturnCode popContext(Shared::XMLNode *script, Shared::XMLNode *current);
250 
254  ReturnCode end(Shared::XMLNode *script, Shared::XMLNode *current);
255 
259  ReturnCode waitForKeypress(Shared::XMLNode *script, Shared::XMLNode *current);
260 
264  ReturnCode redirect(Shared::XMLNode *script, Shared::XMLNode *current);
265 
269  ReturnCode include(Shared::XMLNode *script, Shared::XMLNode *current);
270 
274  ReturnCode wait(Shared::XMLNode *script, Shared::XMLNode *current);
275 
279  ReturnCode forLoop(Shared::XMLNode *script, Shared::XMLNode *current);
280 
284  ReturnCode randomScript(Shared::XMLNode *script, Shared::XMLNode *current);
285 
289  ReturnCode move(Shared::XMLNode *script, Shared::XMLNode *current);
290 
294  ReturnCode sleep(Shared::XMLNode *script, Shared::XMLNode *current);
295 
299  ReturnCode cursor(Shared::XMLNode *script, Shared::XMLNode *current);
300 
304  ReturnCode pay(Shared::XMLNode *script, Shared::XMLNode *current);
305 
309  ReturnCode _if(Shared::XMLNode *script, Shared::XMLNode *current);
310 
314  ReturnCode input(Shared::XMLNode *script, Shared::XMLNode *current);
315 
319  ReturnCode add(Shared::XMLNode *script, Shared::XMLNode *current);
320 
324  ReturnCode lose(Shared::XMLNode *script, Shared::XMLNode *current);
325 
329  ReturnCode heal(Shared::XMLNode *script, Shared::XMLNode *current);
330 
334  ReturnCode castSpell(Shared::XMLNode *script, Shared::XMLNode *current);
335 
339  ReturnCode damage(Shared::XMLNode *script, Shared::XMLNode *current);
340 
344  ReturnCode karma(Shared::XMLNode *script, Shared::XMLNode *current);
345 
349  ReturnCode music(Shared::XMLNode *script, Shared::XMLNode *current);
350 
354  ReturnCode setVar(Shared::XMLNode *script, Shared::XMLNode *current);
355 
359  ReturnCode ztats(Shared::XMLNode *script, Shared::XMLNode *current);
360 
361  /*
362  * Math and comparison functions
363  */
364 
371  void mathParseChildren(Shared::XMLNode *math, Common::String *result);
372 
376  int mathValue(const Common::String &str);
377 
381  int math(int lval, int rval, Common::String &op);
382 
388  bool mathParse(const Common::String &str, int *lval, int *rval, Common::String *op);
389 
394  void parseOperation(const Common::String &str, Common::String *lval, Common::String *rval, Common::String *op);
395 
400  bool compare(const Common::String &str);
401 
405  void funcParse(const Common::String &str, Common::String *funcName, Common::String *contents);
406 
407  /*
408  * Static variables
409  */
410 private:
412  ActionMap _actionMap;
413 
414 private:
415  void removeCurrentVariable(const Common::String &name);
416  Shared::XMLNode *_vendorScriptDoc;
417  Shared::XMLNode *_scriptNode;
418  bool _debug;
419 
420  State _state;
421  Shared::XMLNode *_currentScript;
422  Shared::XMLNode *_currentItem;
423  Common::List<Shared::XMLNode *> _translationContext;
424  Common::String _target;
425  InputType _inputType;
426  Common::String _inputName;
427  int _inputMaxLen;
429  Common::String _nounName;
430  Common::String _idPropName;
433  Common::String _choices;
434  int _iterator;
435 
438 };
439 
440 } // End of namespace Ultima4
441 } // End of namespace Ultima
442 
443 #endif
ReturnCode
Definition: script.h:93
Definition: str.h:59
Definition: script.h:47
Definition: list.h:44
Definition: xml_node.h:36
Definition: script.h:56
Definition: detection.h:27
bool load(const Common::String &filename, const Common::String &baseId, const Common::String &subNodeName="", const Common::String &subNodeId="")
Action
Definition: script.h:124
State
Definition: script.h:102
void run(const Common::String &script)
ReturnCode execute(Shared::XMLNode *script, Shared::XMLNode *currentItem=nullptr, Common::String *output=nullptr)
InputType
Definition: script.h:112
void addProvider(const Common::String &name, Provider *p)