1 #define MAX_RPN_STACK 8
4 string rpn_stack[MAX_RPN_STACK];
8 return rpn_stack[rpn_sp];
10 print("rpn: stack underflow\n");
15 void rpn_push(string s) {
16 if(rpn_sp < MAX_RPN_STACK) {
17 rpn_stack[rpn_sp] = s;
20 print("rpn: stack overflow\n");
26 return rpn_stack[rpn_sp - 1];
28 print("rpn: empty stack\n");
33 void rpn_set(string s) {
35 rpn_stack[rpn_sp - 1] = s;
37 print("rpn: empty stack\n");
41 float rpn_getf() { return stof(rpn_get()); }
42 float rpn_popf() { return stof(rpn_pop()); }
43 void rpn_pushf(float f) { return rpn_push(ftos(f)); }
44 void rpn_setf(float f) { return rpn_set(ftos(f)); }
46 float GameCommand_Generic(string command)
51 argc = tokenize(command);
54 print(" rpn EXPRESSION... - a RPN calculator.\n");
55 print(" Operator description (x: string, s: set, f: float):\n");
56 print(" x pop -----------------------------> : removes the top\n");
57 print(" x dup -----------------------------> x x : duplicates the top\n");
58 print(" x x exch --------------------------> x x : swap the top two\n");
59 print(" /cvarname load --------------------> x : loads a cvar\n");
60 print(" /cvarname x def -------------------> : writes to a cvar\n");
61 print(" f f add|sub|mul|div|mod -----------> f : adds/... two numbers\n");
62 print(" f neg|abs|sgn|rand ----------------> f : negates/... a number\n");
63 print(" s s union|intersection|difference -> s : set operations\n");
64 print(" s shuffle -------------------------> s : randomly arrange elements\n");
65 print(" Set operations operate on 'such''strings' like g_maplist.\n");
66 print(" Unknown tokens insert their cvar value.\n");
67 print(" maplist add map\n");
68 print(" maplist remove map\n");
69 print(" maplist shuffle\n");
73 if(argv(0) == "maplist")
75 if(argv(1) == "add" && argc == 3)
78 f = fopen(strcat("maps/", argv(2), ".bsp"), FILE_READ);
82 print("maplist: ERROR: ", argv(2), " does not exist!\n");
85 if(cvar_string("g_maplist") == "")
86 cvar_set("g_maplist", argv(2));
88 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
90 f = fopen(strcat("maps/", argv(2), ".mapcfg"), FILE_READ);
94 print("maplist: ERROR: ", argv(2), " does not exist!\n");
97 cvar_set("g_maplist", strcat("'", argv(2), "'", cvar_string("g_maplist")));
101 else if(argv(1) == "remove" && argc == 3)
105 n = tokenizebyseparator(cvar_string("g_maplist"), " ");
107 n = tokenize(cvar_string("g_maplist"));
110 for(i = 0; i < n; ++i)
114 s2 = strcat(s2, " ", argv(i));
116 s2 = strcat(s2, "'", argv(i), "'");
120 s2 = substring(s2, 1, strlen(s2) - 1);
122 cvar_set("g_maplist", s2);
125 else if(argv(1) == "shuffle" && argc == 2)
127 s = cvar_string("g_maplist");
129 for(i = 1; i < (n = tokenizebyseparator(s, " ")); ++i)
131 for(i = 1; i < (n = tokenize(s)); ++i)
134 // swap i-th item at a random position from 0 to i
135 // proof for even distribution:
138 // item n+1 gets at any position with chance 1/(n+1)
139 // all others will get their 1/n chance reduced by factor n/(n+1)
140 // to be on place n+1, their chance will be 1/(n+1)
141 // 1/n * n/(n+1) = 1/(n+1)
143 f = ceil(random() * (i + 1)) - 1; // 0 to i
145 continue; // no change
148 for(j = 0; j < n; ++j)
150 s2 = strcat(s2, " ", argv((j == i) ? f : (j == f) ? i : j));
151 s = substring(s2, 1, strlen(s2) - 1);
153 s2 = strcat(s2, "'", argv((j == i) ? f : (j == f) ? i : j), "'");
157 cvar_set("g_maplist", s);
161 else if(argv(0) == "rpn")
170 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
172 rpncmd = argv(rpnpos);
175 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
177 } else if(substring(rpncmd, 0, 1) == "0") {
179 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
181 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
183 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
184 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
185 } else if(rpncmd == "def" || rpncmd == "=") {
189 registercvar(s2, "", 0);
191 registercvar(s2, "");
193 if(!rpn_error) // don't change cvars if a stack error had happened!
195 } else if(rpncmd == "load") {
196 rpn_set(cvar_string(rpn_get()));
197 } else if(rpncmd == "exch") {
202 } else if(rpncmd == "dup") {
204 } else if(rpncmd == "pop") {
206 } else if(rpncmd == "add" || rpncmd == "+") {
208 rpn_setf(rpn_getf() + f);
209 } else if(rpncmd == "sub" || rpncmd == "-") {
211 rpn_setf(rpn_getf() - f);
212 } else if(rpncmd == "mul" || rpncmd == "*") {
214 rpn_setf(rpn_getf() * f);
215 } else if(rpncmd == "div" || rpncmd == "/") {
217 rpn_setf(rpn_getf() / f);
218 } else if(rpncmd == "mod" || rpncmd == "%") {
221 rpn_setf(f2 - f * floor(f2 / f));
222 } else if(rpncmd == "abs") {
223 rpn_setf(fabs(rpn_getf()));
224 } else if(rpncmd == "sgn") {
232 } else if(rpncmd == "neg" || rpncmd == "~") {
233 rpn_setf(-rpn_getf());
234 } else if(rpncmd == "rand") {
235 rpn_setf(ceil(random() * rpn_getf()) - 1);
236 } else if(rpncmd == "union") {
241 f2 = tokenize(strcat(s, s2));
242 // tokens 0..(f-1) represent s
243 // tokens f..f2 represent s2
244 // UNION: add all tokens to s that are in s2 but not in s
246 for(i = 0; i < f; ++i)
247 s = strcat(s, "'", argv(i), "'");
248 for(i = f; i < f2; ++i) {
249 for(j = 0; j < f; ++j)
250 if(argv(i) == argv(j))
252 s = strcat(s, "'", argv(i), "'");
257 } else if(rpncmd == "intersection") {
262 f2 = tokenize(strcat(s, s2));
263 // tokens 0..(f-1) represent s
264 // tokens f..f2 represent s2
265 // INTERSECTION: keep only the tokens from s that are also in s2
267 for(i = 0; i < f; ++i) {
268 for(j = f; j < f2; ++j)
269 if(argv(i) == argv(j))
271 s = strcat(s, "'", argv(i), "'");
277 } else if(rpncmd == "difference") {
282 f2 = tokenize(strcat(s, s2));
283 // tokens 0..(f-1) represent s
284 // tokens f..f2 represent s2
285 // DIFFERENCE: keep only the tokens from s that are not in s2
287 for(i = 0; i < f; ++i) {
288 for(j = f; j < f2; ++j)
289 if(argv(i) == argv(j))
290 goto skip_difference;
291 s = strcat(s, "'", argv(i), "'");
296 } else if(rpncmd == "shuffle") {
301 for(i = 0; i < f - 1; ++i) {
302 // move a random item from i..f-1 to position i
304 f2 = ceil(random() * (f - i) + i) - 1;
305 for(j = 0; j < i; ++j)
306 s = strcat(s, "'", argv(j), "'");
307 s = strcat(s, "'", argv(f2), "'");
308 for(j = i; j < f; ++j)
310 s = strcat(s, "'", argv(j), "'");
316 } else if(rpncmd == "fexists_assert") {
320 f = fopen(s, FILE_READ);
324 print("rpn: ERROR: ", s, " does not exist!\n");
329 rpn_push(cvar_string(rpncmd));
337 print("rpn: still on stack: ", s, "\n");