Files
garrysmod-coop/gamemode/weaponsystem.lua
T
2026-09-06 14:38:17 +02:00

192 lines
7.1 KiB
Lua

CreateConVar("gc_respawndistance", 75, SERVER_CAN_EXECUTE, "Item distance to map origin to trigger a respawn")
hook.Add("PlayerSpawn", "SpawningInitializeAmmo", function(ply)
timer.Simple(0, function()
ply:ShouldDropWeapon(false)
end)
end)
hook.Add( "OnEntityCreated", "Respawn items", function( ent )
if not ent:IsValid() then
--print("Ignoring invalid item")
return
end
local entityClass = ent:GetClass()
if not string.StartWith(entityClass, "item_") and not string.StartWith(entityClass, "weapon_") then
--print("Ignoring not-item " .. entityClass)
return
end
timer.Simple(0, function()
if not ent:IsValid() then
-- print("Ignoring invalid item " .. entityClass .. " (from timer)")
return
end
local entityClass = ent:GetClass()
local entityPos = ent:GetPos()
local entityAngles = ent:GetAngles()
local entityModel = ent:GetModel()
local entityCollisionGroup = ent:GetCollisionGroup()
local entityCreatedByMap = ent:CreatedByMap()
ent:SetNWBool("blockPickup", true)
ent:SetNWVector("respawnPos", entityPos)
ent:SetNWAngle("respawnAngle", entityAngle)
ent:CallOnRemove("Respawn", function(ent)
-- print("item " .. entityClass .. " removed!")
timer.Simple(1, function()
if (entityCreatedByMap) then
entityPos = entityPos + Vector(0, 0, 3)
end
local ent = ents.Create(entityClass)
ent:SetPos(entityPos)
ent:SetAngles(entityAngles)
ent:SetModel(entityModel)
ent:SetCollisionGroup(entityCollisionGroup)
ent:Spawn()
ent:EmitSound("items/suitchargeok1.wav")
-- print("item " .. entityClass .. " recreated!")
end)
end)
local callbackId = ent:AddCallback( "OnAngleChange", function( entity, newangle )
local distance = entity:GetPos():Distance(entityPos)
if (distance < GetConVarNumber("gc_respawndistance")) then
return
end
local callbackId = entity:GetNWInt("callbackId")
entity:RemoveCallback( "OnAngleChange", callbackId )
-- print("item " .. entityClass .. " distance: " .. distance .. " callbackId: " .. callbackId)
timer.Simple(1, function()
if not entity:IsValid() then
return
end
entity:Remove()
end)
end )
ent:SetNWInt("callbackId", callbackId)
-- print("Map item processed: " .. ent:GetClass() .. " pos: " .. entityPos.x .. "," .. entityPos.y .. "," .. entityPos.z)
end)
end )
hook.Add("PlayerCanPickupWeapon", "DontPickupIfAlreadyFull", function (ply, weapon)
local className = weapon:GetClass()
for k, v in pairs(ply:GetWeapons()) do
if (v:GetClass() == className) then
local primaryAmmoType = weapon:GetPrimaryAmmoType()
local secondaryAmmoType = weapon:GetSecondaryAmmoType()
local primaryAmmoCount = ply:GetAmmoCount(primaryAmmoType)
local secondaryAmmoCount = ply:GetAmmoCount(secondaryAmmoType)
local vClip1 = v:Clip1()
local vClip2 = v:Clip2()
local vMaxClip1 = v:GetMaxClip1()
local vMaxClip2 = v:GetMaxClip2()
if ply:KeyDown(IN_USE) then
ply:ChatPrint(className .. ": vClip1:" .. vClip1 .. " maxClip1:" .. vMaxClip1 .. " vClip2:" .. vClip2.. " maxClip2:" .. vMaxClip2 ..
" playerClip1:" .. primaryAmmoCount .. " playerClip2:" .. secondaryAmmoCount ..
" wClip1:" .. weapon:Clip1() .. " wClip2:" .. weapon:Clip2())
end
local blacklist = {
"weapon_crowbar",
"weapon_fists",
"weapon_frag",
"weapon_rpg"
}
local whitelist = {
"weapon_pistol",
"weapon_357"
}
if (table.HasValue(blacklist, className)) then
local gave = false
if (vMaxClip1 < 0 and primaryAmmoCount < 15) then
local amountToGive = 15 - primaryAmmoCount
ply:GiveAmmo(amountToGive, primaryAmmoType)
--ply:ChatPrint(className .. " give1 " .. amountToGive .. " primary (" .. vClip1 .. "/" .. primaryAmmoCount .. "/" .. vMaxClip1 .. ")")
gave = true
end
if (vMaxClip2 < 0 and secondaryAmmoCount < 15) then
local amountToGive = 15 - secondaryAmmoCount
ply:GiveAmmo(amountToGive, secondaryAmmoType)
--ply:ChatPrint(className .. " give1 " .. amountToGive .. " secondary (" .. vClip2 .. "/" .. secondaryAmmoCount .. "/" .. vMaxClip2 .. ")")
gave = true
end
if gave then
return table.HasValue(whitelist, className)
end
end
if primaryAmmoCount >= vMaxClip1 and secondaryAmmoCount >= vMaxClip2 then
return false
else
if primaryAmmoCount < vMaxClip1 then
ply:GiveAmmo(math.min(weapon:Clip1(), vMaxClip1 - primaryAmmoCount), primaryAmmoType)
--ply:ChatPrint(className .. " give2 " .. math.min(weapon:Clip1(), vMaxClip1 - primaryAmmoCount) .. " primary")
end
if secondaryAmmoCount < vMaxClip2 then
ply:GiveAmmo(math.min(weapon:Clip2(), vMaxClip2 - secondaryAmmoCount), secondaryAmmoType)
--ply:ChatPrint(className .. " give2 " .. math.min(weapon:Clip2(), vMaxClip2 - secondaryAmmoCount) .. " secondary")
end
return table.HasValue(whitelist, className)
end
end
end
return true
end)
hook.Add( "WeaponEquip", "RespawnOnCollected", function(weapon, ply)
-- print("WeaponEquip " .. weapon:GetClass())
local entityClass = weapon:GetClass()
local entityPos = weapon:GetNWVector("respawnPos")
local entityAngles = weapon:GetNWAngle("respawnAngle")
local entityModel = weapon:GetModel()
local entityCollisionGroup = weapon:GetCollisionGroup()
local entityCreatedByMap = weapon:CreatedByMap()
timer.Simple(1, function()
if (entityCreatedByMap) then
entityPos = entityPos + Vector(0, 0, 3)
end
local ent = ents.Create(entityClass)
ent:SetPos(entityPos)
ent:SetAngles(entityAngles)
ent:SetModel(entityModel)
ent:SetCollisionGroup(entityCollisionGroup)
ent:Spawn()
ent:EmitSound("items/suitchargeok1.wav")
-- print("item " .. entityClass .. " recreated!")
end)
--if (weapon:GetClass() == "weapon_crowbar") then
-- if (ply:HasWeapon("weapon_fists")) then
-- ply:StripWeapon("weapon_fists")
-- end
--end
end)
hook.Add("AllowPlayerPickup", "some_unique_name", function (ply, ent)
return not ent:GetNWBool("blockPickup")
end)