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