Advertisement
kophysty

Flatten Terrain for the road (4x3)

Jan 31st, 2025 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.75 KB | Gaming | 0 0
  1. -- Safe forward movement: if blocked by an entity or obstacle,
  2. -- try to dig if it's a block, or wait if it's an entity.
  3. function safeForward()
  4.   while not turtle.forward() do
  5.     local success, data = turtle.inspect()
  6.     if success then
  7.       turtle.dig()
  8.     else
  9.       -- Likely an entity in the way; wait for it to clear.
  10.       os.sleep(0.5)
  11.     end
  12.   end
  13. end
  14.  
  15. -- Functions for lateral movement (keeping original direction)
  16. function moveLeft(n)
  17.   turtle.turnLeft()
  18.   for i = 1, n do
  19.     safeForward()
  20.   end
  21.   turtle.turnRight()
  22. end
  23.  
  24. function moveRight(n)
  25.   turtle.turnRight()
  26.   for i = 1, n do
  27.     safeForward()
  28.   end
  29.   turtle.turnLeft()
  30. end
  31.  
  32. -- Function to "flatten" a column:
  33. -- Assumes the turtle is at ground level.
  34. -- It first moves up 2 blocks (ensuring a 3-block high space: floor + 2 air blocks),
  35. -- then moves back down. If there is no block underfoot, it places one from the inventory.
  36. function flattenColumn()
  37.   -- Clear the vertical space (upper 2 blocks)
  38.   for i = 1, 2 do
  39.     if turtle.detectUp() then
  40.       turtle.digUp()
  41.     end
  42.     turtle.up()
  43.   end
  44.  
  45.   -- Move back down to ground level
  46.   turtle.down()
  47.   turtle.down()
  48.  
  49.   -- If no block is detected below, place a block from the inventory.
  50.   if not turtle.detectDown() then
  51.     for slot = 1, 16 do
  52.       turtle.select(slot)
  53.       if turtle.getItemCount() > 0 then
  54.         turtle.placeDown()
  55.         break
  56.       end
  57.     end
  58.   end
  59. end
  60.  
  61. -- Function to check for a deep drop (more than 5 blocks)
  62. -- The turtle temporarily moves forward, then descends counting missing blocks.
  63. -- Afterwards, it returns to the original position.
  64. function isDeepDropAhead()
  65.   if not turtle.forward() then
  66.     -- Movement blocked, so assume no drop.
  67.     return false
  68.   end
  69.  
  70.   local depth = 0
  71.   while depth <= 5 and not turtle.detectDown() do
  72.     depth = depth + 1
  73.     turtle.down()
  74.   end
  75.  
  76.   -- Return to the original level.
  77.   for i = 1, depth do
  78.     turtle.up()
  79.   end
  80.   turtle.back()
  81.  
  82.   return depth > 5
  83. end
  84.  
  85. -- Function for one step: flatten the corridor section and then move forward.
  86. -- The corridor is considered 4 blocks wide (current block plus 3 blocks to the right).
  87. function flattenStep()
  88.   if isDeepDropAhead() then
  89.     print("Deep drop ahead - stopping.")
  90.     return false
  91.   end
  92.  
  93.   local corridorWidth = 4
  94.   for offset = 0, corridorWidth - 1 do
  95.     if offset > 0 then
  96.       moveRight(offset)
  97.     end
  98.  
  99.     flattenColumn()
  100.  
  101.     if offset > 0 then
  102.       moveLeft(offset)
  103.     end
  104.   end
  105.  
  106.   safeForward()
  107.   return true
  108. end
  109.  
  110. -- Main function: while fuel is available, perform the flattening step by step.
  111. function flattenTerrain()
  112.   while turtle.getFuelLevel() > 0 do
  113.     if not flattenStep() then
  114.       break
  115.     end
  116.   end
  117. end
  118.  
  119. flattenTerrain()
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement