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