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, Notation notation)
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  if (notation == FixedNotation)
62  {
63  out << std::fixed << value;
64  mFormattedMessage += out.str();
65  }
66  else if (notation == ShortestNotation)
67  {
68  out << value;
69  std::string standard = out.str();
70 
71  out.str(std::string());
72  out.clear();
73 
74  out << std::scientific << value;
75  std::string scientific = out.str();
76 
77  mFormattedMessage += standard.length() < scientific.length() ? standard : scientific;
78  }
79  else
80  {
81  out << std::scientific << value;
82  mFormattedMessage += out.str();
83  }
84  }
85  break;
86  default:
87  break;
88  }
89  }
90 
91  virtual void visitedCharacter(char c)
92  {
93  mFormattedMessage += c;
94  }
95 
96  public:
98  : mRuntime(runtime)
99  {
100  }
101 
102  virtual void process(const std::string& message)
103  {
104  mFormattedMessage.clear();
105  MessageFormatParser::process(message);
106  }
107 
108  std::string getFormattedMessage() const
109  {
110  return mFormattedMessage;
111  }
112  };
113 
114  inline std::string formatMessage (const std::string& message, Runtime& runtime)
115  {
116  RuntimeMessageFormatter formatter(runtime);
117  formatter.process(message);
118 
119  std::string formattedMessage = formatter.getFormattedMessage();
120  formattedMessage = fixDefinesMsgBox(formattedMessage, runtime.getContext());
121  return formattedMessage;
122  }
123 
124  class OpMessageBox : public Opcode1
125  {
126  public:
127 
128  virtual void execute (Runtime& runtime, unsigned int arg0)
129  {
130  // message
131  int index = runtime[0].mInteger;
132  runtime.pop();
133  std::string message = runtime.getStringLiteral (index);
134 
135  // buttons
136  std::vector<std::string> buttons;
137 
138  for (std::size_t i=0; i<arg0; ++i)
139  {
140  index = runtime[0].mInteger;
141  runtime.pop();
142  buttons.push_back (runtime.getStringLiteral (index));
143  }
144 
145  std::reverse (buttons.begin(), buttons.end());
146 
147  // handle additional parameters
148  std::string formattedMessage = formatMessage (message, runtime);
149 
150  runtime.getContext().messageBox (formattedMessage, buttons);
151  }
152  };
153 
154  class OpReport : public Opcode0
155  {
156  public:
157 
158  virtual void execute (Runtime& runtime)
159  {
160  // message
161  int index = runtime[0].mInteger;
162  runtime.pop();
163  std::string message = runtime.getStringLiteral (index);
164 
165  // handle additional parameters
166  std::string formattedMessage = formatMessage (message, runtime);
167 
168  runtime.getContext().report (formattedMessage);
169  }
170  };
171 
172  class OpMenuMode : public Opcode0
173  {
174  public:
175 
176  virtual void execute (Runtime& runtime)
177  {
178  runtime.push (runtime.getContext().menuMode());
179  }
180  };
181 
182  class OpRandom : public Opcode0
183  {
184  public:
185 
186  virtual void execute (Runtime& runtime)
187  {
188  Type_Integer limit = runtime[0].mInteger;
189 
190  if (limit<0)
191  throw std::runtime_error (
192  "random: argument out of range (Don't be so negative!)");
193 
194  Type_Integer value = Misc::Rng::rollDice(limit); // [o, limit)
195 
196  runtime[0].mInteger = value;
197  }
198  };
199 
201  {
202  public:
203 
204  virtual void execute (Runtime& runtime)
205  {
206  Type_Float duration = runtime.getContext().getSecondsPassed();
207 
208  runtime.push (duration);
209  }
210  };
211 
212  class OpEnable : public Opcode0
213  {
214  public:
215 
216  virtual void execute (Runtime& runtime)
217  {
218  runtime.getContext().enable();
219  }
220  };
221 
222  class OpDisable : public Opcode0
223  {
224  public:
225 
226  virtual void execute (Runtime& runtime)
227  {
228  runtime.getContext().disable();
229  }
230  };
231 
232  class OpGetDisabled : public Opcode0
233  {
234  public:
235 
236  virtual void execute (Runtime& runtime)
237  {
238  runtime.push (runtime.getContext().isDisabled());
239  }
240  };
241 
242  class OpEnableExplicit : public Opcode0
243  {
244  public:
245 
246  virtual void execute (Runtime& runtime)
247  {
248  int index = runtime[0].mInteger;
249  runtime.pop();
250  std::string id = runtime.getStringLiteral (index);
251 
252  runtime.getContext().enable (id);
253  }
254  };
255 
256  class OpDisableExplicit : public Opcode0
257  {
258  public:
259 
260  virtual void execute (Runtime& runtime)
261  {
262  int index = runtime[0].mInteger;
263  runtime.pop();
264  std::string id = runtime.getStringLiteral (index);
265 
266  runtime.getContext().disable (id);
267  }
268  };
269 
271  {
272  public:
273 
274  virtual void execute (Runtime& runtime)
275  {
276  int index = runtime[0].mInteger;
277  runtime.pop();
278  std::string id = runtime.getStringLiteral (index);
279 
280  runtime.push (runtime.getContext().isDisabled (id));
281  }
282  };
283 
284 }
285 
286 #endif
virtual void enable(const std::string &id="")=0
std::string formatMessage(const std::string &message, Runtime &runtime)
Definition: miscopcodes.hpp:114
void message(CodeContainer &code, Literals &literals, const std::string &message, int buttons)
Definition: generator.cpp:537
std::string getFormattedMessage() const
Definition: miscopcodes.hpp:108
void pop()
pop stack
Definition: runtime.cpp:94
virtual void visitedPlaceholder(Placeholder placeholder, char padding, int width, int precision, Notation notation)
Definition: miscopcodes.hpp:27
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:256
opcode for 0 arguments
Definition: opcodes.hpp:9
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:260
RuntimeMessageFormatter(Runtime &runtime)
Definition: miscopcodes.hpp:97
Definition: miscopcodes.hpp:212
virtual bool isDisabled(const std::string &id="") const =0
Placeholder
Definition: messageformatparser.hpp:11
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:216
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:204
Definition: messageformatparser.hpp:13
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:274
Definition: messageformatparser.hpp:15
Runtime & mRuntime
Definition: miscopcodes.hpp:24
Definition: miscopcodes.hpp:154
static int rollDice(int max)
return value in range [0, max) <- note open upper range.
Definition: rng.cpp:26
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:226
Definition: miscopcodes.hpp:182
Definition: miscopcodes.hpp:270
Definition: miscopcodes.hpp:200
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:246
virtual void messageBox(const std::string &message, const std::vector< std::string > &buttons)=0
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:158
Definition: messageformatparser.hpp:8
Definition: messageformatparser.hpp:14
virtual void execute(Runtime &runtime, unsigned int arg0)
Definition: miscopcodes.hpp:128
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:186
Context & getContext()
Definition: runtime.cpp:110
Definition: miscopcodes.hpp:172
virtual float getSecondsPassed() const =0
Definition: messageformatparser.hpp:22
virtual void process(const std::string &message)
Definition: miscopcodes.hpp:102
Notation
Definition: messageformatparser.hpp:18
float Type_Float
Definition: types.hpp:16
std::string fixDefinesMsgBox(const std::string &text, Context &context)
Definition: defines.cpp:215
Definition: miscopcodes.hpp:222
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:176
opcode for 1 argument
Definition: opcodes.hpp:19
std::string mFormattedMessage
Definition: miscopcodes.hpp:23
virtual void execute(Runtime &runtime)
Definition: miscopcodes.hpp:236
Definition: miscopcodes.hpp:232
virtual void disable(const std::string &id="")=0
Definition: miscopcodes.hpp:242
Definition: miscopcodes.hpp:20
Definition: messageformatparser.hpp:20
virtual bool menuMode()=0
virtual void report(const std::string &message)=0
virtual void visitedCharacter(char c)
Definition: miscopcodes.hpp:91
Definition: miscopcodes.hpp:124