sosochka

domainexpansion.lua

Jan 9th, 2025 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.03 KB | None | 0 0
  1. DESIGNATED_BLOCK = "minecraft:stone"
  2. DESIGNATED_FUEL = "minecraft:coal"
  3. INVENTORY_SIZE = 16
  4.  
  5. local function log(message)
  6.     print(">-- "..message)
  7. end
  8.  
  9. local function get_slot_with_item_in_inventory(item)
  10.     for i = 1, INVENTORY_SIZE, 1 do
  11.         if turtle.getItemDetail(i) then
  12.             if turtle.getItemDetail().name == item then
  13.                 return i
  14.             end
  15.         end
  16.     end
  17.     log("Couldn't find item with name: "..item)
  18.     return nil
  19. end
  20.  
  21. local function wait_for_item_in_inventory_and_get_slot(item)
  22.     while true do
  23.         for i = 1, INVENTORY_SIZE, 1 do
  24.             if turtle.getItemDetail(i) then
  25.                 if turtle.getItemDetail().name == item then
  26.                     return i
  27.                 end
  28.             end
  29.         end
  30.         os.sleep(0.5)
  31.         log("Cannot find item with name: "..item)
  32.     end
  33. end
  34.  
  35. local function placeBlock()
  36.     if turtle.getItemDetail() then
  37.         if turtle.getItemDetail().name ~= DESIGNATED_BLOCK then
  38.             turtle.select(wait_for_item_in_inventory_and_get_slot(DESIGNATED_BLOCK))
  39.         end
  40.     else
  41.         turtle.select(wait_for_item_in_inventory_and_get_slot(DESIGNATED_BLOCK))
  42.     end
  43.  
  44.     if not turtle.compareDown() then
  45.         turtle.digDown()
  46.         turtle.placeDown()
  47.     end
  48. end
  49.  
  50. local function proceedForward()
  51.     if turtle.detect() then
  52.         turtle.dig()
  53.     end
  54.     turtle.forward()
  55. end
  56.  
  57. local function proceedLeft()
  58.     placeBlock()
  59.     turtle.turnLeft()
  60.     if turtle.detect() then
  61.         turtle.dig()
  62.     end
  63.     turtle.forward()
  64.     turtle.turnLeft()
  65. end
  66.  
  67. local function proceedRight()
  68.     placeBlock()
  69.     turtle.turnRight()
  70.     if turtle.detect() then
  71.         turtle.dig()
  72.     end
  73.     turtle.forward()
  74.     turtle.turnRight()
  75. end
  76.  
  77. local function refuel(wait)
  78.     repeat
  79.         for i = 1, INVENTORY_SIZE, 1 do
  80.             turtle.select(i)
  81.             if turtle.refuel() then
  82.                 log("Succesfully refueled")
  83.                 return
  84.             end
  85.         end
  86.         if wait then
  87.             log("Could find fuel, waiting for fuel in inventory")
  88.             os.sleep(0.5)
  89.         else
  90.             log("Could find fuel, proceeding...")
  91.         end
  92.     until not wait
  93. end
  94.  
  95. --Main
  96.  
  97. local function main()
  98.     term.write("Width: ")
  99.     local target_width = io.read()
  100.  
  101.     term.write("Height: ")
  102.     local target_height = io.read()
  103.  
  104.     print(target_width, target_height)
  105.  
  106.     for width = 1, target_width, 1 do
  107.         for height = 1, target_height - 1, 1 do
  108.             if turtle.getFuelLevel() < 100 then
  109.                 log("Low on fuel")
  110.                 refuel(false)
  111.             elseif turtle.getFuelLevel() < 10 then
  112.                 log("Fuel level critical, waiting for fuel")
  113.                 refuel(true)
  114.             end
  115.             placeBlock()
  116.             proceedForward()
  117.             log(width.." "..height)
  118.         end
  119.         if width % 2 == 1 then
  120.             proceedRight()
  121.         else
  122.             proceedLeft()
  123.         end
  124.     end
  125. end
  126.  
  127.  
  128. main()
Advertisement
Add Comment
Please, Sign In to add comment