]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/gamecommand.qc
Now equal to whats on the site
[divverent/nexuiz.git] / data / qcsrc / common / gamecommand.qc
1 #define MAX_RPN_STACK 8
2 float rpn_error;
3 float rpn_sp;
4 string rpn_stack[MAX_RPN_STACK];
5 string rpn_pop() {
6         if(rpn_sp > 0) {
7                 --rpn_sp;
8                 return rpn_stack[rpn_sp];
9         } else {
10                 print("rpn: stack underflow\n");
11                 rpn_error = TRUE;
12                 return "";
13         }
14 }
15 void rpn_push(string s) {
16         if(rpn_sp < MAX_RPN_STACK) {
17                 rpn_stack[rpn_sp] = s;
18                 ++rpn_sp;
19         } else {
20                 print("rpn: stack overflow\n");
21                 rpn_error = TRUE;
22         }
23 }
24 string rpn_get() {
25         if(rpn_sp > 0) {
26                 return rpn_stack[rpn_sp - 1];
27         } else {
28                 print("rpn: empty stack\n");
29                 rpn_error = TRUE;
30                 return "";
31         }
32 }
33 void rpn_set(string s) {
34         if(rpn_sp > 0) {
35                 rpn_stack[rpn_sp - 1] = s;
36         } else {
37                 print("rpn: empty stack\n");
38                 rpn_error = TRUE;
39         }
40 }
41 float rpn_getf() { return stof(rpn_get()); }
42 float rpn_popf() { return stof(rpn_pop()); }
43 void rpn_pushf(float f) { return rpn_push(ftos(f)); }
44 void rpn_setf(float f) { return rpn_set(ftos(f)); }
45
46 float GameCommand_Generic(string command)
47 {
48         float argc;
49         float i, j, f, n;
50         string s, s2;
51         argc = tokenize(command);
52         if(argv(0) == "help")
53         {
54                 print("  rpn EXPRESSION... - a RPN calculator.\n");
55                 print("    Operator description (x: string, s: set, f: float):\n");
56                 print("    x pop ----------------------------->     : removes the top\n");
57                 print("    x dup -----------------------------> x x : duplicates the top\n");
58                 print("    x x exch --------------------------> x x : swap the top two\n");
59                 print("    /cvarname load --------------------> x   : loads a cvar\n");
60                 print("    /cvarname x def ------------------->     : writes to a cvar\n");
61                 print("    f f add|sub|mul|div|mod|max|min ---> f   : adds/... two numbers\n");
62                 print("    f f eq|ne|gt|ge|lt|le -------------> f   : compares two numbers\n");
63                 print("    f neg|abs|sgn|rand ----------------> f   : negates/... a number\n");
64                 print("    f f f bound -----------------------> f   : bounds the middle number\n");
65                 print("    f1 f2 b when ----------------------> f   : f1 if b, f2 otherwise\n");
66                 print("    s s union|intersection|difference -> s   : set operations\n");
67                 print("    s shuffle -------------------------> s   : randomly arrange elements\n");
68                 print("    Set operations operate on 'such''strings' like g_maplist.\n");
69                 print("    Unknown tokens insert their cvar value.\n");
70                 print("  maplist add map\n");
71                 print("  maplist remove map\n");
72                 print("  maplist shuffle\n");
73                 return TRUE;
74         }
75         
76         if(argv(0) == "maplist")
77         {
78                 if(argv(1) == "add" && argc == 3)
79                 {
80 #ifdef MAPINFO
81                         f = fopen(strcat("maps/", argv(2), ".bsp"), FILE_READ);
82                         if(f != -1)
83                                 fclose(f);
84                         else {
85                                 print("maplist: ERROR: ", argv(2), " does not exist!\n");
86                                 return TRUE;
87                         }
88                         if(cvar_string("g_maplist") == "")
89                                 cvar_set("g_maplist", argv(2));
90                         else
91                                 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
92 #else
93                         f = fopen(strcat("maps/", argv(2), ".mapcfg"), FILE_READ);
94                         if(f != -1)
95                                 fclose(f);
96                         else {
97                                 print("maplist: ERROR: ", argv(2), " does not exist!\n");
98                                 return TRUE;
99                         }
100                         cvar_set("g_maplist", strcat("'", argv(2), "'", cvar_string("g_maplist")));
101 #endif
102                         return TRUE;
103                 }
104                 else if(argv(1) == "remove" && argc == 3)
105                 {
106                         s = argv(2);
107 #ifdef MAPINFO
108                         n = tokenizebyseparator(cvar_string("g_maplist"), " ");
109 #else
110                         n = tokenize(cvar_string("g_maplist"));
111 #endif
112                         s2 = "";
113                         for(i = 0; i < n; ++i)
114                                 if(argv(i) != s)
115                                 {
116 #ifdef MAPINFO
117                                         s2 = strcat(s2, " ", argv(i));
118 #else
119                                         s2 = strcat(s2, "'", argv(i), "'");
120 #endif
121                                 }
122 #ifdef MAPINFO
123                         s2 = substring(s2, 1, strlen(s2) - 1);
124 #endif
125                         cvar_set("g_maplist", s2);
126                         return TRUE;
127                 }
128                 else if(argv(1) == "shuffle" && argc == 2)
129                 {
130                         s = cvar_string("g_maplist");
131 #ifdef MAPINFO
132                         for(i = 1; i < (n = tokenizebyseparator(s, " ")); ++i)
133 #else
134                         for(i = 1; i < (n = tokenize(s)); ++i)
135 #endif
136                         {
137                                 // swap i-th item at a random position from 0 to i
138                                 // proof for even distribution:
139                                 //   n = 1: obvious
140                                 //   n -> n+1:
141                                 //     item n+1 gets at any position with chance 1/(n+1)
142                                 //     all others will get their 1/n chance reduced by factor n/(n+1)
143                                 //     to be on place n+1, their chance will be 1/(n+1)
144                                 //     1/n * n/(n+1) = 1/(n+1)
145                                 //     q.e.d.
146                                 f = ceil(random() * (i + 1)) - 1; // 0 to i
147                                 if(f == i)
148                                         continue; // no change
149
150                                 s2 = "";
151                                 for(j = 0; j < n; ++j)
152 #ifdef MAPINFO
153                                         s2 = strcat(s2, " ", argv((j == i) ? f : (j == f) ? i : j));
154                                 s = substring(s2, 1, strlen(s2) - 1);
155 #else
156                                         s2 = strcat(s2, "'", argv((j == i) ? f : (j == f) ? i : j), "'");
157                                 s = s2;
158 #endif
159                         }
160                         cvar_set("g_maplist", s);
161                         return TRUE;
162                 }
163         }
164         else if(argv(0) == "rpn")
165         {
166                 if(argc >= 2)
167                 {
168                         float rpnpos;
169                         string rpncmd;
170                         float f2, f3;
171                         rpn_sp = 0;
172                         rpn_error = FALSE;
173                         for(rpnpos = 1; rpnpos < argc; ++rpnpos)
174                         {
175                                 rpncmd = argv(rpnpos);
176                                 f = strlen(rpncmd);
177                                 if(rpncmd == "") {
178                                 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
179                                         rpn_push(rpncmd);
180                                 } else if(substring(rpncmd, 0, 1) == "0") {
181                                         rpn_push(rpncmd);
182                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
183                                         rpn_push(rpncmd);
184                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
185                                         rpn_push(rpncmd);
186                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
187                                         rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
188                                 } else if(rpncmd == "def" || rpncmd == "=") {
189                                         s = rpn_pop();
190                                         s2 = rpn_pop();
191 #ifdef MENUQC
192                                         registercvar(s2, "", 0);
193 #else
194                                         registercvar(s2, "");
195 #endif
196                                         if(!rpn_error) // don't change cvars if a stack error had happened!
197                                                 cvar_set(s2, s);
198                                 } else if(rpncmd == "load") {
199                                         rpn_set(cvar_string(rpn_get()));
200                                 } else if(rpncmd == "exch") {
201                                         s = rpn_pop();
202                                         s2 = rpn_get();
203                                         rpn_set(s);
204                                         rpn_push(s2);
205                                 } else if(rpncmd == "dup") {
206                                         rpn_push(rpn_get());
207                                 } else if(rpncmd == "pop") {
208                                         rpn_pop();
209                                 } else if(rpncmd == "add" || rpncmd == "+") {
210                                         f = rpn_popf();
211                                         rpn_setf(rpn_getf() + f);
212                                 } else if(rpncmd == "sub" || rpncmd == "-") {
213                                         f = rpn_popf();
214                                         rpn_setf(rpn_getf() - f);
215                                 } else if(rpncmd == "mul" || rpncmd == "*") {
216                                         f = rpn_popf();
217                                         rpn_setf(rpn_getf() * f);
218                                 } else if(rpncmd == "div" || rpncmd == "/") {
219                                         f = rpn_popf();
220                                         rpn_setf(rpn_getf() / f);
221                                 } else if(rpncmd == "mod" || rpncmd == "%") {
222                                         f = rpn_popf();
223                                         f2 = rpn_getf();
224                                         rpn_setf(f2 - f * floor(f2 / f));
225                                 } else if(rpncmd == "abs") {
226                                         rpn_setf(fabs(rpn_getf()));
227                                 } else if(rpncmd == "sgn") {
228                                         f = rpn_getf();
229                                         if(f < 0)
230                                                 rpn_set("-1");
231                                         else if(f > 0)
232                                                 rpn_set("1");
233                                         else
234                                                 rpn_set("0");
235                                 } else if(rpncmd == "neg" || rpncmd == "~") {
236                                         rpn_setf(-rpn_getf());
237                                 } else if(rpncmd == "max") {
238                                         f = rpn_popf();
239                                         f2 = rpn_getf();
240                                         rpn_setf(max(f2, f));
241                                 } else if(rpncmd == "min") {
242                                         f = rpn_popf();
243                                         f2 = rpn_getf();
244                                         rpn_setf(min(f2, f));
245                                 } else if(rpncmd == "bound") {
246                                         f = rpn_popf();
247                                         f2 = rpn_popf();
248                                         f3 = rpn_getf();
249                                         rpn_setf(bound(f3, f2, f));
250                                 } else if(rpncmd == "when") {
251                                         f = rpn_popf();
252                                         f2 = rpn_popf();
253                                         f3 = rpn_getf();
254                                         if(f)
255                                                 rpn_setf(f3);
256                                         else
257                                                 rpn_setf(f2);
258                                 } else if(rpncmd == ">" || rpncmd == "gt") {
259                                         f = rpn_popf();
260                                         rpn_setf(rpn_getf() > f);
261                                 } else if(rpncmd == "<" || rpncmd == "lt") {
262                                         f = rpn_popf();
263                                         rpn_setf(rpn_getf() < f);
264                                 } else if(rpncmd == "==" || rpncmd == "eq") {
265                                         f = rpn_popf();
266                                         rpn_setf(rpn_getf() == f);
267                                 } else if(rpncmd == ">=" || rpncmd == "ge") {
268                                         f = rpn_popf();
269                                         rpn_setf(rpn_getf() >= f);
270                                 } else if(rpncmd == "<=" || rpncmd == "le") {
271                                         f = rpn_popf();
272                                         rpn_setf(rpn_getf() <= f);
273                                 } else if(rpncmd == "!=" || rpncmd == "ne") {
274                                         f = rpn_popf();
275                                         rpn_setf(rpn_getf() != f);
276                                 } else if(rpncmd == "rand") {
277                                         rpn_setf(ceil(random() * rpn_getf()) - 1);
278                                 } else if(rpncmd == "union") {
279                                         // s s2 union
280                                         s2 = rpn_pop();
281                                         s = rpn_get();
282                                         f = tokenize(s);
283                                         f2 = tokenize(strcat(s, s2));
284                                         // tokens 0..(f-1) represent s
285                                         // tokens f..f2 represent s2
286                                         // UNION: add all tokens to s that are in s2 but not in s
287                                         s = "";
288                                         for(i = 0; i < f; ++i)
289                                                 s = strcat(s, "'", argv(i), "'");
290                                         for(i = f; i < f2; ++i) {
291                                                 for(j = 0; j < f; ++j)
292                                                         if(argv(i) == argv(j))
293                                                                 goto skip_union;
294                                                 s = strcat(s, "'", argv(i), "'");
295 :skip_union
296                                         }
297                                         rpn_set(s);
298                                         tokenize(command);
299                                 } else if(rpncmd == "intersection") {
300                                         // s s2 intersection
301                                         s2 = rpn_pop();
302                                         s = rpn_get();
303                                         f = tokenize(s);
304                                         f2 = tokenize(strcat(s, s2));
305                                         // tokens 0..(f-1) represent s
306                                         // tokens f..f2 represent s2
307                                         // INTERSECTION: keep only the tokens from s that are also in s2
308                                         s = "";
309                                         for(i = 0; i < f; ++i) {
310                                                 for(j = f; j < f2; ++j)
311                                                         if(argv(i) == argv(j))
312                                                         {
313                                                                 s = strcat(s, "'", argv(i), "'");
314                                                                 break;
315                                                         }
316                                         }
317                                         rpn_set(s);
318                                         tokenize(command);
319                                 } else if(rpncmd == "difference") {
320                                         // s s2 difference
321                                         s2 = rpn_pop();
322                                         s = rpn_get();
323                                         f = tokenize(s);
324                                         f2 = tokenize(strcat(s, s2));
325                                         // tokens 0..(f-1) represent s
326                                         // tokens f..f2 represent s2
327                                         // DIFFERENCE: keep only the tokens from s that are not in s2
328                                         s = "";
329                                         for(i = 0; i < f; ++i) {
330                                                 for(j = f; j < f2; ++j)
331                                                         if(argv(i) == argv(j))
332                                                                 goto skip_difference;
333                                                 s = strcat(s, "'", argv(i), "'");
334 :skip_difference
335                                         }
336                                         rpn_set(s);
337                                         tokenize(command);
338                                 } else if(rpncmd == "shuffle") {
339                                         // s shuffle
340                                         s = rpn_get();
341                                         f = tokenize(s);
342
343                                         for(i = 0; i < f - 1; ++i) {
344                                                 // move a random item from i..f-1 to position i
345                                                 s = "";
346                                                 f2 = ceil(random() * (f - i) + i) - 1;
347                                                 for(j = 0; j < i; ++j)
348                                                         s = strcat(s, "'", argv(j), "'");
349                                                 s = strcat(s, "'", argv(f2), "'");
350                                                 for(j = i; j < f; ++j)
351                                                         if(j != f2)
352                                                                 s = strcat(s, "'", argv(j), "'");
353                                                 f = tokenize(s);
354                                         }
355
356                                         rpn_set(s);
357                                         tokenize(command);
358                                 } else if(rpncmd == "fexists_assert") {
359                                         s = rpn_pop();
360                                         if(!rpn_error)
361                                         {
362                                                 f = fopen(s, FILE_READ);
363                                                 if(f != -1)
364                                                         fclose(f);
365                                                 else {
366                                                         print("rpn: ERROR: ", s, " does not exist!\n");
367                                                         rpn_error = TRUE;
368                                                 }
369                                         }
370                                 } else {
371                                         rpn_push(cvar_string(rpncmd));
372                                 }
373                                 if(rpn_error)
374                                         break;
375                         }
376                         while(rpn_sp > 0)
377                         {
378                                 s = rpn_pop();
379                                 print("rpn: still on stack: ", s, "\n");
380                         }
381                         return TRUE;
382                 }
383         }
384
385         return FALSE;
386 }