Advertisement
nmarkro

MMBN3 coop lua script idea test

Mar 4th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.19 KB | None | 0 0
  1. -- MMBN3 coop lua script idea test
  2. -- hooks ingame functions to create and complete events
  3. -- this might only work with Blue currently due to func addresses being different between versions
  4. -- by NMarkro
  5. memory.usememorydomain("System Bus")
  6.  
  7. pressed = false
  8. queue = {}
  9.  
  10. end_main_func_addr = 0x08000322
  11. -- function calls are addr+1 to call in THUMB mode
  12. give_chip_func_addr = 0x08011281
  13. give_zenny_func_addr = 0x0801480B
  14. give_bugfrag_func_addr = 0x0801485F
  15.  
  16. valid_registers = emu.getregisters()
  17.  
  18. --[[
  19. hooks the end of the main function
  20. code normally looks like this:
  21.     0800031C    ldr     r0,=3006825h                        load some function addr into r0
  22.     0800031E    mov     r14,r15                             move the PC into r14 for returning here
  23.     08000320    bx r0                   <- hooking here     call the function loaded in r0
  24.     08000322    b       80002B4h                            goes back to start of main loop
  25. the idea is to call whatever function we want with r0 then rerun this code normally
  26. --]]
  27. function main_hook()
  28.     if next(queue) ~= nil then
  29.         local index, event = next(queue)
  30.         emu.setregister("R0", event.addr)
  31.         emu.setregister("R14", 0x0800031C)
  32.     end
  33. end
  34.  
  35. -- r0 is used to call functions and as function args
  36. -- we need to set the args manually at the beginning of functions
  37. function set_args(registers)
  38.     for reg, value in pairs(registers) do
  39.         if valid_registers[reg] ~= nil then
  40.             emu.setregister(reg, value)
  41.         end
  42.     end
  43. end
  44.  
  45. -- hooks the give_chip function
  46. -- has 2 purposes, to create new events for other players or complete current queued events
  47. function give_chip_hook()
  48.     -- check if we called this function
  49.     if emu.getregister("R14") == 0x0800031C then
  50.         local index, event = next(queue)
  51.         set_args(event.registers)      
  52.         table.remove(queue, 1)
  53.     else
  54.         -- the player was given this chip normally
  55.         -- here you would create an event from the given args to be sent over the network
  56.     end
  57. end
  58.  
  59. -- hooks the give_zenny function
  60. -- has 2 purposes, to create new events for other players or complete current queued events
  61. function give_zenny_hook()
  62.     -- check if we called this function
  63.     if emu.getregister("R14") == 0x0800031C then
  64.         local index, event = next(queue)
  65.         set_args(event.registers)
  66.         table.remove(queue, 1)
  67.     else
  68.         -- the player was given this zenny normally
  69.         -- here you would create an event from the given args to be sent over the network
  70.     end
  71. end
  72.  
  73. -- hooks the give_bugfrag function
  74. -- has 2 purposes, to create new events for other players or complete current queued events
  75. function give_bugfrag_hook()
  76.     -- check if we called this function
  77.     if emu.getregister("R14") == 0x0800031C then
  78.         local index, event = next(queue)
  79.         set_args(event.registers)
  80.         table.remove(queue, 1)
  81.     else
  82.         -- the player was given this bugfrag normally
  83.         -- here you would create an event from the given args to be sent over the network
  84.     end
  85. end
  86.  
  87. event.onmemoryexecute(main_hook, end_main_func_addr)
  88. -- bizhawk needs even addr for events
  89. event.onmemoryexecute(give_chip_hook, give_chip_func_addr+1)
  90. event.onmemoryexecute(give_zenny_hook, give_zenny_func_addr+1)
  91. event.onmemoryexecute(give_bugfrag_hook, give_bugfrag_func_addr+1)
  92.  
  93. while true do
  94.     local inputs = joypad.get()
  95.     -- pressing L+R+A will give you 1x LavaStg T
  96.     if inputs.L and inputs.R and inputs.A then
  97.         if not pressed then
  98.             -- for give_chip the args are
  99.             -- R0 = chip_id
  100.             -- R1 = chip_code
  101.             -- R2 = amount
  102.             local new_event = {
  103.                 addr=give_chip_func_addr,
  104.                 registers={["R0"]=0x0B3,["R1"]=0x13,["R2"]=0x1}
  105.             }
  106.             table.insert(queue, new_event)
  107.             pressed = true
  108.         end
  109.     -- pressing L+R+B will give you 1000z
  110.     elseif inputs.L and inputs.R and inputs.B then
  111.         if not pressed then
  112.             -- for give_zenny the args are
  113.             -- R0 = amount
  114.             local new_event = {
  115.                 addr=give_zenny_func_addr,
  116.                 registers={["R0"]=1000}
  117.             }
  118.             table.insert(queue, new_event)
  119.             pressed = true
  120.         end
  121.     -- pressing L+R+Select will give you 10 bugfrags
  122.     elseif inputs.L and inputs.R and inputs.Select then
  123.         if not pressed then
  124.             -- for give_bugfrag the args are
  125.             -- R0 = amount
  126.             local new_event = {
  127.                 addr=give_bugfrag_func_addr,
  128.                 registers={["R0"]=10}
  129.             }
  130.             table.insert(queue, new_event)
  131.             pressed = true
  132.         end
  133.     else
  134.         pressed = false
  135.     end
  136.  
  137.     emu.frameadvance()
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement