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