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