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(" s localtime -----------------------> s : formats the current local time\n");
79 print(" s gmtime --------------------------> s : formats the current UTC time\n");
80 print(" time ------------------------------> f : seconds since VM start\n");
81 print(" Set operations operate on 'such''strings'.\n");
82 print(" Unknown tokens insert their cvar value.\n");
83 print(" maplist add map\n");
84 print(" maplist remove map\n");
85 print(" maplist shuffle\n");
86 print(" maplist cleanup\n");
87 print(" addtolist variable addedvalue\n");
91 if(argv(0) == "maplist")
93 if(argv(1) == "add" && argc == 3)
95 f = fopen(strcat("maps/", argv(2), ".bsp"), FILE_READ);
99 print("maplist: ERROR: ", argv(2), " does not exist!\n");
102 if(cvar_string("g_maplist") == "")
103 cvar_set("g_maplist", argv(2));
105 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
108 else if(argv(1) == "remove" && argc == 3)
111 n = tokenizebyseparator(cvar_string("g_maplist"), " ");
113 for(i = 0; i < n; ++i)
115 s2 = strcat(s2, " ", argv(i));
116 s2 = substring(s2, 1, strlen(s2) - 1);
117 cvar_set("g_maplist", s2);
120 else if(argv(1) == "shuffle" && argc == 2)
122 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
125 else if(argv(1) == "cleanup")
128 if(cvar("g_maplist_allow_hidden"))
131 i = MAPINFO_FLAG_HIDDEN;
132 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0, i, 0);
133 n = tokenizebyseparator(cvar_string("g_maplist"), " ");
135 for(i = 0; i < n; ++i)
136 if(MapInfo_CheckMap(argv(i)))
137 s2 = strcat(s2, " ", argv(i));
138 s2 = substring(s2, 1, strlen(s2) - 1);
139 cvar_set("g_maplist", s2);
143 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
146 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
148 n = floor(random() * 6 + 2);
151 for(i = 0; i < n; ++i)
153 s2 = strcat(s2, "AH");
157 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
163 s = strcat(s, " ", s2);
165 s = strcat(s2, " ", s);
167 s2 = substring(s, strlen(s) - 2, 2);
168 if(s2 == "AH" || s2 == "AY")
171 s = strcat(s, " ))");
174 s = substring(s, 0, strlen(s) - 1);
177 s = strconv(1, 0, 0, s);
179 localcmd(strcat(argv(1), " ", s));
183 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55970)
185 // test case for terrencehill's color codes
186 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
190 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
193 for(i = 0; i < n; ++i)
195 c = substring(s, i, 1);
202 if(substring(s, i+1, 1) == "^")
208 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
209 c = strcat(rgb_to_hexcolor(rgb), c);
214 localcmd(strcat(argv(1), " ", s2));
218 else if(argv(0) == "rpn")
222 rpn_db = db_create();
223 db_put(rpn_db, "stack.pointer", "0");
224 db_put(rpn_db, "stack.pos", "-1");
233 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
235 rpncmd = argv(rpnpos);
238 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
240 } else if(substring(rpncmd, 0, 1) == "0") {
242 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
244 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
246 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
247 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
248 } else if(rpncmd == "clear") {
250 } else if(rpncmd == "def" || rpncmd == "=") {
254 registercvar(s2, "", 0);
256 registercvar(s2, "");
258 if(!rpn_error) // don't change cvars if a stack error had happened!
260 } else if(rpncmd == "defs" || rpncmd == "@") {
264 while(rpn_sp > 1 && (j || i > 0))
266 s = strcat("/", rpn_pop(), " ", s);
271 registercvar(s2, "", 0);
273 registercvar(s2, "");
275 if(!rpn_error) // don't change cvars if a stack error had happened!
277 } else if(rpncmd == "load") {
278 rpn_set(cvar_string(rpn_get()));
279 } else if(rpncmd == "exch") {
284 } else if(rpncmd == "dup") {
286 } else if(rpncmd == "pop") {
288 } else if(rpncmd == "add" || rpncmd == "+") {
290 rpn_setf(rpn_getf() + f);
291 } else if(rpncmd == "sub" || rpncmd == "-") {
293 rpn_setf(rpn_getf() - f);
294 } else if(rpncmd == "mul" || rpncmd == "*") {
296 rpn_setf(rpn_getf() * f);
297 } else if(rpncmd == "div" || rpncmd == "/") {
299 rpn_setf(rpn_getf() / f);
300 } else if(rpncmd == "mod" || rpncmd == "%") {
303 rpn_setf(f2 - f * floor(f2 / f));
304 } else if(rpncmd == "abs") {
305 rpn_setf(fabs(rpn_getf()));
306 } else if(rpncmd == "sgn") {
314 } else if(rpncmd == "neg" || rpncmd == "~") {
315 rpn_setf(-rpn_getf());
316 } else if(rpncmd == "floor" || rpncmd == "f") {
317 rpn_setf(floor(rpn_getf()));
318 } else if(rpncmd == "ceil" || rpncmd == "c") {
319 rpn_setf(ceil(rpn_getf()));
320 } else if(rpncmd == "max") {
323 rpn_setf(max(f2, f));
324 } else if(rpncmd == "min") {
327 rpn_setf(min(f2, f));
328 } else if(rpncmd == "bound") {
332 rpn_setf(bound(f3, f2, f));
333 } else if(rpncmd == "when") {
341 } else if(rpncmd == ">" || rpncmd == "gt") {
343 rpn_setf(rpn_getf() > f);
344 } else if(rpncmd == "<" || rpncmd == "lt") {
346 rpn_setf(rpn_getf() < f);
347 } else if(rpncmd == "==" || rpncmd == "eq") {
349 rpn_setf(rpn_getf() == f);
350 } else if(rpncmd == ">=" || rpncmd == "ge") {
352 rpn_setf(rpn_getf() >= f);
353 } else if(rpncmd == "<=" || rpncmd == "le") {
355 rpn_setf(rpn_getf() <= f);
356 } else if(rpncmd == "!=" || rpncmd == "ne") {
358 rpn_setf(rpn_getf() != f);
359 } else if(rpncmd == "rand") {
360 rpn_setf(ceil(random() * rpn_getf()) - 1);
361 } else if(rpncmd == "crc16") {
362 rpn_setf(crc16(FALSE, rpn_get()));
363 } else if(rpncmd == "dbpush") {
367 i = stof(db_get(rpn_db, "stack.pointer"));
368 db_put(rpn_db, "stack.pointer", ftos(i+1));
369 db_put(rpn_db, strcat("stack.", ftos(i)), s);
372 db_put(rpn_db, "stack.pos", "0");
373 } else if(rpncmd == "dbpop") {
374 i = stof(db_get(rpn_db, "stack.pointer"));
378 db_put(rpn_db, "stack.pointer", s);
379 rpn_push(db_get(rpn_db, strcat("stack.", s)));
380 j = stof(db_get(rpn_db, "stack.pos"));
382 db_put(rpn_db, "stack.pos", ftos(i-2));
385 print("rpn: database underflow\n");
387 } else if(rpncmd == "dbget") {
389 i = stof(db_get(rpn_db, "stack.pointer"));
392 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
395 print("rpn: database empty\n");
397 } else if(rpncmd == "dblen") {
398 rpn_push(db_get(rpn_db, "stack.pointer"));
399 } else if(rpncmd == "dbclr") {
401 rpn_db = db_create();
402 db_put(rpn_db, "stack.pointer", "0");
403 db_put(rpn_db, "stack.pos", "-1");
404 } else if(rpncmd == "dbsave") {
408 } else if(rpncmd == "dbload") {
415 } else if(rpncmd == "dbins") {
420 j = stof(db_get(rpn_db, "stack.pointer"));
421 i = stof(db_get(rpn_db, "stack.pos"));
426 db_put(rpn_db, "stack.pos", "0");
429 db_put(rpn_db, "stack.pointer", ftos(j+1));
430 for(--j; j >= i; --j)
432 db_put(rpn_db, strcat("stack.", ftos(j+1)),
433 db_get(rpn_db, (strcat("stack.", ftos(j))))
436 db_put(rpn_db, strcat("stack.", ftos(i)), s);
438 } else if(rpncmd == "dbext") {
439 j = stof(db_get(rpn_db, "stack.pointer"));
440 i = stof(db_get(rpn_db, "stack.pos"));
444 print("rpn: empty database\n");
447 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
448 db_put(rpn_db, "stack.pointer", ftos(j));
451 db_put(rpn_db, "stack.pos", ftos(j-1));
455 db_put(rpn_db, strcat("stack.", ftos(i)),
456 db_get(rpn_db, (strcat("stack.", ftos(i+1))))
462 } else if(rpncmd == "dbread") {
463 s = db_get(rpn_db, "stack.pos");
466 rpn_push(db_get(rpn_db, strcat("stack.", s)));
469 print("rpn: empty database\n");
471 } else if(rpncmd == "dbat") {
472 rpn_push(db_get(rpn_db, "stack.pos"));
473 } else if(rpncmd == "dbmov") {
474 j = stof(db_get(rpn_db, "stack.pointer"));
475 i = stof(db_get(rpn_db, "stack.pos"));
481 print("rpn: database cursor out of bounds\n");
486 db_put(rpn_db, "stack.pos", ftos(i));
489 } else if(rpncmd == "dbgoto") {
491 j = stof(db_get(rpn_db, "stack.pointer"));
495 print("rpn: empty database, cannot move cursor\n");
500 i = stof(db_get(rpn_db, "stack.pointer"))-1;
506 j = stof(db_get(rpn_db, "stack.pointer"));
509 print("rpn: database cursor destination out of bounds\n");
514 db_put(rpn_db, "stack.pos", ftos(i));
517 } else if(rpncmd == "union") {
521 f = tokenize_sane(s);
522 f2 = tokenize_sane(strcat(s, " ", s2));
523 // tokens 0..(f-1) represent s
524 // tokens f..f2 represent s2
525 // UNION: add all tokens to s that are in s2 but not in s
527 for(i = 0; i < f; ++i)
528 s = strcat(s, " ", argv(i));
529 for(i = f; i < f2; ++i) {
530 for(j = 0; j < f; ++j)
531 if(argv(i) == argv(j))
533 s = strcat(s, " ", argv(i));
536 if(substring(s, 0, 1) == " ")
537 s = substring(s, 1, 99999);
539 tokenize_sane(command);
540 } else if(rpncmd == "intersection") {
544 f = tokenize_sane(s);
545 f2 = tokenize_sane(strcat(s, " ", s2));
546 // tokens 0..(f-1) represent s
547 // tokens f..f2 represent s2
548 // INTERSECTION: keep only the tokens from s that are also in s2
550 for(i = 0; i < f; ++i) {
551 for(j = f; j < f2; ++j)
552 if(argv(i) == argv(j))
554 s = strcat(s, " ", argv(i));
558 if(substring(s, 0, 1) == " ")
559 s = substring(s, 1, 99999);
561 tokenize_sane(command);
562 } else if(rpncmd == "difference") {
566 f = tokenize_sane(s);
567 f2 = tokenize_sane(strcat(s, " ", s2));
568 // tokens 0..(f-1) represent s
569 // tokens f..f2 represent s2
570 // DIFFERENCE: keep only the tokens from s that are not in s2
572 for(i = 0; i < f; ++i) {
573 for(j = f; j < f2; ++j)
574 if(argv(i) == argv(j))
575 goto skip_difference;
576 s = strcat(s, " ", argv(i));
579 if(substring(s, 0, 1) == " ")
580 s = substring(s, 1, 99999);
582 tokenize_sane(command);
583 } else if(rpncmd == "shuffle") {
586 f = tokenize_sane(s);
588 for(i = 0; i < f - 1; ++i) {
589 // move a random item from i..f-1 to position i
591 f2 = floor(random() * (f - i) + i);
592 for(j = 0; j < i; ++j)
593 s = strcat(s, " ", argv(j));
594 s = strcat(s, " ", argv(f2));
595 for(j = i; j < f; ++j)
597 s = strcat(s, " ", argv(j));
598 f = tokenize_sane(s);
601 if(substring(s, 0, 1) == " ")
602 s = substring(s, 1, 99999);
604 tokenize_sane(command);
605 } else if(rpncmd == "fexists_assert") {
609 f = fopen(s, FILE_READ);
613 print("rpn: ERROR: ", s, " does not exist!\n");
617 } else if(rpncmd == "localtime") {
618 rpn_set(strftime(TRUE, rpn_get()));
619 } else if(rpncmd == "gmtime") {
620 rpn_set(strftime(FALSE, rpn_get()));
621 } else if(rpncmd == "time") {
624 rpn_push(cvar_string(rpncmd));
632 print("rpn: still on stack: ", s, "\n");
636 } else if(argv(0) == "addtolist") {
641 if(cvar_string(s) == "")
644 n = tokenizebyseparator(cvar_string(s), " ");
645 for(i = 0; i < n; ++i)
647 return TRUE; // already in list
648 cvar_set(s, strcat(s2, " ", cvar_string(s)));
653 } else if(argv(0) == "cp") {
657 for(i = 2; i < argc; ++i)
658 s = strcat(s, " ", argv(i));
659 centerprint(unescape(s));