Sanity Script


_Q_
  • Closed

So this is the Recalc function form sanity component.

function Sanity:Recalc(dt)
    local total_dapperness = self.dapperness or 0
    for k, v in pairs(self.inst.components.inventory.equipslots) do
        if v.components.equippable ~= nil then
            total_dapperness = total_dapperness + v.components.equippable:GetDapperness(self.inst)
        end
    end

    total_dapperness = total_dapperness * self.dapperness_mult

    local dapper_delta = total_dapperness * TUNING.SANITY_DAPPERNESS

    local moisture_delta = easing.inSine(self.inst.components.moisture:GetMoisture(), 0, TUNING.MOISTURE_SANITY_PENALTY_MAX, self.inst.components.moisture:GetMaxMoisture())

    local light_delta 
    if TheWorld.state.isday and not TheWorld:HasTag("cave") then
        light_delta = TUNING.SANITY_DAY_GAIN
    else
        local lightval = CanEntitySeeInDark(self.inst) and .9 or self.inst.LightWatcher:GetLightValue()
        light_delta =
            (   (lightval > TUNING.SANITY_HIGH_LIGHT and TUNING.SANITY_NIGHT_LIGHT) or
                (lightval < TUNING.SANITY_LOW_LIGHT and TUNING.SANITY_NIGHT_DARK) or
                TUNING.SANITY_NIGHT_MID
            ) * self.night_drain_mult
    end

    local aura_delta = 0
    local x, y, z = self.inst.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x, y, z, TUNING.SANITY_EFFECT_RANGE, nil, { "FX", "NOCLICK", "DECOR","INLIMBO" })
    for i, v in ipairs(ents) do 
        if v.components.sanityaura ~= nil and v ~= self.inst then
            local aura_val = v.components.sanityaura:GetAura(self.inst) / math.max(1, self.inst:GetDistanceSqToInst(v))
            aura_delta = aura_delta + (aura_val < 0 and aura_val * self.neg_aura_mult or aura_val)
        end
    end

    local mount = self.inst.components.rider:IsRiding() and self.inst.components.rider:GetMount() or nil
    if mount ~= nil and mount.components.sanityaura ~= nil then
        local aura_val = mount.components.sanityaura:GetAura(self.inst)
        aura_delta = aura_delta + (aura_val < 0 and aura_val * self.neg_aura_mult or aura_val)
    end

    self:RecalcGhostDrain()
    local ghost_delta = TUNING.SANITY_GHOST_PLAYER_DRAIN * self.ghost_drain_mult

    self.rate = dapper_delta + moisture_delta + light_delta + aura_delta + ghost_delta

    if self.custom_rate_fn ~= nil then
        self.rate = self.rate + self.custom_rate_fn(self.inst)
    end

    self.rate = self.rate * self.rate_modifier
    self.ratescale =
        (self.rate > .2 and RATE_SCALE.INCREASE_HIGH) or
        (self.rate > .1 and RATE_SCALE.INCREASE_MED) or
        (self.rate > .01 and RATE_SCALE.INCREASE_LOW) or
        (self.rate < -.3 and RATE_SCALE.DECREASE_HIGH) or
        (self.rate < -.1 and RATE_SCALE.DECREASE_MED) or
        (self.rate < -.02 and RATE_SCALE.DECREASE_LOW) or
        RATE_SCALE.NEUTRAL

    --print (string.format("dapper: %2.2f light: %2.2f TOTAL: %2.2f", dapper_delta, light_delta, self.rate*dt))
    self:DoDelta(self.rate * dt, true)
end

The code was carried over from normal DS versions, the problem is the check for the day is not needed, in DST and in other game versions as well.

So the code without unneded checks and calls to clock component works just fine as you compare light values from lightwatcher anyway.

So the whole function could look something like this:

function Sanity:Recalc(dt)
    local total_dapperness = self.dapperness or 0
    for k, v in pairs(self.inst.components.inventory.equipslots) do
        if v.components.equippable ~= nil then
            total_dapperness = total_dapperness + v.components.equippable:GetDapperness(self.inst)
        end
    end

    total_dapperness = total_dapperness * self.dapperness_mult

    local dapper_delta = total_dapperness * TUNING.SANITY_DAPPERNESS

    local moisture_delta = easing.inSine(self.inst.components.moisture:GetMoisture(), 0, TUNING.MOISTURE_SANITY_PENALTY_MAX, self.inst.components.moisture:GetMaxMoisture())

    local light_delta = 0

        local lightval = CanEntitySeeInDark(self.inst) and .9 or self.inst.LightWatcher:GetLightValue()
        light_delta =
            (   (lightval > TUNING.SANITY_HIGH_LIGHT and TUNING.SANITY_NIGHT_LIGHT) or
                (lightval < TUNING.SANITY_LOW_LIGHT and TUNING.SANITY_NIGHT_DARK) or
                TUNING.SANITY_NIGHT_MID
            ) * self.night_drain_mult

    local aura_delta = 0
    local x, y, z = self.inst.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x, y, z, TUNING.SANITY_EFFECT_RANGE, nil, { "FX", "NOCLICK", "DECOR","INLIMBO" })
    for i, v in ipairs(ents) do 
        if v.components.sanityaura ~= nil and v ~= self.inst then
            local aura_val = v.components.sanityaura:GetAura(self.inst) / math.max(1, self.inst:GetDistanceSqToInst(v))
            aura_delta = aura_delta + (aura_val < 0 and aura_val * self.neg_aura_mult or aura_val)
        end
    end

    local mount = self.inst.components.rider:IsRiding() and self.inst.components.rider:GetMount() or nil
    if mount ~= nil and mount.components.sanityaura ~= nil then
        local aura_val = mount.components.sanityaura:GetAura(self.inst)
        aura_delta = aura_delta + (aura_val < 0 and aura_val * self.neg_aura_mult or aura_val)
    end

    self:RecalcGhostDrain()
    local ghost_delta = TUNING.SANITY_GHOST_PLAYER_DRAIN * self.ghost_drain_mult

    self.rate = dapper_delta + moisture_delta + light_delta + aura_delta + ghost_delta

    if self.custom_rate_fn ~= nil then
        self.rate = self.rate + self.custom_rate_fn(self.inst)
    end

    self.rate = self.rate * self.rate_modifier
    self.ratescale =
        (self.rate > .2 and RATE_SCALE.INCREASE_HIGH) or
        (self.rate > .1 and RATE_SCALE.INCREASE_MED) or
        (self.rate > .01 and RATE_SCALE.INCREASE_LOW) or
        (self.rate < -.3 and RATE_SCALE.DECREASE_HIGH) or
        (self.rate < -.1 and RATE_SCALE.DECREASE_MED) or
        (self.rate < -.02 and RATE_SCALE.DECREASE_LOW) or
        RATE_SCALE.NEUTRAL

    --print (string.format("dapper: %2.2f light: %2.2f TOTAL: %2.2f", dapper_delta, light_delta, self.rate*dt))
    self:DoDelta(self.rate * dt, true)
end

Eliminates the check for day and if its cave, also TUNING.SANITY_DAY_GAIN variable is no longer needed, so saves some meomry as well.

 


Steps to Reproduce
Just the general doubt about script writing, I may be wrong on this one.


User Feedback


It looks to me that its purpose is to allow mods to easily change how much sanity is gained during the day in the forest (zero in unmodded game).

Are you saying that this light check is duplicated and there is another place somewhere in the code that does the same?

Share this comment


Link to comment
Share on other sites
1 hour ago, Muche said:

It looks to me that its purpose is to allow mods to easily change how much sanity is gained during the day in the forest (zero in unmodded game).

Are you saying that this light check is duplicated and there is another place somewhere in the code that does the same?

Mods can easly change whole function or whole component via componentpostinit, the

TUNING.SANITY_DAY_GAIN

 

along with the day check is relic of the past, in the first update with sanity, player was ganing sanity during the day phase without any items, that was later removed, but the variabke and day check is still there.

Its not character specific as if you change the SANITY_DAY_GAIN all characters will get day sanity bonus.

And if I'm right here the day check is not needed at all, since you are getting right values from

LightWatcher:GetLightValue()

I did some quick testing today and sanity was calculated correctly, on the surface and in caves.

 

Share this comment


Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now