Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sourcemod>
- #include <adt_array>
- #include <SDKTools>
- #include <cstrike>
- #include <CustomPlayerSkins>
- #include <sdkhooks>
- #include <halflife>
- // Game constants - use these first, if need be to implement file I/O just exchange these constants with file loaded values!
- int MAX_PLAYERS = 12;
- int MIN_PLAYERS = 2;
- int HACKER_VOTE_DUR = 8;
- char CLR_HEADER[9] = " \x01\x0B";
- char CLR_DEFAULT[4] = "\x01";
- char CLR_ERROR[4] = "\x02";
- char CLR_NOTIFY[4] = "\x03";
- char CLR_SUCCESS[4] = "\x04";
- char CLR_GOLD[4] = "\x10";
- // Game vars
- int hacker;
- int hackerTeam;
- int prevHacker;
- int prevHackerTeam;
- int roundCount;
- int ghost;
- char gameState[64];
- bool runningGame;
- bool ctHasPerk;
- bool tHasPerk;
- ArrayList queue;
- ArrayList ct;
- ArrayList t;
- ArrayList readyQueue;
- ArrayList playerList;
- public Plugin myinfo = {
- name = "Hacker",
- author = "Jonathan Lin",
- description = "Hacker gamemode",
- url = "https://brassygaming.com/",
- };
- public void OnPluginStart() {
- // Init variables, these are defaults!
- queue = CreateArray(1);
- ct = CreateArray(1);
- t = CreateArray(1);
- readyQueue = CreateArray(1);
- playerList = CreateArray(1);
- roundCount = 0;
- gameState = "";
- runningGame = false;
- hacker = 0;
- hackerTeam = 0;
- prevHacker = 0;
- prevHackerTeam = 0;
- tHasPerk = true;
- ctHasPerk = true;
- ghost = 0;
- HookEvent("player_activate", OnPlayerActivate);
- HookEvent("player_disconnect", OnPlayerDisconnect);
- HookEvent("round_poststart", OnRoundPostStart);
- HookEvent("round_end", OnRoundEnd);
- HookEvent("round_start", OnRoundStart);
- HookEvent("cs_intermission", OnGameEnd);
- HookEvent("announce_phase_end", OnHalftime);
- HookUserMessage(GetUserMessageId("TextMsg"), OnTextChat, true);
- // what
- RegConsoleCmd("queue", Command_Queue);
- RegConsoleCmd("q", Command_Queue);
- RegConsoleCmd("leave", Command_Leave);
- RegConsoleCmd("l", Command_Leave);
- RegConsoleCmd("ready", Command_Ready);
- RegConsoleCmd("r", Command_Ready);
- RegConsoleCmd("notready", Command_NotReady);
- RegConsoleCmd("nr", Command_NotReady);
- AddCommandListener(OnJoinTeamCommand, "jointeam");
- PrintToServer(">> Hacker has been enabled!");
- }
- public void OnPluginEnd() {
- PrintToServer(">> Hacker has been disabled!");
- }
- public Action OnTextChat(UserMsg msgId, Handle pb, const players[], playersNum, bool reliable, bool init) {
- char msgType[32];
- PbReadString(pb, "params", msgType, sizeof(msgType), 0);
- if (StrContains(msgType, "#SFUI_Notice_Match", false) != -1) {
- return Plugin_Handled;
- }
- return Plugin_Continue;
- }
- public Action OnHalftime(Event event, const char[] name, bool dontBroadcast) {
- PrintToServer("Switched teams");
- ArrayList switchQueue1 = CreateArray(1);
- ArrayList switchQueue2 = CreateArray(1);
- for (int i = 0; i < ct.Length; i++) {
- switchQueue1.Push(ct.Get(i));
- }
- for (int i = 0; i < t.Length; i++) {
- switchQueue2.Push(t.Get(i));
- }
- ct.Clear();
- t.Clear();
- for (int i = 0; i < switchQueue1.Length; i++) {
- t.Push(switchQueue1.Get(i));
- }
- for (int i = 0; i < switchQueue2.Length; i++) {
- ct.Push(switchQueue2.Get(i));
- }
- delete switchQueue1;
- delete switchQueue2;
- ClearHalf();
- }
- public Action OnGameEnd(Event event, const char[] name, bool dontBroadcast) {
- EndGame();
- }
- // Call for pre-round votes and to increment roundCount
- public Action OnRoundStart(Event event, const char[] name, bool dontBroadcast) {
- gameState = "pre_round";
- roundCount++;
- PrintToServer("Previous round: %d, Current Round: %d", roundCount - 1, roundCount);
- // Vote on the previous round if > 0, meaning hackerTeam must be != 0
- // Hacker has not been assigned yet, hacker team has no been assigned yet
- if (roundCount - 1 > 0) {
- if (hacker == -1) {
- PrintToChatAll("%s%sSince the hacker left last round, there will be no vote.", CLR_HEADER, CLR_NOTIFY);
- } else {
- if (hackerTeam == 2) {
- new voters[t.Length];
- for (int i = 0; i < t.Length; i++) {
- voters[i] = t.Get(i);
- }
- Menu_HackerVote(voters, t.Length, HACKER_VOTE_DUR);
- } else if (hackerTeam == 3) {
- new voters[ct.Length];
- for (int i = 0; i < ct.Length; i++) {
- voters[i] = ct.Get(i);
- }
- Menu_HackerVote(voters, ct.Length, HACKER_VOTE_DUR);
- }
- }
- }
- // Flip the hacker team - this sets hackerTeam
- PrintToServer("Hacker team assigned!");
- if (hackerTeam == 0) {
- int randomInt = GetRandomInt(2, 3);
- hackerTeam = randomInt;
- } else if (hackerTeam == 2) {
- prevHackerTeam = 2;
- hackerTeam = 3;
- } else if (hackerTeam == 3) {
- prevHackerTeam = 3;
- hackerTeam = 2;
- }
- // Assign the ghost
- // No ghost is selected if the playerList is only 2
- if (prevHackerTeam == 3 && playerList.Length > 2) {
- int client = t.Get(GetRandomInt(0, t.Length - 1))
- ghost = client;
- PrintToServer("Ghost has been selected: %d", client);
- PrintToChat(client, "%s%s>> You have been selected as the ghost! You are now invisible to the hacker! <<", CLR_HEADER, CLR_GOLD);
- } else if (prevHackerTeam == 2 && playerList.Length > 2) {
- int client = ct.Get(GetRandomInt(0, ct.Length - 1))
- ghost = client;
- PrintToServer("Ghost has been selected: %d", client);
- PrintToChat(client, "%s%s>>You have been selected as the ghost! You are now invisible to the hacker! <<", CLR_HEADER, CLR_GOLD);
- }
- // Set the glow
- for (int i = 0; i < playerList.Length; i++) {
- int client = playerList.Get(i);
- if (GetClientTeam(client) != hackerTeam && client != ghost) {
- PrintToServer("Client %d set to glow on team %d", client, GetClientTeam(client));
- SetGlowing(client);
- }
- }
- return Plugin_Continue;
- }
- public void SetGlowing(int client) {
- char model[PLATFORM_MAX_PATH];
- GetClientModel(client, model, sizeof(model));
- CPS_RemoveSkin(client);
- CPS_SetSkin(client, model, CPS_RENDER);
- int skin = CPS_GetSkin(client);
- if (SDKHookEx(skin, SDKHook_SetTransmit, OnShouldDisplay)) {
- SetupGlow(skin);
- }
- }
- public void SetupGlow(int skin) {
- SetEntProp(skin, Prop_Send, "m_bShouldGlow", true, true);
- SetEntProp(skin, Prop_Send, "m_nGlowStyle", 0);
- SetEntPropFloat(skin, Prop_Send, "m_flGlowMaxDist", 1000000000.0);
- // So now setup given glow colors for the skin
- SetEntData(skin, GetEntSendPropOffs(skin, "m_clrGlow"), 192, _, true); // Red
- SetEntData(skin, GetEntSendPropOffs(skin, "m_clrGlow") + 1, 160, _, true); // Green
- SetEntData(skin, GetEntSendPropOffs(skin, "m_clrGlow") + 2, 96, _, true); // Blue
- SetEntData(skin, GetEntSendPropOffs(skin, "m_clrGlow") + 3, 64, _, true); // Alpha
- }
- public Action OnShouldDisplay(int entity, int client) {
- if (client == hacker) {
- return Plugin_Continue;
- }
- return Plugin_Handled;
- }
- // When the round actually starts, apply perks and select players
- public Action OnRoundPostStart(Event event, const char[] name, bool dontBroadcast) {
- PrintToServer(">> Round start");
- gameState = "round_start";
- if (hacker != 0 && hacker != -1) {
- prevHacker = hacker;
- PrintToServer("Previous hacker set to %d", prevHacker);
- }
- // Assign the hacker
- if (hackerTeam == 2) {
- hacker = ct.Get(GetRandomInt(0, ct.Length - 1));
- } else if (hackerTeam == 3) {
- hacker = t.Get(GetRandomInt(0, t.Length - 1));
- } else {
- PrintToServer("Error in assigning hacker, hackerTeam is 0!");
- }
- PrintToServer("Hacker set to: %d", hacker);
- PrintToChat(hacker, "%s%s>> You have been selected as the hacker! <<", CLR_HEADER, CLR_GOLD);
- return Plugin_Handled;
- }
- public Action OnRoundEnd(Event event, const char[] name, bool dontBroadcast) {
- if (roundCount == 0) {
- return Plugin_Continue;
- }
- ctHasPerk = true;
- tHasPerk = true;
- PrintToServer("Perk reset!");
- PrintToServer(">> Round end");
- gameState = "round_end";
- return Plugin_Continue;
- }
- public Action OnPlayerDisconnect(Event event, const char[] name, bool dontBroadcast) {
- int client = GetClientOfUserId(event.GetInt("userid"));
- int queueIndex = queue.FindValue(client);
- if (queueIndex != -1) {
- queue.Erase(queueIndex);
- }
- int ctIndex = ct.FindValue(client);
- int tIndex = t.FindValue(client);
- if (ctIndex != -1) {
- ct.Erase(ctIndex);
- }
- if (tIndex != -1) {
- t.Erase(tIndex);
- }
- // If the player amount falls below MIN_PLAYERS, stop the game
- if (runningGame == true && (ct.Length + t.Length < MIN_PLAYERS || ct.Length == 0 || t.Length == 0)) {
- PrintToChatAll("%s%sNot enough players to continue playing!", CLR_HEADER, CLR_ERROR);
- EndGame();
- PrintToServer(">> Game ended because not enough players!");
- }
- // If the hacker leaves the game, set hacker = -1
- if (hacker == client) {
- hacker = -1;
- }
- // Add a 1 minute timer to rejoin, then automatically add someone from the queue?
- return Plugin_Continue;
- }
- public Action OnPlayerActivate(Event event, const char[] name, bool dontBroadcast) {
- int client = GetClientOfUserId(event.GetInt("userid"));
- ChangeClientTeam(client, 1);
- CreateTimer(3.0, Timer_PlayerJoinMessage, client);
- return Plugin_Continue;
- }
- public Action Timer_PlayerJoinMessage(Handle timer, any client) {
- if (IsClientInGame(client)) {
- PrintToChat(client, "%s%sWelcome to %sBrassy Gaming's Hacker Server%s!", CLR_HEADER,
- CLR_DEFAULT, CLR_GOLD, CLR_DEFAULT);
- PrintToChat(client, "%s%sType %s/queue %sor %s/q %sto join the queue.", CLR_HEADER, CLR_NOTIFY, CLR_DEFAULT, CLR_NOTIFY,
- CLR_DEFAULT, CLR_NOTIFY);
- }
- return Plugin_Stop;
- }
- // Is there a better way to block this?
- public Action OnJoinTeamCommand(int client, char[] command, int argc) {
- PrintToChat(client, "%s%sYou cannot change teams! Type %s/queue %sto join the queue.", CLR_HEADER, CLR_ERROR, CLR_DEFAULT, CLR_ERROR);
- return Plugin_Handled;
- }
- public Action Command_NotReady(int client, int args) {
- if (client) {
- int queueIndex = queue.FindValue(client);
- if (queueIndex != -1) {
- if (readyQueue.FindValue(client) != -1) {
- readyQueue.Erase(readyQueue.FindValue(client));
- PrintToChat(client, "%s%sYou have been removed from ready status!", CLR_HEADER, CLR_ERROR);
- } else {
- PrintToChat(client, "%s%sYou aren't even ready! Type %s/ready%s.", CLR_HEADER, CLR_ERROR, CLR_DEFAULT, CLR_ERROR);
- }
- } else {
- PrintToChat(client, "%s%sYou aren't even queued! Type %s/queue%s.", CLR_HEADER, CLR_ERROR, CLR_DEFAULT, CLR_ERROR);
- }
- }
- return Plugin_Handled;
- }
- public Action Command_Ready(int client, int args) {
- if (client) {
- int queueIndex = queue.FindValue(client);
- if (queueIndex != -1) {
- if (queue.Length < MIN_PLAYERS) {
- PrintToChat(client, "%s%sYou can't ready yet, there aren't enough players!", CLR_HEADER, CLR_ERROR);
- return Plugin_Handled;
- }
- if (runningGame == true) {
- PrintToChat(client, "%s%sYou can't ready yet, there is currently a game going on!", CLR_HEADER, CLR_ERROR);
- return Plugin_Handled;
- }
- if (readyQueue.FindValue(client) == -1) {
- readyQueue.Push(client);
- PrintToChat(client, "%s%sReady! There are %d out of %d players ready.", CLR_HEADER, CLR_NOTIFY, readyQueue.Length, GetQueuedPlayers());
- } else {
- PrintToChat(client, "%s%sThere are %d out of %d players ready.", CLR_HEADER, CLR_NOTIFY, readyQueue.Length, GetQueuedPlayers());
- }
- if (readyQueue.Length == GetQueuedPlayers()) {
- StartGame();
- }
- } else {
- PrintToChat(client, "%s%sYou must use %s/queue %sto join the queue first!", CLR_HEADER, CLR_ERROR, CLR_DEFAULT, CLR_ERROR);
- }
- return Plugin_Handled;
- }
- return Plugin_Continue;
- }
- public int GetQueuedPlayers() {
- int queuedPlayers = queue.Length;
- if (queue.Length > MAX_PLAYERS) {
- queuedPlayers = MAX_PLAYERS;
- }
- return queuedPlayers;
- }
- public Action Command_Queue(int client, int args) {
- if (client) {
- int queueIndex = queue.FindValue(client);
- if (ct.FindValue(client) != -1 || t.FindValue(client) != -1) {
- PrintToChat(client, "%s%sYou cannot join the queue, you're already in game!", CLR_HEADER, CLR_ERROR);
- return Plugin_Handled;
- }
- if (queueIndex == -1) {
- queue.Push(client);
- // Time to check if there are enough players to play!
- PrintToChat(client, "%s%sYou have been added to the queue! Type %s/queue %sagain to check the queue and %s/leave %sto leave the queue!",
- CLR_HEADER, CLR_SUCCESS, CLR_DEFAULT, CLR_SUCCESS, CLR_DEFAULT, CLR_SUCCESS);
- CheckStart();
- } else {
- PrintToChat(client, "%s%sYou are %s#%d %sin the queue!", CLR_HEADER, CLR_NOTIFY, CLR_DEFAULT, queueIndex + 1, CLR_NOTIFY);
- }
- return Plugin_Handled;
- }
- return Plugin_Continue;
- }
- public Action Command_Leave(int client, int args) {
- if (client) {
- int queueIndex = queue.FindValue(client);
- if (queueIndex != -1) {
- queue.Erase(queueIndex);
- PrintToChat(client, "%s%sYou have been removed from the queue!", CLR_HEADER, CLR_SUCCESS);
- } else {
- PrintToChat(client, "%s%sLmao that didn't do anything.", CLR_HEADER, CLR_ERROR);
- }
- return Plugin_Handled;
- }
- return Plugin_Continue;
- }
- public bool CheckStart() {
- if (queue.Length >= MIN_PLAYERS) {
- PrintToChatAll("%s%sThe game will start when all players type %s/ready%s.", CLR_HEADER, CLR_NOTIFY, CLR_DEFAULT, CLR_NOTIFY);
- return true;
- }
- return false;
- }
- public void ClearGlobals() {
- roundCount = 0;
- gameState = "";
- runningGame = false;
- hacker = 0;
- hackerTeam = 0;
- prevHacker = 0;
- prevHackerTeam = 0;
- tHasPerk = true;
- ctHasPerk = true;
- ghost = 0;
- }
- public void ClearHalf() {
- hacker = 0;
- hackerTeam = 0;
- prevHacker = 0;
- prevHackerTeam = 0;
- tHasPerk = true;
- ctHasPerk = true;
- ghost = 0;
- }
- public void EndGame() {
- ClearGlobals();
- for (int i = 0; i < ct.Length; i++) {
- int client = ct.Get(i);
- ChangeClientTeam(client, 1);
- }
- for (int i = 0; i < t.Length; i++) {
- int client = t.Get(i);
- ChangeClientTeam(client, 1);
- }
- t.Clear();
- ct.Clear();
- playerList.Clear();
- }
- public void StartGame() {
- PrintToChatAll("%s%s>> Game is live! <<", CLR_HEADER, CLR_GOLD);
- runningGame = true;
- t.Clear();
- readyQueue.Clear();
- int queuedPlayers = GetQueuedPlayers();
- // Get players into playerList
- for (int i = 0; i < queuedPlayers; i++) {
- playerList.Push(queue.Get(i));
- }
- // Remove players in playerList from queue and add to teams
- for (int i = 0; i < playerList.Length; i++) {
- int client = playerList.Get(i);
- queue.Erase(queue.FindValue(client));
- if (ct.Length == t.Length) {
- ChangeClientTeam(client, 2);
- ct.Push(client);
- } else if (ct.Length > t.Length) {
- ChangeClientTeam(client, 3);
- t.Push(client);
- PrintToServer("Client %d added to team 3", client);
- } else if (t.Length > ct.Length) {
- ChangeClientTeam(client, 2);
- ct.Push(client);
- PrintToServer("Client %d added to team 2", client);
- }
- }
- }
- public Action Menu_PerkVote(int[] clients, int size, int dur) {
- // This is called after hacker team changes
- Menu menu = new Menu(MenuHandler_PerkVote);
- menu.SetTitle("Choose a perk to fight the hacker!");
- menu.AddItem("Gamble", "Gamble");
- menu.AddItem("Money Hog", "Money Hog");
- menu.ExitButton = false;
- menu.DisplayVote(clients, size, dur);
- return Plugin_Handled;
- }
- public int MenuHandler_PerkVote(Menu menu, MenuAction action, int param1, int param2) {
- // This comes after hackerTeam switch so use prevHacker
- if (action == MenuAction_End) {
- delete menu;
- } else if (action == MenuAction_VoteEnd) {
- // Random Money
- if (param1 == 0) {
- if (prevHackerTeam == 2) {
- for (int i = 0; i < ct.Length; i++) {
- int randomAmt = GetRandomInt(2000, 8000);
- SetEntProp(ct.Get(i), Prop_Send, "m_iAccount", randomAmt);
- PrintToChat(ct.Get(i), "%s%sYour team gambled and you ended up with %s%d%s!", CLR_HEADER, CLR_NOTIFY, CLR_DEFAULT, randomAmt, CLR_NOTIFY);
- }
- } else if (prevHackerTeam == 3) {
- for (int i = 0; i < t.Length; i++) {
- int randomAmt = GetRandomInt(2000, 8000);
- SetEntProp(t.Get(i), Prop_Send, "m_iAccount", randomAmt);
- PrintToChat(t.Get(i), "%s%sYour team gambled and you ended up with %s%d%s!", CLR_HEADER, CLR_NOTIFY, CLR_DEFAULT, randomAmt, CLR_NOTIFY);
- }
- }
- } else if (param1 == 1) {
- if (prevHackerTeam == 2) {
- int client = ct.Get(GetRandomInt(0, ct.Length - 1));
- SetEntProp(client, Prop_Send, "m_iAccount", 8000);
- PrintToChat(client, "%s%sYou got all the money, money hog!", CLR_HEADER, CLR_NOTIFY);
- } else if (prevHackerTeam == 3) {
- int client = t.Get(GetRandomInt(0, t.Length - 1))
- SetEntProp(client, Prop_Send, "m_iAccount", 8000);
- PrintToChat(client, "%s%sYou got all the money, money hog!", CLR_HEADER, CLR_NOTIFY);
- }
- }
- }
- }
- public Action Menu_HackerVote(int[] clients, int size, int dur) {
- // Vote comes before hacker team changes so it's all gucci to use hackerTeam here
- Menu menu = new Menu(MenuHandler_HackerVote);
- menu.SetTitle("Who was the hacker last round?");
- if (hackerTeam == 2) {
- for (int i = 0; i < ct.Length; i++) {
- char name[255];
- GetClientName(ct.Get(i), name, 255);
- menu.AddItem(name, name);
- }
- } else if (hackerTeam == 3) {
- for (int i = 0; i < t.Length; i++) {
- char name[255];
- GetClientName(t.Get(i), name, 255);
- menu.AddItem(name, name);
- }
- }
- menu.ExitButton = false;
- menu.DisplayVote(clients, size, dur);
- return Plugin_Handled;
- }
- public int MenuHandler_HackerVote(Menu menu, MenuAction action, int param1, int param2) {
- if (action == MenuAction_End) {
- delete menu;
- } else if (action == MenuAction_VoteEnd) {
- // Chosen index is param1 - so assuming that items were added in the right order it is the same as teamIndex for respective arraylist
- // This happens after hackerTeam switch and after hacker is chosen so use prevHacker and prevHackerTeam
- int teamIndex = param1;
- char selectionName[255];
- int prevHackerClient;
- if (prevHackerTeam == 2) {
- prevHackerClient = ct.Get(teamIndex);
- GetClientName(prevHackerClient, selectionName, 255);
- } else if (prevHackerTeam == 3) {
- prevHackerClient = t.Get(teamIndex);
- GetClientName(prevHackerClient, selectionName, 255);
- }
- PrintToServer("prevHackerClient = %d, prevHacker = %d", prevHackerClient, prevHacker);
- if (prevHackerClient == prevHacker) {
- // hacker selected!
- if (prevHackerTeam == 2) {
- PrintToChatAll("%s%s%s %swas the hacker! %sTs %sdon't get a perk this round!", CLR_HEADER, CLR_DEFAULT, selectionName,
- CLR_NOTIFY, CLR_DEFAULT, CLR_NOTIFY);
- tHasPerk = false;
- } else if (prevHackerTeam == 3) {
- PrintToChatAll("%s%s%s %swas the hacker! %sCTs %sdon't get a perk this round!", CLR_HEADER, CLR_DEFAULT, selectionName,
- CLR_NOTIFY, CLR_DEFAULT, CLR_NOTIFY);
- ctHasPerk = false;
- }
- } else {
- // hacker not selected!
- PrintToChatAll("%s%s%s%s was not the hacker!", CLR_HEADER, CLR_DEFAULT, selectionName, CLR_NOTIFY);
- SendPerkVote();
- }
- }
- }
- public void SendPerkVote() {
- // Set the nonhacker perk
- PrintToServer("Nonhacker perk vote");
- if (hackerTeam == 2 && tHasPerk) {
- // nonhackerTeam = 3
- new voters[t.Length];
- for (int i = 0; i < t.Length; i++) {
- voters[i] = t.Get(i);
- }
- Menu_PerkVote(voters, t.Length, HACKER_VOTE_DUR);
- } else if (hackerTeam == 3 && ctHasPerk) {
- // nonhackerTeam = 3
- new voters[ct.Length];
- for (int i = 0; i < ct.Length; i++) {
- voters[i] = ct.Get(i);
- }
- Menu_PerkVote(voters, ct.Length, HACKER_VOTE_DUR);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement