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