Advertisement
Guest User

SML2 TAS Lua Bizhawk 2

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