Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend weapon functionality #2683

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 182 additions & 3 deletions lua/entities/gmod_wire_expression2/core/weapon.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
/******************************************************************************\
--[[----------------------------------------------------------------------------
Player-weapon support
\******************************************************************************/
------------------------------------------------------------------------------]]

local CanTool = WireLib.CanTool
local isFriend = E2Lib.isFriend

local setAmmoCVar = CreateConVar("wire_expression2_weapon_ammo_set_enable", 0, FCVAR_ARCHIVE, "Whether or not to allow E2s to set ammo for weapons and players")
local giveAmmoCVar = CreateConVar("wire_expression2_weapon_ammo_give_enable", 0, FCVAR_ARCHIVE, "Whether or not to allow E2s to give ammo to players")
local giveWeaponCVar = CreateConVar("wire_expression2_weapon_give_enable", 0, FCVAR_ARCHIVE, "Whether or not to allow E2s to give weapons to players")
local stripWeaponCVar = CreateConVar("wire_expression2_weapon_strip_enable", 0, FCVAR_ARCHIVE, "Whether or not to allow E2s to strip weapons from players")

__e2setcost(2) -- temporary

[nodiscard]
e2function entity entity:weapon()
if not IsValid(this) then return nil end
if not this:IsPlayer() and not this:IsNPC() then return nil end

return this:GetActiveWeapon()
end


[nodiscard]
e2function entity entity:weapon(string weaponclassname)
if not IsValid(this) then return nil end
if not this:IsPlayer() and not this:IsNPC() then return nil end

return this:GetWeapon(weaponclassname)
end

[nodiscard]
e2function number entity:hasWeapon(string classname)
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if not this:IsPlayer() then return self:throw("Expected a Player but got Entity", 0) end

return this:HasWeapon(classname) and 1 or 0
end

[nodiscard]
e2function array entity:weapons()
if not IsValid(this) then return {} end
if not this:IsPlayer() then return {} end
Expand All @@ -29,6 +47,7 @@
return ret
end

[nodiscard]
e2function string entity:primaryAmmoType()
if not IsValid(this) then return "" end
if not this:IsWeapon() then return "" end
Expand All @@ -38,6 +57,7 @@
return game.GetAmmoName(ammoId) or ""
end

[nodiscard]
e2function string entity:secondaryAmmoType()
if not IsValid(this) then return "" end
if not this:IsWeapon() then return "" end
Expand All @@ -47,27 +67,65 @@
return game.GetAmmoName(ammoId) or ""
end

[nodiscard]
e2function number entity:ammoCount(string ammo_type)
if not IsValid(this) then return 0 end
if not this:IsPlayer() then return 0 end

return this:GetAmmoCount(ammo_type)
end

[nodiscard]
e2function number entity:clip1()
if not IsValid(this) then return 0 end
if not this:IsWeapon() then return 0 end

return this:Clip1()
end

[nodiscard]
e2function number entity:clip1Size()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if not this:IsWeapon() then return self:throw("Expected a Weapon but got Entity", 0) end

return this:GetMaxClip1()
end

e2function number entity:setClip1(amount)
if not setAmmoCVar:GetBool() then return self:throw("The server has disabled setting ammo", 0) end
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if not this:IsWeapon() then return self:throw("Expected a Weapon but got Entity", 0) end
if not isFriend(self.player, this) and not CanTool(self.player, this, "wire_expression2") then return self:throw("You cannot target this weapon", 0) end

return this:SetClip1(amount)
end

[nodiscard]
e2function number entity:clip2()
if not IsValid(this) then return 0 end
if not this:IsWeapon() then return 0 end

return this:Clip2()
end

[nodiscard]
e2function number entity:clip2Size()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if not this:IsWeapon() then return self:throw("Expected a Weapon but got Entity", 0) end

return this:GetMaxClip2()
end

e2function number entity:setClip2(amount)
if not setAmmoCVar:GetBool() then return self:throw("The server has disabled setting ammo", 0) end
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if not this:IsWeapon() then return self:throw("Expected a Weapon but got Entity", 0) end
if not isFriend(self.player, this) and not CanTool(self.player, this, "wire_expression2") then return self:throw("You cannot target this weapon", 0) end

return this:SetClip2(amount)
end

[nodiscard]
e2function string entity:tool()
if not IsValid(this) then return "" end
if not this:IsPlayer() then return "" end
Expand All @@ -78,3 +136,124 @@

return weapon.Mode
end

local function checkGive(self, target, classname)
if not giveWeaponCVar:GetBool() then return self:throw("The server has disabled giving weapons", false) end
if not IsValid(target) then return self:throw("Invalid entity!", false) end
if not target:IsPlayer() then return self:throw("Expected a Player but got Entity", false) end
if not isFriend(self.player, target) and not CanTool(self.player, target, "wire_expression2") then return self:throw("You cannot target this player", false) end
if not list.HasEntry("Weapon", classname) then return self:throw("Invalid weapon class '" .. classname .. "'", false) end
if hook.Run("PlayerGiveSWEP", target, classname, list.Get("Weapon")[classname]) == false then
return self:throw("The server blocked the weapon from being given", false)
end

return true
end

e2function entity entity:giveWeapon(string classname)
if not checkGive(self, this, classname) then return NULL end

return this:Give(classname)
end

e2function entity entity:giveWeapon(string classname, noAmmo)
if not checkGive(self, this, classname) then return NULL end

return this:Give(classname, noAmmo ~= 0)
end

e2function void entity:selectWeapon(string classname)
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
if not this:IsPlayer() then return self:throw("Expected a Player but got Entity", nil) end
if not isFriend(self.player, this) and not CanTool(self.player, this, "wire_expression2") then return self:throw("You cannot target this player", nil) end
if not list.HasEntry("Weapon", classname) then return self:throw("Invalid weapon class '" .. classname .. "'", nil) end

this:SelectWeapon(classname)
end

e2function number entity:giveAmmo(amount, string type)
if not giveAmmoCVar:GetBool() then return self:throw("The server has disabled giving ammo", 0) end
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if not this:IsPlayer() then return self:throw("Expected a Player but got Entity", 0) end
if not isFriend(self.player, this) and not CanTool(self.player, this, "wire_expression2") then return self:throw("You cannot target this player", 0) end
if not table.HasValue(game.GetAmmoTypes(), type) then return self:throw("Invalid ammo type: '" .. type .. "'", -1) end

return this:GiveAmmo(amount, type)
end

e2function number entity:giveAmmo(amount, string type, hidePopUp)
if not giveAmmoCVar:GetBool() then return self:throw("The server has disabled giving ammo", 0) end
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if not this:IsPlayer() then return self:throw("Expected a Player but got Entity", 0) end
if not isFriend(self.player, this) and not CanTool(self.player, this, "wire_expression2") then return self:throw("You cannot target this player", 0) end
if not table.HasValue(game.GetAmmoTypes(), type) then return self:throw("Invalid ammo type: '" .. type .. "'", -1) end

return this:GiveAmmo(amount, type, hidePopUp ~= 0)
end

e2function void entity:setAmmo(ammoCount, string type)
if not setAmmoCVar:GetBool() then return self:throw("The server has disabled setting ammo", 0) end
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
if not this:IsPlayer() then return self:throw("Expected a Player but got Entity", nil) end
if not isFriend(self.player, this) and not CanTool(self.player, this, "wire_expression2") then return self:throw("You cannot target this player", nil) end
if not table.HasValue(game.GetAmmoTypes(), type) then return self:throw("Invalid ammo type: '" .. type .. "'", nil) end

this:SetAmmo(ammoCount, type)
end

e2function void entity:removeAmmo(ammoCount, string type)
if not setAmmoCVar:GetBool() then return self:throw("The server has disabled setting ammo", nil) end
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
aske02 marked this conversation as resolved.
Show resolved Hide resolved
if not this:IsPlayer() then return self:throw("Expected a Player but got Entity", nil) end
if not isFriend(self.player, this) and not CanTool(self.player, this, "wire_expression2") then return self:throw("You cannot target this player", nil) end
if not table.HasValue(game.GetAmmoTypes(), type) then return self:throw("Invalid ammo type: '" .. type .. "'", nil) end

this:RemoveAmmo(ammoCount, type)
end

e2function void entity:removeAllAmmo()
if not setAmmoCVar:GetBool() then return self:throw("The server has disabled setting ammo", nil) end
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
if not this:IsPlayer() then return self:throw("Expected a Player but got Entity", nil) end
if not isFriend(self.player, this) and not CanTool(self.player, this, "wire_expression2") then return self:throw("You cannot target this player", nil) end

this:RemoveAllAmmo()
end

e2function void entity:stripWeapon(string classname)
if not stripWeaponCVar:GetBool() then return self:throw("The server has disabled stripping weapons", nil) end
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
aske02 marked this conversation as resolved.
Show resolved Hide resolved
if not this:IsPlayer() then return self:throw("Expected a Player but got Entity", nil) end
if not isFriend(self.player, this) and not CanTool(self.player, this, "wire_expression2") then return self:throw("You cannot target this player", nil) end
if not list.HasEntry("Weapon", classname) then return self:throw("Invalid weapon class '" .. classname .. "'", nil) end

this:StripWeapon(classname)
end

e2function void entity:stripWeapons()
if not stripWeaponCVar:GetBool() then return self:throw("The server has disabled stripping weapons", nil) end
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
if not this:IsPlayer() then return self:throw("Expected a Player but got Entity", nil) end
if not isFriend(self.player, this) and not CanTool(self.player, this, "wire_expression2") then return self:throw("You cannot target this player", nil) end

this:StripWeapons()
end

E2Lib.registerEvent("weaponPickup", {
{ "Weapon", "e" },
{ "Owner", "e" }
})

hook.Add("WeaponEquip", "E2_weaponPickup", function(weapon, owner)
E2Lib.triggerEvent("weaponPickup", { weapon, owner })
end)

E2Lib.registerEvent("weaponSwitched", {
{ "Player", "e" },
{ "OldWeapon", "e" },
{ "NewWeapon", "e" }
})

hook.Add("PlayerSwitchWeapon", "E2_weaponSwitched", function(player, oldWeapon, newWeapon)
E2Lib.triggerEvent("weaponSwitched", { player, oldWeapon, newWeapon })
end)
15 changes: 15 additions & 0 deletions lua/wire/client/e2descriptions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,21 @@ E2Helper.Descriptions["weapon(e:)"] = "Returns the weapon that player E is curre
E2Helper.Descriptions["weapons(e:)"] = "Returns the weapons that player E has"
E2Helper.Descriptions["clip1(e:)"] = "Returns the amount of ammo in the primary clip of weapon E, -1 if there is no primary clip"
E2Helper.Descriptions["clip2(e:)"] = "Returns the amount of ammo in the secondary clip of weapon E, -1 if there is no secondary clip 1)"
E2Helper.Descriptions["hasWeapon(e:s)"] = "Returns 1 if player E has a weapon with class S, 0 otherwise"
E2Helper.Descriptions["clip1Size(e:)"] = "Returns the maximum size of the primary clip"
E2Helper.Descriptions["setClip1(e:n)"] = "Sets the amount of ammo in the primary clip of weapon E. Requires wire_expression2_weapon_ammo_set_enable to be set to 1."
E2Helper.Descriptions["clip2Size(e:)"] = "Returns the maximum size of the secondary clip"
E2Helper.Descriptions["setClip2(e:n)"] = "Sets the amount of ammo in the secondary clip of weapon E. Requires wire_expression2_weapon_ammo_set_enable to be set to 1."
E2Helper.Descriptions["giveWeapon(e:s)"] = "Gives player E the weapon with class S. Requires wire_expression2_weapon_give_enable to be set to 1."
E2Helper.Descriptions["giveWeapon(e:sn)"] = "Gives player E the weapon with class S. If N is not 0, the weapon will be given with full ammo. Requires wire_expression2_weapon_give_enable to be set to 1."
E2Helper.Descriptions["selectWeapon(e:s)"] = "Sets the active weapon with class S on player E"
E2Helper.Descriptions["giveAmmo(e:ns)"] = "Gives the player E N amount of ammo of type S. Requires wire_expression2_weapon_ammo_give_enable to be set to 1."
E2Helper.Descriptions["giveAmmo(e:nsn)"] = "Gives the player E N amount of ammo of type S. If N is not 0, the pop-up will not appear. Requires wire_expression2_weapon_ammo_give_enable to be set to 1."
E2Helper.Descriptions["setAmmo(e:ns)"] = "Sets the amount of ammo of type S on player E to N. Requires wire_expression2_weapon_ammo_set_enable to be set to 1."
E2Helper.Descriptions["removeAmmo(e:ns)"] = "Removes N amount ammo of type S from player E. Requires wire_expression2_weapon_ammo_set_enable to be set to 1."
E2Helper.Descriptions["removeAllAmmo(e:)"] = "Removes all ammo from the player E. Requires wire_expression2_weapon_ammo_set_enable to be set to 1."
E2Helper.Descriptions["stripWeapon(e:s)"] = "Removes the weapon with class S from player E. Requires wire_expression2_weapon_strip_enable to be set to 1."
E2Helper.Descriptions["stripWeapons(e:)"] = "Removes all weapons from player E. Requires wire_expression2_weapon_strip_enable to be set to 1."
E2Helper.Descriptions["primaryAmmoType(e:)"] = "Returns the name of the primary weapon's ammo"
E2Helper.Descriptions["secondaryAmmoType(e:)"] = "Returns the name of the secondary weapon's ammo"
E2Helper.Descriptions["ammoCount(e:s)"] = "Returns the amount of stored ammo of type S on player E, excluding current clip"
Expand Down
Loading