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