Advertisement
ImmoralKoala

CC_Crate_3DMovement

Jan 28th, 2022 (edited)
1,219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.28 KB | None | 0 0
  1. -- Blocks Per Second at 1 RPM
  2. baseBlocksPerSecond = 0.0380
  3.  
  4. -- Current RPM
  5. rpm = 0
  6.  
  7. -- Current Axis Positions
  8. currentX = 0
  9. currentY = 0
  10.  
  11. minerExtended = false
  12.  
  13. --Possible Axes
  14. axis = {"x", "y"}
  15.  
  16. -- Linear Interpolation
  17. function lerp(a,b,t) return a * (1-t) + b * t end
  18.  
  19. function resetRedstoneOutputs()
  20.     redstone.setOutput("back", false)
  21.     redstone.setOutput("left", false)
  22.     redstone.setOutput("right", false)
  23.     redstone.setOutput("bottom", false)
  24. end
  25.  
  26. -- Move a Specific Axis by an Amount
  27. function moveAxis (selectedAxis, blocks, speed)
  28.     -- Declare Local Variables
  29.     local currentPosition = 0
  30.     local inverted = false
  31.  
  32.     -- Output Redstone Signal based on Axis
  33.     if selectedAxis == axis[1] then
  34.         redstone.setOutput("left", false)
  35.         currentPosition = currentX
  36.     elseif selectedAxis == axis[2] then
  37.         redstone.setOutput("left", true)
  38.         currentPosition = currentY
  39.     else
  40.         error("Attempted to Move by Non-Existent Axis")
  41.     end
  42.  
  43.     -- Should Travel be Inverted?
  44.     if blocks < 0 then
  45.         redstone.setOutput("right", true)
  46.         inverted = true
  47.     else
  48.         redstone.setOutput("right", false)
  49.         inverted = false
  50.     end
  51.  
  52.     -- Calculate Desired Position
  53.     local desiredPosition = currentPosition + blocks
  54.  
  55.     -- Calculate Travel Time
  56.     local travelTime = math.abs(blocks / speed)
  57.  
  58.     -- Start Movement
  59.     redstone.setOutput("back", true)
  60.  
  61.     -- Wait x Seconds, Finishing on arrival.
  62.     os.sleep(travelTime)
  63.  
  64.     currentPosition = desiredPosition
  65.  
  66.     -- Stopping Movement and Resetting to Default Redstone Values
  67.     resetRedstoneOutputs()
  68.    
  69.     -- Returning New Position
  70.     return currentPosition
  71.  
  72. end
  73.  
  74. function resetZ()
  75.     resetRedstoneOutputs()
  76.  
  77.     redstone.setOutput("left", true)
  78.     redstone.setOutput("bottom", true)
  79.     redstone.setOutput("back", true)
  80.     os.sleep(0.5)
  81.     while not redstone.getInput("top") do
  82.         os.sleep(0.5)
  83.     end
  84.  
  85.     minerExtended = false
  86.  
  87.     resetRedstoneOutputs()
  88. end
  89.  
  90. function mine(speed)
  91.     redstone.setOutput("left", true)
  92.     redstone.setOutput("bottom", true)
  93.     redstone.setOutput("right", true)
  94.     redstone.setOutput("back", true)
  95.  
  96.     os.sleep(0.5)
  97.     while not redstone.getInput("top") do
  98.         os.sleep(speed)
  99.     end
  100.  
  101.     minerExtended = true
  102.  
  103.     resetZ()
  104. end
  105.  
  106. -- Move to Given Position
  107. function moveToPosition(x, y)
  108.     resetZ()
  109.     -- Calculate Difference in Positions
  110.     local moveX = x - currentX
  111.     local moveY = y - currentY
  112.  
  113.     -- Calculate Speed
  114.     local speed = baseBlocksPerSecond * rpm
  115.  
  116.     -- Move To Desired Position and Update Current Position
  117.     if (moveX ~= 0) then currentX = moveAxis(axis[1], moveX, speed) end
  118.     if (moveY ~= 0) then currentY = moveAxis(axis[2], moveY, speed) end
  119. end
  120.  
  121. -- Initialisation
  122. function init()
  123.     resetRedstoneOutputs()
  124.     term.clear()
  125.     io.write("Current RPM: ")
  126.     rpm = tonumber(read())
  127. end
  128.  
  129. init()
  130.  
  131. -- Move To Pos Interaction
  132. while true do
  133.     term.clear()
  134.     io.write("X: ")
  135.     xPos = tonumber(read())
  136.     io.write("Y: ")
  137.     yPos = tonumber(read())
  138.     print("Moving to Position...")
  139.     moveToPosition(xPos, yPos)
  140.  
  141.     io.write("Start Mining? [Y/N]: ")
  142.     if read() == "y" then
  143.         mine()
  144.     end
  145. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement