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