kophysty

Terrain Leveler

Feb 6th, 2025 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.01 KB | Gaming | 0 0
  1. -- Default settings.
  2. local HOUSE_WIDTH = 15       -- House width (excluding borders)
  3. local HOUSE_LENGTH = 20      -- House length (excluding borders)
  4. -- Leveling area: house dimensions + 1 block border on each side
  5. local LEVEL_WIDTH = HOUSE_WIDTH + 2    
  6. local LEVEL_LENGTH = HOUSE_LENGTH + 2  
  7.  
  8. -- Vertical clearance: only the immediate block by default.
  9. local CLEARANCE = 1        
  10.  
  11. -- FILL_DEPTH determines how many blocks downward to fill when a hole is encountered.
  12. local FILL_DEPTH = 2         -- Default fill depth; can be overridden via command-line argument
  13.  
  14. -- Global variable to store the desired material name.
  15. local desiredMaterial = nil
  16.  
  17. -- Function to get the desired material from the inventory.
  18. -- It first checks slot 1; if that is empty, it searches all slots.
  19. local function getDesiredMaterial()
  20.   local detail = turtle.getItemDetail(1)
  21.   if detail then
  22.     desiredMaterial = detail.name
  23.     return desiredMaterial
  24.   else
  25.     for i = 2, 16 do
  26.       local d = turtle.getItemDetail(i)
  27.       if d then
  28.         desiredMaterial = d.name
  29.         return desiredMaterial
  30.       end
  31.     end
  32.   end
  33.   error("No material found in inventory. Please add the desired material.")
  34. end
  35.  
  36. -- Function that searches the inventory for the desired material.
  37. -- It selects the first slot containing that material.
  38. function ensureMaterialFromSlot1()
  39.   local materialName = getDesiredMaterial()
  40.   for i = 1, 16 do
  41.     local detail = turtle.getItemDetail(i)
  42.     if detail and detail.name == materialName and detail.count > 0 then
  43.       turtle.select(i)
  44.       return true
  45.     end
  46.   end
  47.   error("Out of material: " .. materialName)
  48. end
  49.  
  50. -- Function to remove tall grass below the turtle.
  51. -- It checks the block below, and if it is "minecraft:grass" or "minecraft:tall_grass", it digs it.
  52. function removeTallGrassDown()
  53.   local success, block = turtle.inspectDown()
  54.   if success and (block.name == "minecraft:grass" or block.name == "minecraft:tall_grass") then
  55.     turtle.digDown()
  56.     os.sleep(0.2)
  57.   end
  58. end
  59.  
  60. -- Function to check if the block below should be replaced.
  61. -- Returns true if no block is detected or if the block below is air.
  62. function isHoleBelow()
  63.   if not turtle.detectDown() then
  64.     return true
  65.   end
  66.   local success, data = turtle.inspectDown()
  67.   if not success then
  68.     return true
  69.   else
  70.     if data.name == "minecraft:air" then
  71.       return true
  72.     else
  73.       return false
  74.     end
  75.   end
  76. end
  77.  
  78. -- Safe forward movement with obstacle removal and mob detection.
  79. function safeForward()
  80.   local attempts = 0
  81.   while not turtle.forward() do
  82.     if turtle.detect() then
  83.       local success, data = turtle.inspect()
  84.       if success then
  85.         turtle.dig()
  86.       else
  87.         turtle.attack()
  88.       end
  89.     else
  90.       turtle.attack()
  91.     end
  92.     attempts = attempts + 1
  93.     if attempts >= 5 then
  94.       print("Persistent obstacle detected in front. Attempting to bypass...")
  95.       turtle.turnLeft()
  96.       safeForward()  -- try sidestepping
  97.       turtle.turnRight()
  98.       attempts = 0
  99.     end
  100.     os.sleep(0.5)
  101.   end
  102. end
  103.  
  104. -- Safe upward movement with obstacle removal and mob detection.
  105. function safeUp()
  106.   local attempts = 0
  107.   while not turtle.up() do
  108.     if turtle.detectUp() then
  109.       local success, data = turtle.inspectUp()
  110.       if success then
  111.         turtle.digUp()
  112.       else
  113.         turtle.attackUp()
  114.       end
  115.     else
  116.       turtle.attackUp()
  117.     end
  118.     attempts = attempts + 1
  119.     if attempts >= 5 then
  120.       print("Persistent obstacle detected above. Attempting alternative maneuver...")
  121.       turtle.turnLeft()
  122.       safeForward()  -- move sideways
  123.       turtle.turnRight()
  124.       attempts = 0
  125.     end
  126.     os.sleep(0.5)
  127.   end
  128. end
  129.  
  130. -- Safe downward movement with obstacle removal and basic mob check.
  131. function safeDown()
  132.   local attempts = 0
  133.   while not turtle.down() do
  134.     if turtle.detectDown() then
  135.       local success, data = turtle.inspectDown()
  136.       if success then
  137.         turtle.digDown()
  138.       else
  139.         turtle.attack()
  140.       end
  141.     else
  142.       turtle.attack()
  143.     end
  144.     attempts = attempts + 1
  145.     if attempts >= 5 then
  146.       print("Persistent obstacle detected below. Waiting before retrying...")
  147.       attempts = 0
  148.     end
  149.     os.sleep(0.5)
  150.   end
  151. end
  152.  
  153. -- Function to check vertical clearance above the turtle.
  154. function checkVerticalClearance(clearance)
  155.   if turtle.detectUp() then
  156.     return false
  157.   end
  158.   if clearance > 1 then
  159.     for i = 2, clearance do
  160.       safeUp()
  161.       if turtle.detectUp() then
  162.         for j = 1, i do safeDown() end
  163.         return false
  164.       end
  165.     end
  166.     for i = 2, clearance do safeDown() end
  167.   end
  168.   return true
  169. end
  170.  
  171. -- Function fillHole(fillDepth)
  172. -- Fills a hole downward to the specified depth, starting from the lowest level.
  173. -- Before placing a block, it calls removeTallGrassDown() so that only individual tall grass is removed.
  174. function fillHole(fillDepth)
  175.   if fillDepth <= 1 then
  176.     if isHoleBelow() then
  177.       removeTallGrassDown()
  178.       ensureMaterialFromSlot1()
  179.       turtle.placeDown()
  180.       os.sleep(0.2)
  181.     end
  182.     return
  183.   end
  184.  
  185.   -- Descend to the lowest level (up to fillDepth-1 times)
  186.   local descent = 0
  187.   for i = 1, fillDepth - 1 do
  188.     if isHoleBelow() then
  189.       removeTallGrassDown()
  190.       safeDown()
  191.       descent = descent + 1
  192.     else
  193.       break
  194.     end
  195.   end
  196.  
  197.   -- At the lowest level, place a block after removing tall grass if present
  198.   if isHoleBelow() then
  199.     removeTallGrassDown()
  200.     ensureMaterialFromSlot1()
  201.     turtle.placeDown()
  202.     os.sleep(0.2)
  203.   end
  204.  
  205.   -- Ascend back, filling the hole on the way up
  206.   for i = 1, descent do
  207.     safeUp()
  208.     if isHoleBelow() then
  209.       removeTallGrassDown()
  210.       ensureMaterialFromSlot1()
  211.       turtle.placeDown()
  212.       os.sleep(0.2)
  213.     end
  214.   end
  215. end
  216.  
  217. -- Function levelGround()
  218. -- Traverses the leveling area in a snake pattern.
  219. -- When a hole is detected, fillHole(fillDepth) is called.
  220. function levelGround(width, length, clearance, fillDepth)
  221.   clearance = clearance or CLEARANCE
  222.   fillDepth = fillDepth or FILL_DEPTH
  223.   for row = 1, length do
  224.     for col = 1, width do
  225.       if isHoleBelow() then
  226.         fillHole(fillDepth)
  227.       end
  228.       if not checkVerticalClearance(clearance) then
  229.         turtle.digUp()
  230.         os.sleep(0.1)
  231.       end
  232.       if col < width then
  233.         safeForward()
  234.       end
  235.     end
  236.     if row < length then
  237.       if row % 2 == 1 then
  238.         turtle.turnRight()
  239.         safeForward()
  240.         turtle.turnRight()
  241.       else
  242.         turtle.turnLeft()
  243.         safeForward()
  244.         turtle.turnLeft()
  245.       end
  246.     end
  247.   end
  248.   repositionToStart(width, length)
  249. end
  250.  
  251. -- Function repositionToStart()
  252. -- Returns the turtle to the starting position after traversing the area.
  253. function repositionToStart(width, length)
  254.   if length % 2 == 1 then
  255.     turtle.turnRight()
  256.     turtle.turnRight()  -- now facing west
  257.     for i = 1, (width - 1) do
  258.       safeForward()
  259.     end
  260.     turtle.turnLeft()   -- now facing south
  261.     for i = 1, (length - 1) do
  262.       safeForward()
  263.     end
  264.     turtle.turnLeft()   -- returns to original orientation (east)
  265.   else
  266.     turtle.turnLeft()   -- turn from west to south
  267.     for i = 1, (length - 1) do
  268.       safeForward()
  269.     end
  270.     turtle.turnLeft()   -- now facing east
  271.   end
  272. end
  273.  
  274. -- Main function.
  275. -- Command-line parameters:
  276. -- 1: Leveling area width, 2: length, 3: vertical clearance, 4: fill depth.
  277. function main(...)
  278.   local args = {...}
  279.   local width = tonumber(args[1]) or LEVEL_WIDTH
  280.   local length = tonumber(args[2]) or LEVEL_LENGTH
  281.   local clearance = tonumber(args[3]) or CLEARANCE
  282.   local fillDepth = tonumber(args[4]) or FILL_DEPTH
  283.  
  284.   print("Leveling area size: " .. width .. " x " .. length)
  285.   print("Vertical clearance: " .. clearance .. " block(s)")
  286.   print("Fill depth: " .. fillDepth .. " block(s)")
  287.  
  288.   levelGround(width, length, clearance, fillDepth)
  289.  
  290.   print("Leveling complete. The area is ready for building.")
  291. end
  292.  
  293. -- Run main function with command-line parameters.
  294. main(...)
  295.  
Add Comment
Please, Sign In to add comment