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