1 #include "mapvoting.qh"
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>
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;
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;
36 int mapvote_selections[MAPVOTE_COUNT];
37 int mapvote_maps_flags[MAPVOTE_COUNT];
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
49 Gametype GameTypeVote_Type_FromString(string type_name)
51 Gametype type = MapInfo_Type_FromString(type_name, false);
53 type = MapInfo_Type_FromString(cvar_string(
54 strcat("sv_vote_gametype_",type_name,"_type")), false);
58 int GameTypeVote_AvailabilityStatus(string type_name)
60 int flag = GTV_FORBIDDEN;
62 Gametype type = MapInfo_Type_FromString(type_name, false);
65 type = MapInfo_Type_FromString(cvar_string(
66 strcat("sv_vote_gametype_",type_name,"_type")), false);
73 if ( autocvar_nextmap != "" )
75 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, NULL) )
77 if (!(MapInfo_Map_supportedGametypes & type.m_flags))
81 return flag | GTV_AVAILABLE;
84 int GameTypeVote_GetMask()
86 int n, j, gametype_mask;
87 n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
88 n = min(MAPVOTE_COUNT, n);
90 for(j = 0; j < n; ++j)
91 gametype_mask |= GameTypeVote_Type_FromString(argv(j)).m_flags;
95 string GameTypeVote_MapInfo_FixName(string m)
97 if ( autocvar_sv_vote_gametype )
100 _MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
102 return MapInfo_FixName(m);
105 void MapVote_ClearAllVotes()
107 FOREACH_CLIENT(true, { it.mapvote = 0; });
110 void MapVote_UnzoneStrings()
112 for(int j = 0; j < mapvote_count; ++j)
114 strfree(mapvote_maps[j]);
115 strfree(mapvote_maps_pakfile[j]);
119 string MapVote_Suggest(entity this, string 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)
128 return "Can't suggest - voting is already in progress!";
129 m = GameTypeVote_MapInfo_FixName(m);
131 return "The map you suggested is not available on this server.";
132 if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
134 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
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)
144 i = floor(random() * mapvote_suggestion_ptr);
148 i = mapvote_suggestion_ptr;
149 mapvote_suggestion_ptr += 1;
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.");
159 void MapVote_AddVotable(string nextMap, bool isSuggestion)
162 string pakfile, mapfile;
166 for(j = 0; j < mapvote_count; ++j)
167 if(mapvote_maps[j] == nextMap)
169 // suggestions might be no longer valid/allowed after gametype switch!
171 if(!MapInfo_CheckMap(nextMap))
173 mapvote_maps[mapvote_count] = strzone(nextMap);
174 mapvote_maps_suggested[mapvote_count] = isSuggestion;
176 pakfile = string_null;
177 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
179 mapfile = strcat(mapvote_screenshot_dirs[i], "/", nextMap);
180 pakfile = whichpack(strcat(mapfile, ".tga"));
182 pakfile = whichpack(strcat(mapfile, ".jpg"));
184 pakfile = whichpack(strcat(mapfile, ".png"));
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);
193 mapvote_maps_screenshot_dir[mapvote_count] = i;
194 mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
195 mapvote_maps_flags[mapvote_count] = GTV_AVAILABLE;
205 MapVote_ClearAllVotes();
206 MapVote_UnzoneStrings();
209 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
210 mapvote_abstain = boolean(autocvar_g_maplist_votable_abstain);
213 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
215 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
216 smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
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));
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);
230 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
231 MapVote_AddVotable(GetNextMap(), false);
233 if(mapvote_count == 0)
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)
239 localcmd("\nmenu_cmd sync\n");
240 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
241 MapVote_AddVotable(GetNextMap(), false);
244 mapvote_count_real = mapvote_count;
246 MapVote_AddVotable("don't care", false);
248 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
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;
258 void MapVote_SendPicture(entity to, int id)
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);
267 void MapVote_WriteMask()
269 if ( mapvote_count < 24 )
272 for(int j = 0; j < mapvote_count; ++j)
274 if(mapvote_maps_flags[j] & GTV_AVAILABLE)
278 if(mapvote_count < 8)
279 WriteByte(MSG_ENTITY, mask);
280 else if (mapvote_count < 16)
281 WriteShort(MSG_ENTITY,mask);
283 WriteLong(MSG_ENTITY, mask);
287 for (int j = 0; j < mapvote_count; ++j)
288 WriteByte(MSG_ENTITY, mapvote_maps_flags[j]);
293 * Sends a single map vote option to the client
295 void MapVote_SendOption(int i)
298 if(mapvote_abstain && i == mapvote_count - 1)
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
306 WriteString(MSG_ENTITY, mapvote_maps[i]);
307 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
308 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
313 * Sends a single gametype vote option to the client
315 void GameTypeVote_SendOption(int i)
318 if(mapvote_abstain && i == mapvote_count - 1)
320 WriteString(MSG_ENTITY, ""); // abstain needs no text
321 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
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 )
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")));
340 bool MapVote_SendEntity(entity this, entity to, int sf)
345 sf &= ~2; // if we send 1, we don't need to also send 2
347 WriteHeader(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
348 WriteByte(MSG_ENTITY, sf);
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);
364 WriteByte(MSG_ENTITY, 1);
365 WriteString(MSG_ENTITY, autocvar_nextmap);
367 else if ( autocvar_sv_vote_gametype )
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()));
374 WriteByte(MSG_ENTITY, 0); // map vote
378 // Send data for the vote options
379 for(i = 0; i < mapvote_count; ++i)
382 GameTypeVote_SendOption(i);
384 MapVote_SendOption(i);
390 // flag 2 == update of mask
397 for(i = 0; i < mapvote_count; ++i)
398 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
399 WriteByte(MSG_ENTITY, mapvote_selections[i]);
401 WriteByte(MSG_ENTITY, to.mapvote);
409 Net_LinkEntity(mapvote_ent = new(mapvote_ent), false, 0, MapVote_SendEntity);
412 void MapVote_TouchMask()
414 mapvote_ent.SendFlags |= 2;
417 void MapVote_TouchVotes(entity voter)
419 mapvote_ent.SendFlags |= 4;
422 bool MapVote_Finished(int mappos)
424 if(alreadychangedlevel)
431 if(autocvar_sv_eventlog)
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 )
439 didntvote -= mapvote_selections[i];
442 result = strcat(result, ":", mapvote_maps[i]);
443 result = strcat(result, ":", ftos(mapvote_selections[i]));
446 result = strcat(result, ":didn't vote:", ftos(didntvote));
449 if(mapvote_maps_suggested[mappos])
450 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
453 FOREACH_CLIENT(IS_REAL_CLIENT(it), { FixClientCvars(it); });
457 if ( GameTypeVote_Finished(mappos) )
459 gametypevote = false;
460 if(autocvar_nextmap != "")
462 Map_Goto_SetStr(autocvar_nextmap);
464 alreadychangedlevel = true;
473 Map_Goto_SetStr(mapvote_maps[mappos]);
475 alreadychangedlevel = true;
480 void MapVote_CheckRules_1()
482 for (int i = 0; i < mapvote_count; ++i)
483 if (mapvote_maps_flags[i] & GTV_AVAILABLE)
485 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
486 mapvote_selections[i] = 0;
490 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
494 int idx = it.mapvote - 1;
495 //dprint("Player ", it.netname, " vote = ", ftos(idx), "\n");
496 ++mapvote_selections[idx];
501 bool MapVote_CheckRules_2()
504 int firstPlace, secondPlace, currentPlace;
505 int firstPlaceVotes, secondPlaceVotes, currentVotes;
506 int mapvote_voters_real;
509 if(mapvote_count_real == 1)
510 return MapVote_Finished(0);
512 mapvote_voters_real = mapvote_voters;
514 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
516 RandomSelection_Init();
519 for(i = 0; i < mapvote_count_real; ++i)
520 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
522 RandomSelection_AddFloat(i, 1, mapvote_selections[i]);
523 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
525 currentVotes = mapvote_selections[i];
529 firstPlaceVotes = RandomSelection_best_priority;
530 if ( autocvar_sv_vote_gametype_default_current && firstPlaceVotes == 0 )
531 firstPlace = currentPlace;
533 firstPlace = RandomSelection_chosen_float;
535 //dprint("First place: ", ftos(firstPlace), "\n");
536 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
538 RandomSelection_Init();
539 for(i = 0; i < mapvote_count_real; ++i)
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");
549 error("No first place in map vote... WTF?");
551 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
552 return MapVote_Finished(firstPlace);
554 if(mapvote_keeptwotime)
555 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
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)
566 didntvote -= mapvote_selections[i];
570 result = strcat(result, ":", mapvote_maps[i]);
571 result = strcat(result, ":", ftos(mapvote_selections[i]));
572 if(i < mapvote_count_real)
574 mapvote_maps_flags[i] &= ~GTV_AVAILABLE;
578 result = strcat(result, ":didn't vote:", ftos(didntvote));
579 if(autocvar_sv_eventlog)
589 MapVote_CheckRules_1(); // count
590 if(MapVote_CheckRules_2()) // decide
594 FOREACH_CLIENT(true, {
595 if(!IS_REAL_CLIENT(it))
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);
602 // hide scoreboard again
603 if(GetResource(it, RES_HEALTH) != 2342)
605 SetResourceExplicit(it, RES_HEALTH, 2342); // health in the voting phase
609 WriteByte(MSG_ONE, SVC_FINALE);
610 WriteString(MSG_ONE, "");
613 // clear possibly invalid votes
614 if ( !(mapvote_maps_flags[it.mapvote-1] & GTV_AVAILABLE) )
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 )
620 it.mapvote = CS(it).impulse;
621 MapVote_TouchVotes(it);
629 MapVote_CheckRules_1(); // just count
634 // if mapvote is already running, don't do this initialization again
635 if(mapvote_run) { return; }
637 // don't start mapvote until after playerstats gamereport is sent
638 if(PlayerStats_GameReport_DelayMapVote) { return; }
641 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
650 if(alreadychangedlevel)
653 if(time < mapvote_nextthink)
657 mapvote_nextthink = time + 0.5;
659 if(!mapvote_initialized)
661 if(autocvar_rescan_pending == 1)
663 cvar_set("rescan_pending", "2");
664 localcmd("fs_rescan\nrescan_pending 3\n");
667 else if(autocvar_rescan_pending == 2)
671 else if(autocvar_rescan_pending == 3)
673 // now build missing mapinfo files
674 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
677 // we're done, start the timer
678 cvar_set("rescan_pending", "0");
681 mapvote_initialized = true;
682 if(DoNextMapOverride(0))
684 if(!autocvar_g_maplist_votable || player_count <= 0)
690 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
691 else if(autocvar_nextmap == "") { MapVote_Init(); }
697 bool GameTypeVote_SetGametype(Gametype type)
699 if (MapInfo_CurrentGametype() == type)
702 Gametype tsave = MapInfo_CurrentGametype();
704 MapInfo_SwitchGameType(type);
707 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
708 if(MapInfo_count > 0)
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");
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);
723 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
725 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
726 if(autocvar_g_maplist_shuffle)
732 bool gametypevote_finished;
733 bool GameTypeVote_Finished(int pos)
735 if(!gametypevote || gametypevote_finished)
738 localcmd("sv_vote_gametype_hook_all\n");
739 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
741 if ( !GameTypeVote_SetGametype(GameTypeVote_Type_FromString(mapvote_maps[pos])) )
743 LOG_TRACE("Selected gametype is not supported by any map");
746 gametypevote_finished = true;
751 bool GameTypeVote_AddVotable(string nextMode)
753 if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == NULL )
756 for(int j = 0; j < mapvote_count; ++j)
757 if(mapvote_maps[j] == nextMode)
760 mapvote_maps[mapvote_count] = strzone(nextMode);
761 mapvote_maps_suggested[mapvote_count] = false;
763 mapvote_maps_screenshot_dir[mapvote_count] = 0;
764 mapvote_maps_pakfile[mapvote_count] = strzone("");
765 mapvote_maps_flags[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
773 bool GameTypeVote_Start()
775 MapVote_ClearAllVotes();
776 MapVote_UnzoneStrings();
779 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
780 mapvote_abstain = false;
781 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
783 int n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
784 n = min(MAPVOTE_COUNT, n);
786 int really_available, which_available;
787 really_available = 0;
788 which_available = -1;
789 for(int j = 0; j < n; ++j)
791 if ( GameTypeVote_AddVotable(argv(j)) )
792 if ( mapvote_maps_flags[j] & GTV_AVAILABLE )
799 mapvote_count_real = mapvote_count;
803 if ( really_available == 0 )
805 if ( mapvote_count > 0 )
806 strunzone(mapvote_maps[0]);
807 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
808 //GameTypeVote_Finished(0);
812 if ( really_available == 1 )
814 //GameTypeVote_Finished(which_available);
815 MapVote_Finished(which_available);
819 mapvote_count_real = mapvote_count;
821 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
822 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
823 mapvote_keeptwotime = 0;