]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mapvoting.qc
Apply voted game type on map switch, not on game type vote end so that the "restart...
[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_AddVotableMaps(int nmax, int smax)
205 {
206         int available_maps = 0;
207         if (autocvar_g_maplist != "")
208         {
209                 int c = tokenizebyseparator(autocvar_g_maplist, " ");
210                 for (int i = 0; i < c; ++i)
211                 {
212                         if (Map_Check(i, 1) || Map_Check(i, 2))
213                                 ++available_maps;
214                 }
215         }
216         int max_attempts = available_maps;
217         if (available_maps >= 2)
218                 max_attempts = min(available_maps * 5, 100);
219
220         if (smax && mapvote_suggestion_ptr)
221                 for(int i = 0; i < max_attempts && mapvote_count < smax; ++i)
222                         MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], true);
223
224         for (int i = 0; i < max_attempts && mapvote_count < nmax; ++i)
225                 MapVote_AddVotable(GetNextMap(), false);
226 }
227
228 bool GameTypeVote_SetGametype(Gametype type);
229 void GameTypeVote_ApplyGameType(Gametype type)
230 {
231         localcmd("sv_vote_gametype_hook_all\n");
232         localcmd("sv_vote_gametype_hook_", MapInfo_Type_ToString(type), "\n");
233
234         if (!GameTypeVote_SetGametype(type))
235                 LOG_TRACE("Selected gametype is not supported by any map");
236 }
237
238 Gametype voted_gametype;
239 Gametype match_gametype;
240 void MapVote_Init()
241 {
242         int nmax, smax;
243
244         MapVote_ClearAllVotes();
245         MapVote_UnzoneStrings();
246
247         mapvote_count = 0;
248         mapvote_detail = !autocvar_g_maplist_votable_nodetail;
249         mapvote_abstain = boolean(autocvar_g_maplist_votable_abstain);
250
251         if(mapvote_abstain)
252                 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
253         else
254                 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
255         smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
256
257         // we need this for AddVotable, as that cycles through the screenshot dirs
258         mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
259         if(mapvote_screenshot_dirs_count == 0)
260                 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
261         mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
262         for(int i = 0; i < mapvote_screenshot_dirs_count; ++i)
263                 mapvote_screenshot_dirs[i] = strzone(argv(i));
264
265         MapVote_AddVotableMaps(nmax, smax);
266
267         if(mapvote_count == 0)
268         {
269                 bprint( "Maplist contains no single playable map!  Resetting it to default map list.\n" );
270                 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
271                 if(autocvar_g_maplist_shuffle)
272                         ShuffleMaplist();
273                 localcmd("\nmenu_cmd sync\n");
274                 MapVote_AddVotableMaps(nmax, 0);
275         }
276
277         mapvote_count_real = mapvote_count;
278         if(mapvote_abstain)
279                 MapVote_AddVotable("don't care", false);
280
281         //dprint("mapvote count is ", ftos(mapvote_count), "\n");
282
283         mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
284         mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
285         if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
286                 mapvote_keeptwotime = 0;
287
288         MapVote_Spawn();
289
290         // If match_gametype is set it means voted_gametype has just been applied (on game type vote end).
291         // In this case apply back match_gametype here so that the "restart" command, if called,
292         // properly restarts the map applying the current game type.
293         // Applying voted_gametype before map vote start is needed to properly initialize map vote.
294         if (match_gametype)
295                 GameTypeVote_ApplyGameType(match_gametype);
296 }
297
298 void MapVote_SendPicture(entity to, int id)
299 {
300         msg_entity = to;
301         WriteHeader(MSG_ONE, TE_CSQC_PICTURE);
302         WriteByte(MSG_ONE, id);
303         WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
304 }
305
306
307 void MapVote_WriteMask()
308 {
309         if ( mapvote_count < 24 )
310         {
311                 int mask = 0;
312                 for(int j = 0; j < mapvote_count; ++j)
313                 {
314                         if(mapvote_maps_flags[j] & GTV_AVAILABLE)
315                                 mask |= BIT(j);
316                 }
317
318                 if(mapvote_count < 8)
319                         WriteByte(MSG_ENTITY, mask);
320                 else if (mapvote_count < 16)
321                         WriteShort(MSG_ENTITY,mask);
322                 else
323                         WriteLong(MSG_ENTITY, mask);
324         }
325         else
326         {
327                 for (int j = 0; j < mapvote_count; ++j)
328                         WriteByte(MSG_ENTITY, mapvote_maps_flags[j]);
329         }
330 }
331
332 /*
333  * Sends a single map vote option to the client
334  */
335 void MapVote_SendOption(int i)
336 {
337         // abstain
338         if(mapvote_abstain && i == mapvote_count - 1)
339         {
340                 WriteString(MSG_ENTITY, ""); // abstain needs no text
341                 WriteString(MSG_ENTITY, ""); // abstain needs no pack
342                 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
343         }
344         else
345         {
346                 WriteString(MSG_ENTITY, mapvote_maps[i]);
347                 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
348                 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
349         }
350 }
351
352 /*
353  * Sends a single gametype vote option to the client
354  */
355 void GameTypeVote_SendOption(int i)
356 {
357         // abstain
358         if(mapvote_abstain && i == mapvote_count - 1)
359         {
360                 WriteString(MSG_ENTITY, ""); // abstain needs no text
361                 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
362         }
363         else
364         {
365                 string type_name = mapvote_maps[i];
366                 WriteString(MSG_ENTITY, type_name);
367                 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
368                 if ( mapvote_maps_flags[i] & GTV_CUSTOM )
369                 {
370                         WriteString(MSG_ENTITY, cvar_string(
371                                 strcat("sv_vote_gametype_",type_name,"_name")));
372                         WriteString(MSG_ENTITY, cvar_string(
373                                 strcat("sv_vote_gametype_",type_name,"_description")));
374                         WriteString(MSG_ENTITY, cvar_string(
375                                 strcat("sv_vote_gametype_",type_name,"_type")));
376                 }
377         }
378 }
379
380 int mapvote_winner;
381 float mapvote_winner_time;
382 bool MapVote_SendEntity(entity this, entity to, int sf)
383 {
384         int i;
385
386         if(sf & 1)
387                 sf &= ~2; // if we send 1, we don't need to also send 2
388
389         if (!mapvote_winner_time)
390                 sf &= ~8; // no winner yet
391
392         WriteHeader(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
393         WriteByte(MSG_ENTITY, sf);
394
395         if(sf & 1)
396         {
397                 // flag 1 == initialization
398                 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
399                         WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
400                 WriteString(MSG_ENTITY, "");
401                 WriteByte(MSG_ENTITY, mapvote_count);
402                 WriteByte(MSG_ENTITY, mapvote_abstain);
403                 WriteByte(MSG_ENTITY, mapvote_detail);
404                 WriteCoord(MSG_ENTITY, mapvote_timeout);
405
406                 if ( gametypevote )
407                 {
408                         // gametype vote
409                         WriteByte(MSG_ENTITY, BIT(0)); // gametypevote_flags
410                         WriteString(MSG_ENTITY, autocvar_nextmap);
411                 }
412                 else if ( autocvar_sv_vote_gametype )
413                 {
414                         // map vote but gametype has been chosen via voting screen
415                         WriteByte(MSG_ENTITY, BIT(1)); // gametypevote_flags
416                         WriteString(MSG_ENTITY, MapInfo_Type_ToText(voted_gametype));
417                 }
418                 else
419                         WriteByte(MSG_ENTITY, 0); // map vote
420
421                 MapVote_WriteMask();
422
423                 // Send data for the vote options
424                 for(i = 0; i < mapvote_count; ++i)
425                 {
426                         if(gametypevote)
427                                 GameTypeVote_SendOption(i);
428                         else
429                                 MapVote_SendOption(i);
430                 }
431         }
432
433         if(sf & 2)
434         {
435                 // flag 2 == update of mask
436                 MapVote_WriteMask();
437         }
438
439         if(sf & 4)
440         {
441                 if(mapvote_detail)
442                         for(i = 0; i < mapvote_count; ++i)
443                                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
444                                         WriteByte(MSG_ENTITY, mapvote_selections[i]);
445
446                 WriteByte(MSG_ENTITY, to.mapvote);
447         }
448
449         if(sf & 8)
450         {
451                 WriteByte(MSG_ENTITY, mapvote_winner + 1);
452         }
453
454         return true;
455 }
456
457 void MapVote_Spawn()
458 {
459         Net_LinkEntity(mapvote_ent = new(mapvote_ent), false, 0, MapVote_SendEntity);
460 }
461
462 void MapVote_TouchMask()
463 {
464         mapvote_ent.SendFlags |= 2;
465 }
466
467 void MapVote_TouchVotes(entity voter)
468 {
469         mapvote_ent.SendFlags |= 4;
470 }
471
472 void MapVote_Winner(int mappos)
473 {
474         mapvote_ent.SendFlags |= 8;
475         mapvote_winner_time = time;
476         mapvote_winner = mappos;
477 }
478
479 bool MapVote_Finished(int mappos)
480 {
481         if(alreadychangedlevel)
482                 return false;
483
484         string result;
485         int i;
486         int didntvote;
487
488         if(autocvar_sv_eventlog)
489         {
490                 result = strcat(":vote:finished:", mapvote_maps[mappos]);
491                 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
492                 didntvote = mapvote_voters;
493                 for(i = 0; i < mapvote_count; ++i)
494                         if(mapvote_maps_flags[i] & GTV_AVAILABLE )
495                         {
496                                 didntvote -= mapvote_selections[i];
497                                 if(i != mappos)
498                                 {
499                                         result = strcat(result, ":", mapvote_maps[i]);
500                                         result = strcat(result, ":", ftos(mapvote_selections[i]));
501                                 }
502                         }
503                 result = strcat(result, ":didn't vote:", ftos(didntvote));
504
505                 GameLogEcho(result);
506                 if(mapvote_maps_suggested[mappos])
507                         GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
508         }
509
510         FOREACH_CLIENT(IS_REAL_CLIENT(it), { FixClientCvars(it); });
511
512         if(gametypevote)
513         {
514                 if ( GameTypeVote_Finished(mappos) )
515                 {
516                         gametypevote = false;
517                         if(autocvar_nextmap != "")
518                         {
519                                 Map_Goto_SetStr(autocvar_nextmap);
520                                 Map_Goto(0);
521                                 alreadychangedlevel = true;
522                                 return true;
523                         }
524                         else
525                                 MapVote_Init();
526                 }
527                 return false;
528         }
529
530         MapVote_Winner(mappos);
531         alreadychangedlevel = true;
532
533         return true;
534 }
535
536 void MapVote_CheckRules_1()
537 {
538         for (int i = 0; i < mapvote_count; ++i)
539                 if (mapvote_maps_flags[i] & GTV_AVAILABLE)
540                 {
541                         //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
542                         mapvote_selections[i] = 0;
543                 }
544
545         mapvote_voters = 0;
546         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
547                 ++mapvote_voters;
548                 if (it.mapvote)
549                 {
550                         int idx = it.mapvote - 1;
551                         //dprint("Player ", it.netname, " vote = ", ftos(idx), "\n");
552                         ++mapvote_selections[idx];
553                 }
554         });
555 }
556
557 bool MapVote_CheckRules_2()
558 {
559         int i;
560         int firstPlace, secondPlace, currentPlace;
561         int firstPlaceVotes, secondPlaceVotes, currentVotes;
562         int mapvote_voters_real;
563         string result;
564
565         if(mapvote_count_real == 1)
566                 return MapVote_Finished(0);
567
568         mapvote_voters_real = mapvote_voters;
569         if(mapvote_abstain)
570                 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
571
572         RandomSelection_Init();
573         currentPlace = 0;
574         currentVotes = -1;
575         for(i = 0; i < mapvote_count_real; ++i)
576                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
577                 {
578                         RandomSelection_AddFloat(i, 1, mapvote_selections[i]);
579                         if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
580                         {
581                                 currentVotes = mapvote_selections[i];
582                                 currentPlace = i;
583                         }
584                 }
585         firstPlaceVotes = RandomSelection_best_priority;
586         if (gametypevote && autocvar_sv_vote_gametype_default_current && firstPlaceVotes == 0)
587                 firstPlace = currentPlace;
588         else
589                 firstPlace = RandomSelection_chosen_float;
590
591         //dprint("First place: ", ftos(firstPlace), "\n");
592         //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
593
594         RandomSelection_Init();
595         for(i = 0; i < mapvote_count_real; ++i)
596                 if(i != firstPlace)
597                 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
598                         RandomSelection_AddFloat(i, 1, mapvote_selections[i]);
599         secondPlace = RandomSelection_chosen_float;
600         secondPlaceVotes = RandomSelection_best_priority;
601         //dprint("Second place: ", ftos(secondPlace), "\n");
602         //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
603
604         if(firstPlace == -1)
605                 error("No first place in map vote... WTF?");
606
607         if(secondPlace == -1 || time > mapvote_timeout
608                 || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes
609                 || mapvote_selections[mapvote_count - 1] == mapvote_voters)
610         {
611                 return MapVote_Finished(firstPlace);
612         }
613
614         if(mapvote_keeptwotime)
615                 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
616                 {
617                         MapVote_TouchMask();
618                         mapvote_keeptwotime = 0;
619                         result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
620                         result = strcat(result, ":", ftos(firstPlaceVotes));
621                         result = strcat(result, ":", mapvote_maps[secondPlace]);
622                         result = strcat(result, ":", ftos(secondPlaceVotes), "::");
623                         int didntvote = mapvote_voters;
624                         for(i = 0; i < mapvote_count; ++i)
625                         {
626                                 didntvote -= mapvote_selections[i];
627                                 if(i != firstPlace)
628                                         if(i != secondPlace)
629                                         {
630                                                 result = strcat(result, ":", mapvote_maps[i]);
631                                                 result = strcat(result, ":", ftos(mapvote_selections[i]));
632                                                 if(i < mapvote_count_real)
633                                                 {
634                                                         mapvote_maps_flags[i] &= ~GTV_AVAILABLE;
635                                                 }
636                                         }
637                         }
638                         result = strcat(result, ":didn't vote:", ftos(didntvote));
639                         if(autocvar_sv_eventlog)
640                                 GameLogEcho(result);
641                 }
642
643         return false;
644 }
645
646 void MapVote_Tick()
647 {
648         MapVote_CheckRules_1(); // count
649         if(MapVote_CheckRules_2()) // decide
650                 return;
651
652         int totalvotes = 0;
653         FOREACH_CLIENT(true, {
654                 if(!IS_REAL_CLIENT(it))
655                 {
656                         // apply the same special health value to bots too for consistency's sake
657                         if(GetResource(it, RES_HEALTH) != 2342)
658                                 SetResourceExplicit(it, RES_HEALTH, 2342);
659                         continue;
660                 }
661                 // hide scoreboard again
662                 if(GetResource(it, RES_HEALTH) != 2342)
663                 {
664                         SetResourceExplicit(it, RES_HEALTH, 2342); // health in the voting phase
665                         CS(it).impulse = 0;
666
667                         msg_entity = it;
668                         WriteByte(MSG_ONE, SVC_FINALE);
669                         WriteString(MSG_ONE, "");
670                 }
671
672                 // clear possibly invalid votes
673                 if ( !(mapvote_maps_flags[it.mapvote-1] & GTV_AVAILABLE) )
674                         it.mapvote = 0;
675                 // use impulses as new vote
676                 if(CS(it).impulse >= 1 && CS(it).impulse <= mapvote_count)
677                         if( mapvote_maps_flags[CS(it).impulse - 1] & GTV_AVAILABLE )
678                         {
679                                 it.mapvote = CS(it).impulse;
680                                 MapVote_TouchVotes(it);
681                         }
682                 CS(it).impulse = 0;
683
684                 if(it.mapvote)
685                         ++totalvotes;
686         });
687
688         MapVote_CheckRules_1(); // just count
689 }
690
691 void MapVote_Start()
692 {
693         // if mapvote is already running, don't do this initialization again
694         if(mapvote_run) { return; }
695
696         // don't start mapvote until after playerstats gamereport is sent
697         if(PlayerStats_GameReport_DelayMapVote) { return; }
698
699         MapInfo_Enumerate();
700         if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
701                 mapvote_run = true;
702 }
703
704 void MapVote_Think()
705 {
706         if(!mapvote_run)
707                 return;
708
709         if (mapvote_winner_time)
710         {
711                 if (time > mapvote_winner_time + 1)
712                 {
713                         if (voted_gametype)
714                         {
715                                 // clear match_gametype so that GameTypeVote_ApplyGameType
716                                 // prints the game type switch message
717                                 match_gametype = NULL;
718                                 GameTypeVote_ApplyGameType(voted_gametype);
719                         }
720
721                         Map_Goto_SetStr(mapvote_maps[mapvote_winner]);
722                         Map_Goto(0);
723                 }
724                 return;
725         }
726
727         if(alreadychangedlevel)
728                 return;
729
730         if(time < mapvote_nextthink)
731                 return;
732         //dprint("tick\n");
733
734         mapvote_nextthink = time + 0.5;
735         if (mapvote_nextthink > mapvote_timeout - 0.1) // make sure there's no delay when map vote times out
736                 mapvote_nextthink = mapvote_timeout + 0.001;
737
738         if(!mapvote_initialized)
739         {
740                 if(autocvar_rescan_pending == 1)
741                 {
742                         cvar_set("rescan_pending", "2");
743                         localcmd("fs_rescan\nrescan_pending 3\n");
744                         return;
745                 }
746                 else if(autocvar_rescan_pending == 2)
747                 {
748                         return;
749                 }
750                 else if(autocvar_rescan_pending == 3)
751                 {
752                         // now build missing mapinfo files
753                         if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
754                                 return;
755
756                         // we're done, start the timer
757                         cvar_set("rescan_pending", "0");
758                 }
759
760                 mapvote_initialized = true;
761                 if(DoNextMapOverride(0))
762                         return;
763                 if(!autocvar_g_maplist_votable || player_count <= 0)
764                 {
765                         GotoNextMap(0);
766                         return;
767                 }
768
769                 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
770                 else if(autocvar_nextmap == "") { MapVote_Init(); }
771         }
772
773         MapVote_Tick();
774 }
775
776 bool GameTypeVote_SetGametype(Gametype type)
777 {
778         if (MapInfo_CurrentGametype() == type)
779                 return true;
780
781         Gametype tsave = MapInfo_CurrentGametype();
782
783         MapInfo_SwitchGameType(type);
784
785         MapInfo_Enumerate();
786         MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
787         if(MapInfo_count > 0)
788         {
789                 // update lsmaps in case the gametype changed, this way people can easily list maps for it
790                 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
791                 lsmaps_reply = strzone(getlsmaps());
792
793                 if (!match_gametype) // don't show this msg if we are temporarily switching game type
794                         bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
795         }
796         else
797         {
798                 bprint("Cannot use this game type: no map for it found\n");
799                 MapInfo_SwitchGameType(tsave);
800                 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
801                 return false;
802         }
803
804         //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
805
806         cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
807         if(autocvar_g_maplist_shuffle)
808                 ShuffleMaplist();
809
810         return true;
811 }
812
813 bool gametypevote_finished;
814 bool GameTypeVote_Finished(int pos)
815 {
816         if(!gametypevote || gametypevote_finished)
817                 return false;
818
819         match_gametype = MapInfo_CurrentGametype();
820         voted_gametype = MapInfo_Type_FromString(mapvote_maps[pos], false, false);
821
822         GameTypeVote_ApplyGameType(voted_gametype);
823
824         gametypevote_finished = true;
825
826         return true;
827 }
828
829 bool GameTypeVote_AddVotable(string nextMode)
830 {
831         if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == NULL )
832                 return false;
833
834         for(int j = 0; j < mapvote_count; ++j)
835                 if(mapvote_maps[j] == nextMode)
836                         return false;
837
838         mapvote_maps[mapvote_count] = strzone(nextMode);
839         mapvote_maps_suggested[mapvote_count] = false;
840
841         mapvote_maps_screenshot_dir[mapvote_count] = 0;
842         mapvote_maps_pakfile[mapvote_count] = strzone("");
843         mapvote_maps_flags[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
844
845         mapvote_count += 1;
846
847         return true;
848
849 }
850
851 bool GameTypeVote_Start()
852 {
853         MapVote_ClearAllVotes();
854         MapVote_UnzoneStrings();
855
856         mapvote_count = 0;
857         mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
858         mapvote_abstain = false;
859         mapvote_detail = !autocvar_g_maplist_votable_nodetail;
860
861         int n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
862         n = min(MAPVOTE_COUNT, n);
863
864         int really_available, which_available;
865         really_available = 0;
866         which_available = -1;
867         for(int j = 0; j < n; ++j)
868         {
869                 if ( GameTypeVote_AddVotable(argv(j)) )
870                 if ( mapvote_maps_flags[j] & GTV_AVAILABLE )
871                 {
872                         really_available++;
873                         which_available = j;
874                 }
875         }
876
877         mapvote_count_real = mapvote_count;
878
879         gametypevote = true;
880
881         if ( really_available == 0 )
882         {
883                 if ( mapvote_count > 0 )
884                         strunzone(mapvote_maps[0]);
885                 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
886                 //GameTypeVote_Finished(0);
887                 MapVote_Finished(0);
888                 return false;
889         }
890         if ( really_available == 1 )
891         {
892                 //GameTypeVote_Finished(which_available);
893                 MapVote_Finished(which_available);
894                 return false;
895         }
896
897         mapvote_count_real = mapvote_count;
898
899         mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
900         if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
901                 mapvote_keeptwotime = 0;
902
903         MapVote_Spawn();
904
905         return true;
906 }