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