]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mapvoting.qc
d22073ef6c5d02e25e50263a0ac643c085372aee
[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/net_linked.qh>
8 #include "../common/mapinfo.qh"
9 #include "../common/playerstats.qh"
10 #include <common/state.qh>
11 #include "../common/util.qh"
12
13
14 // definitions
15
16 float mapvote_nextthink;
17 float mapvote_keeptwotime;
18 float mapvote_timeout;
19 const int MAPVOTE_SCREENSHOT_DIRS_COUNT = 4;
20 string mapvote_screenshot_dirs[MAPVOTE_SCREENSHOT_DIRS_COUNT];
21 int mapvote_screenshot_dirs_count;
22
23 int mapvote_count;
24 int mapvote_count_real;
25 string mapvote_maps[MAPVOTE_COUNT];
26 int mapvote_maps_screenshot_dir[MAPVOTE_COUNT];
27 string mapvote_maps_pakfile[MAPVOTE_COUNT];
28 bool mapvote_maps_suggested[MAPVOTE_COUNT];
29 string mapvote_suggestions[MAPVOTE_COUNT];
30 int mapvote_suggestion_ptr;
31 int mapvote_voters;
32 int mapvote_selections[MAPVOTE_COUNT];
33 int mapvote_maps_flags[MAPVOTE_COUNT];
34 bool mapvote_run;
35 bool mapvote_detail;
36 bool mapvote_abstain;
37 .int mapvote;
38
39 entity mapvote_ent;
40
41 /**
42  * Returns the gamtype ID from its name, if type_name isn't a real gametype it
43  * checks for sv_vote_gametype_(type_name)_type
44  */
45 Gametype GameTypeVote_Type_FromString(string type_name)
46 {
47         Gametype type = MapInfo_Type_FromString(type_name);
48         if (type == NULL)
49                 type = MapInfo_Type_FromString(cvar_string(
50                         strcat("sv_vote_gametype_",type_name,"_type")));
51         return type;
52 }
53
54 int GameTypeVote_AvailabilityStatus(string type_name)
55 {
56         int flag = GTV_FORBIDDEN;
57
58         Gametype type = MapInfo_Type_FromString(type_name);
59         if ( type == NULL )
60         {
61                 type = MapInfo_Type_FromString(cvar_string(
62                         strcat("sv_vote_gametype_",type_name,"_type")));
63                 flag |= GTV_CUSTOM;
64         }
65
66         if( type == NULL )
67                 return flag;
68
69         if ( autocvar_nextmap != "" )
70         {
71                 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, NULL) )
72                         return flag;
73                 if (!(MapInfo_Map_supportedGametypes & type.m_flags))
74                         return flag;
75         }
76
77         return flag | GTV_AVAILABLE;
78 }
79
80 int GameTypeVote_GetMask()
81 {
82         int n, j, gametype_mask;
83         n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
84         n = min(MAPVOTE_COUNT, n);
85         gametype_mask = 0;
86         for(j = 0; j < n; ++j)
87                 gametype_mask |= GameTypeVote_Type_FromString(argv(j)).m_flags;
88         return gametype_mask;
89 }
90
91 string GameTypeVote_MapInfo_FixName(string m)
92 {
93         if ( autocvar_sv_vote_gametype )
94         {
95                 MapInfo_Enumerate();
96                 _MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
97         }
98         return MapInfo_FixName(m);
99 }
100
101 void MapVote_ClearAllVotes()
102 {
103         FOREACH_CLIENT(true, LAMBDA(it.mapvote = 0));
104 }
105
106 void MapVote_UnzoneStrings()
107 {
108         for(int 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         int 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, bool isSuggestion)
164 {
165         int 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], "/", nextMap);
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         int i;
207         int 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 = boolean(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", false);
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
259         MapVote_Spawn();
260 }
261
262 void MapVote_SendPicture(entity to, int id)
263 {
264         msg_entity = to;
265         WriteHeader(MSG_ONE, TE_CSQC_PICTURE);
266         WriteByte(MSG_ONE, id);
267         WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
268 }
269
270
271 void MapVote_WriteMask()
272 {
273         if ( mapvote_count < 24 )
274         {
275                 int mask = 0;
276                 for(int j = 0; j < mapvote_count; ++j)
277                 {
278                         if(mapvote_maps_flags[j] & GTV_AVAILABLE)
279                                 mask |= BIT(j);
280                 }
281
282                 if(mapvote_count < 8)
283                         WriteByte(MSG_ENTITY, mask);
284                 else if (mapvote_count < 16)
285                         WriteShort(MSG_ENTITY,mask);
286                 else
287                         WriteLong(MSG_ENTITY, mask);
288         }
289         else
290         {
291                 for (int j = 0; j < mapvote_count; ++j)
292                         WriteByte(MSG_ENTITY, mapvote_maps_flags[j]);
293         }
294 }
295
296 /*
297  * Sends a single map vote option to the client
298  */
299 void MapVote_SendOption(int i)
300 {
301         // abstain
302         if(mapvote_abstain && i == mapvote_count - 1)
303         {
304                 WriteString(MSG_ENTITY, ""); // abstain needs no text
305                 WriteString(MSG_ENTITY, ""); // abstain needs no pack
306                 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
307         }
308         else
309         {
310                 WriteString(MSG_ENTITY, mapvote_maps[i]);
311                 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
312                 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
313         }
314 }
315
316 /*
317  * Sends a single gametype vote option to the client
318  */
319 void GameTypeVote_SendOption(int i)
320 {
321         // abstain
322         if(mapvote_abstain && i == mapvote_count - 1)
323         {
324                 WriteString(MSG_ENTITY, ""); // abstain needs no text
325                 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
326         }
327         else
328         {
329                 string type_name = mapvote_maps[i];
330                 WriteString(MSG_ENTITY, type_name);
331                 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
332                 if ( mapvote_maps_flags[i] & GTV_CUSTOM )
333                 {
334                         WriteString(MSG_ENTITY, cvar_string(
335                                 strcat("sv_vote_gametype_",type_name,"_name")));
336                         WriteString(MSG_ENTITY, cvar_string(
337                                 strcat("sv_vote_gametype_",type_name,"_description")));
338                         WriteString(MSG_ENTITY, cvar_string(
339                                 strcat("sv_vote_gametype_",type_name,"_type")));
340                 }
341         }
342 }
343
344 bool MapVote_SendEntity(entity this, entity to, int sf)
345 {
346         int i;
347
348         if(sf & 1)
349                 sf &= ~2; // if we send 1, we don't need to also send 2
350
351         WriteHeader(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
352         WriteByte(MSG_ENTITY, sf);
353
354         if(sf & 1)
355         {
356                 // flag 1 == initialization
357                 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
358                         WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
359                 WriteString(MSG_ENTITY, "");
360                 WriteByte(MSG_ENTITY, mapvote_count);
361                 WriteByte(MSG_ENTITY, mapvote_abstain);
362                 WriteByte(MSG_ENTITY, mapvote_detail);
363                 WriteCoord(MSG_ENTITY, mapvote_timeout);
364
365                 if ( gametypevote )
366                 {
367                         // gametype vote
368                         WriteByte(MSG_ENTITY, 1);
369                         WriteString(MSG_ENTITY, autocvar_nextmap);
370                 }
371                 else if ( autocvar_sv_vote_gametype )
372                 {
373                         // map vote but gametype has been chosen via voting screen
374                         WriteByte(MSG_ENTITY, 2);
375                         WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
376                 }
377                 else
378                         WriteByte(MSG_ENTITY, 0); // map vote
379
380                 MapVote_WriteMask();
381
382                 // Send data for the vote options
383                 for(i = 0; i < mapvote_count; ++i)
384                 {
385                         if(gametypevote)
386                                 GameTypeVote_SendOption(i);
387                         else
388                                 MapVote_SendOption(i);
389                 }
390         }
391
392         if(sf & 2)
393         {
394                 // flag 2 == update of mask
395                 MapVote_WriteMask();
396         }
397
398         if(sf & 4)
399         {
400                 if(mapvote_detail)
401                         for(i = 0; i < mapvote_count; ++i)
402                                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
403                                         WriteByte(MSG_ENTITY, mapvote_selections[i]);
404
405                 WriteByte(MSG_ENTITY, to.mapvote);
406         }
407
408         return true;
409 }
410
411 void MapVote_Spawn()
412 {
413         Net_LinkEntity(mapvote_ent = spawn(), false, 0, MapVote_SendEntity);
414 }
415
416 void MapVote_TouchMask()
417 {
418         mapvote_ent.SendFlags |= 2;
419 }
420
421 void MapVote_TouchVotes(entity voter)
422 {
423         mapvote_ent.SendFlags |= 4;
424 }
425
426 bool MapVote_Finished(int mappos)
427 {
428         if(alreadychangedlevel)
429                 return false;
430
431         string result;
432         int i;
433         int didntvote;
434
435         if(autocvar_sv_eventlog)
436         {
437                 result = strcat(":vote:finished:", mapvote_maps[mappos]);
438                 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
439                 didntvote = mapvote_voters;
440                 for(i = 0; i < mapvote_count; ++i)
441                         if(mapvote_maps_flags[i] & GTV_AVAILABLE )
442                         {
443                                 didntvote -= mapvote_selections[i];
444                                 if(i != mappos)
445                                 {
446                                         result = strcat(result, ":", mapvote_maps[i]);
447                                         result = strcat(result, ":", ftos(mapvote_selections[i]));
448                                 }
449                         }
450                 result = strcat(result, ":didn't vote:", ftos(didntvote));
451
452                 GameLogEcho(result);
453                 if(mapvote_maps_suggested[mappos])
454                         GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
455         }
456
457         FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(FixClientCvars(it)));
458
459         if(gametypevote)
460         {
461                 if ( GameTypeVote_Finished(mappos) )
462                 {
463                         gametypevote = false;
464                         if(autocvar_nextmap != "")
465                         {
466                                 Map_Goto_SetStr(autocvar_nextmap);
467                                 Map_Goto(0);
468                                 alreadychangedlevel = true;
469                                 return true;
470                         }
471                         else
472                                 MapVote_Init();
473                 }
474                 return false;
475         }
476
477         Map_Goto_SetStr(mapvote_maps[mappos]);
478         Map_Goto(0);
479         alreadychangedlevel = true;
480
481         return true;
482 }
483
484 void MapVote_CheckRules_1()
485 {
486         for (int i = 0; i < mapvote_count; ++i)
487                 if (mapvote_maps_flags[i] & GTV_AVAILABLE)
488                 {
489                         //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
490                         mapvote_selections[i] = 0;
491                 }
492
493         mapvote_voters = 0;
494         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
495                 ++mapvote_voters;
496                 if (it.mapvote)
497                 {
498                         int idx = it.mapvote - 1;
499                         //dprint("Player ", it.netname, " vote = ", ftos(idx), "\n");
500                         ++mapvote_selections[idx];
501                 }
502         });
503 }
504
505 bool MapVote_CheckRules_2()
506 {
507         int i;
508         int firstPlace, secondPlace, currentPlace;
509         int firstPlaceVotes, secondPlaceVotes, currentVotes;
510         int mapvote_voters_real;
511         string result;
512
513         if(mapvote_count_real == 1)
514                 return MapVote_Finished(0);
515
516         mapvote_voters_real = mapvote_voters;
517         if(mapvote_abstain)
518                 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
519
520         RandomSelection_Init();
521         currentPlace = 0;
522         currentVotes = -1;
523         for(i = 0; i < mapvote_count_real; ++i)
524                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
525                 {
526                         RandomSelection_AddFloat(i, 1, mapvote_selections[i]);
527                         if ( gametypevote &&  mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
528                         {
529                                 currentVotes = mapvote_selections[i];
530                                 currentPlace = i;
531                         }
532                 }
533         firstPlaceVotes = RandomSelection_best_priority;
534         if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
535                 firstPlace = currentPlace;
536         else
537                 firstPlace = RandomSelection_chosen_float;
538
539         //dprint("First place: ", ftos(firstPlace), "\n");
540         //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
541
542         RandomSelection_Init();
543         for(i = 0; i < mapvote_count_real; ++i)
544                 if(i != firstPlace)
545                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
546                         RandomSelection_AddFloat(i, 1, mapvote_selections[i]);
547         secondPlace = RandomSelection_chosen_float;
548         secondPlaceVotes = RandomSelection_best_priority;
549         //dprint("Second place: ", ftos(secondPlace), "\n");
550         //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
551
552         if(firstPlace == -1)
553                 error("No first place in map vote... WTF?");
554
555         if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
556                 return MapVote_Finished(firstPlace);
557
558         if(mapvote_keeptwotime)
559                 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
560                 {
561                         MapVote_TouchMask();
562                         mapvote_keeptwotime = 0;
563                         result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
564                         result = strcat(result, ":", ftos(firstPlaceVotes));
565                         result = strcat(result, ":", mapvote_maps[secondPlace]);
566                         result = strcat(result, ":", ftos(secondPlaceVotes), "::");
567                         int didntvote = mapvote_voters;
568                         for(i = 0; i < mapvote_count; ++i)
569                         {
570                                 didntvote -= mapvote_selections[i];
571                                 if(i != firstPlace)
572                                         if(i != secondPlace)
573                                         {
574                                                 result = strcat(result, ":", mapvote_maps[i]);
575                                                 result = strcat(result, ":", ftos(mapvote_selections[i]));
576                                                 if(i < mapvote_count_real)
577                                                 {
578                                                         mapvote_maps_flags[i] &= ~GTV_AVAILABLE;
579                                                 }
580                                         }
581                         }
582                         result = strcat(result, ":didn't vote:", ftos(didntvote));
583                         if(autocvar_sv_eventlog)
584                                 GameLogEcho(result);
585                 }
586
587         return false;
588 }
589
590 void MapVote_Tick()
591 {
592
593         MapVote_CheckRules_1(); // count
594         if(MapVote_CheckRules_2()) // decide
595                 return;
596
597         int totalvotes = 0;
598         FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
599                 // hide scoreboard again
600                 if(it.health != 2342)
601                 {
602                         it.health = 2342;
603                         CS(it).impulse = 0;
604
605                         msg_entity = it;
606                         WriteByte(MSG_ONE, SVC_FINALE);
607                         WriteString(MSG_ONE, "");
608                 }
609
610                 // clear possibly invalid votes
611                 if ( !(mapvote_maps_flags[it.mapvote-1] & GTV_AVAILABLE) )
612                         it.mapvote = 0;
613                 // use impulses as new vote
614                 if(CS(it).impulse >= 1 && CS(it).impulse <= mapvote_count)
615                         if( mapvote_maps_flags[CS(it).impulse - 1] & GTV_AVAILABLE )
616                         {
617                                 it.mapvote = CS(it).impulse;
618                                 MapVote_TouchVotes(it);
619                         }
620                 CS(it).impulse = 0;
621
622                 if(it.mapvote)
623                         ++totalvotes;
624         ));
625
626         MapVote_CheckRules_1(); // just count
627 }
628
629 void MapVote_Start()
630 {
631         // if mapvote is already running, don't do this initialization again
632         if(mapvote_run) { return; }
633
634         // don't start mapvote until after playerstats gamereport is sent
635         if(PlayerStats_GameReport_DelayMapVote) { return; }
636
637         MapInfo_Enumerate();
638         if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
639                 mapvote_run = true;
640 }
641
642 void MapVote_Think()
643 {
644         if(!mapvote_run)
645                 return;
646
647         if(alreadychangedlevel)
648                 return;
649
650         if(time < mapvote_nextthink)
651                 return;
652         //dprint("tick\n");
653
654         mapvote_nextthink = time + 0.5;
655
656         if(!mapvote_initialized)
657         {
658                 if(autocvar_rescan_pending == 1)
659                 {
660                         cvar_set("rescan_pending", "2");
661                         localcmd("fs_rescan\nrescan_pending 3\n");
662                         return;
663                 }
664                 else if(autocvar_rescan_pending == 2)
665                 {
666                         return;
667                 }
668                 else if(autocvar_rescan_pending == 3)
669                 {
670                         // now build missing mapinfo files
671                         if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
672                                 return;
673
674                         // we're done, start the timer
675                         cvar_set("rescan_pending", "0");
676                 }
677
678                 mapvote_initialized = true;
679                 if(DoNextMapOverride(0))
680                         return;
681                 if(!autocvar_g_maplist_votable || player_count <= 0)
682                 {
683                         GotoNextMap(0);
684                         return;
685                 }
686
687                 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
688                 else if(autocvar_nextmap == "") { MapVote_Init(); }
689         }
690
691         MapVote_Tick();
692 }
693
694 bool GameTypeVote_SetGametype(Gametype type)
695 {
696         if (MapInfo_CurrentGametype() == type)
697                 return true;
698
699         Gametype tsave = MapInfo_CurrentGametype();
700
701         MapInfo_SwitchGameType(type);
702
703         MapInfo_Enumerate();
704         MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
705         if(MapInfo_count > 0)
706         {
707                 // update lsmaps in case the gametype changed, this way people can easily list maps for it
708                 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
709                 lsmaps_reply = strzone(getlsmaps());
710                 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
711         }
712         else
713         {
714                 bprint("Cannot use this game type: no map for it found\n");
715                 MapInfo_SwitchGameType(tsave);
716                 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
717                 return false;
718         }
719
720         //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
721
722         cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
723         if(autocvar_g_maplist_shuffle)
724                 ShuffleMaplist();
725
726         return true;
727 }
728
729 bool gametypevote_finished;
730 bool GameTypeVote_Finished(int pos)
731 {
732         if(!gametypevote || gametypevote_finished)
733                 return false;
734
735         if ( !GameTypeVote_SetGametype(GameTypeVote_Type_FromString(mapvote_maps[pos])) )
736         {
737                 LOG_TRACE("Selected gametype is not supported by any map");
738         }
739
740         localcmd("sv_vote_gametype_hook_all\n");
741         localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
742
743         gametypevote_finished = true;
744
745         return true;
746 }
747
748 bool GameTypeVote_AddVotable(string nextMode)
749 {
750         if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == NULL )
751                 return false;
752
753         for(int j = 0; j < mapvote_count; ++j)
754                 if(mapvote_maps[j] == nextMode)
755                         return false;
756
757         mapvote_maps[mapvote_count] = strzone(nextMode);
758         mapvote_maps_suggested[mapvote_count] = false;
759
760         mapvote_maps_screenshot_dir[mapvote_count] = 0;
761         mapvote_maps_pakfile[mapvote_count] = strzone("");
762         mapvote_maps_flags[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
763
764         mapvote_count += 1;
765
766         return true;
767
768 }
769
770 bool GameTypeVote_Start()
771 {
772         MapVote_ClearAllVotes();
773         MapVote_UnzoneStrings();
774
775         mapvote_count = 0;
776         mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
777         mapvote_abstain = false;
778         mapvote_detail = !autocvar_g_maplist_votable_nodetail;
779
780         int n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
781         n = min(MAPVOTE_COUNT, n);
782
783         int really_available, which_available;
784         really_available = 0;
785         which_available = -1;
786         for(int j = 0; j < n; ++j)
787         {
788                 if ( GameTypeVote_AddVotable(argv(j)) )
789                 if ( mapvote_maps_flags[j] & GTV_AVAILABLE )
790                 {
791                         really_available++;
792                         which_available = j;
793                 }
794         }
795
796         mapvote_count_real = mapvote_count;
797
798         gametypevote = 1;
799
800         if ( really_available == 0 )
801         {
802                 if ( mapvote_count > 0 )
803                         strunzone(mapvote_maps[0]);
804                 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
805                 //GameTypeVote_Finished(0);
806                 MapVote_Finished(0);
807                 return false;
808         }
809         if ( really_available == 1 )
810         {
811                 //GameTypeVote_Finished(which_available);
812                 MapVote_Finished(which_available);
813                 return false;
814         }
815
816         mapvote_count_real = mapvote_count;
817
818         mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
819         if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
820                 mapvote_keeptwotime = 0;
821
822         MapVote_Spawn();
823
824         return true;
825 }