OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
miscopcodes.hpp
Go to the documentation of this file.
1 #ifndef INTERPRETER_MISCOPCODES_H_INCLUDED
2 #define INTERPRETER_MISCOPCODES_H_INCLUDED
3 
4 #include <cstdlib>
5 #include <stdexcept>
6 #include <vector>
7 #include <string>
8 #include <sstream>
9 #include <algorithm>
10 
11 #include "opcodes.hpp"
12 #include "runtime.hpp"
13 #include "defines.hpp"
14 
15 #include <components/misc/rng.hpp>
17 
18 namespace Interpreter
19 {
21  {
22  private:
23  std::string mFormattedMessage;
25 
26  protected:
27  virtual void visitedPlaceholder(Placeholder placeholder, char padding, int width, int precision)
28  {
29  std::ostringstream out;
30  out.fill(padding);
31  if (width != -1)
32  out.width(width);
33  if (precision != -1)
34  out.precision(precision);
35 
36  switch (placeholder)
37  {
38  case StringPlaceholder:
39  {
40  int index = mRuntime[0].mInteger;
41  mRuntime.pop();
42 
43  out << mRuntime.getStringLiteral(index);
44  mFormattedMessage += out.str();
45  }
46  break;
47  case IntegerPlaceholder:
48  {
49  Type_Integer value = mRuntime[0].mInteger;
50  mRuntime.pop();
51 
52  out << value;
53  mFormattedMessage += out.str();
54  }
55  break;
56  case FloatPlaceholder:
57  {
58  float value = mRuntime[0].mFloat;
59  mRuntime.pop();
60 
61  out << std::fixed << value;
62  mFormattedMessage += out.str();
63  }
64  break;
65  default:
66  break;
67  }
68  }
69 
70  virtual void visitedCharacter(char c)
71  {
72  mFormattedMessage += c;
73  }
74 
75  public:
77  : mRuntime(runtime)
78  {
79  }
80 
81  virtual void process(const std::string& message)
82  {
83  mFormattedMessage.clear();
84  MessageFormatParser::process(message);
85  }
86 
87  std::string getFormattedMessage() const
88  {
89  return mFormattedMessage;
90  }
91  };
92 
93  inline std::string formatMessage (const std::string& message, Runtime& runtime)
94  {
95  RuntimeMessageFormatter formatter(runtime);
96  formatter.process(message);
97 
98  std::string formattedMessage = formatter.getFormattedMessage();
99  formattedMessage = fixDefinesMsgBox(formattedMessage, runtime.getContext());
100  return formattedMessage;
101  }
102 
103  class OpMessageBox : public Opcode1
104  {
105  public:
106 
107  virtual void execute (Runtime& runtime, unsigned int arg0)
108  {
109  // message
110  int index = runtime[0].mInteger;
111  runtime.pop();
112  std::string message = runtime.getStringLiteral (index);
113 
114  // buttons
115  std::vector<std::string> buttons;
116 
117  for (std::size_t i=0; i<arg0; ++i)
118  {
119  index = runtime[0].mInteger;
120  runtime.pop();
121  buttons.push_back (runtime.getStringLiteral (index));
122  }
123 
124  std::reverse (buttons.begin(), buttons.end());
125 
126  // handle additional parameters
127  std::string formattedMessage = formatMessage (message, runtime);
128 
129  runtime.getContext().messageBox (formattedMessage, buttons);
130  }
131  };
132 
133  class OpReport : public Opcode0
134  {
135  public:
136 
137  virtual void execute (Runtime& runtime)
138  {
139  // message
140  int index = runtime[0].mInteger;
141  runtime.pop();
142  std::string message = runtime.getStringLiteral (index);
143 
144  // handle additional parameters
145  std::string formattedMessage = formatMessage (message, runtime);
146 
147  runtime.getContext().report (formattedMessage);
148  }
149  };
150 
151  class OpMenuMode : public Opcode0
152  {
153  public:
154 
155  virtual void execute (Runtime& runtime)
156  {
157  runtime.push (runtime.getContext().menuMode());
158  }
159  };
160 
161  class OpRandom : public Opcode0
162  {
163  public:
164 
165  virtual void execute (Runtime& runtime)
166  {
167  Type_Integer limit = runtime[0].mInteger;
168 
169  if (limit<0)
170  throw std::runtime_error (
171  "random: argument out of range (Don't be so negative!)");
172 
173  Type_Integer value = Misc::Rng::rollDice(limit); // [o, limit)
174 
175  runtime[0].mInteger = value;
176  }
177  };
178 
180  {
181  public:
182 
183  virtual void execute (Runtime& runtime)
184  {
185  Type_Float duration = runtime.getContext().getSecondsPassed();
186 
187  runtime.push (duration);
188  }
189  };
190 
191  class OpEnable : public Opcode0
192  {
193  public:
194 
195  virtual void execute (Runtime& runtime)
196  {
197  runtime.getContext().enable();
198  }
199  };
200 
201  class OpDisable : public Opcode0
202  {
203  public:
204 
205  virtual void execute (Runtime& runtime)
206  {
207  runtime.getContext().disable();
208  }
209  };
210 
211  class OpGetDisabled : public Opcode0
212  {
213  public:
214 
215  virtual void execute (Runtime& runtime)
216  {
217  runtime.push (runtime.getContext().isDisabled());
218  }
219  };
220 
221  class OpEnableExplicit : public Opcode0
222  {
223  public:
224 
225  virtual void execute (Runtime& runtime)
226  {
227  int index = runtime[0].mInteger;
228  runtime.pop();
229  std::string id = runtime.getStringLiteral (index);
230 
231  runtime.getContext().enable (id);
232  }
233  };
234 
235  class OpDisableExplicit : public Opcode0
236  {
237  public:
238 
239  virtual void execute (Runtime& runtime)
240  {
241  int index = runtime[0].mInteger;
242  runtime.pop();
243  std::string id = runtime.getStringLiteral (index);
244 
245  runtime.getContext().disable (id);
246  }
247  };
248 
250  {
251  public:
252 
253  virtual void execute (Runtime& runtime)
254  {
255  int index = runtime[0].mInteger;
256  runtime.pop();
257  std::string id = runtime.getStringLiteral (index);
258 
259  runtime.push (runtime.getContext().isDisabled (id));
260  }
261  };
262 
263 }
264 
265 #endif
virtual void enable(const std::string &id="")=0
std::string formatMessage(const std::string &message, Runtime &runtime)
Definition: miscopcodes.hpp:93
void message(CodeContainer &code, Literals &literals, const std::string &message, int buttons)
Definition: generator.cpp:537
std::string getFormattedMessage() const
Definition: miscopcodes.hpp:87
void pop()
pop stack
Definition: runtime.cpp:94
void push(const Data &data)
push data on stack
Definition: runtime.cpp:75
std::string getStringLiteral(int index) const
Definition: runtime.cpp:34
Runtime data and engine interface.
Definition: runtime.hpp:15
Definition: miscopcodes.hpp:235
opcode for 0 arguments
Definition: opcodes.hpp:9
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:239
RuntimeMessageFormatter(Runtime &runtime)
Definition: miscopcodes.hpp:76
Definition: miscopcodes.hpp:191
virtual bool isDisabled(const std::string &id="") const =0
Placeholder
Definition: messageformatparser.hpp:11
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:195
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:183
Definition: messageformatparser.hpp:13
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:253
Definition: messageformatparser.hpp:15
Runtime & mRuntime
Definition: miscopcodes.hpp:24
Definition: miscopcodes.hpp:133
static int rollDice(int max)
return value in range [0, max) <- note open upper range.
Definition: rng.cpp:23
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:205
Definition: miscopcodes.hpp:161
Definition: miscopcodes.hpp:249
Definition: miscopcodes.hpp:179
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:225
virtual void messageBox(const std::string &message, const std::vector< std::string > &buttons)=0
virtual void visitedPlaceholder(Placeholder placeholder, char padding, int width, int precision)
Definition: miscopcodes.hpp:27
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:137
Definition: messageformatparser.hpp:8
Definition: messageformatparser.hpp:14
virtual void execute(Runtime &runtime, unsigned int arg0)
Definition: miscopcodes.hpp:107
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:165
Context & getContext()
Definition: runtime.cpp:110
Definition: miscopcodes.hpp:151
virtual float getSecondsPassed() const =0
virtual void process(const std::string &message)
Definition: miscopcodes.hpp:81
float Type_Float
Definition: types.hpp:16
std::string fixDefinesMsgBox(const std::string &text, Context &context)
Definition: defines.cpp:215
Definition: miscopcodes.hpp:201
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:155
opcode for 1 argument
Definition: opcodes.hpp:19
std::string mFormattedMessage
Definition: miscopcodes.hpp:23
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:215
Definition: miscopcodes.hpp:211
virtual void disable(const std::string &id="")=0
Definition: miscopcodes.hpp:221
Definition: miscopcodes.hpp:20
virtual bool menuMode()=0
virtual void report(const std::string &message)=0
virtual void visitedCharacter(char c)
Definition: miscopcodes.hpp:70
Definition: miscopcodes.hpp:103