Advertisement
Blazephlozard

Banjo-Kazooie line Lua (The8bitbeast)

Jun 21st, 2020
1,173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.96 KB | None | 0 0
  1. key = 0
  2. aLimit = 0
  3. coProg = 1
  4. maxVel = 0
  5. simulating = false
  6. opt = 1
  7. prevAng = 50
  8. deadConstant = 0.1124
  9.  
  10. firstTime = false
  11.  
  12. origX = 0
  13. origZ = 0
  14.  
  15.  
  16. function getVelocity() -- Calculated VXZ
  17.     local vX = mainmemory.readfloat(0x37C4B8, true);
  18.     local vZ = mainmemory.readfloat(0x37C4C0, true);
  19.     return math.sqrt(vX*vX + vZ*vZ);
  20. end
  21.  
  22. function angleFix(angle)
  23.     if angle < -1*math.pi+deadConstant then
  24.         return 50
  25.    
  26.     elseif angle < (-1*math.pi)/2-deadConstant then
  27.         return 2*math.pi+angle-4*deadConstant
  28.    
  29.     elseif angle < (-1*math.pi)/2+deadConstant then
  30.         return 50
  31.    
  32.     elseif angle < 0-deadConstant then
  33.         return 2*math.pi+angle-6*deadConstant
  34.        
  35.     elseif angle < deadConstant then
  36.         return 50
  37.    
  38.     elseif angle < math.pi / 2 - deadConstant then
  39.         return angle - deadConstant
  40.        
  41.     elseif angle < math.pi / 2 + deadConstant then
  42.         return 50
  43.    
  44.     elseif angle < math.pi - deadConstant then
  45.         return angle - 2*deadConstant
  46.        
  47.     else
  48.         return 50
  49.     end
  50. end
  51.  
  52. function line(fr,alim,endfr)
  53.     co = {}
  54.     co[1] = {}
  55.     co[1].x = 0
  56.     co[1].y = 0
  57.     co[1].angle = 1
  58.     key = fr
  59.     endFrame = endfr
  60.     aLimit = alim
  61.     tempInputs = movie.getinput(key-4)
  62.     tempX = math.min(62,tempInputs["P1 X Axis"])
  63.     tempY = math.min(62,tempInputs["P1 Y Axis"])
  64.     prevAng = angleFix(math.atan2(tempY,tempX))
  65.     --print(prevAngle)
  66.    
  67.     for x=-62,62 do
  68.         for y = -62,62 do
  69.             if math.sqrt(x*x+y*y) >= 62 then
  70.                 endco = #co+1
  71.                 co[endco] = {}
  72.                 co[endco].x = x
  73.                 co[endco].y = y
  74.                 co[endco].angle = angleFix(math.atan2(y,x))
  75.             end
  76.         end
  77.     end
  78.     --for i = 1,#co do
  79.     --  print(co[i].x,co[i].y,co[i].angle)
  80.     --end
  81.     simulating = true
  82.     firstTime = true
  83.     client.unpause()
  84.     tastudio.setplayback(key-2)
  85.     tastudio.setmarker(key-2)
  86.     tastudio.setrecording(true)
  87.     simulate()
  88. end
  89.  
  90. function cancel()
  91.     tastudio.setrecording(false)
  92.     tastudio.removemarker(key-2)
  93.     simulating = false
  94.     client.pause()
  95.     tastudio.setrecording(false)
  96.    
  97.     key = 0
  98.     aLimit = 0
  99.     coProg = 1
  100.     maxVel = 0
  101.     opt = 1
  102.     prevAng = 50
  103. end
  104.  
  105. function simulate()
  106.     if simulating == true then
  107.         frame = emu.framecount()
  108.         if (firstTime == true) then
  109.             if frame < key-2 then
  110.                 tempInputs = movie.getinput(frame)
  111.                 joypad.setanalog({["P1 X Axis"] = tempInputs["P1 X Axis"]})
  112.                 joypad.setanalog({["P1 Y Axis"] = tempInputs["P1 Y Axis"]})
  113.                 if frame <= aLimit then
  114.                     joypad.set(({["P1 A"] = true}))
  115.                 else
  116.                     joypad.set(({["P1 A"] = false}))
  117.                 end
  118.             end
  119.             if frame == key-2 or frame == key-1 then
  120.                 tempInputs = movie.getinput(frame)
  121.                 joypad.setanalog({["P1 X Axis"] = tempInputs["P1 X Axis"]})
  122.                 joypad.setanalog({["P1 Y Axis"] = tempInputs["P1 Y Axis"]})
  123.                 if frame <= aLimit then
  124.                     joypad.set(({["P1 A"] = true}))
  125.                 else
  126.                     joypad.set(({["P1 A"] = false}))
  127.                 end
  128.                
  129.                 --For detecting if my input didn't move me
  130.                 origX = mainmemory.readfloat(0x37C5A0, true);
  131.                 origZ = mainmemory.readfloat(0x37C5A8, true);
  132.                
  133.             end
  134.             if frame == key then
  135.                 local newX = mainmemory.readfloat(0x37C5A0, true);
  136.                 local newZ = mainmemory.readfloat(0x37C5A8, true);
  137.                 if (origX == newX and origZ == newZ) then
  138.                     tastudio.removemarker(key-2)
  139.                     key = key+1
  140.                     tastudio.setmarker(key-2)
  141.                     coProg = 1
  142.                     maxVel = 0
  143.                     --prevAng = co[opt].angle
  144.                     opt = 1
  145.                     tastudio.setplayback(key-2)
  146.                 else
  147.                     firstTime = false
  148.                     tastudio.setplayback(key-2)
  149.                 end
  150.             end
  151.         elseif coProg <= #co then
  152.             if frame < key-2 then
  153.                 tempInputs = movie.getinput(frame)
  154.                 joypad.setanalog({["P1 X Axis"] = tempInputs["P1 X Axis"]})
  155.                 joypad.setanalog({["P1 Y Axis"] = tempInputs["P1 Y Axis"]})
  156.                 if frame <= aLimit then
  157.                     joypad.set(({["P1 A"] = true}))
  158.                 else
  159.                     joypad.set(({["P1 A"] = false}))
  160.                 end
  161.             end
  162.             if frame == key-2 or frame == key-1 then
  163.                 joypad.setanalog({["P1 X Axis"] = co[coProg].x})
  164.                 joypad.setanalog({["P1 Y Axis"] = co[coProg].y})
  165.                 if frame <= aLimit then
  166.                     joypad.set(({["P1 A"] = true}))
  167.                 else
  168.                     joypad.set(({["P1 A"] = false}))
  169.                 end
  170.             end
  171.             if frame == key then
  172.                 vel = getVelocity()
  173.                 if vel >= maxVel then
  174.                     maxVel = vel
  175.                     opt = coProg
  176.                 end
  177.                
  178.                 coProg = coProg+1
  179.                 if coProg <= #co then
  180.                     angle = tonumber(co[coProg].angle)
  181.                     while math.abs(angle-prevAng) > 0.1 and math.abs(math.abs(angle-prevAng)-(2*math.pi-7*deadConstant)) > 0.1 do
  182.                         coProg = coProg+1
  183.                         --print(frame,coProg, angle, prevAng, math.abs(angle-prevAng))
  184.                         if coProg > #co then
  185.                             break
  186.                         end
  187.                         angle = tonumber(co[coProg].angle)
  188.                     end
  189.                 end
  190.                 if coProg <= #co then
  191.                     joypad.setanalog({["P1 X Axis"] = co[coProg].x})
  192.                     joypad.setanalog({["P1 Y Axis"] = co[coProg].y})
  193.                 else
  194.                     joypad.setanalog({["P1 X Axis"] = co[opt].x})
  195.                     joypad.setanalog({["P1 Y Axis"] = co[opt].y})
  196.                 end
  197.                 if frame <= aLimit then
  198.                     joypad.set(({["P1 A"] = true}))
  199.                 else
  200.                     joypad.set(({["P1 A"] = false}))
  201.                 end
  202.                 tastudio.setplayback(key-2)
  203.                 movie.setrerecordcount(movie.getrerecordcount()+1)
  204.             end
  205.         else
  206.             if frame < key-2 then
  207.                 tempInputs = movie.getinput(frame-1)
  208.                 joypad.setanalog({["P1 X Axis"] = tempInputs["P1 X Axis"]})
  209.                 joypad.setanalog({["P1 Y Axis"] = tempInputs["P1 Y Axis"]})
  210.             end
  211.             if frame == key-2 or frame == key-1 then
  212.                 joypad.setanalog({["P1 X Axis"] = co[opt].x})
  213.                 joypad.setanalog({["P1 Y Axis"] = co[opt].y})
  214.                 if frame <= aLimit then
  215.                     joypad.set(({["P1 A"] = true}))
  216.                 else
  217.                     joypad.set(({["P1 A"] = false}))
  218.                 end
  219.             end
  220.             if frame == key then
  221.                 tastudio.removemarker(key-2)
  222.                 key = key+2
  223.                 tastudio.setmarker(key-2)
  224.                 firstTime = true
  225.                 coProg = 1
  226.                 maxVel = 0
  227.                 if frame >= endFrame then
  228.                     tastudio.removemarker(key-2)
  229.                     simulating = false
  230.                     client.pause()
  231.                     tastudio.setrecording(false)
  232.                 end
  233.            
  234.                 prevAng = co[opt].angle
  235.                 opt = 1
  236.                 tastudio.setplayback(key-2)
  237.                 tastudio.setrecording(false)
  238.             end
  239.         end
  240.     end
  241. end
  242.  
  243. event.onframestart(simulate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement