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