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 const float MAPVOTE_SCREENSHOT_DIRS_COUNT = 4;
18 string mapvote_screenshot_dirs[MAPVOTE_SCREENSHOT_DIRS_COUNT];
19 float mapvote_screenshot_dirs_count;
22 float mapvote_count_real;
23 string mapvote_maps[MAPVOTE_COUNT];
24 float mapvote_maps_screenshot_dir[MAPVOTE_COUNT];
25 string mapvote_maps_pakfile[MAPVOTE_COUNT];
26 float mapvote_maps_suggested[MAPVOTE_COUNT];
27 string mapvote_suggestions[MAPVOTE_COUNT];
28 float mapvote_suggestion_ptr;
30 float mapvote_selections[MAPVOTE_COUNT];
31 float mapvote_maps_flags[MAPVOTE_COUNT];
34 float mapvote_abstain;
40 * Returns the gamtype ID from its name, if type_name isn't a real gametype it
41 * checks for sv_vote_gametype_(type_name)_type
43 Gametype GameTypeVote_Type_FromString(string type_name)
45 Gametype type = MapInfo_Type_FromString(type_name);
47 type = MapInfo_Type_FromString(cvar_string(
48 strcat("sv_vote_gametype_",type_name,"_type")));
52 int GameTypeVote_AvailabilityStatus(string type_name)
54 int flag = GTV_FORBIDDEN;
56 Gametype type = MapInfo_Type_FromString(type_name);
59 type = MapInfo_Type_FromString(cvar_string(
60 strcat("sv_vote_gametype_",type_name,"_type")));
67 if ( autocvar_nextmap != "" )
69 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, NULL) )
71 if (!(MapInfo_Map_supportedGametypes & type.m_flags))
75 return flag | GTV_AVAILABLE;
78 float GameTypeVote_GetMask()
80 float n, j, gametype_mask;
81 n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
82 n = min(MAPVOTE_COUNT, n);
84 for(j = 0; j < n; ++j)
85 gametype_mask |= GameTypeVote_Type_FromString(argv(j)).m_flags;
89 string GameTypeVote_MapInfo_FixName(string m)
91 if ( autocvar_sv_vote_gametype )
94 _MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
96 return MapInfo_FixName(m);
99 void MapVote_ClearAllVotes()
101 FOREACH_CLIENT(true, LAMBDA(it.mapvote = 0));
104 void MapVote_UnzoneStrings()
107 for(j = 0; j < mapvote_count; ++j)
109 if ( mapvote_maps[j] )
111 strunzone(mapvote_maps[j]);
112 mapvote_maps[j] = string_null;
114 if ( mapvote_maps_pakfile[j] )
116 strunzone(mapvote_maps_pakfile[j]);
117 mapvote_maps_pakfile[j] = string_null;
122 string MapVote_Suggest(entity this, string m)
126 return "That's not how to use this command.";
127 if(!autocvar_g_maplist_votable_suggestions)
128 return "Suggestions are not accepted on this server.";
129 if(mapvote_initialized)
131 return "Can't suggest - voting is already in progress!";
132 m = GameTypeVote_MapInfo_FixName(m);
134 return "The map you suggested is not available on this server.";
135 if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
137 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
139 if (!autocvar_sv_vote_gametype)
140 if(!MapInfo_CheckMap(m))
141 return "The map you suggested does not support the current game mode.";
142 for(i = 0; i < mapvote_suggestion_ptr; ++i)
143 if(mapvote_suggestions[i] == m)
144 return "This map was already suggested.";
145 if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
147 i = floor(random() * mapvote_suggestion_ptr);
151 i = mapvote_suggestion_ptr;
152 mapvote_suggestion_ptr += 1;
154 if(mapvote_suggestions[i] != "")
155 strunzone(mapvote_suggestions[i]);
156 mapvote_suggestions[i] = strzone(m);
157 if(autocvar_sv_eventlog)
158 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(this.playerid)));
159 return strcat("Suggestion of ", m, " accepted.");
162 void MapVote_AddVotable(string nextMap, float isSuggestion)
165 string pakfile, mapfile;
169 for(j = 0; j < mapvote_count; ++j)
170 if(mapvote_maps[j] == nextMap)
172 // suggestions might be no longer valid/allowed after gametype switch!
174 if(!MapInfo_CheckMap(nextMap))
176 mapvote_maps[mapvote_count] = strzone(nextMap);
177 mapvote_maps_suggested[mapvote_count] = isSuggestion;
179 pakfile = string_null;
180 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
182 mapfile = strcat(mapvote_screenshot_dirs[i], "/", mapvote_maps[i]);
183 pakfile = whichpack(strcat(mapfile, ".tga"));
185 pakfile = whichpack(strcat(mapfile, ".jpg"));
187 pakfile = whichpack(strcat(mapfile, ".png"));
191 if(i >= mapvote_screenshot_dirs_count)
192 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?
193 for(o = strstrofs(pakfile, "/", 0)+1; o > 0; o = strstrofs(pakfile, "/", 0)+1)
194 pakfile = substring(pakfile, o, -1);
196 mapvote_maps_screenshot_dir[mapvote_count] = i;
197 mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
198 mapvote_maps_flags[mapvote_count] = GTV_AVAILABLE;
208 MapVote_ClearAllVotes();
209 MapVote_UnzoneStrings();
212 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
213 mapvote_abstain = autocvar_g_maplist_votable_abstain;
216 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
218 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
219 smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
221 // we need this for AddVotable, as that cycles through the screenshot dirs
222 mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
223 if(mapvote_screenshot_dirs_count == 0)
224 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
225 mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
226 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
227 mapvote_screenshot_dirs[i] = strzone(argv(i));
229 if(mapvote_suggestion_ptr)
230 for(i = 0; i < 100 && mapvote_count < smax; ++i)
231 MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], true);
233 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
234 MapVote_AddVotable(GetNextMap(), false);
236 if(mapvote_count == 0)
238 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
239 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
240 if(autocvar_g_maplist_shuffle)
242 localcmd("\nmenu_cmd sync\n");
243 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
244 MapVote_AddVotable(GetNextMap(), false);
247 mapvote_count_real = mapvote_count;
249 MapVote_AddVotable("don't care", 0);
251 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
253 mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
254 mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
255 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
256 mapvote_keeptwotime = 0;
261 void MapVote_SendPicture(entity to, int id)
264 WriteHeader(MSG_ONE, TE_CSQC_PICTURE);
265 WriteByte(MSG_ONE, id);
266 WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
270 void MapVote_WriteMask()
273 if ( mapvote_count < 24 )
277 for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
278 if(mapvote_maps_flags[i] & GTV_AVAILABLE )
281 if(mapvote_count < 8)
282 WriteByte(MSG_ENTITY, mask);
283 else if (mapvote_count < 16)
284 WriteShort(MSG_ENTITY,mask);
286 WriteLong(MSG_ENTITY, mask);
290 for ( i = 0; i < mapvote_count; ++i )
291 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
296 * Sends a single map vote option to the client
298 void MapVote_SendOption(int i)
301 if(mapvote_abstain && i == mapvote_count - 1)
303 WriteString(MSG_ENTITY, ""); // abstain needs no text
304 WriteString(MSG_ENTITY, ""); // abstain needs no pack
305 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
309 WriteString(MSG_ENTITY, mapvote_maps[i]);
310 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
311 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
316 * Sends a single gametype vote option to the client
318 void GameTypeVote_SendOption(int i)
321 if(mapvote_abstain && i == mapvote_count - 1)
323 WriteString(MSG_ENTITY, ""); // abstain needs no text
324 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
328 string type_name = mapvote_maps[i];
329 WriteString(MSG_ENTITY, type_name);
330 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
331 if ( mapvote_maps_flags[i] & GTV_CUSTOM )
333 WriteString(MSG_ENTITY, cvar_string(
334 strcat("sv_vote_gametype_",type_name,"_name")));
335 WriteString(MSG_ENTITY, cvar_string(
336 strcat("sv_vote_gametype_",type_name,"_description")));
337 WriteString(MSG_ENTITY, cvar_string(
338 strcat("sv_vote_gametype_",type_name,"_type")));
343 bool MapVote_SendEntity(entity this, entity to, int sf)
348 sf &= ~2; // if we send 1, we don't need to also send 2
350 WriteHeader(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
351 WriteByte(MSG_ENTITY, sf);
355 // flag 1 == initialization
356 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
357 WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
358 WriteString(MSG_ENTITY, "");
359 WriteByte(MSG_ENTITY, mapvote_count);
360 WriteByte(MSG_ENTITY, mapvote_abstain);
361 WriteByte(MSG_ENTITY, mapvote_detail);
362 WriteCoord(MSG_ENTITY, mapvote_timeout);
367 WriteByte(MSG_ENTITY, 1);
368 WriteString(MSG_ENTITY, autocvar_nextmap);
370 else if ( autocvar_sv_vote_gametype )
372 // map vote but gametype has been chosen via voting screen
373 WriteByte(MSG_ENTITY, 2);
374 WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
377 WriteByte(MSG_ENTITY, 0); // map vote
381 // Send data for the vote options
382 for(i = 0; i < mapvote_count; ++i)
385 GameTypeVote_SendOption(i);
387 MapVote_SendOption(i);
393 // flag 2 == update of mask
400 for(i = 0; i < mapvote_count; ++i)
401 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
402 WriteByte(MSG_ENTITY, mapvote_selections[i]);
404 WriteByte(MSG_ENTITY, to.mapvote);
412 Net_LinkEntity(mapvote_ent = spawn(), false, 0, MapVote_SendEntity);
415 void MapVote_TouchMask()
417 mapvote_ent.SendFlags |= 2;
420 void MapVote_TouchVotes(entity voter)
422 mapvote_ent.SendFlags |= 4;
425 float MapVote_Finished(float mappos)
427 if(alreadychangedlevel)
434 if(autocvar_sv_eventlog)
436 result = strcat(":vote:finished:", mapvote_maps[mappos]);
437 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
438 didntvote = mapvote_voters;
439 for(i = 0; i < mapvote_count; ++i)
440 if(mapvote_maps_flags[i] & GTV_AVAILABLE )
442 didntvote -= mapvote_selections[i];
445 result = strcat(result, ":", mapvote_maps[i]);
446 result = strcat(result, ":", ftos(mapvote_selections[i]));
449 result = strcat(result, ":didn't vote:", ftos(didntvote));
452 if(mapvote_maps_suggested[mappos])
453 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
456 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(FixClientCvars(it)));
460 if ( GameTypeVote_Finished(mappos) )
462 gametypevote = false;
463 if(autocvar_nextmap != "")
465 Map_Goto_SetStr(autocvar_nextmap);
467 alreadychangedlevel = true;
476 Map_Goto_SetStr(mapvote_maps[mappos]);
478 alreadychangedlevel = true;
483 void MapVote_CheckRules_1()
485 for (int i = 0; i < mapvote_count; ++i)
486 if (mapvote_maps_flags[i] & GTV_AVAILABLE)
488 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
489 mapvote_selections[i] = 0;
493 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
497 int idx = it.mapvote - 1;
498 //dprint("Player ", it.netname, " vote = ", ftos(idx), "\n");
499 ++mapvote_selections[idx];
504 float MapVote_CheckRules_2()
507 float firstPlace, secondPlace, currentPlace;
508 float firstPlaceVotes, secondPlaceVotes, currentVotes;
509 float mapvote_voters_real;
512 if(mapvote_count_real == 1)
513 return MapVote_Finished(0);
515 mapvote_voters_real = mapvote_voters;
517 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
519 RandomSelection_Init();
522 for(i = 0; i < mapvote_count_real; ++i)
523 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
525 RandomSelection_Add(NULL, i, string_null, 1, mapvote_selections[i]);
526 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
528 currentVotes = mapvote_selections[i];
532 firstPlaceVotes = RandomSelection_best_priority;
533 if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
534 firstPlace = currentPlace;
536 firstPlace = RandomSelection_chosen_float;
538 //dprint("First place: ", ftos(firstPlace), "\n");
539 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
541 RandomSelection_Init();
542 for(i = 0; i < mapvote_count_real; ++i)
544 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
545 RandomSelection_Add(NULL, i, string_null, 1, mapvote_selections[i]);
546 secondPlace = RandomSelection_chosen_float;
547 secondPlaceVotes = RandomSelection_best_priority;
548 //dprint("Second place: ", ftos(secondPlace), "\n");
549 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
552 error("No first place in map vote... WTF?");
554 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
555 return MapVote_Finished(firstPlace);
557 if(mapvote_keeptwotime)
558 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
562 mapvote_keeptwotime = 0;
563 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
564 result = strcat(result, ":", ftos(firstPlaceVotes));
565 result = strcat(result, ":", mapvote_maps[secondPlace]);
566 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
567 didntvote = mapvote_voters;
568 for(i = 0; i < mapvote_count; ++i)
570 didntvote -= mapvote_selections[i];
574 result = strcat(result, ":", mapvote_maps[i]);
575 result = strcat(result, ":", ftos(mapvote_selections[i]));
576 if(i < mapvote_count_real)
578 mapvote_maps_flags[i] &= ~GTV_AVAILABLE;
582 result = strcat(result, ":didn't vote:", ftos(didntvote));
583 if(autocvar_sv_eventlog)
594 MapVote_CheckRules_1(); // count
595 if(MapVote_CheckRules_2()) // decide
599 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
600 // hide scoreboard again
601 if(it.health != 2342)
607 WriteByte(MSG_ONE, SVC_FINALE);
608 WriteString(MSG_ONE, "");
611 // clear possibly invalid votes
612 if ( !(mapvote_maps_flags[it.mapvote-1] & GTV_AVAILABLE) )
614 // use impulses as new vote
615 if(it.impulse >= 1 && it.impulse <= mapvote_count)
616 if( mapvote_maps_flags[it.impulse - 1] & GTV_AVAILABLE )
618 it.mapvote = it.impulse;
619 MapVote_TouchVotes(it);
627 MapVote_CheckRules_1(); // just count
632 // if mapvote is already running, don't do this initialization again
633 if(mapvote_run) { return; }
635 // don't start mapvote until after playerstats gamereport is sent
636 if(PlayerStats_GameReport_DelayMapVote) { return; }
639 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
648 if(alreadychangedlevel)
651 if(time < mapvote_nextthink)
655 mapvote_nextthink = time + 0.5;
657 if(!mapvote_initialized)
659 if(autocvar_rescan_pending == 1)
661 cvar_set("rescan_pending", "2");
662 localcmd("fs_rescan\nrescan_pending 3\n");
665 else if(autocvar_rescan_pending == 2)
669 else if(autocvar_rescan_pending == 3)
671 // now build missing mapinfo files
672 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
675 // we're done, start the timer
676 cvar_set("rescan_pending", "0");
679 mapvote_initialized = true;
680 if(DoNextMapOverride(0))
682 if(!autocvar_g_maplist_votable || player_count <= 0)
688 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
689 else if(autocvar_nextmap == "") { MapVote_Init(); }
695 float GameTypeVote_SetGametype(Gametype type)
697 if (MapInfo_CurrentGametype() == type)
700 Gametype tsave = MapInfo_CurrentGametype();
702 MapInfo_SwitchGameType(type);
705 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
706 if(MapInfo_count > 0)
708 // update lsmaps in case the gametype changed, this way people can easily list maps for it
709 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
710 lsmaps_reply = strzone(getlsmaps());
711 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
715 bprint("Cannot use this game type: no map for it found\n");
716 MapInfo_SwitchGameType(tsave);
717 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
721 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
723 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
724 if(autocvar_g_maplist_shuffle)
730 float gametypevote_finished;
731 float GameTypeVote_Finished(float pos)
733 if(!gametypevote || gametypevote_finished)
736 if ( !GameTypeVote_SetGametype(GameTypeVote_Type_FromString(mapvote_maps[pos])) )
738 LOG_TRACE("Selected gametype is not supported by any map");
741 localcmd("sv_vote_gametype_hook_all\n");
742 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
744 gametypevote_finished = true;
749 float GameTypeVote_AddVotable(string nextMode)
752 if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == NULL )
754 for(j = 0; j < mapvote_count; ++j)
755 if(mapvote_maps[j] == nextMode)
758 mapvote_maps[mapvote_count] = strzone(nextMode);
759 mapvote_maps_suggested[mapvote_count] = false;
761 mapvote_maps_screenshot_dir[mapvote_count] = 0;
762 mapvote_maps_pakfile[mapvote_count] = strzone("");
763 mapvote_maps_flags[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
771 float GameTypeVote_Start()
774 MapVote_ClearAllVotes();
775 MapVote_UnzoneStrings();
778 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
780 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
782 float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
783 n = min(MAPVOTE_COUNT, n);
785 float really_available, which_available;
786 really_available = 0;
787 which_available = -1;
788 for(j = 0; j < n; ++j)
790 if ( GameTypeVote_AddVotable(argv(j)) )
791 if ( mapvote_maps_flags[j] & GTV_AVAILABLE )
798 mapvote_count_real = mapvote_count;
802 if ( really_available == 0 )
804 if ( mapvote_count > 0 )
805 strunzone(mapvote_maps[0]);
806 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
807 //GameTypeVote_Finished(0);
811 if ( really_available == 1 )
813 //GameTypeVote_Finished(which_available);
814 MapVote_Finished(which_available);
818 mapvote_count_real = mapvote_count;
820 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
821 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
822 mapvote_keeptwotime = 0;