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.
Just the general doubt about script writing, I may be wrong on this one.
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 accountSign in
Already have an account? Sign in here.
Sign In Now