]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/command/rpn.qc
Merge remote branch 'origin/terencehill/arena_and_ca_fixes'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / command / rpn.qc
1 // ========================================
2 //  RPN command code, written by divVerent
3 //  Last updated: December 28th, 2011
4 // ========================================
5
6 string rpn_pop() 
7 {
8         if(rpn_sp > 0) {
9                 --rpn_sp;
10                 return rpn_stack[rpn_sp];
11         } else {
12                 print("rpn: stack underflow\n");
13                 rpn_error = TRUE;
14                 return "";
15         }
16 }
17 void rpn_push(string s) 
18 {
19         if(rpn_sp < MAX_RPN_STACK) {
20                 rpn_stack[rpn_sp] = s;
21                 ++rpn_sp;
22         } else {
23                 print("rpn: stack overflow\n");
24                 rpn_error = TRUE;
25         }
26 }
27 string rpn_get() 
28 {
29         if(rpn_sp > 0) {
30                 return rpn_stack[rpn_sp - 1];
31         } else {
32                 print("rpn: empty stack\n");
33                 rpn_error = TRUE;
34                 return "";
35         }
36 }
37 void rpn_set(string s) 
38 {
39         if(rpn_sp > 0) {
40                 rpn_stack[rpn_sp - 1] = s;
41         } else {
42                 print("rpn: empty stack\n");
43                 rpn_error = TRUE;
44         }
45 }
46
47 float rpn_getf() { return stof(rpn_get()); }
48 float rpn_popf() { return stof(rpn_pop()); }
49 void rpn_pushf(float f) { return rpn_push(ftos(f)); }
50 void rpn_setf(float f) { return rpn_set(ftos(f)); }
51
52 void GenericCommand_rpn(float request, float argc, string command)
53 {
54         switch(request)
55         {
56                 case CMD_REQUEST_COMMAND:
57                 {
58                         float i, j, f, f2, f3, rpnpos;
59                         //vector rgb;
60                         string s, s2, rpncmd;
61                         
62                         if(!rpn_db)
63                         {
64                                 rpn_db = db_create();
65                                 db_put(rpn_db, "stack.pointer", "0");
66                                 db_put(rpn_db, "stack.pos", "-1");
67                         }
68                         
69                         if(argc >= 2)
70                         {
71                                 rpn_sp = 0;
72                                 rpn_error = FALSE;
73                                 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
74                                 {
75                                         rpncmd = argv(rpnpos);
76                                         f = strlen(rpncmd);
77                                         if(rpncmd == "") {
78                                         } else if(stof(substring(rpncmd, 0, 1)) > 0) {
79                                                 rpn_push(rpncmd);
80                                         } else if(substring(rpncmd, 0, 1) == "0") {
81                                                 rpn_push(rpncmd);
82                                         } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
83                                                 rpn_push(rpncmd);
84                                         } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
85                                                 rpn_push(rpncmd);
86                                         } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
87                                                 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
88                                         } else if(rpncmd == "clear") {
89                                                 rpn_sp = 0;
90                                         } else if(rpncmd == "def" || rpncmd == "=") {
91                                                 s = rpn_pop();
92                                                 s2 = rpn_pop();
93
94                                                 if(s2 != "")
95                                                 {
96                                                         #ifdef MENUQC
97                                                         registercvar(s2, "", 0);
98                                                         #else
99                                                         registercvar(s2, "");
100                                                         #endif
101                                                         if(!rpn_error) // don't change cvars if a stack error had happened!
102                                                                 cvar_set(s2, s);
103                                                 }
104                                                 else
105                                                 {
106                                                         print("rpn: empty cvar name for 'def'\n");
107                                                         rpn_error = TRUE;
108                                                 }
109                                         } else if(rpncmd == "defs" || rpncmd == "@") {
110                                                 s = "";
111                                                 i = rpn_popf();
112                                                 j = (i == 0);
113                                                 while(rpn_sp > 1 && (j || i > 0))
114                                                 {
115                                                         s = strcat("/", rpn_pop(), " ", s);
116                                                         --i;
117                                                 }
118                                                 s2 = rpn_pop();
119                                                 if(s2 != "")
120                                                 {
121                                                         #ifdef MENUQC
122                                                         registercvar(s2, "", 0);
123                                                         #else
124                                                         registercvar(s2, "");
125                                                         #endif
126                                                         if(!rpn_error) // don't change cvars if a stack error had happened!
127                                                                 cvar_set(s2, s);
128                                                 }
129                                                 else
130                                                 {
131                                                         print("rpn: empty cvar name for 'defs'\n");
132                                                         rpn_error = TRUE;
133                                                 }
134                                         } else if(rpncmd == "load") {
135                                                 rpn_set(cvar_string(rpn_get()));
136                                         } else if(rpncmd == "exch") {
137                                                 s = rpn_pop();
138                                                 s2 = rpn_get();
139                                                 rpn_set(s);
140                                                 rpn_push(s2);
141                                         } else if(rpncmd == "dup") {
142                                                 rpn_push(rpn_get());
143                                         } else if(rpncmd == "pop") {
144                                                 rpn_pop();
145                                         } else if(rpncmd == "add" || rpncmd == "+") {
146                                                 f = rpn_popf();
147                                                 rpn_setf(rpn_getf() + f);
148                                         } else if(rpncmd == "sub" || rpncmd == "-") {
149                                                 f = rpn_popf();
150                                                 rpn_setf(rpn_getf() - f);
151                                         } else if(rpncmd == "mul" || rpncmd == "*") {
152                                                 f = rpn_popf();
153                                                 rpn_setf(rpn_getf() * f);
154                                         } else if(rpncmd == "div" || rpncmd == "/") {
155                                                 f = rpn_popf();
156                                                 rpn_setf(rpn_getf() / f);
157                                         } else if(rpncmd == "mod" || rpncmd == "%") {
158                                                 f = rpn_popf();
159                                                 f2 = rpn_getf();
160                                                 rpn_setf(f2 - f * floor(f2 / f));
161                                         } else if(rpncmd == "abs") {
162                                                 rpn_setf(fabs(rpn_getf()));
163                                         } else if(rpncmd == "sgn") {
164                                                 f = rpn_getf();
165                                                 if(f < 0)
166                                                         rpn_set("-1");
167                                                 else if(f > 0)
168                                                         rpn_set("1");
169                                                 else
170                                                         rpn_set("0");
171                                         } else if(rpncmd == "neg" || rpncmd == "~") {
172                                                 rpn_setf(-rpn_getf());
173                                         } else if(rpncmd == "floor" || rpncmd == "f") {
174                                                 rpn_setf(floor(rpn_getf()));
175                                         } else if(rpncmd == "ceil" || rpncmd == "c") {
176                                                 rpn_setf(ceil(rpn_getf()));
177                                         } else if(rpncmd == "max") {
178                                                 f = rpn_popf();
179                                                 f2 = rpn_getf();
180                                                 rpn_setf(max(f2, f));
181                                         } else if(rpncmd == "min") {
182                                                 f = rpn_popf();
183                                                 f2 = rpn_getf();
184                                                 rpn_setf(min(f2, f));
185                                         } else if(rpncmd == "bound") {
186                                                 f = rpn_popf();
187                                                 f2 = rpn_popf();
188                                                 f3 = rpn_getf();
189                                                 rpn_setf(bound(f3, f2, f));
190                                         } else if(rpncmd == "when") {
191                                                 f = rpn_popf();
192                                                 f2 = rpn_popf();
193                                                 f3 = rpn_getf();
194                                                 if(f)
195                                                         rpn_setf(f3);
196                                                 else
197                                                         rpn_setf(f2);
198                                         } else if(rpncmd == ">" || rpncmd == "gt") {
199                                                 f = rpn_popf();
200                                                 rpn_setf(rpn_getf() > f);
201                                         } else if(rpncmd == "<" || rpncmd == "lt") {
202                                                 f = rpn_popf();
203                                                 rpn_setf(rpn_getf() < f);
204                                         } else if(rpncmd == "==" || rpncmd == "eq") {
205                                                 f = rpn_popf();
206                                                 rpn_setf(rpn_getf() == f);
207                                         } else if(rpncmd == ">=" || rpncmd == "ge") {
208                                                 f = rpn_popf();
209                                                 rpn_setf(rpn_getf() >= f);
210                                         } else if(rpncmd == "<=" || rpncmd == "le") {
211                                                 f = rpn_popf();
212                                                 rpn_setf(rpn_getf() <= f);
213                                         } else if(rpncmd == "!=" || rpncmd == "ne") {
214                                                 f = rpn_popf();
215                                                 rpn_setf(rpn_getf() != f);
216                                         } else if(rpncmd == "rand") {
217                                                 rpn_setf(ceil(random() * rpn_getf()) - 1);
218                                         } else if(rpncmd == "crc16") {
219                                                 rpn_setf(crc16(FALSE, rpn_get()));
220                                         } else if(rpncmd == "put") {
221                                                 s2 = rpn_pop();
222                                                 if (!rpn_error)
223                                                 {
224                                                         s = rpn_pop();
225                                                         if (!rpn_error)
226                                                                 db_put(rpn_db, s, s2);
227                                                 }
228                                         } else if(rpncmd == "get") {
229                                                 s = rpn_pop();
230                                                 if (!rpn_error)
231                                                         rpn_push(db_get(rpn_db, s));
232                                         } else if(rpncmd == "dbpush") {
233                                                 s = rpn_pop();
234                                                 if(!rpn_error)
235                                                 {
236                                                         i = stof(db_get(rpn_db, "stack.pointer"));
237                                                         db_put(rpn_db, "stack.pointer", ftos(i+1));
238                                                         db_put(rpn_db, strcat("stack.", ftos(i)), s);
239                                                 }
240                                                 if(!i)
241                                                         db_put(rpn_db, "stack.pos", "0");
242                                         } else if(rpncmd == "dbpop") {
243                                                 i = stof(db_get(rpn_db, "stack.pointer"));
244                                                 if(i)
245                                                 {
246                                                         s = ftos(i-1);
247                                                         db_put(rpn_db, "stack.pointer", s);
248                                                         rpn_push(db_get(rpn_db, strcat("stack.", s)));
249                                                         j = stof(db_get(rpn_db, "stack.pos"));
250                                                         if(j >= i)
251                                                                 db_put(rpn_db, "stack.pos", ftos(i-2));
252                                                 } else {
253                                                         rpn_error = 1;
254                                                         print("rpn: database underflow\n");
255                                                 }
256                                         } else if(rpncmd == "dbget") {
257                                                 
258                                                 i = stof(db_get(rpn_db, "stack.pointer"));
259                                                 if(i)
260                                                 {
261                                                         rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
262                                                 } else {
263                                                         rpn_error = 1;
264                                                         print("rpn: database empty\n");
265                                                 }
266                                         } else if(rpncmd == "dblen") {
267                                                 rpn_push(db_get(rpn_db, "stack.pointer"));
268                                         } else if(rpncmd == "dbclr") {
269                                                 db_close(rpn_db);
270                                                 rpn_db = db_create();
271                                                 db_put(rpn_db, "stack.pointer", "0");
272                                                 db_put(rpn_db, "stack.pos", "-1");
273                                         } else if(rpncmd == "dbsave") {
274                                                 s = rpn_pop();
275                                                 if(!rpn_error)
276                                                         db_save(rpn_db, s);
277                                         } else if(rpncmd == "dbload") {
278                                                 s = rpn_pop();
279                                                 if(!rpn_error)
280                                                 {
281                                                         db_close(rpn_db);
282                                                         rpn_db = db_load(s);
283                                                 }
284                                         } else if(rpncmd == "dbins") {
285                                                 s = rpn_pop();
286                                                 if(!rpn_error)
287                                                         //if(rpn_sp > 0)
288                                                 {
289                                                         j = stof(db_get(rpn_db, "stack.pointer"));
290                                                         i = stof(db_get(rpn_db, "stack.pos"));
291                                                         
292                                                         if(i < 0)
293                                                         {
294                                                                 i = 0;
295                                                                 db_put(rpn_db, "stack.pos", "0");
296                                                         }
297                                                         
298                                                         db_put(rpn_db, "stack.pointer", ftos(j+1));
299                                                         for(--j; j >= i; --j)
300                                                         {
301                                                                 db_put(rpn_db, strcat("stack.", ftos(j+1)),
302                                                                                          db_get(rpn_db, (strcat("stack.", ftos(j))))
303                                                                         );
304                                                         }
305                                                         db_put(rpn_db, strcat("stack.", ftos(i)), s);
306                                                 }
307                                         } else if(rpncmd == "dbext") {
308                                                 j = stof(db_get(rpn_db, "stack.pointer"));
309                                                 i = stof(db_get(rpn_db, "stack.pos"));
310                                                 if(!j)
311                                                 {
312                                                         rpn_error = TRUE;
313                                                         print("rpn: empty database\n");
314                                                 } else {
315                                                         --j;
316                                                         rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
317                                                         db_put(rpn_db, "stack.pointer", ftos(j));
318                                                         if(i == j)
319                                                         {
320                                                                 db_put(rpn_db, "stack.pos", ftos(j-1));
321                                                         } else {
322                                                                 while(i < j)
323                                                                 {
324                                                                         db_put(rpn_db, strcat("stack.", ftos(i)),
325                                                                                                  db_get(rpn_db, (strcat("stack.", ftos(i+1))))
326                                                                                 );
327                                                                         ++i;
328                                                                 }
329                                                         }
330                                                 }
331                                         } else if(rpncmd == "dbread") {
332                                                 s = db_get(rpn_db, "stack.pos");
333                                                 if(stof(s) >= 0)
334                                                 {
335                                                         rpn_push(db_get(rpn_db, strcat("stack.", s)));
336                                                 } else {
337                                                         rpn_error = 1;
338                                                         print("rpn: empty database\n");
339                                                 }
340                                         } else if(rpncmd == "dbat") {
341                                                 rpn_push(db_get(rpn_db, "stack.pos"));
342                                         } else if(rpncmd == "dbmov") {
343                                                 j = stof(db_get(rpn_db, "stack.pointer"));
344                                                 i = stof(db_get(rpn_db, "stack.pos"));
345                                                 i += rpn_popf();
346                                                 if(!rpn_error)
347                                                 {
348                                                         if(i < 0 || i >= j)
349                                                         {
350                                                                 print("rpn: database cursor out of bounds\n");
351                                                                 rpn_error = TRUE;
352                                                         }
353                                                         if(!rpn_error)
354                                                         {
355                                                                 db_put(rpn_db, "stack.pos", ftos(i));
356                                                         }
357                                                 }
358                                         } else if(rpncmd == "dbgoto") {
359                                                 s = rpn_pop();
360                                                 j = stof(db_get(rpn_db, "stack.pointer"));
361                                                 if(!j)
362                                                 {
363                                                         rpn_error = TRUE;
364                                                         print("rpn: empty database, cannot move cursor\n");
365                                                 }
366                                                 if(!rpn_error)
367                                                 {
368                                                         if(s == "end")
369                                                                 i = stof(db_get(rpn_db, "stack.pointer"))-1;
370                                                         else if(s == "beg")
371                                                                 i = 0;
372                                                         else
373                                                                 i = stof(s);
374                                                         
375                                                         j = stof(db_get(rpn_db, "stack.pointer"));
376                                                         if(i < 0 || i >= j)
377                                                         {
378                                                                 print("rpn: database cursor destination out of bounds\n");
379                                                                 rpn_error = TRUE;
380                                                         }
381                                                         if(!rpn_error)
382                                                         {
383                                                                 db_put(rpn_db, "stack.pos", ftos(i));
384                                                         }
385                                                 }
386                                         } else if(rpncmd == "union") {
387                                                 // s s2 union
388                                                 s2 = rpn_pop();
389                                                 s = rpn_get();
390                                                 f = tokenize_console(s);
391                                                 f2 = tokenize_console(strcat(s, " ", s2));
392                                                 // tokens 0..(f-1) represent s
393                                                 // tokens f..f2 represent s2
394                                                 // UNION: add all tokens to s that are in s2 but not in s
395                                                 s = "";
396                                                 for(i = 0; i < f; ++i)  
397                                                         s = strcat(s, " ", argv(i));
398                                                 for(i = f; i < f2; ++i) {
399                                                         for(j = 0; j < f; ++j)
400                                                                 if(argv(i) == argv(j))
401                                                                         goto skip_union;
402                                                         s = strcat(s, " ", argv(i));
403                                                         :skip_union
404                                                 }
405                                                 if(substring(s, 0, 1) == " ")
406                                                         s = substring(s, 1, 99999);
407                                                 rpn_set(s);
408                                                 tokenize_console(command);
409                                         } else if(rpncmd == "intersection") {
410                                                 // s s2 intersection
411                                                 s2 = rpn_pop();
412                                                 s = rpn_get();
413                                                 f = tokenize_console(s);
414                                                 f2 = tokenize_console(strcat(s, " ", s2));
415                                                 // tokens 0..(f-1) represent s
416                                                 // tokens f..f2 represent s2
417                                                 // INTERSECTION: keep only the tokens from s that are also in s2
418                                                 s = "";
419                                                 for(i = 0; i < f; ++i) {
420                                                         for(j = f; j < f2; ++j)
421                                                                 if(argv(i) == argv(j))
422                                                                 {
423                                                                         s = strcat(s, " ", argv(i));
424                                                                         break;
425                                                                 }
426                                                 }
427                                                 if(substring(s, 0, 1) == " ")
428                                                         s = substring(s, 1, 99999);
429                                                 rpn_set(s);
430                                                 tokenize_console(command);
431                                         } else if(rpncmd == "difference") {
432                                                 // s s2 difference
433                                                 s2 = rpn_pop();
434                                                 s = rpn_get();
435                                                 f = tokenize_console(s);
436                                                 f2 = tokenize_console(strcat(s, " ", s2));
437                                                 // tokens 0..(f-1) represent s
438                                                 // tokens f..f2 represent s2
439                                                 // DIFFERENCE: keep only the tokens from s that are not in s2
440                                                 s = "";
441                                                 for(i = 0; i < f; ++i) {
442                                                         for(j = f; j < f2; ++j)
443                                                                 if(argv(i) == argv(j))
444                                                                         goto skip_difference;
445                                                         s = strcat(s, " ", argv(i));
446                                                         :skip_difference
447                                                 }
448                                                 if(substring(s, 0, 1) == " ")
449                                                         s = substring(s, 1, 99999);
450                                                 rpn_set(s);
451                                                 tokenize_console(command);
452                                         } else if(rpncmd == "shuffle") {
453                                                 // s shuffle
454                                                 s = rpn_get();
455                                                 f = tokenize_console(s);
456
457                                                 for(i = 0; i < f - 1; ++i) {
458                                                         // move a random item from i..f-1 to position i
459                                                         s = "";
460                                                         f2 = floor(random() * (f - i) + i);
461                                                         for(j = 0; j < i; ++j)
462                                                                 s = strcat(s, " ", argv(j));
463                                                         s = strcat(s, " ", argv(f2));
464                                                         for(j = i; j < f; ++j)
465                                                                 if(j != f2)
466                                                                         s = strcat(s, " ", argv(j));
467                                                         f = tokenize_console(s);
468                                                 }
469
470                                                 if(substring(s, 0, 1) == " ")
471                                                         s = substring(s, 1, 99999);
472                                                 rpn_set(s);
473                                                 tokenize_console(command);
474                                         } else if(rpncmd == "fexists_assert") {
475                                                 s = rpn_pop();
476                                                 if(!rpn_error)
477                                                 {
478                                                         if (!fexists(s))
479                                                         {
480                                                                 print("rpn: ERROR: ", s, " does not exist!\n");
481                                                                 rpn_error = TRUE;
482                                                         }
483                                                 }
484                                         } else if(rpncmd == "fexists") {
485                                                 s = rpn_get();
486                                                 if(!rpn_error)
487                                                 {
488                                                         if (fexists(s))
489                                                                 rpn_setf(1);
490                                                         else
491                                                                 rpn_setf(0);
492                                                 }
493                                         } else if(rpncmd == "localtime") {
494                                                 rpn_set(strftime(TRUE, rpn_get()));
495                                         } else if(rpncmd == "gmtime") {
496                                                 rpn_set(strftime(FALSE, rpn_get()));
497                                         } else if(rpncmd == "time") {
498                                                 rpn_pushf(time);
499                                         } else if(rpncmd == "digest") {
500                                                 s = rpn_pop();
501                                                 rpn_set(digest_hex(s, rpn_get()));
502                                         } else if(rpncmd == "sprintf1s") {
503                                                 s = rpn_pop();
504                                                 rpn_set(sprintf(s, rpn_get()));
505                                         } else {
506                                                 rpn_push(cvar_string(rpncmd));
507                                         }
508                                         if(rpn_error)
509                                                 break;
510                                 }
511                                 while(rpn_sp > 0)
512                                 {
513                                         s = rpn_pop();
514                                         print("rpn: still on stack: ", s, "\n");
515                                 }
516                         }
517
518                         return;
519                 }
520                         
521                 default:
522                 case CMD_REQUEST_USAGE:
523                 {
524                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " rpn EXPRESSION...\n"));
525                         print("    Operator description (x: string, s: set, f: float):\n");
526                         print("    x pop ----------------------------->     : removes the top\n");
527                         print("    x dup -----------------------------> x x : duplicates the top\n");
528                         print("    x x exch --------------------------> x x : swap the top two\n");
529                         print("    /cvarname load --------------------> x   : loads a cvar\n");
530                         print("    /cvarname x def ------------------->     : writes to a cvar\n");
531                         print("    f f add|sub|mul|div|mod|max|min ---> f   : adds/... two numbers\n");
532                         print("    f f eq|ne|gt|ge|lt|le -------------> f   : compares two numbers\n");
533                         print("    f neg|abs|sgn|rand|floor|ceil------> f   : negates/... a number\n");
534                         print("    f f f bound -----------------------> f   : bounds the middle number\n");
535                         print("    f1 f2 b when ----------------------> f   : f1 if b, f2 otherwise\n");
536                         print("    s s union|intersection|difference -> s   : set operations\n");
537                         print("    s shuffle -------------------------> s   : randomly arrange elements\n");
538                         print("    /key /value put ------------------->     : set a database key\n");
539                         print("    /key get --------------------------> s   : get a database value\n");
540                         print("    x dbpush -------------------------->     : pushes the top onto the database\n");
541                         print("    dbpop|dbget -----------------------> x   : removes/reads DB's top\n");
542                         print("    dblen|dbat ------------------------> f   : gets the DB's size/cursor pos\n");
543                         print("    dbclr ----------------------------->     : clear the DB\n");
544                         print("    s dbsave|dbload-------------------->     : save/load the DB to/from a file\n");
545                         print("    x dbins --------------------------->     : moves the top into the DB\n");
546                         print("    dbext|dbread ----------------------> x   : extract/get from the DB's cursor\n");
547                         print("    f dbmov|dbgoto -------------------->     : move or set the DB's cursor\n");
548                         print("    s localtime -----------------------> s   : formats the current local time\n");
549                         print("    s gmtime --------------------------> s   : formats the current UTC time\n");
550                         print("    time ------------------------------> f   : seconds since VM start\n");
551                         print("    s /MD4 digest ---------------------> s   : MD4 digest\n");
552                         print("    s /SHA256 digest ------------------> s   : SHA256 digest\n");
553                         print("    s /formatstring sprintf1s ---------> s   : sprintf with 1 string (pad, cut)\n");
554                         print("    Set operations operate on 'such''strings'.\n");
555                         print("    Unknown tokens insert their cvar value.\n");
556                         return;
557                 }
558         }
559 }