9595ed5c298187c799ca628d402a94bd4c7882de
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamecommand.qc
1 #define MAX_RPN_STACK 16
2 float rpn_db;
3 float rpn_error;
4 float rpn_sp;
5 string rpn_stack[MAX_RPN_STACK];
6 string rpn_pop() {
7         if(rpn_sp > 0) {
8                 --rpn_sp;
9                 return rpn_stack[rpn_sp];
10         } else {
11                 print("rpn: stack underflow\n");
12                 rpn_error = TRUE;
13                 return "";
14         }
15 }
16 void rpn_push(string s) {
17         if(rpn_sp < MAX_RPN_STACK) {
18                 rpn_stack[rpn_sp] = s;
19                 ++rpn_sp;
20         } else {
21                 print("rpn: stack overflow\n");
22                 rpn_error = TRUE;
23         }
24 }
25 string rpn_get() {
26         if(rpn_sp > 0) {
27                 return rpn_stack[rpn_sp - 1];
28         } else {
29                 print("rpn: empty stack\n");
30                 rpn_error = TRUE;
31                 return "";
32         }
33 }
34 void rpn_set(string s) {
35         if(rpn_sp > 0) {
36                 rpn_stack[rpn_sp - 1] = s;
37         } else {
38                 print("rpn: empty stack\n");
39                 rpn_error = TRUE;
40         }
41 }
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)); }
46
47 #define NUM_MARKUPS     41
48 float markup_init;
49 string markup_from[NUM_MARKUPS];
50 string markup_to[NUM_MARKUPS];
51 void GameCommand_MarkupInit()
52 {
53         float i;
54         if (markup_init)
55                 return;
56         markup_init = 1;
57         i = 0;
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;
99 }
100
101 string GameCommand_Markup(string s2)
102 {
103         float red, ccase, i, j;
104         string s, s3;
105
106         GameCommand_MarkupInit();
107
108         s = "";
109
110         red = 0;
111         ccase = 0;
112         for(i = 0; i < strlen(s2); ++i)
113         {
114                 for(j = 0; j < NUM_MARKUPS; ++j)
115                 {
116                         s3 = substring(s2, i, strlen(markup_from[j]));
117                         if (s3 == markup_from[j])
118                         {
119                                 s = strcat(s, markup_to[j]);
120                                 i += strlen(markup_from[j]) - 1;
121                                 break;
122                         }
123                 }
124
125                 if(j == NUM_MARKUPS)
126                 {
127                         if(substring(s2, i, 2) == "&&")
128                         {
129                                 s = strcat(s, strconv(ccase, red, red, "&"));
130                                 ++i;
131                         }
132                         else if(substring(s2, i, 2) == "&d")
133                         {
134                                 red = 2;
135                                 ccase = 0;
136                                 ++i;
137                         }
138                         else if(substring(s2, i, 2) == "&a")
139                         {
140                                 red = 2;
141                                 ccase = 2;
142                                 ++i;
143                         }
144                         else if(substring(s2, i, 2) == "&n")
145                         {
146                                 red = 0;
147                                 ccase = 0;
148                                 ++i;
149                         }
150                         else
151                                 s = strcat(s, strconv(ccase, red, red, substring(s2, i, 1)));
152                 }
153         }
154
155         return s;
156 }
157
158 float GameCommand_Generic(string command)
159 {
160         float argc;
161         float i, j, f, n;
162         vector rgb;
163         string s, s2, c;
164         argc = tokenize_console(command);
165         if(argv(0) == "help")
166         {
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");
207                 print("  records\n");
208                 print("  rankings (map argument optional)\n");
209                 return TRUE;
210         }
211         
212         if(argv(0) == "maplist")
213         {
214                 if(argv(1) == "add" && argc == 3)
215                 {
216                         if (!fexists(strcat("maps/", argv(2), ".bsp")))
217                         {
218                                 print("maplist: ERROR: ", argv(2), " does not exist!\n");
219                                 return TRUE;
220                         }
221                         if(cvar_string("g_maplist") == "")
222                                 cvar_set("g_maplist", argv(2));
223                         else
224                                 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
225                         return TRUE;
226                 }
227                 else if(argv(1) == "remove" && argc == 3)
228                 {
229                         s = argv(2);
230                         n = tokenizebyseparator(cvar_string("g_maplist"), " ");
231                         s2 = "";
232                         for(i = 0; i < n; ++i)
233                                 if(argv(i) != s)
234                                         s2 = strcat(s2, " ", argv(i));
235                         s2 = substring(s2, 1, strlen(s2) - 1);
236                         cvar_set("g_maplist", s2);
237                         return TRUE;
238                 }
239                 else if(argv(1) == "shuffle" && argc == 2)
240                 {
241                         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
242                         return TRUE;
243                 }
244                 else if(argv(1) == "cleanup")
245                 {
246                         MapInfo_Enumerate();
247                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
248                         n = tokenizebyseparator(cvar_string("g_maplist"), " ");
249                         s2 = "";
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);
255                         return TRUE;
256                 }
257                 else if(argv(1) == "maplist") {
258                         print(maplist_reply);
259                         return TRUE;
260                 }
261                 else if(argv(1) == "lsmaps") {
262                         print(lsmaps_reply);
263                         return TRUE;
264                 }
265                 else if(argv(1) == "lsnewmaps") {
266                         print(lsnewmaps_reply);
267                         return TRUE;
268                 }
269         }
270         else if(argc >= 3 && argv(0) == "red")
271         {
272                 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
273                 localcmd(strcat(argv(1), " ", GameCommand_Markup(s)));
274                 return TRUE;
275         }
276         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
277         {
278                 // other test case
279                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
280
281                 n = floor(random() * 6 + 2);
282
283                 s2 = "";
284                 for(i = 0; i < n; ++i)
285                 {
286                         s2 = strcat(s2, "AH");
287                 }
288
289                 if(random() < 0.1)
290                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
291
292                 if(s == "")
293                         s = s2;
294                 else
295                         if(random() < 0.8)
296                                 s = strcat(s, " ", s2);
297                         else
298                                 s = strcat(s2, " ", s);
299
300                 s2 = substring(s, strlen(s) - 2, 2);
301                 if(s2 == "AH" || s2 == "AY")
302                         s = strcat(s, "))");
303                 else
304                         s = strcat(s, " ))");
305
306                 if(random() < 0.1)
307                         s = substring(s, 0, strlen(s) - 1);
308
309                 if(random() < 0.1)
310                         s = strconv(1, 0, 0, s);
311
312                 localcmd(strcat(argv(1), " ", s));
313
314                 return TRUE;
315         }
316         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
317         {
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)));
320                 s2 = "";
321                 
322                 n = strlen(s);
323                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
324                 f = random() * 6;
325
326                 for(i = 0; i < n; ++i)
327                 {
328                         c = substring(s, i, 1);
329
330                         if(c == ";")
331                                 c = ":";
332                         else if(c == "^")
333                         {
334                                 c = "^^";
335                                 if(substring(s, i+1, 1) == "^")
336                                         ++i;
337                         }
338
339                         if(c != " ")
340                         {
341                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
342                                 c = strcat(rgb_to_hexcolor(rgb), c);
343                         }
344                         s2 = strcat(s2, c);
345                 }
346
347                 localcmd(strcat(argv(1), " ", s2));
348
349                 return TRUE;
350         }
351         else if(argv(0) == "rpn")
352         {
353                 if(!rpn_db)
354                 {
355                         rpn_db = db_create();
356                         db_put(rpn_db, "stack.pointer", "0");
357                         db_put(rpn_db, "stack.pos", "-1");
358                 }
359                 if(argc >= 2)
360                 {
361                         float rpnpos;
362                         string rpncmd;
363                         float f2, f3;
364                         rpn_sp = 0;
365                         rpn_error = FALSE;
366                         for(rpnpos = 1; rpnpos < argc; ++rpnpos)
367                         {
368                                 rpncmd = argv(rpnpos);
369                                 f = strlen(rpncmd);
370                                 if(rpncmd == "") {
371                                 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
372                                         rpn_push(rpncmd);
373                                 } else if(substring(rpncmd, 0, 1) == "0") {
374                                         rpn_push(rpncmd);
375                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
376                                         rpn_push(rpncmd);
377                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
378                                         rpn_push(rpncmd);
379                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
380                                         rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
381                                 } else if(rpncmd == "clear") {
382                                         rpn_sp = 0;
383                                 } else if(rpncmd == "def" || rpncmd == "=") {
384                                         s = rpn_pop();
385                                         s2 = rpn_pop();
386
387                                         if(s2 != "")
388                                         {
389 #ifdef MENUQC
390                                                 registercvar(s2, "", 0);
391 #else
392                                                 registercvar(s2, "");
393 #endif
394                                                 if(!rpn_error) // don't change cvars if a stack error had happened!
395                                                         cvar_set(s2, s);
396                                         }
397                                         else
398                                         {
399                                                 print("rpn: empty cvar name for 'def'\n");
400                                                 rpn_error = TRUE;
401                                         }
402                                 } else if(rpncmd == "defs" || rpncmd == "@") {
403                                         s = "";
404                                         i = rpn_popf();
405                                         j = (i == 0);
406                                         while(rpn_sp > 1 && (j || i > 0))
407                                         {
408                                                 s = strcat("/", rpn_pop(), " ", s);
409                                                 --i;
410                                         }
411                                         s2 = rpn_pop();
412                                         if(s2 != "")
413                                         {
414 #ifdef MENUQC
415                                                 registercvar(s2, "", 0);
416 #else
417                                                 registercvar(s2, "");
418 #endif
419                                                 if(!rpn_error) // don't change cvars if a stack error had happened!
420                                                         cvar_set(s2, s);
421                                         }
422                                         else
423                                         {
424                                                 print("rpn: empty cvar name for 'defs'\n");
425                                                 rpn_error = TRUE;
426                                         }
427                                 } else if(rpncmd == "load") {
428                                         rpn_set(cvar_string(rpn_get()));
429                                 } else if(rpncmd == "exch") {
430                                         s = rpn_pop();
431                                         s2 = rpn_get();
432                                         rpn_set(s);
433                                         rpn_push(s2);
434                                 } else if(rpncmd == "dup") {
435                                         rpn_push(rpn_get());
436                                 } else if(rpncmd == "pop") {
437                                         rpn_pop();
438                                 } else if(rpncmd == "add" || rpncmd == "+") {
439                                         f = rpn_popf();
440                                         rpn_setf(rpn_getf() + f);
441                                 } else if(rpncmd == "sub" || rpncmd == "-") {
442                                         f = rpn_popf();
443                                         rpn_setf(rpn_getf() - f);
444                                 } else if(rpncmd == "mul" || rpncmd == "*") {
445                                         f = rpn_popf();
446                                         rpn_setf(rpn_getf() * f);
447                                 } else if(rpncmd == "div" || rpncmd == "/") {
448                                         f = rpn_popf();
449                                         rpn_setf(rpn_getf() / f);
450                                 } else if(rpncmd == "mod" || rpncmd == "%") {
451                                         f = rpn_popf();
452                                         f2 = rpn_getf();
453                                         rpn_setf(f2 - f * floor(f2 / f));
454                                 } else if(rpncmd == "abs") {
455                                         rpn_setf(fabs(rpn_getf()));
456                                 } else if(rpncmd == "sgn") {
457                                         f = rpn_getf();
458                                         if(f < 0)
459                                                 rpn_set("-1");
460                                         else if(f > 0)
461                                                 rpn_set("1");
462                                         else
463                                                 rpn_set("0");
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") {
471                                         f = rpn_popf();
472                                         f2 = rpn_getf();
473                                         rpn_setf(max(f2, f));
474                                 } else if(rpncmd == "min") {
475                                         f = rpn_popf();
476                                         f2 = rpn_getf();
477                                         rpn_setf(min(f2, f));
478                                 } else if(rpncmd == "bound") {
479                                         f = rpn_popf();
480                                         f2 = rpn_popf();
481                                         f3 = rpn_getf();
482                                         rpn_setf(bound(f3, f2, f));
483                                 } else if(rpncmd == "when") {
484                                         f = rpn_popf();
485                                         f2 = rpn_popf();
486                                         f3 = rpn_getf();
487                                         if(f)
488                                                 rpn_setf(f3);
489                                         else
490                                                 rpn_setf(f2);
491                                 } else if(rpncmd == ">" || rpncmd == "gt") {
492                                         f = rpn_popf();
493                                         rpn_setf(rpn_getf() > f);
494                                 } else if(rpncmd == "<" || rpncmd == "lt") {
495                                         f = rpn_popf();
496                                         rpn_setf(rpn_getf() < f);
497                                 } else if(rpncmd == "==" || rpncmd == "eq") {
498                                         f = rpn_popf();
499                                         rpn_setf(rpn_getf() == f);
500                                 } else if(rpncmd == ">=" || rpncmd == "ge") {
501                                         f = rpn_popf();
502                                         rpn_setf(rpn_getf() >= f);
503                                 } else if(rpncmd == "<=" || rpncmd == "le") {
504                                         f = rpn_popf();
505                                         rpn_setf(rpn_getf() <= f);
506                                 } else if(rpncmd == "!=" || rpncmd == "ne") {
507                                         f = rpn_popf();
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") {
514                                         s2 = rpn_pop();
515                                         if (!rpn_error)
516                                         {
517                                                 s = rpn_pop();
518                                                 if (!rpn_error)
519                                                         db_put(rpn_db, s, s2);
520                                         }
521                                 } else if(rpncmd == "get") {
522                                         s = rpn_pop();
523                                         if (!rpn_error)
524                                                 rpn_push(db_get(rpn_db, s));
525                                 } else if(rpncmd == "dbpush") {
526                                         s = rpn_pop();
527                                         if(!rpn_error)
528                                         {
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);
532                                         }
533                                         if(!i)
534                                                 db_put(rpn_db, "stack.pos", "0");
535                                 } else if(rpncmd == "dbpop") {
536                                         i = stof(db_get(rpn_db, "stack.pointer"));
537                                         if(i)
538                                         {
539                                                 s = ftos(i-1);
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"));
543                                                 if(j >= i)
544                                                         db_put(rpn_db, "stack.pos", ftos(i-2));
545                                         } else {
546                                                 rpn_error = 1;
547                                                 print("rpn: database underflow\n");
548                                         }
549                                 } else if(rpncmd == "dbget") {
550                                         
551                                         i = stof(db_get(rpn_db, "stack.pointer"));
552                                         if(i)
553                                         {
554                                                 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
555                                         } else {
556                                                 rpn_error = 1;
557                                                 print("rpn: database empty\n");
558                                         }
559                                 } else if(rpncmd == "dblen") {
560                                         rpn_push(db_get(rpn_db, "stack.pointer"));
561                                 } else if(rpncmd == "dbclr") {
562                                         db_close(rpn_db);
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") {
567                                         s = rpn_pop();
568                                         if(!rpn_error)
569                                                 db_save(rpn_db, s);
570                                 } else if(rpncmd == "dbload") {
571                                         s = rpn_pop();
572                                         if(!rpn_error)
573                                         {
574                                                 db_close(rpn_db);
575                                                 rpn_db = db_load(s);
576                                         }
577                                 } else if(rpncmd == "dbins") {
578                                         s = rpn_pop();
579                                         if(!rpn_error)
580                                                 //if(rpn_sp > 0)
581                                         {
582                                                 j = stof(db_get(rpn_db, "stack.pointer"));
583                                                 i = stof(db_get(rpn_db, "stack.pos"));
584                                                 
585                                                 if(i < 0)
586                                                 {
587                                                         i = 0;
588                                                         db_put(rpn_db, "stack.pos", "0");
589                                                 }
590                                                 
591                                                 db_put(rpn_db, "stack.pointer", ftos(j+1));
592                                                 for(--j; j >= i; --j)
593                                                 {
594                                                         db_put(rpn_db, strcat("stack.", ftos(j+1)),
595                                                                db_get(rpn_db, (strcat("stack.", ftos(j))))
596                                                                 );
597                                                 }
598                                                 db_put(rpn_db, strcat("stack.", ftos(i)), s);
599                                         }
600                                 } else if(rpncmd == "dbext") {
601                                         j = stof(db_get(rpn_db, "stack.pointer"));
602                                         i = stof(db_get(rpn_db, "stack.pos"));
603                                         if(!j)
604                                         {
605                                                 rpn_error = TRUE;
606                                                 print("rpn: empty database\n");
607                                         } else {
608                                                 --j;
609                                                 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
610                                                 db_put(rpn_db, "stack.pointer", ftos(j));
611                                                 if(i == j)
612                                                 {
613                                                         db_put(rpn_db, "stack.pos", ftos(j-1));
614                                                 } else {
615                                                         while(i < j)
616                                                         {
617                                                                 db_put(rpn_db, strcat("stack.", ftos(i)),
618                                                                        db_get(rpn_db, (strcat("stack.", ftos(i+1))))
619                                                                         );
620                                                                 ++i;
621                                                         }
622                                                 }
623                                         }
624                                 } else if(rpncmd == "dbread") {
625                                         s = db_get(rpn_db, "stack.pos");
626                                         if(stof(s) >= 0)
627                                         {
628                                                 rpn_push(db_get(rpn_db, strcat("stack.", s)));
629                                         } else {
630                                                 rpn_error = 1;
631                                                 print("rpn: empty database\n");
632                                         }
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"));
638                                         i += rpn_popf();
639                                         if(!rpn_error)
640                                         {
641                                                 if(i < 0 || i >= j)
642                                                 {
643                                                         print("rpn: database cursor out of bounds\n");
644                                                         rpn_error = TRUE;
645                                                 }
646                                                 if(!rpn_error)
647                                                 {
648                                                         db_put(rpn_db, "stack.pos", ftos(i));
649                                                 }
650                                         }
651                                 } else if(rpncmd == "dbgoto") {
652                                         s = rpn_pop();
653                                         j = stof(db_get(rpn_db, "stack.pointer"));
654                                         if(!j)
655                                         {
656                                                 rpn_error = TRUE;
657                                                 print("rpn: empty database, cannot move cursor\n");
658                                         }
659                                         if(!rpn_error)
660                                         {
661                                                 if(s == "end")
662                                                         i = stof(db_get(rpn_db, "stack.pointer"))-1;
663                                                 else if(s == "beg")
664                                                         i = 0;
665                                                 else
666                                                         i = stof(s);
667                                                 
668                                                 j = stof(db_get(rpn_db, "stack.pointer"));
669                                                 if(i < 0 || i >= j)
670                                                 {
671                                                         print("rpn: database cursor destination out of bounds\n");
672                                                         rpn_error = TRUE;
673                                                 }
674                                                 if(!rpn_error)
675                                                 {
676                                                         db_put(rpn_db, "stack.pos", ftos(i));
677                                                 }
678                                         }
679                                 } else if(rpncmd == "union") {
680                                         // s s2 union
681                                         s2 = rpn_pop();
682                                         s = rpn_get();
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
688                                         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))
694                                                                 goto skip_union;
695                                                 s = strcat(s, " ", argv(i));
696 :skip_union
697                                         }
698                                         if(substring(s, 0, 1) == " ")
699                                                 s = substring(s, 1, 99999);
700                                         rpn_set(s);
701                                         tokenize_console(command);
702                                 } else if(rpncmd == "intersection") {
703                                         // s s2 intersection
704                                         s2 = rpn_pop();
705                                         s = rpn_get();
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
711                                         s = "";
712                                         for(i = 0; i < f; ++i) {
713                                                 for(j = f; j < f2; ++j)
714                                                         if(argv(i) == argv(j))
715                                                         {
716                                                                 s = strcat(s, " ", argv(i));
717                                                                 break;
718                                                         }
719                                         }
720                                         if(substring(s, 0, 1) == " ")
721                                                 s = substring(s, 1, 99999);
722                                         rpn_set(s);
723                                         tokenize_console(command);
724                                 } else if(rpncmd == "difference") {
725                                         // s s2 difference
726                                         s2 = rpn_pop();
727                                         s = rpn_get();
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
733                                         s = "";
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));
739 :skip_difference
740                                         }
741                                         if(substring(s, 0, 1) == " ")
742                                                 s = substring(s, 1, 99999);
743                                         rpn_set(s);
744                                         tokenize_console(command);
745                                 } else if(rpncmd == "shuffle") {
746                                         // s shuffle
747                                         s = rpn_get();
748                                         f = tokenize_console(s);
749
750                                         for(i = 0; i < f - 1; ++i) {
751                                                 // move a random item from i..f-1 to position i
752                                                 s = "";
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)
758                                                         if(j != f2)
759                                                                 s = strcat(s, " ", argv(j));
760                                                 f = tokenize_console(s);
761                                         }
762
763                                         if(substring(s, 0, 1) == " ")
764                                                 s = substring(s, 1, 99999);
765                                         rpn_set(s);
766                                         tokenize_console(command);
767                                 } else if(rpncmd == "fexists_assert") {
768                                         s = rpn_pop();
769                                         if(!rpn_error)
770                                         {
771                                                 if (!fexists(s))
772                                                 {
773                                                         print("rpn: ERROR: ", s, " does not exist!\n");
774                                                         rpn_error = TRUE;
775                                                 }
776                                         }
777                                 } else if(rpncmd == "fexists") {
778                                         s = rpn_get();
779                                         if(!rpn_error)
780                                         {
781                                                 if (fexists(s))
782                                                         rpn_setf(1);
783                                                 else
784                                                         rpn_setf(0);
785                                         }
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") {
791                                         rpn_pushf(time);
792                                 } else if(rpncmd == "digest") {
793                                         s = rpn_pop();
794                                         rpn_set(digest_hex(s, rpn_get()));
795                                 } else if(rpncmd == "sprintf1s") {
796                                         s = rpn_pop();
797                                         rpn_set(sprintf(s, rpn_get()));
798                                 } else {
799                                         rpn_push(cvar_string(rpncmd));
800                                 }
801                                 if(rpn_error)
802                                         break;
803                         }
804                         while(rpn_sp > 0)
805                         {
806                                 s = rpn_pop();
807                                 print("rpn: still on stack: ", s, "\n");
808                         }
809                         return TRUE;
810                 }
811         } else if(argv(0) == "addtolist") {
812                 if(argc >= 2)
813                 {
814                         s = argv(1);
815                         s2 = argv(2);
816                         if(cvar_string(s) == "")
817                                 cvar_set(s, s2);
818                         else {
819                                 n = tokenizebyseparator(cvar_string(s), " ");
820                                 for(i = 0; i < n; ++i)
821                                         if(argv(i) == s2)
822                                                 return TRUE; // already in list
823                                 cvar_set(s, strcat(s2, " ", cvar_string(s)));
824                         }
825                 }
826                 return TRUE;
827         }
828         else if(argv(0) == "records") {
829                 print(records_reply);
830                 return TRUE;
831         }
832         else if(argv(0) == "ladder") {
833                 print(ladder_reply);
834                 return TRUE;
835         }
836         else if(argv(0) == "rankings") {
837                 print(rankings_reply);
838                 return TRUE;
839 #ifdef MENUQC
840         } else if(argv(0) == "cp") {
841                 if(argc >= 2)
842                 {
843                         s = argv(1);
844                         for(i = 2; i < argc; ++i)
845                                 s = strcat(s, " ", argv(i));
846                         centerprint(unescape(s));
847                 }
848                 return TRUE;
849 #endif
850         }
851
852         return FALSE;
853 }