allow any CompoundPart to gain focus, if it canGainFocus()
[openc2e.git] / caosVM_core.cpp
blob3aa46294ff21874c749956527631e082d44e7ec5
1 /*
2 * caosVM_core.cpp
3 * openc2e
5 * Created by Alyssa Milburn on Tue May 25 2004.
6 * Copyright (c) 2004 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #include "caosVM.h"
21 #include "openc2e.h"
22 #include "World.h"
23 #include "Engine.h"
24 #include <iostream>
26 #include <boost/format.hpp>
28 using std::cout;
29 using std::cerr;
31 using boost::format;
33 /**
34 OUTX (command) val (string)
35 %status maybe
37 Prints the given string on the output stream, after first quoting it and transforming
38 escapes in the string to quoted escapes.
40 void caosVM::c_OUTX() {
41 VM_PARAM_STRING(val)
43 if (!outputstream) return;
45 std::string oh = "\"";
47 for (unsigned int i = 0; i < val.size(); i++) {
48 switch (val[i]) {
49 case '\r': oh += "\\r"; break;
50 case '\n': oh += "\\n"; break;
51 case '\t': oh += "\\t"; break;
52 case '\\': oh += "\\\\"; break;
53 case '"': oh += "\\\""; break;
54 case '\'': oh += "\\'"; break;
55 default: oh += val[i];
59 *outputstream << oh << "\"";
62 /**
63 OUTS (command) val (string)
64 %status maybe
65 %pragma variants all
67 Prints the given string to the output stream. Does nothing when run inside a script.
69 void caosVM::c_OUTS() {
70 VM_VERIFY_SIZE(1)
71 VM_PARAM_STRING(val)
73 if (!outputstream) return;
75 *outputstream << val;
78 /**
79 DDE: PUTS (command) val (bareword)
80 %status maybe
81 %pragma variants c1 c2
82 %pragma implementation caosVM::c_OUTS
85 /**
86 OUTV (command) val (decimal)
87 %status maybe
88 %pragma variants all
90 Prints the given decimal value to the ouput stream. Does nothing when run inside a script.
92 void caosVM::c_OUTV() {
93 VM_VERIFY_SIZE(1)
94 VM_PARAM_VALUE(val)
96 if (!outputstream) return;
98 if (val.hasFloat()) {
99 *outputstream << boost::format("%0.06f") % val.getFloat();
100 } else if (val.hasInt()) {
101 *outputstream << val.getInt();
102 } else if (val.hasVector()) {
103 const Vector<float> &v = val.getVector();
104 *outputstream << boost::format("(%0.6f, %0.6f)") % v.x % v.y;
105 } else throw badParamException();
109 DDE: PUTV (command) val (integer)
110 %status maybe
111 %pragma variants c1 c2
112 %pragma implementation caosVM::c_OUTV
116 GAME (variable) name (string)
117 %status maybe
119 Returns the game variable with the given name.
121 CAOS_LVALUE(GAME, VM_PARAM_STRING(name),
122 world.variables[name],
123 world.variables[name] = newvalue
127 EAME (variable) name (anything)
128 %status maybe
130 Returns the non-persistent game variable with the given name.
132 // XXX: should this be a string argument?
133 CAOS_LVALUE(EAME, VM_PARAM_VALUE(name),
134 engine.eame_variables[name],
135 engine.eame_variables[name] = newvalue
139 DELG (command) name (string)
140 %status maybe
142 Deletes the game variable with the given name.
144 void caosVM::c_DELG() {
145 VM_PARAM_STRING(name)
147 std::map<std::string, caosVar>::iterator i = world.variables.find(name);
148 if (i != world.variables.end())
149 world.variables.erase(i);
153 SCRP (command) family (integer) genus (integer) species (integer) event (integer)
154 %status done
155 %pragma variants c1 c2 cv c3 sm
157 Marks the beginning of a normal script applying to the agent with the given classifier
158 info.
160 void caosVM::c_SCRP() {
161 // handled elsewhere
165 RSCR (command)
166 %status done
167 %pragma variants c2 cv c3 sm
169 Marks the beginning of a removal script.
171 void caosVM::c_RSCR() {
172 // handled elsewhere
176 ISCR (command)
177 %status stub
178 %pragma variants c2 cv c3 sm
180 Marks the beginning of an installer script.
182 void caosVM::c_ISCR() {
183 VM_VERIFY_SIZE(0)
184 // STOP
188 ENDM (command)
189 %status done
190 %pragma variants c1 c2 cv c3 sm
192 Marks the end of a script.
194 void caosVM::c_ENDM() {
195 stop();
199 RGAM (command)
200 %status stub
202 Restore all game variables to their saved or default values.
204 void caosVM::c_RGAM() {}
207 MOWS (integer)
208 %status stub
210 Returns whether the lawn was cut last Sunday or not, in theory.
211 How the C2E engine determines this, and whose lawn, exactly, and whether or not it takes into account the fact that the lawn may have been mown on Saturday or Friday, and whether it will cut you any slack if it's winter and the grass isn't growing much, is currently unknown.
213 In openc2e, currently a no-op (ie, the lawn is never, ever cut properly).
215 void caosVM::v_MOWS() {
216 result.setInt(0); // We're too busy coding to mow the lawn.
220 VMNR (integer)
221 %status maybe
223 Returns the minor version number of the engine.
225 void caosVM::v_VMNR() {
226 result.setInt(1);
230 VMJR (integer)
231 %status maybe
233 Returns the major version number of the engine.
235 void caosVM::v_VMJR() {
236 result.setInt(0);
240 VRSN (command) required (integer)
241 %status maybe
242 %pragma variants c1 c2
244 Stop running this script unless VRSN is equal to or greater than the specified value.
246 void caosVM::c_VRSN() {
247 VM_PARAM_INTEGER(required)
249 // TODO: is this good for c1? which version is c2?
250 int thisversion = (engine.version == 1) ? 2 : 0;
252 if (thisversion < required) {
253 std::cout << "Warning: stopping script due to version requirement of " << required << " (we are reporting a version of " << thisversion << ")" << std::endl;
254 stop();
259 VRSN (integer)
260 %status maybe
261 %pragma variants c1 c2
263 Return the build version number of the engine.
265 void caosVM::v_VRSN() {
266 // TODO: is this good for c1? which version is c2?
267 int thisversion = (engine.version == 1) ? 2 : 0;
269 result.setInt(thisversion);
273 WOLF (integer) andmask (integer) eormask (integer)
274 %status maybe
276 This returns/sets some engine settings which are useful for 'wolfing runs', among other things.
277 Set andmask for the information you want returned, and eormask for the information you want changed.
279 1 is for display rendering (turn it off to speed up the game)
280 2 is for running ticks as fast as possible, rather than according to BUZZ
281 4 is for refreshing the display (when display rendering is turned off, this will update the display at the end of the tick)
282 (note that 4 is unset by the engine when the display is refreshed)
283 8 is autokill
285 void caosVM::v_WOLF() {
286 VM_PARAM_INTEGER(eormask)
287 VM_PARAM_INTEGER(andmask)
289 if (eormask & 1) engine.dorendering = !engine.dorendering;
290 if (eormask & 2) engine.fastticks = !engine.fastticks;
291 if (eormask & 4) engine.refreshdisplay = !engine.refreshdisplay;
292 if (eormask & 8) world.autokill = !world.autokill;
294 int r = 0;
295 if (andmask & 1 && engine.dorendering) r += 1;
296 if (andmask & 2 && engine.fastticks) r += 2;
297 if (andmask & 4 && engine.refreshdisplay) r += 4;
298 if (andmask & 8 && world.autokill) r += 8;
299 result.setInt(r);
303 LANG (string)
304 %status stub
306 void caosVM::v_LANG() {
307 result.setString("en");
311 TOKN (integer) token (bareword)
312 %status maybe
313 %pragma variants c1 c2
315 void caosVM::v_TOKN() {
316 VM_PARAM_STRING(token)
318 caos_assert(token.size() == 4);
320 int *data = (int *)token.c_str();
321 result.setInt(*data);
325 GAME (variable) category (integer) variable (integer)
326 %status stub
327 %pragma variants c2
328 %pragma implementation caosVM::v_GAME_c2
330 CAOS_LVALUE(GAME_c2,
331 VM_PARAM_INTEGER(variable) VM_PARAM_INTEGER(category),
332 caosVar(),
333 (void)0) // TODO
335 #include <boost/filesystem/operations.hpp>
338 OC2E DDIR (string)
339 %status maybe
340 %pragma variants all
342 Returns a list of the data directories available, separated with \n. Remember that the last one is the working directory.
344 void caosVM::v_OC2E_DDIR() {
345 std::string d;
347 for (std::vector<boost::filesystem::path>::iterator i = world.data_directories.begin(); i != world.data_directories.end(); i++) {
348 boost::filesystem::path &p = *i;
349 d = d + boost::filesystem::system_complete(p).native_file_string() + "\n";
352 result.setString(d);
356 SYS: CMND (command) menuid (integer)
357 %status stub
358 %pragma variants c1 c2
360 Do something by providing a menu ID from the original Creatures 1 or Creatures 2 engines. This is obviously limited to the IDs that openc2e is aware of.
362 void caosVM::c_SYS_CMND() {
363 VM_PARAM_INTEGER(menuid)
365 // TODO
368 /* vim: set noet: */