Advertisement
FlacoBey

Untitled

Feb 15th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.70 KB | None | 0 0
  1. #pragma semicolon 1
  2. #pragma newdecls required
  3.  
  4. #include <sourcemod>
  5. #include <sdktools>
  6.  
  7. iLastWeapon[MAXPLAYERS+1];
  8.  
  9. bool bM60[MAXPLAYERS+1] = {false, ...};
  10. bool InReloadM60[MAXPLAYERS+1] = {false, ...};
  11.  
  12. Handle M60AmmoCVAR = INVALID_HANDLE;
  13. Handle M60AmmoReserveCVAR = INVALID_HANDLE;
  14. Handle M60ResupplyCVAR = INVALID_HANDLE;
  15. Handle ReloadNotifyCVAR = INVALID_HANDLE;
  16.  
  17. public Plugin myinfo =
  18. {
  19.     name = "[L4D2] Improved Prevent M60 Drop",
  20.     author = "MasterMind420, Ludastar, DeathChaos25",
  21.     description = "Prevents dropping the M60 when ammo runs out and allows reloading",
  22.     version = "1.6",
  23.     url = ""
  24. };
  25.  
  26. public void OnPluginStart()
  27. {
  28.     char sGame[32];
  29.     GetGameFolderName(sGame, sizeof(sGame));
  30.  
  31.     if (!StrEqual(sGame, "left4dead2", false))
  32.         SetFailState("Plugin supports Left 4 Dead 2 only.");
  33.  
  34.     ReloadNotifyCVAR = CreateConVar("m60_reload_notify", "2", "0 = Disabled, 1 = Chat, 2 = Hint ", FCVAR_NOTIFY);
  35.     M60AmmoCVAR = CreateConVar("m60_ammo", "150", " How much Ammo for the M60 ", FCVAR_NOTIFY);
  36.     M60AmmoReserveCVAR = CreateConVar("m60_ammo_reserve", "300", " How much Ammo Reserve for the M60 ", FCVAR_NOTIFY);
  37.     M60ResupplyCVAR = CreateConVar("m60_resupply", "1", " Do you allow players to resupply the M60 off ammospots ", FCVAR_NOTIFY);
  38.  
  39.     HookConVarChange(M60AmmoReserveCVAR, CVARChanged);
  40.  
  41.     HookEvent("player_use", ePlayerUse);
  42.     HookEvent("weapon_fire", eWeaponFire);
  43.     //HookEvent("item_pickup", eItemPickup);
  44.     HookEvent("receive_upgrade", eAmmoUpgrade);
  45.     HookEvent("upgrade_explosive_ammo", eAmmoUpgrade);
  46.     HookEvent("upgrade_incendiary_ammo", eAmmoUpgrade);
  47.     HookEvent("upgrade_pack_added", eSpecialAmmo);
  48.     HookEvent("weapon_reload", eWeaponReloadPre, EventHookMode_Pre);
  49.  
  50.     //CreateTimer(0.1, M60AmmoCheck, _, TIMER_REPEAT);
  51.  
  52.     AutoExecConfig(true, "l4d2_improved_prevent_m60_drop");
  53.  
  54.     CvarsChanged();
  55. }
  56.  
  57. public void OnMapStart()
  58. {
  59.     if (!IsModelPrecached("models/w_models/weapons/w_m60.mdl"))
  60.         PrecacheModel("models/w_models/weapons/w_m60.mdl");
  61.  
  62.     if (!IsModelPrecached("models/v_models/v_m60.mdl"))
  63.         PrecacheModel("models/v_models/v_m60.mdl");
  64.  
  65.     CvarsChanged();
  66. }
  67.  
  68. public void CVARChanged(Handle hCvar, const char[] sOldVal, const char[] sNewVal)
  69. {
  70.     CvarsChanged();
  71. }
  72.  
  73. void CvarsChanged()
  74. {
  75.     SetConVarInt(FindConVar("ammo_m60_max"), GetConVarInt(M60AmmoReserveCVAR));
  76. }
  77.  
  78. public Action M60AmmoCheck(Handle Timer)
  79. {
  80.     int ReserveAmmo = GetConVarInt(FindConVar("ammo_m60_max"));
  81.  
  82.     for (int i = 1; i <= MaxClients; i++)
  83.     {
  84.         if (!IsSurvivor(i) || !IsPlayerAlive(i))
  85.             continue;
  86.  
  87.         int Weapon = GetPlayerWeaponSlot(i, 0);
  88.  
  89.         if (Weapon == -1)
  90.             continue;
  91.  
  92.         if(iLastWeapon[i] != Weapon)
  93.         {
  94.             iLastWeapon[i] = Weapon;
  95.  
  96.             char sWeapon[17];
  97.             GetEntityClassname(Weapon, sWeapon, sizeof(sWeapon));
  98.  
  99.             if(sWeapon[13] != 'm' || !StrEqual(sWeapon, "weapon_rifle_m60"))
  100.             {
  101.                 bM60[i] = false;
  102.                 continue;
  103.             }
  104.  
  105.             bM60[i] = true;
  106.         }
  107.  
  108.         if(!bM60[i])
  109.             continue;
  110.  
  111.         int Clip = GetEntProp(Weapon, Prop_Data, "m_iClip1");
  112.         int PrimType = GetEntProp(Weapon, Prop_Send, "m_iPrimaryAmmoType");
  113.         int Ammo = GetEntProp(i, Prop_Send, "m_iAmmo", _, PrimType);
  114.  
  115.         if (Clip == 0 && Ammo > ReserveAmmo + 150)
  116.             SetEntProp(i, Prop_Send, "m_iAmmo", ReserveAmmo + 150, _, PrimType);
  117.         else if (Clip > 0 && Ammo > ReserveAmmo)
  118.             SetEntProp(i, Prop_Send, "m_iAmmo", ReserveAmmo + (150 - Clip), _, PrimType);
  119.     }
  120. }
  121.  
  122. public void eItemPickup(Event event, const char[] name, bool dontBroadcast)
  123. {
  124.     int client = GetClientOfUserId(event.GetInt("userid"));
  125.  
  126.     if (!IsSurvivor(client) || !IsPlayerAlive(client))
  127.         return;
  128.  
  129.     int Weapon = GetEntPropEnt(client, Prop_Data, "m_hActiveWeapon");
  130.  
  131.     if (Weapon == -1)
  132.         return;
  133.  
  134.     if(iLastWeapon[client] != Weapon)
  135.     {
  136.         iLastWeapon[client] = Weapon;
  137.  
  138.         char sWeapon[32];
  139.         GetEntityClassname(Weapon, sWeapon, sizeof(sWeapon));
  140.  
  141.         if(sWeapon[13] != 'm' || !StrEqual(sWeapon, "weapon_rifle_m60"))
  142.         {
  143.             bM60[client] = false;
  144.             return;
  145.         }
  146.  
  147.         bM60[client] = true;
  148.     }
  149.  
  150.     if(!bM60[client])
  151.         return;
  152.  
  153.     int AmmoType = GetEntProp(Weapon, Prop_Data, "m_iPrimaryAmmoType");
  154.  
  155.     if (AmmoType == -1)
  156.         return;
  157.  
  158.     SetEntProp(Weapon, Prop_Data, "m_iClip1", GetConVarInt(M60AmmoCVAR), 1);
  159.     SetEntProp(client, Prop_Send, "m_iAmmo", GetConVarInt(M60AmmoReserveCVAR), _, AmmoType);
  160. }
  161.  
  162. public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float vel[3], float angles[3], int &weapon)
  163. {
  164.     if (!IsSurvivor(client))
  165.         return;
  166.  
  167.     static int slot;
  168.     slot = GetPlayerWeaponSlot(client, 0);
  169.  
  170.     if (IsValidEntity(slot))
  171.     {
  172.         char sWeapon[17];
  173.         GetEntityClassname(slot, sWeapon, sizeof(sWeapon));
  174.  
  175.         if(!StrEqual(sWeapon, "weapon_rifle_m60"))
  176.             return;
  177.  
  178.         int Clip = GetEntProp(slot, Prop_Data, "m_iClip1");
  179.         int PrimType = GetEntProp(slot, Prop_Send, "m_iPrimaryAmmoType");
  180.         int Ammo = GetEntProp(client, Prop_Send, "m_iAmmo", _, PrimType);
  181.         int ReserveAmmo = GetConVarInt(FindConVar("ammo_m60_max"));
  182.  
  183.         if (Clip == 0 && Ammo > ReserveAmmo + 150)
  184.             SetEntProp(client, Prop_Send, "m_iAmmo", ReserveAmmo + 150, _, PrimType);
  185.         else if (Clip > 0 && Ammo > ReserveAmmo)
  186.             SetEntProp(client, Prop_Send, "m_iAmmo", ReserveAmmo + (150 - Clip), _, PrimType);
  187.         else if (Ammo <= 0 && Clip <= 1)
  188.             SetEntPropFloat(slot, Prop_Send, "m_flNextPrimaryAttack", GetGameTime() + 1.0);
  189.     }
  190. }
  191.        
  192. public void eWeaponFire(Event event, const char[] name, bool dontBroadcast)
  193. {
  194.     int client = GetClientOfUserId(event.GetInt("userid"));
  195.  
  196.     if (!IsSurvivor(client) || !IsPlayerAlive(client))
  197.         return;
  198.  
  199.     int Weapon = GetEntPropEnt(client, Prop_Data, "m_hActiveWeapon");
  200.  
  201.     if (Weapon == -1)
  202.         return;
  203.  
  204.     if(iLastWeapon[client] != Weapon)
  205.     {
  206.         iLastWeapon[client] = Weapon;
  207.  
  208.         char sWeapon[17];
  209.         GetEntityClassname(Weapon, sWeapon, sizeof(sWeapon));
  210.  
  211.         if(sWeapon[13] != 'm' || !StrEqual(sWeapon, "weapon_rifle_m60"))
  212.         {
  213.             bM60[client] = false;
  214.             return;
  215.         }
  216.  
  217.         bM60[client] = true;
  218.     }
  219.  
  220.     if(!bM60[client])
  221.         return;
  222.  
  223.     int Clip = GetEntProp(Weapon, Prop_Data, "m_iClip1");
  224.     int PrimType = GetEntProp(Weapon, Prop_Send, "m_iPrimaryAmmoType");
  225.     int Ammo = GetEntProp(client, Prop_Send, "m_iAmmo", _, PrimType);
  226.     int Laser = GetEntProp(Weapon, Prop_Send, "m_upgradeBitVec");
  227.     int InReload = GetEntProp(Weapon, Prop_Data, "m_bInReload");
  228.  
  229.     if (Clip == 1)
  230.         InReloadM60[client] = true;
  231.  
  232.     if (InReload)
  233.         return;
  234.  
  235.     if (Clip <= 1 && InReloadM60[client])
  236.     {
  237.         AcceptEntityInput(Weapon, "kill");
  238.         int M60 = CreateEntityByName("weapon_rifle_m60");
  239.         DispatchSpawn(M60);
  240.         EquipPlayerWeapon(client, M60);
  241.         SetEntProp(M60, Prop_Send, "m_iClip1", 0);
  242.         SetEntProp(client, Prop_Send, "m_iAmmo", Ammo, _, PrimType);
  243.         SetEntProp(M60, Prop_Send, "m_upgradeBitVec", Laser);
  244.         InReloadM60[client] = false;
  245.     }
  246. }
  247.  
  248. public void eWeaponReloadPre(Event event, const char[] name, bool dontBroadcast)
  249. {
  250.     int client = GetClientOfUserId(event.GetInt("userid"));
  251.  
  252.     if (!IsSurvivor(client) || !IsPlayerAlive(client))
  253.         return;
  254.  
  255.     int Weapon = GetPlayerWeaponSlot(client, 0);
  256.  
  257.     if (Weapon == -1)
  258.         return;
  259.  
  260.     if(iLastWeapon[client] != Weapon)
  261.     {
  262.         iLastWeapon[client] = Weapon;
  263.  
  264.         char sWeapon[32];
  265.         GetEntityClassname(Weapon, sWeapon, sizeof(sWeapon));
  266.  
  267.         if(sWeapon[13] != 'm' || !StrEqual(sWeapon, "weapon_rifle_m60"))
  268.         {
  269.             bM60[client] = false;
  270.             return;
  271.         }
  272.  
  273.         bM60[client] = true;
  274.     }
  275.  
  276.     if(!bM60[client])
  277.         return;
  278.  
  279.     SetEntProp(Weapon, Prop_Send, "m_releasedFireButton", 1);
  280.     SetEntPropFloat(Weapon, Prop_Send, "m_flNextPrimaryAttack", GetGameTime() + 2.0);
  281. }
  282.  
  283. public void eAmmoUpgrade(Event event, const char[] name, bool dontBroadcast)
  284. {
  285.     int client = GetClientOfUserId(event.GetInt("userid"));
  286.  
  287.     if (!IsSurvivor(client) || !IsPlayerAlive(client))
  288.         return;
  289.  
  290.     int Weapon = GetPlayerWeaponSlot(client, 0);
  291.  
  292.     if (Weapon == -1)
  293.         return;
  294.  
  295.     if(iLastWeapon[client] != Weapon)
  296.     {
  297.         iLastWeapon[client] = Weapon;
  298.  
  299.         char sWeapon[32];
  300.         GetEntityClassname(Weapon, sWeapon, sizeof(sWeapon));
  301.  
  302.         if(sWeapon[13] != 'm' || !StrEqual(sWeapon, "weapon_rifle_m60"))
  303.         {
  304.             bM60[client] = false;
  305.             return;
  306.         }
  307.  
  308.         bM60[client] = true;
  309.     }
  310.  
  311.     if(!bM60[client])
  312.         return;
  313.  
  314.     int Clip = GetEntProp(Weapon, Prop_Data, "m_iClip1");
  315.  
  316.     if (Clip != 0)
  317.         return;
  318.  
  319.     SetEntProp(Weapon, Prop_Send, "m_nUpgradedPrimaryAmmoLoaded", Clip, 1);
  320.     SetEntProp(Weapon, Prop_Send, "m_iClip1", Clip);
  321. }
  322.  
  323. public void ePlayerUse(Event event, const char[] name, bool dontBroadcast)
  324. {
  325.     if (GetConVarInt(M60ResupplyCVAR) != 1)
  326.         return;
  327.  
  328.     int client = GetClientOfUserId(event.GetInt("userid"));
  329.  
  330.     if (!IsSurvivor(client) || !IsPlayerAlive(client))
  331.         return;
  332.  
  333.     int AmmoPile = event.GetInt("targetid");
  334.  
  335.     if (!IsValidEntity(AmmoPile))
  336.         return;
  337.  
  338.     char sWeapon[32];
  339.     GetEntityClassname(AmmoPile, sWeapon, sizeof(sWeapon));
  340.  
  341.     if (!StrEqual(sWeapon, "weapon_ammo_spawn", false))
  342.         return;
  343.  
  344.     int Weapon = GetPlayerWeaponSlot(client, 0);
  345.  
  346.     if (Weapon == -1)
  347.         return;
  348.  
  349.     GetEntityClassname(Weapon, sWeapon, sizeof(sWeapon));
  350.  
  351.     if (!StrEqual(sWeapon, "weapon_rifle_m60", false))
  352.         return;
  353.  
  354.     int AmmoType = GetEntProp(Weapon, Prop_Data, "m_iPrimaryAmmoType");
  355.  
  356.     if (AmmoType == -1)
  357.         return;
  358.  
  359.     int Clip = GetEntProp(Weapon, Prop_Send, "m_iClip1");
  360.     int Ammo = GetEntProp(client, Prop_Data, "m_iAmmo", _, AmmoType);
  361.  
  362.     float cPos[3];
  363.     float aPos[3];
  364.  
  365.     GetEntPropVector(client, Prop_Data, "m_vecAbsOrigin", cPos);
  366.     GetEntPropVector(AmmoPile, Prop_Data, "m_vecAbsOrigin", aPos);
  367.  
  368.     if (GetVectorDistance(cPos, aPos) <= 100)
  369.     {
  370.         //DONT RELOAD WHEN FULL
  371.         if (Ammo >= GetConVarInt(M60AmmoReserveCVAR) && Clip == GetConVarInt(M60AmmoCVAR))
  372.             return;
  373.         else if (Ammo > GetConVarInt(M60AmmoReserveCVAR) && Clip <= GetConVarInt(M60AmmoCVAR))
  374.             return;
  375.  
  376.         //SET CLIP & RESERVE TO 0 WHEN 1 AMMO LEFT IN CLIP
  377.         if (Ammo < 1 && Clip == 1)
  378.         {
  379.             SetEntProp(Weapon, Prop_Send, "m_iClip1", 0);
  380.             SetEntProp(client, Prop_Send, "m_iAmmo", 0, _, AmmoType);
  381.         }
  382.  
  383.         SetEntPropFloat(Weapon, Prop_Send, "m_flNextPrimaryAttack", GetGameTime() + 0.1);
  384.         SetEntProp(client, Prop_Send, "m_iAmmo", GetConVarInt(M60AmmoReserveCVAR) + GetConVarInt(M60AmmoCVAR), _, AmmoType);
  385.  
  386.         if (GetConVarInt(ReloadNotifyCVAR) == 1)
  387.             PrintToChat(client, "\x04M60 RELOADED");
  388.         else if (GetConVarInt(ReloadNotifyCVAR) == 2)
  389.             PrintHintText(client, "M60 RELOADED");
  390.     }
  391. }
  392.  
  393. public void eSpecialAmmo(Event event, const char[] name, bool dontBroadcast)
  394. {
  395.     int client = GetClientOfUserId(event.GetInt("userid"));
  396.  
  397.     if(!IsSurvivor(client))
  398.         return;
  399.  
  400.     int Weapon = GetEntPropEnt(client, Prop_Data, "m_hActiveWeapon");
  401.  
  402.     if (Weapon == -1)
  403.         return;
  404.  
  405.     char sWeapon[32];
  406.     GetEntityClassname(Weapon, sWeapon, sizeof(sWeapon));
  407.  
  408.     if (!StrEqual(sWeapon, "weapon_rifle_m60"))
  409.         return;
  410.  
  411.     int UpgradeId = event.GetInt("upgradeid");
  412.     GetEntityClassname(UpgradeId, sWeapon, sizeof(sWeapon));
  413.  
  414.     if (StrEqual(sWeapon, "upgrade_laser_sight"))
  415.         return;
  416.  
  417.     int NewAmmo;
  418.     int Ammo = GetSpecialAmmoInPlayerGun(client);
  419.  
  420.     if (StrEqual(sWeapon, "upgrade_ammo_incendiary"))
  421.         NewAmmo = Ammo * 1;
  422.     else if (StrEqual(sWeapon, "upgrade_ammo_explosive"))
  423.         NewAmmo = Ammo * 1;
  424.  
  425.     if (NewAmmo > 1)
  426.         SetSpecialAmmoInPlayerGun(client, NewAmmo);
  427.     else
  428.         return;
  429. }
  430.  
  431. stock int GetSpecialAmmoInPlayerGun(int client)
  432. {
  433.     if (!client)
  434.         client = 1;
  435.  
  436.     int Gun = GetPlayerWeaponSlot(client, 0);
  437.  
  438.     if (IsValidEntity(Gun))
  439.         return GetEntProp(Gun, Prop_Send, "m_nUpgradedPrimaryAmmoLoaded", 1);
  440.     else
  441.         return 0;
  442. }
  443.  
  444. stock int SetSpecialAmmoInPlayerGun(int client, int amount)
  445. {
  446.     if (!client)
  447.         client = 1;
  448.  
  449.     int Gun = GetPlayerWeaponSlot(client, 0);
  450.  
  451.     if (IsValidEntity(Gun))
  452.         SetEntProp(Gun, Prop_Send, "m_nUpgradedPrimaryAmmoLoaded", amount, 1);
  453. }
  454.  
  455. stock bool IsSurvivor(int client)
  456. {
  457.     return (client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 2);
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement