]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/gamecommand.qc
add a semicolon to the makefile
[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 string bruteforce_crc16(float crc, float len)
48 {
49         string s;
50         float i, j;
51         for(j = 0; j < 262144; ++j)
52         {
53                 s = "";
54                 for(i = 0; i < len; ++i)
55                         s = strcat(s, substring("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", floor(random() * 62), 1));
56                 if(crc16(FALSE, s) == crc)
57                         return s;
58         }
59         return "";
60 }
61
62 float GameCommand_Generic(string command)
63 {
64         float argc;
65         float i, j, f, n;
66         vector rgb;
67         string s, s2, c;
68         argc = tokenize_sane(command);
69         if(argv(0) == "help")
70         {
71                 print("  rpn EXPRESSION... - a RPN calculator.\n");
72                 print("    Operator description (x: string, s: set, f: float):\n");
73                 print("    x pop ----------------------------->     : removes the top\n");
74                 print("    x dup -----------------------------> x x : duplicates the top\n");
75                 print("    x x exch --------------------------> x x : swap the top two\n");
76                 print("    /cvarname load --------------------> x   : loads a cvar\n");
77                 print("    /cvarname x def ------------------->     : writes to a cvar\n");
78                 print("    f f add|sub|mul|div|mod|max|min ---> f   : adds/... two numbers\n");
79                 print("    f f eq|ne|gt|ge|lt|le -------------> f   : compares two numbers\n");
80                 print("    f neg|abs|sgn|rand|floor|ceil------> f   : negates/... a number\n");
81                 print("    f f f bound -----------------------> f   : bounds the middle number\n");
82                 print("    f1 f2 b when ----------------------> f   : f1 if b, f2 otherwise\n");
83                 print("    s s union|intersection|difference -> s   : set operations\n");
84                 print("    s shuffle -------------------------> s   : randomly arrange elements\n");
85                 print("    x dbpush -------------------------->     : pushes the top onto the database\n");
86                 print("    dbpop|dbget -----------------------> x   : removes/reads DB's top\n");
87                 print("    dblen|dbat ------------------------> f   : gets the DB's size/cursor pos\n");
88                 print("    dbclr ----------------------------->     : clear the DB\n");
89                 print("    s dbsave|dbload-------------------->     : save/load the DB to/from a file\n");
90                 print("    x dbins --------------------------->     : moves the top into the DB\n");
91                 print("    dbext|dbread ----------------------> x   : extract/get from the DB's cursor\n");
92                 print("    f dbmov|dbgoto -------------------->     : move or set the DB's cursor\n");
93                 print("    s localtime -----------------------> s   : formats the current local time\n");
94                 print("    s gmtime --------------------------> s   : formats the current UTC time\n");
95                 print("    time ------------------------------> f   : seconds since VM start\n");
96                 print("    Set operations operate on 'such''strings'.\n");
97                 print("    Unknown tokens insert their cvar value.\n");
98                 print("  maplist add map\n");
99                 print("  maplist remove map\n");
100                 print("  maplist shuffle\n");
101                 print("  maplist cleanup\n");
102                 print("  addtolist variable addedvalue\n");
103                 return TRUE;
104         }
105         
106         if(argv(0) == "maplist")
107         {
108                 if(argv(1) == "add" && argc == 3)
109                 {
110                         f = fopen(strcat("maps/", argv(2), ".bsp"), FILE_READ);
111                         if(f != -1)
112                                 fclose(f);
113                         else {
114                                 print("maplist: ERROR: ", argv(2), " does not exist!\n");
115                                 return TRUE;
116                         }
117                         if(cvar_string("g_maplist") == "")
118                                 cvar_set("g_maplist", argv(2));
119                         else
120                                 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
121                         return TRUE;
122                 }
123                 else if(argv(1) == "remove" && argc == 3)
124                 {
125                         s = argv(2);
126                         n = tokenizebyseparator(cvar_string("g_maplist"), " ");
127                         s2 = "";
128                         for(i = 0; i < n; ++i)
129                                 if(argv(i) != s)
130                                         s2 = strcat(s2, " ", argv(i));
131                         s2 = substring(s2, 1, strlen(s2) - 1);
132                         cvar_set("g_maplist", s2);
133                         return TRUE;
134                 }
135                 else if(argv(1) == "shuffle" && argc == 2)
136                 {
137                         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
138                         return TRUE;
139                 }
140                 else if(argv(1) == "cleanup")
141                 {
142                         MapInfo_Enumerate();
143                         if(cvar("g_maplist_allow_hidden"))
144                                 i = MAPINFO_FLAG_FORBIDDEN;
145                         else
146                                 i = MAPINFO_FLAG_HIDDEN | MAPINFO_FLAG_FORBIDDEN;
147                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0, i, 0);
148                         n = tokenizebyseparator(cvar_string("g_maplist"), " ");
149                         s2 = "";
150                         for(i = 0; i < n; ++i)
151                                 if(MapInfo_CheckMap(argv(i)))
152                                         s2 = strcat(s2, " ", argv(i));
153                         s2 = substring(s2, 1, strlen(s2) - 1);
154                         cvar_set("g_maplist", s2);
155                         return TRUE;
156                 }
157         }
158         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
159         {
160                 // other test case
161                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
162
163                 n = floor(random() * 6 + 2);
164
165                 s2 = "";
166                 for(i = 0; i < n; ++i)
167                 {
168                         s2 = strcat(s2, "AH");
169                 }
170
171                 if(random() < 0.1)
172                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
173
174                 if(s == "")
175                         s = s2;
176                 else
177                         if(random() < 0.8)
178                                 s = strcat(s, " ", s2);
179                         else
180                                 s = strcat(s2, " ", s);
181
182                 s2 = substring(s, strlen(s) - 2, 2);
183                 if(s2 == "AH" || s2 == "AY")
184                         s = strcat(s, "))");
185                 else
186                         s = strcat(s, " ))");
187
188                 if(random() < 0.1)
189                         s = substring(s, 0, strlen(s) - 1);
190
191                 if(random() < 0.1)
192                         s = strconv(1, 0, 0, s);
193
194                 localcmd(strcat(argv(1), " ", s));
195
196                 return TRUE;
197         }
198         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55970)
199         {
200                 // test case for terrencehill's color codes
201                 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
202                 s2 = "";
203                 
204                 n = strlen(s);
205                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
206                 f = random() * 6;
207
208                 for(i = 0; i < n; ++i)
209                 {
210                         c = substring(s, i, 1);
211
212                         if(c == ";")
213                                 c = ":";
214                         else if(c == "^")
215                         {
216                                 c = "^^";
217                                 if(substring(s, i+1, 1) == "^")
218                                         ++i;
219                         }
220
221                         if(c != " ")
222                         {
223                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
224                                 c = strcat(rgb_to_hexcolor(rgb), c);
225                         }
226                         s2 = strcat(s2, c);
227                 }
228
229                 localcmd(strcat(argv(1), " ", s2));
230
231                 return TRUE;
232         }
233         else if(argv(0) == "rpn")
234         {
235                 if(!rpn_db)
236                 {
237                         rpn_db = db_create();
238                         db_put(rpn_db, "stack.pointer", "0");
239                         db_put(rpn_db, "stack.pos", "-1");
240                 }
241                 if(argc >= 2)
242                 {
243                         float rpnpos;
244                         string rpncmd;
245                         float f2, f3;
246                         rpn_sp = 0;
247                         rpn_error = FALSE;
248                         for(rpnpos = 1; rpnpos < argc; ++rpnpos)
249                         {
250                                 rpncmd = argv(rpnpos);
251                                 f = strlen(rpncmd);
252                                 if(rpncmd == "") {
253                                 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
254                                         rpn_push(rpncmd);
255                                 } else if(substring(rpncmd, 0, 1) == "0") {
256                                         rpn_push(rpncmd);
257                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
258                                         rpn_push(rpncmd);
259                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
260                                         rpn_push(rpncmd);
261                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
262                                         rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
263                                 } else if(rpncmd == "clear") {
264                                         rpn_sp = 0;
265                                 } else if(rpncmd == "def" || rpncmd == "=") {
266                                         s = rpn_pop();
267                                         s2 = rpn_pop();
268 #ifdef MENUQC
269                                         registercvar(s2, "", 0);
270 #else
271                                         registercvar(s2, "");
272 #endif
273                                         if(!rpn_error) // don't change cvars if a stack error had happened!
274                                                 cvar_set(s2, s);
275                                 } else if(rpncmd == "defs" || rpncmd == "@") {
276                                         s = "";
277                                         i = rpn_popf();
278                                         j = (i == 0);
279                                         while(rpn_sp > 1 && (j || i > 0))
280                                         {
281                                                 s = strcat("/", rpn_pop(), " ", s);
282                                                 --i;
283                                         }
284                                         s2 = rpn_pop();
285 #ifdef MENUQC
286                                         registercvar(s2, "", 0);
287 #else
288                                         registercvar(s2, "");
289 #endif
290                                         if(!rpn_error) // don't change cvars if a stack error had happened!
291                                                 cvar_set(s2, s);
292                                 } else if(rpncmd == "load") {
293                                         rpn_set(cvar_string(rpn_get()));
294                                 } else if(rpncmd == "exch") {
295                                         s = rpn_pop();
296                                         s2 = rpn_get();
297                                         rpn_set(s);
298                                         rpn_push(s2);
299                                 } else if(rpncmd == "dup") {
300                                         rpn_push(rpn_get());
301                                 } else if(rpncmd == "pop") {
302                                         rpn_pop();
303                                 } else if(rpncmd == "add" || rpncmd == "+") {
304                                         f = rpn_popf();
305                                         rpn_setf(rpn_getf() + f);
306                                 } else if(rpncmd == "sub" || rpncmd == "-") {
307                                         f = rpn_popf();
308                                         rpn_setf(rpn_getf() - f);
309                                 } else if(rpncmd == "mul" || rpncmd == "*") {
310                                         f = rpn_popf();
311                                         rpn_setf(rpn_getf() * f);
312                                 } else if(rpncmd == "div" || rpncmd == "/") {
313                                         f = rpn_popf();
314                                         rpn_setf(rpn_getf() / f);
315                                 } else if(rpncmd == "mod" || rpncmd == "%") {
316                                         f = rpn_popf();
317                                         f2 = rpn_getf();
318                                         rpn_setf(f2 - f * floor(f2 / f));
319                                 } else if(rpncmd == "abs") {
320                                         rpn_setf(fabs(rpn_getf()));
321                                 } else if(rpncmd == "sgn") {
322                                         f = rpn_getf();
323                                         if(f < 0)
324                                                 rpn_set("-1");
325                                         else if(f > 0)
326                                                 rpn_set("1");
327                                         else
328                                                 rpn_set("0");
329                                 } else if(rpncmd == "neg" || rpncmd == "~") {
330                                         rpn_setf(-rpn_getf());
331                                 } else if(rpncmd == "floor" || rpncmd == "f") {
332                                         rpn_setf(floor(rpn_getf()));
333                                 } else if(rpncmd == "ceil" || rpncmd == "c") {
334                                         rpn_setf(ceil(rpn_getf()));
335                                 } else if(rpncmd == "max") {
336                                         f = rpn_popf();
337                                         f2 = rpn_getf();
338                                         rpn_setf(max(f2, f));
339                                 } else if(rpncmd == "min") {
340                                         f = rpn_popf();
341                                         f2 = rpn_getf();
342                                         rpn_setf(min(f2, f));
343                                 } else if(rpncmd == "bound") {
344                                         f = rpn_popf();
345                                         f2 = rpn_popf();
346                                         f3 = rpn_getf();
347                                         rpn_setf(bound(f3, f2, f));
348                                 } else if(rpncmd == "when") {
349                                         f = rpn_popf();
350                                         f2 = rpn_popf();
351                                         f3 = rpn_getf();
352                                         if(f)
353                                                 rpn_setf(f3);
354                                         else
355                                                 rpn_setf(f2);
356                                 } else if(rpncmd == ">" || rpncmd == "gt") {
357                                         f = rpn_popf();
358                                         rpn_setf(rpn_getf() > f);
359                                 } else if(rpncmd == "<" || rpncmd == "lt") {
360                                         f = rpn_popf();
361                                         rpn_setf(rpn_getf() < f);
362                                 } else if(rpncmd == "==" || rpncmd == "eq") {
363                                         f = rpn_popf();
364                                         rpn_setf(rpn_getf() == f);
365                                 } else if(rpncmd == ">=" || rpncmd == "ge") {
366                                         f = rpn_popf();
367                                         rpn_setf(rpn_getf() >= f);
368                                 } else if(rpncmd == "<=" || rpncmd == "le") {
369                                         f = rpn_popf();
370                                         rpn_setf(rpn_getf() <= f);
371                                 } else if(rpncmd == "!=" || rpncmd == "ne") {
372                                         f = rpn_popf();
373                                         rpn_setf(rpn_getf() != f);
374                                 } else if(rpncmd == "rand") {
375                                         rpn_setf(ceil(random() * rpn_getf()) - 1);
376                                 } else if(rpncmd == "crc16") {
377                                         rpn_setf(crc16(FALSE, rpn_get()));
378                                 } else if(rpncmd == "brutus") {
379                                         f = rpn_popf();
380                                         rpn_set(bruteforce_crc16(rpn_getf(), f));
381                                 } else if(rpncmd == "dbpush") {
382                                         s = rpn_pop();
383                                         if(!rpn_error)
384                                         {
385                                                 i = stof(db_get(rpn_db, "stack.pointer"));
386                                                 db_put(rpn_db, "stack.pointer", ftos(i+1));
387                                                 db_put(rpn_db, strcat("stack.", ftos(i)), s);
388                                         }
389                                         if(!i)
390                                                 db_put(rpn_db, "stack.pos", "0");
391                                 } else if(rpncmd == "dbpop") {
392                                         i = stof(db_get(rpn_db, "stack.pointer"));
393                                         if(i)
394                                         {
395                                                 s = ftos(i-1);
396                                                 db_put(rpn_db, "stack.pointer", s);
397                                                 rpn_push(db_get(rpn_db, strcat("stack.", s)));
398                                                 j = stof(db_get(rpn_db, "stack.pos"));
399                                                 if(j >= i)
400                                                         db_put(rpn_db, "stack.pos", ftos(i-2));
401                                         } else {
402                                                 rpn_error = 1;
403                                                 print("rpn: database underflow\n");
404                                         }
405                                 } else if(rpncmd == "dbget") {
406                                         
407                                         i = stof(db_get(rpn_db, "stack.pointer"));
408                                         if(i)
409                                         {
410                                                 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
411                                         } else {
412                                                 rpn_error = 1;
413                                                 print("rpn: database empty\n");
414                                         }
415                                 } else if(rpncmd == "dblen") {
416                                         rpn_push(db_get(rpn_db, "stack.pointer"));
417                                 } else if(rpncmd == "dbclr") {
418                                         db_close(rpn_db);
419                                         rpn_db = db_create();
420                                         db_put(rpn_db, "stack.pointer", "0");
421                                         db_put(rpn_db, "stack.pos", "-1");
422                                 } else if(rpncmd == "dbsave") {
423                                         s = rpn_pop();
424                                         if(!rpn_error)
425                                                 db_save(rpn_db, s);
426                                 } else if(rpncmd == "dbload") {
427                                         s = rpn_pop();
428                                         if(!rpn_error)
429                                         {
430                                                 db_close(rpn_db);
431                                                 rpn_db = db_load(s);
432                                         }
433                                 } else if(rpncmd == "dbins") {
434                                         s = rpn_pop();
435                                         if(!rpn_error)
436                                                 //if(rpn_sp > 0)
437                                         {
438                                                 j = stof(db_get(rpn_db, "stack.pointer"));
439                                                 i = stof(db_get(rpn_db, "stack.pos"));
440                                                 
441                                                 if(i < 0)
442                                                 {
443                                                         i = 0;
444                                                         db_put(rpn_db, "stack.pos", "0");
445                                                 }
446                                                 
447                                                 db_put(rpn_db, "stack.pointer", ftos(j+1));
448                                                 for(--j; j >= i; --j)
449                                                 {
450                                                         db_put(rpn_db, strcat("stack.", ftos(j+1)),
451                                                                db_get(rpn_db, (strcat("stack.", ftos(j))))
452                                                                 );
453                                                 }
454                                                 db_put(rpn_db, strcat("stack.", ftos(i)), s);
455                                         }
456                                 } else if(rpncmd == "dbext") {
457                                         j = stof(db_get(rpn_db, "stack.pointer"));
458                                         i = stof(db_get(rpn_db, "stack.pos"));
459                                         if(!j)
460                                         {
461                                                 rpn_error = TRUE;
462                                                 print("rpn: empty database\n");
463                                         } else {
464                                                 --j;
465                                                 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
466                                                 db_put(rpn_db, "stack.pointer", ftos(j));
467                                                 if(i == j)
468                                                 {
469                                                         db_put(rpn_db, "stack.pos", ftos(j-1));
470                                                 } else {
471                                                         while(i < j)
472                                                         {
473                                                                 db_put(rpn_db, strcat("stack.", ftos(i)),
474                                                                        db_get(rpn_db, (strcat("stack.", ftos(i+1))))
475                                                                         );
476                                                                 ++i;
477                                                         }
478                                                 }
479                                         }
480                                 } else if(rpncmd == "dbread") {
481                                         s = db_get(rpn_db, "stack.pos");
482                                         if(stof(s) >= 0)
483                                         {
484                                                 rpn_push(db_get(rpn_db, strcat("stack.", s)));
485                                         } else {
486                                                 rpn_error = 1;
487                                                 print("rpn: empty database\n");
488                                         }
489                                 } else if(rpncmd == "dbat") {
490                                         rpn_push(db_get(rpn_db, "stack.pos"));
491                                 } else if(rpncmd == "dbmov") {
492                                         j = stof(db_get(rpn_db, "stack.pointer"));
493                                         i = stof(db_get(rpn_db, "stack.pos"));
494                                         i += rpn_popf();
495                                         if(!rpn_error)
496                                         {
497                                                 if(i < 0 || i >= j)
498                                                 {
499                                                         print("rpn: database cursor out of bounds\n");
500                                                         rpn_error = TRUE;
501                                                 }
502                                                 if(!rpn_error)
503                                                 {
504                                                         db_put(rpn_db, "stack.pos", ftos(i));
505                                                 }
506                                         }
507                                 } else if(rpncmd == "dbgoto") {
508                                         s = rpn_pop();
509                                         j = stof(db_get(rpn_db, "stack.pointer"));
510                                         if(!j)
511                                         {
512                                                 rpn_error = TRUE;
513                                                 print("rpn: empty database, cannot move cursor\n");
514                                         }
515                                         if(!rpn_error)
516                                         {
517                                                 if(s == "end")
518                                                         i = stof(db_get(rpn_db, "stack.pointer"))-1;
519                                                 else if(s == "beg")
520                                                         i = 0;
521                                                 else
522                                                         i = stof(s);
523                                                 
524                                                 j = stof(db_get(rpn_db, "stack.pointer"));
525                                                 if(i < 0 || i >= j)
526                                                 {
527                                                         print("rpn: database cursor destination out of bounds\n");
528                                                         rpn_error = TRUE;
529                                                 }
530                                                 if(!rpn_error)
531                                                 {
532                                                         db_put(rpn_db, "stack.pos", ftos(i));
533                                                 }
534                                         }
535                                 } else if(rpncmd == "union") {
536                                         // s s2 union
537                                         s2 = rpn_pop();
538                                         s = rpn_get();
539                                         f = tokenize_sane(s);
540                                         f2 = tokenize_sane(strcat(s, " ", s2));
541                                         // tokens 0..(f-1) represent s
542                                         // tokens f..f2 represent s2
543                                         // UNION: add all tokens to s that are in s2 but not in s
544                                         s = "";
545                                         for(i = 0; i < f; ++i)  
546                                                 s = strcat(s, " ", argv(i));
547                                         for(i = f; i < f2; ++i) {
548                                                 for(j = 0; j < f; ++j)
549                                                         if(argv(i) == argv(j))
550                                                                 goto skip_union;
551                                                 s = strcat(s, " ", argv(i));
552 :skip_union
553                                         }
554                                         if(substring(s, 0, 1) == " ")
555                                                 s = substring(s, 1, 99999);
556                                         rpn_set(s);
557                                         tokenize_sane(command);
558                                 } else if(rpncmd == "intersection") {
559                                         // s s2 intersection
560                                         s2 = rpn_pop();
561                                         s = rpn_get();
562                                         f = tokenize_sane(s);
563                                         f2 = tokenize_sane(strcat(s, " ", s2));
564                                         // tokens 0..(f-1) represent s
565                                         // tokens f..f2 represent s2
566                                         // INTERSECTION: keep only the tokens from s that are also in s2
567                                         s = "";
568                                         for(i = 0; i < f; ++i) {
569                                                 for(j = f; j < f2; ++j)
570                                                         if(argv(i) == argv(j))
571                                                         {
572                                                                 s = strcat(s, " ", argv(i));
573                                                                 break;
574                                                         }
575                                         }
576                                         if(substring(s, 0, 1) == " ")
577                                                 s = substring(s, 1, 99999);
578                                         rpn_set(s);
579                                         tokenize_sane(command);
580                                 } else if(rpncmd == "difference") {
581                                         // s s2 difference
582                                         s2 = rpn_pop();
583                                         s = rpn_get();
584                                         f = tokenize_sane(s);
585                                         f2 = tokenize_sane(strcat(s, " ", s2));
586                                         // tokens 0..(f-1) represent s
587                                         // tokens f..f2 represent s2
588                                         // DIFFERENCE: keep only the tokens from s that are not in s2
589                                         s = "";
590                                         for(i = 0; i < f; ++i) {
591                                                 for(j = f; j < f2; ++j)
592                                                         if(argv(i) == argv(j))
593                                                                 goto skip_difference;
594                                                 s = strcat(s, " ", argv(i));
595 :skip_difference
596                                         }
597                                         if(substring(s, 0, 1) == " ")
598                                                 s = substring(s, 1, 99999);
599                                         rpn_set(s);
600                                         tokenize_sane(command);
601                                 } else if(rpncmd == "shuffle") {
602                                         // s shuffle
603                                         s = rpn_get();
604                                         f = tokenize_sane(s);
605
606                                         for(i = 0; i < f - 1; ++i) {
607                                                 // move a random item from i..f-1 to position i
608                                                 s = "";
609                                                 f2 = floor(random() * (f - i) + i);
610                                                 for(j = 0; j < i; ++j)
611                                                         s = strcat(s, " ", argv(j));
612                                                 s = strcat(s, " ", argv(f2));
613                                                 for(j = i; j < f; ++j)
614                                                         if(j != f2)
615                                                                 s = strcat(s, " ", argv(j));
616                                                 f = tokenize_sane(s);
617                                         }
618
619                                         if(substring(s, 0, 1) == " ")
620                                                 s = substring(s, 1, 99999);
621                                         rpn_set(s);
622                                         tokenize_sane(command);
623                                 } else if(rpncmd == "fexists_assert") {
624                                         s = rpn_pop();
625                                         if(!rpn_error)
626                                         {
627                                                 f = fopen(s, FILE_READ);
628                                                 if(f != -1)
629                                                         fclose(f);
630                                                 else {
631                                                         print("rpn: ERROR: ", s, " does not exist!\n");
632                                                         rpn_error = TRUE;
633                                                 }
634                                         }
635                                 } else if(rpncmd == "fexists") {
636                                         s = rpn_get();
637                                         if(!rpn_error)
638                                         {
639                                                 f = fopen(s, FILE_READ);
640                                                 if(f != -1) {
641                                                         fclose(f);
642                                                         rpn_setf(1);
643                                                 } else {
644                                                         rpn_setf(0);
645                                                 }
646                                         }
647                                 } else if(rpncmd == "localtime") {
648                                         rpn_set(strftime(TRUE, rpn_get()));
649                                 } else if(rpncmd == "gmtime") {
650                                         rpn_set(strftime(FALSE, rpn_get()));
651                                 } else if(rpncmd == "time") {
652                                         rpn_pushf(time);
653                                 } else {
654                                         rpn_push(cvar_string(rpncmd));
655                                 }
656                                 if(rpn_error)
657                                         break;
658                         }
659                         while(rpn_sp > 0)
660                         {
661                                 s = rpn_pop();
662                                 print("rpn: still on stack: ", s, "\n");
663                         }
664                         return TRUE;
665                 }
666         } else if(argv(0) == "addtolist") {
667                 if(argc >= 2)
668                 {
669                         s = argv(1);
670                         s2 = argv(2);
671                         if(cvar_string(s) == "")
672                                 cvar_set(s, s2);
673                         else {
674                                 n = tokenizebyseparator(cvar_string(s), " ");
675                                 for(i = 0; i < n; ++i)
676                                         if(argv(i) == s2)
677                                                 return TRUE; // already in list
678                                 cvar_set(s, strcat(s2, " ", cvar_string(s)));
679                         }
680                 }
681                 return TRUE;
682 #ifdef MENUQC
683         } else if(argv(0) == "cp") {
684                 if(argc >= 2)
685                 {
686                         s = argv(1);
687                         for(i = 2; i < argc; ++i)
688                                 s = strcat(s, " ", argv(i));
689                         centerprint(unescape(s));
690                 }
691                 return TRUE;
692 #endif
693         }
694
695         return FALSE;
696 }