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