Jump to content

Gaining sanity when destorying structures\losing sanity when building structures as a specific character


Recommended Posts

Hello!
I need help with creating something similar to the title, if it's even possible
Maybe Wormwood's tree\planting\destroying code can come in handy... I just don't know what to change, I'm new to coding and was wondering if someone can help me here ><
Any help would be much appreciated!!

 

local function SanityRateFn(inst, dt)
    local amt = 0
    for bonus_index = #inst.plantbonuses, 1, -1 do
        local bonus_data = inst.plantbonuses[bonus_index]
        if bonus_data.t > dt then
            bonus_data.t = bonus_data.t - dt
        else
            table.remove(inst.plantbonuses, bonus_index)
        end
        amt = amt + bonus_data.amt
    end
    for bonus_index = #inst.plantpenalties, 1, -1 do
        local penalty_data = inst.plantpenalties[bonus_index]
        if penalty_data.t > dt then
            penalty_data.t = penalty_data.t - dt
        else
            table.remove(inst.plantpenalties, bonus_index)
        end
        amt = amt + penalty_data.amt
    end
    return amt
end

local function DoPlantBonus(inst, bonus, overtime)
    if overtime then
        table.insert(inst.plantbonuses, {
            amt = bonus / SANITY_DRAIN_TIME,
            t = SANITY_DRAIN_TIME
        })
    else
        while #inst.plantpenalties > 0 do
            table.remove(inst.plantpenalties)
        end
        inst.components.sanity:DoDelta(bonus)
        inst.components.talker:Say(GetString(inst, "ANNOUNCE_GROWPLANT"))
    end
end

local function DoKillPlantPenalty(inst, penalty, overtime)
    if overtime then
        table.insert(inst.plantpenalties, {
            amt = -penalty / SANITY_DRAIN_TIME,
            t = SANITY_DRAIN_TIME
        })
    else
        while #inst.plantbonuses > 0 do
            table.remove(inst.plantbonuses)
        end
        inst.components.sanity:DoDelta(-penalty)
        inst.components.talker:Say(GetString(inst, "ANNOUNCE_KILLEDPLANT"))
    end
end

local function CalcSanityMult(distsq)
    distsq = 1 - math.sqrt(distsq / WATCH_WORLD_PLANTS_DIST_SQ)
    return distsq * distsq
end

local function WatchWorldPlants(inst)
    if not inst._onitemplanted then
        inst._onitemplanted = function(src, data)
            if not data then
                --shouldn't happen
            elseif data.doer == inst then
                DoPlantBonus(inst, TUNING.SANITY_TINY * 2)
            elseif data.pos then
                local distsq = inst:GetDistanceSqToPoint(data.pos)
                if distsq < WATCH_WORLD_PLANTS_DIST_SQ then
                    DoPlantBonus(inst, CalcSanityMult(distsq) * TUNING.SANITY_SUPERTINY * 2, true)
                end
            end
        end
        inst:ListenForEvent("itemplanted", inst._onitemplanted, TheWorld)
    end

    if not inst._onplantkilled then
        inst._onplantkilled = function(src, data)
            if not data then
                --shouldn't happen
            elseif data.doer == inst then
                DoKillPlantPenalty(inst, data.workaction and data.workaction ~= ACTIONS.DIG and TUNING.SANITY_MED or TUNING.SANITY_TINY)
            elseif data.pos then
                local distsq = inst:GetDistanceSqToPoint(data.pos)
                if distsq < WATCH_WORLD_PLANTS_DIST_SQ then
                    DoKillPlantPenalty(inst, CalcSanityMult(distsq) * TUNING.SANITY_SUPERTINY * 2, true)
                end
            end
        end
        inst:ListenForEvent("plantkilled", inst._onplantkilled, TheWorld)
    end
end

local function StopWatchingWorldPlants(inst)
    if inst._onitemplanted then
        inst:RemoveEventCallback("itemplanted", inst._onitemplanted, TheWorld)
        inst._onitemplanted = nil
    end
    if inst._onplantkilled then
        inst:RemoveEventCallback("plantkilled", inst._onplantkilled, TheWorld)
        inst._onplantkilled = nil
    end
end

 

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
 Share

×
  • Create New...