Jump to content

Changing code? Stealth mode


Recommended Posts

Hey everyone,


First of all is it possible to create a custom key bind for something? If yes how? If not, how do i add a custom item?


I found some code similar to what i wanna implement to my character but i don't know which sections of the code would go where. Also i dunno what i have to change in the code so it works for me.

Idea: Adding an ability to enter some kind of stealth mode / invisibility. I want my character to loose hunger quicker and get thier movement speed slowed down while invisible.


Here's the code i found the invisiblity is granted by a potion (custom item):

Spoiler

local potiontime = 120
local AGGR_TIME =5

local assets=
{
    Asset("ANIM", "anim/invisibilitypotion.zip"),
    Asset("ATLAS", "images/inventoryimages/invisibilitypotion.xml"),
    Asset("IMAGE", "images/inventoryimages/invisibilitypotion.tex"),
}

 PrefabFiles = {
    "invisibilitypotion",
}

local function DropTargets(eater)
	local x,y,z = eater.Transform:GetWorldPosition()	
	local ents = TheSim:FindEntities(x, y, z, 50)

	for k,v in pairs(ents) do
		if v.components.combat and v.components.combat.target == eater then
			v.components.combat.target = nil
		end
		--if v:HasTag("hound") and v.components.follower and v.components.follower.leader == owner then
		--	v.components.follower:SetLeader(nil)
		--end
	end
end

local function GoInvisible(inst, eater)
	
	if inst.invisibilitypotion_active then 
		return
	end
	inst.invisibilitypotion_active = true
	--inst.m_compatibility.UpdateAnim(inst,true) --true means only once
	--inst.net_transparentpotion_active:set(true)
	inst:AddTag("notarget")
	inst:AddTag("invisibilitypotion_active")
	
	inst.AnimState:SetMultColour(0.2,0.2,0.2,.2)
	inst.AnimState:SetMultColour(0.03,0.03,0.03,0.03)
	if inst.DynamicShadow then
		inst.DynamicShadow:Enable(false)
	end
	if inst.MiniMapEntity then
		inst.MiniMapEntity:SetEnabled(false)
	end
	if not inst:HasTag("noplayerindicator") then
		inst:AddTag("noplayerindicator")
	end


	-- Stop anyone from actively attacking me (notarget tag will make it so you dont get retarggeted)
	local timer = 0
	inst.task_drop_targets = inst:DoPeriodicTask(0.2,function(inst)
		timer = timer + 0.2
		if timer > AGGR_TIME then
			DropTargets(inst)
		end
		if inst.task_drop_targets and (timer > AGGR_TIME or not inst.invisibilitypotion_active)  then
			inst.task_drop_targets:Cancel()
			inst.task_drop_targets = nil
		end
	end)
end

local function GoVisible(inst, eater)
	
	if not inst.invisibilitypotion_active then 
		return 
	end
	
	inst.invisibilitypotion_active = false
	--inst.m_compatibility.UpdateAnim(inst,true) --true means only once
	--inst.net_transparentpotion_active:set(false)
	inst:RemoveTag("notarget")
	inst:RemoveTag("invisibilitypotion_active")

	inst.AnimState:SetMultColour(1,1,1,1)
	if inst.DynamicShadow then
		inst.DynamicShadow:Enable(true)
	end
	if inst.MiniMapEntity then
		inst.MiniMapEntity:SetEnabled(true)
	end
	if inst:HasTag("noplayerindicator") then
		inst:RemoveTag("noplayerindicator")
	end

	inst.components.talker:Say("Everyone can see me now.", 4)
end

local function oneaten(inst, eater)
	inst.invisibilitypotion_active = true
	inst:AddTag("notarget")
	
    DropTargets(eater)
	
	GoInvisible(eater)
	
	eater:DoTaskInTime(potiontime, GoVisible)
	
	eater.components.talker:Say("I'm invisible for now.", 4)
end


local function fn()
   
    local inst = CreateEntity()
 
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()
    MakeInventoryPhysics(inst)
    
	inst:AddTag("invisibilitypotion")
	
    inst.AnimState:SetBank("invisibilitypotion")
    inst.AnimState:SetBuild("invisibilitypotion")
    inst.AnimState:PlayAnimation("idle")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")
	inst:AddComponent("stackable")
    inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM
      
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "invisibilitypotion"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/invisibilitypotion.xml"
	
	inst:AddComponent("edible")
	inst.components.edible.hungervalue = 0
	inst.components.edible.hungervalue = 0
	inst.components.edible.sanityvalue = 0
	inst.components.edible:SetOnEatenFn(oneaten)

	inst:AddComponent("tradable")
 
    MakeHauntableLaunch(inst)
     
    return inst
end
return Prefab("common/inventory/invisibilitypotion", fn, assets)

 


If needed i provide the code of my character (character file):

Spoiler


local MakePlayerCharacter = require "prefabs/player_common"


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

-- Custom starting items
local start_inv = {

		"spear",
		"blowdart_pipe",
		"blowdart_pipe",
		"blowdart_pipe",
		"blowdart_pipe",
		"blowdart_pipe",
		"trap",
		"fish",
		"fish",
}
	
-- 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) -- Add tags here

inst:AddTag("mycharactertag")

	-- Minimap icon
	inst.MiniMapEntity:SetIcon( "narisa.tex" )
	
	-- Night Vision
	local NIGHTVISION_COLOURCUBES =
{
    day = "images/colour_cubes/mole_vision_on_cc.tex",
    dusk = "images/colour_cubes/mole_vision_on_cc.tex",
    night = "images/colour_cubes/mole_vision_on_cc.tex",
    full_moon = "images/colour_cubes/mole_vision_on_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

-- Insanity/sanity Aura
 local function CalcSanityAura(inst, observer)
    

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

 return TUNING.SANITYAURA_MED

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

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

-- 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

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

else

inst.components.sanity.night_drain_mult= 0.6

end
  end  

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


-- Can eat monstermeat
inst.components.eater.strongstomach = true

-- Sanity Aura
inst:AddComponent("sanityaura")
    inst.components.sanityaura.aurafn = CalcSanityAura
	
	-- Choose which sounds this character will play
	inst.soundsname = "willow"
	
	-- Stats	
	inst.components.health:SetMaxHealth(200)
	inst.components.hunger:SetMax(75)
	inst.components.sanity:SetMax(150)
	inst.components.temperature.mintemp = 15.
	inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.5)
	inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.5)
	
	
	-- Damage Multiplier
	if (inst.components.temperature:GetCurrent() > 55) then
	
	inst.components.combat.damagemultiplier = 0.6
	
	else
	
	inst.components.combat.damagemultiplier = 1.5
	
	end
	
	-- Peridocic Tasks
	inst:DoPeriodicTask(4, healthsanity, nil, inst)--checks every 4 seconds on your health
	inst:DoPeriodicTask(4, sanityhunger, nil, inst)--checks every 4 seconds on your sanity
	
	inst.OnLoad = onload
    inst.OnNewSpawn = function()
  	onload(inst)
	local backpack = SpawnPrefab("backpack")
	inst.components.inventory:Equip(backpack)
	end
end

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

 


I'm not good at coding! Still i wanna create my own character but i need help ^^' If possible pls explain any of the provided code so I kinda understand how it works thanks in advance :3

Link to comment
Share on other sites

The key to invisibility is just right there in your reach and yet no replies. huh ah well I modified the code to trigger invisibility with equipping a certain item of mine so I removed a bit of code that made it a potion and some other code I didn't see where they were going with it(also some lines were repeated).

local function DropTargets(inst)
    local x,y,z = inst.Transform:GetWorldPosition()    
    local ents = TheSim:FindEntities(x, y, z, 50)--get the creatures within the screen radius
    for k,v in pairs(ents) do
        if v.components.combat and v.components.combat.target == inst then--if the creature is targeting inst
            v.components.combat.target = nil--drop inst
        end
    end
end

local function GoInvisible(inst)
    inst.components.hunger.hungerrate = 1.5 --high hunger rate
    inst.components.locomotor.walkspeed = 2--lower speed
    inst.components.locomotor.runspeed = 4
    inst:AddTag("notarget") --non-targetable
    inst.AnimState:SetMultColour(0.2,0.2,0.2,.2) --hardly visible too ghostly for me
    if inst.DynamicShadow then --no shadow
        inst.DynamicShadow:Enable(false)
    end
    if inst.MiniMapEntity then --remove the minimap icon
        inst.MiniMapEntity:SetEnabled(false)
    end
    if not inst:HasTag("noplayerindicator") then --other players cant see your icon when getting near or faraway
        inst:AddTag("noplayerindicator")
    end
end

local function GoVisible(inst)
    if not inst:HasTag("combatskat") then
    inst.components.hunger.hungerrate = TUNING.WILSON_HUNGER_RATE --regular hunger rate
    inst.components.locomotor.walkspeed = 4 --regular speed
    inst.components.locomotor.runspeed = 6
    inst:RemoveTag("notarget") --npcs can now target inst
    inst.AnimState:SetMultColour(1,1,1,1)--regular color
    if inst.DynamicShadow then --put back the shadow
        inst.DynamicShadow:Enable(true)
    end
    if inst.MiniMapEntity then --icon back
        inst.MiniMapEntity:SetEnabled(true)
    end
    if inst:HasTag("noplayerindicator") then --players can now see your indicater
        inst:RemoveTag("noplayerindicator")
    end
    end
end
    local function stealthmode(inst)
    if inst:HasTag("combatskat") then--does inst have this tag? replace it with beefalo and equip a beefalo hat if you don't believe me
    inst:AddTag("notarget") --put notarget so no npcs can target them
    DropTargets(inst) --do this function
    GoInvisible(inst) --this one as well
    end
end

 

inst:ListenForEvent("equip", stealthmode) --listen when inst puts on something see if inst can go invisible
inst:ListenForEvent("unequip", GoVisible) --listen when inst removes something see if inst goes back to visible

All this goes into the character.lua, all but the last two lines of code go at the very top of the master_postinit and the last two lines near the inst.OnLoad = onload. So you'll need a custom item/equipable to make it work and for that idk what you want to go with. If you need more explanation on some things ill check in the morning its late here.

Edited by K1NGT1GER609
Link to comment
Share on other sites

So far so helpfull ^-^ but then how do i create a custom item like an amulet / neckless to trigger the effect? Also it should be unbreakable :3


Also i think a keybind would be "easier" wouldn't it? ^^' (In my mind it would go kinda like SetKeybind = c)?
 

 

EDIT:

I just noticed there is a diffrence in the hunger rate stuff, what is the diffrence between hunger.burnrate and hunger.hungerrate? If there even is as diffrence xD I'm asking since I'm still changing values :3

Also I'm having issues with uploading the mod to the steam workshop so i don't have to sent it to my friends every time i update it :/ any ideas?
 

UPDATE:
I'm still able to attack while invis is there a possiblity to remove the ability to do that while invis? Didn't think about that ^^'
I also noticed that if i equip any item in my Tool slot and unequip it while invis i loose the invisbility, same if i use wormholes any idea?
Also shadow creatures and hounds should still be able to spot me.

Edited by _FrostyFoxy
Link to comment
Share on other sites

Oh man thats one heck of a subway sandwich of questions so ill answer a couple of them for creating a amulet theres the hard way and the lazy way. The lazy way is by making an item with this:

under sample prefab, it won't show on the character but its good if you want a quick test. The hard way is editing a existing amulet file's tex files, renaming the build.bin and coding it just right. Key bind isn't my speciality today it'll take me time to decode how to keybind. burnrate and hungerrate idk the difference right now im busy atm I need time.uploading the mod to steam workshop can have numerous problems that I can't pin down with only this information. for shadows and hounds i think it goes something like this:

local ents = TheSim:FindEntities(x, y, z, 50, nil, {"hound", "shadow"}, nil)--get the creatures within the screen radius

no combat i say try including these lines to the invisible and visible functions respectively:

inst.components.combat.canattack = true

inst.components.combat.canattack = false

and thats it for the next few hours.(I know I missed a few questions but ill get back to them later)

Edited by K1NGT1GER609
Link to comment
Share on other sites

Alright well for the difference between burnrate and hungerrate according to the notes is not much. Their almost the same if anything burnrate is a old source of code from don't starve and may be removed later on due it being considered depreciated (it has happened before that they removed some old functions and used new ones). So use hungerrate instead. For an unbreakable amulet its as easy as just not including the fuelable or finiteuses components along with any code that tries to use consume/onfinished (usually its code that has inst.remove). Other than that Idk about the wormhole/tool equip problem ill look into it.

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...