Advertisement
LilPinkus

lanmolasimul.lua

Jan 18th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.60 KB | None | 0 0
  1. local STATE = 'new_attempt'
  2. local POSITIONS = {}
  3. local CURRENT_POSITION_ID = 0
  4.  
  5. local SAVE = savestate.create()
  6.  
  7. local function tohex(num, i)
  8.     return string.format('%0' .. tostring(i) .. 'X', num)
  9. end
  10.  
  11. local function print_results()
  12.     local hits = {}
  13.     for _, position in ipairs(POSITIONS) do
  14.         local i = tohex(position['y'], 4) .. ',' .. tohex(position['x'], 4)
  15.         local coord = hits[i]
  16.         if coord == nil then
  17.             coord = {}
  18.             hits[i] = coord
  19.         end
  20.         table.insert(coord, position['hits'])
  21.     end
  22.  
  23.     for coord, hits in pairs(hits) do
  24.         local total_hits = 0
  25.         for _, hit in ipairs(hits) do
  26.             total_hits = total_hits + hit
  27.         end
  28.         local avg_hits = total_hits / #hits
  29.  
  30.         print(coord, avg_hits)
  31.     end
  32. end
  33.  
  34. local function init_positions()
  35.     local lanmo_xs = {0x58, 0x50, 0x60, 0x70, 0x80, 0x90, 0xA0, 0x98}
  36.     local lanmo_ys = {0x68, 0x60, 0x70, 0x80, 0x90, 0xA0, 0xA8, 0xB0}
  37.  
  38.     local starting_y = 0x0763
  39.     local starting_x = 0x0678
  40.     for dy = -2, 8 do
  41.         local y = starting_y + dy
  42.         for dx = -1, 4 do
  43.             for _, lanmo_x in ipairs(lanmo_xs) do
  44.                 for _, lanmo_y in ipairs(lanmo_ys) do
  45.                     local x = starting_x + dx
  46.                     for framerule = 0, 3 do
  47.                         position = {
  48.                             ['hits'] = 0,
  49.                             ['y'] = y,
  50.                             ['x'] = x,
  51.                             ['framerule'] = framerule,
  52.                             ['lanmo_x'] = lanmo_x,
  53.                             ['lanmo_y'] = lanmo_y,
  54.                         }
  55.                         table.insert(POSITIONS, position)
  56.                     end
  57.                 end
  58.             end
  59.         end
  60.     end
  61. end
  62.  
  63. local function set_position()
  64.     local position = POSITIONS[CURRENT_POSITION_ID]
  65.     memory.writebyte(0x7E0DA0, position['lanmo_x'])
  66.     memory.writebyte(0x7E0DB0, position['lanmo_y'])
  67. end
  68.  
  69. local function tick()
  70.     local position = POSITIONS[CURRENT_POSITION_ID]
  71.  
  72.     local frame = emu.framecount()
  73.     local first_shot = frame == 99700
  74.     local second_shot = frame == 99718
  75.     local check_results = frame >= 99768
  76.  
  77.     if STATE == 'new_attempt' then
  78.         savestate.load(SAVE)
  79.         STATE = 'before_shoot'
  80.  
  81.         CURRENT_POSITION_ID = CURRENT_POSITION_ID + 1
  82.         position = POSITIONS[CURRENT_POSITION_ID]
  83.         if not position then
  84.             STATE = 'done'
  85.         else
  86.             memory.writeword(0x7E0020, position['y'])
  87.             memory.writeword(0x7E0022, position['x'])
  88.             memory.writebyte(0x7E001A, position['framerule'])
  89.         end
  90.     elseif STATE == 'before_shoot' then
  91.         if first_shot then
  92.             joypad.set(1, {['Y'] = true})
  93.         elseif second_shot then
  94.             joypad.set(1, {['Y'] = true})
  95.             STATE = 'wait_for_results'
  96.         end
  97.     elseif STATE == 'wait_for_results' then
  98.         if check_results then
  99.             local b1_hits = 4 - (memory.readbyteunsigned(0x7E0E50) / 4)
  100.             local b2_hits = 4 - (memory.readbyteunsigned(0x7E0E51) / 4)
  101.             position['hits'] = b1_hits + b2_hits
  102.             STATE = 'new_attempt'
  103.         end
  104.     end
  105. end
  106.  
  107. savestate.save(SAVE)
  108.  
  109. emu.registerbefore(tick)
  110. memory.registerexecute(0x05a411, set_position)
  111.  
  112. init_positions()
  113.  
  114. while STATE ~= 'done' do
  115.     snes9x.frameadvance()
  116.     gui.text(50, 50, 'attempt ' .. tostring(CURRENT_POSITION_ID) .. '/' .. tostring(#POSITIONS) .. ' ' .. tostring(CURRENT_POSITION_ID / #POSITIONS * 100) .. '%')
  117. end
  118.  
  119. print_results()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement