Jump to content

[Solved] Make flowers hauntable by spell/ability


Recommended Posts

Hello, still working on the staff tweaking mod. Right now I am changing how opal staff (or moon caller's staff) works, I want that the spell (right click) forces a haunt and pseudo-full moon effect on all/most entities in a 20 range radius from caster. Right now it works, but flowers, mushrooms and a few other stuff are not affected by it (I haven't tried it on caves, or on a server with caves, so I don't know how it will go there...), here is the code:

Spoiler

AddPrefabPostInit("opalstaff", function(inst)
	
	local function change(doer, inst)
	
		if inst:IsInLimbo() then
			return
		end

		if inst.components.hauntable ~= nil then
			doer:PushEvent("haunt", { target = inst } )
			inst.components.hauntable:DoHaunt(doer)
			return
		end
		
		if inst.components.werebeast ~= nil and not inst:HasTag("werepig") then
			local remainingtime = TUNING.TOTAL_DAY_TIME * (1 - GLOBAL.TheWorld.state.time)
			local mintime = TUNING.SEG_TIME
			inst.components.werebeast:SetWere(math.max(mintime, remainingtime) + math.random() * TUNING.SEG_TIME)
			return
		end
		
		if inst:HasTag("player") and inst:HasTag("woodcutter") and not inst:HasTag("beaver") then
			if inst.components.beaverness:GetPercent() >= TUNING.WOODIE_TRANSFORM_TO_HUMAN	then
				inst.components.beaverness:DoDelta(-TUNING.SANITY_MEDLARGE)
			end
			inst:PushEvent("transform_werebeaver")
			return
		end
		
	end
    
	local function forcechange(staff)
	
		local caster = staff.components.inventoryitem.owner
			
        caster.components.sanity:DoDelta(-TUNING.SANITY_LARGE)

        local x, y, z = caster.Transform:GetWorldPosition()
        local range = 30
		local ents = GLOBAL.TheSim:FindEntities(x, y, z, range, nil, { "hauntable", "pickable", "stump", "INLIMBO" })
		if #ents > 0 then
			--change(table.remove(ents, math.random(#ents)))
            if #ents > 0 then
				local timevar = 1 - 1 / (#ents + 1)
                for i, v in ipairs(ents) do
                    v:DoTaskInTime(timevar * math.random(), change(caster, v))
				end
			end
        end
	end
	
	local function onattack_opal(weapon, attacker, target)
	
		if not skipsanity and attacker ~= nil and attacker.components.sanity ~= nil then
			attacker.components.sanity:DoDelta(-TUNING.SANITY_SUPERTINY)
		end
		
		if target.components.combat ~= nil then
            target.components.combat:SetTarget(nil)
        end
		
		if not target:IsValid() then
			return
		end
	
		if target.components.freezable ~= nil then
			target.components.freezable:AddColdness(-1)
			if target.components.freezable:IsFrozen() then
				target.components.freezable:Unfreeze()
			end
		end

		if target.components.sleeper ~= nil and target.components.sleeper:IsAsleep() then
			target.components.sleeper:WakeUp()
		end
		
	end
	
	inst:AddTag("rangedweapon")

	inst.components.spellcaster:SetSpellFn(forcechange)
	
	inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(0)
    inst.components.weapon:SetRange(5, 7)
    inst.components.weapon:SetProjectile("ice_projectile")
	inst.components.weapon:SetOnAttack(onattack_opal)
end)

 

When the spell is casted pretty much everything around the player is haunted except for things like reeds, grass tufts, saplings, mushrooms (only unpicked, surfaced mushrooms; picked mushrooms and hidden mushrooms are also haunted) and flowers. Of all of those the ones that I care is flowers and mushrooms as they are affected by haunting (50% chance to turn into evil flowers, and mushrooms can change into another color, I think the chance is 10%). So I would like to know what does my code needs in order to haunt the flowers, if it is possible at all. No big deal, but is something I would like to add.

On another note I would also like to know if it is possible to make nightmare creatures (or any mob) immune to a specific weapon attack. Asking because I tweaked the purple/telelocator staff to deal damage from a range, but I want to make them immune to it. I tried by filtering with HasTag in the onattack function, but it didn't work.

Edited by pedregales
Link to comment
Share on other sites

Finally made it work after several hours of trial and error plus a few console logs and turning off and on pieces of codes xD. Turns out I was using too many tags in TheSim:FindEntities (now I only ask for "hauntable" and "INLIMBO", and I think the last one might not be needed), but was afraid to take them out thinking some things might not be haunted when casted. Leaving this here if someone might want to do something with 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...