1 #include "mapvoting.qh"
3 #include <server/client.qh>
4 #include <common/weapons/_all.qh>
5 #include <common/stats.qh>
6 #include <server/gamelog.qh>
7 #include <server/miscfunctions.qh>
9 #include "command/cmd.qh"
10 #include "command/getreplies.qh"
11 #include "../common/constants.qh"
12 #include <common/net_linked.qh>
13 #include "../common/mapinfo.qh"
14 #include "../common/playerstats.qh"
15 #include <common/state.qh>
16 #include "../common/util.qh"
21 float mapvote_nextthink;
22 float mapvote_keeptwotime;
23 float mapvote_timeout;
24 const int MAPVOTE_SCREENSHOT_DIRS_COUNT = 4;
25 string mapvote_screenshot_dirs[MAPVOTE_SCREENSHOT_DIRS_COUNT];
26 int mapvote_screenshot_dirs_count;
29 int mapvote_count_real;
30 string mapvote_maps[MAPVOTE_COUNT];
31 int mapvote_maps_screenshot_dir[MAPVOTE_COUNT];
32 string mapvote_maps_pakfile[MAPVOTE_COUNT];
33 bool mapvote_maps_suggested[MAPVOTE_COUNT];
34 string mapvote_suggestions[MAPVOTE_COUNT];
35 int mapvote_suggestion_ptr;
37 int mapvote_selections[MAPVOTE_COUNT];
38 int mapvote_maps_flags[MAPVOTE_COUNT];
47 * Returns the gamtype ID from its name, if type_name isn't a real gametype it
48 * checks for sv_vote_gametype_(type_name)_type
50 Gametype GameTypeVote_Type_FromString(string type_name)
52 Gametype type = MapInfo_Type_FromString(type_name, false);
54 type = MapInfo_Type_FromString(cvar_string(
55 strcat("sv_vote_gametype_",type_name,"_type")), false);
59 int GameTypeVote_AvailabilityStatus(string type_name)
61 int flag = GTV_FORBIDDEN;
63 Gametype type = MapInfo_Type_FromString(type_name, false);
66 type = MapInfo_Type_FromString(cvar_string(
67 strcat("sv_vote_gametype_",type_name,"_type")), false);
74 if ( autocvar_nextmap != "" )
76 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, NULL) )
78 if (!(MapInfo_Map_supportedGametypes & type.m_flags))
82 return flag | GTV_AVAILABLE;
85 int GameTypeVote_GetMask()
87 int n, j, gametype_mask;
88 n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
89 n = min(MAPVOTE_COUNT, n);
91 for(j = 0; j < n; ++j)
92 gametype_mask |= GameTypeVote_Type_FromString(argv(j)).m_flags;
96 string GameTypeVote_MapInfo_FixName(string m)
98 if ( autocvar_sv_vote_gametype )
101 _MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
103 return MapInfo_FixName(m);
106 void MapVote_ClearAllVotes()
108 FOREACH_CLIENT(true, { it.mapvote = 0; });
111 void MapVote_UnzoneStrings()
113 for(int j = 0; j < mapvote_count; ++j)
115 strfree(mapvote_maps[j]);
116 strfree(mapvote_maps_pakfile[j]);
120 string MapVote_Suggest(entity this, string m)
124 return "That's not how to use this command.";
125 if(!autocvar_g_maplist_votable_suggestions)
126 return "Suggestions are not accepted on this server.";
127 if(mapvote_initialized)
129 return "Can't suggest - voting is already in progress!";
130 m = GameTypeVote_MapInfo_FixName(m);
132 return "The map you suggested is not available on this server.";
133 if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
135 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
137 if (!autocvar_sv_vote_gametype)
138 if(!MapInfo_CheckMap(m))
139 return "The map you suggested does not support the current game mode.";
140 for(i = 0; i < mapvote_suggestion_ptr; ++i)
141 if(mapvote_suggestions[i] == m)
142 return "This map was already suggested.";
143 if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
145 i = floor(random() * mapvote_suggestion_ptr);
149 i = mapvote_suggestion_ptr;
150 mapvote_suggestion_ptr += 1;
152 if(mapvote_suggestions[i] != "")
153 strunzone(mapvote_suggestions[i]);
154 mapvote_suggestions[i] = strzone(m);
155 if(autocvar_sv_eventlog)
156 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(this.playerid)));
157 return strcat("Suggestion of ", m, " accepted.");
160 void MapVote_AddVotable(string nextMap, bool isSuggestion)
163 string pakfile, mapfile;
167 for(j = 0; j < mapvote_count; ++j)
168 if(mapvote_maps[j] == nextMap)
170 // suggestions might be no longer valid/allowed after gametype switch!
172 if(!MapInfo_CheckMap(nextMap))
174 mapvote_maps[mapvote_count] = strzone(nextMap);
175 mapvote_maps_suggested[mapvote_count] = isSuggestion;
177 pakfile = string_null;
178 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
180 mapfile = strcat(mapvote_screenshot_dirs[i], "/", nextMap);
181 pakfile = whichpack(strcat(mapfile, ".tga"));
183 pakfile = whichpack(strcat(mapfile, ".jpg"));
185 pakfile = whichpack(strcat(mapfile, ".png"));
189 if(i >= mapvote_screenshot_dirs_count)
190 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?
191 for(o = strstrofs(pakfile, "/", 0)+1; o > 0; o = strstrofs(pakfile, "/", 0)+1)
192 pakfile = substring(pakfile, o, -1);
194 mapvote_maps_screenshot_dir[mapvote_count] = i;
195 mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
196 mapvote_maps_flags[mapvote_count] = GTV_AVAILABLE;
206 MapVote_ClearAllVotes();
207 MapVote_UnzoneStrings();
210 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
211 mapvote_abstain = boolean(autocvar_g_maplist_votable_abstain);
214 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
216 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
217 smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
219 // we need this for AddVotable, as that cycles through the screenshot dirs
220 mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
221 if(mapvote_screenshot_dirs_count == 0)
222 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
223 mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
224 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
225 mapvote_screenshot_dirs[i] = strzone(argv(i));
227 if(mapvote_suggestion_ptr)
228 for(i = 0; i < 100 && mapvote_count < smax; ++i)
229 MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], true);
231 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
232 MapVote_AddVotable(GetNextMap(), false);
234 if(mapvote_count == 0)
236 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
237 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
238 if(autocvar_g_maplist_shuffle)
240 localcmd("\nmenu_cmd sync\n");
241 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
242 MapVote_AddVotable(GetNextMap(), false);
245 mapvote_count_real = mapvote_count;
247 MapVote_AddVotable("don't care", false);
249 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
251 mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
252 mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
253 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
254 mapvote_keeptwotime = 0;
259 void MapVote_SendPicture(entity to, int id)
262 WriteHeader(MSG_ONE, TE_CSQC_PICTURE);
263 WriteByte(MSG_ONE, id);
264 WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
268 void MapVote_WriteMask()
270 if ( mapvote_count < 24 )
273 for(int j = 0; j < mapvote_count; ++j)
275 if(mapvote_maps_flags[j] & GTV_AVAILABLE)
279 if(mapvote_count < 8)
280 WriteByte(MSG_ENTITY, mask);
281 else if (mapvote_count < 16)
282 WriteShort(MSG_ENTITY,mask);
284 WriteLong(MSG_ENTITY, mask);
288 for (int j = 0; j < mapvote_count; ++j)
289 WriteByte(MSG_ENTITY, mapvote_maps_flags[j]);
294 * Sends a single map vote option to the client
296 void MapVote_SendOption(int i)
299 if(mapvote_abstain && i == mapvote_count - 1)
301 WriteString(MSG_ENTITY, ""); // abstain needs no text
302 WriteString(MSG_ENTITY, ""); // abstain needs no pack
303 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
307 WriteString(MSG_ENTITY, mapvote_maps[i]);
308 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
309 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
314 * Sends a single gametype vote option to the client
316 void GameTypeVote_SendOption(int i)
319 if(mapvote_abstain && i == mapvote_count - 1)
321 WriteString(MSG_ENTITY, ""); // abstain needs no text
322 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
326 string type_name = mapvote_maps[i];
327 WriteString(MSG_ENTITY, type_name);
328 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
329 if ( mapvote_maps_flags[i] & GTV_CUSTOM )
331 WriteString(MSG_ENTITY, cvar_string(
332 strcat("sv_vote_gametype_",type_name,"_name")));
333 WriteString(MSG_ENTITY, cvar_string(
334 strcat("sv_vote_gametype_",type_name,"_description")));
335 WriteString(MSG_ENTITY, cvar_string(
336 strcat("sv_vote_gametype_",type_name,"_type")));
341 bool MapVote_SendEntity(entity this, entity to, int sf)
346 sf &= ~2; // if we send 1, we don't need to also send 2
348 WriteHeader(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
349 WriteByte(MSG_ENTITY, sf);
353 // flag 1 == initialization
354 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
355 WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
356 WriteString(MSG_ENTITY, "");
357 WriteByte(MSG_ENTITY, mapvote_count);
358 WriteByte(MSG_ENTITY, mapvote_abstain);
359 WriteByte(MSG_ENTITY, mapvote_detail);
360 WriteCoord(MSG_ENTITY, mapvote_timeout);
365 WriteByte(MSG_ENTITY, 1);
366 WriteString(MSG_ENTITY, autocvar_nextmap);
368 else if ( autocvar_sv_vote_gametype )
370 // map vote but gametype has been chosen via voting screen
371 WriteByte(MSG_ENTITY, 2);
372 WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
375 WriteByte(MSG_ENTITY, 0); // map vote
379 // Send data for the vote options
380 for(i = 0; i < mapvote_count; ++i)
383 GameTypeVote_SendOption(i);
385 MapVote_SendOption(i);
391 // flag 2 == update of mask
398 for(i = 0; i < mapvote_count; ++i)
399 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
400 WriteByte(MSG_ENTITY, mapvote_selections[i]);
402 WriteByte(MSG_ENTITY, to.mapvote);
410 Net_LinkEntity(mapvote_ent = spawn(), false, 0, MapVote_SendEntity);
413 void MapVote_TouchMask()
415 mapvote_ent.SendFlags |= 2;
418 void MapVote_TouchVotes(entity voter)
420 mapvote_ent.SendFlags |= 4;
423 bool MapVote_Finished(int mappos)
425 if(alreadychangedlevel)
432 if(autocvar_sv_eventlog)
434 result = strcat(":vote:finished:", mapvote_maps[mappos]);
435 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
436 didntvote = mapvote_voters;
437 for(i = 0; i < mapvote_count; ++i)
438 if(mapvote_maps_flags[i] & GTV_AVAILABLE )
440 didntvote -= mapvote_selections[i];
443 result = strcat(result, ":", mapvote_maps[i]);
444 result = strcat(result, ":", ftos(mapvote_selections[i]));
447 result = strcat(result, ":didn't vote:", ftos(didntvote));
450 if(mapvote_maps_suggested[mappos])
451 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
454 FOREACH_CLIENT(IS_REAL_CLIENT(it), { FixClientCvars(it); });
458 if ( GameTypeVote_Finished(mappos) )
460 gametypevote = false;
461 if(autocvar_nextmap != "")
463 Map_Goto_SetStr(autocvar_nextmap);
465 alreadychangedlevel = true;
474 Map_Goto_SetStr(mapvote_maps[mappos]);
476 alreadychangedlevel = true;
481 void MapVote_CheckRules_1()
483 for (int i = 0; i < mapvote_count; ++i)
484 if (mapvote_maps_flags[i] & GTV_AVAILABLE)
486 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
487 mapvote_selections[i] = 0;
491 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
495 int idx = it.mapvote - 1;
496 //dprint("Player ", it.netname, " vote = ", ftos(idx), "\n");
497 ++mapvote_selections[idx];
502 bool MapVote_CheckRules_2()
505 int firstPlace, secondPlace, currentPlace;
506 int firstPlaceVotes, secondPlaceVotes, currentVotes;
507 int mapvote_voters_real;
510 if(mapvote_count_real == 1)
511 return MapVote_Finished(0);
513 mapvote_voters_real = mapvote_voters;
515 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
517 RandomSelection_Init();
520 for(i = 0; i < mapvote_count_real; ++i)
521 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
523 RandomSelection_AddFloat(i, 1, mapvote_selections[i]);
524 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
526 currentVotes = mapvote_selections[i];
530 firstPlaceVotes = RandomSelection_best_priority;
531 if ( autocvar_sv_vote_gametype_default_current && firstPlaceVotes == 0 )
532 firstPlace = currentPlace;
534 firstPlace = RandomSelection_chosen_float;
536 //dprint("First place: ", ftos(firstPlace), "\n");
537 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
539 RandomSelection_Init();
540 for(i = 0; i < mapvote_count_real; ++i)
542 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
543 RandomSelection_AddFloat(i, 1, mapvote_selections[i]);
544 secondPlace = RandomSelection_chosen_float;
545 secondPlaceVotes = RandomSelection_best_priority;
546 //dprint("Second place: ", ftos(secondPlace), "\n");
547 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
550 error("No first place in map vote... WTF?");
552 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
553 return MapVote_Finished(firstPlace);
555 if(mapvote_keeptwotime)
556 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
559 mapvote_keeptwotime = 0;
560 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
561 result = strcat(result, ":", ftos(firstPlaceVotes));
562 result = strcat(result, ":", mapvote_maps[secondPlace]);
563 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
564 int didntvote = mapvote_voters;
565 for(i = 0; i < mapvote_count; ++i)
567 didntvote -= mapvote_selections[i];
571 result = strcat(result, ":", mapvote_maps[i]);
572 result = strcat(result, ":", ftos(mapvote_selections[i]));
573 if(i < mapvote_count_real)
575 mapvote_maps_flags[i] &= ~GTV_AVAILABLE;
579 result = strcat(result, ":didn't vote:", ftos(didntvote));
580 if(autocvar_sv_eventlog)
590 MapVote_CheckRules_1(); // count
591 if(MapVote_CheckRules_2()) // decide
595 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
596 // hide scoreboard again
597 if(GetResource(it, RES_HEALTH) != 2342)
599 SetResourceExplicit(it, RES_HEALTH, 2342);
603 WriteByte(MSG_ONE, SVC_FINALE);
604 WriteString(MSG_ONE, "");
607 // clear possibly invalid votes
608 if ( !(mapvote_maps_flags[it.mapvote-1] & GTV_AVAILABLE) )
610 // use impulses as new vote
611 if(CS(it).impulse >= 1 && CS(it).impulse <= mapvote_count)
612 if( mapvote_maps_flags[CS(it).impulse - 1] & GTV_AVAILABLE )
614 it.mapvote = CS(it).impulse;
615 MapVote_TouchVotes(it);
623 MapVote_CheckRules_1(); // just count
628 // if mapvote is already running, don't do this initialization again
629 if(mapvote_run) { return; }
631 // don't start mapvote until after playerstats gamereport is sent
632 if(PlayerStats_GameReport_DelayMapVote) { return; }
635 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
644 if(alreadychangedlevel)
647 if(time < mapvote_nextthink)
651 mapvote_nextthink = time + 0.5;
653 if(!mapvote_initialized)
655 if(autocvar_rescan_pending == 1)
657 cvar_set("rescan_pending", "2");
658 localcmd("fs_rescan\nrescan_pending 3\n");
661 else if(autocvar_rescan_pending == 2)
665 else if(autocvar_rescan_pending == 3)
667 // now build missing mapinfo files
668 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
671 // we're done, start the timer
672 cvar_set("rescan_pending", "0");
675 mapvote_initialized = true;
676 if(DoNextMapOverride(0))
678 if(!autocvar_g_maplist_votable || player_count <= 0)
684 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
685 else if(autocvar_nextmap == "") { MapVote_Init(); }
691 bool GameTypeVote_SetGametype(Gametype type)
693 if (MapInfo_CurrentGametype() == type)
696 Gametype tsave = MapInfo_CurrentGametype();
698 MapInfo_SwitchGameType(type);
701 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
702 if(MapInfo_count > 0)
704 // update lsmaps in case the gametype changed, this way people can easily list maps for it
705 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
706 lsmaps_reply = strzone(getlsmaps());
707 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
711 bprint("Cannot use this game type: no map for it found\n");
712 MapInfo_SwitchGameType(tsave);
713 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
717 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
719 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
720 if(autocvar_g_maplist_shuffle)
726 bool gametypevote_finished;
727 bool GameTypeVote_Finished(int pos)
729 if(!gametypevote || gametypevote_finished)
732 localcmd("sv_vote_gametype_hook_all\n");
733 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
735 if ( !GameTypeVote_SetGametype(GameTypeVote_Type_FromString(mapvote_maps[pos])) )
737 LOG_TRACE("Selected gametype is not supported by any map");
740 gametypevote_finished = true;
745 bool GameTypeVote_AddVotable(string nextMode)
747 if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == NULL )
750 for(int j = 0; j < mapvote_count; ++j)
751 if(mapvote_maps[j] == nextMode)
754 mapvote_maps[mapvote_count] = strzone(nextMode);
755 mapvote_maps_suggested[mapvote_count] = false;
757 mapvote_maps_screenshot_dir[mapvote_count] = 0;
758 mapvote_maps_pakfile[mapvote_count] = strzone("");
759 mapvote_maps_flags[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
767 bool GameTypeVote_Start()
769 MapVote_ClearAllVotes();
770 MapVote_UnzoneStrings();
773 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
774 mapvote_abstain = false;
775 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
777 int n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
778 n = min(MAPVOTE_COUNT, n);
780 int really_available, which_available;
781 really_available = 0;
782 which_available = -1;
783 for(int j = 0; j < n; ++j)
785 if ( GameTypeVote_AddVotable(argv(j)) )
786 if ( mapvote_maps_flags[j] & GTV_AVAILABLE )
793 mapvote_count_real = mapvote_count;
797 if ( really_available == 0 )
799 if ( mapvote_count > 0 )
800 strunzone(mapvote_maps[0]);
801 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
802 //GameTypeVote_Finished(0);
806 if ( really_available == 1 )
808 //GameTypeVote_Finished(which_available);
809 MapVote_Finished(which_available);
813 mapvote_count_real = mapvote_count;
815 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
816 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
817 mapvote_keeptwotime = 0;