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