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