Advertisement
hornedcommando

Minecraft Turtle Terraform

Apr 13th, 2024
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | Gaming | 0 0
  1. -- Desc: Attempts to flatten a square area defined by the user
  2. -- By: hornedcommando
  3.  
  4. print("y set to 0")
  5. y = 0
  6.  
  7. local function input()
  8.     while true do
  9.         write("How big me dig?\n")
  10.         size = tonumber(read())
  11.             break
  12.         end
  13.     end
  14.  
  15.  
  16. --TODO: it's hard coded to use dirt, should update to use a table of blocks
  17. local blocks = {
  18.     {name = "minecraft:dirt"},
  19.     {name = "minecraft:cobblestone"}
  20. }
  21.  
  22. -- Function to smartly refuel the turtle
  23. local function smartRefuel()
  24.     -- Check if fuel level is below 200
  25.     while turtle.getFuelLevel() < 200 do
  26.         for slot = 1, 16 do
  27.             turtle.select(slot) -- Select the slot
  28.             if turtle.refuel(0) then -- Check if the selected item is a fuel
  29.                 turtle.refuel(1) -- Refuel with the selected item
  30.                 break -- Stop searching for fuel after refueling
  31.             end
  32.         end
  33.     end
  34. end
  35.  
  36. local function searchInventory(name)
  37.     for slot = 1, 16 do
  38.         turtle.select(slot)
  39.         local slotDetail = turtle.getItemDetail()
  40.         if slotDetail and slotDetail.name:find(name) then
  41.             return slot -- Return the slot number if item found
  42.         end
  43.     end
  44.     return nil -- Return nil if item not found
  45. end
  46.  
  47. -- Function to make the turtle go up
  48. function gu()
  49.     print("Going up")
  50.     while not turtle.up() do
  51.         print("Digging up")
  52.         turtle.digUp()
  53.     end
  54.     y = y + 1
  55.     print("y is now " .. y)
  56. end
  57.  
  58. local function fill(name)
  59.     local found, slot = searchInventory(name)
  60.     if found then
  61.         turtle.placeDown()  -- Place the block
  62.         print("Placed block:", name)
  63.     else
  64.         print("Block not found:", name)
  65.     end
  66. end
  67.  
  68. -- Function to make the turtle go down
  69. function gd()
  70.     print("Going down")
  71.     while not turtle.down() do
  72.         print("Digging down")
  73.         turtle.digDown()
  74.         if not turtle.inspectDown() and y == 0 then
  75.             fill("minecraft:dirt")
  76.         end
  77.     end
  78.     y = y - 1
  79.     print("y is now " .. y)
  80. end
  81.  
  82. -- Function to make the turtle go forward
  83. function gf()
  84.     print("Checking if a block is in front of me")
  85.     if not turtle.inspectDown() then
  86.         fill("minecraft:dirt")
  87.     end
  88.     while turtle.inspect() or turtle.inspectUp() do
  89.         gu()
  90.         end
  91.     print("Moving forward")
  92.     turtle.forward()
  93.     while y > 0 do
  94.         gd()
  95.     end
  96.     if not turtle.inspect() then
  97.         fill("minecraft:dirt")
  98.     end
  99.     smartRefuel()
  100. end
  101.  
  102. -- Function to make the turtle go back along the defined area
  103. function lTurn()
  104.     turtle.turnLeft()    -- Turn left
  105.     gf()     -- Move forward
  106.     turtle.turnLeft()    -- Turn left
  107. end
  108.  
  109. -- Function to make the turtle go back along the defined area (but to the right)
  110. function rTurn()
  111.     turtle.turnRight()   -- Turn right
  112.     gf()     -- Move forward
  113.     turtle.turnRight()   -- Turn right
  114. end
  115.  
  116. -- Function to make the turtle travel the length of the defined area
  117. function fEnd(size)
  118.     for i = 1, size do
  119.         gf()  -- Move forward
  120.     end
  121. end
  122.  
  123. -- Combines movement functions to form a sweeping square pattern
  124. local function sweep(size)
  125.     for i = 1, size do
  126.         fEnd(size - 1)  -- Move to the end of the row
  127.         if i < size then
  128.             if i % 2 == 1 then
  129.                 rTurn()  -- Turn right at the end of the row
  130.             else
  131.                 lTurn()  -- Turn left at the end of the row
  132.             end
  133.         end
  134.     end
  135. end
  136.  
  137.  
  138. smartRefuel()
  139. input()
  140. sweep(size)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement