Sign in to follow this  
_FrostyFoxy

Sanity/Insanity Aura

Recommended Posts

_FrostyFoxy    4

Hey people I am a first timer at this so sry if this looks like complet garbage to you ^^'
I need some advice on how to get the Sanity Aura/Insanity Aura work.

My Idea: While Narisa (The Character) is above 50% sanity she regenerates sanity to people around her but while she is below 50% she lowers their sanity.

My Problem: Requesting the sanity of my character for the variable.

Maybe anyone has an idea :D

Here's my Code so far (I Copied everything in case u spot diffrent issues ^^' I put the Aura thing in a 2nd Spoiler):

 

Spoiler

local MakePlayerCharacter = require "prefabs/player_common"


local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local prefabs = {}

-- Custom starting items
local start_inv = {
}

-- When the character is revived from human
local function onbecamehuman(inst)
    -- Set speed when reviving from ghost (optional)
    inst.components.locomotor:SetExternalSpeedMultiplier(inst, "narisa_speed_mod", 1)
end

local function onbecameghost(inst)
    -- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "narisa_speed_mod")
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end

local common_postinit = function(inst)
    -- Minimap icon
    inst.MiniMapEntity:SetIcon( "narisa.tex" )
    
    -- Night Vision
    local NIGHTVISION_COLOURCUBES =
{
    day = "images/colour_cubes/ruins_light_cc.tex",
    dusk = "images/colour_cubes/ruins_dim_cc.tex",
    night = "images/colour_cubes/purple_moon_cc.tex",
    full_moon = "images/colour_cubes/purple_moon_cc.tex",
}
local function SetNightVision(inst, enable) --This should be obvious
    if TheWorld.state.isnight or TheWorld:HasTag("cave") then
        inst.components.playervision:ForceNightVision(true)
        inst.components.playervision:SetCustomCCTable(NIGHTVISION_COLOURCUBES)
    else
        inst.components.playervision:ForceNightVision(false)
        inst.components.playervision:SetCustomCCTable(nil)
    end
end
    inst:WatchWorldState( "isday", function() SetNightVision(inst) end)
      inst:WatchWorldState( "isdusk", function() SetNightVision(inst) end)
      inst:WatchWorldState( "isnight", function() SetNightVision(inst)  end)
    inst:WatchWorldState( "iscaveday", function() SetNightVision(inst) end)
      inst:WatchWorldState( "iscavedusk", function() SetNightVision(inst) end)
      inst:WatchWorldState( "iscavenight", function() SetNightVision(inst)  end)
    
    SetNightVision(inst)

end
 

Spoiler

-- Insanity/sanity Aura
inst.components.sanity:GetCurrent () = NarisaSanity
    if NarisaSanity > (75)
        then inst:AddComponent("sanityaura")
inst.components.sanityaura.aura = TUNING.SANITYAURA_LARGE
    else  inst:AddComponent ("insanityaura")
inst.components.insanityaura.neg_aura = TUNING.SANITYAURA_LARGE
end

-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)
    -- choose which sounds this character will play
    inst.soundsname = "willow"
    
    -- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
    
    -- Stats    
    inst.components.health:SetMaxHealth(100)
    inst.components.hunger:SetMax(150)
    inst.components.sanity:SetMax(150)
    inst.components.temperature.mintemp = 10.
    
    
    -- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 0.7
    
    -- Hunger rate (optional)
    inst.components.hunger.hungerrate = 0.8 * TUNING.WILSON_HUNGER_RATE
    
    inst.OnLoad = onload
    inst.OnNewSpawn = onload
    
end

return MakePlayerCharacter("narisa", prefabs, assets, common_postinit, master_postinit, start_inv)

 

 

Also if there's a possiblity to increese the hunger drain while insane that would be helpfull too :3

Share this post


Link to post
Share on other sites
K1NGT1GER609    92
Posted (edited)

Your in the wrong forums sections btw, but no less I'll just give it a shot:

local function CalcSanityAura(inst, observer)
    

if (inst.components.sanity:GetPercent() > .5) and not  observer:HasTag("mycharactertag") then

 return TUNING.SANITYAURA_SMALL

elseif (inst.components.sanity:GetPercent() < .5) and not  observer:HasTag("mycharactertag") then

 return -TUNING.SANITYAURA_SMALL --small,med,large
 
end
end

local common_postinit = function(inst) --its the function that has the minimapentity:seticon
    inst:AddTag("mycharactertag")

local master_postinit = function(inst) --your main for you character, this is just reference

inst:AddComponent("sanityaura")
    inst.components.sanityaura.aurafn = CalcSanityAura

Try to read the notes I put them there for a reason and people just don't read.

last note im not sure if you want to drain your character's hunger while insane or the other players hunger.

Edited by K1NGT1GER609
  • Thanks 1

Share this post


Link to post
Share on other sites
_FrostyFoxy    4
Posted (edited)
14 hours ago, K1NGT1GER609 said:

Your in the wrong forums sections btw

Oh thanks for the info ^^' may u tell me which is the correct forum? So i know for further posts :)
 

14 hours ago, K1NGT1GER609 said:

local function CalcSanityAura(inst, observer)
    

if (inst.components.sanity:GetPercent() > .5) and not  observer:HasTag("mycharactertag") then

 return TUNING.SANITYAURA_SMALL

elseif (inst.components.sanity:GetPercent() < .5) and not  observer:HasTag("mycharactertag") then

 return -TUNING.SANITYAURA_SMALL --small,med,large
 
end
end

local common_postinit = function(inst) --its the function that has the minimapentity:seticon
    inst:AddTag("mycharactertag")

local master_postinit = function(inst) --your main for you character, this is just reference

inst:AddComponent("sanityaura")
    inst.components.sanityaura.aurafn = CalcSanityAura

May you be so kind and try explain it so I can try understand how it works? :3
Also how do i config how much sanity the aura gives/drains?

 

14 hours ago, K1NGT1GER609 said:

last note im not sure if you want to drain your character's hunger while insane or the other players hunger.

I want that she looses more hunger the lower her sanity is.


Just cause im unsure if i did it correctly I post it again so you can correct it if possible :)
I marked them again so u can find them easily ^^
 

Spoiler


local MakePlayerCharacter = require "prefabs/player_common"


local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local prefabs = {}

-- Custom starting items
local start_inv = {
}

-- When the character is revived from human
local function onbecamehuman(inst)
    -- Set speed when reviving from ghost (optional)
    inst.components.locomotor:SetExternalSpeedMultiplier(inst, "narisa_speed_mod", 1)
end

local function onbecameghost(inst)
    -- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "narisa_speed_mod")
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end

local common_postinit = function(inst)

Spoiler

inst:AddTag("mycharactertag")

    -- Minimap icon
    inst.MiniMapEntity:SetIcon( "narisa.tex" )
    
    -- Night Vision
    local NIGHTVISION_COLOURCUBES =
{
    day = "images/colour_cubes/ruins_light_cc.tex",
    dusk = "images/colour_cubes/ruins_dim_cc.tex",
    night = "images/colour_cubes/purple_moon_cc.tex",
    full_moon = "images/colour_cubes/purple_moon_cc.tex",
}
local function SetNightVision(inst, enable) --This should be obvious
    if TheWorld.state.isnight or TheWorld:HasTag("cave") then
        inst.components.playervision:ForceNightVision(true)
        inst.components.playervision:SetCustomCCTable(NIGHTVISION_COLOURCUBES)
    else
        inst.components.playervision:ForceNightVision(false)
        inst.components.playervision:SetCustomCCTable(nil)
    end
end
    inst:WatchWorldState( "isday", function() SetNightVision(inst) end)
      inst:WatchWorldState( "isdusk", function() SetNightVision(inst) end)
      inst:WatchWorldState( "isnight", function() SetNightVision(inst)  end)
    inst:WatchWorldState( "iscaveday", function() SetNightVision(inst) end)
      inst:WatchWorldState( "iscavedusk", function() SetNightVision(inst) end)
      inst:WatchWorldState( "iscavenight", function() SetNightVision(inst)  end)
    
    SetNightVision(inst)end

Spoiler

-- Insanity/sanity Aura

local function CalcSanityAura(inst, observer)
    

if (inst.components.sanity:GetPercent() > .5) and not  observer:HasTag("mycharactertag") then

 return TUNING.SANITYAURA_SMALL

elseif (inst.components.sanity:GetPercent() < .5) and not  observer:HasTag("mycharactertag") then

 return -TUNING.SANITYAURA_SMALL --small,med,large
 
end
end


-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)

 

Spoiler

inst:AddComponent("sanityaura")

inst.components.sanityaura.aurafn = CalcSanityAura


    
    -- choose which sounds this character will play
    inst.soundsname = "willow"
    
    -- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
    
    -- Stats    
    inst.components.health:SetMaxHealth(100)
    inst.components.hunger:SetMax(150)
    inst.components.sanity:SetMax(150)
    inst.components.temperature.mintemp = 20.
    
    
    -- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 0.7
    
    -- Hunger rate (optional)
    inst.components.hunger.hungerrate = 0.8 * TUNING.WILSON_HUNGER_RATE
    
    inst.OnLoad = onload
    inst.OnNewSpawn = onload
    
end

return MakePlayerCharacter("narisa", prefabs, assets, common_postinit, master_postinit, start_inv)

 

 

Edited by _FrostyFoxy

Share this post


Link to post
Share on other sites
K1NGT1GER609    92
Posted (edited)

The correct forum for don't starve together is here:

https://forums.kleientertainment.com/forum/79-dont-starve-together-mods-and-tools/

Now to explain the code inside the master postinit I added the component sanityaura thats usually used by mobs like the spider, deerclops and friendly pigmen. And those mobs give out a radius(about 3-4 berrybushes radius) of sanity draining or raising "aura" whenever the player gets close to them. So the aurafn tells what the aura does and goes to calcsanityaura; the if else if function in there checks the players current sanity level is above or below 50% respectively. I also tried to exclude the player from benefiting from the aura by adding a tag into the common postinit, and if you see inside the calcsanityaura it excludes those with that tag (or at least it should) from benefiting the sanity aura. To calibrate the sanity aura you can change it to (it has to be in all caps) sanityaura_small, sanityaura_med, and sanityaura_large. Unless your talking about making configurations via mod configurations I say ask someone else for that, I get headaches from just thinking about it.

Loosing hunger when sanity is low well heres more code to add:

local function sanityhunger(inst) --outside of the master postinit
    if (inst.components.sanity:GetPercent() < .5)then

inst.components.hunger.burnrate = 1.2--20% faster hunger

else

inst.components.hunger.burnrate = 1--normal hunger

end
end

 

inst:DoPeriodicTask(4, sanityhunger, nil, inst)--goes into the master postinit and check every 4 seconds on our sanity

last from your placement looks like it should be good.

Edited by K1NGT1GER609
  • Thanks 1

Share this post


Link to post
Share on other sites
_FrostyFoxy    4
Posted (edited)
4 hours ago, K1NGT1GER609 said:

To calibrate the sanity aura you can change it to (it has to be in all caps) sanityaura_small, sanityaura_med, and sanityaura_large. Unless your talking about making configurations via mod configurations I say ask someone else for that, I get headaches from just thinking about it.

I was talking about config the amout of sanity it gives or drains ^^' if that's too complicated I'm fine with what i have. I was just courius.

Also while i have someone who knows how it kinda works:
- I wanna add something similar like the "low sanity/hunger drain" thing but "Low Health/sanity drain" can i use a similar code like this?

Spoiler

-- Sanity drain on low Health
local function healthsanity (inst)
    if (inst.components.health:GetPercent() < .5)then
    
inst.components.sanity.night_drain_mult= 2

else

inst.components.sanity.night_drain_mult= 0.8

end
    end

- I also wanna add that if she gets too hot her dmg goes down until she cools down. How does that work?

These would be the last two things ^^ I really am thankfull for your help so far! :3

EDIT:
I tried to start the character and it crashed. The game told me the variable "sanityhunger" is not declared
5b0f1d3763fe5_Screenshot(166).thumb.png.fbc4c25abd688c522e854169fc727996.png

Did i miss anything?

Spoiler

-- Hunger drain on low sanity
 local function sanityhunger(inst)
    if (inst.components.sanity:GetPercent() < .5)then

inst.components.hunger.burnrate = 1.2--20% faster hunger

else

inst.components.hunger.burnrate = 0.6

end
    end
-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)

inst:DoPeriodicTask(4, sanityhunger, nil, inst)--checks every 4 seconds on your sanity

inst:AddComponent("sanityaura")
    inst.components.sanityaura.aurafn = CalcSanityAura
    
    -- choose which sounds this character will play
    inst.soundsname = "willow"
    
    -- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
    
    -- Stats    
    inst.components.health:SetMaxHealth(175)
    inst.components.hunger:SetMax(75)
    inst.components.sanity:SetMax(150)
    inst.components.temperature.mintemp = 15.
    inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.3)
    inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.3)
    
    
    -- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1.2
    
    inst.OnLoad = onload
    inst.OnNewSpawn = onload
end

return MakePlayerCharacter("narisa", prefabs, assets, common_postinit, master_postinit, start_inv)

 


If you dont know any further I'd ask in the DST forum maybe there is a slight diffrence there ^^'
 

Edited by _FrostyFoxy

Share this post


Link to post
Share on other sites
K1NGT1GER609    92
Posted (edited)

Hmm try moving:

inst:DoPeriodicTask(4, sanityhunger, nil, inst)

under the damage multiplier (inst.components.combat.damagemultiplier = 1.2). The healthsanity would work but you'd need another inst:DoPeriodicTask(4, healthsanity, nil, inst), or you can always combine the two functions as an alternative like so:

local function sanityhunger(inst)
    if (inst.components.sanity:GetPercent() < .5)then

inst.components.hunger.burnrate = 1.2--20% faster hunger

else

inst.components.hunger.burnrate = 0.6

end

if (inst.components.health:GetPercent() < .5)then
    
inst.components.sanity.night_drain_mult= 2

else

inst.components.sanity.night_drain_mult= 0.8

end
  end

well that only saved three lines of code, thought it'd be more space saving. As for the overheating ill give it a guess

if (inst.components.temperature:GetCurrent() > 70)then

inst.components.combat.damagemultiplier = 0.5

else

inst.components.combat.damagemultiplier = 1.2

end

it simple activates when the player starts to overheat and takes damage, not sure if you want it modified a bit more and you can also combine it with sanityhunger. Heh "someone who knows how it kinda works" I just made my explanation simple, didn't give the full detail since I don't want people to regurgitate coding, they should learn the material as all my professors say.

Edited by K1NGT1GER609
  • Thanks 1

Share this post


Link to post
Share on other sites
_FrostyFoxy    4
Posted (edited)
9 hours ago, K1NGT1GER609 said:

Hmm try moving:

inst:DoPeriodicTask(4, sanityhunger, nil, inst)

under the damage multiplier (inst.components.combat.damagemultiplier = 1.2).

It worked thanks ^-^.
 

9 hours ago, K1NGT1GER609 said:

I just made my explanation simple, didn't give the full detail since I don't want people to regurgitate coding, they should learn the material as all my professors say.

Sure i wanna understand it but if i have an explanation on how it works its easier for me to understand i guess. If i have an explanation i see how it works and when i know how it works i can start learning it ^^.

Anyway as already said I'm really tankfull for your help ^-^ everything seems to work perfectly.

One last thing i just came up with:
What tag do i have to add so she gets attack by anything (monsters and pigs/catcoons For example)?

Edit:
I somehow can't figure out how to set that i don't suffer neg effects from monstermeat. Do I have to have the monster tag so i dont suffer from it? If yes would it still be possible that monsters do attack me?

Why doesnt it gives me a backpack?

-- Custom starting items
local start_inv = {

        "backpack",
        "trap",
        "birdtrap",
        "fish",
        "monstermeat",
        
}

Edited by _FrostyFoxy

Share this post


Link to post
Share on other sites
K1NGT1GER609    92

Attacked by anything, so you want volt goats, monkeys, beefalo, bees, penguins, kolaphants, and everything else that can attack, attack you? If so then I'll need a little time to write the code. For eating monster meat you need this code in your master_postinit:

inst.components.eater.strongstomach = true

Last you don't get a backpack because its code is set to do not store in inventory/slot, best thing I can suggest is giving a blueprint like so:

"backpack_blueprint",

  • Thanks 1

Share this post


Link to post
Share on other sites
_FrostyFoxy    4
Posted (edited)
2 hours ago, K1NGT1GER609 said:

Attacked by anything, so you want volt goats, monkeys, beefalo, bees, penguins, kolaphants, and everything else that can attack, attack you? If so then I'll need a little time to write the code

Yea I'd be thankfull ^^
 

2 hours ago, K1NGT1GER609 said:

For eating monster meat you need this code in your master_postinit:

inst.components.eater.strongstomach = true

Oh well i put it in the wrong postnit...
 

 

2 hours ago, K1NGT1GER609 said:

Last you don't get a backpack because its code is set to do not store in inventory/slot, best thing I can suggest is giving a blueprint like so:

"backpack_blueprint",

Oki thanks ^-^ is it possible to dircetly spawn it in my equipment slot or would that be to much coding?

Edited by _FrostyFoxy

Share this post


Link to post
Share on other sites
K1NGT1GER609    92

Alright this goes at the very top of the master_postinit:

local function attackfn(inst)
local x,y,z = inst.Transform:GetWorldPosition()
local npcs = TheSim:FindEntities(x,y,z, 10, nil, nil, {"_combat"}) --10 is the radius that finds creatures to provoke, change it to your liking, anything too big will cause a crash
    for k,v in pairs(npcs) do
if (v.components.combat) then
    v.components.combat:SuggestTarget(inst)
    end
    end
end

And the periodictask next to the other periodictask:

inst:DoPeriodicTask(1, attackfn, nil, inst)

For directly spawning a backpack I found some code from @IronHunter (another modder) some time ago (the klei forums are a mess when it comes to searching for what you need, almost makes those 150+ post useless and previous code posted lost) and it also goes into the master_postinit:

inst.OnNewSpawn = function()

local backpack = SpawnPrefab("backpack")

inst.components.inventory:Equip(backpack)

end

 

  • Thanks 1

Share this post


Link to post
Share on other sites
_FrostyFoxy    4
14 hours ago, K1NGT1GER609 said:

For directly spawning a backpack I found some code from @IronHunter (another modder) some time ago (the klei forums are a mess when it comes to searching for what you need, almost makes those 150+ post useless and previous code posted lost) and it also goes into the master_postinit:

inst.OnNewSpawn = function()

local backpack = SpawnPrefab("backpack")

inst.components.inventory:Equip(backpack)

end

Doesn't seem to work ^^' i tried around a bit but i can't get it working so i guess it's outdated?

Anyway the provokeaura, I'd say, is working perfectly ^-^

Also i think i have an understanding on how this works :3

Again I am really thankfull for your help I would have need much long with coding my char without your help ^-^

Share this post


Link to post
Share on other sites
K1NGT1GER609    92
Posted (edited)

Heh ah well can't really help with the backpack maybe you can ask around in the dst forums, either way see you around.

Edited by K1NGT1GER609

Share this post


Link to post
Share on other sites
IronHunter    187
Posted (edited)
48 minutes ago, _FrostyFoxy said:

Doesn't seem to work ^^' i tried around a bit but i can't get it working so i guess it's outdated?

Anyway the provokeaura, I'd say, is working perfectly ^-^

Also i think i have an understanding on how this works :3

Again I am really thankfull for your help I would have need much long with coding my char without your help ^-^

This code is meant to be placed in the same area where you would have onsave, onload etc which is usually near the bottom of the masterpostinit.

I noticed in your original code you also have a inst.OnNewSpawn = onload which is likely overwriting the one you are trying to add.

inst.OnNewSpawn = function()
  	onload(inst)
	local backpack = SpawnPrefab("backpack")
	inst.components.inventory:Equip(backpack)
end

Try replacing your original one with this, adding new instances of the same function will just cause things to overwrite each other and is why copypasting code without knowing how to use it, isn't always a good idea.

Welcome to the forums.

Edited by IronHunter
  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites
_FrostyFoxy    4
Posted (edited)

@IronHunter

Thanks for the help ^^'
 

5 hours ago, IronHunter said:

is why copypasting code without knowing how to use it, isn't always a good idea.

Im getting there someday xD I'm learning with each step but until them i gotta do it like that ^^

Edited by _FrostyFoxy
  • Haha 1

Share this post


Link to post
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

Sign in to follow this