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"
13 * Returns the gamtype ID from its name, if type_name isn't a real gametype it
14 * checks for sv_vote_gametype_(type_name)_type
16 float GameTypeVote_Type_FromString(string type_name)
18 float type = MapInfo_Type_FromString(type_name);
20 type = MapInfo_Type_FromString(cvar_string(
21 strcat("sv_vote_gametype_",type_name,"_type")));
25 float GameTypeVote_AvailabilityStatus(string gtname)
27 float type = GameTypeVote_Type_FromString(gtname);
31 if ( autocvar_nextmap != "" )
33 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, 0) )
35 if (!(MapInfo_Map_supportedGametypes & type))
42 float GameTypeVote_GetMask()
44 float n, j, gametype_mask;
45 n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
46 n = min(MAPVOTE_COUNT, n);
48 for(j = 0; j < n; ++j)
49 gametype_mask |= GameTypeVote_Type_FromString(argv(j));
53 string GameTypeVote_MapInfo_FixName(string m)
55 if ( autocvar_sv_vote_gametype )
58 MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
60 return MapInfo_FixName(m);
63 void MapVote_ClearAllVotes()
65 FOR_EACH_CLIENT(other)
69 void MapVote_UnzoneStrings()
72 for(j = 0; j < mapvote_count; ++j)
74 if ( mapvote_maps[j] )
76 strunzone(mapvote_maps[j]);
77 mapvote_maps[j] = string_null;
79 if ( mapvote_maps_pakfile[j] )
81 strunzone(mapvote_maps_pakfile[j]);
82 mapvote_maps_pakfile[j] = string_null;
87 string MapVote_Suggest(string m)
91 return "That's not how to use this command.";
92 if(!autocvar_g_maplist_votable_suggestions)
93 return "Suggestions are not accepted on this server.";
94 if(mapvote_initialized)
96 return "Can't suggest - voting is already in progress!";
97 m = GameTypeVote_MapInfo_FixName(m);
99 return "The map you suggested is not available on this server.";
100 if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
102 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
104 if (!autocvar_sv_vote_gametype)
105 if(!MapInfo_CheckMap(m))
106 return "The map you suggested does not support the current game mode.";
107 for(i = 0; i < mapvote_suggestion_ptr; ++i)
108 if(mapvote_suggestions[i] == m)
109 return "This map was already suggested.";
110 if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
112 i = floor(random() * mapvote_suggestion_ptr);
116 i = mapvote_suggestion_ptr;
117 mapvote_suggestion_ptr += 1;
119 if(mapvote_suggestions[i] != "")
120 strunzone(mapvote_suggestions[i]);
121 mapvote_suggestions[i] = strzone(m);
122 if(autocvar_sv_eventlog)
123 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid)));
124 return strcat("Suggestion of ", m, " accepted.");
127 void MapVote_AddVotable(string nextMap, float isSuggestion)
130 string pakfile, mapfile;
134 for(j = 0; j < mapvote_count; ++j)
135 if(mapvote_maps[j] == nextMap)
137 // suggestions might be no longer valid/allowed after gametype switch!
139 if(!MapInfo_CheckMap(nextMap))
141 mapvote_maps[mapvote_count] = strzone(nextMap);
142 mapvote_maps_suggested[mapvote_count] = isSuggestion;
144 pakfile = string_null;
145 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
147 mapfile = strcat(mapvote_screenshot_dirs[i], "/", mapvote_maps[i]);
148 pakfile = whichpack(strcat(mapfile, ".tga"));
150 pakfile = whichpack(strcat(mapfile, ".jpg"));
152 pakfile = whichpack(strcat(mapfile, ".png"));
156 if(i >= mapvote_screenshot_dirs_count)
157 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?
158 for(o = strstr(pakfile, "/", 0)+1; o > 0; o = strstr(pakfile, "/", 0)+1)
159 pakfile = substring(pakfile, o, -1);
161 mapvote_maps_screenshot_dir[mapvote_count] = i;
162 mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
163 mapvote_maps_availability[mapvote_count] = GTV_AVAILABLE;
173 MapVote_ClearAllVotes();
174 MapVote_UnzoneStrings();
177 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
178 mapvote_abstain = autocvar_g_maplist_votable_abstain;
181 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
183 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
184 smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
186 // we need this for AddVotable, as that cycles through the screenshot dirs
187 mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
188 if(mapvote_screenshot_dirs_count == 0)
189 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
190 mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
191 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
192 mapvote_screenshot_dirs[i] = strzone(argv(i));
194 if(mapvote_suggestion_ptr)
195 for(i = 0; i < 100 && mapvote_count < smax; ++i)
196 MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], true);
198 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
199 MapVote_AddVotable(GetNextMap(), false);
201 if(mapvote_count == 0)
203 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
204 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
205 if(autocvar_g_maplist_shuffle)
207 localcmd("\nmenu_cmd sync\n");
208 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
209 MapVote_AddVotable(GetNextMap(), false);
212 mapvote_count_real = mapvote_count;
214 MapVote_AddVotable("don't care", 0);
216 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
218 mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
219 mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
220 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
221 mapvote_keeptwotime = 0;
222 mapvote_message = "Choose a map and press its key!";
227 void MapVote_SendPicture(float id)
230 WriteByte(MSG_ONE, SVC_TEMPENTITY);
231 WriteByte(MSG_ONE, TE_CSQC_PICTURE);
232 WriteByte(MSG_ONE, id);
233 WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
237 void MapVote_WriteMask()
240 if ( mapvote_count < 24 )
244 for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
245 if(mapvote_maps_availability[i] == GTV_AVAILABLE )
248 if(mapvote_count < 8)
249 WriteByte(MSG_ENTITY, mask);
250 else if (mapvote_count < 16)
251 WriteShort(MSG_ENTITY,mask);
253 WriteLong(MSG_ENTITY, mask);
257 for ( i = 0; i < mapvote_count; ++i )
258 WriteByte(MSG_ENTITY, mapvote_maps_availability[i]);
262 float MapVote_SendEntity(entity to, int sf)
267 sf &= ~2; // if we send 1, we don't need to also send 2
269 WriteByte(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
270 WriteByte(MSG_ENTITY, sf);
274 // flag 1 == initialization
275 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
276 WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
277 WriteString(MSG_ENTITY, "");
278 WriteByte(MSG_ENTITY, mapvote_count);
279 WriteByte(MSG_ENTITY, mapvote_abstain);
280 WriteByte(MSG_ENTITY, mapvote_detail);
281 WriteCoord(MSG_ENTITY, mapvote_timeout);
286 WriteByte(MSG_ENTITY, 1);
287 WriteString(MSG_ENTITY, autocvar_nextmap);
289 else if ( autocvar_sv_vote_gametype )
291 // map vote but gametype has been chosen via voting screen
292 WriteByte(MSG_ENTITY, 2);
293 WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
296 WriteByte(MSG_ENTITY, 0); // map vote
300 for(i = 0; i < mapvote_count; ++i)
302 if(mapvote_abstain && i == mapvote_count - 1)
304 WriteString(MSG_ENTITY, ""); // abstain needs no text
305 WriteString(MSG_ENTITY, ""); // abstain needs no pack
306 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
307 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
311 WriteString(MSG_ENTITY, mapvote_maps[i]);
312 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
313 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
314 WriteByte(MSG_ENTITY, mapvote_maps_availability[i]);
321 // flag 2 == update of mask
328 for(i = 0; i < mapvote_count; ++i)
329 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
330 WriteByte(MSG_ENTITY, mapvote_selections[i]);
332 WriteByte(MSG_ENTITY, to.mapvote);
340 Net_LinkEntity(mapvote_ent = spawn(), false, 0, MapVote_SendEntity);
343 void MapVote_TouchMask()
345 mapvote_ent.SendFlags |= 2;
348 void MapVote_TouchVotes(entity voter)
350 mapvote_ent.SendFlags |= 4;
353 float MapVote_Finished(float mappos)
355 if(alreadychangedlevel)
362 if(autocvar_sv_eventlog)
364 result = strcat(":vote:finished:", mapvote_maps[mappos]);
365 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
366 didntvote = mapvote_voters;
367 for(i = 0; i < mapvote_count; ++i)
368 if(mapvote_maps_availability[i] == GTV_AVAILABLE )
370 didntvote -= mapvote_selections[i];
373 result = strcat(result, ":", mapvote_maps[i]);
374 result = strcat(result, ":", ftos(mapvote_selections[i]));
377 result = strcat(result, ":didn't vote:", ftos(didntvote));
380 if(mapvote_maps_suggested[mappos])
381 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
384 FOR_EACH_REALCLIENT(other)
385 FixClientCvars(other);
389 if ( GameTypeVote_Finished(mappos) )
391 gametypevote = false;
392 if(autocvar_nextmap != "")
394 Map_Goto_SetStr(autocvar_nextmap);
396 alreadychangedlevel = true;
405 Map_Goto_SetStr(mapvote_maps[mappos]);
407 alreadychangedlevel = true;
412 void MapVote_CheckRules_1()
416 for(i = 0; i < mapvote_count; ++i)
417 if( mapvote_maps_availability[i] == GTV_AVAILABLE )
419 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
420 mapvote_selections[i] = 0;
424 FOR_EACH_REALCLIENT(other)
429 i = other.mapvote - 1;
430 //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
431 mapvote_selections[i] = mapvote_selections[i] + 1;
436 float MapVote_CheckRules_2()
439 float firstPlace, secondPlace, currentPlace;
440 float firstPlaceVotes, secondPlaceVotes, currentVotes;
441 float mapvote_voters_real;
444 if(mapvote_count_real == 1)
445 return MapVote_Finished(0);
447 mapvote_voters_real = mapvote_voters;
449 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
451 RandomSelection_Init();
454 for(i = 0; i < mapvote_count_real; ++i)
455 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
457 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
458 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
460 currentVotes = mapvote_selections[i];
464 firstPlaceVotes = RandomSelection_best_priority;
465 if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
466 firstPlace = currentPlace;
468 firstPlace = RandomSelection_chosen_float;
470 //dprint("First place: ", ftos(firstPlace), "\n");
471 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
473 RandomSelection_Init();
474 for(i = 0; i < mapvote_count_real; ++i)
476 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
477 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
478 secondPlace = RandomSelection_chosen_float;
479 secondPlaceVotes = RandomSelection_best_priority;
480 //dprint("Second place: ", ftos(secondPlace), "\n");
481 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
484 error("No first place in map vote... WTF?");
486 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
487 return MapVote_Finished(firstPlace);
489 if(mapvote_keeptwotime)
490 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
494 mapvote_message = "Now decide between the TOP TWO!";
495 mapvote_keeptwotime = 0;
496 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
497 result = strcat(result, ":", ftos(firstPlaceVotes));
498 result = strcat(result, ":", mapvote_maps[secondPlace]);
499 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
500 didntvote = mapvote_voters;
501 for(i = 0; i < mapvote_count; ++i)
503 didntvote -= mapvote_selections[i];
507 result = strcat(result, ":", mapvote_maps[i]);
508 result = strcat(result, ":", ftos(mapvote_selections[i]));
509 if(i < mapvote_count_real)
511 mapvote_maps_availability[i] = GTV_FORBIDDEN;
515 result = strcat(result, ":didn't vote:", ftos(didntvote));
516 if(autocvar_sv_eventlog)
528 keeptwo = mapvote_keeptwotime;
529 MapVote_CheckRules_1(); // count
530 if(MapVote_CheckRules_2()) // decide
534 FOR_EACH_REALCLIENT(other)
536 // hide scoreboard again
537 if(other.health != 2342)
541 if(IS_REAL_CLIENT(other))
544 WriteByte(MSG_ONE, SVC_FINALE);
545 WriteString(MSG_ONE, "");
549 // clear possibly invalid votes
550 if ( mapvote_maps_availability[other.mapvote-1] != GTV_AVAILABLE )
552 // use impulses as new vote
553 if(other.impulse >= 1 && other.impulse <= mapvote_count)
554 if( mapvote_maps_availability[other.impulse - 1] == GTV_AVAILABLE )
556 other.mapvote = other.impulse;
557 MapVote_TouchVotes(other);
565 MapVote_CheckRules_1(); // just count
570 // if mapvote is already running, don't do this initialization again
571 if(mapvote_run) { return; }
573 // don't start mapvote until after playerstats gamereport is sent
574 if(PlayerStats_GameReport_DelayMapVote) { return; }
577 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
586 if(alreadychangedlevel)
589 if(time < mapvote_nextthink)
593 mapvote_nextthink = time + 0.5;
595 if(!mapvote_initialized)
597 if(autocvar_rescan_pending == 1)
599 cvar_set("rescan_pending", "2");
600 localcmd("fs_rescan\nrescan_pending 3\n");
603 else if(autocvar_rescan_pending == 2)
607 else if(autocvar_rescan_pending == 3)
609 // now build missing mapinfo files
610 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
613 // we're done, start the timer
614 cvar_set("rescan_pending", "0");
617 mapvote_initialized = true;
618 if(DoNextMapOverride(0))
620 if(!autocvar_g_maplist_votable || player_count <= 0)
626 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
627 else if(autocvar_nextmap == "") { MapVote_Init(); }
633 float GameTypeVote_SetGametype(float type)
635 if (MapInfo_CurrentGametype() == type)
638 float tsave = MapInfo_CurrentGametype();
640 MapInfo_SwitchGameType(type);
643 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
644 if(MapInfo_count > 0)
646 // update lsmaps in case the gametype changed, this way people can easily list maps for it
647 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
648 lsmaps_reply = strzone(getlsmaps());
649 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
653 bprint("Cannot use this game type: no map for it found\n");
654 MapInfo_SwitchGameType(tsave);
655 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
659 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
661 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
662 if(autocvar_g_maplist_shuffle)
668 float gametypevote_finished;
669 float GameTypeVote_Finished(float pos)
671 if(!gametypevote || gametypevote_finished)
674 if ( !GameTypeVote_SetGametype(GameTypeVote_Type_FromString(mapvote_maps[pos])) )
676 dprint("Selected gametype is not supported by any map");
679 localcmd("sv_vote_gametype_hook_all\n");
680 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
682 gametypevote_finished = true;
687 float GameTypeVote_AddVotable(string nextMode)
690 if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == 0 )
692 for(j = 0; j < mapvote_count; ++j)
693 if(mapvote_maps[j] == nextMode)
696 mapvote_maps[mapvote_count] = strzone(nextMode);
697 mapvote_maps_suggested[mapvote_count] = false;
699 mapvote_maps_screenshot_dir[mapvote_count] = 0;
700 mapvote_maps_pakfile[mapvote_count] = strzone("");
701 mapvote_maps_availability[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
709 float GameTypeVote_Start()
712 MapVote_ClearAllVotes();
713 MapVote_UnzoneStrings();
716 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
718 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
720 float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
721 n = min(MAPVOTE_COUNT, n);
723 float really_available, which_available;
724 really_available = 0;
725 which_available = -1;
726 for(j = 0; j < n; ++j)
728 if ( GameTypeVote_AddVotable(argv(j)) )
729 if ( mapvote_maps_availability[j] == GTV_AVAILABLE )
736 mapvote_count_real = mapvote_count;
740 if ( really_available == 0 )
742 if ( mapvote_count > 0 )
743 strunzone(mapvote_maps[0]);
744 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
745 //GameTypeVote_Finished(0);
749 if ( really_available == 1 )
751 //GameTypeVote_Finished(which_available);
752 MapVote_Finished(which_available);
756 mapvote_count_real = mapvote_count;
758 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
759 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
760 mapvote_keeptwotime = 0;