]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/gamecommand.qc
doh, no time to fix those dependencies right now
[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 mapvote_nextthink;
48 float mapvote_initialized;
49 float mapvote_keeptwotime;
50 float mapvote_timeout;
51 string mapvote_message;
52 string mapvote_screenshot_dir;
53
54 float mapvote_count;
55 float mapvote_count_real;
56 string mapvote_maps[MAPVOTE_COUNT];
57 float mapvote_maps_suggested[MAPVOTE_COUNT];
58 string mapvote_suggestions[MAPVOTE_COUNT];
59 float mapvote_suggestion_ptr;
60 float mapvote_maxlen;
61 float mapvote_voters;
62 float mapvote_votes[MAPVOTE_COUNT];
63 float mapvote_run;
64 float mapvote_detail;
65 float mapvote_abstain;
66 float mapvote_dirty;
67 .float mapvote;
68
69 void MapVote_ClearAllVotes()
70 {
71         FOR_EACH_CLIENT(other)
72                 other.mapvote = 0;
73 }
74
75 string MapVote_Suggest(string m)
76 {
77         float i;
78         if(m == "")
79                 return "That's not how to use this command.";
80         if(!cvar("g_maplist_votable_suggestions"))
81                 return "Suggestions are not accepted on this server.";
82         if(mapvote_initialized)
83                 return "Can't suggest - voting is already in progress!";
84         m = MapInfo_FixName(m);
85         if(!m)
86                 return "The map you suggested is not available on this server.";
87         if(!cvar("g_maplist_votable_override_mostrecent"))
88                 if(Map_IsRecent(m))
89                         return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
90
91         if(!MapInfo_CheckMap(m))
92                 return "The map you suggested does not support the current game mode.";
93         for(i = 0; i < mapvote_suggestion_ptr; ++i)
94                 if(mapvote_suggestions[i] == m)
95                         return "This map was already suggested.";
96         if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
97         {
98                 i = floor(random() * mapvote_suggestion_ptr);
99         }
100         else
101         {
102                 i = mapvote_suggestion_ptr;
103                 mapvote_suggestion_ptr += 1;
104         }
105         if(mapvote_suggestions[i] != "")
106                 strunzone(mapvote_suggestions[i]);
107         mapvote_suggestions[i] = strzone(m);
108         if(cvar("sv_eventlog"))
109                 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid)));
110         return strcat("Suggestion of ", m, " accepted.");
111 }
112
113 void MapVote_AddVotable(string nextMap, float isSuggestion)
114 {
115         float j;
116         if(nextMap == "")
117                 return;
118         for(j = 0; j < mapvote_count; ++j)
119                 if(mapvote_maps[j] == nextMap)
120                         return;
121         if(strlen(nextMap) > mapvote_maxlen)
122                 mapvote_maxlen = strlen(nextMap);
123         mapvote_maps[mapvote_count] = strzone(nextMap);
124         mapvote_maps_suggested[mapvote_count] = isSuggestion;
125         mapvote_count += 1;
126 }
127
128 void MapVote_SendData(float target);
129 void MapVote_Init()
130 {
131         float i;
132         float nmax, smax;
133
134         MapVote_ClearAllVotes();
135
136         mapvote_count = 0;
137         mapvote_detail = !cvar("g_maplist_votable_nodetail");
138         mapvote_abstain = cvar("g_maplist_votable_abstain");
139
140         if(mapvote_abstain)
141                 nmax = min(MAPVOTE_COUNT - 1, cvar("g_maplist_votable"));
142         else
143                 nmax = min(MAPVOTE_COUNT, cvar("g_maplist_votable"));
144         smax = min3(nmax, cvar("g_maplist_votable_suggestions"), mapvote_suggestion_ptr);
145
146         if(mapvote_suggestion_ptr)
147                 for(i = 0; i < 100 && mapvote_count < smax; ++i)
148                         MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], TRUE);
149
150         for(i = 0; i < 100 && mapvote_count < nmax; ++i)
151                 MapVote_AddVotable(GetNextMap(), FALSE);
152
153         if(mapvote_count == 0)
154         {
155                 bprint( "Maplist contains no single playable map!  Resetting it to default map list.\n" );
156                 cvar_set("g_maplist", MapInfo_ListAllowedMaps(0, MAPINFO_FLAG_HIDDEN));
157                 localcmd("\nmenu_cmd sync\n");
158                 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
159                         MapVote_AddVotable(GetNextMap(), FALSE);
160         }
161
162         mapvote_count_real = mapvote_count;
163         if(mapvote_abstain)
164                 MapVote_AddVotable("don't care", 0);
165
166         //dprint("mapvote count is ", ftos(mapvote_count), "\n");
167
168         mapvote_keeptwotime = time + cvar("g_maplist_votable_keeptwotime");
169         mapvote_timeout = time + cvar("g_maplist_votable_timeout");
170         if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
171                 mapvote_keeptwotime = 0;
172         mapvote_message = "Choose a map and press its key!";
173
174         mapvote_screenshot_dir = cvar_string("g_maplist_votable_screenshot_dir");
175         if(mapvote_screenshot_dir == "")
176                 mapvote_screenshot_dir = "maps";
177         mapvote_screenshot_dir = strzone(mapvote_screenshot_dir);
178
179         if(!cvar("g_maplist_textonly"))
180                 MapVote_SendData(MSG_ALL);
181 }
182
183 void MapVote_SendPicture(float id)
184 {
185         msg_entity = self;
186         WriteByte(MSG_ONE, SVC_TEMPENTITY);
187         WriteByte(MSG_ONE, TE_CSQC_MAPVOTE);
188         WriteByte(MSG_ONE, MAPVOTE_NET_PIC);
189         WriteByte(MSG_ONE, id);
190         WritePicture(MSG_ONE, strcat(mapvote_screenshot_dir, "/", mapvote_maps[id]), 3072);
191 }
192
193 float GameCommand_MapVote(string cmd)
194 {
195         if(!intermission_running)
196                 return FALSE;
197         if(!cvar("g_maplist_textonly"))
198         {
199                 if(cmd == "mv_getpic")
200                 {
201                         MapVote_SendPicture(stof(argv(1)));
202                         return TRUE;
203                 }
204         }
205
206         return FALSE;
207 }
208
209 float MapVote_GetMapMask()
210 {
211         float mask, i, power;
212         mask = 0;
213         for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
214                 if(mapvote_maps[i] != "")
215                         mask |= power;
216         return mask;
217 }
218
219 void MapVote_SendData(float targ)
220 {
221         string mapfile, pakfile;
222         float i, o;
223         WriteByte(targ, SVC_TEMPENTITY);
224         WriteByte(targ, TE_CSQC_CONFIG);
225         WriteString(targ, "mv_screenshot_dir");
226         WriteString(targ, mapvote_screenshot_dir);
227
228         WriteByte(targ, SVC_TEMPENTITY);
229         WriteByte(targ, TE_CSQC_MAPVOTE);
230         WriteByte(targ, MAPVOTE_NET_INIT);
231
232         WriteByte(targ, mapvote_count);
233         WriteByte(targ, mapvote_abstain);
234         WriteByte(targ, mapvote_detail);
235         WriteCoord(targ, mapvote_timeout);
236         if(mapvote_count <= 8)
237                 WriteByte(targ, MapVote_GetMapMask());
238         else
239                 WriteShort(targ, MapVote_GetMapMask());
240         for(i = 0; i < mapvote_count; ++i)
241                 if(mapvote_maps[i] != "")
242                 {
243                         WriteString(targ, mapvote_maps[i]);
244                         mapfile = strcat(mapvote_screenshot_dir, "/", mapvote_maps[i]);
245                         pakfile = whichpack(strcat(mapfile, ".tga"));
246                         if(pakfile == "")
247                                 pakfile = whichpack(strcat(mapfile, ".jpg"));
248                         if(pakfile == "")
249                                 pakfile = whichpack(strcat(mapfile, ".png"));
250                         print("pakfile is ", pakfile, "\n");
251                         for(o = strstr(pakfile, "/", 0)+1; o > 0; o = strstr(pakfile, "/", 0)+1)
252                                 pakfile = substring(pakfile, o, 999);
253                         WriteString(targ, pakfile);
254                 }
255 }
256
257 void MapVote_UpdateData(float targ)
258 {
259         float i;
260         WriteByte(targ, SVC_TEMPENTITY);
261         WriteByte(targ, TE_CSQC_MAPVOTE);
262         WriteByte(targ, MAPVOTE_NET_UPDATE);
263         if(mapvote_count <= 8)
264                 WriteByte(targ, MapVote_GetMapMask());
265         else
266                 WriteShort(targ, MapVote_GetMapMask());
267         if(mapvote_detail)
268                 for(i = 0; i < mapvote_count; ++i)
269                         if(mapvote_maps[i] != "")
270                                 WriteByte(targ, mapvote_votes[i]);
271 }
272
273 void MapVote_TellVote(float targ, float vote)
274 {
275         WriteByte(targ, SVC_TEMPENTITY);
276         WriteByte(targ, TE_CSQC_MAPVOTE);
277         WriteByte(targ, MAPVOTE_NET_OWNVOTE);
278         WriteByte(targ, vote);
279 }
280
281 float MapVote_Finished(float mappos)
282 {
283         string result;
284         float i;
285         float didntvote;
286
287         if(cvar("sv_eventlog"))
288         {
289                 result = strcat(":vote:finished:", mapvote_maps[mappos]);
290                 result = strcat(result, ":", ftos(mapvote_votes[mappos]), "::");
291                 didntvote = mapvote_voters;
292                 for(i = 0; i < mapvote_count; ++i)
293                         if(mapvote_maps[i] != "")
294                         {
295                                 didntvote -= mapvote_votes[i];
296                                 if(i != mappos)
297                                 {
298                                         result = strcat(result, ":", mapvote_maps[i]);
299                                         result = strcat(result, ":", ftos(mapvote_votes[i]));
300                                 }
301                         }
302                 result = strcat(result, ":didn't vote:", ftos(didntvote));
303
304                 GameLogEcho(result);
305                 if(mapvote_maps_suggested[mappos])
306                         GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
307         }
308
309         FOR_EACH_REALCLIENT(other)
310                 FixClientCvars(other);
311
312         Map_Goto_SetStr(mapvote_maps[mappos]);
313         Map_Goto();
314         alreadychangedlevel = TRUE;
315         return TRUE;
316 }
317 void MapVote_CheckRules_1()
318 {
319         float i;
320
321         for(i = 0; i < mapvote_count; ++i) if(mapvote_maps[i] != "")
322         {
323                 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
324                 mapvote_votes[i] = 0;
325         }
326
327         mapvote_voters = 0;
328         FOR_EACH_REALCLIENT(other)
329         {
330                 ++mapvote_voters;
331                 if(other.mapvote)
332                 {
333                         i = other.mapvote - 1;
334                         //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
335                         mapvote_votes[i] = mapvote_votes[i] + 1;
336                 }
337         }
338 }
339
340 float MapVote_CheckRules_2()
341 {
342         float i;
343         float firstPlace, secondPlace;
344         float firstPlaceVotes, secondPlaceVotes;
345         float mapvote_voters_real;
346         string result;
347
348         mapvote_voters_real = mapvote_voters;
349         if(mapvote_abstain)
350                 mapvote_voters_real -= mapvote_votes[mapvote_count - 1];
351
352         RandomSelection_Init();
353         for(i = 0; i < mapvote_count_real; ++i) if(mapvote_maps[i] != "")
354                 RandomSelection_Add(world, i, 1, mapvote_votes[i]);
355         firstPlace = RandomSelection_chosen_float;
356         firstPlaceVotes = RandomSelection_best_priority;
357         //dprint("First place: ", ftos(firstPlace), "\n");
358         //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
359
360         RandomSelection_Init();
361         for(i = 0; i < mapvote_count_real; ++i) if(mapvote_maps[i] != "")
362                 if(i != firstPlace)
363                         RandomSelection_Add(world, i, 1, mapvote_votes[i]);
364         secondPlace = RandomSelection_chosen_float;
365         secondPlaceVotes = RandomSelection_best_priority;
366         //dprint("Second place: ", ftos(secondPlace), "\n");
367         //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
368
369         if(firstPlace == -1)
370                 error("No first place in map vote... WTF?");
371
372         if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
373                 return MapVote_Finished(firstPlace);
374
375         if(mapvote_keeptwotime)
376                 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
377                 {
378                         float didntvote;
379                         mapvote_dirty = TRUE;
380                         mapvote_message = "Now decide between the TOP TWO!";
381                         mapvote_keeptwotime = 0;
382                         result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
383                         result = strcat(result, ":", ftos(firstPlaceVotes));
384                         result = strcat(result, ":", mapvote_maps[secondPlace]);
385                         result = strcat(result, ":", ftos(secondPlaceVotes), "::");
386                         didntvote = mapvote_voters;
387                         for(i = 0; i < mapvote_count; ++i)
388                                 if(mapvote_maps[i] != "")
389                                 {
390                                         didntvote -= mapvote_votes[i];
391                                         if(i != firstPlace)
392                                                 if(i != secondPlace)
393                                                 {
394                                                         result = strcat(result, ":", mapvote_maps[i]);
395                                                         result = strcat(result, ":", ftos(mapvote_votes[i]));
396                                                         if(i < mapvote_count_real)
397                                                         {
398                                                                 strunzone(mapvote_maps[i]);
399                                                                 mapvote_maps[i] = "";
400                                                         }
401                                                 }
402                                 }
403                         result = strcat(result, ":didn't vote:", ftos(didntvote));
404                         if(cvar("sv_eventlog"))
405                                 GameLogEcho(result);
406                 }
407
408         return FALSE;
409 }
410 void MapVote_Tick()
411 {
412         string msgstr;
413         string tmp;
414         float i;
415         float keeptwo;
416         float totalvotes;
417
418         keeptwo = mapvote_keeptwotime;
419         MapVote_CheckRules_1(); // count
420         if(MapVote_CheckRules_2()) // decide
421                 return;
422
423         totalvotes = 0;
424         FOR_EACH_REALCLIENT(other)
425         {
426                 // hide scoreboard again
427                 if(other.health != 2342)
428                 {
429                         other.health = 2342;
430                         other.impulse = 0;
431                         if(clienttype(other) == CLIENTTYPE_REAL)
432                         {
433                                 if(cvar("g_maplist_textonly"))
434                                         stuffcmd(other, "\nin_bind 7 1 \"impulse 1\"; in_bind 7 2 \"impulse 2\"; in_bind 7 3 \"impulse 3\"; in_bind 7 4 \"impulse 4\"; in_bind 7 5 \"impulse 5\"; in_bind 7 6 \"impulse 6\"; in_bind 7 7 \"impulse 7\"; in_bind 7 8 \"impulse 8\"; in_bind 7 9 \"impulse 9\"; in_bind 7 0 \"impulse 10\"; in_bind 7 KP_1 \"impulse 1\"; in_bind 7 KP_2 \"impulse 2\"; in_bind 7 KP_3 \"impulse 3\"; in_bind 7 KP_4 \"impulse 4\"; in_bind 7 KP_5 \"impulse 5\"; in_bind 7 KP_6 \"impulse 6\"; in_bind 7 KP_7 \"impulse 7\"; in_bind 7 KP_8 \"impulse 8\"; in_bind 7 KP_9 \"impulse 9\"; in_bind 7 KP_0 \"impulse 10\"; in_bindmap 7 0\n");
435
436                                 msg_entity = other;
437                                 WriteByte(MSG_ONE, SVC_FINALE);
438                                 WriteString(MSG_ONE, "");
439                         }
440                 }
441
442                 // notify about keep-two
443                 if(keeptwo != 0 && mapvote_keeptwotime == 0)
444                         play2(other, "misc/invshot.wav");
445
446                 // clear possibly invalid votes
447                 if(mapvote_maps[other.mapvote - 1] == "")
448                         other.mapvote = 0;
449                 // use impulses as new vote
450                 if(other.impulse >= 1 && other.impulse <= mapvote_count)
451                         if(mapvote_maps[other.impulse - 1] != "")
452                         {
453                                 other.mapvote = other.impulse;
454                                 if(mapvote_detail)
455                                         mapvote_dirty = TRUE;
456
457                                 msg_entity = other;
458                                 MapVote_TellVote(MSG_ONE, other.mapvote);
459                         }
460                 other.impulse = 0;
461
462                 if(other.mapvote)
463                         ++totalvotes;
464         }
465
466         MapVote_CheckRules_1(); // just count
467
468         if(!cvar("g_maplist_textonly"))
469         if(mapvote_dirty) // 1 if "keeptwo" or "impulse" happened before
470         {
471                 MapVote_UpdateData(MSG_BROADCAST);
472                 mapvote_dirty = FALSE;
473         }
474
475         if(cvar("g_maplist_textonly"))
476         {
477                 FOR_EACH_REALCLIENT(other)
478                 {
479                         // display voting screen
480                         msgstr = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
481                         msgstr = substring(msgstr, 0, strlen(msgstr) - mapvote_count);
482                         if(mapvote_abstain)
483                                 msgstr = substring(msgstr, 1, strlen(msgstr) - 1);
484                         msgstr = strcat(msgstr, mapvote_message);
485                         msgstr = strcat(msgstr, "\n\n");
486                         for(i = 0; i < mapvote_count; ++i)
487                                 if(mapvote_maps[i] == "")
488                                         msgstr = strcat(msgstr, "\n");
489                                 else
490                                 {
491                                         tmp = mapvote_maps[i];
492                                         tmp = strpad(mapvote_maxlen, tmp);
493                                         tmp = strcat(ftos(mod(i + 1, 10)), ": ", tmp);
494                                         if(mapvote_detail)
495                                         {
496                                                 tmp = strcat(tmp, " ^2(", ftos(mapvote_votes[i]), " vote");
497                                                 if(mapvote_votes[i] != 1)
498                                                         tmp = strcat(tmp, "s");
499                                                 tmp = strcat(tmp, ")");
500                                                 tmp = strpad(mapvote_maxlen + 15, tmp);
501                                         }
502                                         if(mapvote_abstain)
503                                                 if(i == mapvote_count - 1)
504                                                         msgstr = strcat(msgstr, "\n");
505                                         if(other.mapvote == i + 1)
506                                                 msgstr = strcat(msgstr, "^3> ", tmp, "\n");
507                                         else
508                                                 msgstr = strcat(msgstr, "^7  ", tmp, "\n");
509                                 }
510
511                         msgstr = strcat(msgstr, "\n\n^2", ftos(totalvotes), " vote");
512                         if(totalvotes != 1)
513                                 msgstr = strcat(msgstr, "s");
514                         msgstr = strcat(msgstr, " cast");
515                         i = ceil(mapvote_timeout - time);
516                         msgstr = strcat(msgstr, "\n", ftos(i), " second");
517                         if(i != 1)
518                                 msgstr = strcat(msgstr, "s");
519                         msgstr = strcat(msgstr, " left");
520
521                         centerprint_atprio(other, CENTERPRIO_MAPVOTE, msgstr);
522                 }
523         }
524 }
525 void MapVote_Start()
526 {
527         if(mapvote_run)
528                 return;
529
530         MapInfo_Enumerate();
531         if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0, (g_maplist_allow_hidden ? MAPINFO_FLAG_HIDDEN : 0), 1))
532                 mapvote_run = TRUE;
533 }
534 void MapVote_Think()
535 {
536         if(!mapvote_run)
537                 return;
538
539         if(alreadychangedlevel)
540                 return;
541
542         if(time < mapvote_nextthink)
543                 return;
544         //dprint("tick\n");
545
546         mapvote_nextthink = time + 0.5;
547
548         if(!mapvote_initialized)
549         {
550                 mapvote_initialized = TRUE;
551                 if(DoNextMapOverride())
552                         return;
553                 if(!cvar("g_maplist_votable") || player_count <= 0)
554                 {
555                         GotoNextMap();
556                         return;
557                 }
558                 MapVote_Init();
559         }
560
561         MapVote_Tick();
562 };
563
564 string GotoMap(string m)
565 {
566         if(!MapInfo_CheckMap(m))
567                 return "The map you chose is not available on this server.";
568         cvar_set("nextmap", m);
569         cvar_set("timelimit", "-1");
570         if(mapvote_initialized || alreadychangedlevel)
571         {
572                 if(DoNextMapOverride())
573                         return "Map switch initiated.";
574                 else
575                         return "Hm... no. For some reason I like THIS map more.";
576         }
577         else
578                 return "Map switch will happen after scoreboard.";
579 }
580
581 float GameCommand_Generic(string command)
582 {
583         float argc;
584         float i, j, f, n;
585         string s, s2;
586         argc = tokenize_sane(command);
587         if(argv(0) == "help")
588         {
589                 print("  rpn EXPRESSION... - a RPN calculator.\n");
590                 print("    Operator description (x: string, s: set, f: float):\n");
591                 print("    x pop ----------------------------->     : removes the top\n");
592                 print("    x dup -----------------------------> x x : duplicates the top\n");
593                 print("    x x exch --------------------------> x x : swap the top two\n");
594                 print("    /cvarname load --------------------> x   : loads a cvar\n");
595                 print("    /cvarname x def ------------------->     : writes to a cvar\n");
596                 print("    f f add|sub|mul|div|mod|max|min ---> f   : adds/... two numbers\n");
597                 print("    f f eq|ne|gt|ge|lt|le -------------> f   : compares two numbers\n");
598                 print("    f neg|abs|sgn|rand|floor|ceil------> f   : negates/... a number\n");
599                 print("    f f f bound -----------------------> f   : bounds the middle number\n");
600                 print("    f1 f2 b when ----------------------> f   : f1 if b, f2 otherwise\n");
601                 print("    s s union|intersection|difference -> s   : set operations\n");
602                 print("    s shuffle -------------------------> s   : randomly arrange elements\n");
603                 print("    x dbpush -------------------------->     : pushes the top onto the database\n");
604                 print("    dbpop|dbget -----------------------> x   : removes/reads DB's top\n");
605                 print("    dblen|dbat ------------------------> f   : gets the DB's size/cursor pos\n");
606                 print("    dbclr ----------------------------->     : clear the DB\n");
607                 print("    s dbsave|dbload-------------------->     : save/load the DB to/from a file\n");
608                 print("    x dbins --------------------------->     : moves the top into the DB\n");
609                 print("    dbext|dbread ----------------------> x   : extract/get from the DB's cursor\n");
610                 print("    f dbmov|dbgoto -------------------->     : move or set the DB's cursor\n");
611                 print("    Set operations operate on 'such''strings'.\n");
612                 print("    Unknown tokens insert their cvar value.\n");
613                 print("  maplist add map\n");
614                 print("  maplist remove map\n");
615                 print("  maplist shuffle\n");
616                 print("  suggestmap map\n");
617                 return TRUE;
618         }
619         
620         if(argv(0) == "maplist")
621         {
622                 if(argv(1) == "add" && argc == 3)
623                 {
624                         f = fopen(strcat("maps/", argv(2), ".bsp"), FILE_READ);
625                         if(f != -1)
626                                 fclose(f);
627                         else {
628                                 print("maplist: ERROR: ", argv(2), " does not exist!\n");
629                                 return TRUE;
630                         }
631                         if(cvar_string("g_maplist") == "")
632                                 cvar_set("g_maplist", argv(2));
633                         else
634                                 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
635                         return TRUE;
636                 }
637                 else if(argv(1) == "remove" && argc == 3)
638                 {
639                         s = argv(2);
640                         n = tokenizebyseparator(cvar_string("g_maplist"), " ");
641                         s2 = "";
642                         for(i = 0; i < n; ++i)
643                                 if(argv(i) != s)
644                                 {
645                                         s2 = strcat(s2, " ", argv(i));
646                                 }
647                         s2 = substring(s2, 1, strlen(s2) - 1);
648                         cvar_set("g_maplist", s2);
649                         return TRUE;
650                 }
651                 else if(argv(1) == "shuffle" && argc == 2)
652                 {
653                         s = cvar_string("g_maplist");
654                         for(i = 1; i < (n = tokenizebyseparator(s, " ")); ++i)
655                         {
656                                 // swap i-th item at a random position from 0 to i
657                                 // proof for even distribution:
658                                 //   n = 1: obvious
659                                 //   n -> n+1:
660                                 //     item n+1 gets at any position with chance 1/(n+1)
661                                 //     all others will get their 1/n chance reduced by factor n/(n+1)
662                                 //     to be on place n+1, their chance will be 1/(n+1)
663                                 //     1/n * n/(n+1) = 1/(n+1)
664                                 //     q.e.d.
665                                 f = floor(random() * (i + 1)); // 0 to i
666                                 if(f == i)
667                                         continue; // no change
668
669                                 s2 = "";
670                                 for(j = 0; j < n; ++j)
671                                         s2 = strcat(s2, " ", argv((j == i) ? f : (j == f) ? i : j));
672                                 s = substring(s2, 1, strlen(s2) - 1);
673                         }
674                         cvar_set("g_maplist", s);
675                         return TRUE;
676                 }
677         }
678         else if(argv(0) == "rpn")
679         {
680                 if(!rpn_db)
681                 {
682                         rpn_db = db_create();
683                         db_put(rpn_db, "stack.pointer", "0");
684                         db_put(rpn_db, "stack.pos", "-1");
685                 }
686                 if(argc >= 2)
687                 {
688                         float rpnpos;
689                         string rpncmd;
690                         float f2, f3;
691                         rpn_sp = 0;
692                         rpn_error = FALSE;
693                         for(rpnpos = 1; rpnpos < argc; ++rpnpos)
694                         {
695                                 rpncmd = argv(rpnpos);
696                                 f = strlen(rpncmd);
697                                 if(rpncmd == "") {
698                                 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
699                                         rpn_push(rpncmd);
700                                 } else if(substring(rpncmd, 0, 1) == "0") {
701                                         rpn_push(rpncmd);
702                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
703                                         rpn_push(rpncmd);
704                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
705                                         rpn_push(rpncmd);
706                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
707                                         rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
708                                 } else if(rpncmd == "clear") {
709                                         rpn_sp = 0;
710                                 } else if(rpncmd == "def" || rpncmd == "=") {
711                                         s = rpn_pop();
712                                         s2 = rpn_pop();
713 #ifdef MENUQC
714                                         registercvar(s2, "", 0);
715 #else
716                                         registercvar(s2, "");
717 #endif
718                                         if(!rpn_error) // don't change cvars if a stack error had happened!
719                                                 cvar_set(s2, s);
720                                 } else if(rpncmd == "defs" || rpncmd == "@") {
721                                         s = "";
722                                         i = rpn_popf();
723                                         j = (i == 0);
724                                         while(rpn_sp > 1 && (j || i > 0))
725                                         {
726                                                 s = strcat("/", rpn_pop(), " ", s);
727                                                 --i;
728                                         }
729                                         s2 = rpn_pop();
730 #ifdef MENUQC
731                                         registercvar(s2, "", 0);
732 #else
733                                         registercvar(s2, "");
734 #endif
735                                         if(!rpn_error) // don't change cvars if a stack error had happened!
736                                                 cvar_set(s2, s);
737                                 } else if(rpncmd == "load") {
738                                         rpn_set(cvar_string(rpn_get()));
739                                 } else if(rpncmd == "exch") {
740                                         s = rpn_pop();
741                                         s2 = rpn_get();
742                                         rpn_set(s);
743                                         rpn_push(s2);
744                                 } else if(rpncmd == "dup") {
745                                         rpn_push(rpn_get());
746                                 } else if(rpncmd == "pop") {
747                                         rpn_pop();
748                                 } else if(rpncmd == "add" || rpncmd == "+") {
749                                         f = rpn_popf();
750                                         rpn_setf(rpn_getf() + f);
751                                 } else if(rpncmd == "sub" || rpncmd == "-") {
752                                         f = rpn_popf();
753                                         rpn_setf(rpn_getf() - f);
754                                 } else if(rpncmd == "mul" || rpncmd == "*") {
755                                         f = rpn_popf();
756                                         rpn_setf(rpn_getf() * f);
757                                 } else if(rpncmd == "div" || rpncmd == "/") {
758                                         f = rpn_popf();
759                                         rpn_setf(rpn_getf() / f);
760                                 } else if(rpncmd == "mod" || rpncmd == "%") {
761                                         f = rpn_popf();
762                                         f2 = rpn_getf();
763                                         rpn_setf(f2 - f * floor(f2 / f));
764                                 } else if(rpncmd == "abs") {
765                                         rpn_setf(fabs(rpn_getf()));
766                                 } else if(rpncmd == "sgn") {
767                                         f = rpn_getf();
768                                         if(f < 0)
769                                                 rpn_set("-1");
770                                         else if(f > 0)
771                                                 rpn_set("1");
772                                         else
773                                                 rpn_set("0");
774                                 } else if(rpncmd == "neg" || rpncmd == "~") {
775                                         rpn_setf(-rpn_getf());
776                                 } else if(rpncmd == "floor" || rpncmd == "f") {
777                                         rpn_setf(floor(rpn_getf()));
778                                 } else if(rpncmd == "ceil" || rpncmd == "c") {
779                                         rpn_setf(ceil(rpn_getf()));
780                                 } else if(rpncmd == "max") {
781                                         f = rpn_popf();
782                                         f2 = rpn_getf();
783                                         rpn_setf(max(f2, f));
784                                 } else if(rpncmd == "min") {
785                                         f = rpn_popf();
786                                         f2 = rpn_getf();
787                                         rpn_setf(min(f2, f));
788                                 } else if(rpncmd == "bound") {
789                                         f = rpn_popf();
790                                         f2 = rpn_popf();
791                                         f3 = rpn_getf();
792                                         rpn_setf(bound(f3, f2, f));
793                                 } else if(rpncmd == "when") {
794                                         f = rpn_popf();
795                                         f2 = rpn_popf();
796                                         f3 = rpn_getf();
797                                         if(f)
798                                                 rpn_setf(f3);
799                                         else
800                                                 rpn_setf(f2);
801                                 } else if(rpncmd == ">" || rpncmd == "gt") {
802                                         f = rpn_popf();
803                                         rpn_setf(rpn_getf() > f);
804                                 } else if(rpncmd == "<" || rpncmd == "lt") {
805                                         f = rpn_popf();
806                                         rpn_setf(rpn_getf() < f);
807                                 } else if(rpncmd == "==" || rpncmd == "eq") {
808                                         f = rpn_popf();
809                                         rpn_setf(rpn_getf() == f);
810                                 } else if(rpncmd == ">=" || rpncmd == "ge") {
811                                         f = rpn_popf();
812                                         rpn_setf(rpn_getf() >= f);
813                                 } else if(rpncmd == "<=" || rpncmd == "le") {
814                                         f = rpn_popf();
815                                         rpn_setf(rpn_getf() <= f);
816                                 } else if(rpncmd == "!=" || rpncmd == "ne") {
817                                         f = rpn_popf();
818                                         rpn_setf(rpn_getf() != f);
819                                 } else if(rpncmd == "rand") {
820                                         rpn_setf(ceil(random() * rpn_getf()) - 1);
821                                 } else if(rpncmd == "dbpush") {
822                                         s = rpn_pop();
823                                         if(!rpn_error)
824                                         {
825                                                 i = stof(db_get(rpn_db, "stack.pointer"));
826                                                 db_put(rpn_db, "stack.pointer", ftos(i+1));
827                                                 db_put(rpn_db, strcat("stack.", ftos(i)), s);
828                                         }
829                                         if(!i)
830                                                 db_put(rpn_db, "stack.pos", "0");
831                                 } else if(rpncmd == "dbpop") {
832                                         i = stof(db_get(rpn_db, "stack.pointer"));
833                                         if(i)
834                                         {
835                                                 s = ftos(i-1);
836                                                 db_put(rpn_db, "stack.pointer", s);
837                                                 rpn_push(db_get(rpn_db, strcat("stack.", s)));
838                                                 j = stof(db_get(rpn_db, "stack.pos"));
839                                                 if(j >= i)
840                                                         db_put(rpn_db, "stack.pos", ftos(i-2));
841                                         } else {
842                                                 rpn_error = 1;
843                                                 print("rpn: database underflow\n");
844                                         }
845                                 } else if(rpncmd == "dbget") {
846                                         
847                                         i = stof(db_get(rpn_db, "stack.pointer"));
848                                         if(i)
849                                         {
850                                                 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
851                                         } else {
852                                                 rpn_error = 1;
853                                                 print("rpn: database empty\n");
854                                         }
855                                 } else if(rpncmd == "dblen") {
856                                         rpn_push(db_get(rpn_db, "stack.pointer"));
857                                 } else if(rpncmd == "dbclr") {
858                                         db_close(rpn_db);
859                                         rpn_db = db_create();
860                                         db_put(rpn_db, "stack.pointer", "0");
861                                         db_put(rpn_db, "stack.pos", "-1");
862                                 } else if(rpncmd == "dbsave") {
863                                         s = rpn_pop();
864                                         if(!rpn_error)
865                                                 db_save(rpn_db, s);
866                                 } else if(rpncmd == "dbload") {
867                                         s = rpn_pop();
868                                         if(!rpn_error)
869                                         {
870                                                 db_close(rpn_db);
871                                                 rpn_db = db_load(s);
872                                         }
873                                 } else if(rpncmd == "dbins") {
874                                         s = rpn_pop();
875                                         if(!rpn_error)
876                                                 //if(rpn_sp > 0)
877                                         {
878                                                 j = stof(db_get(rpn_db, "stack.pointer"));
879                                                 i = stof(db_get(rpn_db, "stack.pos"));
880                                                 
881                                                 if(i < 0)
882                                                 {
883                                                         i = 0;
884                                                         db_put(rpn_db, "stack.pos", "0");
885                                                 }
886                                                 
887                                                 db_put(rpn_db, "stack.pointer", ftos(j+1));
888                                                 for(--j; j >= i; --j)
889                                                 {
890                                                         db_put(rpn_db, strcat("stack.", ftos(j+1)),
891                                                                db_get(rpn_db, (strcat("stack.", ftos(j))))
892                                                                 );
893                                                 }
894                                                 db_put(rpn_db, strcat("stack.", ftos(i)), s);
895                                         }
896                                 } else if(rpncmd == "dbext") {
897                                         j = stof(db_get(rpn_db, "stack.pointer"));
898                                         i = stof(db_get(rpn_db, "stack.pos"));
899                                         if(!j)
900                                         {
901                                                 rpn_error = TRUE;
902                                                 print("rpn: empty database\n");
903                                         } else {
904                                                 --j;
905                                                 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
906                                                 db_put(rpn_db, "stack.pointer", ftos(j));
907                                                 if(i == j)
908                                                 {
909                                                         db_put(rpn_db, "stack.pos", ftos(j-1));
910                                                 } else {
911                                                         while(i < j)
912                                                         {
913                                                                 db_put(rpn_db, strcat("stack.", ftos(i)),
914                                                                        db_get(rpn_db, (strcat("stack.", ftos(i+1))))
915                                                                         );
916                                                                 ++i;
917                                                         }
918                                                 }
919                                         }
920                                 } else if(rpncmd == "dbread") {
921                                         s = db_get(rpn_db, "stack.pos");
922                                         if(stof(s) >= 0)
923                                         {
924                                                 rpn_push(db_get(rpn_db, strcat("stack.", s)));
925                                         } else {
926                                                 rpn_error = 1;
927                                                 print("rpn: empty database\n");
928                                         }
929                                 } else if(rpncmd == "dbat") {
930                                         rpn_push(db_get(rpn_db, "stack.pos"));
931                                 } else if(rpncmd == "dbmov") {
932                                         j = stof(db_get(rpn_db, "stack.pointer"));
933                                         i = stof(db_get(rpn_db, "stack.pos"));
934                                         i += rpn_popf();
935                                         if(!rpn_error)
936                                         {
937                                                 if(i < 0 || i >= j)
938                                                 {
939                                                         print("rpn: database cursor out of bounds\n");
940                                                         rpn_error = TRUE;
941                                                 }
942                                                 if(!rpn_error)
943                                                 {
944                                                         db_put(rpn_db, "stack.pos", ftos(i));
945                                                 }
946                                         }
947                                 } else if(rpncmd == "dbgoto") {
948                                         s = rpn_pop();
949                                         j = stof(db_get(rpn_db, "stack.pointer"));
950                                         if(!j)
951                                         {
952                                                 rpn_error = TRUE;
953                                                 print("rpn: empty database, cannot move cursor\n");
954                                         }
955                                         if(!rpn_error)
956                                         {
957                                                 if(s == "end")
958                                                         i = stof(db_get(rpn_db, "stack.pointer"))-1;
959                                                 else if(s == "beg")
960                                                         i = 0;
961                                                 else
962                                                         i = stof(s);
963                                                 
964                                                 j = stof(db_get(rpn_db, "stack.pointer"));
965                                                 if(i < 0 || i >= j)
966                                                 {
967                                                         print("rpn: database cursor destination out of bounds\n");
968                                                         rpn_error = TRUE;
969                                                 }
970                                                 if(!rpn_error)
971                                                 {
972                                                         db_put(rpn_db, "stack.pos", ftos(i));
973                                                 }
974                                         }
975                                 } else if(rpncmd == "union") {
976                                         // s s2 union
977                                         s2 = rpn_pop();
978                                         s = rpn_get();
979                                         f = tokenize_sane(s);
980                                         f2 = tokenize_sane(strcat(s, " ", s2));
981                                         // tokens 0..(f-1) represent s
982                                         // tokens f..f2 represent s2
983                                         // UNION: add all tokens to s that are in s2 but not in s
984                                         s = "";
985                                         for(i = 0; i < f; ++i)  
986                                                 s = strcat(s, " ", argv(i));
987                                         for(i = f; i < f2; ++i) {
988                                                 for(j = 0; j < f; ++j)
989                                                         if(argv(i) == argv(j))
990                                                                 goto skip_union;
991                                                 s = strcat(s, " ", argv(i));
992 :skip_union
993                                         }
994                                         if(substring(s, 0, 1) == " ")
995                                                 s = substring(s, 1, 99999);
996                                         rpn_set(s);
997                                         tokenize_sane(command);
998                                 } else if(rpncmd == "intersection") {
999                                         // s s2 intersection
1000                                         s2 = rpn_pop();
1001                                         s = rpn_get();
1002                                         f = tokenize_sane(s);
1003                                         f2 = tokenize_sane(strcat(s, " ", s2));
1004                                         // tokens 0..(f-1) represent s
1005                                         // tokens f..f2 represent s2
1006                                         // INTERSECTION: keep only the tokens from s that are also in s2
1007                                         s = "";
1008                                         for(i = 0; i < f; ++i) {
1009                                                 for(j = f; j < f2; ++j)
1010                                                         if(argv(i) == argv(j))
1011                                                         {
1012                                                                 s = strcat(s, " ", argv(i));
1013                                                                 break;
1014                                                         }
1015                                         }
1016                                         if(substring(s, 0, 1) == " ")
1017                                                 s = substring(s, 1, 99999);
1018                                         rpn_set(s);
1019                                         tokenize_sane(command);
1020                                 } else if(rpncmd == "difference") {
1021                                         // s s2 difference
1022                                         s2 = rpn_pop();
1023                                         s = rpn_get();
1024                                         f = tokenize_sane(s);
1025                                         f2 = tokenize_sane(strcat(s, " ", s2));
1026                                         // tokens 0..(f-1) represent s
1027                                         // tokens f..f2 represent s2
1028                                         // DIFFERENCE: keep only the tokens from s that are not in s2
1029                                         s = "";
1030                                         for(i = 0; i < f; ++i) {
1031                                                 for(j = f; j < f2; ++j)
1032                                                         if(argv(i) == argv(j))
1033                                                                 goto skip_difference;
1034                                                 s = strcat(s, " ", argv(i));
1035 :skip_difference
1036                                         }
1037                                         if(substring(s, 0, 1) == " ")
1038                                                 s = substring(s, 1, 99999);
1039                                         rpn_set(s);
1040                                         tokenize_sane(command);
1041                                 } else if(rpncmd == "shuffle") {
1042                                         // s shuffle
1043                                         s = rpn_get();
1044                                         f = tokenize_sane(s);
1045
1046                                         for(i = 0; i < f - 1; ++i) {
1047                                                 // move a random item from i..f-1 to position i
1048                                                 s = "";
1049                                                 f2 = floor(random() * (f - i) + i);
1050                                                 for(j = 0; j < i; ++j)
1051                                                         s = strcat(s, " ", argv(j));
1052                                                 s = strcat(s, " ", argv(f2));
1053                                                 for(j = i; j < f; ++j)
1054                                                         if(j != f2)
1055                                                                 s = strcat(s, " ", argv(j));
1056                                                 f = tokenize_sane(s);
1057                                         }
1058
1059                                         if(substring(s, 0, 1) == " ")
1060                                                 s = substring(s, 1, 99999);
1061                                         rpn_set(s);
1062                                         tokenize_sane(command);
1063                                         } else if(rpncmd == "fexists_assert") {
1064                                         s = rpn_pop();
1065                                         if(!rpn_error)
1066                                         {
1067                                                 f = fopen(s, FILE_READ);
1068                                                 if(f != -1)
1069                                                         fclose(f);
1070                                                 else {
1071                                                         print("rpn: ERROR: ", s, " does not exist!\n");
1072                                                         rpn_error = TRUE;
1073                                                 }
1074                                         }
1075                                 } else {
1076                                         rpn_push(cvar_string(rpncmd));
1077                                 }
1078                                 if(rpn_error)
1079                                         break;
1080                         }
1081                         while(rpn_sp > 0)
1082                         {
1083                                 s = rpn_pop();
1084                                 print("rpn: still on stack: ", s, "\n");
1085                         }
1086                         return TRUE;
1087                 }
1088 #ifdef MENUQC
1089         } else if(argv(0) == "cp") {
1090                 if(argc >= 2)
1091                 {
1092                         s = argv(1);
1093                         for(i = 2; i < argc; ++i)
1094                                 s = strcat(s, " ", argv(i));
1095                         centerprint(unescape(s));
1096                 }
1097                 return TRUE;
1098 #endif
1099         } else if(argv(0) == "suggestmap") {
1100                 print(strcat(MapVote_Suggest(argv(1)), "\n"));
1101         }
1102
1103         return FALSE;
1104 }