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