Advertisement
Guest User

Kirby's Adventure Lua Bizhawk

a guest
Sep 18th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.08 KB | None | 0 0
  1. local ScriptInfo = {"Kirby's Adventure LUA script for Bizhawk 2.9",
  2.                     "v0.1 fix",
  3.                     "Sep 2024"}
  4.  
  5. local Padding = {
  6.     Left = 40,
  7.     Top = 20,
  8.     Right = 180,
  9.     Bottom = 20
  10. }
  11.  
  12.  
  13. local AddressTableKirby= {
  14. [1] = {0,"X",0xFFB0B0B0},  
  15. [2] = {1,"Byte",0xFFFFB0C0,"RAM",0x0083,1,true,false},
  16. [3] = {1,"Map",0xFFFFB0C0,"RAM",0x0095,1,true,false},
  17. [4] = {1,"Sub",0xFFFFB0C0,"RAM",0x0071,1,true,false},
  18. [5] = {1,"Speed",0xFFFFB0C0,"RAM",0x05B9,2,true,false},
  19. [6] = {0,"Y",0xFFB0B0B0},  
  20. [7] = {1,"Byte",0xFFFFB0C0,"RAM",0x00b9,1,true,false},
  21. [8] = {1,"Map",0xFFFFB0C0,"RAM",0x00cb,1,true,false},
  22. [9] = {1,"Sub",0xFFFFB0C0,"RAM",0x00a7,1,true,false},
  23. [10] = {1,"Speed",0xFFFFB0C0,"RAM",0x05Bd,2,true,false}
  24. }
  25.  
  26. local Displays = {
  27.     -- Name, active, col, x, y, width, height, offset of list, edit mode, table, entries shown
  28.     [1]= {"Kirby", false, 0xFFFFB0C0, 40, 40, 162, 98, 0, false, AddressTableKirby,10}
  29. }
  30.  
  31. local Mouse = {
  32.     X               = 0,
  33.     Y               = 0,
  34.     XBefore         = 0,
  35.     YBefore         = 0,
  36.     clickedFrames   = 0,
  37.     clicked         = false
  38. }
  39.  
  40. local Color = {
  41.     Selected    = 0xB0A0A0A0,
  42.     Normal      = 0xA0303030,
  43.     Grey        = 0xA0C0C0C0
  44. }
  45.  
  46. function text(x, y, text, color, backcolor)
  47.     if backcolor==nil then backcolor=0x00000000 end
  48.     gui.drawText(x, y, text,color,backcolor,10,"Arial")
  49. end
  50.  
  51. function box(x,y,x2,y2)
  52.     gui.drawBox(x,y,x2,y2,0x00000000,0xD0000000)
  53. end
  54.  
  55. function boxNormal(x,y,x2,y2)
  56.     gui.drawBox(x,y,x2,y2,0xFF000000,0xA0000000)
  57. end
  58.  
  59. function boxSelected(x,y,x2,y2)
  60.     gui.drawBox(x,y,x2,y2,0xFF000000,0xA0505050)
  61. end
  62.  
  63. function arrowDown(xpos,ypos,col)
  64.     gui.drawLine(xpos,ypos,xpos+6,ypos,col)
  65.     gui.drawLine(xpos+1,ypos+1,xpos+5,ypos+1,col)
  66.     gui.drawLine(xpos+2,ypos+2,xpos+4,ypos+2,col)
  67.     gui.drawPixel(xpos+3,ypos+3,col)
  68. end
  69.  
  70. function arrowUp(xpos,ypos,col)
  71.     gui.drawLine(xpos,ypos,xpos+6,ypos,col)
  72.     gui.drawLine(xpos+1,ypos-1,xpos+5,ypos-1,col)
  73.     gui.drawLine(xpos+2,ypos-2,xpos+4,ypos-2,col)
  74.     gui.drawPixel(xpos+3,ypos-3,col)
  75. end
  76.  
  77. function drawDisplayBox(id,bordercolor,color)
  78.     posx=Displays[id][4]
  79.     posy=Displays[id][5]
  80.     width=Displays[id][6]
  81.     height=Displays[id][7]
  82.    
  83.     if Mouse.clicked then
  84.  
  85.         if Mouse.X > posx and Mouse.X < posx+width and Mouse.Y > posy and Mouse.Y < posy+height then
  86.  
  87.             if Mouse.clickedFrames > 0 then
  88.                 menuscreen=0 -- close menu
  89.                
  90.                 posy = posy + (Mouse.Y-Mouse.YBefore) -- enables mouse drag
  91.                 posx = posx + (Mouse.X-Mouse.XBefore)
  92.  
  93.                 vertical_padding = Padding.Top+Padding.Bottom
  94.                 horizontal_padding = Padding.Left+Padding.Right
  95.  
  96.                 if posy < 0 then posy=0 -- prevents display from going offscreen
  97.                 elseif posy > 159+vertical_padding-height then posy=159+vertical_padding-height end
  98.                 if posx < 0 then posx=0
  99.                 elseif posx > 239+horizontal_padding-width then posx=239+horizontal_padding-width end
  100.            
  101.                 Displays[id][4]=posx
  102.                 Displays[id][5]=posy
  103.             end
  104.         end
  105.     end
  106.     gui.drawBox(posx,posy,posx+width,posy+height,bordercolor,color)
  107.     text(posx+3,posy-1,Displays[id][1],0xFF808080)
  108. end
  109.        
  110. function drawCloseButton(id)
  111.     width=Displays[id][6]
  112.     x=Displays[id][4]
  113.     y=Displays[id][5]
  114.    
  115.     drawButton(x+width-10,y,10,10,"x",Color.Grey,15,function()
  116.         Displays[id][2] = false
  117.     end)
  118. end
  119.  
  120. function drawEditButton(id)
  121.  
  122.     width=Displays[id][6]
  123.     x=Displays[id][4]
  124.     y=Displays[id][5]
  125.     if Displays[id][9] then col=Color.Selected
  126.     else col=Color.Normal end
  127.    
  128.     drawButton(x+width-25,y,12,10,"e",col,15,function()
  129.         Displays[id][9]=not Displays[id][9]
  130.     end)
  131. end
  132.  
  133. function drawButton(posx,posy,width,height,label,color,frequency,clickedfunction)
  134.     if Mouse.X>posx and Mouse.X<posx+width and Mouse.Y>posy and Mouse.Y<posy+height then
  135.             if Mouse.clicked and Mouse.clickedFrames%frequency==1 then  
  136.                 clickedfunction()
  137.             end
  138.         gui.drawBox(posx,posy,posx+width,posy+height,color-0x00303030,color+0x30303030)
  139.     else
  140.         gui.drawBox(posx,posy,posx+width,posy+height,color-0x00303030,color)
  141.     end
  142.     text(posx+1,posy-2,label,0xFFFFFFFF)
  143. end
  144.  
  145. function drawMenuButton(posx,posy,width,height,label,z,drawindicator,indicator,col,clickedfunction)
  146.     if Mouse.X>posx and Mouse.X<posx+width and Mouse.Y>posy and Mouse.Y<posy+height then
  147.         if Mouse.clicked and Mouse.clickedFrames==z then
  148.             menuscreen=0
  149.             clickedfunction()
  150.         end
  151.         boxSelected(posx,posy,posx+width,posy+height)
  152.     else
  153.         boxNormal(posx,posy,posx+width,posy+height)
  154.     end
  155.     text(posx+2,posy-1,label,col)
  156.    
  157.     if drawindicator then
  158.         if indicator then text(posx+width-10,posy,"o",0xFF80FF80)
  159.         else text(posx+width-10,posy-1,"x",0xFFFF9090) end
  160.     end
  161. end
  162.  
  163. local getChangeAmount = function(valueChangeAmount)
  164.     if Mouse.clickedFrames>540 then
  165.         valueChangeAmount=valueChangeAmount*1000000
  166.     elseif Mouse.clickedFrames>450 then
  167.         valueChangeAmount=valueChangeAmount*100000
  168.     elseif Mouse.clickedFrames>360 then
  169.         valueChangeAmount=valueChangeAmount*10000
  170.     elseif Mouse.clickedFrames>270 then
  171.         valueChangeAmount=valueChangeAmount*1000
  172.     elseif Mouse.clickedFrames>180 then
  173.         valueChangeAmount=valueChangeAmount*100
  174.     elseif Mouse.clickedFrames>90 then
  175.         valueChangeAmount=valueChangeAmount*10
  176.     end
  177.     return valueChangeAmount
  178. end
  179.  
  180. local addressTableSetValue = function(address, addressSize, addressEndian, newvalue)
  181.  
  182.     if addressSize==1 then
  183.         memory.write_u8(address,newvalue)
  184.     elseif addressSize==2 then
  185.         if addressEndian then
  186.             memory.write_u16_le(address,newvalue)
  187.         else
  188.             memory.write_u16_be(address,newvalue)
  189.         end            
  190.     elseif addressSize==3 then
  191.         if addressEndian then
  192.             memory.write_u24_le(address,newvalue)
  193.         else
  194.             memory.write_u24_be(address,newvalue)
  195.         end
  196.     elseif addressSize==4 then
  197.         if addressEndian then
  198.             memory.write_u32_le(address,newvalue)
  199.         else
  200.             memory.write_u32_be(address,newvalue)
  201.         end
  202.     end
  203.    
  204. end
  205.  
  206. local addressTableGetValue = function(address, addressSize, addressEndian, addressSigned)
  207.  
  208.     if addressSigned then
  209.         if addressSize==1 then
  210.             value=memory.read_s8(address)
  211.         elseif addressSize==2 then
  212.             if addressEndian then
  213.                 value=memory.read_s16_le(address)
  214.             else
  215.                 value=memory.read_s16_be(address)
  216.             end            
  217.         elseif addressSize==3 then
  218.             if addressEndian then
  219.                 value=memory.read_s24_le(address)
  220.             else
  221.                 value=memory.read_s24_be(address)
  222.             end
  223.         elseif addressSize==4 then
  224.             if addressEndian then
  225.                 value=memory.read_s32_le(address)
  226.             else
  227.                 value=memory.read_s32_be(address)
  228.             end
  229.         end
  230.     else
  231.         if addressSize==1 then
  232.             value=memory.read_u8(address)
  233.         elseif addressSize==2 then
  234.             if addressEndian then
  235.                 value=memory.read_u16_le(address)
  236.             else
  237.                 value=memory.read_u16_be(address)
  238.             end            
  239.         elseif addressSize==3 then
  240.             if addressEndian then
  241.                 value=memory.read_u24_le(address)
  242.             else
  243.                 value=memory.read_u24_be(address)
  244.             end
  245.         elseif addressSize==4 then
  246.             if addressEndian then
  247.                 value=memory.read_u32_le(address)
  248.             else
  249.                 value=memory.read_u32_be(address)
  250.             end
  251.         end
  252.     end
  253.    
  254.     return value   
  255. end
  256.  
  257. local DisplayAddressTable = function(display_id, has_arrows,  has_buttons,  display_description,  description_offset,  arrows_offset,  buttons_offset)
  258.    
  259.     inputTable=Displays[display_id][10]
  260.     xpos=Displays[display_id][4]
  261.     ypos=Displays[display_id][5]
  262.     memorydomainBefore=memory.getcurrentmemorydomain()
  263.     tableSize=#inputTable
  264.     table_start=1
  265.     table_end=Displays[display_id][11]
  266.     table_iterations = 1
  267.     offset=Displays[display_id][8]
  268.     list_height = 8*(table_end - table_start)+14
  269.  
  270.     --display arrows:
  271.     if has_arrows then
  272.         --bottom arrow button
  273.         if (tableSize - offset) > table_end then
  274.             if Mouse.X>xpos+arrows_offset and Mouse.X<xpos+arrows_offset+12 and Mouse.Y>ypos+list_height and Mouse.Y<ypos+list_height+6 then
  275.                 if Mouse.clicked then
  276.                     Displays[display_id][8]=offset+1
  277.                 end
  278.                 arrowDown(xpos+arrows_offset+2,ypos+list_height,0xFFFFFFFF)
  279.             else
  280.                 arrowDown(xpos+arrows_offset+2,ypos+list_height,0xA0FFFFFF)
  281.             end
  282.         else
  283.             if offset < 0 then
  284.                 Displays[display_id][8]=0
  285.             end
  286.         end
  287.            
  288.         --top arrow button
  289.         if offset > 0 then
  290.             if Mouse.X>xpos+arrows_offset and Mouse.X<xpos+arrows_offset+12 and Mouse.Y>ypos+list_height-10 and Mouse.Y<ypos+list_height-4 then
  291.                 if Mouse.clicked then
  292.                     Displays[display_id][8]=offset-1
  293.                 end
  294.                 arrowUp(xpos+arrows_offset+2,ypos+list_height-6,0xFFFFFFFF)
  295.             else
  296.                 arrowUp(xpos+arrows_offset+2,ypos+list_height-6,0xA0FFFFFF)
  297.             end
  298.         end
  299.     end
  300.     -- applying display offset
  301.     table_start = table_start + offset
  302.     table_end   = table_end + offset
  303.  
  304.     -- going through the table
  305.     for i=table_start,table_end do --show a part of the list
  306.  
  307.         contenttype=inputTable[i][1]
  308.         description=inputTable[i][2]
  309.         textColor=inputTable[i][3]
  310.         memorydomain=inputTable[i][4]
  311.        
  312.         if contenttype==0 then  -- TITLE
  313.                
  314.             gui.drawBox(5+xpos,4+ypos+table_iterations*8,arrows_offset+xpos,12+ypos+table_iterations*8,0x00000000,0xFF505050)
  315.    
  316.         elseif contenttype==7 then -- BINARY
  317.            
  318.             memory.usememorydomain(memorydomain)
  319.             address=inputTable[i][5]
  320.             currentbit=inputTable[i][6]
  321.             value=memory.read_u8(address)
  322.  
  323.             value=bit.check(value, currentbit)
  324.            
  325.             if value then
  326.                 value="Yes"
  327.                 valueColor=0xFF80FF80
  328.             else
  329.                 value="No"
  330.                 valueColor=0xFFFF9090
  331.             end
  332.            
  333.             text(5+xpos,3+ypos+table_iterations*8,value,valueColor)
  334.        
  335.         else    -- OTHER TYPES 
  336.             memory.usememorydomain(memorydomain)
  337.             address=inputTable[i][5]
  338.             addressSize=inputTable[i][6]
  339.             addressEndian=inputTable[i][7] -- true:little endian, false:big endian
  340.             addressSigned=inputTable[i][8]
  341.             valueChangeAmount=1                  
  342.             value = addressTableGetValue(address,addressSize,addressEndian, addressSigned)
  343.             text(5+xpos,3+ypos+table_iterations*8,value, textColor)
  344.         end
  345.        
  346.         if display_description then
  347.             text(xpos+description_offset,3+ypos+table_iterations*8,description, textColor)
  348.         end
  349.  
  350.         if has_buttons then
  351.            
  352.             if contenttype==7 then -- binary
  353.            
  354.                 drawButton(xpos+buttons_offset,4+ypos+table_iterations*8,21,8,"Set",Color.Normal,8,function()
  355.    
  356.                     currentvalue=memory.read_u8(address)
  357.  
  358.                     if bit.check(currentvalue,currentbit) then
  359.                         memory.write_u8(address, bit.clear(currentvalue,currentbit))
  360.                     else
  361.                         memory.write_u8(address, bit.set(currentvalue,currentbit)) 
  362.                     end
  363.                
  364.                 end)
  365.  
  366.             elseif contenttype~=0 then -- normal values
  367.            
  368.                 drawButton(xpos+buttons_offset,4+ypos+table_iterations*8,9,8,"-",Color.Normal,5,function()
  369.                     valueChangeAmount = getChangeAmount(valueChangeAmount) -- increases when holding mouse key 
  370.                     newvalue = value - valueChangeAmount
  371.                     addressTableSetValue(address,addressSize,addressEndian,newvalue)                   
  372.                 end)
  373.  
  374.                 drawButton(12+xpos+buttons_offset,4+ypos+table_iterations*8,9,8,"+",Color.Normal,5,function()
  375.                     valueChangeAmount = getChangeAmount(valueChangeAmount) -- increases when holding mouse key         
  376.                     newvalue = value + valueChangeAmount
  377.                     addressTableSetValue(address,addressSize,addressEndian,newvalue)               
  378.                 end)
  379.             end        
  380.         end
  381.         table_iterations=table_iterations+1
  382.     end
  383.     memory.usememorydomain(memorydomainBefore)
  384. end
  385.    
  386. local drawDisplay = function(id)
  387.     if Displays[id][4]~=nil then
  388.         drawDisplayBox(id, 0xFF202020,0xA0000000)
  389.         drawCloseButton(id)
  390.         drawEditButton(id)
  391.         if Displays[id][9] then
  392.             DisplayAddressTable(id,true,true,true,66,158,42)   
  393.         else
  394.             DisplayAddressTable(id,true,false,true,46,158,0)
  395.         end
  396.     end
  397. end
  398.  
  399. local Menu = {
  400.     [1] =       {   [0] = "Script",
  401.                     [1] = 1,
  402.                 }
  403. }
  404.  
  405. local drawMenu = function()
  406.  
  407.     verticalOffset=0
  408.  
  409.     for a=1,#Menu,1 do
  410.         drawMenuButton(-38+a*45,18,40,11,Menu[a][0],2,false,true,0xFFFFFFFF,function()
  411.             menuscreen=a
  412.         end)   
  413.        
  414.         if menuscreen==a then
  415.            
  416.             for b=1,#Menu[a],1 do
  417.            
  418.                 id              = Menu[a][b]
  419.                 title           = Displays[id][1]
  420.                 indicator       = Displays[id][2]
  421.                 col             = Displays[id][3]
  422.                
  423.                 drawMenuButton(7,32+verticalOffset*10,72,10,title,1,true,indicator,col,function()
  424.                     Displays[id][2] = not indicator
  425.                 end)           
  426.                
  427.                 verticalOffset=verticalOffset+1
  428.             end        
  429.         end
  430.     end
  431.    
  432.     if Mouse.clicked and Mouse.clickedFrames==1 then
  433.            menuscreen = 0
  434.     end
  435. end
  436.  
  437. local PaddingRefresh = function()
  438.     client.SetGameExtraPadding(Padding.Left,Padding.Top,Padding.Right,Padding.Bottom)
  439. end
  440.  
  441. local PaddingOff = function()
  442.     client.SetGameExtraPadding(0,0,0,0)
  443. end
  444.  
  445. event.onexit(function()
  446.     PaddingOff()
  447. end)
  448.  
  449. menuscreen=0
  450. frames=0
  451. console.clear()
  452. for i=1,#ScriptInfo,1 do
  453.     print(ScriptInfo[i])
  454. end
  455.  
  456. PaddingRefresh()
  457.  
  458. memory.usememorydomain("RAM")
  459.  
  460. local onloadstate = function()
  461.     PaddingRefresh()
  462.     lagcount = 0
  463. end
  464.  
  465. event.onloadstate(function()
  466.     onloadstate()
  467. end)
  468.  
  469. tastudio.onbranchload(function()
  470.     onloadstate()
  471. end)
  472.  
  473. local lagcount = 0
  474. local current_frame = emu.framecount()
  475.  
  476. while true do
  477.  
  478.     Mouse.X = input.getmouse().X + Padding.Left
  479.     Mouse.Y = input.getmouse().Y + Padding.Top
  480.     Mouse.clicked = input.getmouse().Left
  481.        
  482.     -- all displays
  483.     for i=1,#Displays do
  484.         if Displays[i] ~= nil then
  485.             if Displays[i][2] then
  486.                 drawDisplay(i)
  487.             end
  488.         end
  489.     end
  490.    
  491.     drawMenu() -- menu
  492.    
  493.     -- draw some things by default
  494.     memory.usememorydomain("RAM")
  495.     boxNormal(148,9,258,58, "black")
  496.    
  497.     lag = memory.read_u8(0x01a5)
  498.     xspeed = memory.read_u8(0x05B9)+memory.read_s8(0x05Ba)*256
  499.     yspeed = memory.read_u8(0x05Bd)+memory.read_s8(0x05Be)*256
  500.     xpos = memory.read_u8(0x0083)+memory.read_s8(0x0095)*256+memory.read_u8(0x0071)/256
  501.     ypos = memory.read_u8(0x00b9)+memory.read_s8(0x00cb)*256+memory.read_u8(0x00a7)/256
  502.      
  503.     text(150,9,"        pos           speed\n(X):\n(Y):\nlag:", 0xFF00FF00, 0xFF000000)
  504.     text(172,21, math.floor(xpos*1000)/1000 .. "\n" .. math.floor(ypos*1000)/1000, 0xFFFFFFFF, 0xFF000000)
  505.     text(224,21, xspeed .. "\n" .. yspeed,0xFFFFFFFF, 0xFF000000)
  506.    
  507.    
  508.     if lag == 1 and emu.framecount() ~= current_frame then
  509.         lagcount = lagcount + 1
  510.         text(200, 44, "*********" , 0xFFFF0000, 0xFF000000)
  511.         current_frame = emu.framecount()
  512.     end
  513.     text(172, 44, lagcount , 0xFFFF0000, 0xFF000000)
  514.     -----------
  515.  
  516.     Mouse.XBefore=Mouse.X
  517.     Mouse.YBefore=Mouse.Y
  518.     if Mouse.clicked then Mouse.clickedFrames = Mouse.clickedFrames + 1
  519.     else Mouse.clickedFrames = 0 end
  520.    
  521.    if client.ispaused() then
  522.         --gui.DrawFinish()
  523.         emu.yield()
  524.     else
  525.         emu.frameadvance()
  526.     end
  527.    
  528.     frames=frames+1
  529. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement