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