-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcaosVM_core.cpp
More file actions
368 lines (298 loc) · 8.25 KB
/
caosVM_core.cpp
File metadata and controls
368 lines (298 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* caosVM_core.cpp
* openc2e
*
* Created by Alyssa Milburn on Tue May 25 2004.
* Copyright (c) 2004 Alyssa Milburn. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
#include "caosVM.h"
#include "openc2e.h"
#include "World.h"
#include "Engine.h"
#include <iostream>
#include <boost/format.hpp>
using std::cout;
using std::cerr;
using boost::format;
/**
OUTX (command) val (string)
%status maybe
Prints the given string on the output stream, after first quoting it and transforming
escapes in the string to quoted escapes.
*/
void caosVM::c_OUTX() {
VM_PARAM_STRING(val)
if (!outputstream) return;
std::string oh = "\"";
for (unsigned int i = 0; i < val.size(); i++) {
switch (val[i]) {
case '\r': oh += "\\r"; break;
case '\n': oh += "\\n"; break;
case '\t': oh += "\\t"; break;
case '\\': oh += "\\\\"; break;
case '"': oh += "\\\""; break;
case '\'': oh += "\\'"; break;
default: oh += val[i];
}
}
*outputstream << oh << "\"";
}
/**
OUTS (command) val (string)
%status maybe
%pragma variants all
Prints the given string to the output stream. Does nothing when run inside a script.
*/
void caosVM::c_OUTS() {
VM_VERIFY_SIZE(1)
VM_PARAM_STRING(val)
if (!outputstream) return;
*outputstream << val;
}
/**
DDE: PUTS (command) val (bareword)
%status maybe
%pragma variants c1 c2
%pragma implementation caosVM::c_OUTS
*/
/**
OUTV (command) val (decimal)
%status maybe
%pragma variants all
Prints the given decimal value to the ouput stream. Does nothing when run inside a script.
*/
void caosVM::c_OUTV() {
VM_VERIFY_SIZE(1)
VM_PARAM_VALUE(val)
if (!outputstream) return;
if (val.hasFloat()) {
*outputstream << boost::format("%0.06f") % val.getFloat();
} else if (val.hasInt()) {
*outputstream << val.getInt();
} else if (val.hasVector()) {
const Vector<float> &v = val.getVector();
*outputstream << boost::format("(%0.6f, %0.6f)") % v.x % v.y;
} else throw badParamException();
}
/**
DDE: PUTV (command) val (integer)
%status maybe
%pragma variants c1 c2
%pragma implementation caosVM::c_OUTV
*/
/**
GAME (variable) name (string)
%status maybe
Returns the game variable with the given name.
*/
CAOS_LVALUE(GAME, VM_PARAM_STRING(name),
world.variables[name],
world.variables[name] = newvalue
)
/**
EAME (variable) name (anything)
%status maybe
Returns the non-persistent game variable with the given name.
*/
// XXX: should this be a string argument?
CAOS_LVALUE(EAME, VM_PARAM_VALUE(name),
engine.eame_variables[name],
engine.eame_variables[name] = newvalue
)
/**
DELG (command) name (string)
%status maybe
Deletes the game variable with the given name.
*/
void caosVM::c_DELG() {
VM_PARAM_STRING(name)
std::map<std::string, caosVar>::iterator i = world.variables.find(name);
if (i != world.variables.end())
world.variables.erase(i);
}
/**
SCRP (command) family (integer) genus (integer) species (integer) event (integer)
%status done
%pragma variants c1 c2 cv c3 sm
Marks the beginning of a normal script applying to the agent with the given classifier
info.
*/
void caosVM::c_SCRP() {
// handled elsewhere
}
/**
RSCR (command)
%status done
%pragma variants c2 cv c3 sm
Marks the beginning of a removal script.
*/
void caosVM::c_RSCR() {
// handled elsewhere
}
/**
ISCR (command)
%status stub
%pragma variants c2 cv c3 sm
Marks the beginning of an installer script.
*/
void caosVM::c_ISCR() {
VM_VERIFY_SIZE(0)
// STOP
}
/**
ENDM (command)
%status done
%pragma variants c1 c2 cv c3 sm
Marks the end of a script.
*/
void caosVM::c_ENDM() {
stop();
}
/**
RGAM (command)
%status stub
Restore all game variables to their saved or default values.
*/
void caosVM::c_RGAM() {}
/**
MOWS (integer)
%status stub
Returns whether the lawn was cut last Sunday or not, in theory.
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.
In openc2e, currently a no-op (ie, the lawn is never, ever cut properly).
*/
void caosVM::v_MOWS() {
result.setInt(0); // We're too busy coding to mow the lawn.
}
/**
VMNR (integer)
%status maybe
Returns the minor version number of the engine.
*/
void caosVM::v_VMNR() {
result.setInt(1);
}
/**
VMJR (integer)
%status maybe
Returns the major version number of the engine.
*/
void caosVM::v_VMJR() {
result.setInt(0);
}
/**
VRSN (command) required (integer)
%status maybe
%pragma variants c1 c2
Stop running this script unless VRSN is equal to or greater than the specified value.
*/
void caosVM::c_VRSN() {
VM_PARAM_INTEGER(required)
// TODO: is this good for c1? which version is c2?
int thisversion = (engine.version == 1) ? 2 : 0;
if (thisversion < required) {
std::cout << "Warning: stopping script due to version requirement of " << required << " (we are reporting a version of " << thisversion << ")" << std::endl;
stop();
}
}
/**
VRSN (integer)
%status maybe
%pragma variants c1 c2
Return the build version number of the engine.
*/
void caosVM::v_VRSN() {
// TODO: is this good for c1? which version is c2?
int thisversion = (engine.version == 1) ? 2 : 0;
result.setInt(thisversion);
}
/**
WOLF (integer) andmask (integer) eormask (integer)
%status maybe
This returns/sets some engine settings which are useful for 'wolfing runs', among other things.
Set andmask for the information you want returned, and eormask for the information you want changed.
1 is for display rendering (turn it off to speed up the game)
2 is for running ticks as fast as possible, rather than according to BUZZ
4 is for refreshing the display (when display rendering is turned off, this will update the display at the end of the tick)
(note that 4 is unset by the engine when the display is refreshed)
8 is autokill
*/
void caosVM::v_WOLF() {
VM_PARAM_INTEGER(eormask)
VM_PARAM_INTEGER(andmask)
if (eormask & 1) engine.dorendering = !engine.dorendering;
if (eormask & 2) engine.fastticks = !engine.fastticks;
if (eormask & 4) engine.refreshdisplay = !engine.refreshdisplay;
if (eormask & 8) world.autokill = !world.autokill;
int r = 0;
if (andmask & 1 && engine.dorendering) r += 1;
if (andmask & 2 && engine.fastticks) r += 2;
if (andmask & 4 && engine.refreshdisplay) r += 4;
if (andmask & 8 && world.autokill) r += 8;
result.setInt(r);
}
/**
LANG (string)
%status stub
*/
void caosVM::v_LANG() {
result.setString("en");
}
/**
TOKN (integer) token (bareword)
%status maybe
%pragma variants c1 c2
*/
void caosVM::v_TOKN() {
VM_PARAM_STRING(token)
caos_assert(token.size() == 4);
int *data = (int *)token.c_str();
result.setInt(*data);
}
/**
GAME (variable) category (integer) variable (integer)
%status stub
%pragma variants c2
%pragma implementation caosVM::v_GAME_c2
*/
CAOS_LVALUE(GAME_c2,
VM_PARAM_INTEGER(variable) VM_PARAM_INTEGER(category),
caosVar(),
(void)0) // TODO
#include <boost/filesystem/operations.hpp>
/**
OC2E DDIR (string)
%status maybe
%pragma variants all
Returns a list of the data directories available, separated with \n. Remember that the last one is the working directory.
*/
void caosVM::v_OC2E_DDIR() {
std::string d;
for (std::vector<boost::filesystem::path>::iterator i = world.data_directories.begin(); i != world.data_directories.end(); i++) {
boost::filesystem::path &p = *i;
d = d + boost::filesystem::system_complete(p).string() + "\n";
}
result.setString(d);
}
/**
SYS: CMND (command) menuid (integer)
%status stub
%pragma variants c1 c2
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.
*/
void caosVM::c_SYS_CMND() {
VM_PARAM_INTEGER(menuid)
// TODO
}
/* vim: set noet: */