1 #include "mapvoting.qh"
4 #include "command/cmd.qh"
5 #include "command/getreplies.qh"
6 #include "../common/constants.qh"
7 #include "../common/mapinfo.qh"
8 #include "../common/playerstats.qh"
9 #include "../common/util.qh"
14 float mapvote_nextthink;
15 float mapvote_keeptwotime;
16 float mapvote_timeout;
17 string mapvote_message;
18 const float MAPVOTE_SCREENSHOT_DIRS_COUNT = 4;
19 string mapvote_screenshot_dirs[MAPVOTE_SCREENSHOT_DIRS_COUNT];
20 float mapvote_screenshot_dirs_count;
23 float mapvote_count_real;
24 string mapvote_maps[MAPVOTE_COUNT];
25 float mapvote_maps_screenshot_dir[MAPVOTE_COUNT];
26 string mapvote_maps_pakfile[MAPVOTE_COUNT];
27 float mapvote_maps_suggested[MAPVOTE_COUNT];
28 string mapvote_suggestions[MAPVOTE_COUNT];
29 float mapvote_suggestion_ptr;
31 float mapvote_selections[MAPVOTE_COUNT];
32 float mapvote_maps_flags[MAPVOTE_COUNT];
35 float mapvote_abstain;
41 * Returns the gamtype ID from its name, if type_name isn't a real gametype it
42 * checks for sv_vote_gametype_(type_name)_type
44 float GameTypeVote_Type_FromString(string type_name)
46 float type = MapInfo_Type_FromString(type_name);
48 type = MapInfo_Type_FromString(cvar_string(
49 strcat("sv_vote_gametype_",type_name,"_type")));
53 int GameTypeVote_AvailabilityStatus(string type_name)
55 int flag = GTV_FORBIDDEN;
57 float type = MapInfo_Type_FromString(type_name);
60 type = MapInfo_Type_FromString(cvar_string(
61 strcat("sv_vote_gametype_",type_name,"_type")));
68 if ( autocvar_nextmap != "" )
70 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, 0) )
72 if (!(MapInfo_Map_supportedGametypes & type))
76 return flag | GTV_AVAILABLE;
79 float GameTypeVote_GetMask()
81 float n, j, gametype_mask;
82 n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
83 n = min(MAPVOTE_COUNT, n);
85 for(j = 0; j < n; ++j)
86 gametype_mask |= GameTypeVote_Type_FromString(argv(j));
90 string GameTypeVote_MapInfo_FixName(string m)
92 if ( autocvar_sv_vote_gametype )
95 MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
97 return MapInfo_FixName(m);
100 void MapVote_ClearAllVotes()
102 FOR_EACH_CLIENT(other)
106 void MapVote_UnzoneStrings()
109 for(j = 0; j < mapvote_count; ++j)
111 if ( mapvote_maps[j] )
113 strunzone(mapvote_maps[j]);
114 mapvote_maps[j] = string_null;
116 if ( mapvote_maps_pakfile[j] )
118 strunzone(mapvote_maps_pakfile[j]);
119 mapvote_maps_pakfile[j] = string_null;
124 string MapVote_Suggest(string m)
128 return "That's not how to use this command.";
129 if(!autocvar_g_maplist_votable_suggestions)
130 return "Suggestions are not accepted on this server.";
131 if(mapvote_initialized)
133 return "Can't suggest - voting is already in progress!";
134 m = GameTypeVote_MapInfo_FixName(m);
136 return "The map you suggested is not available on this server.";
137 if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
139 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
141 if (!autocvar_sv_vote_gametype)
142 if(!MapInfo_CheckMap(m))
143 return "The map you suggested does not support the current game mode.";
144 for(i = 0; i < mapvote_suggestion_ptr; ++i)
145 if(mapvote_suggestions[i] == m)
146 return "This map was already suggested.";
147 if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
149 i = floor(random() * mapvote_suggestion_ptr);
153 i = mapvote_suggestion_ptr;
154 mapvote_suggestion_ptr += 1;
156 if(mapvote_suggestions[i] != "")
157 strunzone(mapvote_suggestions[i]);
158 mapvote_suggestions[i] = strzone(m);
159 if(autocvar_sv_eventlog)
160 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid)));
161 return strcat("Suggestion of ", m, " accepted.");
164 void MapVote_AddVotable(string nextMap, float isSuggestion)
167 string pakfile, mapfile;
171 for(j = 0; j < mapvote_count; ++j)
172 if(mapvote_maps[j] == nextMap)
174 // suggestions might be no longer valid/allowed after gametype switch!
176 if(!MapInfo_CheckMap(nextMap))
178 mapvote_maps[mapvote_count] = strzone(nextMap);
179 mapvote_maps_suggested[mapvote_count] = isSuggestion;
181 pakfile = string_null;
182 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
184 mapfile = strcat(mapvote_screenshot_dirs[i], "/", mapvote_maps[i]);
185 pakfile = whichpack(strcat(mapfile, ".tga"));
187 pakfile = whichpack(strcat(mapfile, ".jpg"));
189 pakfile = whichpack(strcat(mapfile, ".png"));
193 if(i >= mapvote_screenshot_dirs_count)
194 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?
195 for(o = strstr(pakfile, "/", 0)+1; o > 0; o = strstr(pakfile, "/", 0)+1)
196 pakfile = substring(pakfile, o, -1);
198 mapvote_maps_screenshot_dir[mapvote_count] = i;
199 mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
200 mapvote_maps_flags[mapvote_count] = GTV_AVAILABLE;
210 MapVote_ClearAllVotes();
211 MapVote_UnzoneStrings();
214 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
215 mapvote_abstain = autocvar_g_maplist_votable_abstain;
218 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
220 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
221 smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
223 // we need this for AddVotable, as that cycles through the screenshot dirs
224 mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
225 if(mapvote_screenshot_dirs_count == 0)
226 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
227 mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
228 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
229 mapvote_screenshot_dirs[i] = strzone(argv(i));
231 if(mapvote_suggestion_ptr)
232 for(i = 0; i < 100 && mapvote_count < smax; ++i)
233 MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], true);
235 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
236 MapVote_AddVotable(GetNextMap(), false);
238 if(mapvote_count == 0)
240 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
241 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
242 if(autocvar_g_maplist_shuffle)
244 localcmd("\nmenu_cmd sync\n");
245 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
246 MapVote_AddVotable(GetNextMap(), false);
249 mapvote_count_real = mapvote_count;
251 MapVote_AddVotable("don't care", 0);
253 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
255 mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
256 mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
257 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
258 mapvote_keeptwotime = 0;
259 mapvote_message = "Choose a map and press its key!";
264 void MapVote_SendPicture(float id)
267 WriteByte(MSG_ONE, SVC_TEMPENTITY);
268 WriteByte(MSG_ONE, TE_CSQC_PICTURE);
269 WriteByte(MSG_ONE, id);
270 WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
274 void MapVote_WriteMask()
277 if ( mapvote_count < 24 )
281 for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
282 if(mapvote_maps_flags[i] & GTV_AVAILABLE )
285 if(mapvote_count < 8)
286 WriteByte(MSG_ENTITY, mask);
287 else if (mapvote_count < 16)
288 WriteShort(MSG_ENTITY,mask);
290 WriteLong(MSG_ENTITY, mask);
294 for ( i = 0; i < mapvote_count; ++i )
295 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
300 * Sends a single map vote option to the client
302 void MapVote_SendOption(int i)
305 if(mapvote_abstain && i == mapvote_count - 1)
307 WriteString(MSG_ENTITY, ""); // abstain needs no text
308 WriteString(MSG_ENTITY, ""); // abstain needs no pack
309 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
313 WriteString(MSG_ENTITY, mapvote_maps[i]);
314 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
315 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
320 * Sends a single gametype vote option to the client
322 void GameTypeVote_SendOption(int i)
325 if(mapvote_abstain && i == mapvote_count - 1)
327 WriteString(MSG_ENTITY, ""); // abstain needs no text
328 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
332 string type_name = mapvote_maps[i];
333 WriteString(MSG_ENTITY, type_name);
334 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
335 if ( mapvote_maps_flags[i] & GTV_CUSTOM )
337 WriteString(MSG_ENTITY, cvar_string(
338 strcat("sv_vote_gametype_",type_name,"_name")));
339 WriteString(MSG_ENTITY, cvar_string(
340 strcat("sv_vote_gametype_",type_name,"_description")));
345 bool MapVote_SendEntity(entity this, entity to, int sf)
350 sf &= ~2; // if we send 1, we don't need to also send 2
352 WriteByte(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
353 WriteByte(MSG_ENTITY, sf);
357 // flag 1 == initialization
358 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
359 WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
360 WriteString(MSG_ENTITY, "");
361 WriteByte(MSG_ENTITY, mapvote_count);
362 WriteByte(MSG_ENTITY, mapvote_abstain);
363 WriteByte(MSG_ENTITY, mapvote_detail);
364 WriteCoord(MSG_ENTITY, mapvote_timeout);
369 WriteByte(MSG_ENTITY, 1);
370 WriteString(MSG_ENTITY, autocvar_nextmap);
372 else if ( autocvar_sv_vote_gametype )
374 // map vote but gametype has been chosen via voting screen
375 WriteByte(MSG_ENTITY, 2);
376 WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
379 WriteByte(MSG_ENTITY, 0); // map vote
383 // Send data for the vote options
384 for(i = 0; i < mapvote_count; ++i)
387 GameTypeVote_SendOption(i);
389 MapVote_SendOption(i);
395 // flag 2 == update of mask
402 for(i = 0; i < mapvote_count; ++i)
403 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
404 WriteByte(MSG_ENTITY, mapvote_selections[i]);
406 WriteByte(MSG_ENTITY, to.mapvote);
414 Net_LinkEntity(mapvote_ent = spawn(), false, 0, MapVote_SendEntity);
417 void MapVote_TouchMask()
419 mapvote_ent.SendFlags |= 2;
422 void MapVote_TouchVotes(entity voter)
424 mapvote_ent.SendFlags |= 4;
427 float MapVote_Finished(float mappos)
429 if(alreadychangedlevel)
436 if(autocvar_sv_eventlog)
438 result = strcat(":vote:finished:", mapvote_maps[mappos]);
439 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
440 didntvote = mapvote_voters;
441 for(i = 0; i < mapvote_count; ++i)
442 if(mapvote_maps_flags[i] & GTV_AVAILABLE )
444 didntvote -= mapvote_selections[i];
447 result = strcat(result, ":", mapvote_maps[i]);
448 result = strcat(result, ":", ftos(mapvote_selections[i]));
451 result = strcat(result, ":didn't vote:", ftos(didntvote));
454 if(mapvote_maps_suggested[mappos])
455 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
458 FOR_EACH_REALCLIENT(other)
459 FixClientCvars(other);
463 if ( GameTypeVote_Finished(mappos) )
465 gametypevote = false;
466 if(autocvar_nextmap != "")
468 Map_Goto_SetStr(autocvar_nextmap);
470 alreadychangedlevel = true;
479 Map_Goto_SetStr(mapvote_maps[mappos]);
481 alreadychangedlevel = true;
486 void MapVote_CheckRules_1()
490 for(i = 0; i < mapvote_count; ++i)
491 if( mapvote_maps_flags[i] & GTV_AVAILABLE )
493 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
494 mapvote_selections[i] = 0;
498 FOR_EACH_REALCLIENT(other)
503 i = other.mapvote - 1;
504 //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
505 mapvote_selections[i] = mapvote_selections[i] + 1;
510 float MapVote_CheckRules_2()
513 float firstPlace, secondPlace, currentPlace;
514 float firstPlaceVotes, secondPlaceVotes, currentVotes;
515 float mapvote_voters_real;
518 if(mapvote_count_real == 1)
519 return MapVote_Finished(0);
521 mapvote_voters_real = mapvote_voters;
523 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
525 RandomSelection_Init();
528 for(i = 0; i < mapvote_count_real; ++i)
529 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
531 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
532 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
534 currentVotes = mapvote_selections[i];
538 firstPlaceVotes = RandomSelection_best_priority;
539 if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
540 firstPlace = currentPlace;
542 firstPlace = RandomSelection_chosen_float;
544 //dprint("First place: ", ftos(firstPlace), "\n");
545 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
547 RandomSelection_Init();
548 for(i = 0; i < mapvote_count_real; ++i)
550 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
551 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
552 secondPlace = RandomSelection_chosen_float;
553 secondPlaceVotes = RandomSelection_best_priority;
554 //dprint("Second place: ", ftos(secondPlace), "\n");
555 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
558 error("No first place in map vote... WTF?");
560 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
561 return MapVote_Finished(firstPlace);
563 if(mapvote_keeptwotime)
564 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
568 mapvote_message = "Now decide between the TOP TWO!";
569 mapvote_keeptwotime = 0;
570 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
571 result = strcat(result, ":", ftos(firstPlaceVotes));
572 result = strcat(result, ":", mapvote_maps[secondPlace]);
573 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
574 didntvote = mapvote_voters;
575 for(i = 0; i < mapvote_count; ++i)
577 didntvote -= mapvote_selections[i];
581 result = strcat(result, ":", mapvote_maps[i]);
582 result = strcat(result, ":", ftos(mapvote_selections[i]));
583 if(i < mapvote_count_real)
585 mapvote_maps_flags[i] &= ~GTV_AVAILABLE;
589 result = strcat(result, ":didn't vote:", ftos(didntvote));
590 if(autocvar_sv_eventlog)
602 keeptwo = mapvote_keeptwotime;
603 MapVote_CheckRules_1(); // count
604 if(MapVote_CheckRules_2()) // decide
608 FOR_EACH_REALCLIENT(other)
610 // hide scoreboard again
611 if(other.health != 2342)
615 if(IS_REAL_CLIENT(other))
618 WriteByte(MSG_ONE, SVC_FINALE);
619 WriteString(MSG_ONE, "");
623 // clear possibly invalid votes
624 if ( !(mapvote_maps_flags[other.mapvote-1] & GTV_AVAILABLE) )
626 // use impulses as new vote
627 if(other.impulse >= 1 && other.impulse <= mapvote_count)
628 if( mapvote_maps_flags[other.impulse - 1] & GTV_AVAILABLE )
630 other.mapvote = other.impulse;
631 MapVote_TouchVotes(other);
639 MapVote_CheckRules_1(); // just count
644 // if mapvote is already running, don't do this initialization again
645 if(mapvote_run) { return; }
647 // don't start mapvote until after playerstats gamereport is sent
648 if(PlayerStats_GameReport_DelayMapVote) { return; }
651 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
660 if(alreadychangedlevel)
663 if(time < mapvote_nextthink)
667 mapvote_nextthink = time + 0.5;
669 if(!mapvote_initialized)
671 if(autocvar_rescan_pending == 1)
673 cvar_set("rescan_pending", "2");
674 localcmd("fs_rescan\nrescan_pending 3\n");
677 else if(autocvar_rescan_pending == 2)
681 else if(autocvar_rescan_pending == 3)
683 // now build missing mapinfo files
684 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
687 // we're done, start the timer
688 cvar_set("rescan_pending", "0");
691 mapvote_initialized = true;
692 if(DoNextMapOverride(0))
694 if(!autocvar_g_maplist_votable || player_count <= 0)
700 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
701 else if(autocvar_nextmap == "") { MapVote_Init(); }
707 float GameTypeVote_SetGametype(float type)
709 if (MapInfo_CurrentGametype() == type)
712 float tsave = MapInfo_CurrentGametype();
714 MapInfo_SwitchGameType(type);
717 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
718 if(MapInfo_count > 0)
720 // update lsmaps in case the gametype changed, this way people can easily list maps for it
721 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
722 lsmaps_reply = strzone(getlsmaps());
723 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
727 bprint("Cannot use this game type: no map for it found\n");
728 MapInfo_SwitchGameType(tsave);
729 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
733 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
735 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
736 if(autocvar_g_maplist_shuffle)
742 float gametypevote_finished;
743 float GameTypeVote_Finished(float pos)
745 if(!gametypevote || gametypevote_finished)
748 if ( !GameTypeVote_SetGametype(GameTypeVote_Type_FromString(mapvote_maps[pos])) )
750 LOG_TRACE("Selected gametype is not supported by any map");
753 localcmd("sv_vote_gametype_hook_all\n");
754 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
756 gametypevote_finished = true;
761 float GameTypeVote_AddVotable(string nextMode)
764 if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == 0 )
766 for(j = 0; j < mapvote_count; ++j)
767 if(mapvote_maps[j] == nextMode)
770 mapvote_maps[mapvote_count] = strzone(nextMode);
771 mapvote_maps_suggested[mapvote_count] = false;
773 mapvote_maps_screenshot_dir[mapvote_count] = 0;
774 mapvote_maps_pakfile[mapvote_count] = strzone("");
775 mapvote_maps_flags[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
783 float GameTypeVote_Start()
786 MapVote_ClearAllVotes();
787 MapVote_UnzoneStrings();
790 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
792 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
794 float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
795 n = min(MAPVOTE_COUNT, n);
797 float really_available, which_available;
798 really_available = 0;
799 which_available = -1;
800 for(j = 0; j < n; ++j)
802 if ( GameTypeVote_AddVotable(argv(j)) )
803 if ( mapvote_maps_flags[j] & GTV_AVAILABLE )
810 mapvote_count_real = mapvote_count;
814 if ( really_available == 0 )
816 if ( mapvote_count > 0 )
817 strunzone(mapvote_maps[0]);
818 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
819 //GameTypeVote_Finished(0);
823 if ( really_available == 1 )
825 //GameTypeVote_Finished(which_available);
826 MapVote_Finished(which_available);
830 mapvote_count_real = mapvote_count;
832 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
833 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
834 mapvote_keeptwotime = 0;