Contra (NES) - Lua

From ROM Detectives Wiki
Jump to navigation Jump to search

Change Gun With Select

Cycle through the guns by pressing select.

The following Lua script lets either player cycle through the guns by press select. It works with FCEUX and BizHawk and on every version of the ROM.

-- Lua script for Contra.
-- Pressing select cycles through the guns for each player.
-- Written by Dean Tersigni (TheAlmightyGuru), 2021-01-13
-- 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

selected = {}

while true do
	-- Loop through both players.
	for i = 1, 2 do
		-- Use emulator-specific methods for reading joypad input.
		if emulator == "FCEUX" then
			gamepad = joypad.get(i)
			select = gamepad.select
			
		elseif emulator == "BizHawk" then
			gamepad = joypad.get(i)
			select = gamepad.Select
		end

		if select == true then
			if selected[i] == false then
				selected[i] = true
				
				-- Read the current memory address of the gun.
				gun = memory.readbyte(0x00A9 + i)
				
				-- Give Rapid Fire to the player if they don't yet have it.
				if gun < 0x10 then
					gun = 0x10
				end
				
				-- Cycle through the guns.
				gun = gun + 1
				if gun > 0x14 then
					gun = 0x10
				end
				
				-- Update the memory address of the gun.
				memory.writebyte(0x00A9 + i, gun)
			end
		else
			-- Require the player to release the select button each time they change guns.
			selected[i] = false
		end
	end
	
	emu.frameadvance()
end

Highlight Bullets

Enemy bullets are now much more obvious.

This Lua script highlights enemy bullets in flashing red and white boxes so that they're easier to see. It works on both FCEUX and BizHawk and on every version of the ROM.

-- Lua script for Contra.
-- Makes the bullets more conspicuous.
-- Written by Dean Tersigni (TheAlmightyGuru), 2021-01-14
-- 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

function box(x1, y1, x2, y2, fillColor, lineColor)
	-- Use emulator-specific methods for drawing.
	if x1 > -1 and x1 < 256 and x2 > -1 and x2 < 256 and y1 > -1 and y1 < 242 and y2 > -1 and y2 < 242 then
		if emulator == "FCEUX" then
			gui.box(x1, y1, x2, y2, fillColor, lineColor)
		elseif emulator == "BizHawk" then
			gui.drawBox(x1, y1, x2, y2, fillColor, lineColor)
		end
	end
end

-- Load flashing variables.
flashDelay = 3
flashCount = 0
color = "white"

while true do
	-- Loop through both players.
	for i = 1, 25 do
		-- Make bullets flash.
		flashCount = flashCount + 1
		if flashCount == flashDelay then
			flashCount = 0
			if color == "white" then
				color = "red"
			else
				color = "white"
			end
		end
		
		-- Load the object information.
		objectGraphic = memory.readbyte(0x300 + i)
		objectY = memory.readbyte(0x31A + i)
		objectX = memory.readbyte(0x334 + i)
		objectId = memory.readbyte(0x34E + i)
		
		if (objectId == 0x01 and objectGraphic == 0x1E) 		-- White enemy bullets.
		 or (objectId == 0x02 and objectGraphic == 0x07) then	-- Red enemy bullets.
			box(objectX - 3, objectY - 2, objectX + 1, objectY + 2, color, color)
		end
	end
	
	emu.frameadvance()
end