AddCSLuaFile()

ENT.Base = "base_gmodentity"
ENT.Type = "anim"
ENT.PrintName = "Loot"

if SERVER then
    function ENT:Initialize()
        self:SetModel("models/props_junk/wood_crate002a.mdl")
        self:PhysicsInit(SOLID_VPHYSICS)
        self:SetMoveType(MOVETYPE_VPHYSICS)
        self:SetUseType(SIMPLE_USE)
        self:SetCollisionGroup(COLLISION_GROUP_DEBRIS_TRIGGER)
        local phys = self:GetPhysicsObject()
        if IsValid(phys) then
            phys:EnableMotion(true)
            phys:Wake()
        end
    end

    function ENT:Use(ply)
        self:EmitSound("items/ammocrate_open.wav", 70)
        ply:PlayVoiceLine("pickup")
        if SuR.ActiveMutator != 7 then
            for i=1, math.random(4,8) do
                local class = SuR:GetRandomClass("loot")
                if class != 6 then
                    local ent = ents.Create("sur_loot")
                    ent.LootType = class
                    ent:SetPos(self:WorldSpaceCenter())
                    ent:SetAngles(AngleRand())
                    ent:Spawn()
                    if IsValid(ent:GetPhysicsObject()) then
                        ent:GetPhysicsObject():SetVelocity(VectorRand(-250,250))
                    end
                end
            end
            for i=1, math.random(1,2) do
                local class = SuR:GetRandomClass("weapon")
                local weapon = nil

                local tab = weapons.Get(class)
                if tab then
                    weapon = {
                        name = tab.PrintName,
                        model = tab.WorldModel,
                        usefunc = function(self, ply)
                            ply:Give(class)
                        end
                    }
                end

                local ent = ents.Create("sur_loot")
                ent.Weapon = weapon
                ent:SetPos(self:WorldSpaceCenter())
                ent:SetAngles(AngleRand())
                ent:Spawn()
                if IsValid(ent:GetPhysicsObject()) then
                    ent:GetPhysicsObject():SetVelocity(VectorRand(-250,250))
                end
            end
        end
        SuR:MessageOnClient(18, ply:Nick())
        self:Remove()
    end
end
