FlacoBey

Untitled

May 5th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.83 KB | None | 0 0
  1. #include <sourcemod>
  2. #include <sdktools>
  3. #include <sdktools_functions>
  4. #include <sdkhooks>
  5. new GrenadeLauncherClip[MAXPLAYERS+1];
  6. new GrenadeLauncherAmmo[MAXPLAYERS+1];
  7. new GrenadeLauncherReloadState[MAXPLAYERS+1];
  8. new WeaponEnt[MAXPLAYERS+1];
  9. new LastClip[MAXPLAYERS+1];
  10. new bool:Hooked[MAXPLAYERS+1];
  11. new Handle:l4d_gl_clips;
  12. new Handle:l4d_gl_count;
  13. new GameMode;
  14. new L4D2Version;
  15. new ammoOffset ;
  16. public Plugin:myinfo =
  17. {
  18.     name = "Multi-Clip Grenade Launcher",
  19.     author = "Pan Xiaohai",
  20.     description = "need sdk hook",
  21.     version = "1.3",
  22.     url = "<- URL ->"
  23. }
  24.  
  25. public OnPluginStart()
  26. {
  27.     ammoOffset = FindSendPropInfo("CTerrorPlayer", "m_iAmmo");
  28.     l4d_gl_clips = CreateConVar("l4d_gl_clips", "5", " max clips for grenade launcher, 0 disable this plugin", FCVAR_PLUGIN);
  29.     l4d_gl_count = CreateConVar("l4d_gl_count", "2", "clip count for every reloading, must >0", FCVAR_PLUGIN); 
  30.     GameCheck();
  31.     if(L4D2Version && GameMode!=2)
  32.     {
  33.      
  34.         HookEvent("weapon_reload", weapon_reload );
  35.        
  36.         HookEvent("player_spawn", player_spawn);   
  37.         HookEvent("player_death", player_death);
  38.        
  39.         HookEvent("player_bot_replace", player_bot_replace );    
  40.         HookEvent("bot_player_replace", bot_player_replace );  
  41.      
  42.         HookEvent("round_end", round_end );
  43.         HookEvent("map_transition", round_end );       
  44.     }  
  45.     AutoExecConfig(true, "l4d_grenade_launcher_v11");
  46. }
  47.  
  48.  
  49. GameCheck()
  50. {
  51.     decl String:GameName[16];
  52.     GetConVarString(FindConVar("mp_gamemode"), GameName, sizeof(GameName));
  53.    
  54.    
  55.     if (StrEqual(GameName, "survival", false))
  56.         GameMode = 3;
  57.     else if (StrEqual(GameName, "versus", false) || StrEqual(GameName, "teamversus", false) || StrEqual(GameName, "scavenge", false) || StrEqual(GameName, "teamscavenge", false))
  58.         GameMode = 2;
  59.     else if (StrEqual(GameName, "coop", false) || StrEqual(GameName, "realism", false))
  60.         GameMode = 1;
  61.     else
  62.     {
  63.         GameMode = 0;
  64.     }
  65.      
  66.     GetGameFolderName(GameName, sizeof(GameName));
  67.     if (StrEqual(GameName, "left4dead2", false))
  68.     {
  69.  
  70.         L4D2Version=true;
  71.     }  
  72.     else
  73.     {
  74.  
  75.         L4D2Version=false;
  76.     }
  77.     L4D2Version=!!L4D2Version;
  78. }
  79. ResetClientState(client)
  80. {
  81.     Hooked[client]=false;
  82.     GrenadeLauncherReloadState[client]=0;
  83.     WeaponEnt[client]=0;
  84. }
  85. public player_bot_replace(Handle:Spawn_Event, const String:Spawn_Name[], bool:Spawn_Broadcast)
  86. {
  87.     new client = GetClientOfUserId(GetEventInt(Spawn_Event, "player"));
  88.     new bot = GetClientOfUserId(GetEventInt(Spawn_Event, "bot"));  
  89.    
  90.     ResetClientState(client);
  91.     ResetClientState(bot);   
  92.    
  93.     HookWeaponSwitch(client, false);
  94.     HookWeaponSwitch(bot, false );
  95. }
  96. public bot_player_replace(Handle:Spawn_Event, const String:Spawn_Name[], bool:Spawn_Broadcast)
  97. {
  98.     new client = GetClientOfUserId(GetEventInt(Spawn_Event, "player"));
  99.     new bot = GetClientOfUserId(GetEventInt(Spawn_Event, "bot"));  
  100.  
  101.     ResetClientState(client);
  102.     ResetClientState(bot);   
  103.    
  104.     HookWeaponSwitch(client, true);
  105.     HookWeaponSwitch(bot, false );
  106. }
  107. public Action:player_spawn(Handle:hEvent, const String:strName[], bool:DontBroadcast)
  108. {
  109.     new client = GetClientOfUserId(GetEventInt(hEvent, "userid"));  
  110.     ResetClientState(client);
  111.     HookWeaponSwitch(client, true);
  112. }
  113. public Action:player_death(Handle:hEvent, const String:strName[], bool:DontBroadcast)
  114. {
  115.     new client = GetClientOfUserId(GetEventInt(hEvent, "userid"));
  116.     ResetClientState(client);
  117.     HookWeaponSwitch(client, false);
  118. }
  119.  
  120. public Action:weapon_reload (Handle:hEvent, const String:strName[], bool:DontBroadcast)
  121. {
  122.      
  123.     if(GetConVarInt(l4d_gl_clips)>1)
  124.     {
  125.         new client = GetClientOfUserId(GetEventInt(hEvent, "userid"));
  126.          
  127.         if(!Hooked[client])HookWeaponSwitch(client, true);
  128.     }
  129.  
  130. }  
  131. new HookType=SDKHook_PreThink;
  132. public Action:OnWeaponSwitch(client, weapon)
  133. {
  134.     if(GetConVarInt(l4d_gl_clips)>1)
  135.     {
  136.         new String:sweapon[32];
  137.         GetEdictClassname(weapon, sweapon, 32);
  138.         GrenadeLauncherReloadState[client]=0;
  139.         //PrintToChatAll("switch %s", sweapon);
  140.         if(StrEqual(sweapon, "weapon_grenade_launcher"))
  141.         {
  142.             SDKUnhook(client, HookType, GLPreThinkPostHook);
  143.             SDKHook(client, HookType, GLPreThinkPostHook); 
  144.             //PrintToChatAll("start  ");
  145.         }
  146.         else
  147.         {
  148.             SDKUnhook(client, HookType, GLPreThinkPostHook);
  149.             //PrintToChatAll("end  ");
  150.         }
  151.     }
  152.  
  153. }
  154. HookWeaponSwitch(client, bool:hook)
  155. {
  156.     if(client>0)
  157.     {
  158.         SDKUnhook(client, SDKHook_WeaponSwitch, OnWeaponSwitch);   
  159.         SDKUnhook(client, HookType, GLPreThinkPostHook);   
  160.         Hooked[client]=false;
  161.         if(hook && IsClientInGame(client) && GetClientTeam(client)==2 && !IsFakeClient(client) && IsPlayerAlive(client))
  162.         {
  163.             SDKHook(client, SDKHook_WeaponSwitch, OnWeaponSwitch); 
  164.             Hooked[client]=true;
  165.             //PrintToChatAll("Start HookWeaponSwitch");
  166.         }
  167.     }
  168. }
  169.  
  170. public Action:round_end(Handle:event, const String:name[], bool:dontBroadcast)
  171. {  
  172.     for(new i=1; i<=MaxClients; i++)
  173.     {
  174.         if(IsClientInGame(i))
  175.         {            
  176.          
  177.             HookWeaponSwitch(i, false);
  178.         }
  179.     }
  180. }
  181.  
  182. public GLPreThinkPostHook(client)
  183. {
  184.    
  185.     new launcher=0;
  186.     launcher=GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon" );
  187.     if(launcher<=0)return;
  188.    
  189.     decl String:weapon[64];
  190.     GetEdictClassname(launcher, weapon, 64);  
  191.     if (!StrEqual(weapon, "weapon_grenade_launcher"))
  192.     {
  193.         return;
  194.     }  
  195.    
  196.     if(launcher!=WeaponEnt[client])
  197.     {
  198.         WeaponEnt[client]=launcher;
  199.     }
  200.    
  201.     new inreload=GetEntProp(launcher, Prop_Send, "m_bInReload");   
  202.     new lastReloadState=GrenadeLauncherReloadState[client];
  203.     new clip ,ammo;
  204.     GetGrenadeLauncherAmmoClip(client, launcher, ammo, clip);
  205.     if(inreload)
  206.     {
  207.         //PrintToChatAll("reloading ammo %d, clip %d", ammo, clip);
  208.         if(!lastReloadState)
  209.         {
  210.             //PrintToChatAll("start reload %d", clip);
  211.             SetGrenadeLauncherAmmoClip(client, launcher, GrenadeLauncherAmmo[client], GrenadeLauncherClip[client]);
  212.         }
  213.     }
  214.     else
  215.     {
  216.         new button=GetClientButtons(client);
  217.         if(lastReloadState && clip==1)
  218.         {
  219.             //PrintToChatAll("realod end ");
  220.             clip=GrenadeLauncherClip[client]+GetConVarInt(l4d_gl_count);
  221.             ammo=GrenadeLauncherAmmo[client]-GetConVarInt(l4d_gl_count);
  222.             if(ammo<0)
  223.             {
  224.                 new o=ammo;
  225.                 clip+=o;
  226.                 ammo=0;
  227.             }
  228.             if(clip>GetConVarInt(l4d_gl_clips))
  229.             {
  230.                 new o=clip-GetConVarInt(l4d_gl_clips);
  231.                 clip=GetConVarInt(l4d_gl_clips);
  232.                 ammo+=o;
  233.             }
  234.             SetGrenadeLauncherAmmoClip(client, launcher, ammo, clip);
  235.         }
  236.         else if(!lastReloadState && clip==1 && (button & IN_RELOAD))
  237.         {
  238.             if(clip<GetConVarInt(l4d_gl_clips))
  239.             {
  240.              
  241.                 //PrintToChatAll("press reload");
  242.                 if(ForeceGrenadeLauncherReload(client, launcher))
  243.                 {                
  244.                     GrenadeLauncherClip[client]=clip;
  245.                     GrenadeLauncherAmmo[client]=ammo;                
  246.                 }
  247.             }
  248.         }
  249.         GrenadeLauncherClip[client]=clip;
  250.         GrenadeLauncherAmmo[client]=ammo;
  251.     }
  252.     GrenadeLauncherReloadState[client]=inreload;
  253.     LastClip[client]=clip;
  254.     return ;
  255. }
  256. ForeceGrenadeLauncherReload(client,   launcher )
  257. {
  258.  
  259.     new bool:ok=false;
  260.     if (launcher > 0)
  261.     {
  262.  
  263.         new ammo;
  264.         new clip;
  265.        
  266.  
  267.         clip = GetEntProp(launcher, Prop_Send, "m_iClip1");
  268.         ammo = GetEntData(client, ammoOffset+(68));
  269.        
  270.         if(ammo>0 && clip>0 && clip<GetConVarInt(l4d_gl_clips))
  271.         {
  272.             SetEntProp(launcher, Prop_Send, "m_iClip1", 0);  
  273.             ok=true;
  274.         }            
  275.      
  276.                  
  277.     }
  278.     return ok;
  279. }
  280. GetGrenadeLauncherAmmoClip(client, launcher, &ammo, &clip )
  281. {
  282.  
  283.     clip = GetEntProp(launcher, Prop_Send, "m_iClip1");
  284.     ammo = GetEntData(client, ammoOffset+(68));
  285.     //ammo=GetEntProp(launcher, Prop_Send, "m_iExtraPrimaryAmmo");
  286.          
  287. }
  288. SetGrenadeLauncherAmmoClip(client, launcher, ammo, clip )
  289. {
  290.     SetEntData(client, ammoOffset+(68), ammo);
  291.     SetEntProp(launcher, Prop_Send, "m_iClip1", clip);
  292. }
  293. HaveGrenadeLauncher(client)
  294. {
  295.     new index=GetPlayerWeaponSlot(client, 0);
  296.    
  297.     if (index > 0)
  298.     {
  299.         new String:weapon[32];
  300.  
  301.         GetEdictClassname(index, weapon, 32);
  302.  
  303.         if (StrEqual(weapon, "weapon_grenade_launcher"))
  304.         {
  305.              
  306.         }
  307.         else
  308.         {
  309.             index=0;
  310.         }
  311.     }
  312.     return index;
  313. }
  314. HoldGrenadeLauncher(client)
  315. {
  316.     new g=0;
  317.     new String:weapon[32];
  318.     GetClientWeapon(client, weapon, 32);     
  319.     if (StrEqual(weapon, "weapon_grenade_launcher"))
  320.     {
  321.         g=HaveGrenadeLauncher(client);
  322.     }
  323.  
  324.     return g;
  325. }
  326.  
  327. //code from "DJ_WEST"
  328.  
  329. public Action:DisplayHint(Handle:h_Timer, any:i_Client)
  330. {
  331.      
  332.     if ( IsClientInGame(i_Client))  ClientCommand(i_Client, "gameinstructor_enable 1")
  333.        
  334.     CreateTimer(0.3, DelayDisplayHint, i_Client)
  335. }
  336. public Action:DelayDisplayHint(Handle:h_Timer, any:i_Client)
  337. {
  338.  
  339.     DisplayInstructorHint(i_Client, "Press reload button to add a granade for grenade launcher", "+reload");
  340.      
  341. }
  342. public DisplayInstructorHint(i_Client, String:s_Message[256], String:s_Bind[])
  343. {
  344.     decl i_Ent, String:s_TargetName[32], Handle:h_RemovePack
  345.    
  346.     i_Ent = CreateEntityByName("env_instructor_hint")
  347.     FormatEx(s_TargetName, sizeof(s_TargetName), "hint%d", i_Client)
  348.     ReplaceString(s_Message, sizeof(s_Message), "\n", " ")
  349.     DispatchKeyValue(i_Client, "targetname", s_TargetName)
  350.     DispatchKeyValue(i_Ent, "hint_target", s_TargetName)
  351.     DispatchKeyValue(i_Ent, "hint_timeout", "5")
  352.     DispatchKeyValue(i_Ent, "hint_range", "0.01")
  353.     DispatchKeyValue(i_Ent, "hint_color", "255 255 255")
  354.     DispatchKeyValue(i_Ent, "hint_icon_onscreen", "use_binding")
  355.     DispatchKeyValue(i_Ent, "hint_caption", s_Message)
  356.     DispatchKeyValue(i_Ent, "hint_binding", s_Bind)
  357.     DispatchSpawn(i_Ent)
  358.     AcceptEntityInput(i_Ent, "ShowHint")
  359.    
  360.     h_RemovePack = CreateDataPack()
  361.     WritePackCell(h_RemovePack, i_Client)
  362.     WritePackCell(h_RemovePack, i_Ent)
  363.     CreateTimer(5.0, RemoveInstructorHint, h_RemovePack)
  364. }
  365.    
  366. public Action:RemoveInstructorHint(Handle:h_Timer, Handle:h_Pack)
  367. {
  368.     decl i_Ent, i_Client
  369.    
  370.     ResetPack(h_Pack, false)
  371.     i_Client = ReadPackCell(h_Pack)
  372.     i_Ent = ReadPackCell(h_Pack)
  373.     CloseHandle(h_Pack)
  374.    
  375.     if (!i_Client || !IsClientInGame(i_Client))
  376.         return Plugin_Handled
  377.    
  378.     if (IsValidEntity(i_Ent))
  379.             RemoveEdict(i_Ent)
  380.    
  381.     ClientCommand(i_Client, "gameinstructor_enable 0")
  382.        
  383.     DispatchKeyValue(i_Client, "targetname", "")
  384.        
  385.     return Plugin_Continue
  386. }
Advertisement
Add Comment
Please, Sign In to add comment