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