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");
83 print(" maplist cleanup\n");
84 print(" addtolist variable addedvalue\n");
88 if(argv(0) == "maplist")
90 if(argv(1) == "add" && argc == 3)
92 f = fopen(strcat("maps/", argv(2), ".bsp"), FILE_READ);
96 print("maplist: ERROR: ", argv(2), " does not exist!\n");
99 if(cvar_string("g_maplist") == "")
100 cvar_set("g_maplist", argv(2));
102 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
105 else if(argv(1) == "remove" && argc == 3)
108 n = tokenizebyseparator(cvar_string("g_maplist"), " ");
110 for(i = 0; i < n; ++i)
112 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 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
122 else if(argv(1) == "cleanup")
125 if(cvar("g_maplist_allow_hidden"))
128 i = MAPINFO_FLAG_HIDDEN;
129 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0, i, 0);
130 n = tokenizebyseparator(cvar_string("g_maplist"), " ");
132 for(i = 0; i < n; ++i)
133 if(MapInfo_CheckMap(argv(i)))
134 s2 = strcat(s2, " ", argv(i));
135 s2 = substring(s2, 1, strlen(s2) - 1);
136 cvar_set("g_maplist", s2);
140 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
143 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
145 n = floor(random() * 6 + 2);
148 for(i = 0; i < n; ++i)
150 s2 = strcat(s2, "AH");
154 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
160 s = strcat(s, " ", s2);
162 s = strcat(s2, " ", s);
164 s2 = substring(s, strlen(s) - 2, 2);
165 if(s2 == "AH" || s2 == "AY")
168 s = strcat(s, " ))");
171 s = substring(s, 0, strlen(s) - 1);
174 s = strconv(1, 0, 0, s);
176 localcmd(strcat(argv(1), " ", s));
180 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55970)
182 // test case for terrencehill's color codes
183 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
187 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
190 for(i = 0; i < n; ++i)
192 c = substring(s, i, 1);
199 if(substring(s, i+1, 1) == "^")
205 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
206 c = strcat(rgb_to_hexcolor(rgb), c);
211 localcmd(strcat(argv(1), " ", s2));
215 else if(argv(0) == "rpn")
219 rpn_db = db_create();
220 db_put(rpn_db, "stack.pointer", "0");
221 db_put(rpn_db, "stack.pos", "-1");
230 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
232 rpncmd = argv(rpnpos);
235 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
237 } else if(substring(rpncmd, 0, 1) == "0") {
239 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
241 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
243 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
244 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
245 } else if(rpncmd == "clear") {
247 } else if(rpncmd == "def" || rpncmd == "=") {
251 registercvar(s2, "", 0);
253 registercvar(s2, "");
255 if(!rpn_error) // don't change cvars if a stack error had happened!
257 } else if(rpncmd == "defs" || rpncmd == "@") {
261 while(rpn_sp > 1 && (j || i > 0))
263 s = strcat("/", rpn_pop(), " ", s);
268 registercvar(s2, "", 0);
270 registercvar(s2, "");
272 if(!rpn_error) // don't change cvars if a stack error had happened!
274 } else if(rpncmd == "load") {
275 rpn_set(cvar_string(rpn_get()));
276 } else if(rpncmd == "exch") {
281 } else if(rpncmd == "dup") {
283 } else if(rpncmd == "pop") {
285 } else if(rpncmd == "add" || rpncmd == "+") {
287 rpn_setf(rpn_getf() + f);
288 } else if(rpncmd == "sub" || rpncmd == "-") {
290 rpn_setf(rpn_getf() - f);
291 } else if(rpncmd == "mul" || rpncmd == "*") {
293 rpn_setf(rpn_getf() * f);
294 } else if(rpncmd == "div" || rpncmd == "/") {
296 rpn_setf(rpn_getf() / f);
297 } else if(rpncmd == "mod" || rpncmd == "%") {
300 rpn_setf(f2 - f * floor(f2 / f));
301 } else if(rpncmd == "abs") {
302 rpn_setf(fabs(rpn_getf()));
303 } else if(rpncmd == "sgn") {
311 } else if(rpncmd == "neg" || rpncmd == "~") {
312 rpn_setf(-rpn_getf());
313 } else if(rpncmd == "floor" || rpncmd == "f") {
314 rpn_setf(floor(rpn_getf()));
315 } else if(rpncmd == "ceil" || rpncmd == "c") {
316 rpn_setf(ceil(rpn_getf()));
317 } else if(rpncmd == "max") {
320 rpn_setf(max(f2, f));
321 } else if(rpncmd == "min") {
324 rpn_setf(min(f2, f));
325 } else if(rpncmd == "bound") {
329 rpn_setf(bound(f3, f2, f));
330 } else if(rpncmd == "when") {
338 } else if(rpncmd == ">" || rpncmd == "gt") {
340 rpn_setf(rpn_getf() > f);
341 } else if(rpncmd == "<" || rpncmd == "lt") {
343 rpn_setf(rpn_getf() < f);
344 } else if(rpncmd == "==" || rpncmd == "eq") {
346 rpn_setf(rpn_getf() == f);
347 } else if(rpncmd == ">=" || rpncmd == "ge") {
349 rpn_setf(rpn_getf() >= f);
350 } else if(rpncmd == "<=" || rpncmd == "le") {
352 rpn_setf(rpn_getf() <= f);
353 } else if(rpncmd == "!=" || rpncmd == "ne") {
355 rpn_setf(rpn_getf() != f);
356 } else if(rpncmd == "rand") {
357 rpn_setf(ceil(random() * rpn_getf()) - 1);
358 } else if(rpncmd == "crc16") {
359 rpn_setf(crc16(FALSE, rpn_get()));
360 } else if(rpncmd == "dbpush") {
364 i = stof(db_get(rpn_db, "stack.pointer"));
365 db_put(rpn_db, "stack.pointer", ftos(i+1));
366 db_put(rpn_db, strcat("stack.", ftos(i)), s);
369 db_put(rpn_db, "stack.pos", "0");
370 } else if(rpncmd == "dbpop") {
371 i = stof(db_get(rpn_db, "stack.pointer"));
375 db_put(rpn_db, "stack.pointer", s);
376 rpn_push(db_get(rpn_db, strcat("stack.", s)));
377 j = stof(db_get(rpn_db, "stack.pos"));
379 db_put(rpn_db, "stack.pos", ftos(i-2));
382 print("rpn: database underflow\n");
384 } else if(rpncmd == "dbget") {
386 i = stof(db_get(rpn_db, "stack.pointer"));
389 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
392 print("rpn: database empty\n");
394 } else if(rpncmd == "dblen") {
395 rpn_push(db_get(rpn_db, "stack.pointer"));
396 } else if(rpncmd == "dbclr") {
398 rpn_db = db_create();
399 db_put(rpn_db, "stack.pointer", "0");
400 db_put(rpn_db, "stack.pos", "-1");
401 } else if(rpncmd == "dbsave") {
405 } else if(rpncmd == "dbload") {
412 } else if(rpncmd == "dbins") {
417 j = stof(db_get(rpn_db, "stack.pointer"));
418 i = stof(db_get(rpn_db, "stack.pos"));
423 db_put(rpn_db, "stack.pos", "0");
426 db_put(rpn_db, "stack.pointer", ftos(j+1));
427 for(--j; j >= i; --j)
429 db_put(rpn_db, strcat("stack.", ftos(j+1)),
430 db_get(rpn_db, (strcat("stack.", ftos(j))))
433 db_put(rpn_db, strcat("stack.", ftos(i)), s);
435 } else if(rpncmd == "dbext") {
436 j = stof(db_get(rpn_db, "stack.pointer"));
437 i = stof(db_get(rpn_db, "stack.pos"));
441 print("rpn: empty database\n");
444 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
445 db_put(rpn_db, "stack.pointer", ftos(j));
448 db_put(rpn_db, "stack.pos", ftos(j-1));
452 db_put(rpn_db, strcat("stack.", ftos(i)),
453 db_get(rpn_db, (strcat("stack.", ftos(i+1))))
459 } else if(rpncmd == "dbread") {
460 s = db_get(rpn_db, "stack.pos");
463 rpn_push(db_get(rpn_db, strcat("stack.", s)));
466 print("rpn: empty database\n");
468 } else if(rpncmd == "dbat") {
469 rpn_push(db_get(rpn_db, "stack.pos"));
470 } else if(rpncmd == "dbmov") {
471 j = stof(db_get(rpn_db, "stack.pointer"));
472 i = stof(db_get(rpn_db, "stack.pos"));
478 print("rpn: database cursor out of bounds\n");
483 db_put(rpn_db, "stack.pos", ftos(i));
486 } else if(rpncmd == "dbgoto") {
488 j = stof(db_get(rpn_db, "stack.pointer"));
492 print("rpn: empty database, cannot move cursor\n");
497 i = stof(db_get(rpn_db, "stack.pointer"))-1;
503 j = stof(db_get(rpn_db, "stack.pointer"));
506 print("rpn: database cursor destination out of bounds\n");
511 db_put(rpn_db, "stack.pos", ftos(i));
514 } else if(rpncmd == "union") {
518 f = tokenize_sane(s);
519 f2 = tokenize_sane(strcat(s, " ", s2));
520 // tokens 0..(f-1) represent s
521 // tokens f..f2 represent s2
522 // UNION: add all tokens to s that are in s2 but not in s
524 for(i = 0; i < f; ++i)
525 s = strcat(s, " ", argv(i));
526 for(i = f; i < f2; ++i) {
527 for(j = 0; j < f; ++j)
528 if(argv(i) == argv(j))
530 s = strcat(s, " ", argv(i));
533 if(substring(s, 0, 1) == " ")
534 s = substring(s, 1, 99999);
536 tokenize_sane(command);
537 } else if(rpncmd == "intersection") {
541 f = tokenize_sane(s);
542 f2 = tokenize_sane(strcat(s, " ", s2));
543 // tokens 0..(f-1) represent s
544 // tokens f..f2 represent s2
545 // INTERSECTION: keep only the tokens from s that are also in s2
547 for(i = 0; i < f; ++i) {
548 for(j = f; j < f2; ++j)
549 if(argv(i) == argv(j))
551 s = strcat(s, " ", argv(i));
555 if(substring(s, 0, 1) == " ")
556 s = substring(s, 1, 99999);
558 tokenize_sane(command);
559 } else if(rpncmd == "difference") {
563 f = tokenize_sane(s);
564 f2 = tokenize_sane(strcat(s, " ", s2));
565 // tokens 0..(f-1) represent s
566 // tokens f..f2 represent s2
567 // DIFFERENCE: keep only the tokens from s that are not in s2
569 for(i = 0; i < f; ++i) {
570 for(j = f; j < f2; ++j)
571 if(argv(i) == argv(j))
572 goto skip_difference;
573 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 == "shuffle") {
583 f = tokenize_sane(s);
585 for(i = 0; i < f - 1; ++i) {
586 // move a random item from i..f-1 to position i
588 f2 = floor(random() * (f - i) + i);
589 for(j = 0; j < i; ++j)
590 s = strcat(s, " ", argv(j));
591 s = strcat(s, " ", argv(f2));
592 for(j = i; j < f; ++j)
594 s = strcat(s, " ", argv(j));
595 f = tokenize_sane(s);
598 if(substring(s, 0, 1) == " ")
599 s = substring(s, 1, 99999);
601 tokenize_sane(command);
602 } else if(rpncmd == "fexists_assert") {
606 f = fopen(s, FILE_READ);
610 print("rpn: ERROR: ", s, " does not exist!\n");
615 rpn_push(cvar_string(rpncmd));
623 print("rpn: still on stack: ", s, "\n");
627 } else if(argv(0) == "addtolist") {
632 if(cvar_string(s) == "")
635 n = tokenizebyseparator(cvar_string(s), " ");
636 for(i = 0; i < n; ++i)
638 return TRUE; // already in list
639 cvar_set(s, strcat(s2, " ", cvar_string(s)));
644 } else if(argv(0) == "cp") {
648 for(i = 2; i < argc; ++i)
649 s = strcat(s, " ", argv(i));
650 centerprint(unescape(s));