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_availability[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 float GameTypeVote_AvailabilityStatus(string gtname)
56 float type = GameTypeVote_Type_FromString(gtname);
60 if ( autocvar_nextmap != "" )
62 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, 0) )
64 if (!(MapInfo_Map_supportedGametypes & type))
71 float GameTypeVote_GetMask()
73 float n, j, gametype_mask;
74 n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
75 n = min(MAPVOTE_COUNT, n);
77 for(j = 0; j < n; ++j)
78 gametype_mask |= GameTypeVote_Type_FromString(argv(j));
82 string GameTypeVote_MapInfo_FixName(string m)
84 if ( autocvar_sv_vote_gametype )
87 MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
89 return MapInfo_FixName(m);
92 void MapVote_ClearAllVotes()
94 FOR_EACH_CLIENT(other)
98 void MapVote_UnzoneStrings()
101 for(j = 0; j < mapvote_count; ++j)
103 if ( mapvote_maps[j] )
105 strunzone(mapvote_maps[j]);
106 mapvote_maps[j] = string_null;
108 if ( mapvote_maps_pakfile[j] )
110 strunzone(mapvote_maps_pakfile[j]);
111 mapvote_maps_pakfile[j] = string_null;
116 string MapVote_Suggest(string m)
120 return "That's not how to use this command.";
121 if(!autocvar_g_maplist_votable_suggestions)
122 return "Suggestions are not accepted on this server.";
123 if(mapvote_initialized)
125 return "Can't suggest - voting is already in progress!";
126 m = GameTypeVote_MapInfo_FixName(m);
128 return "The map you suggested is not available on this server.";
129 if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
131 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
133 if (!autocvar_sv_vote_gametype)
134 if(!MapInfo_CheckMap(m))
135 return "The map you suggested does not support the current game mode.";
136 for(i = 0; i < mapvote_suggestion_ptr; ++i)
137 if(mapvote_suggestions[i] == m)
138 return "This map was already suggested.";
139 if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
141 i = floor(random() * mapvote_suggestion_ptr);
145 i = mapvote_suggestion_ptr;
146 mapvote_suggestion_ptr += 1;
148 if(mapvote_suggestions[i] != "")
149 strunzone(mapvote_suggestions[i]);
150 mapvote_suggestions[i] = strzone(m);
151 if(autocvar_sv_eventlog)
152 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid)));
153 return strcat("Suggestion of ", m, " accepted.");
156 void MapVote_AddVotable(string nextMap, float isSuggestion)
159 string pakfile, mapfile;
163 for(j = 0; j < mapvote_count; ++j)
164 if(mapvote_maps[j] == nextMap)
166 // suggestions might be no longer valid/allowed after gametype switch!
168 if(!MapInfo_CheckMap(nextMap))
170 mapvote_maps[mapvote_count] = strzone(nextMap);
171 mapvote_maps_suggested[mapvote_count] = isSuggestion;
173 pakfile = string_null;
174 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
176 mapfile = strcat(mapvote_screenshot_dirs[i], "/", mapvote_maps[i]);
177 pakfile = whichpack(strcat(mapfile, ".tga"));
179 pakfile = whichpack(strcat(mapfile, ".jpg"));
181 pakfile = whichpack(strcat(mapfile, ".png"));
185 if(i >= mapvote_screenshot_dirs_count)
186 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?
187 for(o = strstr(pakfile, "/", 0)+1; o > 0; o = strstr(pakfile, "/", 0)+1)
188 pakfile = substring(pakfile, o, -1);
190 mapvote_maps_screenshot_dir[mapvote_count] = i;
191 mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
192 mapvote_maps_availability[mapvote_count] = GTV_AVAILABLE;
202 MapVote_ClearAllVotes();
203 MapVote_UnzoneStrings();
206 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
207 mapvote_abstain = autocvar_g_maplist_votable_abstain;
210 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
212 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
213 smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
215 // we need this for AddVotable, as that cycles through the screenshot dirs
216 mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
217 if(mapvote_screenshot_dirs_count == 0)
218 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
219 mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
220 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
221 mapvote_screenshot_dirs[i] = strzone(argv(i));
223 if(mapvote_suggestion_ptr)
224 for(i = 0; i < 100 && mapvote_count < smax; ++i)
225 MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], true);
227 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
228 MapVote_AddVotable(GetNextMap(), false);
230 if(mapvote_count == 0)
232 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
233 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
234 if(autocvar_g_maplist_shuffle)
236 localcmd("\nmenu_cmd sync\n");
237 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
238 MapVote_AddVotable(GetNextMap(), false);
241 mapvote_count_real = mapvote_count;
243 MapVote_AddVotable("don't care", 0);
245 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
247 mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
248 mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
249 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
250 mapvote_keeptwotime = 0;
251 mapvote_message = "Choose a map and press its key!";
256 void MapVote_SendPicture(float id)
259 WriteByte(MSG_ONE, SVC_TEMPENTITY);
260 WriteByte(MSG_ONE, TE_CSQC_PICTURE);
261 WriteByte(MSG_ONE, id);
262 WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
266 void MapVote_WriteMask()
269 if ( mapvote_count < 24 )
273 for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
274 if(mapvote_maps_availability[i] == GTV_AVAILABLE )
277 if(mapvote_count < 8)
278 WriteByte(MSG_ENTITY, mask);
279 else if (mapvote_count < 16)
280 WriteShort(MSG_ENTITY,mask);
282 WriteLong(MSG_ENTITY, mask);
286 for ( i = 0; i < mapvote_count; ++i )
287 WriteByte(MSG_ENTITY, mapvote_maps_availability[i]);
291 float MapVote_SendEntity(entity to, int sf)
296 sf &= ~2; // if we send 1, we don't need to also send 2
298 WriteByte(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
299 WriteByte(MSG_ENTITY, sf);
303 // flag 1 == initialization
304 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
305 WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
306 WriteString(MSG_ENTITY, "");
307 WriteByte(MSG_ENTITY, mapvote_count);
308 WriteByte(MSG_ENTITY, mapvote_abstain);
309 WriteByte(MSG_ENTITY, mapvote_detail);
310 WriteCoord(MSG_ENTITY, mapvote_timeout);
315 WriteByte(MSG_ENTITY, 1);
316 WriteString(MSG_ENTITY, autocvar_nextmap);
318 else if ( autocvar_sv_vote_gametype )
320 // map vote but gametype has been chosen via voting screen
321 WriteByte(MSG_ENTITY, 2);
322 WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
325 WriteByte(MSG_ENTITY, 0); // map vote
329 for(i = 0; i < mapvote_count; ++i)
331 if(mapvote_abstain && i == mapvote_count - 1)
333 WriteString(MSG_ENTITY, ""); // abstain needs no text
334 WriteString(MSG_ENTITY, ""); // abstain needs no pack
335 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
336 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
340 WriteString(MSG_ENTITY, mapvote_maps[i]);
341 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
342 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
343 WriteByte(MSG_ENTITY, mapvote_maps_availability[i]);
350 // flag 2 == update of mask
357 for(i = 0; i < mapvote_count; ++i)
358 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
359 WriteByte(MSG_ENTITY, mapvote_selections[i]);
361 WriteByte(MSG_ENTITY, to.mapvote);
369 Net_LinkEntity(mapvote_ent = spawn(), false, 0, MapVote_SendEntity);
372 void MapVote_TouchMask()
374 mapvote_ent.SendFlags |= 2;
377 void MapVote_TouchVotes(entity voter)
379 mapvote_ent.SendFlags |= 4;
382 float MapVote_Finished(float mappos)
384 if(alreadychangedlevel)
391 if(autocvar_sv_eventlog)
393 result = strcat(":vote:finished:", mapvote_maps[mappos]);
394 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
395 didntvote = mapvote_voters;
396 for(i = 0; i < mapvote_count; ++i)
397 if(mapvote_maps_availability[i] == GTV_AVAILABLE )
399 didntvote -= mapvote_selections[i];
402 result = strcat(result, ":", mapvote_maps[i]);
403 result = strcat(result, ":", ftos(mapvote_selections[i]));
406 result = strcat(result, ":didn't vote:", ftos(didntvote));
409 if(mapvote_maps_suggested[mappos])
410 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
413 FOR_EACH_REALCLIENT(other)
414 FixClientCvars(other);
418 if ( GameTypeVote_Finished(mappos) )
420 gametypevote = false;
421 if(autocvar_nextmap != "")
423 Map_Goto_SetStr(autocvar_nextmap);
425 alreadychangedlevel = true;
434 Map_Goto_SetStr(mapvote_maps[mappos]);
436 alreadychangedlevel = true;
441 void MapVote_CheckRules_1()
445 for(i = 0; i < mapvote_count; ++i)
446 if( mapvote_maps_availability[i] == GTV_AVAILABLE )
448 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
449 mapvote_selections[i] = 0;
453 FOR_EACH_REALCLIENT(other)
458 i = other.mapvote - 1;
459 //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
460 mapvote_selections[i] = mapvote_selections[i] + 1;
465 float MapVote_CheckRules_2()
468 float firstPlace, secondPlace, currentPlace;
469 float firstPlaceVotes, secondPlaceVotes, currentVotes;
470 float mapvote_voters_real;
473 if(mapvote_count_real == 1)
474 return MapVote_Finished(0);
476 mapvote_voters_real = mapvote_voters;
478 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
480 RandomSelection_Init();
483 for(i = 0; i < mapvote_count_real; ++i)
484 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
486 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
487 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
489 currentVotes = mapvote_selections[i];
493 firstPlaceVotes = RandomSelection_best_priority;
494 if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
495 firstPlace = currentPlace;
497 firstPlace = RandomSelection_chosen_float;
499 //dprint("First place: ", ftos(firstPlace), "\n");
500 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
502 RandomSelection_Init();
503 for(i = 0; i < mapvote_count_real; ++i)
505 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
506 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
507 secondPlace = RandomSelection_chosen_float;
508 secondPlaceVotes = RandomSelection_best_priority;
509 //dprint("Second place: ", ftos(secondPlace), "\n");
510 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
513 error("No first place in map vote... WTF?");
515 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
516 return MapVote_Finished(firstPlace);
518 if(mapvote_keeptwotime)
519 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
523 mapvote_message = "Now decide between the TOP TWO!";
524 mapvote_keeptwotime = 0;
525 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
526 result = strcat(result, ":", ftos(firstPlaceVotes));
527 result = strcat(result, ":", mapvote_maps[secondPlace]);
528 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
529 didntvote = mapvote_voters;
530 for(i = 0; i < mapvote_count; ++i)
532 didntvote -= mapvote_selections[i];
536 result = strcat(result, ":", mapvote_maps[i]);
537 result = strcat(result, ":", ftos(mapvote_selections[i]));
538 if(i < mapvote_count_real)
540 mapvote_maps_availability[i] = GTV_FORBIDDEN;
544 result = strcat(result, ":didn't vote:", ftos(didntvote));
545 if(autocvar_sv_eventlog)
557 keeptwo = mapvote_keeptwotime;
558 MapVote_CheckRules_1(); // count
559 if(MapVote_CheckRules_2()) // decide
563 FOR_EACH_REALCLIENT(other)
565 // hide scoreboard again
566 if(other.health != 2342)
570 if(IS_REAL_CLIENT(other))
573 WriteByte(MSG_ONE, SVC_FINALE);
574 WriteString(MSG_ONE, "");
578 // clear possibly invalid votes
579 if ( mapvote_maps_availability[other.mapvote-1] != GTV_AVAILABLE )
581 // use impulses as new vote
582 if(other.impulse >= 1 && other.impulse <= mapvote_count)
583 if( mapvote_maps_availability[other.impulse - 1] == GTV_AVAILABLE )
585 other.mapvote = other.impulse;
586 MapVote_TouchVotes(other);
594 MapVote_CheckRules_1(); // just count
599 // if mapvote is already running, don't do this initialization again
600 if(mapvote_run) { return; }
602 // don't start mapvote until after playerstats gamereport is sent
603 if(PlayerStats_GameReport_DelayMapVote) { return; }
606 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
615 if(alreadychangedlevel)
618 if(time < mapvote_nextthink)
622 mapvote_nextthink = time + 0.5;
624 if(!mapvote_initialized)
626 if(autocvar_rescan_pending == 1)
628 cvar_set("rescan_pending", "2");
629 localcmd("fs_rescan\nrescan_pending 3\n");
632 else if(autocvar_rescan_pending == 2)
636 else if(autocvar_rescan_pending == 3)
638 // now build missing mapinfo files
639 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
642 // we're done, start the timer
643 cvar_set("rescan_pending", "0");
646 mapvote_initialized = true;
647 if(DoNextMapOverride(0))
649 if(!autocvar_g_maplist_votable || player_count <= 0)
655 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
656 else if(autocvar_nextmap == "") { MapVote_Init(); }
662 float GameTypeVote_SetGametype(float type)
664 if (MapInfo_CurrentGametype() == type)
667 float tsave = MapInfo_CurrentGametype();
669 MapInfo_SwitchGameType(type);
672 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
673 if(MapInfo_count > 0)
675 // update lsmaps in case the gametype changed, this way people can easily list maps for it
676 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
677 lsmaps_reply = strzone(getlsmaps());
678 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
682 bprint("Cannot use this game type: no map for it found\n");
683 MapInfo_SwitchGameType(tsave);
684 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
688 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
690 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
691 if(autocvar_g_maplist_shuffle)
697 float gametypevote_finished;
698 float GameTypeVote_Finished(float pos)
700 if(!gametypevote || gametypevote_finished)
703 if ( !GameTypeVote_SetGametype(GameTypeVote_Type_FromString(mapvote_maps[pos])) )
705 dprint("Selected gametype is not supported by any map");
708 localcmd("sv_vote_gametype_hook_all\n");
709 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
711 gametypevote_finished = true;
716 float GameTypeVote_AddVotable(string nextMode)
719 if ( nextMode == "" || GameTypeVote_Type_FromString(nextMode) == 0 )
721 for(j = 0; j < mapvote_count; ++j)
722 if(mapvote_maps[j] == nextMode)
725 mapvote_maps[mapvote_count] = strzone(nextMode);
726 mapvote_maps_suggested[mapvote_count] = false;
728 mapvote_maps_screenshot_dir[mapvote_count] = 0;
729 mapvote_maps_pakfile[mapvote_count] = strzone("");
730 mapvote_maps_availability[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
738 float GameTypeVote_Start()
741 MapVote_ClearAllVotes();
742 MapVote_UnzoneStrings();
745 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
747 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
749 float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
750 n = min(MAPVOTE_COUNT, n);
752 float really_available, which_available;
753 really_available = 0;
754 which_available = -1;
755 for(j = 0; j < n; ++j)
757 if ( GameTypeVote_AddVotable(argv(j)) )
758 if ( mapvote_maps_availability[j] == GTV_AVAILABLE )
765 mapvote_count_real = mapvote_count;
769 if ( really_available == 0 )
771 if ( mapvote_count > 0 )
772 strunzone(mapvote_maps[0]);
773 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
774 //GameTypeVote_Finished(0);
778 if ( really_available == 1 )
780 //GameTypeVote_Finished(which_available);
781 MapVote_Finished(which_available);
785 mapvote_count_real = mapvote_count;
787 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
788 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
789 mapvote_keeptwotime = 0;