Skip to content

Instantly share code, notes, and snippets.

@nagadomi
Last active January 3, 2016 00:19
Show Gist options
  • Save nagadomi/8381754 to your computer and use it in GitHub Desktop.
Save nagadomi/8381754 to your computer and use it in GitHub Desktop.
// Force strict semicolon mode
// bug fix by def075
#pragma semicolon 1
#include <sourcemod>
#define PLUGIN_VERSION "0.666 beta- fix"
new String:g_gameMode[64];
new Handle:g_mapMenu = INVALID_HANDLE;
new Handle:g_mapVoteTime = INVALID_HANDLE;
new bool: game_l4d2 = false;
public Plugin:myinfo =
{
name = "[L4D2] Campaign/Map Voter",
author = "satannuts",
description = "Allows voting by players to change campaign/map",
version = PLUGIN_VERSION,
url = "..."
}
public OnPluginStart()
{
decl String: game_name[64];
GetGameFolderName(game_name, sizeof(game_name));
if (!StrEqual(game_name, "left4dead", false) && !StrEqual(game_name, "left4dead2", false))
{
SetFailState("Use this in Left 4 Dead or Left 4 Dead 2 only.");
}
if (StrEqual(game_name, "left4dead2", false))
{
game_l4d2 = true;
}
CreateConVar("l4d_mapvote_version", PLUGIN_VERSION, "[[L4D2] Campaign/Map Voter Version", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_NOTIFY|FCVAR_DONTRECORD);
RegAdminCmd("sm_cancelvote", Command_CancelVote, ADMFLAG_VOTE);
RegAdminCmd("sm_mapvote", Command_MapVote, ADMFLAG_CHANGEMAP);
g_mapVoteTime = CreateConVar("sm_mapvotetime", "20", "Default time to vote on a map in seconds",FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_NOTIFY);
CreateConVar("l4d_mapvote_announce_mode", "1", "Controls how mapvote announcement is displayed.");
RegConsoleCmd("sm_mapvote", Command_UserMapVote, "initiate mapvote");
AutoExecConfig(true, "l4d2_mapvote_beta");
}
public OnMapStart()
{
new Handle:currentGameMode = FindConVar("mp_gamemode");
GetConVarString(currentGameMode, g_gameMode, sizeof(g_gameMode));
}
public OnClientPutInServer(client)
{
if(GetConVarInt(FindConVar("l4d_mapvote_announce_mode")) != 0)
{
CreateTimer(5.0, Timer_WelcomeMessage, client);
}
}
public Action:Timer_WelcomeMessage(Handle:timer, any:client)
{
new String:announce[] = "\x01[SM] To call a vote to change map/campaign votes, Type: \x04!mapvote\x01 in chat.";
if (IsClientConnected(client) && IsClientInGame(client) && !IsFakeClient(client))
{
PrintToChat(client, announce);
}
}
public Action:Command_UserMapVote(client, args)
{
DoMapVoteList(client);
return Plugin_Handled;
}
DoMapVoteList(client)
{
g_mapMenu = BuildMapMenu(false);
DisplayMenu(g_mapMenu, client, 60);
}
public Handle_MapVoteList(Handle:mapMenu, MenuAction:action, param1, param2)
{
// Change the map to the selected item.
if(action == MenuAction_Select)
{
decl String:map[64];
decl String:map_title[128];
decl style;
GetMenuItem(mapMenu, param2, map, sizeof(map), style, map_title, sizeof(map_title));
DoVoteMenu (map, map_title);
}
/* If the menu has ended, destroy it */
else if (action == MenuAction_End)
{
CloseHandle(mapMenu);
}
}
DoVoteMenu(const String:map[], const String:map_title[])
{
if(IsVoteInProgress())
{
return;
}
new Handle:voteMenu = CreateMenu(Handle_VoteMenu);
SetMenuTitle(voteMenu, "Change Campaign/Map to: %s?", map_title);
AddMenuItem(voteMenu, map, "Yes");
AddMenuItem(voteMenu, "no", "No");
SetMenuExitButton(voteMenu, false);
new voteTime = GetConVarInt(g_mapVoteTime);
VoteMenuToAll(voteMenu, voteTime);
PrintToChatAll("Map change vote in progress...");
}
public Handle_VoteMenu(Handle:voteMenu, MenuAction:action, param1, param2)
{
if(action == MenuAction_End)
{
CloseHandle(voteMenu);
}
else if(action == MenuAction_VoteEnd)
{
new votes, totalVotes, yes_vote, no_vote;
decl String:map[64];
decl String:map_title[2][128];
decl String:vote_title[128];
GetMenuVoteInfo(param2, votes, totalVotes);
GetMenuTitle(voteMenu, vote_title, sizeof(vote_title));
ExplodeString(vote_title, "to: ", map_title, 2, 128);
ReplaceString(map_title[1], 128, "?", " ");
GetMenuItem(voteMenu, param1, map, sizeof(map));
if (strcmp(map, "no", true) == 0) {
yes_vote = totalVotes - votes;
no_vote = votes;
} else {
yes_vote = votes;
no_vote = totalVotes - votes;
}
if (yes_vote > no_vote) {
new Handle:pack;
PrintToChatAll("Vote Successful! (yes:%d/no:%d) Changing to %s", yes_vote, no_vote, map_title[1]);
CreateDataTimer(5.0, Timer_MapChange, pack);
WritePackString(pack, map);
} else {
PrintToChatAll("Vote Failed! (yes:%d/no:%d) Keeping current map.", yes_vote, no_vote);
}
}
else if(action == MenuAction_VoteCancel)
{
// If we receive 0 votes, pick at random.
if (param1 == VoteCancel_NoVotes)
{
PrintToChatAll("Vote Failed! Keeping current map.");
}
else
{
// We were actually cancelled. Guess we do nothing.
}
}
}
public Action:Timer_MapChange(Handle:timer, Handle:pack)
{
new String:map[64];
ResetPack(pack);
ReadPackString(pack, map, sizeof(map));
ServerCommand("changelevel %s", map);
}
public Action:Command_CancelVote(client, args)
{
CancelVote();
return Plugin_Handled;
}
public Action:Command_MapVote(client, args)
{
g_mapMenu = BuildMapMenu(true);
DisplayMenu(g_mapMenu, client, 60);
return Plugin_Handled;
}
public Handle_AdminMapMenu(Handle:mapMenu, MenuAction:action, param1, param2)
{
// Change the map to the selected item.
if(action == MenuAction_Select)
{
decl String:map[64];
GetMenuItem(mapMenu, param2, map, sizeof(map));
ServerCommand("changelevel %s", map);
}
// If the menu was cancelled, choose a random map.
else if (action == MenuAction_Cancel)
{
CloseHandle(mapMenu);
}
// If the menu has ended, destroy it
else if (action == MenuAction_End)
{
CloseHandle(mapMenu);
}
}
Handle:BuildMapMenu(bool:adminMode)
{
new Handle:mapMenu = INVALID_HANDLE;
if(adminMode)
{
mapMenu = CreateMenu(Handle_AdminMapMenu);
}
else
{
mapMenu = CreateMenu(Handle_MapVoteList);
}
SetMenuTitle(mapMenu, "Choose a Map");
SetMenuExitButton(mapMenu, false);
if(game_l4d2)
{
if(strcmp(g_gameMode, "coop", false) == 0)
{
AddMenuItem(mapMenu, "c1m1_hotel", "Dead Center");
AddMenuItem(mapMenu, "c2m1_highway", "Dark Carnival");
AddMenuItem(mapMenu, "c3m1_plankcountry", "Swamp Fever");
AddMenuItem(mapMenu, "c4m1_milltown_a", "Hard Rain");
AddMenuItem(mapMenu, "c5m1_waterfront", "The Parish");
}
else if(strcmp(g_gameMode, "realism", false) == 0)
{
AddMenuItem(mapMenu, "c1m1_hotel", "Dead Center");
AddMenuItem(mapMenu, "c2m1_highway", "Dark Carnival");
AddMenuItem(mapMenu, "c3m1_plankcountry", "Swamp Fever");
AddMenuItem(mapMenu, "c4m1_milltown_a", "Hard Rain");
AddMenuItem(mapMenu, "c5m1_waterfront", "The Parish");
}
else if(strcmp(g_gameMode, "versus", false) == 0)
{
AddMenuItem(mapMenu, "c1m1_hotel", "Dead Center");
AddMenuItem(mapMenu, "c2m1_highway", "Dark Carnival");
AddMenuItem(mapMenu, "c3m1_plankcountry", "Swamp Fever");
AddMenuItem(mapMenu, "c4m1_milltown_a", "Hard Rain");
AddMenuItem(mapMenu, "c5m1_waterfront", "The Parish");
}
else if(strcmp(g_gameMode, "teamversus", false) == 0)
{
AddMenuItem(mapMenu, "c1m1_hotel", "Dead Center");
AddMenuItem(mapMenu, "c2m1_highway", "Dark Carnival");
AddMenuItem(mapMenu, "c3m1_plankcountry", "Swamp Fever");
AddMenuItem(mapMenu, "c4m1_milltown_a", "Hard Rain");
AddMenuItem(mapMenu, "c5m1_waterfront", "The Parish");
}
else if(strcmp(g_gameMode, "survival", false) == 0)
{
AddMenuItem(mapMenu, "c1m4_atrium", "Atrium");
AddMenuItem(mapMenu, "c2m1_highway", "Highway");
AddMenuItem(mapMenu, "c2m4_barns", "Barns");
AddMenuItem(mapMenu, "c2m5_concert", "Concert");
AddMenuItem(mapMenu, "c3m1_plankcountry", "Plank Country");
AddMenuItem(mapMenu, "c3m4_plantation", "Plantation");
AddMenuItem(mapMenu, "c4m1_milltown_a", "Mill Town 1");
AddMenuItem(mapMenu, "c4m2_sugarmill_a", "Sugar Mill 1");
AddMenuItem(mapMenu, "c5m2_park", "Park");
AddMenuItem(mapMenu, "c5m5_bridge ", "Bridge");
}
else if(strcmp(g_gameMode, "scavenge", false) == 0)
{
AddMenuItem(mapMenu, "c1m4_atrium", "Atrium");
AddMenuItem(mapMenu, "c2m1_highway", "Highway");
AddMenuItem(mapMenu, "c3m1_plankcountry", "Plank Country");
AddMenuItem(mapMenu, "c4m1_milltown_a", "Mill Town 1");
AddMenuItem(mapMenu, "c4m2_sugarmill_a", "Sugar Mill 1");
AddMenuItem(mapMenu, "c5m2_park", "Park");
}
else if(strcmp(g_gameMode, "teamscavenge", false) == 0)
{
AddMenuItem(mapMenu, "c1m4_atrium", "Atrium");
AddMenuItem(mapMenu, "c2m1_highway", "Highway");
AddMenuItem(mapMenu, "c3m1_plankcountry", "Plank Country");
AddMenuItem(mapMenu, "c4m1_milltown_a", "Mill Town 1");
AddMenuItem(mapMenu, "c4m2_sugarmill_a", "Sugar Mill 1");
AddMenuItem(mapMenu, "c5m2_park", "Park");
}
else
{
AddMenuItem(mapMenu, "c1m1_hotel", "Dead Center");
AddMenuItem(mapMenu, "c2m1_highway", "Dark Carnival");
AddMenuItem(mapMenu, "c3m1_plankcountry", "Swamp Fever");
AddMenuItem(mapMenu, "c4m1_milltown_a", "Hard Rain");
AddMenuItem(mapMenu, "c5m1_waterfront", "The Parish");
}
}
else
{
if(strcmp(g_gameMode, "coop", false) == 0)
{
AddMenuItem(mapMenu, "l4d_hospital01_apartment", "Mercy Hospital");
AddMenuItem(mapMenu, "l4d_garage01_alleys", "Crash Course");
AddMenuItem(mapMenu, "l4d_smalltown01_caves", "Death Toll");
AddMenuItem(mapMenu, "l4d_airport01_greenhouse", "Dead Air");
AddMenuItem(mapMenu, "l4d_farm01_hilltop", "Blood Harvest");
}
else if(strcmp(g_gameMode, "versus", false) == 0)
{
AddMenuItem(mapMenu, "l4d_vs_hospital01_apartment", "Mercy Hospital");
AddMenuItem(mapMenu, "l4d_garage01_alleys", "Crash Course");
AddMenuItem(mapMenu, "l4d_vs_smalltown01_caves", "Death Toll");
AddMenuItem(mapMenu, "l4d_vs_airport01_greenhouse", "Dead Air");
AddMenuItem(mapMenu, "l4d_vs_farm01_hilltop", "Blood Harvest");
}
else if(strcmp(g_gameMode, "survival", false) == 0)
{
AddMenuItem(mapMenu, "l4d_hospital02_subway", "Generator Room");
AddMenuItem(mapMenu, "l4d_hospital03_sewers", "Gas Station");
AddMenuItem(mapMenu, "l4d_hospital04_interior", "Hospital");
AddMenuItem(mapMenu, "l4d_vs_hospital05_rooftop", "Rooftop");
AddMenuItem(mapMenu, "l4d_garage01_alleys", "Bridge (crashcourse)");
AddMenuItem(mapMenu, "l4d_garage02_lots", "Truck Depot");
AddMenuItem(mapMenu, "l4d_smalltown02_drainage", "Drains");
AddMenuItem(mapMenu, "l4d_smalltown03_ranchhouse", "Church");
AddMenuItem(mapMenu, "l4d_smalltown04_mainstreet", "Street");
AddMenuItem(mapMenu, "l4d_vs_smalltown05_houseboat", "Boathouse");
AddMenuItem(mapMenu, "l4d_airport02_offices", "Crane");
AddMenuItem(mapMenu, "l4d_airport03_garage", "Construction Site");
AddMenuItem(mapMenu, "l4d_airport04_terminal", "Terminal");
AddMenuItem(mapMenu, "l4d_vs_airport05_runway", "Runway");
AddMenuItem(mapMenu, "l4d_farm02_traintunnel", "Warehouse");
AddMenuItem(mapMenu, "l4d_farm03_bridge", "Bridge (bloodharvest)");
AddMenuItem(mapMenu, "l4d_vs_farm05_cornfield", "Farmhouse");
AddMenuItem(mapMenu, "l4d_sv_lighthouse", "Lighthouse");
}
}
return mapMenu;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment