]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/gamecommand.qc
fix maplist editing commands for mapinfo
[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 -----------> f   : adds/... two numbers\n");
62                 print("    f neg|abs|sgn|rand ----------------> f   : negates/... a number\n");
63                 print("    s s union|intersection|difference -> s   : set operations\n");
64                 print("    s shuffle -------------------------> s   : randomly arrange elements\n");
65                 print("    Set operations operate on 'such''strings' like g_maplist.\n");
66                 print("    Unknown tokens insert their cvar value.\n");
67                 print("  maplist add map\n");
68                 print("  maplist remove map\n");
69                 print("  maplist shuffle\n");
70                 return TRUE;
71         }
72         
73         if(argv(0) == "maplist")
74         {
75                 if(argv(1) == "add" && argc == 3)
76                 {
77 #ifdef MAPINFO
78                         f = fopen(strcat("maps/", argv(2), ".bsp"), FILE_READ);
79                         if(f != -1)
80                                 fclose(f);
81                         else {
82                                 print("maplist: ERROR: ", argv(2), " does not exist!\n");
83                                 return TRUE;
84                         }
85                         if(cvar_string("g_maplist") == "")
86                                 cvar_set("g_maplist", argv(2));
87                         else
88                                 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
89 #else
90                         f = fopen(strcat("maps/", argv(2), ".mapcfg"), 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                         cvar_set("g_maplist", strcat("'", argv(2), "'", cvar_string("g_maplist")));
98 #endif
99                         return TRUE;
100                 }
101                 else if(argv(1) == "remove" && argc == 3)
102                 {
103                         s = argv(2);
104 #ifdef MAPINFO
105                         n = tokenizebyseparator(cvar_string("g_maplist"), " ");
106 #else
107                         n = tokenize(cvar_string("g_maplist"));
108 #endif
109                         s2 = "";
110                         for(i = 0; i < n; ++i)
111                                 if(argv(i) != s)
112                                 {
113 #ifdef MAPINFO
114                                         s2 = strcat(s2, " ", argv(i));
115 #else
116                                         s2 = strcat(s2, "'", argv(i), "'");
117 #endif
118                                 }
119 #ifdef MAPINFO
120                         s2 = substring(s2, 1, strlen(s2) - 1);
121 #endif
122                         cvar_set("g_maplist", s2);
123                         return TRUE;
124                 }
125                 else if(argv(1) == "shuffle" && argc == 2)
126                 {
127                         s = cvar_string("g_maplist");
128 #ifdef MAPINFO
129                         for(i = 1; i < (n = tokenizebyseparator(s, " ")); ++i)
130 #else
131                         for(i = 1; i < (n = tokenize(s)); ++i)
132 #endif
133                         {
134                                 // swap i-th item at a random position from 0 to i
135                                 // proof for even distribution:
136                                 //   n = 1: obvious
137                                 //   n -> n+1:
138                                 //     item n+1 gets at any position with chance 1/(n+1)
139                                 //     all others will get their 1/n chance reduced by factor n/(n+1)
140                                 //     to be on place n+1, their chance will be 1/(n+1)
141                                 //     1/n * n/(n+1) = 1/(n+1)
142                                 //     q.e.d.
143                                 f = ceil(random() * (i + 1)) - 1; // 0 to i
144                                 if(f == i)
145                                         continue; // no change
146
147                                 s2 = "";
148                                 for(j = 0; j < n; ++j)
149 #ifdef MAPINFO
150                                         s2 = strcat(s2, " ", argv((j == i) ? f : (j == f) ? i : j));
151                                 s = substring(s2, 1, strlen(s2) - 1);
152 #else
153                                         s2 = strcat(s2, "'", argv((j == i) ? f : (j == f) ? i : j), "'");
154                                 s = s2;
155 #endif
156                         }
157                         cvar_set("g_maplist", s);
158                         return TRUE;
159                 }
160         }
161         else if(argv(0) == "rpn")
162         {
163                 if(argc >= 2)
164                 {
165                         float rpnpos;
166                         string rpncmd;
167                         float f2;
168                         rpn_sp = 0;
169                         rpn_error = FALSE;
170                         for(rpnpos = 1; rpnpos < argc; ++rpnpos)
171                         {
172                                 rpncmd = argv(rpnpos);
173                                 f = strlen(rpncmd);
174                                 if(rpncmd == "") {
175                                 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
176                                         rpn_push(rpncmd);
177                                 } else if(substring(rpncmd, 0, 1) == "0") {
178                                         rpn_push(rpncmd);
179                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
180                                         rpn_push(rpncmd);
181                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
182                                         rpn_push(rpncmd);
183                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
184                                         rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
185                                 } else if(rpncmd == "def" || rpncmd == "=") {
186                                         s = rpn_pop();
187                                         s2 = rpn_pop();
188 #ifdef MENUQC
189                                         registercvar(s2, "", 0);
190 #else
191                                         registercvar(s2, "");
192 #endif
193                                         if(!rpn_error) // don't change cvars if a stack error had happened!
194                                                 cvar_set(s2, s);
195                                 } else if(rpncmd == "load") {
196                                         rpn_set(cvar_string(rpn_get()));
197                                 } else if(rpncmd == "exch") {
198                                         s = rpn_pop();
199                                         s2 = rpn_get();
200                                         rpn_set(s);
201                                         rpn_push(s2);
202                                 } else if(rpncmd == "dup") {
203                                         rpn_push(rpn_get());
204                                 } else if(rpncmd == "pop") {
205                                         rpn_pop();
206                                 } else if(rpncmd == "add" || rpncmd == "+") {
207                                         f = rpn_popf();
208                                         rpn_setf(rpn_getf() + f);
209                                 } else if(rpncmd == "sub" || rpncmd == "-") {
210                                         f = rpn_popf();
211                                         rpn_setf(rpn_getf() - f);
212                                 } else if(rpncmd == "mul" || rpncmd == "*") {
213                                         f = rpn_popf();
214                                         rpn_setf(rpn_getf() * f);
215                                 } else if(rpncmd == "div" || rpncmd == "/") {
216                                         f = rpn_popf();
217                                         rpn_setf(rpn_getf() / f);
218                                 } else if(rpncmd == "mod" || rpncmd == "%") {
219                                         f = rpn_popf();
220                                         f2 = rpn_getf();
221                                         rpn_setf(f2 - f * floor(f2 / f));
222                                 } else if(rpncmd == "abs") {
223                                         rpn_setf(fabs(rpn_getf()));
224                                 } else if(rpncmd == "sgn") {
225                                         f = rpn_getf();
226                                         if(f < 0)
227                                                 rpn_set("-1");
228                                         else if(f > 0)
229                                                 rpn_set("1");
230                                         else
231                                                 rpn_set("0");
232                                 } else if(rpncmd == "neg" || rpncmd == "~") {
233                                         rpn_setf(-rpn_getf());
234                                 } else if(rpncmd == "rand") {
235                                         rpn_setf(ceil(random() * rpn_getf()) - 1);
236                                 } else if(rpncmd == "union") {
237                                         // s s2 union
238                                         s2 = rpn_pop();
239                                         s = rpn_get();
240                                         f = tokenize(s);
241                                         f2 = tokenize(strcat(s, s2));
242                                         // tokens 0..(f-1) represent s
243                                         // tokens f..f2 represent s2
244                                         // UNION: add all tokens to s that are in s2 but not in s
245                                         s = "";
246                                         for(i = 0; i < f; ++i)
247                                                 s = strcat(s, "'", argv(i), "'");
248                                         for(i = f; i < f2; ++i) {
249                                                 for(j = 0; j < f; ++j)
250                                                         if(argv(i) == argv(j))
251                                                                 goto skip_union;
252                                                 s = strcat(s, "'", argv(i), "'");
253 :skip_union
254                                         }
255                                         rpn_set(s);
256                                         tokenize(command);
257                                 } else if(rpncmd == "intersection") {
258                                         // s s2 intersection
259                                         s2 = rpn_pop();
260                                         s = rpn_get();
261                                         f = tokenize(s);
262                                         f2 = tokenize(strcat(s, s2));
263                                         // tokens 0..(f-1) represent s
264                                         // tokens f..f2 represent s2
265                                         // INTERSECTION: keep only the tokens from s that are also in s2
266                                         s = "";
267                                         for(i = 0; i < f; ++i) {
268                                                 for(j = f; j < f2; ++j)
269                                                         if(argv(i) == argv(j))
270                                                         {
271                                                                 s = strcat(s, "'", argv(i), "'");
272                                                                 break;
273                                                         }
274                                         }
275                                         rpn_set(s);
276                                         tokenize(command);
277                                 } else if(rpncmd == "difference") {
278                                         // s s2 difference
279                                         s2 = rpn_pop();
280                                         s = rpn_get();
281                                         f = tokenize(s);
282                                         f2 = tokenize(strcat(s, s2));
283                                         // tokens 0..(f-1) represent s
284                                         // tokens f..f2 represent s2
285                                         // DIFFERENCE: keep only the tokens from s that are not in s2
286                                         s = "";
287                                         for(i = 0; i < f; ++i) {
288                                                 for(j = f; j < f2; ++j)
289                                                         if(argv(i) == argv(j))
290                                                                 goto skip_difference;
291                                                 s = strcat(s, "'", argv(i), "'");
292 :skip_difference
293                                         }
294                                         rpn_set(s);
295                                         tokenize(command);
296                                 } else if(rpncmd == "shuffle") {
297                                         // s shuffle
298                                         s = rpn_get();
299                                         f = tokenize(s);
300
301                                         for(i = 0; i < f - 1; ++i) {
302                                                 // move a random item from i..f-1 to position i
303                                                 s = "";
304                                                 f2 = ceil(random() * (f - i) + i) - 1;
305                                                 for(j = 0; j < i; ++j)
306                                                         s = strcat(s, "'", argv(j), "'");
307                                                 s = strcat(s, "'", argv(f2), "'");
308                                                 for(j = i; j < f; ++j)
309                                                         if(j != f2)
310                                                                 s = strcat(s, "'", argv(j), "'");
311                                                 f = tokenize(s);
312                                         }
313
314                                         rpn_set(s);
315                                         tokenize(command);
316                                 } else if(rpncmd == "fexists_assert") {
317                                         s = rpn_pop();
318                                         if(!rpn_error)
319                                         {
320                                                 f = fopen(s, FILE_READ);
321                                                 if(f != -1)
322                                                         fclose(f);
323                                                 else {
324                                                         print("rpn: ERROR: ", s, " does not exist!\n");
325                                                         rpn_error = TRUE;
326                                                 }
327                                         }
328                                 } else {
329                                         rpn_push(cvar_string(rpncmd));
330                                 }
331                                 if(rpn_error)
332                                         break;
333                         }
334                         while(rpn_sp > 0)
335                         {
336                                 s = rpn_pop();
337                                 print("rpn: still on stack: ", s, "\n");
338                         }
339                         return TRUE;
340                 }
341         }
342
343         return FALSE;
344 }