1 #include "mapvoting.qh"
3 #include <server/defs.qh>
4 #include <server/gamelog.qh>
5 #include <server/miscfunctions.qh>
7 #include "command/cmd.qh"
8 #include "command/getreplies.qh"
9 #include "../common/constants.qh"
10 #include <common/net_linked.qh>
11 #include "../common/mapinfo.qh"
12 #include "../common/playerstats.qh"
13 #include <common/state.qh>
14 #include "../common/util.qh"
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;
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;
35 int mapvote_selections[MAPVOTE_COUNT];
36 int mapvote_maps_flags[MAPVOTE_COUNT];
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
48 Gametype GameTypeVote_Type_FromString(string type_name)
50 Gametype type = MapInfo_Type_FromString(type_name);
52 type = MapInfo_Type_FromString(cvar_string(
53 strcat("sv_vote_gametype_",type_name,"_type")));
57 int GameTypeVote_AvailabilityStatus(string type_name)
59 int flag = GTV_FORBIDDEN;
61 Gametype type = MapInfo_Type_FromString(type_name);
64 type = MapInfo_Type_FromString(cvar_string(
65 strcat("sv_vote_gametype_",type_name,"_type")));
72 if ( autocvar_nextmap != "" )
74 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, NULL) )
76 if (!(MapInfo_Map_supportedGametypes & type.m_flags))
80 return flag | GTV_AVAILABLE;
83 int GameTypeVote_GetMask()
85 int n, j, gametype_mask;
86 n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
87 n = min(MAPVOTE_COUNT, n);
89 for(j = 0; j < n; ++j)
90 gametype_mask |= GameTypeVote_Type_FromString(argv(j)).m_flags;
94 string GameTypeVote_MapInfo_FixName(string m)
96 if ( autocvar_sv_vote_gametype )
99 _MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
101 return MapInfo_FixName(m);
104 void MapVote_ClearAllVotes()
106 FOREACH_CLIENT(true, { it.mapvote = 0; });
109 void MapVote_UnzoneStrings()
111 for(int j = 0; j < mapvote_count; ++j)
113 strfree(mapvote_maps[j]);
114 strfree(mapvote_maps_pakfile[j]);
118 string MapVote_Suggest(entity this, string m)
122 return "That's not how to use this command.";
123 if(!autocvar_g_maplist_votable_suggestions)
124 return "Suggestions are not accepted on this server.";
125 if(mapvote_initialized)
127 return "Can't suggest - voting is already in progress!";
128 m = GameTypeVote_MapInfo_FixName(m);
130 return "The map you suggested is not available on this server.";
131 if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
133 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
135 if (!autocvar_sv_vote_gametype)
136 if(!MapInfo_CheckMap(m))
137 return "The map you suggested does not support the current game mode.";
138 for(i = 0; i < mapvote_suggestion_ptr; ++i)
139 if(mapvote_suggestions[i] == m)
140 return "This map was already suggested.";
141 if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
143 i = floor(random() * mapvote_suggestion_ptr);
147 i = mapvote_suggestion_ptr;
148 mapvote_suggestion_ptr += 1;
150 if(mapvote_suggestions[i] != "")
151 strunzone(mapvote_suggestions[i]);
152 mapvote_suggestions[i] = strzone(m);
153 if(autocvar_sv_eventlog)
154 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(this.playerid)));
155 return strcat("Suggestion of ", m, " accepted.");
158 void MapVote_AddVotable(string nextMap, bool isSuggestion)
161 string pakfile, mapfile;
165 for(j = 0; j < mapvote_count; ++j)
166 if(mapvote_maps[j] == nextMap)
168 // suggestions might be no longer valid/allowed after gametype switch!
170 if(!MapInfo_CheckMap(nextMap))
172 mapvote_maps[mapvote_count] = strzone(nextMap);
173 mapvote_maps_suggested[mapvote_count] = isSuggestion;
175 pakfile = string_null;
176 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
178 mapfile = strcat(mapvote_screenshot_dirs[i], "/", nextMap);
179 pakfile = whichpack(strcat(mapfile, ".tga"));
181 pakfile = whichpack(strcat(mapfile, ".jpg"));
183 pakfile = whichpack(strcat(mapfile, ".png"));
187 if(i >= mapvote_screenshot_dirs_count)
188 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?
189 for(o = strstrofs(pakfile, "/", 0)+1; o > 0; o = strstrofs(pakfile, "/", 0)+1)
190 pakfile = substring(pakfile, o, -1);
192 mapvote_maps_screenshot_dir[mapvote_count] = i;
193 mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
194 mapvote_maps_flags[mapvote_count] = GTV_AVAILABLE;
204 MapVote_ClearAllVotes();
205 MapVote_UnzoneStrings();
208 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
209 mapvote_abstain = boolean(autocvar_g_maplist_votable_abstain);
212 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
214 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
215 smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
217 // we need this for AddVotable, as that cycles through the screenshot dirs
218 mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
219 if(mapvote_screenshot_dirs_count == 0)
220 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
221 mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
222 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
223 mapvote_screenshot_dirs[i] = strzone(argv(i));
225 if(mapvote_suggestion_ptr)
226 for(i = 0; i < 100 && mapvote_count < smax; ++i)
227 MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], true);
229 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
230 MapVote_AddVotable(GetNextMap(), false);
232 if(mapvote_count == 0)
234 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
235 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
236 if(autocvar_g_maplist_shuffle)
238 localcmd("\nmenu_cmd sync\n");
239 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
240 MapVote_AddVotable(GetNextMap(), false);
243 mapvote_count_real = mapvote_count;
245 MapVote_AddVotable("don't care", false);
247 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
249 mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
250 mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
251 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
252 mapvote_keeptwotime = 0;
257 void MapVote_SendPicture(entity to, int id)
260 WriteHeader(MSG_ONE, TE_CSQC_PICTURE);
261 WriteByte(MSG_ONE, id);
262 WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
266 void MapVote_WriteMask()
268 if ( mapvote_count < 24 )
271 for(int j = 0; j < mapvote_count; ++j)
273 if(mapvote_maps_flags[j] & GTV_AVAILABLE)
277 if(mapvote_count < 8)
278 WriteByte(MSG_ENTITY, mask);
279 else if (mapvote_count < 16)
280 WriteShort(MSG_ENTITY,mask);
282 WriteLong(MSG_ENTITY, mask);
286 for (int j = 0; j < mapvote_count; ++j)
287 WriteByte(MSG_ENTITY, mapvote_maps_flags[j]);
292 * Sends a single map vote option to the client
294 void MapVote_SendOption(int i)
297 if(mapvote_abstain && i == mapvote_count - 1)
299 WriteString(MSG_ENTITY, ""); // abstain needs no text
300 WriteString(MSG_ENTITY, ""); // abstain needs no pack
301 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
305 WriteString(MSG_ENTITY, mapvote_maps[i]);
306 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
307 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
312 * Sends a single gametype vote option to the client
314 void GameTypeVote_SendOption(int i)
317 if(mapvote_abstain && i == mapvote_count - 1)
319 WriteString(MSG_ENTITY, ""); // abstain needs no text
320 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
324 string type_name = mapvote_maps[i];
325 WriteString(MSG_ENTITY, type_name);
326 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
327 if ( mapvote_maps_flags[i] & GTV_CUSTOM )
329 WriteString(MSG_ENTITY, cvar_string(
330 strcat("sv_vote_gametype_",type_name,"_name")));
331 WriteString(MSG_ENTITY, cvar_string(
332 strcat("sv_vote_gametype_",type_name,"_description")));
333 WriteString(MSG_ENTITY, cvar_string(
334 strcat("sv_vote_gametype_",type_name,"_type")));
339 bool MapVote_SendEntity(entity this, entity to, int sf)
344 sf &= ~2; // if we send 1, we don't need to also send 2
346 WriteHeader(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
347 WriteByte(MSG_ENTITY, sf);
351 // flag 1 == initialization
352 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
353 WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
354 WriteString(MSG_ENTITY, "");
355 WriteByte(MSG_ENTITY, mapvote_count);
356 WriteByte(MSG_ENTITY, mapvote_abstain);
357 WriteByte(MSG_ENTITY, mapvote_detail);
358 WriteCoord(MSG_ENTITY, mapvote_timeout);
363 WriteByte(MSG_ENTITY, 1);
364 WriteString(MSG_ENTITY, autocvar_nextmap);
366 else if ( autocvar_sv_vote_gametype )
368 // map vote but gametype has been chosen via voting screen
369 WriteByte(MSG_ENTITY, 2);
370 WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
373 WriteByte(MSG_ENTITY, 0); // map vote
377 // Send data for the vote options
378 for(i = 0; i < mapvote_count; ++i)
381 GameTypeVote_SendOption(i);
383 MapVote_SendOption(i);
389 // flag 2 == update of mask
396 for(i = 0; i < mapvote_count; ++i)
397 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
398 WriteByte(MSG_ENTITY, mapvote_selections[i]);
400 WriteByte(MSG_ENTITY, to.mapvote);
408 Net_LinkEntity(mapvote_ent = spawn(), false, 0, MapVote_SendEntity);
411 void MapVote_TouchMask()
413 mapvote_ent.SendFlags |= 2;
416 void MapVote_TouchVotes(entity voter)
418 mapvote_ent.SendFlags |= 4;
421 bool MapVote_Finished(int mappos)
423 if(alreadychangedlevel)
430 if(autocvar_sv_eventlog)
432 result = strcat(":vote:finished:", mapvote_maps[mappos]);
433 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
434 didntvote = mapvote_voters;
435 for(i = 0; i < mapvote_count; ++i)
436 if(mapvote_maps_flags[i] & GTV_AVAILABLE )
438 didntvote -= mapvote_selections[i];
441 result = strcat(result, ":", mapvote_maps[i]);
442 result = strcat(result, ":", ftos(mapvote_selections[i]));
445 result = strcat(result, ":didn't vote:", ftos(didntvote));
448 if(mapvote_maps_suggested[mappos])
449 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
452 FOREACH_CLIENT(IS_REAL_CLIENT(it), { FixClientCvars(it); });
456 if ( GameTypeVote_Finished(mappos) )
458 gametypevote = false;
459 if(autocvar_nextmap != "")
461 Map_Goto_SetStr(autocvar_nextmap);
463 alreadychangedlevel = true;
472 Map_Goto_SetStr(mapvote_maps[mappos]);
474 alreadychangedlevel = true;
479 void MapVote_CheckRules_1()
481 for (int i = 0; i < mapvote_count; ++i)
482 if (mapvote_maps_flags[i] & GTV_AVAILABLE)
484 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
485 mapvote_selections[i] = 0;
489 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
493 int idx = it.mapvote - 1;
494 //dprint("Player ", it.netname, " vote = ", ftos(idx), "\n");
495 ++mapvote_selections[idx];
500 bool MapVote_CheckRules_2()
503 int firstPlace, secondPlace, currentPlace;
504 int firstPlaceVotes, secondPlaceVotes, currentVotes;
505 int mapvote_voters_real;
508 if(mapvote_count_real == 1)
509 return MapVote_Finished(0);
511 mapvote_voters_real = mapvote_voters;
513 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
515 RandomSelection_Init();
518 for(i = 0; i < mapvote_count_real; ++i)
519 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
521 RandomSelection_AddFloat(i, 1, mapvote_selections[i]);
522 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
524 currentVotes = mapvote_selections[i];
528 firstPlaceVotes = RandomSelection_best_priority;
529 if ( autocvar_sv_vote_gametype_default_current && firstPlaceVotes == 0 )
530 firstPlace = currentPlace;
532 firstPlace = RandomSelection_chosen_float;
534 //dprint("First place: ", ftos(firstPlace), "\n");
535 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
537 RandomSelection_Init();
538 for(i = 0; i < mapvote_count_real; ++i)
540 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
541 RandomSelection_AddFloat(i, 1, mapvote_selections[i]);
542 secondPlace = RandomSelection_chosen_float;
543 secondPlaceVotes = RandomSelection_best_priority;
544 //dprint("Second place: ", ftos(secondPlace), "\n");
545 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
548 error("No first place in map vote... WTF?");
550 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
551 return MapVote_Finished(firstPlace);
553 if(mapvote_keeptwotime)
554 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
557 mapvote_keeptwotime = 0;
558 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
559 result = strcat(result, ":", ftos(firstPlaceVotes));
560 result = strcat(result, ":", mapvote_maps[secondPlace]);
561 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
562 int didntvote = mapvote_voters;
563 for(i = 0; i < mapvote_count; ++i)
565 didntvote -= mapvote_selections[i];
569 result = strcat(result, ":", mapvote_maps[i]);
570 result = strcat(result, ":", ftos(mapvote_selections[i]));
571 if(i < mapvote_count_real)
573 mapvote_maps_flags[i] &= ~GTV_AVAILABLE;
577 result = strcat(result, ":didn't vote:", ftos(didntvote));
578 if(autocvar_sv_eventlog)
588 MapVote_CheckRules_1(); // count
589 if(MapVote_CheckRules_2()) // decide
593 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
594 // hide scoreboard again
595 if(GetResource(it, RES_HEALTH) != 2342)
597 SetResourceExplicit(it, RES_HEALTH, 2342);
601 WriteByte(MSG_ONE, SVC_FINALE);
602 WriteString(MSG_ONE, "");
605 // clear possibly invalid votes
606 if ( !(mapvote_maps_flags[it.mapvote-1] & GTV_AVAILABLE) )
608 // use impulses as new vote
609 if(CS(it).impulse >= 1 && CS(it).impulse <= mapvote_count)
610 if( mapvote_maps_flags[CS(it).impulse - 1] & GTV_AVAILABLE )
612 it.mapvote = CS(it).impulse;
613 MapVote_TouchVotes(it);
621 MapVote_CheckRules_1(); // just count
626 // if mapvote is already running, don't do this initialization again
627 if(mapvote_run) { return; }
629 // don't start mapvote until after playerstats gamereport is sent
630 if(PlayerStats_GameReport_DelayMapVote) { return; }
633 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
642 if(alreadychangedlevel)
645 if(time < mapvote_nextthink)
649 mapvote_nextthink = time + 0.5;
651 if(!mapvote_initialized)
653 if(autocvar_rescan_pending == 1)
655 cvar_set("rescan_pending", "2");
656 localcmd("fs_rescan\nrescan_pending 3\n");
659 else if(autocvar_rescan_pending == 2)
663 else if(autocvar_rescan_pending == 3)
665 // now build missing mapinfo files
666 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
669 // we're done, start the timer
670 cvar_set("rescan_pending", "0");
673 mapvote_initialized = true;
674 if(DoNextMapOverride(0))
676 if(!autocvar_g_maplist_votable || player_count <= 0)
682 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
683 else if(autocvar_nextmap == "") { MapVote_Init(); }
689 bool GameTypeVote_SetGametype(Gametype type)
691 if (MapInfo_CurrentGametype() == type)
694 Gametype tsave = MapInfo_CurrentGametype();
696 MapInfo_SwitchGameType(type);
699 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
700 if(MapInfo_count > 0)
702 // update lsmaps in case the gametype changed, this way people can easily list maps for it
703 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
704 lsmaps_reply = strzone(getlsmaps());
705 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
709 bprint("Cannot use this game type: no map for it found\n");
710 MapInfo_SwitchGameType(tsave);
711 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
715 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
717 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
718 if(autocvar_g_maplist_shuffle)
724 bool gametypevote_finished;
725 bool GameTypeVote_Finished(int pos)
727 if(!gametypevote || gametypevote_finished)
730 localcmd("sv_vote_gametype_hook_all\n");
731 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
733 if ( !GameTypeVote_SetGametype(GameTypeVote_Type_FromString(mapvote_maps[pos])) )
735 LOG_TRACE("Selected gametype is not supported by any map");
738 gametypevote_finished = true;
743 bool GameTypeVote_AddVotable(string nextMode)
745 if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == NULL )
748 for(int j = 0; j < mapvote_count; ++j)
749 if(mapvote_maps[j] == nextMode)
752 mapvote_maps[mapvote_count] = strzone(nextMode);
753 mapvote_maps_suggested[mapvote_count] = false;
755 mapvote_maps_screenshot_dir[mapvote_count] = 0;
756 mapvote_maps_pakfile[mapvote_count] = strzone("");
757 mapvote_maps_flags[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
765 bool GameTypeVote_Start()
767 MapVote_ClearAllVotes();
768 MapVote_UnzoneStrings();
771 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
772 mapvote_abstain = false;
773 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
775 int n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
776 n = min(MAPVOTE_COUNT, n);
778 int really_available, which_available;
779 really_available = 0;
780 which_available = -1;
781 for(int j = 0; j < n; ++j)
783 if ( GameTypeVote_AddVotable(argv(j)) )
784 if ( mapvote_maps_flags[j] & GTV_AVAILABLE )
791 mapvote_count_real = mapvote_count;
795 if ( really_available == 0 )
797 if ( mapvote_count > 0 )
798 strunzone(mapvote_maps[0]);
799 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
800 //GameTypeVote_Finished(0);
804 if ( really_available == 1 )
806 //GameTypeVote_Finished(which_available);
807 MapVote_Finished(which_available);
811 mapvote_count_real = mapvote_count;
813 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
814 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
815 mapvote_keeptwotime = 0;