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