1 #include "mapvoting.qh"
5 #include "command/cmd.qh"
6 #include "command/getreplies.qh"
7 #include "../common/constants.qh"
8 #include "../common/mapinfo.qh"
9 #include "../common/playerstats.qh"
10 #include "../common/util.qh"
15 float mapvote_nextthink;
16 float mapvote_keeptwotime;
17 float mapvote_timeout;
18 string mapvote_message;
19 const float MAPVOTE_SCREENSHOT_DIRS_COUNT = 4;
20 string mapvote_screenshot_dirs[MAPVOTE_SCREENSHOT_DIRS_COUNT];
21 float mapvote_screenshot_dirs_count;
24 float mapvote_count_real;
25 string mapvote_maps[MAPVOTE_COUNT];
26 float mapvote_maps_screenshot_dir[MAPVOTE_COUNT];
27 string mapvote_maps_pakfile[MAPVOTE_COUNT];
28 float mapvote_maps_suggested[MAPVOTE_COUNT];
29 string mapvote_suggestions[MAPVOTE_COUNT];
30 float mapvote_suggestion_ptr;
32 float mapvote_selections[MAPVOTE_COUNT];
33 float mapvote_maps_flags[MAPVOTE_COUNT];
36 float mapvote_abstain;
42 * Returns the gamtype ID from its name, if type_name isn't a real gametype it
43 * checks for sv_vote_gametype_(type_name)_type
45 float GameTypeVote_Type_FromString(string type_name)
47 float type = MapInfo_Type_FromString(type_name);
49 type = MapInfo_Type_FromString(cvar_string(
50 strcat("sv_vote_gametype_",type_name,"_type")));
54 int GameTypeVote_AvailabilityStatus(string type_name)
56 int flag = GTV_FORBIDDEN;
58 float type = MapInfo_Type_FromString(type_name);
61 type = MapInfo_Type_FromString(cvar_string(
62 strcat("sv_vote_gametype_",type_name,"_type")));
69 if ( autocvar_nextmap != "" )
71 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, 0) )
73 if (!(MapInfo_Map_supportedGametypes & type))
77 return flag | GTV_AVAILABLE;
80 float GameTypeVote_GetMask()
82 float n, j, gametype_mask;
83 n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
84 n = min(MAPVOTE_COUNT, n);
86 for(j = 0; j < n; ++j)
87 gametype_mask |= GameTypeVote_Type_FromString(argv(j));
91 string GameTypeVote_MapInfo_FixName(string m)
93 if ( autocvar_sv_vote_gametype )
96 MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
98 return MapInfo_FixName(m);
101 void MapVote_ClearAllVotes()
103 FOR_EACH_CLIENT(other)
107 void MapVote_UnzoneStrings()
110 for(j = 0; j < mapvote_count; ++j)
112 if ( mapvote_maps[j] )
114 strunzone(mapvote_maps[j]);
115 mapvote_maps[j] = string_null;
117 if ( mapvote_maps_pakfile[j] )
119 strunzone(mapvote_maps_pakfile[j]);
120 mapvote_maps_pakfile[j] = string_null;
125 string MapVote_Suggest(string m)
129 return "That's not how to use this command.";
130 if(!autocvar_g_maplist_votable_suggestions)
131 return "Suggestions are not accepted on this server.";
132 if(mapvote_initialized)
134 return "Can't suggest - voting is already in progress!";
135 m = GameTypeVote_MapInfo_FixName(m);
137 return "The map you suggested is not available on this server.";
138 if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
140 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
142 if (!autocvar_sv_vote_gametype)
143 if(!MapInfo_CheckMap(m))
144 return "The map you suggested does not support the current game mode.";
145 for(i = 0; i < mapvote_suggestion_ptr; ++i)
146 if(mapvote_suggestions[i] == m)
147 return "This map was already suggested.";
148 if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
150 i = floor(random() * mapvote_suggestion_ptr);
154 i = mapvote_suggestion_ptr;
155 mapvote_suggestion_ptr += 1;
157 if(mapvote_suggestions[i] != "")
158 strunzone(mapvote_suggestions[i]);
159 mapvote_suggestions[i] = strzone(m);
160 if(autocvar_sv_eventlog)
161 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid)));
162 return strcat("Suggestion of ", m, " accepted.");
165 void MapVote_AddVotable(string nextMap, float isSuggestion)
168 string pakfile, mapfile;
172 for(j = 0; j < mapvote_count; ++j)
173 if(mapvote_maps[j] == nextMap)
175 // suggestions might be no longer valid/allowed after gametype switch!
177 if(!MapInfo_CheckMap(nextMap))
179 mapvote_maps[mapvote_count] = strzone(nextMap);
180 mapvote_maps_suggested[mapvote_count] = isSuggestion;
182 pakfile = string_null;
183 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
185 mapfile = strcat(mapvote_screenshot_dirs[i], "/", mapvote_maps[i]);
186 pakfile = whichpack(strcat(mapfile, ".tga"));
188 pakfile = whichpack(strcat(mapfile, ".jpg"));
190 pakfile = whichpack(strcat(mapfile, ".png"));
194 if(i >= mapvote_screenshot_dirs_count)
195 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?
196 for(o = strstr(pakfile, "/", 0)+1; o > 0; o = strstr(pakfile, "/", 0)+1)
197 pakfile = substring(pakfile, o, -1);
199 mapvote_maps_screenshot_dir[mapvote_count] = i;
200 mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
201 mapvote_maps_flags[mapvote_count] = GTV_AVAILABLE;
211 MapVote_ClearAllVotes();
212 MapVote_UnzoneStrings();
215 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
216 mapvote_abstain = autocvar_g_maplist_votable_abstain;
219 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
221 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
222 smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
224 // we need this for AddVotable, as that cycles through the screenshot dirs
225 mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
226 if(mapvote_screenshot_dirs_count == 0)
227 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
228 mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
229 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
230 mapvote_screenshot_dirs[i] = strzone(argv(i));
232 if(mapvote_suggestion_ptr)
233 for(i = 0; i < 100 && mapvote_count < smax; ++i)
234 MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], true);
236 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
237 MapVote_AddVotable(GetNextMap(), false);
239 if(mapvote_count == 0)
241 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
242 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
243 if(autocvar_g_maplist_shuffle)
245 localcmd("\nmenu_cmd sync\n");
246 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
247 MapVote_AddVotable(GetNextMap(), false);
250 mapvote_count_real = mapvote_count;
252 MapVote_AddVotable("don't care", 0);
254 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
256 mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
257 mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
258 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
259 mapvote_keeptwotime = 0;
260 mapvote_message = "Choose a map and press its key!";
265 void MapVote_SendPicture(float id)
268 WriteByte(MSG_ONE, SVC_TEMPENTITY);
269 WriteByte(MSG_ONE, TE_CSQC_PICTURE);
270 WriteByte(MSG_ONE, id);
271 WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
275 void MapVote_WriteMask()
278 if ( mapvote_count < 24 )
282 for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
283 if(mapvote_maps_flags[i] & GTV_AVAILABLE )
286 if(mapvote_count < 8)
287 WriteByte(MSG_ENTITY, mask);
288 else if (mapvote_count < 16)
289 WriteShort(MSG_ENTITY,mask);
291 WriteLong(MSG_ENTITY, mask);
295 for ( i = 0; i < mapvote_count; ++i )
296 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
301 * Sends a single map vote option to the client
303 void MapVote_SendOption(int i)
306 if(mapvote_abstain && i == mapvote_count - 1)
308 WriteString(MSG_ENTITY, ""); // abstain needs no text
309 WriteString(MSG_ENTITY, ""); // abstain needs no pack
310 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
314 WriteString(MSG_ENTITY, mapvote_maps[i]);
315 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
316 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
321 * Sends a single gametype vote option to the client
323 void GameTypeVote_SendOption(int i)
326 if(mapvote_abstain && i == mapvote_count - 1)
328 WriteString(MSG_ENTITY, ""); // abstain needs no text
329 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
333 string type_name = mapvote_maps[i];
334 WriteString(MSG_ENTITY, type_name);
335 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
336 if ( mapvote_maps_flags[i] & GTV_CUSTOM )
338 WriteString(MSG_ENTITY, cvar_string(
339 strcat("sv_vote_gametype_",type_name,"_name")));
340 WriteString(MSG_ENTITY, cvar_string(
341 strcat("sv_vote_gametype_",type_name,"_description")));
346 float MapVote_SendEntity(entity to, int sf)
351 sf &= ~2; // if we send 1, we don't need to also send 2
353 WriteByte(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
354 WriteByte(MSG_ENTITY, sf);
358 // flag 1 == initialization
359 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
360 WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
361 WriteString(MSG_ENTITY, "");
362 WriteByte(MSG_ENTITY, mapvote_count);
363 WriteByte(MSG_ENTITY, mapvote_abstain);
364 WriteByte(MSG_ENTITY, mapvote_detail);
365 WriteCoord(MSG_ENTITY, mapvote_timeout);
370 WriteByte(MSG_ENTITY, 1);
371 WriteString(MSG_ENTITY, autocvar_nextmap);
373 else if ( autocvar_sv_vote_gametype )
375 // map vote but gametype has been chosen via voting screen
376 WriteByte(MSG_ENTITY, 2);
377 WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
380 WriteByte(MSG_ENTITY, 0); // map vote
384 // Send data for the vote options
385 for(i = 0; i < mapvote_count; ++i)
388 GameTypeVote_SendOption(i);
390 MapVote_SendOption(i);
396 // flag 2 == update of mask
403 for(i = 0; i < mapvote_count; ++i)
404 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
405 WriteByte(MSG_ENTITY, mapvote_selections[i]);
407 WriteByte(MSG_ENTITY, to.mapvote);
415 Net_LinkEntity(mapvote_ent = spawn(), false, 0, MapVote_SendEntity);
418 void MapVote_TouchMask()
420 mapvote_ent.SendFlags |= 2;
423 void MapVote_TouchVotes(entity voter)
425 mapvote_ent.SendFlags |= 4;
428 float MapVote_Finished(float mappos)
430 if(alreadychangedlevel)
437 if(autocvar_sv_eventlog)
439 result = strcat(":vote:finished:", mapvote_maps[mappos]);
440 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
441 didntvote = mapvote_voters;
442 for(i = 0; i < mapvote_count; ++i)
443 if(mapvote_maps_flags[i] & GTV_AVAILABLE )
445 didntvote -= mapvote_selections[i];
448 result = strcat(result, ":", mapvote_maps[i]);
449 result = strcat(result, ":", ftos(mapvote_selections[i]));
452 result = strcat(result, ":didn't vote:", ftos(didntvote));
455 if(mapvote_maps_suggested[mappos])
456 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
459 FOR_EACH_REALCLIENT(other)
460 FixClientCvars(other);
464 if ( GameTypeVote_Finished(mappos) )
466 gametypevote = false;
467 if(autocvar_nextmap != "")
469 Map_Goto_SetStr(autocvar_nextmap);
471 alreadychangedlevel = true;
480 Map_Goto_SetStr(mapvote_maps[mappos]);
482 alreadychangedlevel = true;
487 void MapVote_CheckRules_1()
491 for(i = 0; i < mapvote_count; ++i)
492 if( mapvote_maps_flags[i] & GTV_AVAILABLE )
494 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
495 mapvote_selections[i] = 0;
499 FOR_EACH_REALCLIENT(other)
504 i = other.mapvote - 1;
505 //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
506 mapvote_selections[i] = mapvote_selections[i] + 1;
511 float MapVote_CheckRules_2()
514 float firstPlace, secondPlace, currentPlace;
515 float firstPlaceVotes, secondPlaceVotes, currentVotes;
516 float mapvote_voters_real;
519 if(mapvote_count_real == 1)
520 return MapVote_Finished(0);
522 mapvote_voters_real = mapvote_voters;
524 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
526 RandomSelection_Init();
529 for(i = 0; i < mapvote_count_real; ++i)
530 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
532 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
533 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
535 currentVotes = mapvote_selections[i];
539 firstPlaceVotes = RandomSelection_best_priority;
540 if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
541 firstPlace = currentPlace;
543 firstPlace = RandomSelection_chosen_float;
545 //dprint("First place: ", ftos(firstPlace), "\n");
546 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
548 RandomSelection_Init();
549 for(i = 0; i < mapvote_count_real; ++i)
551 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
552 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
553 secondPlace = RandomSelection_chosen_float;
554 secondPlaceVotes = RandomSelection_best_priority;
555 //dprint("Second place: ", ftos(secondPlace), "\n");
556 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
559 error("No first place in map vote... WTF?");
561 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
562 return MapVote_Finished(firstPlace);
564 if(mapvote_keeptwotime)
565 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
569 mapvote_message = "Now decide between the TOP TWO!";
570 mapvote_keeptwotime = 0;
571 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
572 result = strcat(result, ":", ftos(firstPlaceVotes));
573 result = strcat(result, ":", mapvote_maps[secondPlace]);
574 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
575 didntvote = mapvote_voters;
576 for(i = 0; i < mapvote_count; ++i)
578 didntvote -= mapvote_selections[i];
582 result = strcat(result, ":", mapvote_maps[i]);
583 result = strcat(result, ":", ftos(mapvote_selections[i]));
584 if(i < mapvote_count_real)
586 mapvote_maps_flags[i] &= ~GTV_AVAILABLE;
590 result = strcat(result, ":didn't vote:", ftos(didntvote));
591 if(autocvar_sv_eventlog)
603 keeptwo = mapvote_keeptwotime;
604 MapVote_CheckRules_1(); // count
605 if(MapVote_CheckRules_2()) // decide
609 FOR_EACH_REALCLIENT(other)
611 // hide scoreboard again
612 if(other.health != 2342)
616 if(IS_REAL_CLIENT(other))
619 WriteByte(MSG_ONE, SVC_FINALE);
620 WriteString(MSG_ONE, "");
624 // clear possibly invalid votes
625 if ( !(mapvote_maps_flags[other.mapvote-1] & GTV_AVAILABLE) )
627 // use impulses as new vote
628 if(other.impulse >= 1 && other.impulse <= mapvote_count)
629 if( mapvote_maps_flags[other.impulse - 1] & GTV_AVAILABLE )
631 other.mapvote = other.impulse;
632 MapVote_TouchVotes(other);
640 MapVote_CheckRules_1(); // just count
645 // if mapvote is already running, don't do this initialization again
646 if(mapvote_run) { return; }
648 // don't start mapvote until after playerstats gamereport is sent
649 if(PlayerStats_GameReport_DelayMapVote) { return; }
652 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
661 if(alreadychangedlevel)
664 if(time < mapvote_nextthink)
668 mapvote_nextthink = time + 0.5;
670 if(!mapvote_initialized)
672 if(autocvar_rescan_pending == 1)
674 cvar_set("rescan_pending", "2");
675 localcmd("fs_rescan\nrescan_pending 3\n");
678 else if(autocvar_rescan_pending == 2)
682 else if(autocvar_rescan_pending == 3)
684 // now build missing mapinfo files
685 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
688 // we're done, start the timer
689 cvar_set("rescan_pending", "0");
692 mapvote_initialized = true;
693 if(DoNextMapOverride(0))
695 if(!autocvar_g_maplist_votable || player_count <= 0)
701 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
702 else if(autocvar_nextmap == "") { MapVote_Init(); }
708 float GameTypeVote_SetGametype(float type)
710 if (MapInfo_CurrentGametype() == type)
713 float tsave = MapInfo_CurrentGametype();
715 MapInfo_SwitchGameType(type);
718 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
719 if(MapInfo_count > 0)
721 // update lsmaps in case the gametype changed, this way people can easily list maps for it
722 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
723 lsmaps_reply = strzone(getlsmaps());
724 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
728 bprint("Cannot use this game type: no map for it found\n");
729 MapInfo_SwitchGameType(tsave);
730 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
734 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
736 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
737 if(autocvar_g_maplist_shuffle)
743 float gametypevote_finished;
744 float GameTypeVote_Finished(float pos)
746 if(!gametypevote || gametypevote_finished)
749 if ( !GameTypeVote_SetGametype(GameTypeVote_Type_FromString(mapvote_maps[pos])) )
751 LOG_TRACE("Selected gametype is not supported by any map");
754 localcmd("sv_vote_gametype_hook_all\n");
755 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
757 gametypevote_finished = true;
762 float GameTypeVote_AddVotable(string nextMode)
765 if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == 0 )
767 for(j = 0; j < mapvote_count; ++j)
768 if(mapvote_maps[j] == nextMode)
771 mapvote_maps[mapvote_count] = strzone(nextMode);
772 mapvote_maps_suggested[mapvote_count] = false;
774 mapvote_maps_screenshot_dir[mapvote_count] = 0;
775 mapvote_maps_pakfile[mapvote_count] = strzone("");
776 mapvote_maps_flags[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
784 float GameTypeVote_Start()
787 MapVote_ClearAllVotes();
788 MapVote_UnzoneStrings();
791 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
793 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
795 float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
796 n = min(MAPVOTE_COUNT, n);
798 float really_available, which_available;
799 really_available = 0;
800 which_available = -1;
801 for(j = 0; j < n; ++j)
803 if ( GameTypeVote_AddVotable(argv(j)) )
804 if ( mapvote_maps_flags[j] & GTV_AVAILABLE )
811 mapvote_count_real = mapvote_count;
815 if ( really_available == 0 )
817 if ( mapvote_count > 0 )
818 strunzone(mapvote_maps[0]);
819 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
820 //GameTypeVote_Finished(0);
824 if ( really_available == 1 )
826 //GameTypeVote_Finished(which_available);
827 MapVote_Finished(which_available);
831 mapvote_count_real = mapvote_count;
833 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
834 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
835 mapvote_keeptwotime = 0;