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|max|min ---> f : adds/... two numbers\n");
62 print(" f f eq|ne|gt|ge|lt|le -------------> f : compares two numbers\n");
63 print(" f neg|abs|sgn|rand ----------------> f : negates/... a number\n");
64 print(" f f f bound -----------------------> f : bounds the middle number\n");
65 print(" f1 f2 b when ----------------------> f : f1 if b, f2 otherwise\n");
66 print(" s s union|intersection|difference -> s : set operations\n");
67 print(" s shuffle -------------------------> s : randomly arrange elements\n");
68 print(" Set operations operate on 'such''strings' like g_maplist.\n");
69 print(" Unknown tokens insert their cvar value.\n");
70 print(" maplist add map\n");
71 print(" maplist remove map\n");
72 print(" maplist shuffle\n");
76 if(argv(0) == "maplist")
78 if(argv(1) == "add" && argc == 3)
81 f = fopen(strcat("maps/", argv(2), ".bsp"), FILE_READ);
85 print("maplist: ERROR: ", argv(2), " does not exist!\n");
88 if(cvar_string("g_maplist") == "")
89 cvar_set("g_maplist", argv(2));
91 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
93 f = fopen(strcat("maps/", argv(2), ".mapcfg"), FILE_READ);
97 print("maplist: ERROR: ", argv(2), " does not exist!\n");
100 cvar_set("g_maplist", strcat("'", argv(2), "'", cvar_string("g_maplist")));
104 else if(argv(1) == "remove" && argc == 3)
108 n = tokenizebyseparator(cvar_string("g_maplist"), " ");
110 n = tokenize(cvar_string("g_maplist"));
113 for(i = 0; i < n; ++i)
117 s2 = strcat(s2, " ", argv(i));
119 s2 = strcat(s2, "'", argv(i), "'");
123 s2 = substring(s2, 1, strlen(s2) - 1);
125 cvar_set("g_maplist", s2);
128 else if(argv(1) == "shuffle" && argc == 2)
130 s = cvar_string("g_maplist");
132 for(i = 1; i < (n = tokenizebyseparator(s, " ")); ++i)
134 for(i = 1; i < (n = tokenize(s)); ++i)
137 // swap i-th item at a random position from 0 to i
138 // proof for even distribution:
141 // item n+1 gets at any position with chance 1/(n+1)
142 // all others will get their 1/n chance reduced by factor n/(n+1)
143 // to be on place n+1, their chance will be 1/(n+1)
144 // 1/n * n/(n+1) = 1/(n+1)
146 f = ceil(random() * (i + 1)) - 1; // 0 to i
148 continue; // no change
151 for(j = 0; j < n; ++j)
153 s2 = strcat(s2, " ", argv((j == i) ? f : (j == f) ? i : j));
154 s = substring(s2, 1, strlen(s2) - 1);
156 s2 = strcat(s2, "'", argv((j == i) ? f : (j == f) ? i : j), "'");
160 cvar_set("g_maplist", s);
164 else if(argv(0) == "rpn")
173 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
175 rpncmd = argv(rpnpos);
178 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
180 } else if(substring(rpncmd, 0, 1) == "0") {
182 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
184 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
186 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
187 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
188 } else if(rpncmd == "def" || rpncmd == "=") {
192 registercvar(s2, "", 0);
194 registercvar(s2, "");
196 if(!rpn_error) // don't change cvars if a stack error had happened!
198 } else if(rpncmd == "load") {
199 rpn_set(cvar_string(rpn_get()));
200 } else if(rpncmd == "exch") {
205 } else if(rpncmd == "dup") {
207 } else if(rpncmd == "pop") {
209 } else if(rpncmd == "add" || rpncmd == "+") {
211 rpn_setf(rpn_getf() + f);
212 } else if(rpncmd == "sub" || rpncmd == "-") {
214 rpn_setf(rpn_getf() - f);
215 } else if(rpncmd == "mul" || rpncmd == "*") {
217 rpn_setf(rpn_getf() * f);
218 } else if(rpncmd == "div" || rpncmd == "/") {
220 rpn_setf(rpn_getf() / f);
221 } else if(rpncmd == "mod" || rpncmd == "%") {
224 rpn_setf(f2 - f * floor(f2 / f));
225 } else if(rpncmd == "abs") {
226 rpn_setf(fabs(rpn_getf()));
227 } else if(rpncmd == "sgn") {
235 } else if(rpncmd == "neg" || rpncmd == "~") {
236 rpn_setf(-rpn_getf());
237 } else if(rpncmd == "max") {
240 rpn_setf(max(f2, f));
241 } else if(rpncmd == "min") {
244 rpn_setf(min(f2, f));
245 } else if(rpncmd == "bound") {
249 rpn_setf(bound(f3, f2, f));
250 } else if(rpncmd == "when") {
258 } else if(rpncmd == ">" || rpncmd == "gt") {
260 rpn_setf(rpn_getf() > f);
261 } else if(rpncmd == "<" || rpncmd == "lt") {
263 rpn_setf(rpn_getf() < f);
264 } else if(rpncmd == "==" || rpncmd == "eq") {
266 rpn_setf(rpn_getf() == f);
267 } else if(rpncmd == ">=" || rpncmd == "ge") {
269 rpn_setf(rpn_getf() >= f);
270 } else if(rpncmd == "<=" || rpncmd == "le") {
272 rpn_setf(rpn_getf() <= f);
273 } else if(rpncmd == "!=" || rpncmd == "ne") {
275 rpn_setf(rpn_getf() != f);
276 } else if(rpncmd == "rand") {
277 rpn_setf(ceil(random() * rpn_getf()) - 1);
278 } else if(rpncmd == "union") {
283 f2 = tokenize(strcat(s, s2));
284 // tokens 0..(f-1) represent s
285 // tokens f..f2 represent s2
286 // UNION: add all tokens to s that are in s2 but not in s
288 for(i = 0; i < f; ++i)
289 s = strcat(s, "'", argv(i), "'");
290 for(i = f; i < f2; ++i) {
291 for(j = 0; j < f; ++j)
292 if(argv(i) == argv(j))
294 s = strcat(s, "'", argv(i), "'");
299 } else if(rpncmd == "intersection") {
304 f2 = tokenize(strcat(s, s2));
305 // tokens 0..(f-1) represent s
306 // tokens f..f2 represent s2
307 // INTERSECTION: keep only the tokens from s that are also in s2
309 for(i = 0; i < f; ++i) {
310 for(j = f; j < f2; ++j)
311 if(argv(i) == argv(j))
313 s = strcat(s, "'", argv(i), "'");
319 } else if(rpncmd == "difference") {
324 f2 = tokenize(strcat(s, s2));
325 // tokens 0..(f-1) represent s
326 // tokens f..f2 represent s2
327 // DIFFERENCE: keep only the tokens from s that are not in s2
329 for(i = 0; i < f; ++i) {
330 for(j = f; j < f2; ++j)
331 if(argv(i) == argv(j))
332 goto skip_difference;
333 s = strcat(s, "'", argv(i), "'");
338 } else if(rpncmd == "shuffle") {
343 for(i = 0; i < f - 1; ++i) {
344 // move a random item from i..f-1 to position i
346 f2 = ceil(random() * (f - i) + i) - 1;
347 for(j = 0; j < i; ++j)
348 s = strcat(s, "'", argv(j), "'");
349 s = strcat(s, "'", argv(f2), "'");
350 for(j = i; j < f; ++j)
352 s = strcat(s, "'", argv(j), "'");
358 } else if(rpncmd == "fexists_assert") {
362 f = fopen(s, FILE_READ);
366 print("rpn: ERROR: ", s, " does not exist!\n");
371 rpn_push(cvar_string(rpncmd));
379 print("rpn: still on stack: ", s, "\n");