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