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