Files
wnsrc/gamemodes/helix/gamemode/items/base/sh_weapons.lua

334 lines
8.1 KiB
Lua
Raw Normal View History

2024-08-04 23:54:45 +03:00
--[[
| This file was obtained through the combined efforts
| of Madbluntz & Plymouth Antiquarian Society.
|
| Credits: lifestorm, Gregory Wayne Rossel JR.,
| Maloy, DrPepper10 @ RIP, Atle!
|
| Visit for more: https://plymouth.thetwilightzone.ru/
--]]
ITEM.name = "Weapon"
ITEM.description = "A Weapon."
ITEM.category = "Weapons"
ITEM.model = "models/weapons/w_pistol.mdl"
ITEM.class = "weapon_pistol"
ITEM.width = 2
ITEM.height = 2
ITEM.isWeapon = true
ITEM.isGrenade = false
ITEM.weaponCategory = "sidearm"
ITEM.useSound = "items/ammo_pickup.wav"
-- Inventory drawing
if (CLIENT) then
function ITEM:PaintOver(item, w, h)
if (item:GetData("equip")) then
surface.SetDrawColor(110, 255, 110, 100)
surface.DrawRect(w - 14, h - 14, 8, 8)
end
end
function ITEM:PopulateTooltip(tooltip)
if (self:GetData("equip")) then
local name = tooltip:GetRow("name")
name:SetBackgroundColor(derma.GetColor("Success", tooltip))
end
end
end
-- On item is dropped, Remove a weapon from the player and keep the ammo in the item.
ITEM:Hook("drop", function(item)
local inventory = ix.item.inventories[item.invID]
if (!inventory) then
return
end
-- the item could have been dropped by someone else (i.e someone searching this player), so we find the real owner
local owner
for client, character in ix.util.GetCharacters() do
if (character:GetID() == inventory.owner) then
owner = client
break
end
end
if (!IsValid(owner)) then
return
end
if (item:GetData("equip")) then
item:SetData("equip", nil)
owner.carryWeapons = owner.carryWeapons or {}
local weapon = owner.carryWeapons[item.weaponCategory]
if (!IsValid(weapon)) then
weapon = owner:GetWeapon(item.class)
end
if (IsValid(weapon)) then
item:SetData("ammo", weapon:Clip1())
owner:StripWeapon(item.class)
owner.carryWeapons[item.weaponCategory] = nil
if (ix.config.Get("equipSoundEnabled", true)) then
owner:EmitSound(item.useSound, ix.config.Get("equipSoundLevel", 60))
end
end
item:RemovePAC(owner)
end
end)
-- On player uneqipped the item, Removes a weapon from the player and keep the ammo in the item.
ITEM.functions.EquipUn = { -- sorry, for name order.
name = "Unequip",
tip = "equipTip",
icon = "icon16/cross.png",
OnRun = function(item)
item:Unequip(item.player, true)
return false
end,
OnCanRun = function(item)
local client = item.player
return !IsValid(item.entity) and IsValid(client) and item:GetData("equip") == true and
hook.Run("CanPlayerUnequipItem", client, item) != false
end
}
-- On player eqipped the item, Gives a weapon to player and load the ammo data from the item.
ITEM.functions.Equip = {
name = "Equip",
tip = "equipTip",
icon = "icon16/tick.png",
OnRun = function(item)
item:Equip(item.player)
return false
end,
OnCanRun = function(item)
local client = item.player
return !IsValid(item.entity) and IsValid(client) and item:GetData("equip") != true and
hook.Run("CanPlayerEquipItem", client, item) != false
end
}
function ITEM:WearPAC(client)
if (ix.pac and self.pacData) then
client:AddPart(self.uniqueID, self)
end
end
function ITEM:RemovePAC(client)
if (ix.pac and self.pacData) then
client:RemovePart(self.uniqueID)
end
end
function ITEM:Equip(client, bNoSelect, bNoSound)
local items = client:GetCharacter():GetInventory():GetItems()
client.carryWeapons = client.carryWeapons or {}
for _, v in pairs(items) do
if (v.id != self.id) then
local itemTable = ix.item.instances[v.id]
if (!itemTable) then
client:NotifyLocalized("tellAdmin", "wid!xt")
return false
else
if (itemTable.isWeapon and client.carryWeapons[self.weaponCategory] and itemTable:GetData("equip")) then
client:NotifyLocalized("weaponSlotFilled", self.weaponCategory)
return false
end
end
end
end
if (client:HasWeapon(self.class)) then
client:StripWeapon(self.class)
end
local weapon = client:Give(self.class, !self.isGrenade)
if (IsValid(weapon)) then
local ammoType = weapon:GetPrimaryAmmoType()
client.carryWeapons[self.weaponCategory] = weapon
if (!bNoSelect) then
client:SelectWeapon(weapon:GetClass())
end
if (!bNoSound) then
if (ix.config.Get("equipSoundEnabled")) then
client:EmitSound(self.useSound, ix.config.Get("equipSoundLevel", 60))
end
end
-- Remove default given ammo.
if (client:GetAmmoCount(ammoType) == weapon:Clip1() and self:GetData("ammo", 0) == 0) then
client:RemoveAmmo(weapon:Clip1(), ammoType)
end
-- assume that a weapon with -1 clip1 and clip2 would be a throwable (i.e hl2 grenade)
-- TODO: figure out if this interferes with any other weapons
if (weapon:GetMaxClip1() == -1 and weapon:GetMaxClip2() == -1 and client:GetAmmoCount(ammoType) == 0) then
client:SetAmmo(1, ammoType)
end
self:SetData("equip", true)
if (self.isGrenade) then
weapon:SetClip1(1)
client:SetAmmo(0, ammoType)
else
weapon:SetClip1(self:GetData("ammo", 0))
end
weapon.ixItem = self
if (self.OnEquipWeapon) then
self:OnEquipWeapon(client, weapon)
end
else
print(Format("[Helix] Cannot equip weapon - %s does not exist!", self.class))
end
end
function ITEM:Unequip(client, bPlaySound, bRemoveItem)
client.carryWeapons = client.carryWeapons or {}
local weapon = client.carryWeapons[self.weaponCategory]
if (!IsValid(weapon)) then
weapon = client:GetWeapon(self.class)
end
if (IsValid(weapon)) then
weapon.ixItem = nil
self:SetData("ammo", weapon:Clip1())
client:StripWeapon(self.class)
else
print(Format("[Helix] Cannot unequip weapon - %s does not exist!", self.class))
end
if (bPlaySound and ix.config.Get("equipSoundEnabled")) then
client:EmitSound(self.useSound, ix.config.Get("equipSoundLevel", 60))
end
client.carryWeapons[self.weaponCategory] = nil
self:SetData("equip", nil)
self:RemovePAC(client)
if (self.OnUnequipWeapon) then
self:OnUnequipWeapon(client, weapon)
end
if (bRemoveItem) then
self:Remove()
end
end
function ITEM:CanTransfer(oldInventory, newInventory)
if (newInventory and self:GetData("equip")) then
local owner = self:GetOwner()
if (IsValid(owner)) then
owner:NotifyLocalized("equippedWeapon")
end
return false
end
return true
end
function ITEM:OnLoadout()
if (self:GetData("equip")) then
local client = self.player
client.carryWeapons = client.carryWeapons or {}
local weapon = client:Give(self.class, true)
if (IsValid(weapon)) then
client:RemoveAmmo(weapon:Clip1(), weapon:GetPrimaryAmmoType())
client.carryWeapons[self.weaponCategory] = weapon
weapon.ixItem = self
weapon:SetClip1(self:GetData("ammo", 0))
if (self.OnEquipWeapon) then
self:OnEquipWeapon(client, weapon)
end
else
print(Format("[Helix] Cannot give weapon - %s does not exist!", self.class))
end
end
end
function ITEM:OnSave()
local weapon = self.player:GetWeapon(self.class)
if (IsValid(weapon) and weapon.ixItem == self and self:GetData("equip")) then
self:SetData("ammo", weapon:Clip1())
end
end
function ITEM:OnRemoved()
local inventory = ix.item.inventories[self.invID]
local owner = inventory.GetOwner and inventory:GetOwner()
if (IsValid(owner) and owner:IsPlayer()) then
local weapon = owner:GetWeapon(self.class)
if (IsValid(weapon)) then
weapon:Remove()
end
self:RemovePAC(owner)
end
end
hook.Add("PlayerDeath", "ixStripClip", function(client)
client.carryWeapons = {}
if (!client:GetCharacter()) then return end
for _, v in pairs(client:GetCharacter():GetInventory():GetItems()) do
if (v.isWeapon and v:GetData("equip")) then
v:SetData("ammo", nil)
v:SetData("equip", nil)
if (v.pacData) then
v:RemovePAC(client)
end
end
end
end)
hook.Add("EntityRemoved", "ixRemoveGrenade", function(entity)
-- hack to remove hl2 grenades after they've all been thrown
if (entity:GetClass() == "weapon_frag") then
local client = entity:GetOwner()
if (IsValid(client) and client:IsPlayer() and client:GetCharacter()) then
local ammoName = game.GetAmmoName(entity:GetPrimaryAmmoType())
if (isstring(ammoName) and ammoName:lower() == "grenade" and client:GetAmmoCount(ammoName) < 1
and entity.ixItem and entity.ixItem.Unequip) then
entity.ixItem:Unequip(client, false, true)
end
end
end
end)