Skip to content

Instantly share code, notes, and snippets.

@asiansteev
Created October 7, 2010 07:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asiansteev/614747 to your computer and use it in GitHub Desktop.
Save asiansteev/614747 to your computer and use it in GitHub Desktop.
SCRIPT_TITLE = "Mega Man 2 - Weapon Select"
SCRIPT_VERSION = "1.0"
require "m_utils"
m_require("m_utils",0)
--[[
Just a quick script to allow Mega Man to cycle through his weapons with the select button without having to pause.
It works, but it's pretty buggy.
Mega Man does not change weapon sprites with this method, so the weapons look like garbage.
Time Stopper will sometimes freeze if you switch while it's running. Not entirely bad, but it will get you stuck if you enter a boss room.
You can cycle through weapons you haven't unlocked yet.
Otherwise it's a lot of fun. Really!
TODO: Fix weapon sprites, "fix" cycling to currently locked weapons.
-steev
-]]
weapon_names = {"Arm Cannon", "Atomic Fire", "Air Shooter", "Leaf Shield", "Bubble Lead", "Quick Boomerang", "Time Stopper", "Metal Blade", "Crash Bombs", "Item-1", "Item-2", "Item-3"}
primary_colors = {17, 21, 17, 25, 0, 37, 20, 24, 38, 22, 22, 22}
secondary_colors = {44, 40, 48, 48, 48, 52, 52, 55, 48, 48, 48, 48}
function update_weapons(prev_select)
j = joypad.read(1).select
if j and prev_select == false then
next_weapon = current_weapon + 1
if next_weapon >= #weapon_names then next_weapon = 0 end
memory.writebyte(0x00A9, next_weapon)
memory.writebyte(0x08A9, next_weapon)
memory.writebyte(0x10A9, next_weapon)
memory.writebyte(0x18A9, next_weapon)
memory.writebyte(0x0369, primary_colors[next_weapon+1])
memory.writebyte(0x0B69, primary_colors[next_weapon+1])
memory.writebyte(0x1369, primary_colors[next_weapon+1])
memory.writebyte(0x1B69, primary_colors[next_weapon+1])
memory.writebyte(0x0368, secondary_colors[next_weapon+1])
memory.writebyte(0x0B68, secondary_colors[next_weapon+1])
memory.writebyte(0x1368, secondary_colors[next_weapon+1])
memory.writebyte(0x1B68, secondary_colors[next_weapon+1])
end
end
function display_weapon(current_weapon)
gui.text(16,8, weapon_names[current_weapon+1])
gui.text(16,16, current_weapon)
gui.text(16,24, #weapon_names)
end
while(true) do
current_weapon = memory.readbyte(0x00A9)
display_weapon(current_weapon)
update_weapons(prev_select)
prev_select = joypad.read(1).select
EMU.frameadvance()
end
@xtoddx
Copy link

xtoddx commented Oct 8, 2010

0x00A9? 0x08A9? Hell yea! I love magic numbers!

@asiansteev
Copy link
Author

haha, i would have thought the descriptive variable names that are being assigned would have been enough. primary_color, secondary_color... you can figure it out. why exactly the same value is being set in four locations i'm still not positive about. setting the values in the first location for each actually works, but when the NES does it, it uses all four, so when in rome...

@nijoakim
Copy link

Time for an almost four years late mini-comment: The value is being set at four locations because of the memory map of the NES; Memory ranges: [0x0800..0x0FFF], [0x1000..0x17FF] and [0x1800..0x1FFF] are actually mirrors of memory range [0x0000..0x07FFF]. Source: http://wiki.nesdev.com/w/index.php/CPU_memory_map.

Also, cool thing you've done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment