Guest User

playermode.sp

a guest
Apr 22nd, 2017
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.45 KB | None | 0 0
  1. #pragma semicolon 1
  2. #define DEBUG 0
  3. #define CVARS_PATH "configs/playermode_cvars.txt"
  4.  
  5. #include <sourcemod>
  6. #include <sdktools>
  7. #include <builtinvotes>
  8. #include "includes/hardcoop_util.sp"
  9.  
  10. new Handle:g_hCvarKV = INVALID_HANDLE;
  11.  
  12. new Handle:hCvarMaxSurvivors;
  13. new Handle:hPlayerModeVote;
  14. new g_iDesiredPlayerMode;
  15.  
  16. public Plugin:myinfo =
  17. {
  18.     name = "Player Mode",
  19.     author = "breezy",
  20.     description = "Allows survivors to change the team limit and adapts gameplay cvars to these changes",
  21.     version = "1.0",
  22.     url = ""
  23. };
  24.  
  25. public OnPluginStart() {
  26.     hCvarMaxSurvivors = CreateConVar( "pm_max_survivors", "8", "Maximum number of survivors allowed in the game" );
  27.     RegConsoleCmd( "sm_playermode", Cmd_PlayerMode, "Change the number of survivors and adapt appropriately" );
  28.    
  29.     decl String:sGameFolder[128];
  30.     GetGameFolderName( sGameFolder, sizeof(sGameFolder) );
  31.     if( !StrEqual(sGameFolder, "left4dead2", false) ) {
  32.         SetFailState("Plugin supports Left 4 dead 2 only!");
  33.     } else {
  34.         g_hCvarKV = CreateKeyValues("Cvars");
  35.         BuildPath( Path_SM, sGameFolder, PLATFORM_MAX_PATH, CVARS_PATH );
  36.         if( !FileToKeyValues(g_hCvarKV, sGameFolder) ) {
  37.             SetFailState("Couldn't load playermode_cvars.txt!");
  38.         }
  39.     }
  40.     LoadCvars( GetConVarInt(FindConVar("survivor_limit")) );
  41. }
  42.  
  43. public OnPluginEnd() {
  44.     SetConVarBool( FindConVar("l4d_ready_enabled"), false );
  45.     // Survivors
  46.     ResetConVar( FindConVar("survivor_limit") );
  47.     ResetConVar( FindConVar("confogl_pills_limit") );
  48.     // Common
  49.     ResetConVar( FindConVar("z_common_limit") );
  50.     ResetConVar( FindConVar("z_mob_spawn_min_size") );
  51.     ResetConVar( FindConVar("z_mob_spawn_max_size") );
  52.     ResetConVar( FindConVar("z_mega_mob_size") );
  53.     // SI
  54.     ResetConVar( FindConVar("z_tank_health") );
  55.     ResetConVar( FindConVar("z_jockey_ride_damage") );
  56.     ResetConVar( FindConVar("z_pounce_damage") );
  57.     ResetConVar( FindConVar("z_pounce_damage_delay") );
  58.     // Autoslayer
  59.     ResetConVar( FindConVar("autoslayer_teamclear_delay") );
  60.     ResetConVar( FindConVar("autoslayer_slay_all_infected") );
  61. }
  62.  
  63. public Action:Cmd_PlayerMode( client, args ) {
  64.     if( IsSurvivor(client) || IsGenericAdmin(client) ) {
  65.         if( args == 1 ) {
  66.             new String:sValue[32];
  67.             GetCmdArg(1, sValue, sizeof(sValue));
  68.             new iValue = StringToInt(sValue);
  69.             if( iValue > 0 && iValue <= GetConVarInt(hCvarMaxSurvivors) ) {
  70.                 PlayerModeVote( client, iValue );
  71.             } else {
  72.                 ReplyToCommand( client, "Command restricted to values from 1 to %d", GetConVarInt(hCvarMaxSurvivors) );
  73.             }
  74.         } else {
  75.             ReplyToCommand( client, "Usage: playermode <value> [ 1 <= value <= %d", GetConVarInt(hCvarMaxSurvivors) );
  76.         }
  77.     } else {
  78.         ReplyToCommand(client, "You do not have access to this command");
  79.     }
  80. }
  81.  
  82. PlayerModeVote( client, playerMode ) {
  83.     if( !IsBuiltinVoteInProgress() ) {
  84.         if( playerMode != GetConVarInt(FindConVar("survivor_limit")) ) {
  85.             hPlayerModeVote = CreateBuiltinVote(VoteActionHandler, BuiltinVoteType_Custom_YesNo, BuiltinVoteAction_Cancel | BuiltinVoteAction_VoteEnd | BuiltinVoteAction_End);
  86.             g_iDesiredPlayerMode = playerMode;
  87.             new String:voteText[32];
  88.             Format( voteText, sizeof(voteText), "Switch to %d player?", playerMode );
  89.             SetBuiltinVoteArgument(hPlayerModeVote, voteText );
  90.             SetBuiltinVoteInitiator( hPlayerModeVote, client );
  91.             SetBuiltinVoteResultCallback( hPlayerModeVote, VoteResultHandler);
  92.             new iPlayerSurvivors[MaxClients];
  93.             new iNumPlayerSurvivors;
  94.             for( new i = 1; i < MaxClients; i++ ) {
  95.                 if( IsSurvivor(i) && !IsFakeClient(i) ) {
  96.                     iPlayerSurvivors[iNumPlayerSurvivors] = i;
  97.                     iNumPlayerSurvivors++;
  98.                 }
  99.             }
  100.             DisplayBuiltinVote( hPlayerModeVote, iPlayerSurvivors, iNumPlayerSurvivors, 20 );
  101.             FakeClientCommand( client, "Vote Yes" );
  102.         } else {
  103.             PrintToChat( client, "This playermode is already active" );
  104.         }
  105.     }
  106. }
  107.  
  108. public VoteResultHandler( Handle:vote, numVotes, numClients, const clientInfo[][2], numItems, const itemInfo[][2] ) {
  109.     new bool:votePassed = false;
  110.     for( new i = 0; i < numItems; i++ ) {
  111.         if( itemInfo[i][BUILTINVOTEINFO_ITEM_INDEX] == BUILTINVOTES_VOTE_YES ) {
  112.             if( itemInfo[i][BUILTINVOTEINFO_ITEM_VOTES] > (numClients / 2) ) {
  113.                 if( g_iDesiredPlayerMode > GetConVarInt(FindConVar("survivor_limit")) ) {
  114.                     votePassed = true;
  115.                 } else {
  116.                     new numPlayerSurvivors = 0;
  117.                     for( new j = 1; j < MaxClients; j++ ) {
  118.                         if( IsSurvivor(j) && !IsFakeClient(j) ) {
  119.                             numPlayerSurvivors++;
  120.                         }
  121.                     }
  122.                     if( g_iDesiredPlayerMode >= numPlayerSurvivors ) {
  123.                         votePassed = true;
  124.                     } else {
  125.                         PrintToChatAll("Too many players to reduce survivor limit");
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.     }
  131.     if( votePassed ) {
  132.         LoadCvars( g_iDesiredPlayerMode );
  133.         DisplayBuiltinVotePass(vote, "Changing player mode...");
  134.     } else {
  135.         DisplayBuiltinVoteFail(vote);
  136.     }
  137. }
  138.  
  139. LoadCvars( playerMode ) {
  140.     LogMessage( "Loading cvars for playermode %d", playerMode );
  141.     KvRewind( g_hCvarKV );
  142.     new String:sPlayerMode[16];
  143.     Format( sPlayerMode, sizeof(sPlayerMode), "%d", playerMode );
  144.     if( KvJumpToKey(g_hCvarKV, sPlayerMode) ) {
  145.         if( KvGotoFirstSubKey( g_hCvarKV ) ){
  146.             do {
  147.                 new String:sCvarName[64];
  148.                 KvGetSectionName( g_hCvarKV, sCvarName, sizeof(sCvarName) );
  149.                 new String:sCvarType[64];
  150.                 KvGetString( g_hCvarKV, "type", sCvarType, sizeof(sCvarType) );
  151.                 // Set cvar according to type
  152.                 if( StrEqual(sCvarType, "int", false) ) {
  153.                     SetConVarInt( FindConVar(sCvarName), KvGetNum(g_hCvarKV, "value", -1) );
  154.                 } else if( StrEqual(sCvarType, "float", false) ) {
  155.                     SetConVarFloat( FindConVar(sCvarName), KvGetFloat(g_hCvarKV, "value", -1.0) );
  156.                 } else if( StrEqual(sCvarType, "string", false) ) {
  157.                     new String:stringValue[128];
  158.                     KvGetString( g_hCvarKV, "value", stringValue, sizeof(stringValue), "Invalid String" );
  159.                     SetConVarString( FindConVar(sCvarName), stringValue );
  160.                 } else {
  161.                     LogError( "Invalid cvar type %s given for %s", sCvarType, sCvarName );
  162.                 }
  163.  
  164.             } while( KvGotoNextKey(g_hCvarKV, true) );
  165.         } else {
  166.             PrintToChatAll("No integer cvar settings listed");
  167.         }
  168.     } else {
  169.         PrintToChatAll( "No configs for player mode %d were found", playerMode );
  170.         LogError("No configs for playermode %d were found", playerMode);
  171.     }
  172. }
  173.  
  174. public VoteActionHandler(Handle:vote, BuiltinVoteAction:action, param1, param2) {
  175.     switch (action) {
  176.         case BuiltinVoteAction_End: {
  177.             hPlayerModeVote = INVALID_HANDLE;
  178.             CloseHandle(vote);
  179.         }
  180.         case BuiltinVoteAction_Cancel: {
  181.             DisplayBuiltinVoteFail(vote, BuiltinVoteFailReason:param1);
  182.         }
  183.     }
  184. }
Add Comment
Please, Sign In to add comment