- add a "frustrating" flag to race maps in mapinfo so they are not playable by default
[divverent/nexuiz.git] / data / 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, x;
104         string s, s3;
105
106         x = 0;
107         for(i = 0; i < 16; ++i)
108         {
109                 for(j = 0; j < 16; ++j)
110                 {
111                         print(chr2str(x));
112                         ++x;
113                 }
114                 print("\n");
115         }
116
117         GameCommand_MarkupInit();
118
119         s = "";
120
121         red = 0;
122         ccase = 0;
123         for(i = 0; i < strlen(s2); ++i)
124         {
125                 for(j = 0; j < NUM_MARKUPS; ++j)
126                 {
127                         s3 = substring(s2, i, strlen(markup_from[j]));
128                         if (s3 == markup_from[j])
129                         {
130                                 s = strcat(s, markup_to[j]);
131                                 i += strlen(markup_from[j]) - 1;
132                                 break;
133                         }
134                 }
135
136                 if(j == NUM_MARKUPS)
137                 {
138                         if(substring(s2, i, 2) == "&&")
139                         {
140                                 s = strcat(s, strconv(ccase, red, red, "&"));
141                                 ++i;
142                         }
143                         else if(substring(s2, i, 2) == "&d")
144                         {
145                                 red = 2;
146                                 ccase = 0;
147                                 ++i;
148                         }
149                         else if(substring(s2, i, 2) == "&a")
150                         {
151                                 red = 2;
152                                 ccase = 2;
153                                 ++i;
154                         }
155                         else if(substring(s2, i, 2) == "&n")
156                         {
157                                 red = 0;
158                                 ccase = 0;
159                                 ++i;
160                         }
161                         else
162                                 s = strcat(s, strconv(ccase, red, red, substring(s2, i, 1)));
163                 }
164         }
165
166         return s;
167 }
168
169 float GameCommand_Generic(string command)
170 {
171         float argc;
172         float i, j, f, n;
173         vector rgb;
174         string s, s2, c;
175         argc = tokenize_console(command);
176         if(argv(0) == "help")
177         {
178                 print("  rpn EXPRESSION... - a RPN calculator.\n");
179                 print("    Operator description (x: string, s: set, f: float):\n");
180                 print("    x pop ----------------------------->     : removes the top\n");
181                 print("    x dup -----------------------------> x x : duplicates the top\n");
182                 print("    x x exch --------------------------> x x : swap the top two\n");
183                 print("    /cvarname load --------------------> x   : loads a cvar\n");
184                 print("    /cvarname x def ------------------->     : writes to a cvar\n");
185                 print("    f f add|sub|mul|div|mod|max|min ---> f   : adds/... two numbers\n");
186                 print("    f f eq|ne|gt|ge|lt|le -------------> f   : compares two numbers\n");
187                 print("    f neg|abs|sgn|rand|floor|ceil------> f   : negates/... a number\n");
188                 print("    f f f bound -----------------------> f   : bounds the middle number\n");
189                 print("    f1 f2 b when ----------------------> f   : f1 if b, f2 otherwise\n");
190                 print("    s s union|intersection|difference -> s   : set operations\n");
191                 print("    s shuffle -------------------------> s   : randomly arrange elements\n");
192                 print("    x dbpush -------------------------->     : pushes the top onto the database\n");
193                 print("    dbpop|dbget -----------------------> x   : removes/reads DB's top\n");
194                 print("    dblen|dbat ------------------------> f   : gets the DB's size/cursor pos\n");
195                 print("    dbclr ----------------------------->     : clear the DB\n");
196                 print("    s dbsave|dbload-------------------->     : save/load the DB to/from a file\n");
197                 print("    x dbins --------------------------->     : moves the top into the DB\n");
198                 print("    dbext|dbread ----------------------> x   : extract/get from the DB's cursor\n");
199                 print("    f dbmov|dbgoto -------------------->     : move or set the DB's cursor\n");
200                 print("    s localtime -----------------------> s   : formats the current local time\n");
201                 print("    s gmtime --------------------------> s   : formats the current UTC time\n");
202                 print("    time ------------------------------> f   : seconds since VM start\n");
203                 print("    Set operations operate on 'such''strings'.\n");
204                 print("    Unknown tokens insert their cvar value.\n");
205                 print("  maplist add map\n");
206                 print("  maplist remove map\n");
207                 print("  maplist shuffle\n");
208                 print("  maplist cleanup\n");
209                 print("  maplist maplist\n");
210                 print("  maplist lsmaps\n");
211                 print("  addtolist variable addedvalue\n");
212                 print("  records\n");
213                 return TRUE;
214         }
215         
216         if(argv(0) == "maplist")
217         {
218                 if(argv(1) == "add" && argc == 3)
219                 {
220                         f = fopen(strcat("maps/", argv(2), ".bsp"), FILE_READ);
221                         if(f != -1)
222                                 fclose(f);
223                         else {
224                                 print("maplist: ERROR: ", argv(2), " does not exist!\n");
225                                 return TRUE;
226                         }
227                         if(cvar_string("g_maplist") == "")
228                                 cvar_set("g_maplist", argv(2));
229                         else
230                                 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
231                         return TRUE;
232                 }
233                 else if(argv(1) == "remove" && argc == 3)
234                 {
235                         s = argv(2);
236                         n = tokenizebyseparator(cvar_string("g_maplist"), " ");
237                         s2 = "";
238                         for(i = 0; i < n; ++i)
239                                 if(argv(i) != s)
240                                         s2 = strcat(s2, " ", argv(i));
241                         s2 = substring(s2, 1, strlen(s2) - 1);
242                         cvar_set("g_maplist", s2);
243                         return TRUE;
244                 }
245                 else if(argv(1) == "shuffle" && argc == 2)
246                 {
247                         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
248                         return TRUE;
249                 }
250                 else if(argv(1) == "cleanup")
251                 {
252                         MapInfo_Enumerate();
253                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
254                         n = tokenizebyseparator(cvar_string("g_maplist"), " ");
255                         s2 = "";
256                         for(i = 0; i < n; ++i)
257                                 if(MapInfo_CheckMap(argv(i)))
258                                         s2 = strcat(s2, " ", argv(i));
259                         s2 = substring(s2, 1, strlen(s2) - 1);
260                         cvar_set("g_maplist", s2);
261                         return TRUE;
262                 }
263                 else if(argv(1) == "maplist") {
264                         print(maplist_reply);
265                         return TRUE;
266                 }
267                 else if(argv(1) == "lsmaps") {
268                         print(lsmaps_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))) == 55970)
319         {
320                 // test case for terrencehill'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 #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                                 } else if(rpncmd == "defs" || rpncmd == "@") {
396                                         s = "";
397                                         i = rpn_popf();
398                                         j = (i == 0);
399                                         while(rpn_sp > 1 && (j || i > 0))
400                                         {
401                                                 s = strcat("/", rpn_pop(), " ", s);
402                                                 --i;
403                                         }
404                                         s2 = rpn_pop();
405 #ifdef MENUQC
406                                         registercvar(s2, "", 0);
407 #else
408                                         registercvar(s2, "");
409 #endif
410                                         if(!rpn_error) // don't change cvars if a stack error had happened!
411                                                 cvar_set(s2, s);
412                                 } else if(rpncmd == "load") {
413                                         rpn_set(cvar_string(rpn_get()));
414                                 } else if(rpncmd == "exch") {
415                                         s = rpn_pop();
416                                         s2 = rpn_get();
417                                         rpn_set(s);
418                                         rpn_push(s2);
419                                 } else if(rpncmd == "dup") {
420                                         rpn_push(rpn_get());
421                                 } else if(rpncmd == "pop") {
422                                         rpn_pop();
423                                 } else if(rpncmd == "add" || rpncmd == "+") {
424                                         f = rpn_popf();
425                                         rpn_setf(rpn_getf() + f);
426                                 } else if(rpncmd == "sub" || rpncmd == "-") {
427                                         f = rpn_popf();
428                                         rpn_setf(rpn_getf() - f);
429                                 } else if(rpncmd == "mul" || rpncmd == "*") {
430                                         f = rpn_popf();
431                                         rpn_setf(rpn_getf() * f);
432                                 } else if(rpncmd == "div" || rpncmd == "/") {
433                                         f = rpn_popf();
434                                         rpn_setf(rpn_getf() / f);
435                                 } else if(rpncmd == "mod" || rpncmd == "%") {
436                                         f = rpn_popf();
437                                         f2 = rpn_getf();
438                                         rpn_setf(f2 - f * floor(f2 / f));
439                                 } else if(rpncmd == "abs") {
440                                         rpn_setf(fabs(rpn_getf()));
441                                 } else if(rpncmd == "sgn") {
442                                         f = rpn_getf();
443                                         if(f < 0)
444                                                 rpn_set("-1");
445                                         else if(f > 0)
446                                                 rpn_set("1");
447                                         else
448                                                 rpn_set("0");
449                                 } else if(rpncmd == "neg" || rpncmd == "~") {
450                                         rpn_setf(-rpn_getf());
451                                 } else if(rpncmd == "floor" || rpncmd == "f") {
452                                         rpn_setf(floor(rpn_getf()));
453                                 } else if(rpncmd == "ceil" || rpncmd == "c") {
454                                         rpn_setf(ceil(rpn_getf()));
455                                 } else if(rpncmd == "max") {
456                                         f = rpn_popf();
457                                         f2 = rpn_getf();
458                                         rpn_setf(max(f2, f));
459                                 } else if(rpncmd == "min") {
460                                         f = rpn_popf();
461                                         f2 = rpn_getf();
462                                         rpn_setf(min(f2, f));
463                                 } else if(rpncmd == "bound") {
464                                         f = rpn_popf();
465                                         f2 = rpn_popf();
466                                         f3 = rpn_getf();
467                                         rpn_setf(bound(f3, f2, f));
468                                 } else if(rpncmd == "when") {
469                                         f = rpn_popf();
470                                         f2 = rpn_popf();
471                                         f3 = rpn_getf();
472                                         if(f)
473                                                 rpn_setf(f3);
474                                         else
475                                                 rpn_setf(f2);
476                                 } else if(rpncmd == ">" || rpncmd == "gt") {
477                                         f = rpn_popf();
478                                         rpn_setf(rpn_getf() > f);
479                                 } else if(rpncmd == "<" || rpncmd == "lt") {
480                                         f = rpn_popf();
481                                         rpn_setf(rpn_getf() < f);
482                                 } else if(rpncmd == "==" || rpncmd == "eq") {
483                                         f = rpn_popf();
484                                         rpn_setf(rpn_getf() == f);
485                                 } else if(rpncmd == ">=" || rpncmd == "ge") {
486                                         f = rpn_popf();
487                                         rpn_setf(rpn_getf() >= f);
488                                 } else if(rpncmd == "<=" || rpncmd == "le") {
489                                         f = rpn_popf();
490                                         rpn_setf(rpn_getf() <= f);
491                                 } else if(rpncmd == "!=" || rpncmd == "ne") {
492                                         f = rpn_popf();
493                                         rpn_setf(rpn_getf() != f);
494                                 } else if(rpncmd == "rand") {
495                                         rpn_setf(ceil(random() * rpn_getf()) - 1);
496                                 } else if(rpncmd == "crc16") {
497                                         rpn_setf(crc16(FALSE, rpn_get()));
498                                 } else if(rpncmd == "dbpush") {
499                                         s = rpn_pop();
500                                         if(!rpn_error)
501                                         {
502                                                 i = stof(db_get(rpn_db, "stack.pointer"));
503                                                 db_put(rpn_db, "stack.pointer", ftos(i+1));
504                                                 db_put(rpn_db, strcat("stack.", ftos(i)), s);
505                                         }
506                                         if(!i)
507                                                 db_put(rpn_db, "stack.pos", "0");
508                                 } else if(rpncmd == "dbpop") {
509                                         i = stof(db_get(rpn_db, "stack.pointer"));
510                                         if(i)
511                                         {
512                                                 s = ftos(i-1);
513                                                 db_put(rpn_db, "stack.pointer", s);
514                                                 rpn_push(db_get(rpn_db, strcat("stack.", s)));
515                                                 j = stof(db_get(rpn_db, "stack.pos"));
516                                                 if(j >= i)
517                                                         db_put(rpn_db, "stack.pos", ftos(i-2));
518                                         } else {
519                                                 rpn_error = 1;
520                                                 print("rpn: database underflow\n");
521                                         }
522                                 } else if(rpncmd == "dbget") {
523                                         
524                                         i = stof(db_get(rpn_db, "stack.pointer"));
525                                         if(i)
526                                         {
527                                                 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
528                                         } else {
529                                                 rpn_error = 1;
530                                                 print("rpn: database empty\n");
531                                         }
532                                 } else if(rpncmd == "dblen") {
533                                         rpn_push(db_get(rpn_db, "stack.pointer"));
534                                 } else if(rpncmd == "dbclr") {
535                                         db_close(rpn_db);
536                                         rpn_db = db_create();
537                                         db_put(rpn_db, "stack.pointer", "0");
538                                         db_put(rpn_db, "stack.pos", "-1");
539                                 } else if(rpncmd == "dbsave") {
540                                         s = rpn_pop();
541                                         if(!rpn_error)
542                                                 db_save(rpn_db, s);
543                                 } else if(rpncmd == "dbload") {
544                                         s = rpn_pop();
545                                         if(!rpn_error)
546                                         {
547                                                 db_close(rpn_db);
548                                                 rpn_db = db_load(s);
549                                         }
550                                 } else if(rpncmd == "dbins") {
551                                         s = rpn_pop();
552                                         if(!rpn_error)
553                                                 //if(rpn_sp > 0)
554                                         {
555                                                 j = stof(db_get(rpn_db, "stack.pointer"));
556                                                 i = stof(db_get(rpn_db, "stack.pos"));
557                                                 
558                                                 if(i < 0)
559                                                 {
560                                                         i = 0;
561                                                         db_put(rpn_db, "stack.pos", "0");
562                                                 }
563                                                 
564                                                 db_put(rpn_db, "stack.pointer", ftos(j+1));
565                                                 for(--j; j >= i; --j)
566                                                 {
567                                                         db_put(rpn_db, strcat("stack.", ftos(j+1)),
568                                                                db_get(rpn_db, (strcat("stack.", ftos(j))))
569                                                                 );
570                                                 }
571                                                 db_put(rpn_db, strcat("stack.", ftos(i)), s);
572                                         }
573                                 } else if(rpncmd == "dbext") {
574                                         j = stof(db_get(rpn_db, "stack.pointer"));
575                                         i = stof(db_get(rpn_db, "stack.pos"));
576                                         if(!j)
577                                         {
578                                                 rpn_error = TRUE;
579                                                 print("rpn: empty database\n");
580                                         } else {
581                                                 --j;
582                                                 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
583                                                 db_put(rpn_db, "stack.pointer", ftos(j));
584                                                 if(i == j)
585                                                 {
586                                                         db_put(rpn_db, "stack.pos", ftos(j-1));
587                                                 } else {
588                                                         while(i < j)
589                                                         {
590                                                                 db_put(rpn_db, strcat("stack.", ftos(i)),
591                                                                        db_get(rpn_db, (strcat("stack.", ftos(i+1))))
592                                                                         );
593                                                                 ++i;
594                                                         }
595                                                 }
596                                         }
597                                 } else if(rpncmd == "dbread") {
598                                         s = db_get(rpn_db, "stack.pos");
599                                         if(stof(s) >= 0)
600                                         {
601                                                 rpn_push(db_get(rpn_db, strcat("stack.", s)));
602                                         } else {
603                                                 rpn_error = 1;
604                                                 print("rpn: empty database\n");
605                                         }
606                                 } else if(rpncmd == "dbat") {
607                                         rpn_push(db_get(rpn_db, "stack.pos"));
608                                 } else if(rpncmd == "dbmov") {
609                                         j = stof(db_get(rpn_db, "stack.pointer"));
610                                         i = stof(db_get(rpn_db, "stack.pos"));
611                                         i += rpn_popf();
612                                         if(!rpn_error)
613                                         {
614                                                 if(i < 0 || i >= j)
615                                                 {
616                                                         print("rpn: database cursor out of bounds\n");
617                                                         rpn_error = TRUE;
618                                                 }
619                                                 if(!rpn_error)
620                                                 {
621                                                         db_put(rpn_db, "stack.pos", ftos(i));
622                                                 }
623                                         }
624                                 } else if(rpncmd == "dbgoto") {
625                                         s = rpn_pop();
626                                         j = stof(db_get(rpn_db, "stack.pointer"));
627                                         if(!j)
628                                         {
629                                                 rpn_error = TRUE;
630                                                 print("rpn: empty database, cannot move cursor\n");
631                                         }
632                                         if(!rpn_error)
633                                         {
634                                                 if(s == "end")
635                                                         i = stof(db_get(rpn_db, "stack.pointer"))-1;
636                                                 else if(s == "beg")
637                                                         i = 0;
638                                                 else
639                                                         i = stof(s);
640                                                 
641                                                 j = stof(db_get(rpn_db, "stack.pointer"));
642                                                 if(i < 0 || i >= j)
643                                                 {
644                                                         print("rpn: database cursor destination out of bounds\n");
645                                                         rpn_error = TRUE;
646                                                 }
647                                                 if(!rpn_error)
648                                                 {
649                                                         db_put(rpn_db, "stack.pos", ftos(i));
650                                                 }
651                                         }
652                                 } else if(rpncmd == "union") {
653                                         // s s2 union
654                                         s2 = rpn_pop();
655                                         s = rpn_get();
656                                         f = tokenize_console(s);
657                                         f2 = tokenize_console(strcat(s, " ", s2));
658                                         // tokens 0..(f-1) represent s
659                                         // tokens f..f2 represent s2
660                                         // UNION: add all tokens to s that are in s2 but not in s
661                                         s = "";
662                                         for(i = 0; i < f; ++i)  
663                                                 s = strcat(s, " ", argv(i));
664                                         for(i = f; i < f2; ++i) {
665                                                 for(j = 0; j < f; ++j)
666                                                         if(argv(i) == argv(j))
667                                                                 goto skip_union;
668                                                 s = strcat(s, " ", argv(i));
669 :skip_union
670                                         }
671                                         if(substring(s, 0, 1) == " ")
672                                                 s = substring(s, 1, 99999);
673                                         rpn_set(s);
674                                         tokenize_console(command);
675                                 } else if(rpncmd == "intersection") {
676                                         // s s2 intersection
677                                         s2 = rpn_pop();
678                                         s = rpn_get();
679                                         f = tokenize_console(s);
680                                         f2 = tokenize_console(strcat(s, " ", s2));
681                                         // tokens 0..(f-1) represent s
682                                         // tokens f..f2 represent s2
683                                         // INTERSECTION: keep only the tokens from s that are also in s2
684                                         s = "";
685                                         for(i = 0; i < f; ++i) {
686                                                 for(j = f; j < f2; ++j)
687                                                         if(argv(i) == argv(j))
688                                                         {
689                                                                 s = strcat(s, " ", argv(i));
690                                                                 break;
691                                                         }
692                                         }
693                                         if(substring(s, 0, 1) == " ")
694                                                 s = substring(s, 1, 99999);
695                                         rpn_set(s);
696                                         tokenize_console(command);
697                                 } else if(rpncmd == "difference") {
698                                         // s s2 difference
699                                         s2 = rpn_pop();
700                                         s = rpn_get();
701                                         f = tokenize_console(s);
702                                         f2 = tokenize_console(strcat(s, " ", s2));
703                                         // tokens 0..(f-1) represent s
704                                         // tokens f..f2 represent s2
705                                         // DIFFERENCE: keep only the tokens from s that are not in s2
706                                         s = "";
707                                         for(i = 0; i < f; ++i) {
708                                                 for(j = f; j < f2; ++j)
709                                                         if(argv(i) == argv(j))
710                                                                 goto skip_difference;
711                                                 s = strcat(s, " ", argv(i));
712 :skip_difference
713                                         }
714                                         if(substring(s, 0, 1) == " ")
715                                                 s = substring(s, 1, 99999);
716                                         rpn_set(s);
717                                         tokenize_console(command);
718                                 } else if(rpncmd == "shuffle") {
719                                         // s shuffle
720                                         s = rpn_get();
721                                         f = tokenize_console(s);
722
723                                         for(i = 0; i < f - 1; ++i) {
724                                                 // move a random item from i..f-1 to position i
725                                                 s = "";
726                                                 f2 = floor(random() * (f - i) + i);
727                                                 for(j = 0; j < i; ++j)
728                                                         s = strcat(s, " ", argv(j));
729                                                 s = strcat(s, " ", argv(f2));
730                                                 for(j = i; j < f; ++j)
731                                                         if(j != f2)
732                                                                 s = strcat(s, " ", argv(j));
733                                                 f = tokenize_console(s);
734                                         }
735
736                                         if(substring(s, 0, 1) == " ")
737                                                 s = substring(s, 1, 99999);
738                                         rpn_set(s);
739                                         tokenize_console(command);
740                                 } else if(rpncmd == "fexists_assert") {
741                                         s = rpn_pop();
742                                         if(!rpn_error)
743                                         {
744                                                 f = fopen(s, FILE_READ);
745                                                 if(f != -1)
746                                                         fclose(f);
747                                                 else {
748                                                         print("rpn: ERROR: ", s, " does not exist!\n");
749                                                         rpn_error = TRUE;
750                                                 }
751                                         }
752                                 } else if(rpncmd == "fexists") {
753                                         s = rpn_get();
754                                         if(!rpn_error)
755                                         {
756                                                 f = fopen(s, FILE_READ);
757                                                 if(f != -1) {
758                                                         fclose(f);
759                                                         rpn_setf(1);
760                                                 } else {
761                                                         rpn_setf(0);
762                                                 }
763                                         }
764                                 } else if(rpncmd == "localtime") {
765                                         rpn_set(strftime(TRUE, rpn_get()));
766                                 } else if(rpncmd == "gmtime") {
767                                         rpn_set(strftime(FALSE, rpn_get()));
768                                 } else if(rpncmd == "time") {
769                                         rpn_pushf(time);
770                                 } else {
771                                         rpn_push(cvar_string(rpncmd));
772                                 }
773                                 if(rpn_error)
774                                         break;
775                         }
776                         while(rpn_sp > 0)
777                         {
778                                 s = rpn_pop();
779                                 print("rpn: still on stack: ", s, "\n");
780                         }
781                         return TRUE;
782                 }
783         } else if(argv(0) == "addtolist") {
784                 if(argc >= 2)
785                 {
786                         s = argv(1);
787                         s2 = argv(2);
788                         if(cvar_string(s) == "")
789                                 cvar_set(s, s2);
790                         else {
791                                 n = tokenizebyseparator(cvar_string(s), " ");
792                                 for(i = 0; i < n; ++i)
793                                         if(argv(i) == s2)
794                                                 return TRUE; // already in list
795                                 cvar_set(s, strcat(s2, " ", cvar_string(s)));
796                         }
797                 }
798                 return TRUE;
799         }
800         else if(argv(0) == "records") {
801                 print(records_reply);
802                 return TRUE;
803 #ifdef MENUQC
804         } else if(argv(0) == "cp") {
805                 if(argc >= 2)
806                 {
807                         s = argv(1);
808                         for(i = 2; i < argc; ++i)
809                                 s = strcat(s, " ", argv(i));
810                         centerprint(unescape(s));
811                 }
812                 return TRUE;
813 #endif
814         }
815
816         return FALSE;
817 }