]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mapvoting.qc
Merge branch 'DefaultUser/gametype_votescreen' into 'master'
[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 Gametype GameTypeVote_Type_FromString(string type_name)
45 {
46         Gametype type = MapInfo_Type_FromString(type_name);
47         if (type == NULL)
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         Gametype type = MapInfo_Type_FromString(type_name);
58         if ( type == NULL )
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 == NULL )
66                 return flag;
67
68         if ( autocvar_nextmap != "" )
69         {
70                 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, NULL) )
71                         return flag;
72                 if (!(MapInfo_Map_supportedGametypes & type.m_flags))
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)).m_flags;
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                         WriteString(MSG_ENTITY, cvar_string(
340                                 strcat("sv_vote_gametype_",type_name,"_type")));
341                 }
342         }
343 }
344
345 bool MapVote_SendEntity(entity this, entity to, int sf)
346 {
347         float i;
348
349         if(sf & 1)
350                 sf &= ~2; // if we send 1, we don't need to also send 2
351
352         WriteHeader(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
353         WriteByte(MSG_ENTITY, sf);
354
355         if(sf & 1)
356         {
357                 // flag 1 == initialization
358                 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
359                         WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
360                 WriteString(MSG_ENTITY, "");
361                 WriteByte(MSG_ENTITY, mapvote_count);
362                 WriteByte(MSG_ENTITY, mapvote_abstain);
363                 WriteByte(MSG_ENTITY, mapvote_detail);
364                 WriteCoord(MSG_ENTITY, mapvote_timeout);
365
366                 if ( gametypevote )
367                 {
368                         // gametype vote
369                         WriteByte(MSG_ENTITY, 1);
370                         WriteString(MSG_ENTITY, autocvar_nextmap);
371                 }
372                 else if ( autocvar_sv_vote_gametype )
373                 {
374                         // map vote but gametype has been chosen via voting screen
375                         WriteByte(MSG_ENTITY, 2);
376                         WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
377                 }
378                 else
379                         WriteByte(MSG_ENTITY, 0); // map vote
380
381                 MapVote_WriteMask();
382
383                 // Send data for the vote options
384                 for(i = 0; i < mapvote_count; ++i)
385                 {
386                         if(gametypevote)
387                                 GameTypeVote_SendOption(i);
388                         else
389                                 MapVote_SendOption(i);
390                 }
391         }
392
393         if(sf & 2)
394         {
395                 // flag 2 == update of mask
396                 MapVote_WriteMask();
397         }
398
399         if(sf & 4)
400         {
401                 if(mapvote_detail)
402                         for(i = 0; i < mapvote_count; ++i)
403                                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
404                                         WriteByte(MSG_ENTITY, mapvote_selections[i]);
405
406                 WriteByte(MSG_ENTITY, to.mapvote);
407         }
408
409         return true;
410 }
411
412 void MapVote_Spawn()
413 {
414         Net_LinkEntity(mapvote_ent = spawn(), false, 0, MapVote_SendEntity);
415 }
416
417 void MapVote_TouchMask()
418 {
419         mapvote_ent.SendFlags |= 2;
420 }
421
422 void MapVote_TouchVotes(entity voter)
423 {
424         mapvote_ent.SendFlags |= 4;
425 }
426
427 float MapVote_Finished(float mappos)
428 {
429         if(alreadychangedlevel)
430                 return false;
431
432         string result;
433         float i;
434         float didntvote;
435
436         if(autocvar_sv_eventlog)
437         {
438                 result = strcat(":vote:finished:", mapvote_maps[mappos]);
439                 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
440                 didntvote = mapvote_voters;
441                 for(i = 0; i < mapvote_count; ++i)
442                         if(mapvote_maps_flags[i] & GTV_AVAILABLE )
443                         {
444                                 didntvote -= mapvote_selections[i];
445                                 if(i != mappos)
446                                 {
447                                         result = strcat(result, ":", mapvote_maps[i]);
448                                         result = strcat(result, ":", ftos(mapvote_selections[i]));
449                                 }
450                         }
451                 result = strcat(result, ":didn't vote:", ftos(didntvote));
452
453                 GameLogEcho(result);
454                 if(mapvote_maps_suggested[mappos])
455                         GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
456         }
457
458         FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(FixClientCvars(it)));
459
460         if(gametypevote)
461         {
462                 if ( GameTypeVote_Finished(mappos) )
463                 {
464                         gametypevote = false;
465                         if(autocvar_nextmap != "")
466                         {
467                                 Map_Goto_SetStr(autocvar_nextmap);
468                                 Map_Goto(0);
469                                 alreadychangedlevel = true;
470                                 return true;
471                         }
472                         else
473                                 MapVote_Init();
474                 }
475                 return false;
476         }
477
478         Map_Goto_SetStr(mapvote_maps[mappos]);
479         Map_Goto(0);
480         alreadychangedlevel = true;
481
482         return true;
483 }
484
485 void MapVote_CheckRules_1()
486 {
487         for (int i = 0; i < mapvote_count; ++i)
488                 if (mapvote_maps_flags[i] & GTV_AVAILABLE)
489                 {
490                         //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
491                         mapvote_selections[i] = 0;
492                 }
493
494         mapvote_voters = 0;
495         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
496                 ++mapvote_voters;
497                 if (it.mapvote)
498                 {
499                         int idx = it.mapvote - 1;
500                         //dprint("Player ", it.netname, " vote = ", ftos(idx), "\n");
501                         ++mapvote_selections[idx];
502                 }
503         });
504 }
505
506 float MapVote_CheckRules_2()
507 {
508         float i;
509         float firstPlace, secondPlace, currentPlace;
510         float firstPlaceVotes, secondPlaceVotes, currentVotes;
511         float mapvote_voters_real;
512         string result;
513
514         if(mapvote_count_real == 1)
515                 return MapVote_Finished(0);
516
517         mapvote_voters_real = mapvote_voters;
518         if(mapvote_abstain)
519                 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
520
521         RandomSelection_Init();
522         currentPlace = 0;
523         currentVotes = -1;
524         for(i = 0; i < mapvote_count_real; ++i)
525                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
526                 {
527                         RandomSelection_Add(NULL, i, string_null, 1, mapvote_selections[i]);
528                         if ( gametypevote &&  mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
529                         {
530                                 currentVotes = mapvote_selections[i];
531                                 currentPlace = i;
532                         }
533                 }
534         firstPlaceVotes = RandomSelection_best_priority;
535         if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
536                 firstPlace = currentPlace;
537         else
538                 firstPlace = RandomSelection_chosen_float;
539
540         //dprint("First place: ", ftos(firstPlace), "\n");
541         //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
542
543         RandomSelection_Init();
544         for(i = 0; i < mapvote_count_real; ++i)
545                 if(i != firstPlace)
546                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
547                         RandomSelection_Add(NULL, i, string_null, 1, mapvote_selections[i]);
548         secondPlace = RandomSelection_chosen_float;
549         secondPlaceVotes = RandomSelection_best_priority;
550         //dprint("Second place: ", ftos(secondPlace), "\n");
551         //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
552
553         if(firstPlace == -1)
554                 error("No first place in map vote... WTF?");
555
556         if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
557                 return MapVote_Finished(firstPlace);
558
559         if(mapvote_keeptwotime)
560                 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
561                 {
562                         float didntvote;
563                         MapVote_TouchMask();
564                         mapvote_message = "Now decide between the TOP TWO!";
565                         mapvote_keeptwotime = 0;
566                         result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
567                         result = strcat(result, ":", ftos(firstPlaceVotes));
568                         result = strcat(result, ":", mapvote_maps[secondPlace]);
569                         result = strcat(result, ":", ftos(secondPlaceVotes), "::");
570                         didntvote = mapvote_voters;
571                         for(i = 0; i < mapvote_count; ++i)
572                         {
573                                 didntvote -= mapvote_selections[i];
574                                 if(i != firstPlace)
575                                         if(i != secondPlace)
576                                         {
577                                                 result = strcat(result, ":", mapvote_maps[i]);
578                                                 result = strcat(result, ":", ftos(mapvote_selections[i]));
579                                                 if(i < mapvote_count_real)
580                                                 {
581                                                         mapvote_maps_flags[i] &= ~GTV_AVAILABLE;
582                                                 }
583                                         }
584                         }
585                         result = strcat(result, ":didn't vote:", ftos(didntvote));
586                         if(autocvar_sv_eventlog)
587                                 GameLogEcho(result);
588                 }
589
590         return false;
591 }
592
593 void MapVote_Tick()
594 {
595         float keeptwo;
596         float totalvotes;
597
598         keeptwo = mapvote_keeptwotime;
599         MapVote_CheckRules_1(); // count
600         if(MapVote_CheckRules_2()) // decide
601                 return;
602
603         totalvotes = 0;
604         FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
605                 // hide scoreboard again
606                 if(it.health != 2342)
607                 {
608                         it.health = 2342;
609                         it.impulse = 0;
610
611                         msg_entity = it;
612                         WriteByte(MSG_ONE, SVC_FINALE);
613                         WriteString(MSG_ONE, "");
614                 }
615
616                 // clear possibly invalid votes
617                 if ( !(mapvote_maps_flags[it.mapvote-1] & GTV_AVAILABLE) )
618                         it.mapvote = 0;
619                 // use impulses as new vote
620                 if(it.impulse >= 1 && it.impulse <= mapvote_count)
621                         if( mapvote_maps_flags[it.impulse - 1] & GTV_AVAILABLE )
622                         {
623                                 it.mapvote = it.impulse;
624                                 MapVote_TouchVotes(it);
625                         }
626                 it.impulse = 0;
627
628                 if(it.mapvote)
629                         ++totalvotes;
630         ));
631
632         MapVote_CheckRules_1(); // just count
633 }
634
635 void MapVote_Start()
636 {
637         // if mapvote is already running, don't do this initialization again
638         if(mapvote_run) { return; }
639
640         // don't start mapvote until after playerstats gamereport is sent
641         if(PlayerStats_GameReport_DelayMapVote) { return; }
642
643         MapInfo_Enumerate();
644         if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
645                 mapvote_run = true;
646 }
647
648 void MapVote_Think()
649 {
650         if(!mapvote_run)
651                 return;
652
653         if(alreadychangedlevel)
654                 return;
655
656         if(time < mapvote_nextthink)
657                 return;
658         //dprint("tick\n");
659
660         mapvote_nextthink = time + 0.5;
661
662         if(!mapvote_initialized)
663         {
664                 if(autocvar_rescan_pending == 1)
665                 {
666                         cvar_set("rescan_pending", "2");
667                         localcmd("fs_rescan\nrescan_pending 3\n");
668                         return;
669                 }
670                 else if(autocvar_rescan_pending == 2)
671                 {
672                         return;
673                 }
674                 else if(autocvar_rescan_pending == 3)
675                 {
676                         // now build missing mapinfo files
677                         if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
678                                 return;
679
680                         // we're done, start the timer
681                         cvar_set("rescan_pending", "0");
682                 }
683
684                 mapvote_initialized = true;
685                 if(DoNextMapOverride(0))
686                         return;
687                 if(!autocvar_g_maplist_votable || player_count <= 0)
688                 {
689                         GotoNextMap(0);
690                         return;
691                 }
692
693                 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
694                 else if(autocvar_nextmap == "") { MapVote_Init(); }
695         }
696
697         MapVote_Tick();
698 }
699
700 float GameTypeVote_SetGametype(Gametype type)
701 {
702         if (MapInfo_CurrentGametype() == type)
703                 return true;
704
705         Gametype tsave = MapInfo_CurrentGametype();
706
707         MapInfo_SwitchGameType(type);
708
709         MapInfo_Enumerate();
710         MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
711         if(MapInfo_count > 0)
712         {
713                 // update lsmaps in case the gametype changed, this way people can easily list maps for it
714                 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
715                 lsmaps_reply = strzone(getlsmaps());
716                 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
717         }
718         else
719         {
720                 bprint("Cannot use this game type: no map for it found\n");
721                 MapInfo_SwitchGameType(tsave);
722                 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
723                 return false;
724         }
725
726         //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
727
728         cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
729         if(autocvar_g_maplist_shuffle)
730                 ShuffleMaplist();
731
732         return true;
733 }
734
735 float gametypevote_finished;
736 float GameTypeVote_Finished(float pos)
737 {
738         if(!gametypevote || gametypevote_finished)
739                 return false;
740
741         if ( !GameTypeVote_SetGametype(GameTypeVote_Type_FromString(mapvote_maps[pos])) )
742         {
743                 LOG_TRACE("Selected gametype is not supported by any map");
744         }
745
746         localcmd("sv_vote_gametype_hook_all\n");
747         localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
748
749         gametypevote_finished = true;
750
751         return true;
752 }
753
754 float GameTypeVote_AddVotable(string nextMode)
755 {
756         float j;
757         if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == NULL )
758                 return false;
759         for(j = 0; j < mapvote_count; ++j)
760                 if(mapvote_maps[j] == nextMode)
761                         return false;
762
763         mapvote_maps[mapvote_count] = strzone(nextMode);
764         mapvote_maps_suggested[mapvote_count] = false;
765
766         mapvote_maps_screenshot_dir[mapvote_count] = 0;
767         mapvote_maps_pakfile[mapvote_count] = strzone("");
768         mapvote_maps_flags[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
769
770         mapvote_count += 1;
771
772         return true;
773
774 }
775
776 float GameTypeVote_Start()
777 {
778         float j;
779         MapVote_ClearAllVotes();
780         MapVote_UnzoneStrings();
781
782         mapvote_count = 0;
783         mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
784         mapvote_abstain = 0;
785         mapvote_detail = !autocvar_g_maplist_votable_nodetail;
786
787         float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
788         n = min(MAPVOTE_COUNT, n);
789
790         float really_available, which_available;
791         really_available = 0;
792         which_available = -1;
793         for(j = 0; j < n; ++j)
794         {
795                 if ( GameTypeVote_AddVotable(argv(j)) )
796                 if ( mapvote_maps_flags[j] & GTV_AVAILABLE )
797                 {
798                         really_available++;
799                         which_available = j;
800                 }
801         }
802
803         mapvote_count_real = mapvote_count;
804
805         gametypevote = 1;
806
807         if ( really_available == 0 )
808         {
809                 if ( mapvote_count > 0 )
810                         strunzone(mapvote_maps[0]);
811                 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
812                 //GameTypeVote_Finished(0);
813                 MapVote_Finished(0);
814                 return false;
815         }
816         if ( really_available == 1 )
817         {
818                 //GameTypeVote_Finished(which_available);
819                 MapVote_Finished(which_available);
820                 return false;
821         }
822
823         mapvote_count_real = mapvote_count;
824
825         mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
826         if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
827                 mapvote_keeptwotime = 0;
828
829         MapVote_Spawn();
830
831         return true;
832 }