AddCSLuaFile() local SligWolf_Addons = SligWolf_Addons local addonName = "garbagetruck" DEFINE_BASECLASS("sligwolf_weapon_base") -- Tell the user something is wrong ("Broken") with the addons in case they see the usually hidden placeholder node. -- This item is moved to a different custom build category if everything is fine and the "Broken" one is hidden. SWEP.Category = "SligWolf's Addons (Broken)" SWEP.Spawnable = false SWEP.AdminOnly = false SWEP.PrintName = "trashbag" SWEP.Base = "sligwolf_weapon_base" SWEP.m_WeaponDeploySpeed = 5 SWEP.ViewModel = "models/sligwolf/garbagetruck/c_trashbagswep.mdl" SWEP.ViewModelFlip = false SWEP.ViewModelFlip1 = false SWEP.ViewModelFlip2 = false SWEP.ViewModelFOV = 62 SWEP.WorldModel = "models/sligwolf/garbagetruck/w_trashbagswep.mdl" SWEP.AutoSwitchFrom = false SWEP.AutoSwitchTo = false SWEP.Weight = 5 SWEP.UseHands = true SWEP.Primary.Ammo = "none" SWEP.Primary.ClipSize = 1 SWEP.Primary.DefaultClip = 1 SWEP.Primary.Automatic = false SWEP.Secondary.Ammo = "none" SWEP.Secondary.ClipSize = 0 SWEP.Secondary.DefaultClip = 0 SWEP.Secondary.Automatic = false if CLIENT then SWEP.Author = "SligWolf" SWEP.Contact = "http://steamcommunity.com/id/SligWolf" SWEP.Purpose = "Wrap up stuff.." SWEP.Instructions = "LeftMouse = Wrap up stuff / Unwrap stuff" SWEP.BobScale = 1 SWEP.SwayScale = 1 SWEP.BounceWeaponIcon = false SWEP.DrawWeaponInfoBox = false SWEP.DrawAmmo = false SWEP.DrawCrosshair = true SWEP.RenderGroup = RENDERGROUP_OPAQUE SWEP.Slot = 5 SWEP.SlotPos = 10 SWEP.SpeechBubbleLid = surface.GetTextureID("gui/speech_lid") SWEP.WepSelectIcon = surface.GetTextureID("hud/sligwolf/garbagetruck/weaponicons/sligwolf_weapon_trashbag_icon") SWEP.CSMuzzleFlashes = false SWEP.CSMuzzleX = false SWEP.AccurateCrosshair = false end if not SligWolf_Addons then return end if not SligWolf_Addons.HasLoadedAddon then return end if not SligWolf_Addons.HasLoadedAddon(addonName) then return end SWEP.Spawnable = true local CONSTANTS = SligWolf_Addons.Constants local LIBConstraints = SligWolf_Addons.Constraints local LIBEntities = SligWolf_Addons.Entities local LIBModel = SligWolf_Addons.Model local trashbag = "models/sligwolf/garbagetruck/trashbag_2.mdl" local dividerVal = 10000 local function normalizeConVar() local min = 3 local max = 99999 local val = GetConVar("sv_sligwolf_garbagetruck_trashbag_capacity"):GetFloat() if val < min then val = min end if val > max then val = max end val = math.Round(val, 2) return val end function SWEP:Initialize() self:SetAddonID(addonName) self:SetHoldType("slam") end local function isTrashBag(target) if not IsValid(target) then return false end if target:GetClass() ~= "prop_physics" then return false end for i = 1, 3 do local trashBagMdl = "models/sligwolf/garbagetruck/trashbag_" .. i .. ".mdl" if target:GetModel() == trashBagMdl then return true end end return false end local whitelist = { prop_physics = true, prop_ragdoll = true, } local function allowTrashing(ent, ply) if CLIENT then return end if ent.sligwolf_is_garbagetruck_trashbag then return true end if not whitelist[ent:GetClass()] then return false end if not IsValid(ent) then return false end if LIBConstraints.HasConstraints(ent) then return false end if IsValid(ent:GetParent()) then return false end if not table.IsEmpty(ent:GetChildren()) then return false end local volume = 0 local physObjCount = ent:GetPhysicsObjectCount() for i = 0, physObjCount do local phys = ent:GetPhysicsObjectNum(i) if IsValid(phys) then volume = volume + phys:GetVolume() end end local finalVol = volume / dividerVal local maxVolume = normalizeConVar() -- print("GarbageTruck. Volume:", finalVol * dividerVal, "ConVar:", maxVolume * dividerVal) if finalVol < maxVolume then return true, volume end return false end local function setTrashBagMdl(ent, ply) if not IsValid(ent) then return "" end if not IsValid(ply) then return "" end local allowed, volume = allowTrashing(ent, ply) if not allowed then return "" end local finalVol = volume / dividerVal if finalVol < 1 then return "models/sligwolf/garbagetruck/trashbag_1.mdl" end if finalVol < 2 then return "models/sligwolf/garbagetruck/trashbag_2.mdl" end if finalVol < 3 then return "models/sligwolf/garbagetruck/trashbag_3.mdl" end return trashbag end function SWEP:TrashEntity(target) if CLIENT then return end if not IsValid(target) then return end local owner = self:GetOwner() local trashBagMdl = setTrashBagMdl(target, owner) if trashBagMdl == "" then return end local pos = target:GetPos() local ang = target:GetAngles() local ent = self:MakeEnt("prop_physics", "Trash") if not IsValid(ent) then return end LIBModel.SetModel(ent, trashBagMdl) ent:SetPos(pos) ent:SetAngles(ang) ent:Spawn() ent.sligwolf_is_garbagetruck_trashbag = true ent.content = target ent.emptied = false ent.childsOldCollision = target:GetCollisionGroup() LIBEntities.CallOnRemove(ent, "RemoveContentToo", function(thisEnt) if thisEnt.emptied then return end local trash = thisEnt.content if IsValid(trash) then trash:Remove() end end) target:SetNoDraw(true) target:SetCollisionGroup(COLLISION_GROUP_IN_VEHICLE) LIBEntities.EnableMotion(target, false) undo.Create("trashbag") undo.AddEntity(ent) undo.SetPlayer(owner) undo.Finish() end function SWEP:RestoreEntity(trashbag) if CLIENT then return end if not IsValid(trashbag) then return end local target = trashbag.content if not IsValid(target) then local owner = self:GetOwner() owner:ChatPrint("This trashbag is empty. There is no content.") return end local pos = trashbag:GetPos() target:SetPos(pos) target:Spawn() target:SetNoDraw(false) target:SetCollisionGroup(trashbag.childsOldCollision) LIBEntities.EnableMotion(target, true) trashbag.emptied = true trashbag:Remove() end local maxDist = 100 * 100 function SWEP:PrimaryAttack() local owner = self:GetOwner() local tr = util.TraceLine(util.GetPlayerTrace(owner)) local eyePos = owner:EyePos() local hitPos = tr.HitPos if eyePos:DistToSqr(hitPos) > maxDist then return false end local target = tr.Entity if not IsValid(target) then return false end if not allowTrashing(target, owner) then if SERVER then owner:ChatPrint("You can't wrap up this object.") end return false end if not self:CanPrimaryAttack() then return false end if target.CPPICanTool then local allowRemove = target:CPPICanTool(owner, "remover") local allowWeld = target:CPPICanTool(owner, "weld") if not allowRemove or not allowWeld then owner:ChatPrint("You are not allowed to do that!") return false end end owner:EmitSound(CONSTANTS.sndBreakItem) self:SetNextPrimaryFire(CurTime() + 0.5) self:ShootEffects(self) if isTrashBag(target) then self:RestoreEntity(target) else self:TrashEntity(target) end return true end function SWEP:SecondaryAttack() return false end function SWEP:Reload() return false end