kophysty

Terrain Leveler

Feb 6th, 2025 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.14 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. local COBBLE_SLOT = 1        -- preferred inventory slot for cobblestone (or chosen material)
  9. local COBBLE_NAME = "minecraft:cobblestone"
  10.  
  11. -- Clearance above the build area: only the immediate block by default.
  12. local CLEARANCE = 1        
  13.  
  14. -- FILL_DEPTH determines how many blocks downward to fill when a hole is encountered.
  15. local FILL_DEPTH = 2         -- default fill depth; can be overridden via command-line argument
  16.  
  17. -- Function to ensure that the active slot has the required material.
  18. -- Automatically checks all inventory slots.
  19. function ensureMaterial(materialName, defaultSlot)
  20.   if turtle.getItemCount(defaultSlot) > 0 then
  21.     turtle.select(defaultSlot)
  22.     return true
  23.   end
  24.   for i = 1, 16 do
  25.     local detail = turtle.getItemDetail(i)
  26.     if detail and detail.name == materialName and detail.count > 0 then
  27.       turtle.select(i)
  28.       return true
  29.     end
  30.   end
  31.   error("Out of material: " .. materialName)
  32. end
  33.  
  34. -- Check if there is a "hole" (air) below the turtle.
  35. function isHoleBelow()
  36.   if not turtle.detectDown() then
  37.     return true
  38.   end
  39.   local success, data = turtle.inspectDown()
  40.   if not success then
  41.     return true
  42.   else
  43.     return data.name == "minecraft:air"
  44.   end
  45. end
  46.  
  47. -- New function: fillHole(fillDepth)
  48. -- Заполняет ямку по вертикали, начиная с нижнего уровня.
  49. function fillHole(fillDepth)
  50.   if fillDepth <= 1 then
  51.     if isHoleBelow() then
  52.       ensureMaterial(COBBLE_NAME, COBBLE_SLOT)
  53.       turtle.placeDown()
  54.       os.sleep(0.2)
  55.     end
  56.     return
  57.   end
  58.  
  59.   -- Спускаемся до нижнего уровня ямы (максимум fillDepth-1 раз)
  60.   local descent = 0
  61.   for i = 1, fillDepth - 1 do
  62.     if isHoleBelow() then
  63.       safeDown()
  64.       descent = descent + 1
  65.     else
  66.       break
  67.     end
  68.   end
  69.   -- На нижнем уровне: ставим блок, если там пусто
  70.   if isHoleBelow() then
  71.     ensureMaterial(COBBLE_NAME, COBBLE_SLOT)
  72.     turtle.placeDown()
  73.     os.sleep(0.2)
  74.   end
  75.   -- Поднимаемся обратно, заполняя ямку по ходу подъёма
  76.   for i = 1, descent do
  77.     safeUp()
  78.     if isHoleBelow() then
  79.       ensureMaterial(COBBLE_NAME, COBBLE_SLOT)
  80.       turtle.placeDown()
  81.       os.sleep(0.2)
  82.     end
  83.   end
  84. end
  85.  
  86. -- Safe forward movement with obstacle removal and mob detection.
  87. function safeForward()
  88.   local attempts = 0
  89.   while not turtle.forward() do
  90.     if turtle.detect() then
  91.       local success, data = turtle.inspect()
  92.       if success then
  93.         turtle.dig()
  94.       else
  95.         turtle.attack()
  96.       end
  97.     else
  98.       turtle.attack()
  99.     end
  100.     attempts = attempts + 1
  101.     if attempts >= 5 then
  102.       print("Persistent obstacle detected in front. Attempting to bypass...")
  103.       turtle.turnLeft()
  104.       safeForward()  -- try moving forward on a sidestep
  105.       turtle.turnRight()
  106.       attempts = 0
  107.     end
  108.     os.sleep(0.5)
  109.   end
  110. end
  111.  
  112. -- Safe upward movement with obstacle removal and mob detection.
  113. function safeUp()
  114.   local attempts = 0
  115.   while not turtle.up() do
  116.     if turtle.detectUp() then
  117.       local success, data = turtle.inspectUp()
  118.       if success then
  119.         turtle.digUp()
  120.       else
  121.         turtle.attackUp()
  122.       end
  123.     else
  124.       turtle.attackUp()
  125.     end
  126.     attempts = attempts + 1
  127.     if attempts >= 5 then
  128.       print("Persistent obstacle detected above. Attempting alternative maneuver...")
  129.       turtle.turnLeft()
  130.       safeForward()  -- move sideways a bit
  131.       turtle.turnRight()
  132.       attempts = 0
  133.     end
  134.     os.sleep(0.5)
  135.   end
  136. end
  137.  
  138. -- Safe downward movement with obstacle removal and basic mob check.
  139. function safeDown()
  140.   local attempts = 0
  141.   while not turtle.down() do
  142.     if turtle.detectDown() then
  143.       local success, data = turtle.inspectDown()
  144.       if success then
  145.         turtle.digDown()
  146.       else
  147.         turtle.attack()
  148.       end
  149.     else
  150.       turtle.attack()
  151.     end
  152.     attempts = attempts + 1
  153.     if attempts >= 5 then
  154.       print("Persistent obstacle detected below. Waiting before retrying...")
  155.       attempts = 0
  156.     end
  157.     os.sleep(0.5)
  158.   end
  159. end
  160.  
  161. -- Function to check vertical clearance above the turtle.
  162. function checkVerticalClearance(clearance)
  163.   if turtle.detectUp() then
  164.     return false
  165.   end
  166.   if clearance > 1 then
  167.     for i = 2, clearance do
  168.       safeUp()
  169.       if turtle.detectUp() then
  170.         for j = 1, i do safeDown() end
  171.         return false
  172.       end
  173.     end
  174.     for i = 2, clearance do safeDown() end
  175.   end
  176.   return true
  177. end
  178.  
  179. -- Function to level the ground in a snake pattern.
  180. -- При обнаружении ямы, вызывается функция fillHole() с заданной глубиной.
  181. function levelGround(width, length, clearance, fillDepth)
  182.   clearance = clearance or CLEARANCE
  183.   fillDepth = fillDepth or FILL_DEPTH
  184.   for row = 1, length do
  185.     for col = 1, width do
  186.       if isHoleBelow() then
  187.         fillHole(fillDepth)
  188.       end
  189.       if not checkVerticalClearance(clearance) then
  190.         turtle.digUp()
  191.         os.sleep(0.1)
  192.       end
  193.       if col < width then
  194.         safeForward()
  195.       end
  196.     end
  197.     if row < length then
  198.       if row % 2 == 1 then
  199.         turtle.turnRight()
  200.         safeForward()
  201.         turtle.turnRight()
  202.       else
  203.         turtle.turnLeft()
  204.         safeForward()
  205.         turtle.turnLeft()
  206.       end
  207.     end
  208.   end
  209.   repositionToStart(width, length)
  210. end
  211.  
  212. -- Function to reposition the turtle to the starting point after the snake pattern.
  213. function repositionToStart(width, length)
  214.   if length % 2 == 1 then
  215.     turtle.turnRight()
  216.     turtle.turnRight()  -- now facing west
  217.     for i = 1, (width - 1) do
  218.       safeForward()
  219.     end
  220.     turtle.turnLeft()   -- now facing south
  221.     for i = 1, (length - 1) do
  222.       safeForward()
  223.     end
  224.     turtle.turnLeft()   -- facing east at the starting position
  225.   else
  226.     turtle.turnLeft()   -- from west, left -> south
  227.     for i = 1, (length - 1) do
  228.       safeForward()
  229.     end
  230.     turtle.turnLeft()   -- now facing east
  231.   end
  232. end
  233.  
  234. -- Main function.
  235. -- Параметры командной строки:
  236. -- 1: leveling area width, 2: leveling area length, 3: vertical clearance, 4: fill depth
  237. function main(...)
  238.   local args = {...}
  239.   local width = tonumber(args[1]) or LEVEL_WIDTH
  240.   local length = tonumber(args[2]) or LEVEL_LENGTH
  241.   local clearance = tonumber(args[3]) or CLEARANCE
  242.   local fillDepth = tonumber(args[4]) or FILL_DEPTH
  243.  
  244.   print("Leveling area size: " .. width .. " x " .. length)
  245.   print("Vertical clearance (clearance): " .. clearance .. " block(s)")
  246.   print("Fill depth for holes: " .. fillDepth .. " block(s)")
  247.  
  248.   levelGround(width, length, clearance, fillDepth)
  249.  
  250.   print("Leveling complete. The area is ready for building.")
  251. end
  252.  
  253. -- Run the main module with command-line arguments.
  254. main(...)
  255.  
Add Comment
Please, Sign In to add comment