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 #define NUM_MARKUPS 41
49 string markup_from[NUM_MARKUPS];
50 string markup_to[NUM_MARKUPS];
51 void GameCommand_MarkupInit()
58 markup_from[i] = "&alien"; markup_to[i] = "\x12"; ++i;
59 markup_from[i] = "&:-)"; markup_to[i] = "\x13"; ++i;
60 markup_from[i] = "&:-("; markup_to[i] = "\x14"; ++i;
61 markup_from[i] = "&x-P"; markup_to[i] = "\x15"; ++i;
62 markup_from[i] = "&:-/"; markup_to[i] = "\x16"; ++i;
63 markup_from[i] = "&:-D"; markup_to[i] = "\x17"; ++i;
64 markup_from[i] = "&<<"; markup_to[i] = "\x18"; ++i;
65 markup_from[i] = "&>>"; markup_to[i] = "\x19"; ++i;
66 markup_from[i] = "&dot"; markup_to[i] = "\x1a"; ++i;
67 markup_from[i] = "&^_"; markup_to[i] = "\x1b"; ++i;
68 markup_from[i] = "&ysplat"; markup_to[i] = "\x1c"; ++i;
69 markup_from[i] = "&-]"; markup_to[i] = "\x1d"; ++i;
70 markup_from[i] = "&--"; markup_to[i] = "\x1e"; ++i;
71 markup_from[i] = "&[-"; markup_to[i] = "\x1f"; ++i;
72 markup_from[i] = "&s<"; markup_to[i] = "\x2c"; ++i;
73 markup_from[i] = "&s>"; markup_to[i] = "\x2e"; ++i;
74 markup_from[i] = "&<-"; markup_to[i] = "\x7f"; ++i;
75 markup_from[i] = "&[="; markup_to[i] = "\x80"; ++i;
76 markup_from[i] = "&=="; markup_to[i] = "\x81"; ++i;
77 markup_from[i] = "&=]"; markup_to[i] = "\x82"; ++i;
78 markup_from[i] = "&r!"; markup_to[i] = "\x84"; ++i;
79 markup_from[i] = "&|o|"; markup_to[i] = "\x85"; ++i;
80 markup_from[i] = "&|u|"; markup_to[i] = "\x86"; ++i;
81 markup_from[i] = "&|i|"; markup_to[i] = "\x87"; ++i;
82 markup_from[i] = "&|c|"; markup_to[i] = "\x88"; ++i;
83 markup_from[i] = "&[c]"; markup_to[i] = "\x89"; ++i;
84 markup_from[i] = "&[n]"; markup_to[i] = "\x8a"; ++i;
85 markup_from[i] = "&[]"; markup_to[i] = "\x8b"; ++i;
86 markup_from[i] = "&r?"; markup_to[i] = "\x8c"; ++i;
87 markup_from[i] = "&|>"; markup_to[i] = "\x8d"; ++i;
88 markup_from[i] = "&splat0"; markup_to[i] = "\x8e"; ++i;
89 markup_from[i] = "&splat1"; markup_to[i] = "\x8f"; ++i;
90 markup_from[i] = "&[["; markup_to[i] = "\x90"; ++i;
91 markup_from[i] = "&]]"; markup_to[i] = "\x91"; ++i;
92 markup_from[i] = "&splat2"; markup_to[i] = "\x9a"; ++i;
93 markup_from[i] = "&)("; markup_to[i] = "\x9b"; ++i;
94 markup_from[i] = "&splat3"; markup_to[i] = "\x9c"; ++i;
95 markup_from[i] = "&(."; markup_to[i] = "\x9d"; ++i;
96 markup_from[i] = "&.."; markup_to[i] = "\x9e"; ++i;
97 markup_from[i] = "&.)"; markup_to[i] = "\x9f"; ++i;
98 markup_from[i] = "&<|"; markup_to[i] = "\xff"; ++i;
101 string GameCommand_Markup(string s2)
103 float red, ccase, i, j;
106 GameCommand_MarkupInit();
112 for(i = 0; i < strlen(s2); ++i)
114 for(j = 0; j < NUM_MARKUPS; ++j)
116 s3 = substring(s2, i, strlen(markup_from[j]));
117 if (s3 == markup_from[j])
119 s = strcat(s, markup_to[j]);
120 i += strlen(markup_from[j]) - 1;
127 if(substring(s2, i, 2) == "&&")
129 s = strcat(s, strconv(ccase, red, red, "&"));
132 else if(substring(s2, i, 2) == "&d")
138 else if(substring(s2, i, 2) == "&a")
144 else if(substring(s2, i, 2) == "&n")
151 s = strcat(s, strconv(ccase, red, red, substring(s2, i, 1)));
158 float GameCommand_Generic(string command)
164 argc = tokenize_console(command);
165 if(argv(0) == "help")
167 print(" rpn EXPRESSION... - a RPN calculator.\n");
168 print(" Operator description (x: string, s: set, f: float):\n");
169 print(" x pop -----------------------------> : removes the top\n");
170 print(" x dup -----------------------------> x x : duplicates the top\n");
171 print(" x x exch --------------------------> x x : swap the top two\n");
172 print(" /cvarname load --------------------> x : loads a cvar\n");
173 print(" /cvarname x def -------------------> : writes to a cvar\n");
174 print(" f f add|sub|mul|div|mod|max|min ---> f : adds/... two numbers\n");
175 print(" f f eq|ne|gt|ge|lt|le -------------> f : compares two numbers\n");
176 print(" f neg|abs|sgn|rand|floor|ceil------> f : negates/... a number\n");
177 print(" f f f bound -----------------------> f : bounds the middle number\n");
178 print(" f1 f2 b when ----------------------> f : f1 if b, f2 otherwise\n");
179 print(" s s union|intersection|difference -> s : set operations\n");
180 print(" s shuffle -------------------------> s : randomly arrange elements\n");
181 print(" /key /value put -------------------> : set a database key\n");
182 print(" /key get --------------------------> s : get a database value\n");
183 print(" x dbpush --------------------------> : pushes the top onto the database\n");
184 print(" dbpop|dbget -----------------------> x : removes/reads DB's top\n");
185 print(" dblen|dbat ------------------------> f : gets the DB's size/cursor pos\n");
186 print(" dbclr -----------------------------> : clear the DB\n");
187 print(" s dbsave|dbload--------------------> : save/load the DB to/from a file\n");
188 print(" x dbins ---------------------------> : moves the top into the DB\n");
189 print(" dbext|dbread ----------------------> x : extract/get from the DB's cursor\n");
190 print(" f dbmov|dbgoto --------------------> : move or set the DB's cursor\n");
191 print(" s localtime -----------------------> s : formats the current local time\n");
192 print(" s gmtime --------------------------> s : formats the current UTC time\n");
193 print(" time ------------------------------> f : seconds since VM start\n");
194 print(" s /MD4 digest ---------------------> s : MD4 digest\n");
195 print(" s /SHA256 digest ------------------> s : SHA256 digest\n");
196 print(" s /formatstring sprintf1s ---------> s : sprintf with 1 string (pad, cut)\n");
197 print(" Set operations operate on 'such''strings'.\n");
198 print(" Unknown tokens insert their cvar value.\n");
199 print(" maplist add map\n");
200 print(" maplist remove map\n");
201 print(" maplist shuffle\n");
202 print(" maplist cleanup\n");
203 print(" maplist maplist\n");
204 print(" maplist lsmaps\n");
205 print(" maplist lsnewmaps\n");
206 print(" addtolist variable addedvalue\n");
208 print(" rankings (map argument optional)\n");
212 if(argv(0) == "maplist")
214 if(argv(1) == "add" && argc == 3)
216 if (!fexists(strcat("maps/", argv(2), ".bsp")))
218 print("maplist: ERROR: ", argv(2), " does not exist!\n");
221 if(cvar_string("g_maplist") == "")
222 cvar_set("g_maplist", argv(2));
224 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
227 else if(argv(1) == "remove" && argc == 3)
230 n = tokenizebyseparator(cvar_string("g_maplist"), " ");
232 for(i = 0; i < n; ++i)
234 s2 = strcat(s2, " ", argv(i));
235 s2 = substring(s2, 1, strlen(s2) - 1);
236 cvar_set("g_maplist", s2);
239 else if(argv(1) == "shuffle" && argc == 2)
241 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
244 else if(argv(1) == "cleanup")
247 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
248 n = tokenizebyseparator(cvar_string("g_maplist"), " ");
250 for(i = 0; i < n; ++i)
251 if(MapInfo_CheckMap(argv(i)))
252 s2 = strcat(s2, " ", argv(i));
253 s2 = substring(s2, 1, strlen(s2) - 1);
254 cvar_set("g_maplist", s2);
257 else if(argv(1) == "maplist") {
258 print(maplist_reply);
261 else if(argv(1) == "lsmaps") {
265 else if(argv(1) == "lsnewmaps") {
266 print(lsnewmaps_reply);
270 else if(argc >= 3 && argv(0) == "red")
272 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
273 localcmd(strcat(argv(1), " ", GameCommand_Markup(s)));
276 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
279 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
281 n = floor(random() * 6 + 2);
284 for(i = 0; i < n; ++i)
286 s2 = strcat(s2, "AH");
290 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
296 s = strcat(s, " ", s2);
298 s = strcat(s2, " ", s);
300 s2 = substring(s, strlen(s) - 2, 2);
301 if(s2 == "AH" || s2 == "AY")
304 s = strcat(s, " ))");
307 s = substring(s, 0, strlen(s) - 1);
310 s = strconv(1, 0, 0, s);
312 localcmd(strcat(argv(1), " ", s));
316 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
318 // test case for terencehill's color codes
319 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
323 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
326 for(i = 0; i < n; ++i)
328 c = substring(s, i, 1);
335 if(substring(s, i+1, 1) == "^")
341 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
342 c = strcat(rgb_to_hexcolor(rgb), c);
347 localcmd(strcat(argv(1), " ", s2));
351 else if(argv(0) == "rpn")
355 rpn_db = db_create();
356 db_put(rpn_db, "stack.pointer", "0");
357 db_put(rpn_db, "stack.pos", "-1");
366 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
368 rpncmd = argv(rpnpos);
371 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
373 } else if(substring(rpncmd, 0, 1) == "0") {
375 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
377 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
379 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
380 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
381 } else if(rpncmd == "clear") {
383 } else if(rpncmd == "def" || rpncmd == "=") {
390 registercvar(s2, "", 0);
392 registercvar(s2, "");
394 if(!rpn_error) // don't change cvars if a stack error had happened!
399 print("rpn: empty cvar name for 'def'\n");
402 } else if(rpncmd == "defs" || rpncmd == "@") {
406 while(rpn_sp > 1 && (j || i > 0))
408 s = strcat("/", rpn_pop(), " ", s);
415 registercvar(s2, "", 0);
417 registercvar(s2, "");
419 if(!rpn_error) // don't change cvars if a stack error had happened!
424 print("rpn: empty cvar name for 'defs'\n");
427 } else if(rpncmd == "load") {
428 rpn_set(cvar_string(rpn_get()));
429 } else if(rpncmd == "exch") {
434 } else if(rpncmd == "dup") {
436 } else if(rpncmd == "pop") {
438 } else if(rpncmd == "add" || rpncmd == "+") {
440 rpn_setf(rpn_getf() + f);
441 } else if(rpncmd == "sub" || rpncmd == "-") {
443 rpn_setf(rpn_getf() - f);
444 } else if(rpncmd == "mul" || rpncmd == "*") {
446 rpn_setf(rpn_getf() * f);
447 } else if(rpncmd == "div" || rpncmd == "/") {
449 rpn_setf(rpn_getf() / f);
450 } else if(rpncmd == "mod" || rpncmd == "%") {
453 rpn_setf(f2 - f * floor(f2 / f));
454 } else if(rpncmd == "abs") {
455 rpn_setf(fabs(rpn_getf()));
456 } else if(rpncmd == "sgn") {
464 } else if(rpncmd == "neg" || rpncmd == "~") {
465 rpn_setf(-rpn_getf());
466 } else if(rpncmd == "floor" || rpncmd == "f") {
467 rpn_setf(floor(rpn_getf()));
468 } else if(rpncmd == "ceil" || rpncmd == "c") {
469 rpn_setf(ceil(rpn_getf()));
470 } else if(rpncmd == "max") {
473 rpn_setf(max(f2, f));
474 } else if(rpncmd == "min") {
477 rpn_setf(min(f2, f));
478 } else if(rpncmd == "bound") {
482 rpn_setf(bound(f3, f2, f));
483 } else if(rpncmd == "when") {
491 } else if(rpncmd == ">" || rpncmd == "gt") {
493 rpn_setf(rpn_getf() > f);
494 } else if(rpncmd == "<" || rpncmd == "lt") {
496 rpn_setf(rpn_getf() < f);
497 } else if(rpncmd == "==" || rpncmd == "eq") {
499 rpn_setf(rpn_getf() == f);
500 } else if(rpncmd == ">=" || rpncmd == "ge") {
502 rpn_setf(rpn_getf() >= f);
503 } else if(rpncmd == "<=" || rpncmd == "le") {
505 rpn_setf(rpn_getf() <= f);
506 } else if(rpncmd == "!=" || rpncmd == "ne") {
508 rpn_setf(rpn_getf() != f);
509 } else if(rpncmd == "rand") {
510 rpn_setf(ceil(random() * rpn_getf()) - 1);
511 } else if(rpncmd == "crc16") {
512 rpn_setf(crc16(FALSE, rpn_get()));
513 } else if(rpncmd == "put") {
519 db_put(rpn_db, s, s2);
521 } else if(rpncmd == "get") {
524 rpn_push(db_get(rpn_db, s));
525 } else if(rpncmd == "dbpush") {
529 i = stof(db_get(rpn_db, "stack.pointer"));
530 db_put(rpn_db, "stack.pointer", ftos(i+1));
531 db_put(rpn_db, strcat("stack.", ftos(i)), s);
534 db_put(rpn_db, "stack.pos", "0");
535 } else if(rpncmd == "dbpop") {
536 i = stof(db_get(rpn_db, "stack.pointer"));
540 db_put(rpn_db, "stack.pointer", s);
541 rpn_push(db_get(rpn_db, strcat("stack.", s)));
542 j = stof(db_get(rpn_db, "stack.pos"));
544 db_put(rpn_db, "stack.pos", ftos(i-2));
547 print("rpn: database underflow\n");
549 } else if(rpncmd == "dbget") {
551 i = stof(db_get(rpn_db, "stack.pointer"));
554 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
557 print("rpn: database empty\n");
559 } else if(rpncmd == "dblen") {
560 rpn_push(db_get(rpn_db, "stack.pointer"));
561 } else if(rpncmd == "dbclr") {
563 rpn_db = db_create();
564 db_put(rpn_db, "stack.pointer", "0");
565 db_put(rpn_db, "stack.pos", "-1");
566 } else if(rpncmd == "dbsave") {
570 } else if(rpncmd == "dbload") {
577 } else if(rpncmd == "dbins") {
582 j = stof(db_get(rpn_db, "stack.pointer"));
583 i = stof(db_get(rpn_db, "stack.pos"));
588 db_put(rpn_db, "stack.pos", "0");
591 db_put(rpn_db, "stack.pointer", ftos(j+1));
592 for(--j; j >= i; --j)
594 db_put(rpn_db, strcat("stack.", ftos(j+1)),
595 db_get(rpn_db, (strcat("stack.", ftos(j))))
598 db_put(rpn_db, strcat("stack.", ftos(i)), s);
600 } else if(rpncmd == "dbext") {
601 j = stof(db_get(rpn_db, "stack.pointer"));
602 i = stof(db_get(rpn_db, "stack.pos"));
606 print("rpn: empty database\n");
609 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
610 db_put(rpn_db, "stack.pointer", ftos(j));
613 db_put(rpn_db, "stack.pos", ftos(j-1));
617 db_put(rpn_db, strcat("stack.", ftos(i)),
618 db_get(rpn_db, (strcat("stack.", ftos(i+1))))
624 } else if(rpncmd == "dbread") {
625 s = db_get(rpn_db, "stack.pos");
628 rpn_push(db_get(rpn_db, strcat("stack.", s)));
631 print("rpn: empty database\n");
633 } else if(rpncmd == "dbat") {
634 rpn_push(db_get(rpn_db, "stack.pos"));
635 } else if(rpncmd == "dbmov") {
636 j = stof(db_get(rpn_db, "stack.pointer"));
637 i = stof(db_get(rpn_db, "stack.pos"));
643 print("rpn: database cursor out of bounds\n");
648 db_put(rpn_db, "stack.pos", ftos(i));
651 } else if(rpncmd == "dbgoto") {
653 j = stof(db_get(rpn_db, "stack.pointer"));
657 print("rpn: empty database, cannot move cursor\n");
662 i = stof(db_get(rpn_db, "stack.pointer"))-1;
668 j = stof(db_get(rpn_db, "stack.pointer"));
671 print("rpn: database cursor destination out of bounds\n");
676 db_put(rpn_db, "stack.pos", ftos(i));
679 } else if(rpncmd == "union") {
683 f = tokenize_console(s);
684 f2 = tokenize_console(strcat(s, " ", s2));
685 // tokens 0..(f-1) represent s
686 // tokens f..f2 represent s2
687 // UNION: add all tokens to s that are in s2 but not in s
689 for(i = 0; i < f; ++i)
690 s = strcat(s, " ", argv(i));
691 for(i = f; i < f2; ++i) {
692 for(j = 0; j < f; ++j)
693 if(argv(i) == argv(j))
695 s = strcat(s, " ", argv(i));
698 if(substring(s, 0, 1) == " ")
699 s = substring(s, 1, 99999);
701 tokenize_console(command);
702 } else if(rpncmd == "intersection") {
706 f = tokenize_console(s);
707 f2 = tokenize_console(strcat(s, " ", s2));
708 // tokens 0..(f-1) represent s
709 // tokens f..f2 represent s2
710 // INTERSECTION: keep only the tokens from s that are also in s2
712 for(i = 0; i < f; ++i) {
713 for(j = f; j < f2; ++j)
714 if(argv(i) == argv(j))
716 s = strcat(s, " ", argv(i));
720 if(substring(s, 0, 1) == " ")
721 s = substring(s, 1, 99999);
723 tokenize_console(command);
724 } else if(rpncmd == "difference") {
728 f = tokenize_console(s);
729 f2 = tokenize_console(strcat(s, " ", s2));
730 // tokens 0..(f-1) represent s
731 // tokens f..f2 represent s2
732 // DIFFERENCE: keep only the tokens from s that are not in s2
734 for(i = 0; i < f; ++i) {
735 for(j = f; j < f2; ++j)
736 if(argv(i) == argv(j))
737 goto skip_difference;
738 s = strcat(s, " ", argv(i));
741 if(substring(s, 0, 1) == " ")
742 s = substring(s, 1, 99999);
744 tokenize_console(command);
745 } else if(rpncmd == "shuffle") {
748 f = tokenize_console(s);
750 for(i = 0; i < f - 1; ++i) {
751 // move a random item from i..f-1 to position i
753 f2 = floor(random() * (f - i) + i);
754 for(j = 0; j < i; ++j)
755 s = strcat(s, " ", argv(j));
756 s = strcat(s, " ", argv(f2));
757 for(j = i; j < f; ++j)
759 s = strcat(s, " ", argv(j));
760 f = tokenize_console(s);
763 if(substring(s, 0, 1) == " ")
764 s = substring(s, 1, 99999);
766 tokenize_console(command);
767 } else if(rpncmd == "fexists_assert") {
773 print("rpn: ERROR: ", s, " does not exist!\n");
777 } else if(rpncmd == "fexists") {
786 } else if(rpncmd == "localtime") {
787 rpn_set(strftime(TRUE, rpn_get()));
788 } else if(rpncmd == "gmtime") {
789 rpn_set(strftime(FALSE, rpn_get()));
790 } else if(rpncmd == "time") {
792 } else if(rpncmd == "digest") {
794 rpn_set(digest_hex(s, rpn_get()));
795 } else if(rpncmd == "sprintf1s") {
797 rpn_set(sprintf(s, rpn_get()));
799 rpn_push(cvar_string(rpncmd));
807 print("rpn: still on stack: ", s, "\n");
811 } else if(argv(0) == "addtolist") {
816 if(cvar_string(s) == "")
819 n = tokenizebyseparator(cvar_string(s), " ");
820 for(i = 0; i < n; ++i)
822 return TRUE; // already in list
823 cvar_set(s, strcat(s2, " ", cvar_string(s)));
828 else if(argv(0) == "records") {
829 print(records_reply);
832 else if(argv(0) == "ladder") {
836 else if(argv(0) == "rankings") {
837 print(rankings_reply);
840 } else if(argv(0) == "cp") {
844 for(i = 2; i < argc; ++i)
845 s = strcat(s, " ", argv(i));
846 centerprint(unescape(s));