Merge branch 'master' into Mario/vaporizer_damage
[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_flags[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 int GameTypeVote_AvailabilityStatus(string type_name)
55 {
56         int flag = GTV_FORBIDDEN;
57
58         float type = MapInfo_Type_FromString(type_name);
59         if ( type == 0 )
60         {
61                 type = MapInfo_Type_FromString(cvar_string(
62                         strcat("sv_vote_gametype_",type_name,"_type")));
63                 flag |= GTV_CUSTOM;
64         }
65
66         if( type == 0 )
67                 return flag;
68
69         if ( autocvar_nextmap != "" )
70         {
71                 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, 0) )
72                         return flag;
73                 if (!(MapInfo_Map_supportedGametypes & type))
74                         return flag;
75         }
76
77         return flag | GTV_AVAILABLE;
78 }
79
80 float GameTypeVote_GetMask()
81 {
82         float n, j, gametype_mask;
83         n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
84         n = min(MAPVOTE_COUNT, n);
85         gametype_mask = 0;
86         for(j = 0; j < n; ++j)
87                 gametype_mask |= GameTypeVote_Type_FromString(argv(j));
88         return gametype_mask;
89 }
90
91 string GameTypeVote_MapInfo_FixName(string m)
92 {
93         if ( autocvar_sv_vote_gametype )
94         {
95                 MapInfo_Enumerate();
96                 MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
97         }
98         return MapInfo_FixName(m);
99 }
100
101 void MapVote_ClearAllVotes()
102 {
103         FOR_EACH_CLIENT(other)
104                 other.mapvote = 0;
105 }
106
107 void MapVote_UnzoneStrings()
108 {
109         float j;
110         for(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(string m)
126 {
127         float 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(self.playerid)));
162         return strcat("Suggestion of ", m, " accepted.");
163 }
164
165 void MapVote_AddVotable(string nextMap, float isSuggestion)
166 {
167         float 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], "/", mapvote_maps[i]);
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 = strstr(pakfile, "/", 0)+1; o > 0; o = strstr(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         float i;
209         float 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 = 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", 0);
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         mapvote_message = "Choose a map and press its key!";
261
262         MapVote_Spawn();
263 }
264
265 void MapVote_SendPicture(float id)
266 {
267         msg_entity = self;
268         WriteByte(MSG_ONE, SVC_TEMPENTITY);
269         WriteByte(MSG_ONE, TE_CSQC_PICTURE);
270         WriteByte(MSG_ONE, id);
271         WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
272 }
273
274
275 void MapVote_WriteMask()
276 {
277         float i;
278         if ( mapvote_count < 24 )
279         {
280                 float mask,power;
281                 mask = 0;
282                 for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
283                         if(mapvote_maps_flags[i] & GTV_AVAILABLE )
284                                 mask |= power;
285
286                 if(mapvote_count < 8)
287                         WriteByte(MSG_ENTITY, mask);
288                 else if (mapvote_count < 16)
289                         WriteShort(MSG_ENTITY,mask);
290                 else
291                         WriteLong(MSG_ENTITY, mask);
292         }
293         else
294         {
295                 for ( i = 0; i < mapvote_count; ++i )
296                         WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
297         }
298 }
299
300 /*
301  * Sends a single map vote option to the client
302  */
303 void MapVote_SendOption(int i)
304 {
305         // abstain
306         if(mapvote_abstain && i == mapvote_count - 1)
307         {
308                 WriteString(MSG_ENTITY, ""); // abstain needs no text
309                 WriteString(MSG_ENTITY, ""); // abstain needs no pack
310                 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
311         }
312         else
313         {
314                 WriteString(MSG_ENTITY, mapvote_maps[i]);
315                 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
316                 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
317         }
318 }
319
320 /*
321  * Sends a single gametype vote option to the client
322  */
323 void GameTypeVote_SendOption(int i)
324 {
325         // abstain
326         if(mapvote_abstain && i == mapvote_count - 1)
327         {
328                 WriteString(MSG_ENTITY, ""); // abstain needs no text
329                 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
330         }
331         else
332         {
333                 string type_name = mapvote_maps[i];
334                 WriteString(MSG_ENTITY, type_name);
335                 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
336                 if ( mapvote_maps_flags[i] & GTV_CUSTOM )
337                 {
338                         WriteString(MSG_ENTITY, cvar_string(
339                                 strcat("sv_vote_gametype_",type_name,"_name")));
340                         WriteString(MSG_ENTITY, cvar_string(
341                                 strcat("sv_vote_gametype_",type_name,"_description")));
342                 }
343         }
344 }
345
346 float MapVote_SendEntity(entity to, int sf)
347 {
348         float i;
349
350         if(sf & 1)
351                 sf &= ~2; // if we send 1, we don't need to also send 2
352
353         WriteByte(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 float MapVote_Finished(float mappos)
429 {
430         if(alreadychangedlevel)
431                 return false;
432
433         string result;
434         float i;
435         float 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         FOR_EACH_REALCLIENT(other)
460                 FixClientCvars(other);
461
462         if(gametypevote)
463         {
464                 if ( GameTypeVote_Finished(mappos) )
465                 {
466                         gametypevote = false;
467                         if(autocvar_nextmap != "")
468                         {
469                                 Map_Goto_SetStr(autocvar_nextmap);
470                                 Map_Goto(0);
471                                 alreadychangedlevel = true;
472                                 return true;
473                         }
474                         else
475                                 MapVote_Init();
476                 }
477                 return false;
478         }
479
480         Map_Goto_SetStr(mapvote_maps[mappos]);
481         Map_Goto(0);
482         alreadychangedlevel = true;
483
484         return true;
485 }
486
487 void MapVote_CheckRules_1()
488 {
489         float i;
490
491         for(i = 0; i < mapvote_count; ++i)
492                 if( mapvote_maps_flags[i] & GTV_AVAILABLE )
493                 {
494                         //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
495                         mapvote_selections[i] = 0;
496                 }
497
498         mapvote_voters = 0;
499         FOR_EACH_REALCLIENT(other)
500         {
501                 ++mapvote_voters;
502                 if(other.mapvote)
503                 {
504                         i = other.mapvote - 1;
505                         //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
506                         mapvote_selections[i] = mapvote_selections[i] + 1;
507                 }
508         }
509 }
510
511 float MapVote_CheckRules_2()
512 {
513         float i;
514         float firstPlace, secondPlace, currentPlace;
515         float firstPlaceVotes, secondPlaceVotes, currentVotes;
516         float mapvote_voters_real;
517         string result;
518
519         if(mapvote_count_real == 1)
520                 return MapVote_Finished(0);
521
522         mapvote_voters_real = mapvote_voters;
523         if(mapvote_abstain)
524                 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
525
526         RandomSelection_Init();
527         currentPlace = 0;
528         currentVotes = -1;
529         for(i = 0; i < mapvote_count_real; ++i)
530                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
531                 {
532                         RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
533                         if ( gametypevote &&  mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
534                         {
535                                 currentVotes = mapvote_selections[i];
536                                 currentPlace = i;
537                         }
538                 }
539         firstPlaceVotes = RandomSelection_best_priority;
540         if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
541                 firstPlace = currentPlace;
542         else
543                 firstPlace = RandomSelection_chosen_float;
544
545         //dprint("First place: ", ftos(firstPlace), "\n");
546         //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
547
548         RandomSelection_Init();
549         for(i = 0; i < mapvote_count_real; ++i)
550                 if(i != firstPlace)
551                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
552                         RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
553         secondPlace = RandomSelection_chosen_float;
554         secondPlaceVotes = RandomSelection_best_priority;
555         //dprint("Second place: ", ftos(secondPlace), "\n");
556         //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
557
558         if(firstPlace == -1)
559                 error("No first place in map vote... WTF?");
560
561         if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
562                 return MapVote_Finished(firstPlace);
563
564         if(mapvote_keeptwotime)
565                 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
566                 {
567                         float didntvote;
568                         MapVote_TouchMask();
569                         mapvote_message = "Now decide between the TOP TWO!";
570                         mapvote_keeptwotime = 0;
571                         result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
572                         result = strcat(result, ":", ftos(firstPlaceVotes));
573                         result = strcat(result, ":", mapvote_maps[secondPlace]);
574                         result = strcat(result, ":", ftos(secondPlaceVotes), "::");
575                         didntvote = mapvote_voters;
576                         for(i = 0; i < mapvote_count; ++i)
577                         {
578                                 didntvote -= mapvote_selections[i];
579                                 if(i != firstPlace)
580                                         if(i != secondPlace)
581                                         {
582                                                 result = strcat(result, ":", mapvote_maps[i]);
583                                                 result = strcat(result, ":", ftos(mapvote_selections[i]));
584                                                 if(i < mapvote_count_real)
585                                                 {
586                                                         mapvote_maps_flags[i] &= ~GTV_AVAILABLE;
587                                                 }
588                                         }
589                         }
590                         result = strcat(result, ":didn't vote:", ftos(didntvote));
591                         if(autocvar_sv_eventlog)
592                                 GameLogEcho(result);
593                 }
594
595         return false;
596 }
597
598 void MapVote_Tick()
599 {
600         float keeptwo;
601         float totalvotes;
602
603         keeptwo = mapvote_keeptwotime;
604         MapVote_CheckRules_1(); // count
605         if(MapVote_CheckRules_2()) // decide
606                 return;
607
608         totalvotes = 0;
609         FOR_EACH_REALCLIENT(other)
610         {
611                 // hide scoreboard again
612                 if(other.health != 2342)
613                 {
614                         other.health = 2342;
615                         other.impulse = 0;
616                         if(IS_REAL_CLIENT(other))
617                         {
618                                 msg_entity = other;
619                                 WriteByte(MSG_ONE, SVC_FINALE);
620                                 WriteString(MSG_ONE, "");
621                         }
622                 }
623
624                 // clear possibly invalid votes
625                 if ( !(mapvote_maps_flags[other.mapvote-1] & GTV_AVAILABLE) )
626                         other.mapvote = 0;
627                 // use impulses as new vote
628                 if(other.impulse >= 1 && other.impulse <= mapvote_count)
629                         if( mapvote_maps_flags[other.impulse - 1] & GTV_AVAILABLE )
630                         {
631                                 other.mapvote = other.impulse;
632                                 MapVote_TouchVotes(other);
633                         }
634                 other.impulse = 0;
635
636                 if(other.mapvote)
637                         ++totalvotes;
638         }
639
640         MapVote_CheckRules_1(); // just count
641 }
642
643 void MapVote_Start()
644 {
645         // if mapvote is already running, don't do this initialization again
646         if(mapvote_run) { return; }
647
648         // don't start mapvote until after playerstats gamereport is sent
649         if(PlayerStats_GameReport_DelayMapVote) { return; }
650
651         MapInfo_Enumerate();
652         if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
653                 mapvote_run = true;
654 }
655
656 void MapVote_Think()
657 {
658         if(!mapvote_run)
659                 return;
660
661         if(alreadychangedlevel)
662                 return;
663
664         if(time < mapvote_nextthink)
665                 return;
666         //dprint("tick\n");
667
668         mapvote_nextthink = time + 0.5;
669
670         if(!mapvote_initialized)
671         {
672                 if(autocvar_rescan_pending == 1)
673                 {
674                         cvar_set("rescan_pending", "2");
675                         localcmd("fs_rescan\nrescan_pending 3\n");
676                         return;
677                 }
678                 else if(autocvar_rescan_pending == 2)
679                 {
680                         return;
681                 }
682                 else if(autocvar_rescan_pending == 3)
683                 {
684                         // now build missing mapinfo files
685                         if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
686                                 return;
687
688                         // we're done, start the timer
689                         cvar_set("rescan_pending", "0");
690                 }
691
692                 mapvote_initialized = true;
693                 if(DoNextMapOverride(0))
694                         return;
695                 if(!autocvar_g_maplist_votable || player_count <= 0)
696                 {
697                         GotoNextMap(0);
698                         return;
699                 }
700
701                 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
702                 else if(autocvar_nextmap == "") { MapVote_Init(); }
703         }
704
705         MapVote_Tick();
706 }
707
708 float GameTypeVote_SetGametype(float type)
709 {
710         if (MapInfo_CurrentGametype() == type)
711                 return true;
712
713         float tsave = MapInfo_CurrentGametype();
714
715         MapInfo_SwitchGameType(type);
716
717         MapInfo_Enumerate();
718         MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
719         if(MapInfo_count > 0)
720         {
721                 // update lsmaps in case the gametype changed, this way people can easily list maps for it
722                 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
723                 lsmaps_reply = strzone(getlsmaps());
724                 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
725         }
726         else
727         {
728                 bprint("Cannot use this game type: no map for it found\n");
729                 MapInfo_SwitchGameType(tsave);
730                 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
731                 return false;
732         }
733
734         //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
735
736         cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
737         if(autocvar_g_maplist_shuffle)
738                 ShuffleMaplist();
739
740         return true;
741 }
742
743 float gametypevote_finished;
744 float GameTypeVote_Finished(float pos)
745 {
746         if(!gametypevote || gametypevote_finished)
747                 return false;
748
749         if ( !GameTypeVote_SetGametype(GameTypeVote_Type_FromString(mapvote_maps[pos])) )
750         {
751                 LOG_TRACE("Selected gametype is not supported by any map");
752         }
753
754         localcmd("sv_vote_gametype_hook_all\n");
755         localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
756
757         gametypevote_finished = true;
758
759         return true;
760 }
761
762 float GameTypeVote_AddVotable(string nextMode)
763 {
764         float j;
765         if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == 0 )
766                 return false;
767         for(j = 0; j < mapvote_count; ++j)
768                 if(mapvote_maps[j] == nextMode)
769                         return false;
770
771         mapvote_maps[mapvote_count] = strzone(nextMode);
772         mapvote_maps_suggested[mapvote_count] = false;
773
774         mapvote_maps_screenshot_dir[mapvote_count] = 0;
775         mapvote_maps_pakfile[mapvote_count] = strzone("");
776         mapvote_maps_flags[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
777
778         mapvote_count += 1;
779
780         return true;
781
782 }
783
784 float GameTypeVote_Start()
785 {
786         float j;
787         MapVote_ClearAllVotes();
788         MapVote_UnzoneStrings();
789
790         mapvote_count = 0;
791         mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
792         mapvote_abstain = 0;
793         mapvote_detail = !autocvar_g_maplist_votable_nodetail;
794
795         float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
796         n = min(MAPVOTE_COUNT, n);
797
798         float really_available, which_available;
799         really_available = 0;
800         which_available = -1;
801         for(j = 0; j < n; ++j)
802         {
803                 if ( GameTypeVote_AddVotable(argv(j)) )
804                 if ( mapvote_maps_flags[j] & GTV_AVAILABLE )
805                 {
806                         really_available++;
807                         which_available = j;
808                 }
809         }
810
811         mapvote_count_real = mapvote_count;
812
813         gametypevote = 1;
814
815         if ( really_available == 0 )
816         {
817                 if ( mapvote_count > 0 )
818                         strunzone(mapvote_maps[0]);
819                 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
820                 //GameTypeVote_Finished(0);
821                 MapVote_Finished(0);
822                 return false;
823         }
824         if ( really_available == 1 )
825         {
826                 //GameTypeVote_Finished(which_available);
827                 MapVote_Finished(which_available);
828                 return false;
829         }
830
831         mapvote_count_real = mapvote_count;
832
833         mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
834         if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
835                 mapvote_keeptwotime = 0;
836
837         MapVote_Spawn();
838
839         return true;
840 }