DeathChaos25

[L4D2] Prevent M60 Drop on empty clip

Aug 30th, 2015
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.54 KB | None | 0 0
  1. // precompiled http://www.mediafire.com/download/09i145l87z7jp8e/%5BL4D2%5D+Prevent+M60+Drop+on+empty+clip.smx
  2.  
  3. #include <sourcemod>
  4. #include <sdktools>
  5. #pragma semicolon 1
  6.  
  7. new g_ActiveWeaponOffset;
  8. static bool:buttondelay[MAXPLAYERS + 1];
  9.  
  10. public Plugin:myinfo =
  11. {
  12.     name = "[L4D2] Prevent M60 Drop on empty clip",
  13.     author = "DeathChaos25",
  14.     description = "Prevents M60s from getting auto deleted when they run empty as well as fixing other various bugs",
  15.     version = "1.1",
  16.     url = "https://forums.alliedmods.net/showthread.php?t=266485"
  17. }
  18.  
  19.  
  20. public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
  21. {
  22.     decl String:s_GameFolder[32];
  23.     GetGameFolderName(s_GameFolder, sizeof(s_GameFolder));
  24.     if (!StrEqual(s_GameFolder, "left4dead2", false))
  25.     {
  26.         strcopy(error, err_max, "This plugin is for Left 4 Dead 2 Only!");
  27.         return APLRes_Failure;
  28.     }
  29.     return APLRes_Success;
  30. }
  31.  
  32. public OnPluginStart()
  33. {
  34.     g_ActiveWeaponOffset = FindSendPropInfo("CBasePlayer", "m_hActiveWeapon");
  35.     HookEvent("upgrade_incendiary_ammo", Event_AmmoUpgrade);
  36.     HookEvent("upgrade_explosive_ammo", Event_AmmoUpgrade);
  37.     HookEvent("receive_upgrade", Event_AmmoUpgrade);
  38. }
  39.  
  40. public OnGameFrame()
  41. {
  42.     decl String:weaponclass[128];
  43.     for (new client = 1; client <= MaxClients; client++)
  44.     {
  45.         if (IsSurvivor(client) && IsPlayerAlive(client))
  46.         {
  47.             new ReserveAmmo = GetConVarInt(FindConVar("ammo_m60_max"));
  48.             new weapon = GetPlayerWeaponSlot(client, 0);
  49.             if (IsValidEdict(weapon) && ReserveAmmo > 0)
  50.             {
  51.                 new PrimType = GetEntProp(weapon, Prop_Send, "m_iPrimaryAmmoType");
  52.                 new ammo = GetEntProp(client, Prop_Send, "m_iAmmo", _, PrimType);
  53.                 GetEdictClassname(weapon, weaponclass, sizeof(weaponclass));
  54.                 if (StrEqual(weaponclass, "weapon_rifle_m60"))
  55.                 {
  56.                     SetEntProp(weapon, Prop_Send, "m_iExtraPrimaryAmmo", ammo);
  57.                 }
  58.             }
  59.         }
  60.     }
  61. }
  62.  
  63. public Action:Event_AmmoUpgrade(Handle:event, const String:name[], bool:dontBroadcast)
  64. {
  65.     new client = GetClientOfUserId(GetEventInt(event, "userid"));
  66.    
  67.     if (!IsSurvivor(client))
  68.     {
  69.         return;
  70.     }
  71.     new weapon = GetPlayerWeaponSlot(client, 0);
  72.     decl String:s_Weapon[32];
  73.     if (IsValidEntity(weapon))
  74.     {
  75.         GetEdictClassname(weapon, s_Weapon, sizeof(s_Weapon));
  76.     }
  77.    
  78.     if (StrEqual(s_Weapon, "weapon_rifle_m60"))
  79.     {
  80.         new clip = GetEntProp(weapon, Prop_Data, "m_iClip1");
  81.         if (clip == 0)
  82.         {
  83.             SetEntProp(weapon, Prop_Send, "m_nUpgradedPrimaryAmmoLoaded", 150, 1);
  84.             SetEntProp(weapon, Prop_Send, "m_iClip1", 150);
  85.         }
  86.     }
  87. }
  88. public Action:OnPlayerRunCmd(client, &buttons, &impulse, Float:f_Velocity[3], Float:f_Angles[3], &weapon)
  89. {
  90.     if (IsSurvivor(client))
  91.     {
  92.         if (buttons & IN_ATTACK)
  93.         {
  94.             decl String:s_Weapon[32];
  95.             weapon = GetEntDataEnt2(client, g_ActiveWeaponOffset);
  96.            
  97.             if (IsValidEntity(weapon))
  98.             {
  99.                 GetEdictClassname(weapon, s_Weapon, sizeof(s_Weapon));
  100.             }
  101.             if (StrEqual(s_Weapon, "weapon_rifle_m60"))
  102.             {
  103.                 new clip = GetEntProp(weapon, Prop_Data, "m_iClip1");
  104.                 new PrimType = GetEntProp(weapon, Prop_Send, "m_iPrimaryAmmoType");
  105.                 new ammo = GetEntProp(client, Prop_Send, "m_iAmmo", _, PrimType);
  106.                 new laser = GetEntProp(weapon, Prop_Send, "m_upgradeBitVec");
  107.                
  108.                 if (clip == 0)
  109.                 {
  110.                     buttons &= ~IN_ATTACK;
  111.                     ClientCommand(client, "-reload");
  112.                     return Plugin_Changed;
  113.                 }
  114.                 else if (clip <= 1)
  115.                 {
  116.                     AcceptEntityInput(weapon, "kill");
  117.                     new M60 = CreateEntityByName("weapon_rifle_m60");
  118.                     DispatchSpawn(M60);
  119.                     EquipPlayerWeapon(client, M60);
  120.                     SetEntProp(M60, Prop_Send, "m_iClip1", 0);
  121.                     SetEntProp(client, Prop_Send, "m_iAmmo", ammo, _, PrimType);
  122.                     SetEntProp(M60, Prop_Send, "m_upgradeBitVec", laser);
  123.                     SetEntPropEnt(client, Prop_Data, "m_hActiveWeapon", M60);
  124.                     ChangeEdictState(client, FindDataMapOffs(client, "m_hActiveWeapon"));
  125.                     //AcceptEntityInput(M60, "Use", client);
  126.                 }
  127.             }
  128.         }
  129.         else if (buttons & IN_USE && !buttondelay[client])
  130.         {
  131.             new M60 = GetClientAimTarget(client, false);
  132.             decl String:s_Weapon[32];
  133.            
  134.             if (IsValidEntity(M60))
  135.             {
  136.                 GetEdictClassname(M60, s_Weapon, sizeof(s_Weapon));
  137.             }
  138.             buttondelay[client] = true;
  139.             CreateTimer(2.0, ResetDelay, client);
  140.             if (StrEqual(s_Weapon, "weapon_rifle_m60"))
  141.             {
  142.                 EquipPlayerWeapon(client, M60);
  143.             }
  144.             else if (StrEqual(s_Weapon, "weapon_ammo_spawn"))
  145.             {
  146.                 new ReserveAmmo = GetConVarInt(FindConVar("ammo_m60_max"));
  147.                 weapon = GetPlayerWeaponSlot(client, 0);
  148.                
  149.                 if (IsValidEdict(weapon))
  150.                 {
  151.                     new PrimType = GetEntProp(weapon, Prop_Send, "m_iPrimaryAmmoType");
  152.                     GetEdictClassname(weapon, s_Weapon, sizeof(s_Weapon));
  153.                     if (StrEqual(s_Weapon, "weapon_rifle_m60"))
  154.                     {
  155.                         SetEntProp(client, Prop_Send, "m_iAmmo", ReserveAmmo, _, PrimType);
  156.                         GiveHint(client);
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.     }
  162.     return Plugin_Continue;
  163. }
  164.  
  165. public Action:ResetDelay(Handle:timer, any:client)
  166. {
  167.     buttondelay[client] = false;
  168. }
  169.  
  170. stock bool:IsSurvivor(client)
  171. {
  172.     return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 2;
  173. }
  174.  
  175. stock GiveHint(client)
  176. {
  177.     if (IsClientInGame(client) && !IsFakeClient(client) && client > 0)
  178.     {
  179.         decl i_Ent, String:s_TargetName[32], String:s_Message[256], Handle:h_Pack;
  180.         i_Ent = CreateEntityByName("env_instructor_hint");
  181.         FormatEx(s_TargetName, sizeof(s_TargetName), "hint%d", client);
  182.         FormatEx(s_Message, sizeof(s_Message), "You have refueled your M60!");
  183.         ReplaceString(s_Message, sizeof(s_Message), "\n", " ");
  184.         DispatchKeyValue(client, "targetname", s_TargetName);
  185.         DispatchKeyValue(i_Ent, "hint_target", s_TargetName);
  186.         DispatchKeyValue(i_Ent, "hint_timeout", "5");
  187.         DispatchKeyValue(i_Ent, "hint_range", "0.01");
  188.         DispatchKeyValue(i_Ent, "hint_icon_onscreen", "icon_tip");
  189.         DispatchKeyValue(i_Ent, "hint_caption", s_Message);
  190.         DispatchKeyValue(i_Ent, "hint_color", "255 255 255");
  191.         DispatchSpawn(i_Ent);
  192.         AcceptEntityInput(i_Ent, "ShowHint");
  193.         h_Pack = CreateDataPack();
  194.         WritePackCell(h_Pack, client);
  195.         WritePackCell(h_Pack, i_Ent);
  196.         CreateTimer(5.0, RemoveInstructorHint, h_Pack);
  197.     }
  198. }
  199.  
  200. public Action:RemoveInstructorHint(Handle:h_Timer, Handle:h_Pack)
  201. {
  202.     decl i_Ent, client;
  203.     ResetPack(h_Pack, false);
  204.     client = ReadPackCell(h_Pack);
  205.     i_Ent = ReadPackCell(h_Pack);
  206.     CloseHandle(h_Pack);
  207.     if (!client || !IsClientInGame(client))
  208.         return Plugin_Handled;
  209.     if (IsValidEntity(i_Ent))
  210.         RemoveEdict(i_Ent);
  211.     return Plugin_Continue;
  212. }
  213.  
  214. public Action:L4D2_OnFindScavengeItem(client, &item)
  215. {
  216.     if (!item)
  217.     {
  218.         decl Float:Origin[3], Float:TOrigin[3];
  219.         new weapon = GetPlayerWeaponSlot(client, 0);
  220.         decl String:class[128];
  221.         if (IsValidEdict(weapon))
  222.         {
  223.             GetEdictClassname(weapon, class, sizeof(class));
  224.             if (StrEqual(class, "weapon_rifle_m60"))
  225.             {
  226.                 for (new i = 0; i <= GetMaxEntities(); i++)
  227.                 {
  228.                     if (IsValidEdict(i))
  229.                     {
  230.                         GetEdictClassname(i, class, sizeof(class));
  231.                         new ReserveAmmo = GetConVarInt(FindConVar("ammo_m60_max"));
  232.                         new PrimType = GetEntProp(weapon, Prop_Send, "m_iPrimaryAmmoType");
  233.                         new ammo = GetEntProp(client, Prop_Send, "m_iAmmo", _, PrimType);
  234.                         if (StrEqual("weapon_ammo_spawn", class) && ReserveAmmo > 0 && ammo < ReserveAmmo)
  235.                         {
  236.                             GetEntPropVector(i, Prop_Send, "m_vecOrigin", Origin);
  237.                             GetEntPropVector(client, Prop_Send, "m_vecOrigin", TOrigin);
  238.                             new Float:distance = GetVectorDistance(TOrigin, Origin);
  239.                             if (distance < 300)
  240.                             {
  241.                                 item = i;
  242.                                 return Plugin_Changed;
  243.                             }
  244.                         }
  245.                     }
  246.                 }
  247.             }
  248.         }
  249.     }
  250.     return Plugin_Continue;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment