Skip to content

Instantly share code, notes, and snippets.

@TurbulentEddie
Created January 31, 2016 06:14
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 TurbulentEddie/4e8fb8e52f64a99dfeb0 to your computer and use it in GitHub Desktop.
Save TurbulentEddie/4e8fb8e52f64a99dfeb0 to your computer and use it in GitHub Desktop.
NES Open Random Mode
-- NES Open Random Mode
-- Written by Turbulent Eddie
----------------------------
------- Set options --------
----------------------------
randomize_aim = false; -- randomize aiming cursor
randomize_wind_speed = true; -- randomize wind speed
randomize_wind_dir = true; -- randomize wind direction
min_wind = 0; -- min wind speed, must be >= 0 and <= 255
max_wind = 255; -- max wind speed, must be >= 0 and <= 255
force_1W = false; -- force 1W for all shots (including putts)
aim_sens = 3; -- how much can aim change each step
wind_speed_sens = 5; -- how much can wind speed change each step
wind_dir_sens = 1; -- how much can wind direction change each step
show_wind_speed = true; -- show updated wind speeds on the screen
update_rate = 10; -- update every n frames (higher is slower, lower is faster)
----------------------------
------- End options --------
----------------------------
-- Get the RAM pointers
pntrs = {};
pntrs['aim'] = 0x00B7;
pntrs['u_dir'] = 0x0096;
pntrs['u_mag'] = 0x0097;
pntrs['power'] = 0x00D6;
pntrs['curve'] = 0x00D7;
pntrs['ready'] = 0x00FF;
pntrs['lie'] = 0x00C9;
pntrs['club'] = 0x00CD;
while (true) do
-- Get the RAM values
vals = {};
for key,value in pairs(pntrs) do
vals[key] = memory.readbyte(value);
end;
-- Every nth frame
if (emu.framecount() % update_rate == 0) then
-- Modify the RAM values by a random amount as set in the options
if randomize_aim then -- aim
memory.writebyte(pntrs['aim'], vals['aim'] + math.random(-aim_sens, aim_sens));
end;
if randomize_wind_speed then -- wind speed
memory.writebyte(pntrs['u_mag'], math.max(min_wind, math.min(max_wind, vals['u_mag'] + math.random(-wind_speed_sens, wind_speed_sens))));
end;
if randomize_wind_dir then -- wind direction
memory.writebyte(pntrs['u_dir'], vals['u_dir'] + math.random(-wind_dir_sens, wind_dir_sens)*8);
end;
end;
if force_1W then -- force 1W
memory.writebyte(pntrs['club'], 0);
end;
-- The wind speed value is not automatically rendered, so write it ourselves,
-- but only when menuing and if set in the options
if (vals['power'] == 48) and (vals['curve'] == 48) and (vals['ready'] == 0) and show_wind_speed then
gui.box(40, 78, 55, 87, 'black');
gui.text(40, 79, string.format('%2dmph', memory.readbyte(0x0097)));
end;
-- Render frame
emu.frameadvance();
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment