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);
144 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
147 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
149 n = floor(random() * 6 + 2);
152 for(i = 0; i < n; ++i)
154 s2 = strcat(s2, "AH");
158 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
164 s = strcat(s, " ", s2);
166 s = strcat(s2, " ", s);
168 s2 = substring(s, strlen(s) - 2, 2);
169 if(s2 == "AH" || s2 == "AY")
172 s = strcat(s, " ))");
175 s = substring(s, 0, strlen(s) - 1);
178 s = strconv(1, 0, 0, s);
180 localcmd(strcat(argv(1), " ", s));
184 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55970)
186 // test case for terrencehill's color codes
187 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
191 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
194 for(i = 0; i < n; ++i)
196 c = substring(s, i, 1);
203 if(substring(s, i+1, 1) == "^")
209 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
210 c = strcat(rgb_to_hexcolor(rgb), c);
215 localcmd(strcat(argv(1), " ", s2));
219 else if(argv(0) == "rpn")
223 rpn_db = db_create();
224 db_put(rpn_db, "stack.pointer", "0");
225 db_put(rpn_db, "stack.pos", "-1");
234 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
236 rpncmd = argv(rpnpos);
239 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
241 } else if(substring(rpncmd, 0, 1) == "0") {
243 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
245 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
247 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
248 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
249 } else if(rpncmd == "clear") {
251 } else if(rpncmd == "def" || rpncmd == "=") {
255 registercvar(s2, "", 0);
257 registercvar(s2, "");
259 if(!rpn_error) // don't change cvars if a stack error had happened!
261 } else if(rpncmd == "defs" || rpncmd == "@") {
265 while(rpn_sp > 1 && (j || i > 0))
267 s = strcat("/", rpn_pop(), " ", s);
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 == "load") {
279 rpn_set(cvar_string(rpn_get()));
280 } else if(rpncmd == "exch") {
285 } else if(rpncmd == "dup") {
287 } else if(rpncmd == "pop") {
289 } else if(rpncmd == "add" || rpncmd == "+") {
291 rpn_setf(rpn_getf() + f);
292 } else if(rpncmd == "sub" || rpncmd == "-") {
294 rpn_setf(rpn_getf() - f);
295 } else if(rpncmd == "mul" || rpncmd == "*") {
297 rpn_setf(rpn_getf() * f);
298 } else if(rpncmd == "div" || rpncmd == "/") {
300 rpn_setf(rpn_getf() / f);
301 } else if(rpncmd == "mod" || rpncmd == "%") {
304 rpn_setf(f2 - f * floor(f2 / f));
305 } else if(rpncmd == "abs") {
306 rpn_setf(fabs(rpn_getf()));
307 } else if(rpncmd == "sgn") {
315 } else if(rpncmd == "neg" || rpncmd == "~") {
316 rpn_setf(-rpn_getf());
317 } else if(rpncmd == "floor" || rpncmd == "f") {
318 rpn_setf(floor(rpn_getf()));
319 } else if(rpncmd == "ceil" || rpncmd == "c") {
320 rpn_setf(ceil(rpn_getf()));
321 } else if(rpncmd == "max") {
324 rpn_setf(max(f2, f));
325 } else if(rpncmd == "min") {
328 rpn_setf(min(f2, f));
329 } else if(rpncmd == "bound") {
333 rpn_setf(bound(f3, f2, f));
334 } else if(rpncmd == "when") {
342 } else if(rpncmd == ">" || rpncmd == "gt") {
344 rpn_setf(rpn_getf() > f);
345 } else if(rpncmd == "<" || rpncmd == "lt") {
347 rpn_setf(rpn_getf() < f);
348 } else if(rpncmd == "==" || rpncmd == "eq") {
350 rpn_setf(rpn_getf() == f);
351 } else if(rpncmd == ">=" || rpncmd == "ge") {
353 rpn_setf(rpn_getf() >= f);
354 } else if(rpncmd == "<=" || rpncmd == "le") {
356 rpn_setf(rpn_getf() <= f);
357 } else if(rpncmd == "!=" || rpncmd == "ne") {
359 rpn_setf(rpn_getf() != f);
360 } else if(rpncmd == "rand") {
361 rpn_setf(ceil(random() * rpn_getf()) - 1);
362 } else if(rpncmd == "crc16") {
363 rpn_setf(crc16(FALSE, rpn_get()));
364 } else if(rpncmd == "dbpush") {
368 i = stof(db_get(rpn_db, "stack.pointer"));
369 db_put(rpn_db, "stack.pointer", ftos(i+1));
370 db_put(rpn_db, strcat("stack.", ftos(i)), s);
373 db_put(rpn_db, "stack.pos", "0");
374 } else if(rpncmd == "dbpop") {
375 i = stof(db_get(rpn_db, "stack.pointer"));
379 db_put(rpn_db, "stack.pointer", s);
380 rpn_push(db_get(rpn_db, strcat("stack.", s)));
381 j = stof(db_get(rpn_db, "stack.pos"));
383 db_put(rpn_db, "stack.pos", ftos(i-2));
386 print("rpn: database underflow\n");
388 } else if(rpncmd == "dbget") {
390 i = stof(db_get(rpn_db, "stack.pointer"));
393 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
396 print("rpn: database empty\n");
398 } else if(rpncmd == "dblen") {
399 rpn_push(db_get(rpn_db, "stack.pointer"));
400 } else if(rpncmd == "dbclr") {
402 rpn_db = db_create();
403 db_put(rpn_db, "stack.pointer", "0");
404 db_put(rpn_db, "stack.pos", "-1");
405 } else if(rpncmd == "dbsave") {
409 } else if(rpncmd == "dbload") {
416 } else if(rpncmd == "dbins") {
421 j = stof(db_get(rpn_db, "stack.pointer"));
422 i = stof(db_get(rpn_db, "stack.pos"));
427 db_put(rpn_db, "stack.pos", "0");
430 db_put(rpn_db, "stack.pointer", ftos(j+1));
431 for(--j; j >= i; --j)
433 db_put(rpn_db, strcat("stack.", ftos(j+1)),
434 db_get(rpn_db, (strcat("stack.", ftos(j))))
437 db_put(rpn_db, strcat("stack.", ftos(i)), s);
439 } else if(rpncmd == "dbext") {
440 j = stof(db_get(rpn_db, "stack.pointer"));
441 i = stof(db_get(rpn_db, "stack.pos"));
445 print("rpn: empty database\n");
448 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
449 db_put(rpn_db, "stack.pointer", ftos(j));
452 db_put(rpn_db, "stack.pos", ftos(j-1));
456 db_put(rpn_db, strcat("stack.", ftos(i)),
457 db_get(rpn_db, (strcat("stack.", ftos(i+1))))
463 } else if(rpncmd == "dbread") {
464 s = db_get(rpn_db, "stack.pos");
467 rpn_push(db_get(rpn_db, strcat("stack.", s)));
470 print("rpn: empty database\n");
472 } else if(rpncmd == "dbat") {
473 rpn_push(db_get(rpn_db, "stack.pos"));
474 } else if(rpncmd == "dbmov") {
475 j = stof(db_get(rpn_db, "stack.pointer"));
476 i = stof(db_get(rpn_db, "stack.pos"));
482 print("rpn: database cursor out of bounds\n");
487 db_put(rpn_db, "stack.pos", ftos(i));
490 } else if(rpncmd == "dbgoto") {
492 j = stof(db_get(rpn_db, "stack.pointer"));
496 print("rpn: empty database, cannot move cursor\n");
501 i = stof(db_get(rpn_db, "stack.pointer"))-1;
507 j = stof(db_get(rpn_db, "stack.pointer"));
510 print("rpn: database cursor destination out of bounds\n");
515 db_put(rpn_db, "stack.pos", ftos(i));
518 } else if(rpncmd == "union") {
522 f = tokenize_sane(s);
523 f2 = tokenize_sane(strcat(s, " ", s2));
524 // tokens 0..(f-1) represent s
525 // tokens f..f2 represent s2
526 // UNION: add all tokens to s that are in s2 but not in s
528 for(i = 0; i < f; ++i)
529 s = strcat(s, " ", argv(i));
530 for(i = f; i < f2; ++i) {
531 for(j = 0; j < f; ++j)
532 if(argv(i) == argv(j))
534 s = strcat(s, " ", argv(i));
537 if(substring(s, 0, 1) == " ")
538 s = substring(s, 1, 99999);
540 tokenize_sane(command);
541 } else if(rpncmd == "intersection") {
545 f = tokenize_sane(s);
546 f2 = tokenize_sane(strcat(s, " ", s2));
547 // tokens 0..(f-1) represent s
548 // tokens f..f2 represent s2
549 // INTERSECTION: keep only the tokens from s that are also in s2
551 for(i = 0; i < f; ++i) {
552 for(j = f; j < f2; ++j)
553 if(argv(i) == argv(j))
555 s = strcat(s, " ", argv(i));
559 if(substring(s, 0, 1) == " ")
560 s = substring(s, 1, 99999);
562 tokenize_sane(command);
563 } else if(rpncmd == "difference") {
567 f = tokenize_sane(s);
568 f2 = tokenize_sane(strcat(s, " ", s2));
569 // tokens 0..(f-1) represent s
570 // tokens f..f2 represent s2
571 // DIFFERENCE: keep only the tokens from s that are not in s2
573 for(i = 0; i < f; ++i) {
574 for(j = f; j < f2; ++j)
575 if(argv(i) == argv(j))
576 goto skip_difference;
577 s = strcat(s, " ", argv(i));
580 if(substring(s, 0, 1) == " ")
581 s = substring(s, 1, 99999);
583 tokenize_sane(command);
584 } else if(rpncmd == "shuffle") {
587 f = tokenize_sane(s);
589 for(i = 0; i < f - 1; ++i) {
590 // move a random item from i..f-1 to position i
592 f2 = floor(random() * (f - i) + i);
593 for(j = 0; j < i; ++j)
594 s = strcat(s, " ", argv(j));
595 s = strcat(s, " ", argv(f2));
596 for(j = i; j < f; ++j)
598 s = strcat(s, " ", argv(j));
599 f = tokenize_sane(s);
602 if(substring(s, 0, 1) == " ")
603 s = substring(s, 1, 99999);
605 tokenize_sane(command);
606 } else if(rpncmd == "fexists_assert") {
610 f = fopen(s, FILE_READ);
614 print("rpn: ERROR: ", s, " does not exist!\n");
619 rpn_push(cvar_string(rpncmd));
627 print("rpn: still on stack: ", s, "\n");
632 } else if(argv(0) == "cp") {
636 for(i = 2; i < argc; ++i)
637 s = strcat(s, " ", argv(i));
638 centerprint(unescape(s));