The Legend of Zelda (NES) - Lua

From ROM Detectives Wiki
Jump to navigation Jump to search

Gambling Cheat

The Gambling Cheat in action.

This FCEUX Lua script will display on the screen the hidden value of rupees in the Money Making Game. It works on all versions of the game including the FDS beta.

-- Lua script for The Legend of Zelda.
-- This script reveals the hidden values behind the rupees in the Money Making Game.
-- Written by Dean Tersigni (TheAlmightyGuru), 2020-12-30
-- Please visit www.romdetectives.com for more scripts.

-- Determine which emulator is being used.
if(FCEU) then
	emulator = "FCEUX"
elseif(bizstring) then
	emulator = "BizHawk"
else
	emulator = "Unknown"
end

while true do
 	-- Address 0x0012 is the room state. A value of 0x0B indicates that we're in a grotto.
	-- Address 0x0350 is the first object in the room.
	-- In a Money Making Game, that will be the Gambling Old Man, which is 0x70.
	if memory.readbyte(0x0012) == 0x0B and memory.readbyte(0x0350) == 0x70 then

		-- Special check to see if we're running the beta version.
		-- All published versions will have a 0x00 in this location,
		-- the beta version has a garbage value.
		betaCheck = memory.readbyte(0x0700)
		
		-- Read each of the three hidden values.
		for i=0, 2 do
			memoryValue = memory.readbyte(0x0448 + i)
			
			-- Determine the text and foreground and background colors based on the values.
			if betaCheck == 0x32 then
				-- The beta FDS version had alternate values.
				if memoryValue == 0x01 then
					rupeeValue = "+1"
					foreColor = "red"
					backColor = "black"
				elseif memoryValue == 0x0A then
					rupeeValue = "+10"
					foreColor = "gray"
					backColor = "black"
				elseif memoryValue == 0x14 then
					rupeeValue = "-20"
					foreColor = "red"
					backColor = "black"
				elseif memoryValue == 0x1E then
					rupeeValue = "+30"
					foreColor = "green"
					backColor = "black"
				elseif memoryValue == 0x32 then
					rupeeValue = "+50"
					foreColor = "black"
					backColor = "green"
				end
			else
				-- Every other version uses these values.
				if memoryValue == 0x0A then
					rupeeValue = "-10"
					foreColor = "red"
					backColor = "black"
				elseif memoryValue == 0x14 then
					rupeeValue = "+20"
					foreColor = "green"
					backColor = "black"
				elseif memoryValue == 0x28 then
					rupeeValue = "-40"
					foreColor = "black"
					backColor = "red"
				elseif memoryValue == 0x32 then
					rupeeValue = "+50"
					foreColor = "black"
					backColor = "green"
				end
			end
			
			if memoryValue ~= 0 then
				-- Reveal the hidden values to the player.
				if emulator == "FCEUX" then
					gui.text(76 + (i * 32), 150, rupeeValue, foreColor, backColor)
				elseif emulator == "BizHawk" then
					gui.drawText(76 + (i * 32), 150, rupeeValue, foreColor, backColor)
				end
			end
		end
	end
	
	emu.frameadvance()
end

Teleport

Using the mouse to teleport Link around the screen.

This script allows the player to teleport anywhere on the screen by clicking the middle mouse button. It also lets you teleport to a specific room on the map by middle-clicking on the map. Note that you won't immediately move to that room, instead, the next time you leave the room, the game will think you've just left the room you've clicked on. Underworld map teleporting is very buggy.

-- Lua script for The Legend of Zelda.
-- This script lets you teleport anywhere on the screen by middle-clicking the mouse,
-- or to a new room by middle-clicking on the map. Map teleporting tricks the game into 
-- thinking you're currently in the new room, so you still have to exit the room to 
-- update it. Underworld teleporting is very buggy.
-- Written by Dean Tersigni (TheAlmightyGuru), 2020-12-31
-- Please visit www.romdetectives.com for more scripts.

-- Determine which emulator is being used.
if(FCEU) then
	emulator = "FCEUX"
elseif(bizstring) then
	emulator = "BizHawk"
else
	emulator = "Unknown"
end

while true do
	-- Use emulator-specific methods for reading mouse input.
	if emulator == "FCEUX" then
		mouse = input.get()
		middleClick = mouse.middleclick
		mouseX = mouse.xmouse
		mouseY = mouse.ymouse
	elseif emulator == "BizHawk" then
		mouse = input.getmouse()
		middleClick = mouse.Middle
		mouseX = mouse.X

		-- Correct for NTSC missing the first 8 scanlines.
		if emu.getdisplaytype() == "NTSC" then
			mouseY = mouse.Y + 8
		else
			mouseY = mouse.Y
		end
	end
	
	if middleClick then
		gui.text(50, 50, mouseX .. ", " .. mouseY)

		-- Check for the middle click being in the action area.
		if mouseX > 7 and mouseX < 249 and mouseY > 68 and mouseY < 230 then
			-- Move Link to the position of the click.
			memory.writebyte(0x0070, mouseX - 8)
			memory.writebyte(0x0084, mouseY - 8)
		end
		
		-- Check for the middle click being in the map area.
		if mouseX > 15 and mouseX < 80 and mouseY > 23 and mouseY < 56 then
			-- Determine the room location based on where the user clicked in the map area.
			col = math.floor((mouseX - 16) / 4)
			row = math.floor((mouseY - 24) / 4)
			room = string.format("%x", row) .. string.format("%x", col)

			mapX = (col * 4) + 16
			mapY = (row * 4) + 24

			-- Give feedback so the user knows where they will land.
			if emulator == "FCEUX" then
				gui.drawbox(mapX, mapY, mapX + 3, mapY + 3, "blue", "white")
			elseif emulator == "BizHawk" then
				gui.drawBox(mapX, mapY, mapX + 3, mapY + 3, "blue", "white")
			end
			
			-- Set the player's current room id to the room they clicked on.
			-- Note: this won't automatically update, but the game will treat it as the current room.
			memory.writebyte(0x00EB, tonumber(room, 16))
		end
	end
	
	emu.frameadvance()
end