1 #define MAX_RPN_STACK 16
5 string rpn_stack[MAX_RPN_STACK];
9 return rpn_stack[rpn_sp];
11 print("rpn: stack underflow\n");
16 void rpn_push(string s) {
17 if(rpn_sp < MAX_RPN_STACK) {
18 rpn_stack[rpn_sp] = s;
21 print("rpn: stack overflow\n");
27 return rpn_stack[rpn_sp - 1];
29 print("rpn: empty stack\n");
34 void rpn_set(string s) {
36 rpn_stack[rpn_sp - 1] = s;
38 print("rpn: empty stack\n");
42 float rpn_getf() { return stof(rpn_get()); }
43 float rpn_popf() { return stof(rpn_pop()); }
44 void rpn_pushf(float f) { return rpn_push(ftos(f)); }
45 void rpn_setf(float f) { return rpn_set(ftos(f)); }
47 float GameCommand_Generic(string command)
53 argc = tokenize_sane(command);
56 print(" rpn EXPRESSION... - a RPN calculator.\n");
57 print(" Operator description (x: string, s: set, f: float):\n");
58 print(" x pop -----------------------------> : removes the top\n");
59 print(" x dup -----------------------------> x x : duplicates the top\n");
60 print(" x x exch --------------------------> x x : swap the top two\n");
61 print(" /cvarname load --------------------> x : loads a cvar\n");
62 print(" /cvarname x def -------------------> : writes to a cvar\n");
63 print(" f f add|sub|mul|div|mod|max|min ---> f : adds/... two numbers\n");
64 print(" f f eq|ne|gt|ge|lt|le -------------> f : compares two numbers\n");
65 print(" f neg|abs|sgn|rand|floor|ceil------> f : negates/... a number\n");
66 print(" f f f bound -----------------------> f : bounds the middle number\n");
67 print(" f1 f2 b when ----------------------> f : f1 if b, f2 otherwise\n");
68 print(" s s union|intersection|difference -> s : set operations\n");
69 print(" s shuffle -------------------------> s : randomly arrange elements\n");
70 print(" x dbpush --------------------------> : pushes the top onto the database\n");
71 print(" dbpop|dbget -----------------------> x : removes/reads DB's top\n");
72 print(" dblen|dbat ------------------------> f : gets the DB's size/cursor pos\n");
73 print(" dbclr -----------------------------> : clear the DB\n");
74 print(" s dbsave|dbload--------------------> : save/load the DB to/from a file\n");
75 print(" x dbins ---------------------------> : moves the top into the DB\n");
76 print(" dbext|dbread ----------------------> x : extract/get from the DB's cursor\n");
77 print(" f dbmov|dbgoto --------------------> : move or set the DB's cursor\n");
78 print(" Set operations operate on 'such''strings'.\n");
79 print(" Unknown tokens insert their cvar value.\n");
80 print(" maplist add map\n");
81 print(" maplist remove map\n");
82 print(" maplist shuffle\n");
86 if(argv(0) == "maplist")
88 if(argv(1) == "add" && argc == 3)
90 f = fopen(strcat("maps/", argv(2), ".bsp"), FILE_READ);
94 print("maplist: ERROR: ", argv(2), " does not exist!\n");
97 if(cvar_string("g_maplist") == "")
98 cvar_set("g_maplist", argv(2));
100 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
103 else if(argv(1) == "remove" && argc == 3)
106 n = tokenizebyseparator(cvar_string("g_maplist"), " ");
108 for(i = 0; i < n; ++i)
111 s2 = strcat(s2, " ", argv(i));
113 s2 = substring(s2, 1, strlen(s2) - 1);
114 cvar_set("g_maplist", s2);
117 else if(argv(1) == "shuffle" && argc == 2)
119 s = cvar_string("g_maplist");
120 for(i = 1; i < (n = tokenizebyseparator(s, " ")); ++i)
122 // swap i-th item at a random position from 0 to i
123 // proof for even distribution:
126 // item n+1 gets at any position with chance 1/(n+1)
127 // all others will get their 1/n chance reduced by factor n/(n+1)
128 // to be on place n+1, their chance will be 1/(n+1)
129 // 1/n * n/(n+1) = 1/(n+1)
131 f = floor(random() * (i + 1)); // 0 to i
133 continue; // no change
136 for(j = 0; j < n; ++j)
137 s2 = strcat(s2, " ", argv((j == i) ? f : (j == f) ? i : j));
138 s = substring(s2, 1, strlen(s2) - 1);
140 cvar_set("g_maplist", s);
143 else if(argv(1) == "cleanup")
146 if(cvar("g_maplist_allow_hidden"))
149 i = MAPINFO_FLAG_HIDDEN;
150 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0, i, 0);
151 n = tokenizebyseparator(cvar_string("g_maplist"), " ");
153 for(i = 0; i < n; ++i)
154 if(MapInfo_CheckMap(argv(i)))
155 s2 = strcat(s2, " ", argv(i));
156 s2 = substring(s2, 1, strlen(s2) - 1);
157 cvar_set("g_maplist", s2);
161 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
164 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
166 n = floor(random() * 6 + 2);
169 for(i = 0; i < n; ++i)
171 s2 = strcat(s2, "AH");
175 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
181 s = strcat(s, " ", s2);
183 s = strcat(s2, " ", s);
185 s2 = substring(s, strlen(s) - 2, 2);
186 if(s2 == "AH" || s2 == "AY")
189 s = strcat(s, " ))");
192 s = substring(s, 0, strlen(s) - 1);
195 s = strconv(1, 0, 0, s);
197 localcmd(strcat(argv(1), " ", s));
201 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55970)
203 // test case for terrencehill's color codes
204 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
208 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
211 for(i = 0; i < n; ++i)
213 c = substring(s, i, 1);
220 if(substring(s, i+1, 1) == "^")
226 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
227 c = strcat(rgb_to_hexcolor(rgb), c);
232 localcmd(strcat(argv(1), " ", s2));
236 else if(argv(0) == "rpn")
240 rpn_db = db_create();
241 db_put(rpn_db, "stack.pointer", "0");
242 db_put(rpn_db, "stack.pos", "-1");
251 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
253 rpncmd = argv(rpnpos);
256 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
258 } else if(substring(rpncmd, 0, 1) == "0") {
260 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
262 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
264 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
265 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
266 } else if(rpncmd == "clear") {
268 } else if(rpncmd == "def" || rpncmd == "=") {
272 registercvar(s2, "", 0);
274 registercvar(s2, "");
276 if(!rpn_error) // don't change cvars if a stack error had happened!
278 } else if(rpncmd == "defs" || rpncmd == "@") {
282 while(rpn_sp > 1 && (j || i > 0))
284 s = strcat("/", rpn_pop(), " ", s);
289 registercvar(s2, "", 0);
291 registercvar(s2, "");
293 if(!rpn_error) // don't change cvars if a stack error had happened!
295 } else if(rpncmd == "load") {
296 rpn_set(cvar_string(rpn_get()));
297 } else if(rpncmd == "exch") {
302 } else if(rpncmd == "dup") {
304 } else if(rpncmd == "pop") {
306 } else if(rpncmd == "add" || rpncmd == "+") {
308 rpn_setf(rpn_getf() + f);
309 } else if(rpncmd == "sub" || rpncmd == "-") {
311 rpn_setf(rpn_getf() - f);
312 } else if(rpncmd == "mul" || rpncmd == "*") {
314 rpn_setf(rpn_getf() * f);
315 } else if(rpncmd == "div" || rpncmd == "/") {
317 rpn_setf(rpn_getf() / f);
318 } else if(rpncmd == "mod" || rpncmd == "%") {
321 rpn_setf(f2 - f * floor(f2 / f));
322 } else if(rpncmd == "abs") {
323 rpn_setf(fabs(rpn_getf()));
324 } else if(rpncmd == "sgn") {
332 } else if(rpncmd == "neg" || rpncmd == "~") {
333 rpn_setf(-rpn_getf());
334 } else if(rpncmd == "floor" || rpncmd == "f") {
335 rpn_setf(floor(rpn_getf()));
336 } else if(rpncmd == "ceil" || rpncmd == "c") {
337 rpn_setf(ceil(rpn_getf()));
338 } else if(rpncmd == "max") {
341 rpn_setf(max(f2, f));
342 } else if(rpncmd == "min") {
345 rpn_setf(min(f2, f));
346 } else if(rpncmd == "bound") {
350 rpn_setf(bound(f3, f2, f));
351 } else if(rpncmd == "when") {
359 } else if(rpncmd == ">" || rpncmd == "gt") {
361 rpn_setf(rpn_getf() > f);
362 } else if(rpncmd == "<" || rpncmd == "lt") {
364 rpn_setf(rpn_getf() < f);
365 } else if(rpncmd == "==" || rpncmd == "eq") {
367 rpn_setf(rpn_getf() == f);
368 } else if(rpncmd == ">=" || rpncmd == "ge") {
370 rpn_setf(rpn_getf() >= f);
371 } else if(rpncmd == "<=" || rpncmd == "le") {
373 rpn_setf(rpn_getf() <= f);
374 } else if(rpncmd == "!=" || rpncmd == "ne") {
376 rpn_setf(rpn_getf() != f);
377 } else if(rpncmd == "rand") {
378 rpn_setf(ceil(random() * rpn_getf()) - 1);
379 } else if(rpncmd == "crc16") {
380 rpn_setf(crc16(FALSE, rpn_get()));
381 } else if(rpncmd == "dbpush") {
385 i = stof(db_get(rpn_db, "stack.pointer"));
386 db_put(rpn_db, "stack.pointer", ftos(i+1));
387 db_put(rpn_db, strcat("stack.", ftos(i)), s);
390 db_put(rpn_db, "stack.pos", "0");
391 } else if(rpncmd == "dbpop") {
392 i = stof(db_get(rpn_db, "stack.pointer"));
396 db_put(rpn_db, "stack.pointer", s);
397 rpn_push(db_get(rpn_db, strcat("stack.", s)));
398 j = stof(db_get(rpn_db, "stack.pos"));
400 db_put(rpn_db, "stack.pos", ftos(i-2));
403 print("rpn: database underflow\n");
405 } else if(rpncmd == "dbget") {
407 i = stof(db_get(rpn_db, "stack.pointer"));
410 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
413 print("rpn: database empty\n");
415 } else if(rpncmd == "dblen") {
416 rpn_push(db_get(rpn_db, "stack.pointer"));
417 } else if(rpncmd == "dbclr") {
419 rpn_db = db_create();
420 db_put(rpn_db, "stack.pointer", "0");
421 db_put(rpn_db, "stack.pos", "-1");
422 } else if(rpncmd == "dbsave") {
426 } else if(rpncmd == "dbload") {
433 } else if(rpncmd == "dbins") {
438 j = stof(db_get(rpn_db, "stack.pointer"));
439 i = stof(db_get(rpn_db, "stack.pos"));
444 db_put(rpn_db, "stack.pos", "0");
447 db_put(rpn_db, "stack.pointer", ftos(j+1));
448 for(--j; j >= i; --j)
450 db_put(rpn_db, strcat("stack.", ftos(j+1)),
451 db_get(rpn_db, (strcat("stack.", ftos(j))))
454 db_put(rpn_db, strcat("stack.", ftos(i)), s);
456 } else if(rpncmd == "dbext") {
457 j = stof(db_get(rpn_db, "stack.pointer"));
458 i = stof(db_get(rpn_db, "stack.pos"));
462 print("rpn: empty database\n");
465 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
466 db_put(rpn_db, "stack.pointer", ftos(j));
469 db_put(rpn_db, "stack.pos", ftos(j-1));
473 db_put(rpn_db, strcat("stack.", ftos(i)),
474 db_get(rpn_db, (strcat("stack.", ftos(i+1))))
480 } else if(rpncmd == "dbread") {
481 s = db_get(rpn_db, "stack.pos");
484 rpn_push(db_get(rpn_db, strcat("stack.", s)));
487 print("rpn: empty database\n");
489 } else if(rpncmd == "dbat") {
490 rpn_push(db_get(rpn_db, "stack.pos"));
491 } else if(rpncmd == "dbmov") {
492 j = stof(db_get(rpn_db, "stack.pointer"));
493 i = stof(db_get(rpn_db, "stack.pos"));
499 print("rpn: database cursor out of bounds\n");
504 db_put(rpn_db, "stack.pos", ftos(i));
507 } else if(rpncmd == "dbgoto") {
509 j = stof(db_get(rpn_db, "stack.pointer"));
513 print("rpn: empty database, cannot move cursor\n");
518 i = stof(db_get(rpn_db, "stack.pointer"))-1;
524 j = stof(db_get(rpn_db, "stack.pointer"));
527 print("rpn: database cursor destination out of bounds\n");
532 db_put(rpn_db, "stack.pos", ftos(i));
535 } else if(rpncmd == "union") {
539 f = tokenize_sane(s);
540 f2 = tokenize_sane(strcat(s, " ", s2));
541 // tokens 0..(f-1) represent s
542 // tokens f..f2 represent s2
543 // UNION: add all tokens to s that are in s2 but not in s
545 for(i = 0; i < f; ++i)
546 s = strcat(s, " ", argv(i));
547 for(i = f; i < f2; ++i) {
548 for(j = 0; j < f; ++j)
549 if(argv(i) == argv(j))
551 s = strcat(s, " ", argv(i));
554 if(substring(s, 0, 1) == " ")
555 s = substring(s, 1, 99999);
557 tokenize_sane(command);
558 } else if(rpncmd == "intersection") {
562 f = tokenize_sane(s);
563 f2 = tokenize_sane(strcat(s, " ", s2));
564 // tokens 0..(f-1) represent s
565 // tokens f..f2 represent s2
566 // INTERSECTION: keep only the tokens from s that are also in s2
568 for(i = 0; i < f; ++i) {
569 for(j = f; j < f2; ++j)
570 if(argv(i) == argv(j))
572 s = strcat(s, " ", argv(i));
576 if(substring(s, 0, 1) == " ")
577 s = substring(s, 1, 99999);
579 tokenize_sane(command);
580 } else if(rpncmd == "difference") {
584 f = tokenize_sane(s);
585 f2 = tokenize_sane(strcat(s, " ", s2));
586 // tokens 0..(f-1) represent s
587 // tokens f..f2 represent s2
588 // DIFFERENCE: keep only the tokens from s that are not in s2
590 for(i = 0; i < f; ++i) {
591 for(j = f; j < f2; ++j)
592 if(argv(i) == argv(j))
593 goto skip_difference;
594 s = strcat(s, " ", argv(i));
597 if(substring(s, 0, 1) == " ")
598 s = substring(s, 1, 99999);
600 tokenize_sane(command);
601 } else if(rpncmd == "shuffle") {
604 f = tokenize_sane(s);
606 for(i = 0; i < f - 1; ++i) {
607 // move a random item from i..f-1 to position i
609 f2 = floor(random() * (f - i) + i);
610 for(j = 0; j < i; ++j)
611 s = strcat(s, " ", argv(j));
612 s = strcat(s, " ", argv(f2));
613 for(j = i; j < f; ++j)
615 s = strcat(s, " ", argv(j));
616 f = tokenize_sane(s);
619 if(substring(s, 0, 1) == " ")
620 s = substring(s, 1, 99999);
622 tokenize_sane(command);
623 } else if(rpncmd == "fexists_assert") {
627 f = fopen(s, FILE_READ);
631 print("rpn: ERROR: ", s, " does not exist!\n");
636 rpn_push(cvar_string(rpncmd));
644 print("rpn: still on stack: ", s, "\n");
649 } else if(argv(0) == "cp") {
653 for(i = 2; i < argc; ++i)
654 s = strcat(s, " ", argv(i));
655 centerprint(unescape(s));