Jump to content

Removing magic sanity drain?


Recommended Posts

So, I've been thinking about a perk for a character who is a magician, and thus shouldn't lose sanity when using staffs and other magic items.

However, I'm well aware of the fact that the sanity drain is coded in items themselves, so I wanted to ask. Does anyone have any ideas on how to integrate it to the mod? The only thing that comes to mind is making a custom recipe for each item(kind of cloning them) and making it that only my character can craft and use them. Which sounds like reverse engineering which I'd rather avoid.

Thanks in advance for any ideas!

Link to comment
Share on other sites

This is the beginning of the onattack function for one of the staves:

local function onattack_blue(inst, attacker, target, skipsanity)
    if not skipsanity and attacker ~= nil and attacker.components.sanity ~= nil then
        attacker.components.sanity:DoDelta(-TUNING.SANITY_SUPERTINY)
    end
........

So, what I would do, is save a local copy of the old onattack, and make a new onattack which simply calls the old onattack function with the "skipsanity" parameter set to true if the attacker's prefab is your character, and otherwise calls the old onattack without the "skipsanity" parameter. Normally I'd do a write-up, but I'm on my way out the door :)

Edited by Ultroman
Link to comment
Share on other sites

Never ever (almost) the last part.

Use the AddPrefabPostInit function to change each of the prefabs you want to change. You can find information on how to use that on the API wiki and in the API examples. The Guides thread and the Getting Started With Modding thread are also full of good information.

Only change the prefabs you have to. I think there're only two staves which reduce sanity on use, but don't quote me on that. Look at the staff.lua to figure that out.

Link to comment
Share on other sites

Klei should've really added some 'source' argument for DoDelta for cases like this, but there's still a solution for this

A bit of a hack of a solution, but you could trick the staffs to use a DoDelta function that doesn't accept negative values

In modmain.lua:

Spoiler

local magician = 'prefabname'


local function staff_weapon (inst)

	local onattack_old = inst.components.weapon.onattack

	inst.components.weapon.onattack = function (inst, attacker, target, skipsanity, ...)

		if attacker ~= nil and attacker.prefab == magician then
			onattack_old(inst, attacker, target, true, ...)
		else
			onattack_old(inst, attacker, target, skipsanity, ...)
		end

	end

end


local function staff_spell (inst)

	local spell_old = inst.components.spellcaster.spell

	inst.components.spellcaster.spell = function (inst, target, ...)

		local caster = inst.components.inventoryitem.owner or target

		if caster ~= nil and caster.prefab == magician and caster.components.sanity ~= nil then

			local DoDelta_old = caster.components.sanity.DoDelta

			caster.components.sanity.DoDelta = function (self, delta, ...)
				if delta >= 0 then
					DoDelta_old(self, delta, ...)
				end
			end

			spell_old(inst, target, ...)

			caster.components.sanity.DoDelta = DoDelta_old

		else
			spell_old(inst, target, ...)
		end

	end

end


local function staff_blink (inst)

	local onblinkfn_old = inst.components.blinkstaff.onblinkfn

	inst.components.blinkstaff.onblinkfn = function (staff, pos, caster, ...)

		if caster ~= nil and caster.prefab == magician and caster.components.sanity ~= nil then

			local DoDelta_old = caster.components.sanity.DoDelta

			caster.components.sanity.DoDelta = function (self, delta, ...)
				if delta >= 0 then
					DoDelta_old(self, delta, ...)
				end
			end

			onblinkfn_old(staff, pos, caster, ...)

			caster.components.sanity.DoDelta = DoDelta_old

		else
			onblinkfn_old(staff, pos, caster, ...)
		end

	end

end


local staffs = {
	icestaff = staff_weapon,
	firestaff = staff_weapon,
	telestaff = staff_spell,
	orangestaff = staff_blink,
	greenstaff = staff_spell,
	yellowstaff = staff_spell,
	opalstaff = staff_spell,
}


for k, v in pairs(staffs) do
	AddPrefabPostInit(k, v)
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...