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