//IMPORTANT NOTE:
//Call once at the start of the level
init_level_variables()
{
game["menu_castvote"] = "callvote_castmenu";
game["menu_callvote"] = "callvote_mainmenu";
precacheMenu(game["menu_castvote"]);
precacheMenu(game["menu_callvote"]);
level.cv_spechud_votestring = &"^3VOTE (";
level.cv_spechud_voteyesstring = &"^3YES:";
level.cv_spechud_votenostring = &"^3, NO: ";
level.cv_votes_localizedstrings = [];
level.cv_votes_localizedstrings["restartMap"] = &"^3): Restart map";
level.cv_votes_localizedstrings["nextMap"] = &"^3): Next map";
level.cv_votes_localizedstrings["changeMap"] = &"^3): Change map";
level.cv_votes_localizedstrings["changeTypeMap"] = &"^3): Change gametype & map";
level.cv_votes_localizedstrings["changeGametype"] = &"^3): Change Gametype";
precacheString(level.cv_spechud_votestring);
precacheString(level.cv_spechud_voteyesstring);
precacheString(level.cv_spechud_votenostring);
precacheString(level.cv_votes_localizedstrings["restartMap"]);
precacheString(level.cv_votes_localizedstrings["nextMap"]);
precacheString(level.cv_votes_localizedstrings["changeMap"]);
precacheString(level.cv_votes_localizedstrings["changeTypeMap"]);
precacheString(level.cv_votes_localizedstrings["changeGametype"]);
setCvar("cv_inProgress", "0");
//Prefix all vote-related level variables with cv (short for callvote)
level.cv_amnt_of_slots = 6;
level.cv_gametypes = [];
level.cv_vote_inprogress = false;
level.cv_vote_updatetime = 1;
level.cv_allowVoteChanges = false;
level.cv_vote_cvar = "cv_votestring";
level.cv_intermediate_results_cvar = "cv_scorestring";
level.cv_optional_third_cvar = "cg_optionalstring";
level.cv_filter_cvar = "callvote_filter";
level.cv_time_between_votes = 80;
//Build the main list of all gametypes
for(i=0;i<getCvarInt("cv_gametypes_amnt");i++)
{
type = getCvar("cv_gametype" + i);
level.cv_gametypes[i] = makeGametypeNode(type, getCvar("cv_gametype" + i + "_name"), buildMapList(type, ""));
}
}
//#######-----------------------------------------------------------------------#######//
//#######- Main thread. Handles all menuresponses handled by the voting system -#######//
//#######-----------------------------------------------------------------------#######//
//Called in Callback_Playerconnect in _ufacmds for every player.
init()
{
if(!isDefined(game["menu_castvote"]))
return;
//Initialize variables
self.cv_list_of_gametypes = [];
self.cv_list_of_maps = [];
self.cv_selected_gametype = 0;
self.cv_selected_map = 0;
self.cv_filter_string = "";
oldfilter = "";
filter_string_max = 15;
self.cv_apply_filter = false;
current_page_gametypes = 0;
current_page_maps = 0;
current_gametype = getCvar("g_gametype");
while(1)
{
//Deal with every possible menuresponse
self waittill("menuresponse", menu, response);
//Add an exception for other menus (e.g. wm_quickmessage) so we don't block out vote_yes and vote_no responses
if(menu != game["menu_callvote"] && menu != game["menu_castvote"] && response != "vote_yes" && response != "vote_no")
continue;
//Check if we're selecting a gametype. Doing it this way so we don't need a different case in the switch for every number
if(maps\mp\_quicksearch::isSubStr("gametypes_select", response))
{
selection_slot = (int)response[16];
response = "gametypes_select";
}
else if(maps\mp\_quicksearch::isSubStr("maplist_select", response))
{
selection_slot = (int)response[14];
response = "maplist_select";
}
else if(maps\mp\_quicksearch::isSubStr("cv_kp_", response))
{
key = response[6];
response = "keypress";
}
//Menu logic. Opening, closing, pages, filter, etc
switch(response)
{
case "open":
if(menu == game["menu_callvote"])
clearSlots();
break;
//Close menu
case "close":
if(menu == game["menu_castvote"])
self.cv_menuOpened = false;
else if(menu == game["menu_callvote"])
self.cv_apply_filter = false;
break;
//Yesvote
case "vote_yes":
self registerVote("yes");
break;
//Novote
case "vote_no":
self registerVote("no");
break;
case "change_vote":
self.cv_voteCast = false;
self setClientCvar("cv_allowchangevote", 0);
self.cv_canVote[0] = true;
self.cv_canVote[1] = "You can't vote (unknown reason)";
self.cv_voteDecision = "maybe";
break;
case "start_filter":
//If we're already applying a filter, clear it. This means we can click the filter-button again and it will clear every input
if(self.cv_apply_filter)
{
//Clear the strings
self setClientCvar(level.cv_filter_cvar, "");
self.cv_filter_string = "";
oldfilter = "";
//Generate the original map- and gametype list
self.cv_list_of_maps = getGametypeMaplist(self.cv_list_of_gametypes, self.cv_selected_gametype);
current_page_maps = self fillMapSlots(self.cv_list_of_maps, level.cv_amnt_of_slots, 0);
self.cv_selected_map = self.cv_selected_map % level.cv_amnt_of_slots;
}
//Else if we aren't filtering yet, allow a filter to be applied
else
{
self.cv_apply_filter = true;
self thread showVisualIndicator();
}
break;
case "keypress":
//If we didn't activate the filter mode, don't accept keypresses
if(!self.cv_apply_filter)
break;
//Exceptions for special keys
if(key == "0")
key = "backspace";
else if(key == "9")
key = "_";
//Build the new string
if(key == "backspace")
{
//Double if, because else we might add "backspace" to our self.cv_filter_string in some special cases
if(self.cv_filter_string.size > 0)
self.cv_filter_string = removeLastChar(self.cv_filter_string);
}
//If we're not at the maximum length, add the character
else if(self.cv_filter_string.size < filter_string_max)
self.cv_filter_string = self.cv_filter_string + key;
//Display the new string
self setClientCvar(level.cv_filter_cvar, self.cv_filter_string);
//Apply the filter. If we already had a filter applied and the oldfilter is a substring of the new filter,
//The filtered-out maps of the old filter will still be filtered out in the new filter, thus we can apply the filter on the already filtered list
if(isDefined(oldfilter) && maps\mp\_quicksearch::isSubStr(oldfilter, self.cv_filter_string))
self.cv_list_of_maps = filterMapList(self.cv_list_of_maps, self.cv_filter_string);
else //Else, filter a newly generated list
self.cv_list_of_maps = filterMapList(getGametypeMaplist(self.cv_list_of_gametypes, self.cv_selected_gametype), self.cv_filter_string);
oldfilter = self.cv_filter_string;
//Fill the slots with the new list of maps
current_page_maps = self fillMapSlots(self.cv_list_of_maps, level.cv_amnt_of_slots, 0);
//Make sure we select the right slot
self.cv_selected_map = self.cv_selected_map % level.cv_amnt_of_slots;
break;
case "open_changegametype":
//Build a list of all gametypes, with the current gametype up front
self.cv_list_of_gametypes = buildGametypeList(level.cg_gametypes, current_gametype);
//Build a list of all maps
self.cv_list_of_maps = getMapListFromGametype(self.cv_list_of_gametypes, current_gametype);
//Display page 1 of list of gametypes
self fillGametypeSlots(self.cv_list_of_gametypes, level.cv_amnt_of_slots, current_page_gametypes);
//Display page 1 of list of maps
self fillMapSlots(self.cv_list_of_maps, level.cv_amnt_of_slots, current_page_maps);
//Select the first slots by default
self.cv_selected_gametype = 0;
self.cv_selected_map = 0;
//Reset the filters
self.cv_apply_filter = false;
self.cv_filter_string = "";
oldfilter = "";
break;
case "gametypes_prevpage":
oldpage = current_page_gametypes;
//fillGametypeSlots returns the number of the new page
current_page_gametypes = self fillGametypeSlots(self.cv_list_of_gametypes, level.cv_amnt_of_slots, (current_page_gametypes - 1));
if(oldpage > current_page_gametypes)
self.cv_selected_gametype = self.cv_selected_gametype - level.cv_amnt_of_slots;
break;
case "gametypes_nextpage":
oldpage = current_page_gametypes;
current_page_gametypes = self fillGametypeSlots(self.cv_list_of_gametypes, level.cv_amnt_of_slots, (current_page_gametypes + 1));
if(oldpage < current_page_gametypes)
self.cv_selected_gametype = self.cv_selected_gametype + level.cv_amnt_of_slots;
break;
case "maplist_prevpage":
oldpage = current_page_maps;
current_page_maps = self fillMapSlots(self.cv_list_of_maps, level.cv_amnt_of_slots, (current_page_maps - 1));
if(oldpage > current_page_maps)
self.cv_selected_map = self.cv_selected_map - level.cv_amnt_of_slots;
break;
case "maplist_nextpage":
oldpage = current_page_maps;
current_page_maps = self fillMapSlots(self.cv_list_of_maps, level.cv_amnt_of_slots, (current_page_maps + 1));
if(oldpage < current_page_maps)
self.cv_selected_map = self.cv_selected_map + level.cv_amnt_of_slots;
break;
case "gametypes_select":
self.cv_selected_gametype = (current_page_gametypes * level.cv_amnt_of_slots) + selection_slot;
// Check if the player selected a valid gametype. If yes, change the map list. If no, empty the maplist
if(self.cv_selected_gametype < self.cv_list_of_gametypes.size)
{
self.cv_list_of_maps = getGametypeMaplist(self.cv_list_of_gametypes, self.cv_selected_gametype);
//Apply the current set filter on the map list
if(self.cv_filter_string != "")
self.cv_list_of_maps = filterMapList(self.cv_list_of_maps, self.cv_filter_string);
//Fill the slots with the new maps
current_page_maps = self fillMapSlots(self.cv_list_of_maps, level.cv_amnt_of_slots, 0);
//Select the highlighted map
self.cv_selected_map = self.cv_selected_map % level.cv_amnt_of_slots;
}
else // Player didn't select an actual gametype, i.e. an empty slot
{
self clearMapSlots();
self.cv_selected_gametype = -1;
self.cv_list_of_maps = [];
}
break;
case "maplist_select":
self.cv_selected_map = (current_page_maps * level.cv_amnt_of_slots) + selection_slot;
break;
}
//Actual votes logic
switch(response)
{
case "typemap":
gametype = current_gametype;
mapname = getCvar("mapname");
//If the selected gametype is out of bounds (i.e. it was a blank slot), make sure we immediately know this is the case
if(self.cv_selected_gametype >= self.cv_list_of_gametypes.size)
self.cv_selected_gametype = -1;
//If the selected slot is blank, make sure we know this is the case
if(self.cv_selected_map >= self.cv_list_of_maps.size)
self.cv_selected_map = -1;
//If the played selected a valid gametype, get the name of the gametype
if(self.cv_selected_gametype != -1)
gametype = getGametypeType(self.cv_list_of_gametypes, self.cv_selected_gametype);
//If the player selected a valid map, get the name of that map
if(self.cv_selected_map != -1)
mapname = self.cv_list_of_maps[self.cv_selected_map];
//If he didn't select a valid gametype or the gametype isn't different from the current one, but he has a valid map, vote that map.
if((self.cv_selected_gametype == -1 || gametype == current_gametype) && self.cv_selected_map != -1)
self thread startVote("changeMap", "Change map: " + mapname, 30);
//If the player selected a valid gametype that isn't the current one and an invalid map, vote only the gametype
else if(self.cv_selected_gametype != -1 && self.cv_selected_gametype != current_gametype && self.cv_selected_map == -1)
self thread startVote("changeGametype", "Gametype: " + gametype, 30);
else if(self.cv_selected_gametype != -1 && self.cv_selected_map != -1)
self thread startVote("changeTypeMap", "Gametype: " + gametype + ", map: " + mapname, 30);
else self iprintlnbold("Sorry, I don't know what to do with your vote");
break;
case "restartmap":
self thread startVote("restartMap", "Restart map", 30);
break;
case "nextmap":
self thread startVote("nextMap", "Next map: " + getCvar("nextmapname"), 30);
break;
}
}
}
// Useful when pressing backspace
removeLastChar(str)
{
newstr = "";
for(i=0;i<str.size;i++)
if(i == str.size - 1)
return newstr;
else
newstr += str[i];
}
showVisualIndicator()
{
blink = true;
while(isDefined(self) && self.cv_apply_filter)
{
wait .8;
if(blink)
self setClientCvar(level.cv_filter_cvar, self.cv_filter_string + "_");
else
self setClientCvar(level.cv_filter_cvar, self.cv_filter_string);
blink = !blink;
}
}
//#######-----------------------------------------------------------------------#######//
//#######------ Vote logic. Starting, stopping votes, counting votes, etc ------#######//
//#######--------------- Basically all logic when a vote is running-------------#######//
//#######-----------------------------------------------------------------------#######//
//Called with self, because certain parameters such as selected map (in case of map change) are saved in player variables
startVote(vote_type, vote_string, vote_time)
{
// Every reason why a player wouldn't be able to start a vote goes here
if(level.cv_vote_inprogress)
{
self iprintln("Vote is already in progress");
return;
}
if(self.pers["team"] == "spectator")
{
self iprintln("Cannot start a vote as a spectator");
return;
}
newtime = (getTime() - level.starttime) / 1000;
if(isDefined(self.cv_lastvote_time) && newtime - self.cv_lastvote_time < level.cv_time_between_votes)
{
self iprintln("Can't start a vote. Please wait a little while.");
return;
}
level.cv_vote_inprogress = true;
setCvar("cv_inProgress", "1");
//Enable the vote controls in the quickcommands menu
players = getentarray("player","classname");
for(i=0;i<players.size;i++)
players[i] initVoteVariables();
//Make sure the vote-starter's vote is already cast to "yes", although he would still be able to change it
self setClientCvar("cv_voteControls","0");
self.cv_voteCast = true;
self.cv_voteDecision = "yes";
self.cv_lastvote_time = newtime;
//Monitor the keyboard for the shortcut to open a vote decision menu
thread monitorKeyboardShortcut();
//Start drawing the vote strings on the screen
premature_end = false;
while(vote_time > 0 && !premature_end)
{
//Count all votes
vote_time = updateVoteTime(vote_time);
countedVotes = countVotes();
yes = getYesVotes(countedVotes);
no = getNoVotes(countedVotes);
maybe = getMaybeVotes(countedVotes);
premature_end = checkEndCondition(yes, no, maybe);
//For every player, print the current vote and vote results on the screen
players = getentarray("player","classname");
for(i=0;i<players.size;i++)
{
//Remove the spectator vote hud, just in case the player goes spec and joins the game again. We want to clean up the hud and replace it with the cvar
players[i] removeSpectatorVoteHud();
if(players[i].pers["team"] == "spectator")
{
if(vote_time == 29 || vote_time % 8 == 0 && vote_time != 0)
self iprintln("^6Current vote: ^7" + vote_string);
players[i] drawSpectatorVoteHud(level.cv_votes_localizedstrings[vote_type], vote_time, yes, no);
}
else
{
players[i] setClientCvar(level.cv_vote_cvar, "^3Vote (" + vote_time + "): " + vote_string);
players[i] setClientCvar(level.cv_intermediate_results_cvar, "^3YES: " + yes + ", NO: " + no + ", Maybe: " + maybe);
}
}
wait level.cv_vote_updatetime;
}
result = calculateResults(yes, no, maybe);
self endVote(vote_type, result);
}
updateVoteTime(vote_time)
{
//Make sure we don't go under zero
if(vote_time <= level.cv_vote_updatetime)
return 0;
else
return vote_time - level.cv_vote_updatetime;
}
checkEndCondition(yes, no, maybe)
{
possible_votes = countPossibleVotes();
if(yes > possible_votes / 2)
return true;
else
return false;
}
//Calculate the result based on the amount of yes/no votes. Returns either true for a pass or false for a fail
calculateResults(yes, no, maybe)
{
possible_votes = countPossibleVotes();
if(yes > possible_votes / 2)
return true;
else
return false;
}
//Count every player that can vote
countPossibleVotes()
{
possible_votes = 0;
players = getentarray("player","classname");
for(i=0;i<players.size;i++)
if(players[i].pers["team"] != "spectator")
possible_votes++;
return possible_votes;
}
//Called with self, the player who called the vote
endVote(vote_type, result)
{
level.cv_vote_inprogress = false;
setCvar("cv_inProgress", "0");
//When a vote ends, clear the vote hud, calculate the result of the vote and end the vote
players = getentarray("player","classname");
for(i=0;i<players.size;i++)
players[i] unloadVoteVariables();
if(result)
iprintln("Vote passed.");
else
{
iprintln("Vote failed.");
return;
}
current_gametypestring = "gametype " + getCvar("g_gametype");
waittime_before_exitlevel = 3;
//Handling different actions for different votes. Add votes here.
switch(vote_type)
{
case "restartMap":
setCvar("sv_maprotation", current_gametypestring + " map " + getCvar("mapname"));
wait waittime_before_exitlevel;
exitLevel(false);
break;
case "nextMap":
setCvar("sv_maprotation", current_gametypestring + " map " + getCvar("nextmapname"));
wait waittime_before_exitlevel;
exitLevel(false);
break;
case "changeMap":
newMapname = self.cv_list_of_maps[self.cv_selected_map];
setCvar("sv_maprotation", current_gametypestring + " map " + newMapname);
wait waittime_before_exitlevel;
exitLevel(false);
break;
case "changeGametype":
newGametype = getGametypeType(self.cv_list_of_gametypes, self.cv_selected_gametype);
setCvar("sv_maprotation", "gametype " + newGametype + " map " + getCvar("mapname"));
wait waittime_before_exitlevel;
exitLevel(false);
break;
case "changeTypeMap":
newMapname = self.cv_list_of_maps[self.cv_selected_map];
newGametype = getGametypeType(self.cv_list_of_gametypes, self.cv_selected_gametype);
setCvar("sv_maprotation", "gametype " + newGametype + " map " + newMapname);
wait waittime_before_exitlevel;
exitLevel(false);
break;
}
}
//Called from the menuresponse-thread in _ufacmds to register votes. Self is the player who sent the vote
RegisterVote(decision)
{
//We need to make sure the new vote system is actually in use, also don't register votes if there is no vote in progress
if(!isDefined(level.cv_vote_inprogress) || !level.cv_vote_inprogress)
return;
//We know a vote is in progress, so check if his vote is legit. If it is, change his decision
if(self canVote())
{
self.cv_voteCast = true;
self.cv_voteDecision = decision;
if(getCvar("cv_allowchangevote") == "0")
self setClientCvar("cv_showexitbutton","1");
}
else
self iprintln(self.cv_canVote[1]);
}
//Make sure the appropriate menu is openend and make sure proper keyboard commands work
monitorKeyboardShortcut()
{
while(level.cv_vote_inprogress)
{
players = getentarray("player","classname");
for(i=0;i<players.size;i++)
{
if(players[i].pers["team"] != "spectator" && !isAlive(players[i]) && players[i] meleebuttonpressed() && players[i] usebuttonpressed() && !players[i].cv_menuOpened)
{
players[i] openMenu(game["menu_castvote"]);
players[i].cv_menuOpened = true;
}
//The closing of the menu is handeled by the init thread that handles all menuresponses
}
wait .1;
}
}
initVoteVariables()
{
//Tell the menu files the player needs the vote controls (i.e. yes/no menus)
self setClientCvar("cv_voteControls","1");
//Replace viewmap menu with votemenu
self setClientCvar("g_scriptQuickMap", game["menu_castvote"]);
//Allow the player to change his vote once.
self setClientCvar("cv_allowchangevote", "1");
self setClientCvar("cv_showexitbutton","0");
//Setup player variables so the player is allowed to vote
self.cv_voteCast = false;
self.cv_voteDecision = "maybe";
self.cv_menuOpened = false;
self.cv_canVote = [];
self.cv_canVote[0] = true;
self.cv_canVote[1] = "You can't vote (unknown reason)";
self.cv_lastvote_time = (getTime() - level.starttime) / 1000;
}
unloadVoteVariables()
{
//un-draw spectator hud if applicable
self removeSpectatorVoteHud();
//Remove voting entries from menus
self setClientCvar("cv_voteControls","0");
//Reset viewmap button
self setClientCvar("g_scriptQuickMap", game["menu_viewmap"]);
self setClientCvar(level.cv_vote_cvar, " ");
self setClientCvar(level.cv_intermediate_results_cvar, " ");
self setClientCvar(level.cv_optional_third_cvar, " ");
//Set it so the player can cast a vote again. This shouldn't technically be here because it's in the init thread too
self.cv_voteCast = false;
self.cv_canVote = [];
self.cv_canVote[0] = true;
self.cv_canVote[1] = "You can't vote (unknown reason)";
}
//Count all votes by iterating over the players list and looking at the player.cv_voteDecision variable
countVotes()
{
countedVotes = [];
countedVotes[0] = 0;
countedVotes[1] = 0;
countedVotes[2] = 0;
players = getentarray("player","classname");
for(i=0;i<players.size;i++)
{
//Make sure our player is properly "initialized" when he wasn't around when the vote started
if(!isDefined(players[i].cv_voteCast))
{
players[i] initVoteVariables();
countedVotes[2]++;
continue;
}
//Try to invalidate the vote to disallow cheating
if(players[i].pers["team"] == "spectator" && players[i].cv_voteCast && players[i].cv_voteDecision != "maybe")
{
players[i] iprintln("Your vote has been invalidated.");
players[i].cv_voteDecision = "maybe";
}
else if(players[i].cv_voteCast && players[i].cv_voteDecision == "yes")
countedVotes[0]++;
else if(players[i].cv_voteCast && players[i].cv_voteDecision == "no")
countedVotes[1]++;
else countedVotes[2]++;
}
return countedVotes;
}
canVote()
{
if(!isDefined(self.cv_voteCast))
self.cv_voteCast = false;
if(isDefined(self.cv_canVote) && !self.cv_canVote[0])
return true;
//Self is a player. We don't want spectators voting
if(self.pers["team"] != "spectator" && !self.cv_voteCast)
{
//If cv_voteCast isn't defined or it's false, the player can clearly vote
if(!self.cv_voteCast)
return true;
//If we allow votechanges, the player can also vote
else if(level.cv_allowVoteChanges)
return true;
}
//If we didn't return true up until now, it's probably false. So try to set a reason.
//You can't vote... "because ..."
self.cv_canVote[0] = false;
if(self.pers["team"] == "spectator")
self.cv_canVote[1] = "You cannot vote as a spectator";
else if(self.cv_voteCast)
self.cv_canVote[1] = "Vote already cast.";
else self.cv_canVote[1] = "You can't vote (no reason in particular)";
return false;
}
getYesVotes(countedVotes)
{
return countedVotes[0];
}
getNoVotes(countedVotes)
{
return countedVotes[1];
}
getMaybeVotes(countedVotes)
{
return countedVotes[2];
}
//#######-----------------------------------------------------------------------#######//
//#######---------------------- Draw the hud for spectators --------------------#######//
//#######-----------------------------------------------------------------------#######//
drawSpectatorVoteHud(votestring, time, yes, no)
{
vote_string_height = 196;
vote_result_height = 205;
// Draw vote string
self.cv_votestring = newHudElem();
self.cv_votestring.x = 8;
self.cv_votestring.y = vote_string_height;
self.cv_votestring.alignX = "left";
self.cv_votestring.alignY = "bottom";
self.cv_votestring.alpha = 1;
self.cv_votestring.fontScale = .8;
self.cv_votestring setText(level.cv_spechud_votestring);
time_x = 41;
//Draw the time
self.cv_vote_timer = newHudElem();
self.cv_vote_timer.x = time_x;
self.cv_vote_timer.y = vote_string_height;
self.cv_vote_timer.alignX = "left";
self.cv_vote_timer.alignY = "bottom";
self.cv_vote_timer.alpha = 1;
self.cv_vote_timer.fontScale = .8;
self.cv_vote_timer.color = (1,1,0); // 1 1 0
self.cv_vote_timer setValue(time);
if(time >= 10 && time % 2 == 0) // if it's an even number it'll be 0
vote_type_x = time_x + 12;
else if(time >= 10 && time % 2 != 0)
vote_type_x = time_x + 11;
else if(time < 10 && time % 2 == 0)
vote_type_x = time_x + 7;
else if(time < 10 && time % 2 != 0)
vote_type_x = time_x + 6;
self.cv_vote_action_string = newHudElem();
self.cv_vote_action_string.x = vote_type_x;
self.cv_vote_action_string.y = vote_string_height;
self.cv_vote_action_string.alignX = "left";
self.cv_vote_action_string.alignY = "bottom";
self.cv_vote_action_string.alpha = 1;
self.cv_vote_action_string.fontScale = .8;
self.cv_vote_action_string setText(votestring);
//Draw yesvotes
yes_x = 8;
self.cv_yesvotes = newHudElem();
self.cv_yesvotes.x = yes_x;
self.cv_yesvotes.y = vote_result_height;
self.cv_yesvotes.alignX = "left";
self.cv_yesvotes.alignY = "bottom";
self.cv_yesvotes.alpha = 1;
self.cv_yesvotes.fontScale = .8;
self.cv_yesvotes setText(level.cv_spechud_voteyesstring);
self.cv_yesvotes_value = newHudElem();
self.cv_yesvotes_value.x = yes_x + 23;
self.cv_yesvotes_value.y = vote_result_height;
self.cv_yesvotes_value.alignX = "left";
self.cv_yesvotes_value.alignY = "bottom";
self.cv_yesvotes_value.alpha = 1;
self.cv_yesvotes_value.fontScale = .8;
self.cv_yesvotes_value.color = (1,1,0);
self.cv_yesvotes_value setValue(yes);
if(yes < 10)
no_x = yes_x + 30;
else
no_x = yes_x + 34;
//Draw novotes
self.cv_novotes = newHudElem();
self.cv_novotes.x = no_x;
self.cv_novotes.y = vote_result_height;
self.cv_novotes.alignX = "left";
self.cv_novotes.alignY = "bottom";
self.cv_novotes.alpha = 1;
self.cv_novotes.fontScale = .8;
self.cv_novotes setText(level.cv_spechud_votenostring);
self.cv_novotes_value = newHudElem();
self.cv_novotes_value.x = no_x + 23;
self.cv_novotes_value.y = vote_result_height;
self.cv_novotes_value.alignX = "left";
self.cv_novotes_value.alignY = "bottom";
self.cv_novotes_value.alpha = 1;
self.cv_novotes_value.fontScale = .8;
self.cv_novotes_value.color = (1,1,0);
self.cv_novotes_value setValue(no);
}
removeSpectatorVoteHud()
{
//If one of the huds is defined, they must all be
if(isDefined(self.cv_yesvotes))
{
self.cv_votestring destroy();
self.cv_vote_timer destroy();
self.cv_vote_action_string destroy();
self.cv_novotes destroy();
self.cv_novotes_value destroy();
self.cv_yesvotes destroy();
self.cv_yesvotes_value destroy();
}
}
//#######-----------------------------------------------------------------------#######//
//#######-------- Procedures for handling lists, displaying pages, etc ---------#######//
//#######-----------------------------------------------------------------------#######//
//Make an array that contains all necessary info about a certain gametype
makeGametypeNode(type, name, maplist)
{
gametype = [];
gametype[0] = type;
gametype[1] = name;
gametype[2] = maplist;
return gametype;
}
//Takes gametype array and index, returns gametype type (e.g. "dm")
getGametypeType(gametype_array, idx)
{
return gametype_array[idx][0];
}
//Takes gametype array and index, returns gametype name (e.g. "Deathmatch")
getGametypeName(gametype_array, idx)
{
return gametype_array[idx][1];
}
//Takes gametype array and index, returns maplist for this gametype
getGametypeMapList(gametype_array, idx)
{
return gametype_array[idx][2];
}
//Takes gametype array and gametype-type (e.g. "dm"), returns maplist
getMapListFromGametype(gametype_array, gametype)
{
return getGametypeMapList(gametype_array, getGametypeNumber(gametype_array, gametype));
}
//Takes a gametype array and a gametype-type (e.g. "dm"), returns the index in this array for the given gametype
getGametypeNumber(gametype_array, gametype)
{
for(i=0;i<gametype_array.size;i++)
if(getGametypeType(gametype_array, i) == gametype)
return i;
return "ERROR in getGametypeNumber, couldn't find number for gametype: " + gametype;
}
//Removes any info displayed in the slots that display the gametype and mapname
clearMapSlots()
{
for(i=0;i<level.cv_amnt_of_slots;i++)
self setClientCvar("vote_map_slot" + i, "");
}
clearGametypeSlots()
{
for(i=0;i<level.cv_amnt_of_slots;i++)
self setClientCvar("vote_gametype_slot" + i, "");
}
clearSlots()
{
self clearMapSlots();
self clearGametypeSlots();
}
//Takes a list of gametypes, an amount of slots per page and the page we wish to display. Returns the pagenumber displayed.
fillGametypeSlots(list_of_gametypes, amnt_per_page, page_nr)
{
//Get the correct page number. Will make sure page numbers aren't out of bounds and that we're always showing content (i.e. no blank pages)
page_nr = getPageNumber(page_nr, list_of_gametypes.size, amnt_per_page);
//Calculate where in the gametypes array our page starts and ends
page_start = amnt_per_page * page_nr;
page_end = (amnt_per_page * page_nr) + amnt_per_page;
//Go from start to end and copy the content to the actual menu
for(i=page_start;i<=page_end;i++)
{
slot = i - page_start;
//Fill with blank strings if we can't fill a complete page with content
if(i >= list_of_gametypes.size)
self setClientCvar("vote_gametype_slot" + slot, "");
else
self setClientCvar("vote_gametype_slot" + slot, getGametypeName(list_of_gametypes, i));
}
return page_nr;
}
//Takes a list of maps, an amount of maps per page and a page number. Returns the pagenumber displayed
fillMapSlots(list_of_maps, amnt_per_page, page_nr)
{
//Works exactly the same as the gametypes one. Read those comments.
page_nr = getPageNumber(page_nr, list_of_maps.size, amnt_per_page);
page_start = amnt_per_page * page_nr;
page_end = (amnt_per_page * page_nr) + amnt_per_page;
for(i=page_start;i<=page_end;i++)
{
slot = i - page_start;
if(i >= list_of_maps.size)
self setClientCvar("vote_map_slot" + slot, "");
else
self setClientCvar("vote_map_slot" + slot, list_of_maps[i]);
}
return page_nr;
}
getPageNumber(page_nr, amnt_of_entries, amnt_per_page)
{
max_page = 0;
if(page_nr < 0)
return 0;
for(i=0;i<=page_nr;i++)
{
//Try to determine how many pages we can fill with actual content
amnt_of_entries = amnt_of_entries - amnt_per_page;
if(page_nr > max_page && amnt_of_entries > 0)
max_page++;
//If we can fill more pages than the one requested, return the one requested
else if (page_nr <= max_page)
break;
//We do not have enough entries left to fill another page, so return the higher possible page number
else
page_nr = max_page;
}
return page_nr;
}
//Takes a certain gametype-type (e.g. "dm") and builds the list of all maps for this gametype that have the filter-variable as a substring
buildMapList(gametype, filter)
{
maplist = [];
//Build an array of all valid maps. Valid means no empty entries.
//Include the maprotation maps
for(i=0;i<=getCvarInt("cv_" + gametype + "_map_last");i++)
{
mapname = getCvar("cv_" + gametype + "_map" + i);
if(mapname != "")
if(maps\mp\_quicksearch::isSubStr(filter, mapname))
maplist[maplist.size] = mapname;
}
//Include extra maps that aren't shown in the maprotation but should be votable
for(i=0;i<=getCvarInt("cv_" + gametype + "_emap_last");i++)
{
mapname = getCvar("cv_" + gametype + "_emap" + i);
if(mapname != "")
if(maps\mp\_quicksearch::isSubStr(filter, mapname))
maplist[maplist.size] = mapname;
}
return maplist;
}
//Have this extra function to filter an already existing map list, rather than build a new list every time
//We do not have to check for empty mapnames because those would've been filtered out by buildMapList
FilterMapList(maplist, filter)
{
new_maplist = [];
empty_idx = 0;
//Iterate over the map list and build a new map list (array) with only those maps that have filter as a substring
for(i=0;i<maplist.size;i++)
if(maps\mp\_quicksearch::isSubStr(filter, maplist[i]))
{
new_maplist[empty_idx] = maplist[i];
empty_idx++;
}
return new_maplist;
}
//Takes a gametype array and a gametype that should be put upfront.
buildGametypeList(gametype_array, first_gametype)
{
gametypes = [];
empty_idx = 1;
//Iterate over the gametype list and build a new list starting at 1. If we find the current gametype anywhere in the iteration, put it up front.
for(i=0;i<level.cv_gametypes.size;i++)
{
//We want the current gametype up front
if(getGametypeType(level.cv_gametypes, i) == first_gametype)
idx = 0;
else
{
idx = empty_idx;
empty_idx++;
}
gametypes[idx] = makeGametypeNode(getGametypeType(level.cv_gametypes, i), getGametypeName(level.cv_gametypes, i), getGametypeMapList(level.cv_gametypes, i));
}
return gametypes;
}