Advertisement
FlacoBey

Untitled

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