Disabling P-Switch Effect for Coins

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
dochalkos
Shy Guy
Shy Guy
Posts: 5
Joined: Sat Nov 26, 2022 8:49 pm

Disabling P-Switch Effect for Coins

Postby dochalkos » Mon Nov 28, 2022 5:41 pm

Trying to get a coin NPC to not transform into a block of some kind while a P-switch event is active. Block configs have "pswitchable" to change their behavior, but I can't find anything that works in reverse.

deice
Volcano Lotus
Volcano Lotus
Posts: 548
Joined: Fri Jul 23, 2021 7:35 am

Re: Disabling P-Switch Effect for Coins

Postby deice » Tue Nov 29, 2022 2:15 pm

the best and cleanest solution to this is to create a custom coin npc. if i recall correctly, it shouldn't be that difficult to do using the "iscoin" configuration.

but, if it's absolutely necessary to use the vanilla coins, here's some code i wrote some time ago for a scrapped library that should do the trick:

Code: Select all

local coinIDs = { 10 } -- add/remove coin ids here, comma-separated

local function checkNPCSection(n)
	if(n:mem(0x12C, FIELD_WORD) > 0) then
		n.section = Player(n:mem(0x12C, FIELD_WORD)).section
		return
	end
	for i = 0, Section.count() - 1 do
		if(n.x >= Section(i).boundary.left and 
		n.x + n.width <= Section(i).boundary.right and
		n.y >= Section(i).boundary.top and
		n.y + n.height <= Section(i).boundary.bottom) then
			n.section = i
			return
		end
	end
	local min_section = -1
	local min_dist = -1
	
	local nch = (n.x + n.width * 0.5)
	local ncv = (n.y + n.height * 0.5)
	
	for i = 0, Section.count() - 1 do
		local disth = 0.5 * (Section(i).boundary.left + Section(i).boundary.right) - nch
		local distv = 0.5 * (Section(i).boundary.top + Section(i).boundary.bottom) - ncv
		local cdist = disth*disth + distv*distv
		if(min_dist == -1 or cdist < min_dist) then
			min_section = i
			min_dist = cdist
		end
	end
	n.section = min_section
end

function onPostEventDirect(e)
	if(e == "P Switch - Start") then
		for _, v in Block.iterate() do
			for _, cid in ipairs(coinIDs) do
				if(v.isValid and v.layerName ~= "Destroyed Blocks" and v:mem(0x5C, FIELD_WORD) == cid) then
					local newnpc = NPC.spawn(cid, v.x, v.y, 0, true)
					checkNPCSection(newnpc)
					newnpc.layerName = v.layerName
					newnpc.deathEventName = v:mem(0x10, FIELD_STRING)
					newnpc.noMoreObjInLayer = v:mem(0x14, FIELD_STRING)
					newnpc.x = newnpc.x + (v.width - newnpc.width) * 0.5
					newnpc.isHidden = v.isHidden
					v:remove(false)
					break
				end
			end
		end
	end
end
just a warning though that this is not only a little hacky (though it's not too different from what happens in the vanilla code when the p-switch effect ends) but also hasn't been extensively tested in a while so there might be some jank i don't remember.

dochalkos
Shy Guy
Shy Guy
Posts: 5
Joined: Sat Nov 26, 2022 8:49 pm

Re: Disabling P-Switch Effect for Coins

Postby dochalkos » Thu Dec 01, 2022 6:24 am

deice wrote:
Tue Nov 29, 2022 2:15 pm
the best and cleanest solution to this is to create a custom coin npc. if i recall correctly, it shouldn't be that difficult to do using the "iscoin" configuration.
I was trying to figure out what iscoin actually did, like specifically what coin-like behaviors it entails, because I think it includes the P-switch-inflicted transformation behavior. The custom coin I set up functions pretty much like a standard coin, which includes turning into a brick when a switch is active (though when it reverts back it lacks its animation for some reason)
Now I'm realizing I should probably just make a custom switch that works on a timer but doesn't include P-shenanigans.

Just_Thomas
Snifit
Snifit
Posts: 233
Joined: Sat Dec 16, 2023 3:32 am
Pronouns: he/him

Re: Disabling P-Switch Effect for Coins

Postby Just_Thomas » Sat Dec 16, 2023 1:41 pm

deice wrote:
Tue Nov 29, 2022 2:15 pm
...
Thanks a lot for that. VERY useful for the dragon coins (npc-274).


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 4 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari