]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mapvoting.qc
Merge branch 'master' into Mario/entrap_nade
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mapvoting.qc
1 #include "mapvoting.qh"
2
3 #include "g_world.qh"
4 #include "command/cmd.qh"
5 #include "command/getreplies.qh"
6 #include "../common/constants.qh"
7 #include "../common/mapinfo.qh"
8 #include "../common/playerstats.qh"
9 #include "../common/util.qh"
10
11
12 // definitions
13
14 float mapvote_nextthink;
15 float mapvote_keeptwotime;
16 float mapvote_timeout;
17 string mapvote_message;
18 const float MAPVOTE_SCREENSHOT_DIRS_COUNT = 4;
19 string mapvote_screenshot_dirs[MAPVOTE_SCREENSHOT_DIRS_COUNT];
20 float mapvote_screenshot_dirs_count;
21
22 float mapvote_count;
23 float mapvote_count_real;
24 string mapvote_maps[MAPVOTE_COUNT];
25 float mapvote_maps_screenshot_dir[MAPVOTE_COUNT];
26 string mapvote_maps_pakfile[MAPVOTE_COUNT];
27 float mapvote_maps_suggested[MAPVOTE_COUNT];
28 string mapvote_suggestions[MAPVOTE_COUNT];
29 float mapvote_suggestion_ptr;
30 float mapvote_voters;
31 float mapvote_selections[MAPVOTE_COUNT];
32 float mapvote_maps_flags[MAPVOTE_COUNT];
33 float mapvote_run;
34 float mapvote_detail;
35 float mapvote_abstain;
36 .float mapvote;
37
38 entity mapvote_ent;
39
40 /**
41  * Returns the gamtype ID from its name, if type_name isn't a real gametype it
42  * checks for sv_vote_gametype_(type_name)_type
43  */
44 float GameTypeVote_Type_FromString(string type_name)
45 {
46         float type = MapInfo_Type_FromString(type_name);
47         if ( type == 0 )
48                 type = MapInfo_Type_FromString(cvar_string(
49                         strcat("sv_vote_gametype_",type_name,"_type")));
50         return type;
51 }
52
53 int GameTypeVote_AvailabilityStatus(string type_name)
54 {
55         int flag = GTV_FORBIDDEN;
56
57         float type = MapInfo_Type_FromString(type_name);
58         if ( type == 0 )
59         {
60                 type = MapInfo_Type_FromString(cvar_string(
61                         strcat("sv_vote_gametype_",type_name,"_type")));
62                 flag |= GTV_CUSTOM;
63         }
64
65         if( type == 0 )
66                 return flag;
67
68         if ( autocvar_nextmap != "" )
69         {
70                 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, 0) )
71                         return flag;
72                 if (!(MapInfo_Map_supportedGametypes & type))
73                         return flag;
74         }
75
76         return flag | GTV_AVAILABLE;
77 }
78
79 float GameTypeVote_GetMask()
80 {
81         float n, j, gametype_mask;
82         n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
83         n = min(MAPVOTE_COUNT, n);
84         gametype_mask = 0;
85         for(j = 0; j < n; ++j)
86                 gametype_mask |= GameTypeVote_Type_FromString(argv(j));
87         return gametype_mask;
88 }
89
90 string GameTypeVote_MapInfo_FixName(string m)
91 {
92         if ( autocvar_sv_vote_gametype )
93         {
94                 MapInfo_Enumerate();
95                 MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
96         }
97         return MapInfo_FixName(m);
98 }
99
100 void MapVote_ClearAllVotes()
101 {
102         FOREACH_CLIENT(true, LAMBDA(it.mapvote = 0));
103 }
104
105 void MapVote_UnzoneStrings()
106 {
107         float j;
108         for(j = 0; j < mapvote_count; ++j)
109         {
110                 if ( mapvote_maps[j] )
111                 {
112                         strunzone(mapvote_maps[j]);
113                         mapvote_maps[j] = string_null;
114                 }
115                 if ( mapvote_maps_pakfile[j] )
116                 {
117                         strunzone(mapvote_maps_pakfile[j]);
118                         mapvote_maps_pakfile[j] = string_null;
119                 }
120         }
121 }
122
123 string MapVote_Suggest(entity this, string m)
124 {
125         float i;
126         if(m == "")
127                 return "That's not how to use this command.";
128         if(!autocvar_g_maplist_votable_suggestions)
129                 return "Suggestions are not accepted on this server.";
130         if(mapvote_initialized)
131         if(!gametypevote)
132                 return "Can't suggest - voting is already in progress!";
133         m = GameTypeVote_MapInfo_FixName(m);
134         if (!m)
135                 return "The map you suggested is not available on this server.";
136         if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
137                 if(Map_IsRecent(m))
138                         return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
139
140         if (!autocvar_sv_vote_gametype)
141         if(!MapInfo_CheckMap(m))
142                 return "The map you suggested does not support the current game mode.";
143         for(i = 0; i < mapvote_suggestion_ptr; ++i)
144                 if(mapvote_suggestions[i] == m)
145                         return "This map was already suggested.";
146         if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
147         {
148                 i = floor(random() * mapvote_suggestion_ptr);
149         }
150         else
151         {
152                 i = mapvote_suggestion_ptr;
153                 mapvote_suggestion_ptr += 1;
154         }
155         if(mapvote_suggestions[i] != "")
156                 strunzone(mapvote_suggestions[i]);
157         mapvote_suggestions[i] = strzone(m);
158         if(autocvar_sv_eventlog)
159                 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(this.playerid)));
160         return strcat("Suggestion of ", m, " accepted.");
161 }
162
163 void MapVote_AddVotable(string nextMap, float isSuggestion)
164 {
165         float j, i, o;
166         string pakfile, mapfile;
167
168         if(nextMap == "")
169                 return;
170         for(j = 0; j < mapvote_count; ++j)
171                 if(mapvote_maps[j] == nextMap)
172                         return;
173         // suggestions might be no longer valid/allowed after gametype switch!
174         if(isSuggestion)
175                 if(!MapInfo_CheckMap(nextMap))
176                         return;
177         mapvote_maps[mapvote_count] = strzone(nextMap);
178         mapvote_maps_suggested[mapvote_count] = isSuggestion;
179
180         pakfile = string_null;
181         for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
182         {
183                 mapfile = strcat(mapvote_screenshot_dirs[i], "/", mapvote_maps[i]);
184                 pakfile = whichpack(strcat(mapfile, ".tga"));
185                 if(pakfile == "")
186                         pakfile = whichpack(strcat(mapfile, ".jpg"));
187                 if(pakfile == "")
188                         pakfile = whichpack(strcat(mapfile, ".png"));
189                 if(pakfile != "")
190                         break;
191         }
192         if(i >= mapvote_screenshot_dirs_count)
193                 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?
194         for(o = strstrofs(pakfile, "/", 0)+1; o > 0; o = strstrofs(pakfile, "/", 0)+1)
195                 pakfile = substring(pakfile, o, -1);
196
197         mapvote_maps_screenshot_dir[mapvote_count] = i;
198         mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
199         mapvote_maps_flags[mapvote_count] = GTV_AVAILABLE;
200
201         mapvote_count += 1;
202 }
203
204 void MapVote_Init()
205 {
206         float i;
207         float nmax, smax;
208
209         MapVote_ClearAllVotes();
210         MapVote_UnzoneStrings();
211
212         mapvote_count = 0;
213         mapvote_detail = !autocvar_g_maplist_votable_nodetail;
214         mapvote_abstain = autocvar_g_maplist_votable_abstain;
215
216         if(mapvote_abstain)
217                 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
218         else
219                 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
220         smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
221
222         // we need this for AddVotable, as that cycles through the screenshot dirs
223         mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
224         if(mapvote_screenshot_dirs_count == 0)
225                 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
226         mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
227         for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
228                 mapvote_screenshot_dirs[i] = strzone(argv(i));
229
230         if(mapvote_suggestion_ptr)
231                 for(i = 0; i < 100 && mapvote_count < smax; ++i)
232                         MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], true);
233
234         for(i = 0; i < 100 && mapvote_count < nmax; ++i)
235                 MapVote_AddVotable(GetNextMap(), false);
236
237         if(mapvote_count == 0)
238         {
239                 bprint( "Maplist contains no single playable map!  Resetting it to default map list.\n" );
240                 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
241                 if(autocvar_g_maplist_shuffle)
242                         ShuffleMaplist();
243                 localcmd("\nmenu_cmd sync\n");
244                 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
245                         MapVote_AddVotable(GetNextMap(), false);
246         }
247
248         mapvote_count_real = mapvote_count;
249         if(mapvote_abstain)
250                 MapVote_AddVotable("don't care", 0);
251
252         //dprint("mapvote count is ", ftos(mapvote_count), "\n");
253
254         mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
255         mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
256         if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
257                 mapvote_keeptwotime = 0;
258         mapvote_message = "Choose a map and press its key!";
259
260         MapVote_Spawn();
261 }
262
263 void MapVote_SendPicture(entity to, int id)
264 {
265         msg_entity = to;
266         WriteHeader(MSG_ONE, TE_CSQC_PICTURE);
267         WriteByte(MSG_ONE, id);
268         WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
269 }
270
271
272 void MapVote_WriteMask()
273 {
274         float i;
275         if ( mapvote_count < 24 )
276         {
277                 float mask,power;
278                 mask = 0;
279                 for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
280                         if(mapvote_maps_flags[i] & GTV_AVAILABLE )
281                                 mask |= power;
282
283                 if(mapvote_count < 8)
284                         WriteByte(MSG_ENTITY, mask);
285                 else if (mapvote_count < 16)
286                         WriteShort(MSG_ENTITY,mask);
287                 else
288                         WriteLong(MSG_ENTITY, mask);
289         }
290         else
291         {
292                 for ( i = 0; i < mapvote_count; ++i )
293                         WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
294         }
295 }
296
297 /*
298  * Sends a single map vote option to the client
299  */
300 void MapVote_SendOption(int i)
301 {
302         // abstain
303         if(mapvote_abstain && i == mapvote_count - 1)
304         {
305                 WriteString(MSG_ENTITY, ""); // abstain needs no text
306                 WriteString(MSG_ENTITY, ""); // abstain needs no pack
307                 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
308         }
309         else
310         {
311                 WriteString(MSG_ENTITY, mapvote_maps[i]);
312                 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
313                 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
314         }
315 }
316
317 /*
318  * Sends a single gametype vote option to the client
319  */
320 void GameTypeVote_SendOption(int i)
321 {
322         // abstain
323         if(mapvote_abstain && i == mapvote_count - 1)
324         {
325                 WriteString(MSG_ENTITY, ""); // abstain needs no text
326                 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
327         }
328         else
329         {
330                 string type_name = mapvote_maps[i];
331                 WriteString(MSG_ENTITY, type_name);
332                 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
333                 if ( mapvote_maps_flags[i] & GTV_CUSTOM )
334                 {
335                         WriteString(MSG_ENTITY, cvar_string(
336                                 strcat("sv_vote_gametype_",type_name,"_name")));
337                         WriteString(MSG_ENTITY, cvar_string(
338                                 strcat("sv_vote_gametype_",type_name,"_description")));
339                 }
340         }
341 }
342
343 bool MapVote_SendEntity(entity this, entity to, int sf)
344 {
345         float i;
346
347         if(sf & 1)
348                 sf &= ~2; // if we send 1, we don't need to also send 2
349
350         WriteHeader(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
351         WriteByte(MSG_ENTITY, sf);
352
353         if(sf & 1)
354         {
355                 // flag 1 == initialization
356                 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
357                         WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
358                 WriteString(MSG_ENTITY, "");
359                 WriteByte(MSG_ENTITY, mapvote_count);
360                 WriteByte(MSG_ENTITY, mapvote_abstain);
361                 WriteByte(MSG_ENTITY, mapvote_detail);
362                 WriteCoord(MSG_ENTITY, mapvote_timeout);
363
364                 if ( gametypevote )
365                 {
366                         // gametype vote
367                         WriteByte(MSG_ENTITY, 1);
368                         WriteString(MSG_ENTITY, autocvar_nextmap);
369                 }
370                 else if ( autocvar_sv_vote_gametype )
371                 {
372                         // map vote but gametype has been chosen via voting screen
373                         WriteByte(MSG_ENTITY, 2);
374                         WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
375                 }
376                 else
377                         WriteByte(MSG_ENTITY, 0); // map vote
378
379                 MapVote_WriteMask();
380
381                 // Send data for the vote options
382                 for(i = 0; i < mapvote_count; ++i)
383                 {
384                         if(gametypevote)
385                                 GameTypeVote_SendOption(i);
386                         else
387                                 MapVote_SendOption(i);
388                 }
389         }
390
391         if(sf & 2)
392         {
393                 // flag 2 == update of mask
394                 MapVote_WriteMask();
395         }
396
397         if(sf & 4)
398         {
399                 if(mapvote_detail)
400                         for(i = 0; i < mapvote_count; ++i)
401                                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
402                                         WriteByte(MSG_ENTITY, mapvote_selections[i]);
403
404                 WriteByte(MSG_ENTITY, to.mapvote);
405         }
406
407         return true;
408 }
409
410 void MapVote_Spawn()
411 {
412         Net_LinkEntity(mapvote_ent = spawn(), false, 0, MapVote_SendEntity);
413 }
414
415 void MapVote_TouchMask()
416 {
417         mapvote_ent.SendFlags |= 2;
418 }
419
420 void MapVote_TouchVotes(entity voter)
421 {
422         mapvote_ent.SendFlags |= 4;
423 }
424
425 float MapVote_Finished(float mappos)
426 {
427         if(alreadychangedlevel)
428                 return false;
429
430         string result;
431         float i;
432         float didntvote;
433
434         if(autocvar_sv_eventlog)
435         {
436                 result = strcat(":vote:finished:", mapvote_maps[mappos]);
437                 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
438                 didntvote = mapvote_voters;
439                 for(i = 0; i < mapvote_count; ++i)
440                         if(mapvote_maps_flags[i] & GTV_AVAILABLE )
441                         {
442                                 didntvote -= mapvote_selections[i];
443                                 if(i != mappos)
444                                 {
445                                         result = strcat(result, ":", mapvote_maps[i]);
446                                         result = strcat(result, ":", ftos(mapvote_selections[i]));
447                                 }
448                         }
449                 result = strcat(result, ":didn't vote:", ftos(didntvote));
450
451                 GameLogEcho(result);
452                 if(mapvote_maps_suggested[mappos])
453                         GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
454         }
455
456         FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(FixClientCvars(it)));
457
458         if(gametypevote)
459         {
460                 if ( GameTypeVote_Finished(mappos) )
461                 {
462                         gametypevote = false;
463                         if(autocvar_nextmap != "")
464                         {
465                                 Map_Goto_SetStr(autocvar_nextmap);
466                                 Map_Goto(0);
467                                 alreadychangedlevel = true;
468                                 return true;
469                         }
470                         else
471                                 MapVote_Init();
472                 }
473                 return false;
474         }
475
476         Map_Goto_SetStr(mapvote_maps[mappos]);
477         Map_Goto(0);
478         alreadychangedlevel = true;
479
480         return true;
481 }
482
483 void MapVote_CheckRules_1()
484 {
485         for (int i = 0; i < mapvote_count; ++i)
486                 if (mapvote_maps_flags[i] & GTV_AVAILABLE)
487                 {
488                         //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
489                         mapvote_selections[i] = 0;
490                 }
491
492         mapvote_voters = 0;
493         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
494                 ++mapvote_voters;
495                 if (it.mapvote)
496                 {
497                         int idx = it.mapvote - 1;
498                         //dprint("Player ", it.netname, " vote = ", ftos(idx), "\n");
499                         ++mapvote_selections[idx];
500                 }
501         });
502 }
503
504 float MapVote_CheckRules_2()
505 {
506         float i;
507         float firstPlace, secondPlace, currentPlace;
508         float firstPlaceVotes, secondPlaceVotes, currentVotes;
509         float mapvote_voters_real;
510         string result;
511
512         if(mapvote_count_real == 1)
513                 return MapVote_Finished(0);
514
515         mapvote_voters_real = mapvote_voters;
516         if(mapvote_abstain)
517                 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
518
519         RandomSelection_Init();
520         currentPlace = 0;
521         currentVotes = -1;
522         for(i = 0; i < mapvote_count_real; ++i)
523                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
524                 {
525                         RandomSelection_Add(NULL, i, string_null, 1, mapvote_selections[i]);
526                         if ( gametypevote &&  mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
527                         {
528                                 currentVotes = mapvote_selections[i];
529                                 currentPlace = i;
530                         }
531                 }
532         firstPlaceVotes = RandomSelection_best_priority;
533         if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
534                 firstPlace = currentPlace;
535         else
536                 firstPlace = RandomSelection_chosen_float;
537
538         //dprint("First place: ", ftos(firstPlace), "\n");
539         //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
540
541         RandomSelection_Init();
542         for(i = 0; i < mapvote_count_real; ++i)
543                 if(i != firstPlace)
544                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
545                         RandomSelection_Add(NULL, i, string_null, 1, mapvote_selections[i]);
546         secondPlace = RandomSelection_chosen_float;
547         secondPlaceVotes = RandomSelection_best_priority;
548         //dprint("Second place: ", ftos(secondPlace), "\n");
549         //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
550
551         if(firstPlace == -1)
552                 error("No first place in map vote... WTF?");
553
554         if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
555                 return MapVote_Finished(firstPlace);
556
557         if(mapvote_keeptwotime)
558                 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
559                 {
560                         float didntvote;
561                         MapVote_TouchMask();
562                         mapvote_message = "Now decide between the TOP TWO!";
563                         mapvote_keeptwotime = 0;
564                         result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
565                         result = strcat(result, ":", ftos(firstPlaceVotes));
566                         result = strcat(result, ":", mapvote_maps[secondPlace]);
567                         result = strcat(result, ":", ftos(secondPlaceVotes), "::");
568                         didntvote = mapvote_voters;
569                         for(i = 0; i < mapvote_count; ++i)
570                         {
571                                 didntvote -= mapvote_selections[i];
572                                 if(i != firstPlace)
573                                         if(i != secondPlace)
574                                         {
575                                                 result = strcat(result, ":", mapvote_maps[i]);
576                                                 result = strcat(result, ":", ftos(mapvote_selections[i]));
577                                                 if(i < mapvote_count_real)
578                                                 {
579                                                         mapvote_maps_flags[i] &= ~GTV_AVAILABLE;
580                                                 }
581                                         }
582                         }
583                         result = strcat(result, ":didn't vote:", ftos(didntvote));
584                         if(autocvar_sv_eventlog)
585                                 GameLogEcho(result);
586                 }
587
588         return false;
589 }
590
591 void MapVote_Tick()
592 {
593         float keeptwo;
594         float totalvotes;
595
596         keeptwo = mapvote_keeptwotime;
597         MapVote_CheckRules_1(); // count
598         if(MapVote_CheckRules_2()) // decide
599                 return;
600
601         totalvotes = 0;
602         FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
603                 // hide scoreboard again
604                 if(it.health != 2342)
605                 {
606                         it.health = 2342;
607                         it.impulse = 0;
608
609                         msg_entity = it;
610                         WriteByte(MSG_ONE, SVC_FINALE);
611                         WriteString(MSG_ONE, "");
612                 }
613
614                 // clear possibly invalid votes
615                 if ( !(mapvote_maps_flags[it.mapvote-1] & GTV_AVAILABLE) )
616                         it.mapvote = 0;
617                 // use impulses as new vote
618                 if(it.impulse >= 1 && it.impulse <= mapvote_count)
619                         if( mapvote_maps_flags[it.impulse - 1] & GTV_AVAILABLE )
620                         {
621                                 it.mapvote = it.impulse;
622                                 MapVote_TouchVotes(it);
623                         }
624                 it.impulse = 0;
625
626                 if(it.mapvote)
627                         ++totalvotes;
628         ));
629
630         MapVote_CheckRules_1(); // just count
631 }
632
633 void MapVote_Start()
634 {
635         // if mapvote is already running, don't do this initialization again
636         if(mapvote_run) { return; }
637
638         // don't start mapvote until after playerstats gamereport is sent
639         if(PlayerStats_GameReport_DelayMapVote) { return; }
640
641         MapInfo_Enumerate();
642         if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
643                 mapvote_run = true;
644 }
645
646 void MapVote_Think()
647 {
648         if(!mapvote_run)
649                 return;
650
651         if(alreadychangedlevel)
652                 return;
653
654         if(time < mapvote_nextthink)
655                 return;
656         //dprint("tick\n");
657
658         mapvote_nextthink = time + 0.5;
659
660         if(!mapvote_initialized)
661         {
662                 if(autocvar_rescan_pending == 1)
663                 {
664                         cvar_set("rescan_pending", "2");
665                         localcmd("fs_rescan\nrescan_pending 3\n");
666                         return;
667                 }
668                 else if(autocvar_rescan_pending == 2)
669                 {
670                         return;
671                 }
672                 else if(autocvar_rescan_pending == 3)
673                 {
674                         // now build missing mapinfo files
675                         if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
676                                 return;
677
678                         // we're done, start the timer
679                         cvar_set("rescan_pending", "0");
680                 }
681
682                 mapvote_initialized = true;
683                 if(DoNextMapOverride(0))
684                         return;
685                 if(!autocvar_g_maplist_votable || player_count <= 0)
686                 {
687                         GotoNextMap(0);
688                         return;
689                 }
690
691                 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
692                 else if(autocvar_nextmap == "") { MapVote_Init(); }
693         }
694
695         MapVote_Tick();
696 }
697
698 float GameTypeVote_SetGametype(float type)
699 {
700         if (MapInfo_CurrentGametype() == type)
701                 return true;
702
703         float tsave = MapInfo_CurrentGametype();
704
705         MapInfo_SwitchGameType(type);
706
707         MapInfo_Enumerate();
708         MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
709         if(MapInfo_count > 0)
710         {
711                 // update lsmaps in case the gametype changed, this way people can easily list maps for it
712                 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
713                 lsmaps_reply = strzone(getlsmaps());
714                 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
715         }
716         else
717         {
718                 bprint("Cannot use this game type: no map for it found\n");
719                 MapInfo_SwitchGameType(tsave);
720                 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
721                 return false;
722         }
723
724         //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
725
726         cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
727         if(autocvar_g_maplist_shuffle)
728                 ShuffleMaplist();
729
730         return true;
731 }
732
733 float gametypevote_finished;
734 float GameTypeVote_Finished(float pos)
735 {
736         if(!gametypevote || gametypevote_finished)
737                 return false;
738
739         if ( !GameTypeVote_SetGametype(GameTypeVote_Type_FromString(mapvote_maps[pos])) )
740         {
741                 LOG_TRACE("Selected gametype is not supported by any map");
742         }
743
744         localcmd("sv_vote_gametype_hook_all\n");
745         localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
746
747         gametypevote_finished = true;
748
749         return true;
750 }
751
752 float GameTypeVote_AddVotable(string nextMode)
753 {
754         float j;
755         if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == 0 )
756                 return false;
757         for(j = 0; j < mapvote_count; ++j)
758                 if(mapvote_maps[j] == nextMode)
759                         return false;
760
761         mapvote_maps[mapvote_count] = strzone(nextMode);
762         mapvote_maps_suggested[mapvote_count] = false;
763
764         mapvote_maps_screenshot_dir[mapvote_count] = 0;
765         mapvote_maps_pakfile[mapvote_count] = strzone("");
766         mapvote_maps_flags[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
767
768         mapvote_count += 1;
769
770         return true;
771
772 }
773
774 float GameTypeVote_Start()
775 {
776         float j;
777         MapVote_ClearAllVotes();
778         MapVote_UnzoneStrings();
779
780         mapvote_count = 0;
781         mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
782         mapvote_abstain = 0;
783         mapvote_detail = !autocvar_g_maplist_votable_nodetail;
784
785         float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
786         n = min(MAPVOTE_COUNT, n);
787
788         float really_available, which_available;
789         really_available = 0;
790         which_available = -1;
791         for(j = 0; j < n; ++j)
792         {
793                 if ( GameTypeVote_AddVotable(argv(j)) )
794                 if ( mapvote_maps_flags[j] & GTV_AVAILABLE )
795                 {
796                         really_available++;
797                         which_available = j;
798                 }
799         }
800
801         mapvote_count_real = mapvote_count;
802
803         gametypevote = 1;
804
805         if ( really_available == 0 )
806         {
807                 if ( mapvote_count > 0 )
808                         strunzone(mapvote_maps[0]);
809                 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
810                 //GameTypeVote_Finished(0);
811                 MapVote_Finished(0);
812                 return false;
813         }
814         if ( really_available == 1 )
815         {
816                 //GameTypeVote_Finished(which_available);
817                 MapVote_Finished(which_available);
818                 return false;
819         }
820
821         mapvote_count_real = mapvote_count;
822
823         mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
824         if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
825                 mapvote_keeptwotime = 0;
826
827         MapVote_Spawn();
828
829         return true;
830 }