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 Gametype GameTypeVote_Type_FromString(string type_name)
46 Gametype 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 Gametype 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, NULL) )
72 if (!(MapInfo_Map_supportedGametypes & type.m_flags))
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)).m_flags;
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 FOREACH_CLIENT(true, LAMBDA(it.mapvote = 0));
105 void MapVote_UnzoneStrings()
108 for(j = 0; j < mapvote_count; ++j)
110 if ( mapvote_maps[j] )
112 strunzone(mapvote_maps[j]);
113 mapvote_maps[j] = string_null;
115 if ( mapvote_maps_pakfile[j] )
117 strunzone(mapvote_maps_pakfile[j]);
118 mapvote_maps_pakfile[j] = string_null;
123 string MapVote_Suggest(entity this, string m)
127 return "That's not how to use this command.";
128 if(!autocvar_g_maplist_votable_suggestions)
129 return "Suggestions are not accepted on this server.";
130 if(mapvote_initialized)
132 return "Can't suggest - voting is already in progress!";
133 m = GameTypeVote_MapInfo_FixName(m);
135 return "The map you suggested is not available on this server.";
136 if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
138 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
140 if (!autocvar_sv_vote_gametype)
141 if(!MapInfo_CheckMap(m))
142 return "The map you suggested does not support the current game mode.";
143 for(i = 0; i < mapvote_suggestion_ptr; ++i)
144 if(mapvote_suggestions[i] == m)
145 return "This map was already suggested.";
146 if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
148 i = floor(random() * mapvote_suggestion_ptr);
152 i = mapvote_suggestion_ptr;
153 mapvote_suggestion_ptr += 1;
155 if(mapvote_suggestions[i] != "")
156 strunzone(mapvote_suggestions[i]);
157 mapvote_suggestions[i] = strzone(m);
158 if(autocvar_sv_eventlog)
159 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(this.playerid)));
160 return strcat("Suggestion of ", m, " accepted.");
163 void MapVote_AddVotable(string nextMap, float isSuggestion)
166 string pakfile, mapfile;
170 for(j = 0; j < mapvote_count; ++j)
171 if(mapvote_maps[j] == nextMap)
173 // suggestions might be no longer valid/allowed after gametype switch!
175 if(!MapInfo_CheckMap(nextMap))
177 mapvote_maps[mapvote_count] = strzone(nextMap);
178 mapvote_maps_suggested[mapvote_count] = isSuggestion;
180 pakfile = string_null;
181 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
183 mapfile = strcat(mapvote_screenshot_dirs[i], "/", mapvote_maps[i]);
184 pakfile = whichpack(strcat(mapfile, ".tga"));
186 pakfile = whichpack(strcat(mapfile, ".jpg"));
188 pakfile = whichpack(strcat(mapfile, ".png"));
192 if(i >= mapvote_screenshot_dirs_count)
193 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?
194 for(o = strstrofs(pakfile, "/", 0)+1; o > 0; o = strstrofs(pakfile, "/", 0)+1)
195 pakfile = substring(pakfile, o, -1);
197 mapvote_maps_screenshot_dir[mapvote_count] = i;
198 mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
199 mapvote_maps_flags[mapvote_count] = GTV_AVAILABLE;
209 MapVote_ClearAllVotes();
210 MapVote_UnzoneStrings();
213 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
214 mapvote_abstain = autocvar_g_maplist_votable_abstain;
217 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
219 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
220 smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
222 // we need this for AddVotable, as that cycles through the screenshot dirs
223 mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
224 if(mapvote_screenshot_dirs_count == 0)
225 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
226 mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
227 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
228 mapvote_screenshot_dirs[i] = strzone(argv(i));
230 if(mapvote_suggestion_ptr)
231 for(i = 0; i < 100 && mapvote_count < smax; ++i)
232 MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], true);
234 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
235 MapVote_AddVotable(GetNextMap(), false);
237 if(mapvote_count == 0)
239 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
240 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
241 if(autocvar_g_maplist_shuffle)
243 localcmd("\nmenu_cmd sync\n");
244 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
245 MapVote_AddVotable(GetNextMap(), false);
248 mapvote_count_real = mapvote_count;
250 MapVote_AddVotable("don't care", 0);
252 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
254 mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
255 mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
256 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
257 mapvote_keeptwotime = 0;
258 mapvote_message = "Choose a map and press its key!";
263 void MapVote_SendPicture(entity to, int id)
266 WriteHeader(MSG_ONE, TE_CSQC_PICTURE);
267 WriteByte(MSG_ONE, id);
268 WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
272 void MapVote_WriteMask()
275 if ( mapvote_count < 24 )
279 for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
280 if(mapvote_maps_flags[i] & GTV_AVAILABLE )
283 if(mapvote_count < 8)
284 WriteByte(MSG_ENTITY, mask);
285 else if (mapvote_count < 16)
286 WriteShort(MSG_ENTITY,mask);
288 WriteLong(MSG_ENTITY, mask);
292 for ( i = 0; i < mapvote_count; ++i )
293 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
298 * Sends a single map vote option to the client
300 void MapVote_SendOption(int i)
303 if(mapvote_abstain && i == mapvote_count - 1)
305 WriteString(MSG_ENTITY, ""); // abstain needs no text
306 WriteString(MSG_ENTITY, ""); // abstain needs no pack
307 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
311 WriteString(MSG_ENTITY, mapvote_maps[i]);
312 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
313 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
318 * Sends a single gametype vote option to the client
320 void GameTypeVote_SendOption(int i)
323 if(mapvote_abstain && i == mapvote_count - 1)
325 WriteString(MSG_ENTITY, ""); // abstain needs no text
326 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
330 string type_name = mapvote_maps[i];
331 WriteString(MSG_ENTITY, type_name);
332 WriteByte(MSG_ENTITY, mapvote_maps_flags[i]);
333 if ( mapvote_maps_flags[i] & GTV_CUSTOM )
335 WriteString(MSG_ENTITY, cvar_string(
336 strcat("sv_vote_gametype_",type_name,"_name")));
337 WriteString(MSG_ENTITY, cvar_string(
338 strcat("sv_vote_gametype_",type_name,"_description")));
339 WriteString(MSG_ENTITY, cvar_string(
340 strcat("sv_vote_gametype_",type_name,"_type")));
341 WriteString(MSG_ENTITY, cvar_string(
342 strcat("sv_vote_gametype_",type_name,"_icon")));
347 bool MapVote_SendEntity(entity this, entity to, int sf)
352 sf &= ~2; // if we send 1, we don't need to also send 2
354 WriteHeader(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
355 WriteByte(MSG_ENTITY, sf);
359 // flag 1 == initialization
360 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
361 WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
362 WriteString(MSG_ENTITY, "");
363 WriteByte(MSG_ENTITY, mapvote_count);
364 WriteByte(MSG_ENTITY, mapvote_abstain);
365 WriteByte(MSG_ENTITY, mapvote_detail);
366 WriteCoord(MSG_ENTITY, mapvote_timeout);
371 WriteByte(MSG_ENTITY, 1);
372 WriteString(MSG_ENTITY, autocvar_nextmap);
374 else if ( autocvar_sv_vote_gametype )
376 // map vote but gametype has been chosen via voting screen
377 WriteByte(MSG_ENTITY, 2);
378 WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
381 WriteByte(MSG_ENTITY, 0); // map vote
385 // Send data for the vote options
386 for(i = 0; i < mapvote_count; ++i)
389 GameTypeVote_SendOption(i);
391 MapVote_SendOption(i);
397 // flag 2 == update of mask
404 for(i = 0; i < mapvote_count; ++i)
405 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
406 WriteByte(MSG_ENTITY, mapvote_selections[i]);
408 WriteByte(MSG_ENTITY, to.mapvote);
416 Net_LinkEntity(mapvote_ent = spawn(), false, 0, MapVote_SendEntity);
419 void MapVote_TouchMask()
421 mapvote_ent.SendFlags |= 2;
424 void MapVote_TouchVotes(entity voter)
426 mapvote_ent.SendFlags |= 4;
429 float MapVote_Finished(float mappos)
431 if(alreadychangedlevel)
438 if(autocvar_sv_eventlog)
440 result = strcat(":vote:finished:", mapvote_maps[mappos]);
441 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
442 didntvote = mapvote_voters;
443 for(i = 0; i < mapvote_count; ++i)
444 if(mapvote_maps_flags[i] & GTV_AVAILABLE )
446 didntvote -= mapvote_selections[i];
449 result = strcat(result, ":", mapvote_maps[i]);
450 result = strcat(result, ":", ftos(mapvote_selections[i]));
453 result = strcat(result, ":didn't vote:", ftos(didntvote));
456 if(mapvote_maps_suggested[mappos])
457 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
460 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(FixClientCvars(it)));
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()
489 for (int i = 0; i < mapvote_count; ++i)
490 if (mapvote_maps_flags[i] & GTV_AVAILABLE)
492 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
493 mapvote_selections[i] = 0;
497 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
501 int idx = it.mapvote - 1;
502 //dprint("Player ", it.netname, " vote = ", ftos(idx), "\n");
503 ++mapvote_selections[idx];
508 float MapVote_CheckRules_2()
511 float firstPlace, secondPlace, currentPlace;
512 float firstPlaceVotes, secondPlaceVotes, currentVotes;
513 float mapvote_voters_real;
516 if(mapvote_count_real == 1)
517 return MapVote_Finished(0);
519 mapvote_voters_real = mapvote_voters;
521 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
523 RandomSelection_Init();
526 for(i = 0; i < mapvote_count_real; ++i)
527 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
529 RandomSelection_Add(NULL, i, string_null, 1, mapvote_selections[i]);
530 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
532 currentVotes = mapvote_selections[i];
536 firstPlaceVotes = RandomSelection_best_priority;
537 if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
538 firstPlace = currentPlace;
540 firstPlace = RandomSelection_chosen_float;
542 //dprint("First place: ", ftos(firstPlace), "\n");
543 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
545 RandomSelection_Init();
546 for(i = 0; i < mapvote_count_real; ++i)
548 if ( mapvote_maps_flags[i] & GTV_AVAILABLE )
549 RandomSelection_Add(NULL, i, string_null, 1, mapvote_selections[i]);
550 secondPlace = RandomSelection_chosen_float;
551 secondPlaceVotes = RandomSelection_best_priority;
552 //dprint("Second place: ", ftos(secondPlace), "\n");
553 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
556 error("No first place in map vote... WTF?");
558 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
559 return MapVote_Finished(firstPlace);
561 if(mapvote_keeptwotime)
562 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
566 mapvote_message = "Now decide between the TOP TWO!";
567 mapvote_keeptwotime = 0;
568 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
569 result = strcat(result, ":", ftos(firstPlaceVotes));
570 result = strcat(result, ":", mapvote_maps[secondPlace]);
571 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
572 didntvote = mapvote_voters;
573 for(i = 0; i < mapvote_count; ++i)
575 didntvote -= mapvote_selections[i];
579 result = strcat(result, ":", mapvote_maps[i]);
580 result = strcat(result, ":", ftos(mapvote_selections[i]));
581 if(i < mapvote_count_real)
583 mapvote_maps_flags[i] &= ~GTV_AVAILABLE;
587 result = strcat(result, ":didn't vote:", ftos(didntvote));
588 if(autocvar_sv_eventlog)
600 keeptwo = mapvote_keeptwotime;
601 MapVote_CheckRules_1(); // count
602 if(MapVote_CheckRules_2()) // decide
606 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
607 // hide scoreboard again
608 if(it.health != 2342)
614 WriteByte(MSG_ONE, SVC_FINALE);
615 WriteString(MSG_ONE, "");
618 // clear possibly invalid votes
619 if ( !(mapvote_maps_flags[it.mapvote-1] & GTV_AVAILABLE) )
621 // use impulses as new vote
622 if(it.impulse >= 1 && it.impulse <= mapvote_count)
623 if( mapvote_maps_flags[it.impulse - 1] & GTV_AVAILABLE )
625 it.mapvote = it.impulse;
626 MapVote_TouchVotes(it);
634 MapVote_CheckRules_1(); // just count
639 // if mapvote is already running, don't do this initialization again
640 if(mapvote_run) { return; }
642 // don't start mapvote until after playerstats gamereport is sent
643 if(PlayerStats_GameReport_DelayMapVote) { return; }
646 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
655 if(alreadychangedlevel)
658 if(time < mapvote_nextthink)
662 mapvote_nextthink = time + 0.5;
664 if(!mapvote_initialized)
666 if(autocvar_rescan_pending == 1)
668 cvar_set("rescan_pending", "2");
669 localcmd("fs_rescan\nrescan_pending 3\n");
672 else if(autocvar_rescan_pending == 2)
676 else if(autocvar_rescan_pending == 3)
678 // now build missing mapinfo files
679 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
682 // we're done, start the timer
683 cvar_set("rescan_pending", "0");
686 mapvote_initialized = true;
687 if(DoNextMapOverride(0))
689 if(!autocvar_g_maplist_votable || player_count <= 0)
695 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
696 else if(autocvar_nextmap == "") { MapVote_Init(); }
702 float GameTypeVote_SetGametype(Gametype type)
704 if (MapInfo_CurrentGametype() == type)
707 Gametype tsave = MapInfo_CurrentGametype();
709 MapInfo_SwitchGameType(type);
712 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
713 if(MapInfo_count > 0)
715 // update lsmaps in case the gametype changed, this way people can easily list maps for it
716 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
717 lsmaps_reply = strzone(getlsmaps());
718 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
722 bprint("Cannot use this game type: no map for it found\n");
723 MapInfo_SwitchGameType(tsave);
724 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
728 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
730 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
731 if(autocvar_g_maplist_shuffle)
737 float gametypevote_finished;
738 float GameTypeVote_Finished(float pos)
740 if(!gametypevote || gametypevote_finished)
743 if ( !GameTypeVote_SetGametype(GameTypeVote_Type_FromString(mapvote_maps[pos])) )
745 LOG_TRACE("Selected gametype is not supported by any map");
748 localcmd("sv_vote_gametype_hook_all\n");
749 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
751 gametypevote_finished = true;
756 float GameTypeVote_AddVotable(string nextMode)
759 if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == NULL )
761 for(j = 0; j < mapvote_count; ++j)
762 if(mapvote_maps[j] == nextMode)
765 mapvote_maps[mapvote_count] = strzone(nextMode);
766 mapvote_maps_suggested[mapvote_count] = false;
768 mapvote_maps_screenshot_dir[mapvote_count] = 0;
769 mapvote_maps_pakfile[mapvote_count] = strzone("");
770 mapvote_maps_flags[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
778 float GameTypeVote_Start()
781 MapVote_ClearAllVotes();
782 MapVote_UnzoneStrings();
785 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
787 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
789 float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
790 n = min(MAPVOTE_COUNT, n);
792 float really_available, which_available;
793 really_available = 0;
794 which_available = -1;
795 for(j = 0; j < n; ++j)
797 if ( GameTypeVote_AddVotable(argv(j)) )
798 if ( mapvote_maps_flags[j] & GTV_AVAILABLE )
805 mapvote_count_real = mapvote_count;
809 if ( really_available == 0 )
811 if ( mapvote_count > 0 )
812 strunzone(mapvote_maps[0]);
813 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
814 //GameTypeVote_Finished(0);
818 if ( really_available == 1 )
820 //GameTypeVote_Finished(which_available);
821 MapVote_Finished(which_available);
825 mapvote_count_real = mapvote_count;
827 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
828 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
829 mapvote_keeptwotime = 0;