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