Advertisement
Eumeus14

MarioSwim

Apr 30th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.54 KB | None | 0 0
  1. -- Written By Eumeus14
  2.  
  3. -- Simple swimming script for smw TASing (speed values in this script apply to Mario Game by Draconif)
  4.  
  5. -- Things to fix:
  6. -- gui.drawtext does nothing
  7. -- Could set it up so that the console only outputs the swimming direction one time when it is changed.
  8.  
  9. -- The address of Mario's speed
  10. local speed = 0x00007B
  11. -- Mario's maximum left/right swimming speeds
  12. local maxSwimSpeedL = -20
  13. local maxSwimSpeedR = 20
  14. -- Mario's maximum left/right swimming speeds with an item
  15. local maxItemSwimSpeedL = -34
  16. local maxItemSwimSpeedR = 34
  17. -- The direction the script will make mario swim
  18. local swimDirection = 'Stopped'
  19. -- EmulatorHeight is just used for ui stuff
  20. local EmulatorHeight = client.bufferheight()
  21. -- Boolean to track if Mario is holding an item or not. defaults to false.
  22. local holdingItem = false
  23.  
  24.  
  25. -- Functions
  26.  
  27. -- Checks for a water current and adjusts max swimming values accordingly
  28. function CheckForWaterCurrent()
  29.     -- Set speed to 0 and wait for a current to push Mario
  30.     mainmemory.write_s8(speed, 0)
  31.     -- Set carried item to 0. Otherwise, the current cannot overpower Mario.
  32.     mainmemory.writebyte(0x00148F, 0)
  33.    
  34.     AdvanceFrames(2)
  35.     if(mainmemory.read_s8(speed) < 0) then
  36.         -- Current pushes all speed values 8 units left
  37.         maxSwimSpeedL = maxSwimSpeedL - 8
  38.         maxSwimSpeedR = maxSwimSpeedR - 8
  39.         maxItemSwimSpeedL = maxItemSwimSpeedL - 8
  40.         maxItemSwimSpeedR = maxItemSwimSpeedR - 8
  41.         console.writeline('Leftward current detected. Adjusting speed values...')
  42.     elseif(mainmemory.read_s8(speed) > 0) then
  43.         -- Current pushes all speed values 8 units right
  44.         maxSwimSpeedL = maxSwimSpeedL + 8
  45.         maxSwimSpeedR = maxSwimSpeedR + 8
  46.         maxItemSwimSpeedL = maxItemSwimSpeedL + 8
  47.         maxItemSwimSpeedR = maxItemSwimSpeedR + 8
  48.         console.writeline('Rightward current detected. Adjusting speed values...')
  49.     else
  50.         -- no adjustment needed
  51.         console.writeline('No current detected.')
  52.     end
  53. end
  54.  
  55.  
  56. -- Advance some number of frames
  57. function AdvanceFrames(numFrames)
  58.     client.unpause()
  59.     for i = 1, numFrames do
  60.         emu.frameadvance()
  61.     end
  62.     client.pause()
  63. end
  64.  
  65.  
  66. -- Determines which direction to swim
  67. function DetermineSwimmingDirection()
  68.     -- Get keyboard input for swimming direction
  69.     local Keypresses = input.get()
  70.    
  71.     -- Check the keyboard inputs and adjust swimming direction accordingly
  72.     if(Keypresses.A == true) then
  73.         swimDirection = 'Left'
  74.         console.writeline('Setting swim direction to left.')
  75.     elseif(Keypresses.D == true) then
  76.         swimDirection = 'Right'
  77.         console.writeline('Setting swim direction to right.')
  78.     elseif(Keypresses.S == true) then
  79.         swimDirection = 'Stopped'
  80.         console.writeline('Stopping swim.')
  81.     end
  82. end
  83.  
  84.  
  85. -- Checks if Mario is currently holding an item
  86. function CheckForHeldItem()
  87.     if(mainmemory.readbyte(0x00148F) == 1) then
  88.         holdingItem = true
  89.     else
  90.         holdingItem = false
  91.     end
  92. end
  93.  
  94.  
  95. -- Swims
  96. function DoSwimming()
  97.     CheckForHeldItem()
  98.     -- Not holding an item
  99.     if(holdingItem == false) then
  100.         -- Left
  101.         if(swimDirection == 'Left') then
  102.             if(mainmemory.read_s8(speed) ~= maxSwimSpeedL) then
  103.                 joypad.set({['Right']=false}, 1)
  104.                 joypad.set({['Left']=true}, 1)
  105.             else
  106.                 joypad.set({['Right']=true}, 1)
  107.                 joypad.set({['Left']=false}, 1)
  108.             end
  109.         -- Right
  110.         elseif(swimDirection == 'Right') then
  111.             if(mainmemory.read_s8(speed) ~= maxSwimSpeedR) then
  112.                 joypad.set({['Right']=true}, 1)
  113.                 joypad.set({['Left']=false}, 1)
  114.             else
  115.                 joypad.set({['Right']=false}, 1)
  116.                 joypad.set({['Left']=true}, 1)
  117.             end
  118.         end
  119.     -- Holding an item
  120.     else
  121.         -- Left
  122.         if(swimDirection == 'Left') then
  123.             if(mainmemory.read_s8(speed) ~= maxItemSwimSpeedL) then
  124.                 joypad.set({['Right']=false}, 1)
  125.                 joypad.set({['Left']=true}, 1)
  126.             else
  127.                 joypad.set({['Right']=true}, 1)
  128.                 joypad.set({['Left']=false}, 1)
  129.             end
  130.         -- Right   
  131.         elseif(swimDirection == 'Right') then
  132.             if(mainmemory.read_s8(speed) ~= maxItemSwimSpeedR) then
  133.                 joypad.set({['Right']=true}, 1)
  134.                 joypad.set({['Left']=false}, 1)
  135.             else
  136.                 joypad.set({['Right']=false}, 1)
  137.                 joypad.set({['Left']=true}, 1)
  138.             end
  139.         end
  140.     end
  141. end
  142.  
  143. -- main
  144. savestate.save('MarioSwim')
  145.  
  146. CheckForWaterCurrent()
  147.  
  148. savestate.load('MarioSwim')
  149.  
  150. while( true ) do
  151.     -- instructions for user
  152.     gui.drawText(1, (EmulatorHeight - 65), 'Press A on the keyboard to swim left.')
  153.     gui.drawText(1, (EmulatorHeight - 50), 'Press D on the keyboard to swim right.')
  154.     gui.drawText(1, (EmulatorHeight - 35), 'Press S on the keyboard to stop swimming.')
  155.    
  156.     DetermineSwimmingDirection()
  157.    
  158.     DoSwimming()
  159.    
  160.     emu.yield()
  161. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement