Advertisement
posicat

/cattech/moves.lua

Sep 20th, 2024 (edited)
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.96 KB | None | 0 0
  1. --File: /cattech/moves.lua
  2.  
  3. local lists = dofile("/cattech/lists.lua")
  4.  
  5. -- Positions at start of script.
  6. local moves = {isDigNotFail = true}
  7.  
  8.  
  9. -- f Forward, h Height, s Sideways
  10. moves.position = { direction = 0, f = 0, h = 0, s = 0 }
  11.  
  12. -- ------------------------------------------------------------------------------------------------------------------------
  13. -- ------------------------------------------------------------------------------------------------------------------------
  14. -- Simple movement commands
  15. -- ------------------------------------------------------------------------------------------------------------------------
  16. -- ------------------------------------------------------------------------------------------------------------------------
  17.  
  18.  
  19. function moves.digUp()
  20.     while (turtle.digUp()) do
  21.         sleep(0.5)
  22.     end
  23. end
  24.  
  25. function moves.digDown()
  26.     while (turtle.digDown()) do
  27.         sleep(0.5)
  28.     end
  29. end
  30.  
  31. function moves.digForward()
  32.     while (turtle.dig()) do
  33.         sleep(0.5)
  34.     end
  35. end
  36.  
  37. function moves.turnLeft()
  38.     if (turtle.turnLeft()) then
  39.         moves.position.direction=(moves.position.direction-1) % 4
  40.         return true
  41.     end
  42.     return false
  43. end
  44.  
  45. function moves.turnRight()
  46.     if (turtle.turnRight()) then
  47.         moves.position.direction=(moves.position.direction+1) % 4
  48.         return true
  49.     end
  50.     return false
  51. end
  52.  
  53. function moves.backward()
  54.     if (moves.isDigNotFail) then
  55.         moves.turnLeft()
  56.         moves.turnLeft()
  57.         moves.digForward()
  58.         local success = moves.forward()
  59.         if success then
  60.             moves._recordMoveFwd(-1)
  61.         end
  62.         moves.turnRight()
  63.         moves.turnRight()
  64.         return success
  65.     else
  66.         local success = turtle.back()
  67.         if success then
  68.             moves._recordMoveFwd(-1)
  69.         end    
  70.         return success
  71.     end
  72. end
  73.  
  74. function moves.forward()
  75.     if (moves.isDigNotFail) then
  76.         moves.digForward()
  77.     end
  78.     local success = turtle.forward()
  79.     if success then
  80.         moves._recordMoveFwd(1)
  81.     end    
  82.     return success
  83. end
  84.  
  85. function moves.up()
  86.     if (moves.isDigNotFail) then
  87.         moves.digUp()
  88.     end
  89.     local success = turtle.up()
  90.  
  91.     if success then
  92.         moves._recordMoveUD(1)
  93.     end    
  94.     return success
  95. end
  96.  
  97. function moves.down()
  98.     if (moves.isDigNotFail) then
  99.         moves.digDown()
  100.     end
  101.     local success = turtle.down()
  102.  
  103.     if success then
  104.         moves._recordMoveUD(-1)
  105.     end    
  106.     return success
  107. end
  108.  
  109. function moves.face(direction)
  110.     local goal=0
  111.     --if direction == "forward" then
  112.         -- goal=0
  113.     --end
  114.     if direction == "right" then
  115.         goal=1
  116.     end
  117.     if direction == "left" then
  118.         goal=2
  119.     end
  120.     if direction == "backward" then
  121.         goal=3
  122.     end
  123.  
  124.     -- moves.position = { direction = 0, f = 0, h = 0, s = 0 }
  125.  
  126.     local dir = math.abs(moves.position.direction - goal)
  127.  
  128.     while moves.position.direction ~= goal do
  129.         if (dir == -1) then
  130.             moves.turnRight()
  131.         else
  132.             moves.turnLeft()
  133.         end
  134.     end
  135. end
  136.  
  137. function moves.secureDown()
  138.     local success,blockData = turtle.inspectDown()
  139.  
  140.     if isLiquid(blockData) or (not success) then
  141.         local placeItem = selectTurtleItemByList(lists.secureBlocks)
  142.         if placeItem ~= nil then
  143.             turtle.placeDown(placeItem)
  144.             return true
  145.         end
  146.     end
  147.     return false
  148. end
  149.  
  150. function moves.secureUp()
  151.     local success,blockData = turtle.inspectUp()
  152.     if isLiquid(blockData) then
  153.         local placeItem = selectTurtleItemByList(lists.secureBlocks)
  154.         if placeItem ~= nil then
  155.             turtle.placeUp(placeItem)
  156.             return true
  157.         end
  158.     end
  159.     return false
  160. end
  161.  
  162. function moves.secureForward()
  163.     local success,blockData = turtle.inspect()
  164.     if isLiquid(blockData) then
  165.         local placeItem = selectTurtleItemByList(lists.secureBlocks)
  166.         if placeItem ~= nil then
  167.             turtle.place(placeItem)
  168.             return true
  169.         end
  170.     end
  171.     return false
  172. end
  173.  
  174. -- ------------------------------------------------------------------------------------------------------------------------
  175. -- ------------------------------------------------------------------------------------------------------------------------
  176. -- Internal routines
  177. -- ------------------------------------------------------------------------------------------------------------------------
  178. -- ------------------------------------------------------------------------------------------------------------------------
  179.  
  180. function moves._recordMoveFwd(delta)
  181.     if moves.direction== 0 then
  182.         moves.position.f = moves.position.f + delta
  183.     elseif moves.direction == 1 then
  184.         moves.position.s = moves.position.s + delta
  185.     elseif moves.direction == 2 then
  186.         moves.position.f = moves.position.f - delta
  187.     elseif moves.direction == 3 then
  188.         moves.position.s = moves.position.s - delta
  189.     end
  190. end
  191.  
  192. function moves._recordMoveUD(delta)
  193.     moves.position.h = moves.position.h + delta
  194. end
  195.  
  196. -- ------------------------------------------------------------------------------------------------------------------------
  197. -- ------------------------------------------------------------------------------------------------------------------------
  198. -- Complex movement functionality.
  199. -- ------------------------------------------------------------------------------------------------------------------------
  200. -- ------------------------------------------------------------------------------------------------------------------------
  201.  
  202. -- Starting from the current position, carve out the right and left side, and move up, repeating to maxHeight steps up, then return.
  203. function moves.digTunnelSlice(maxHeight)
  204.    
  205.     for i=1,maxHeight do
  206.         moves.turnRight()
  207.         moves.digForward()
  208.         moves.turnLeft()
  209.         moves.turnLeft()
  210.         moves.digForward()
  211.         moves.turnRight()
  212.         if i < maxHeight then
  213.             moves.up()
  214.         end
  215.     end
  216.  
  217.     -- Ends up on the new floor/stair
  218.     for i=1,maxHeight -1 do
  219.         moves.down()
  220.     end
  221. end
  222.  
  223. function moves.secureTunnelSlice(maxHeight)
  224.     local isUnsecure = scanForNeedToSecure()
  225.  
  226.     log("debug","chk isUnsecure :" .. tostring(isUnsecure))
  227.  
  228.     if (isUnsecure == 0) then
  229.         return(isUnsecure)
  230.     end
  231.  
  232.     moves.secureDown()
  233.     moves.turnRight()
  234.     moves.forward()
  235.     moves.secureDown()
  236.     moves.secureForward()
  237.  
  238.     for i=1,maxHeight-1 do
  239.         moves.up()
  240.         moves.secureForward()
  241.     end
  242.  
  243.     moves.turnLeft()
  244.     moves.turnLeft()
  245.     moves.secureUp()
  246.  
  247.     for i=1,2 do
  248.         moves.forward()
  249.         moves.secureUp()
  250.     end
  251.  
  252.     moves.secureForward()
  253.  
  254.     for i=1,maxHeight-1 do
  255.         moves.down()
  256.         moves.secureForward()
  257.     end
  258.  
  259.     moves.secureDown()
  260.  
  261.     moves.turnRight()
  262.     moves.turnRight()
  263.     moves.forward()
  264.     moves.secureDown()
  265.     moves.turnLeft()
  266.  
  267. end
  268.  
  269. function moves.placeLight(upBlocks)
  270.     local success = false
  271.     local placeItem = selectTurtleItemByList(lists.placableLights)
  272.  
  273.     if (placeItem ~= nil) then
  274.         for i = 1, upBlocks do
  275.             moves.up()
  276.         end
  277.         moves.turnRight()
  278.         moves.digForward()
  279.         local placeThis = {placeItem.name}
  280.         success = placeItemByList(placeThis)
  281.         moves.turnLeft()
  282.         for i = 1, upBlocks do
  283.                 moves.down()
  284.         end
  285.     end
  286.     return success
  287. end
  288.  
  289. -- ------------------------------------------------------------------------------------------------------------------------
  290. -- ------------------------------------------------------------------------------------------------------------------------
  291. return moves
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement