--[[
作者：小开心
请不要擅自修改或删除
请尊重版权
@

说明：
想患 懒癌强迫症
]]

local function isdst( )
    return TheSim:GetGameID() == "DST"
end

function MakeImmunityDarkness( inst )
    if inst.components.grue then
        inst.components.grue:AddImmunity("darkness")
    end
end

function RemoveImmunityDarkness( inst )
    if inst.components.grue then
        inst.components.grue:RemoveImmunity("darkness")
    end
end

local xk = {}

xk.version = "1.3"

function xk:GetVersion( )
	return self.version
end

function xk.GetLevel( inst ) 
    return inst.components.level and inst.components.level.currentlevel or 0
end

function xk.GetHealth( inst )
    return inst.components.health.currenthealth
end

function xk.SetHealth( inst, maxhealth ) 
    local percent = inst.components.health:GetPercent()
    inst.components.health:SetMaxHealth(maxhealth)
    inst.components.health:SetPercent(percent)
end

function xk.Gethunger( inst )
    return inst.components.hunger.current
end

function xk.SetHunger( inst, maxhunger )
    local percent = inst.components.hunger:GetPercent()
    inst.components.hunger:SetMax(maxhunger)
    inst.components.hunger:SetPercent(percent)
end

function xk.GetSanity( inst )
    return inst.components.sanity.current
end

function xk.SetSanity( inst, maxsanity )
    local percent = inst.components.sanity:GetPercent()
    inst.components.sanity:SetMax(maxsanity)
    inst.components.sanity:SetPercent(percent)
end

function xk.SetAbsorb( inst, absorb )
    inst.components.health:SetAbsorptionAmount( absorb )
end

function xk.GetAbsorb( inst )
    return inst.components.health.absorb
end

function xk.SetArmor( inst, amount, absorb_percent )
    local percent = inst.components.armor:GetPercent()
    inst.components.armor:InitCondition(amount, absorb_percent)
    inst.components.armor:SetPercent(percent)
end

function xk.SetFiniteuses( inst, maxuse )
    if not inst.components.finiteuses then
        inst:AddComponent("finiteuses")
        inst.components.finiteuses:SetOnFinished(inst.Remove)
    end
    local percent = inst.components.finiteuses:GetPercent()
    inst.components.finiteuses:SetMaxUses( maxuse or 100 )
    inst.components.finiteuses:SetPercent(percent)
end

--local speeds = {}
local function decspeedfn( inst, key, mult, dec, time, decperiod, clamp )
    if time <= 0 or mult <= clamp  then
        return inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, key)        
    end 
    mult = mult - dec 
    time = time - decperiod
    inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, key)
    inst.components.locomotor:SetExternalSpeedMultiplier(inst, key, mult)
    inst:DoTaskInTime(decperiod, decspeedfn, key, mult, dec, time, decperiod)
end

function xk.MakeSpeedInTime( inst, key, mult, time, dec, decperiod, clamp, add )
    if inst.components.locomotor and mult then 
        mult = math.abs(mult)
    --[[    if speeds[inst] and speeds[inst][key] and time then
            if add then
                speeds[inst][key][time] = speeds[inst][key][time] + time
            else
                speeds[inst][key][time] = time
            end
            return
        end
        speeds[inst][key][time] = time
    ]]
        inst.components.locomotor:SetExternalSpeedMultiplier(inst, key, mult)
        if time then
            if dec and decperiod then
                clamp = clamp or .1
                inst:DoTaskInTime(decperiod, decspeedfn, key, mult, dec, time, decperiod, clamp)
            else
                inst:DoTaskInTime(time, function( inst ) inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, key) end)
            end
        end
    end
end

function xk.MakeSpeed( inst, key, mult )
    if inst.components.locomotor and mult and mult > .1 then 
        if isdst() then
            inst.components.locomotor:SetExternalSpeedMultiplier(inst, key, mult)
        else
            local self = inst.components.locomotor
            mult = mult - 1
            if self.bonussource == nil then
                self.bonussource = {}
            end
            if self.bonussource[key] == nil then
                self.bonussource[key] = mult
                self.bonusspeed = self.bonusspeed + self.walkspeed * mult 
            end
        end
    end
end

function xk.RemoveSpeed( inst, key )
    if inst.components.locomotor then
        if isdst() then
            inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, key)
        else
            local self = inst.components.locomotor            
            if self.bonussource == nil then
                self.bonussource = {}
            end
            local mult = self.bonussource[key] or 0
            self.bonussource[key] = nil
            self.bonusspeed = self.bonusspeed - self.walkspeed * mult
        end 
    end
end

function xk.SetDamage( inst, damage )
    inst.components.weapon:SetDamage(damage)
end

function xk.GetDamage( inst )
    local damage = 0
    if inst.components.weapon then
        damage = inst.components.weapon.damage
    elseif inst.components.combat then
        damage = inst.components.combat.defaultdamage
    end
    return damage
end

function xk.GetAttacked( inst, attacker, damage, weapon, stimuli )
    inst.components.combat:GetAttacked(attacker, damage, weapon, stimuli)
end

function xk.GetEquipSlot( inst )
    return inst.components.equippable.equipslot
end

function xk.Unequip( inst, equipslot, slip, say )
    if inst.components.inventory then
        inst.components.inventory:GiveItem(inst.components.inventory:Unequip(equipslot, slip))
    end
    if say and inst.components.talker then
        inst.components.talker:Say( say )
    end 
end

function xk.WorkedBy( inst, worker, numworks )
    if inst.components.workable then
        inst.components.workable:WorkedBy(worker, numworks)
    end
end

return xk
