Move fexists from server/miscfunctions.qc to common/util.qc so that it's available...
[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("    Set operations operate on 'such''strings'.\n");
195                 print("    Unknown tokens insert their cvar value.\n");
196                 print("  maplist add map\n");
197                 print("  maplist remove map\n");
198                 print("  maplist shuffle\n");
199                 print("  maplist cleanup\n");
200                 print("  maplist maplist\n");
201                 print("  maplist lsmaps\n");
202                 print("  maplist lsnewmaps\n");
203                 print("  addtolist variable addedvalue\n");
204                 print("  records\n");
205                 print("  rankings (map argument optional)\n");
206                 return TRUE;
207         }
208         
209         if(argv(0) == "maplist")
210         {
211                 if(argv(1) == "add" && argc == 3)
212                 {
213                         if (!fexists(strcat("maps/", argv(2), ".bsp")))
214                         {
215                                 print("maplist: ERROR: ", argv(2), " does not exist!\n");
216                                 return TRUE;
217                         }
218                         if(cvar_string("g_maplist") == "")
219                                 cvar_set("g_maplist", argv(2));
220                         else
221                                 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
222                         return TRUE;
223                 }
224                 else if(argv(1) == "remove" && argc == 3)
225                 {
226                         s = argv(2);
227                         n = tokenizebyseparator(cvar_string("g_maplist"), " ");
228                         s2 = "";
229                         for(i = 0; i < n; ++i)
230                                 if(argv(i) != s)
231                                         s2 = strcat(s2, " ", argv(i));
232                         s2 = substring(s2, 1, strlen(s2) - 1);
233                         cvar_set("g_maplist", s2);
234                         return TRUE;
235                 }
236                 else if(argv(1) == "shuffle" && argc == 2)
237                 {
238                         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
239                         return TRUE;
240                 }
241                 else if(argv(1) == "cleanup")
242                 {
243                         MapInfo_Enumerate();
244                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
245                         n = tokenizebyseparator(cvar_string("g_maplist"), " ");
246                         s2 = "";
247                         for(i = 0; i < n; ++i)
248                                 if(MapInfo_CheckMap(argv(i)))
249                                         s2 = strcat(s2, " ", argv(i));
250                         s2 = substring(s2, 1, strlen(s2) - 1);
251                         cvar_set("g_maplist", s2);
252                         return TRUE;
253                 }
254                 else if(argv(1) == "maplist") {
255                         print(maplist_reply);
256                         return TRUE;
257                 }
258                 else if(argv(1) == "lsmaps") {
259                         print(lsmaps_reply);
260                         return TRUE;
261                 }
262                 else if(argv(1) == "lsnewmaps") {
263                         print(lsnewmaps_reply);
264                         return TRUE;
265                 }
266         }
267         else if(argc >= 3 && argv(0) == "red")
268         {
269                 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
270                 localcmd(strcat(argv(1), " ", GameCommand_Markup(s)));
271                 return TRUE;
272         }
273         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
274         {
275                 // other test case
276                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
277
278                 n = floor(random() * 6 + 2);
279
280                 s2 = "";
281                 for(i = 0; i < n; ++i)
282                 {
283                         s2 = strcat(s2, "AH");
284                 }
285
286                 if(random() < 0.1)
287                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
288
289                 if(s == "")
290                         s = s2;
291                 else
292                         if(random() < 0.8)
293                                 s = strcat(s, " ", s2);
294                         else
295                                 s = strcat(s2, " ", s);
296
297                 s2 = substring(s, strlen(s) - 2, 2);
298                 if(s2 == "AH" || s2 == "AY")
299                         s = strcat(s, "))");
300                 else
301                         s = strcat(s, " ))");
302
303                 if(random() < 0.1)
304                         s = substring(s, 0, strlen(s) - 1);
305
306                 if(random() < 0.1)
307                         s = strconv(1, 0, 0, s);
308
309                 localcmd(strcat(argv(1), " ", s));
310
311                 return TRUE;
312         }
313         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
314         {
315                 // test case for terencehill's color codes
316                 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
317                 s2 = "";
318                 
319                 n = strlen(s);
320                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
321                 f = random() * 6;
322
323                 for(i = 0; i < n; ++i)
324                 {
325                         c = substring(s, i, 1);
326
327                         if(c == ";")
328                                 c = ":";
329                         else if(c == "^")
330                         {
331                                 c = "^^";
332                                 if(substring(s, i+1, 1) == "^")
333                                         ++i;
334                         }
335
336                         if(c != " ")
337                         {
338                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
339                                 c = strcat(rgb_to_hexcolor(rgb), c);
340                         }
341                         s2 = strcat(s2, c);
342                 }
343
344                 localcmd(strcat(argv(1), " ", s2));
345
346                 return TRUE;
347         }
348         else if(argv(0) == "rpn")
349         {
350                 if(!rpn_db)
351                 {
352                         rpn_db = db_create();
353                         db_put(rpn_db, "stack.pointer", "0");
354                         db_put(rpn_db, "stack.pos", "-1");
355                 }
356                 if(argc >= 2)
357                 {
358                         float rpnpos;
359                         string rpncmd;
360                         float f2, f3;
361                         rpn_sp = 0;
362                         rpn_error = FALSE;
363                         for(rpnpos = 1; rpnpos < argc; ++rpnpos)
364                         {
365                                 rpncmd = argv(rpnpos);
366                                 f = strlen(rpncmd);
367                                 if(rpncmd == "") {
368                                 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
369                                         rpn_push(rpncmd);
370                                 } else if(substring(rpncmd, 0, 1) == "0") {
371                                         rpn_push(rpncmd);
372                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
373                                         rpn_push(rpncmd);
374                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
375                                         rpn_push(rpncmd);
376                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
377                                         rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
378                                 } else if(rpncmd == "clear") {
379                                         rpn_sp = 0;
380                                 } else if(rpncmd == "def" || rpncmd == "=") {
381                                         s = rpn_pop();
382                                         s2 = rpn_pop();
383
384                                         if(s2 != "")
385                                         {
386 #ifdef MENUQC
387                                                 registercvar(s2, "", 0);
388 #else
389                                                 registercvar(s2, "");
390 #endif
391                                                 if(!rpn_error) // don't change cvars if a stack error had happened!
392                                                         cvar_set(s2, s);
393                                         }
394                                         else
395                                         {
396                                                 print("rpn: empty cvar name for 'def'\n");
397                                                 rpn_error = TRUE;
398                                         }
399                                 } else if(rpncmd == "defs" || rpncmd == "@") {
400                                         s = "";
401                                         i = rpn_popf();
402                                         j = (i == 0);
403                                         while(rpn_sp > 1 && (j || i > 0))
404                                         {
405                                                 s = strcat("/", rpn_pop(), " ", s);
406                                                 --i;
407                                         }
408                                         s2 = rpn_pop();
409                                         if(s2 != "")
410                                         {
411 #ifdef MENUQC
412                                                 registercvar(s2, "", 0);
413 #else
414                                                 registercvar(s2, "");
415 #endif
416                                                 if(!rpn_error) // don't change cvars if a stack error had happened!
417                                                         cvar_set(s2, s);
418                                         }
419                                         else
420                                         {
421                                                 print("rpn: empty cvar name for 'defs'\n");
422                                                 rpn_error = TRUE;
423                                         }
424                                 } else if(rpncmd == "load") {
425 #ifdef SVQC
426                                         rpn_set(cvar_string_builtin(rpn_get())); // cvar name comes from user
427 #else
428                                         rpn_set(cvar_string(rpn_get()));
429 #endif
430                                 } else if(rpncmd == "exch") {
431                                         s = rpn_pop();
432                                         s2 = rpn_get();
433                                         rpn_set(s);
434                                         rpn_push(s2);
435                                 } else if(rpncmd == "dup") {
436                                         rpn_push(rpn_get());
437                                 } else if(rpncmd == "pop") {
438                                         rpn_pop();
439                                 } else if(rpncmd == "add" || rpncmd == "+") {
440                                         f = rpn_popf();
441                                         rpn_setf(rpn_getf() + f);
442                                 } else if(rpncmd == "sub" || rpncmd == "-") {
443                                         f = rpn_popf();
444                                         rpn_setf(rpn_getf() - f);
445                                 } else if(rpncmd == "mul" || rpncmd == "*") {
446                                         f = rpn_popf();
447                                         rpn_setf(rpn_getf() * f);
448                                 } else if(rpncmd == "div" || rpncmd == "/") {
449                                         f = rpn_popf();
450                                         rpn_setf(rpn_getf() / f);
451                                 } else if(rpncmd == "mod" || rpncmd == "%") {
452                                         f = rpn_popf();
453                                         f2 = rpn_getf();
454                                         rpn_setf(f2 - f * floor(f2 / f));
455                                 } else if(rpncmd == "abs") {
456                                         rpn_setf(fabs(rpn_getf()));
457                                 } else if(rpncmd == "sgn") {
458                                         f = rpn_getf();
459                                         if(f < 0)
460                                                 rpn_set("-1");
461                                         else if(f > 0)
462                                                 rpn_set("1");
463                                         else
464                                                 rpn_set("0");
465                                 } else if(rpncmd == "neg" || rpncmd == "~") {
466                                         rpn_setf(-rpn_getf());
467                                 } else if(rpncmd == "floor" || rpncmd == "f") {
468                                         rpn_setf(floor(rpn_getf()));
469                                 } else if(rpncmd == "ceil" || rpncmd == "c") {
470                                         rpn_setf(ceil(rpn_getf()));
471                                 } else if(rpncmd == "max") {
472                                         f = rpn_popf();
473                                         f2 = rpn_getf();
474                                         rpn_setf(max(f2, f));
475                                 } else if(rpncmd == "min") {
476                                         f = rpn_popf();
477                                         f2 = rpn_getf();
478                                         rpn_setf(min(f2, f));
479                                 } else if(rpncmd == "bound") {
480                                         f = rpn_popf();
481                                         f2 = rpn_popf();
482                                         f3 = rpn_getf();
483                                         rpn_setf(bound(f3, f2, f));
484                                 } else if(rpncmd == "when") {
485                                         f = rpn_popf();
486                                         f2 = rpn_popf();
487                                         f3 = rpn_getf();
488                                         if(f)
489                                                 rpn_setf(f3);
490                                         else
491                                                 rpn_setf(f2);
492                                 } else if(rpncmd == ">" || rpncmd == "gt") {
493                                         f = rpn_popf();
494                                         rpn_setf(rpn_getf() > f);
495                                 } else if(rpncmd == "<" || rpncmd == "lt") {
496                                         f = rpn_popf();
497                                         rpn_setf(rpn_getf() < f);
498                                 } else if(rpncmd == "==" || rpncmd == "eq") {
499                                         f = rpn_popf();
500                                         rpn_setf(rpn_getf() == f);
501                                 } else if(rpncmd == ">=" || rpncmd == "ge") {
502                                         f = rpn_popf();
503                                         rpn_setf(rpn_getf() >= f);
504                                 } else if(rpncmd == "<=" || rpncmd == "le") {
505                                         f = rpn_popf();
506                                         rpn_setf(rpn_getf() <= f);
507                                 } else if(rpncmd == "!=" || rpncmd == "ne") {
508                                         f = rpn_popf();
509                                         rpn_setf(rpn_getf() != f);
510                                 } else if(rpncmd == "rand") {
511                                         rpn_setf(ceil(random() * rpn_getf()) - 1);
512                                 } else if(rpncmd == "crc16") {
513                                         rpn_setf(crc16(FALSE, rpn_get()));
514                                 } else if(rpncmd == "put") {
515                                         s2 = rpn_pop();
516                                         if (!rpn_error)
517                                         {
518                                                 s = rpn_pop();
519                                                 if (!rpn_error)
520                                                         db_put(rpn_db, s, s2);
521                                         }
522                                 } else if(rpncmd == "get") {
523                                         s = rpn_pop();
524                                         if (!rpn_error)
525                                                 rpn_push(db_get(rpn_db, s));
526                                 } else if(rpncmd == "dbpush") {
527                                         s = rpn_pop();
528                                         if(!rpn_error)
529                                         {
530                                                 i = stof(db_get(rpn_db, "stack.pointer"));
531                                                 db_put(rpn_db, "stack.pointer", ftos(i+1));
532                                                 db_put(rpn_db, strcat("stack.", ftos(i)), s);
533                                         }
534                                         if(!i)
535                                                 db_put(rpn_db, "stack.pos", "0");
536                                 } else if(rpncmd == "dbpop") {
537                                         i = stof(db_get(rpn_db, "stack.pointer"));
538                                         if(i)
539                                         {
540                                                 s = ftos(i-1);
541                                                 db_put(rpn_db, "stack.pointer", s);
542                                                 rpn_push(db_get(rpn_db, strcat("stack.", s)));
543                                                 j = stof(db_get(rpn_db, "stack.pos"));
544                                                 if(j >= i)
545                                                         db_put(rpn_db, "stack.pos", ftos(i-2));
546                                         } else {
547                                                 rpn_error = 1;
548                                                 print("rpn: database underflow\n");
549                                         }
550                                 } else if(rpncmd == "dbget") {
551                                         
552                                         i = stof(db_get(rpn_db, "stack.pointer"));
553                                         if(i)
554                                         {
555                                                 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
556                                         } else {
557                                                 rpn_error = 1;
558                                                 print("rpn: database empty\n");
559                                         }
560                                 } else if(rpncmd == "dblen") {
561                                         rpn_push(db_get(rpn_db, "stack.pointer"));
562                                 } else if(rpncmd == "dbclr") {
563                                         db_close(rpn_db);
564                                         rpn_db = db_create();
565                                         db_put(rpn_db, "stack.pointer", "0");
566                                         db_put(rpn_db, "stack.pos", "-1");
567                                 } else if(rpncmd == "dbsave") {
568                                         s = rpn_pop();
569                                         if(!rpn_error)
570                                                 db_save(rpn_db, s);
571                                 } else if(rpncmd == "dbload") {
572                                         s = rpn_pop();
573                                         if(!rpn_error)
574                                         {
575                                                 db_close(rpn_db);
576                                                 rpn_db = db_load(s);
577                                         }
578                                 } else if(rpncmd == "dbins") {
579                                         s = rpn_pop();
580                                         if(!rpn_error)
581                                                 //if(rpn_sp > 0)
582                                         {
583                                                 j = stof(db_get(rpn_db, "stack.pointer"));
584                                                 i = stof(db_get(rpn_db, "stack.pos"));
585                                                 
586                                                 if(i < 0)
587                                                 {
588                                                         i = 0;
589                                                         db_put(rpn_db, "stack.pos", "0");
590                                                 }
591                                                 
592                                                 db_put(rpn_db, "stack.pointer", ftos(j+1));
593                                                 for(--j; j >= i; --j)
594                                                 {
595                                                         db_put(rpn_db, strcat("stack.", ftos(j+1)),
596                                                                db_get(rpn_db, (strcat("stack.", ftos(j))))
597                                                                 );
598                                                 }
599                                                 db_put(rpn_db, strcat("stack.", ftos(i)), s);
600                                         }
601                                 } else if(rpncmd == "dbext") {
602                                         j = stof(db_get(rpn_db, "stack.pointer"));
603                                         i = stof(db_get(rpn_db, "stack.pos"));
604                                         if(!j)
605                                         {
606                                                 rpn_error = TRUE;
607                                                 print("rpn: empty database\n");
608                                         } else {
609                                                 --j;
610                                                 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
611                                                 db_put(rpn_db, "stack.pointer", ftos(j));
612                                                 if(i == j)
613                                                 {
614                                                         db_put(rpn_db, "stack.pos", ftos(j-1));
615                                                 } else {
616                                                         while(i < j)
617                                                         {
618                                                                 db_put(rpn_db, strcat("stack.", ftos(i)),
619                                                                        db_get(rpn_db, (strcat("stack.", ftos(i+1))))
620                                                                         );
621                                                                 ++i;
622                                                         }
623                                                 }
624                                         }
625                                 } else if(rpncmd == "dbread") {
626                                         s = db_get(rpn_db, "stack.pos");
627                                         if(stof(s) >= 0)
628                                         {
629                                                 rpn_push(db_get(rpn_db, strcat("stack.", s)));
630                                         } else {
631                                                 rpn_error = 1;
632                                                 print("rpn: empty database\n");
633                                         }
634                                 } else if(rpncmd == "dbat") {
635                                         rpn_push(db_get(rpn_db, "stack.pos"));
636                                 } else if(rpncmd == "dbmov") {
637                                         j = stof(db_get(rpn_db, "stack.pointer"));
638                                         i = stof(db_get(rpn_db, "stack.pos"));
639                                         i += rpn_popf();
640                                         if(!rpn_error)
641                                         {
642                                                 if(i < 0 || i >= j)
643                                                 {
644                                                         print("rpn: database cursor out of bounds\n");
645                                                         rpn_error = TRUE;
646                                                 }
647                                                 if(!rpn_error)
648                                                 {
649                                                         db_put(rpn_db, "stack.pos", ftos(i));
650                                                 }
651                                         }
652                                 } else if(rpncmd == "dbgoto") {
653                                         s = rpn_pop();
654                                         j = stof(db_get(rpn_db, "stack.pointer"));
655                                         if(!j)
656                                         {
657                                                 rpn_error = TRUE;
658                                                 print("rpn: empty database, cannot move cursor\n");
659                                         }
660                                         if(!rpn_error)
661                                         {
662                                                 if(s == "end")
663                                                         i = stof(db_get(rpn_db, "stack.pointer"))-1;
664                                                 else if(s == "beg")
665                                                         i = 0;
666                                                 else
667                                                         i = stof(s);
668                                                 
669                                                 j = stof(db_get(rpn_db, "stack.pointer"));
670                                                 if(i < 0 || i >= j)
671                                                 {
672                                                         print("rpn: database cursor destination out of bounds\n");
673                                                         rpn_error = TRUE;
674                                                 }
675                                                 if(!rpn_error)
676                                                 {
677                                                         db_put(rpn_db, "stack.pos", ftos(i));
678                                                 }
679                                         }
680                                 } else if(rpncmd == "union") {
681                                         // s s2 union
682                                         s2 = rpn_pop();
683                                         s = rpn_get();
684                                         f = tokenize_console(s);
685                                         f2 = tokenize_console(strcat(s, " ", s2));
686                                         // tokens 0..(f-1) represent s
687                                         // tokens f..f2 represent s2
688                                         // UNION: add all tokens to s that are in s2 but not in s
689                                         s = "";
690                                         for(i = 0; i < f; ++i)  
691                                                 s = strcat(s, " ", argv(i));
692                                         for(i = f; i < f2; ++i) {
693                                                 for(j = 0; j < f; ++j)
694                                                         if(argv(i) == argv(j))
695                                                                 goto skip_union;
696                                                 s = strcat(s, " ", argv(i));
697 :skip_union
698                                         }
699                                         if(substring(s, 0, 1) == " ")
700                                                 s = substring(s, 1, 99999);
701                                         rpn_set(s);
702                                         tokenize_console(command);
703                                 } else if(rpncmd == "intersection") {
704                                         // s s2 intersection
705                                         s2 = rpn_pop();
706                                         s = rpn_get();
707                                         f = tokenize_console(s);
708                                         f2 = tokenize_console(strcat(s, " ", s2));
709                                         // tokens 0..(f-1) represent s
710                                         // tokens f..f2 represent s2
711                                         // INTERSECTION: keep only the tokens from s that are also in s2
712                                         s = "";
713                                         for(i = 0; i < f; ++i) {
714                                                 for(j = f; j < f2; ++j)
715                                                         if(argv(i) == argv(j))
716                                                         {
717                                                                 s = strcat(s, " ", argv(i));
718                                                                 break;
719                                                         }
720                                         }
721                                         if(substring(s, 0, 1) == " ")
722                                                 s = substring(s, 1, 99999);
723                                         rpn_set(s);
724                                         tokenize_console(command);
725                                 } else if(rpncmd == "difference") {
726                                         // s s2 difference
727                                         s2 = rpn_pop();
728                                         s = rpn_get();
729                                         f = tokenize_console(s);
730                                         f2 = tokenize_console(strcat(s, " ", s2));
731                                         // tokens 0..(f-1) represent s
732                                         // tokens f..f2 represent s2
733                                         // DIFFERENCE: keep only the tokens from s that are not in s2
734                                         s = "";
735                                         for(i = 0; i < f; ++i) {
736                                                 for(j = f; j < f2; ++j)
737                                                         if(argv(i) == argv(j))
738                                                                 goto skip_difference;
739                                                 s = strcat(s, " ", argv(i));
740 :skip_difference
741                                         }
742                                         if(substring(s, 0, 1) == " ")
743                                                 s = substring(s, 1, 99999);
744                                         rpn_set(s);
745                                         tokenize_console(command);
746                                 } else if(rpncmd == "shuffle") {
747                                         // s shuffle
748                                         s = rpn_get();
749                                         f = tokenize_console(s);
750
751                                         for(i = 0; i < f - 1; ++i) {
752                                                 // move a random item from i..f-1 to position i
753                                                 s = "";
754                                                 f2 = floor(random() * (f - i) + i);
755                                                 for(j = 0; j < i; ++j)
756                                                         s = strcat(s, " ", argv(j));
757                                                 s = strcat(s, " ", argv(f2));
758                                                 for(j = i; j < f; ++j)
759                                                         if(j != f2)
760                                                                 s = strcat(s, " ", argv(j));
761                                                 f = tokenize_console(s);
762                                         }
763
764                                         if(substring(s, 0, 1) == " ")
765                                                 s = substring(s, 1, 99999);
766                                         rpn_set(s);
767                                         tokenize_console(command);
768                                 } else if(rpncmd == "fexists_assert") {
769                                         s = rpn_pop();
770                                         if(!rpn_error)
771                                         {
772                                                 if (!fexists(s))
773                                                 {
774                                                         print("rpn: ERROR: ", s, " does not exist!\n");
775                                                         rpn_error = TRUE;
776                                                 }
777                                         }
778                                 } else if(rpncmd == "fexists") {
779                                         s = rpn_get();
780                                         if(!rpn_error)
781                                         {
782                                                 if (fexists(s))
783                                                         rpn_setf(1);
784                                                 else
785                                                         rpn_setf(0);
786                                         }
787                                 } else if(rpncmd == "localtime") {
788                                         rpn_set(strftime(TRUE, rpn_get()));
789                                 } else if(rpncmd == "gmtime") {
790                                         rpn_set(strftime(FALSE, rpn_get()));
791                                 } else if(rpncmd == "time") {
792                                         rpn_pushf(time);
793                                 } else {
794                                         rpn_push(cvar_string(rpncmd));
795                                 }
796                                 if(rpn_error)
797                                         break;
798                         }
799                         while(rpn_sp > 0)
800                         {
801                                 s = rpn_pop();
802                                 print("rpn: still on stack: ", s, "\n");
803                         }
804                         return TRUE;
805                 }
806         } else if(argv(0) == "addtolist") {
807                 if(argc >= 2)
808                 {
809                         s = argv(1);
810                         s2 = argv(2);
811                         if(cvar_string(s) == "")
812                                 cvar_set(s, s2);
813                         else {
814                                 n = tokenizebyseparator(cvar_string(s), " ");
815                                 for(i = 0; i < n; ++i)
816                                         if(argv(i) == s2)
817                                                 return TRUE; // already in list
818                                 cvar_set(s, strcat(s2, " ", cvar_string(s)));
819                         }
820                 }
821                 return TRUE;
822         }
823         else if(argv(0) == "records") {
824                 print(records_reply);
825                 return TRUE;
826         }
827         else if(argv(0) == "ladder") {
828                 print(ladder_reply);
829                 return TRUE;
830         }
831         else if(argv(0) == "rankings") {
832                 print(rankings_reply);
833                 return TRUE;
834 #ifdef MENUQC
835         } else if(argv(0) == "cp") {
836                 if(argc >= 2)
837                 {
838                         s = argv(1);
839                         for(i = 2; i < argc; ++i)
840                                 s = strcat(s, " ", argv(i));
841                         centerprint(unescape(s));
842                 }
843                 return TRUE;
844 #endif
845         }
846
847         return FALSE;
848 }