Advertisement
Grexxity

test1234

Jan 20th, 2025 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.31 KB | None | 0 0
  1. -- Specify the wall material (update this as needed)
  2. local WALL_MATERIAL = "railcraft:abyssal"
  3.  
  4. -- Function to refuel the turtle from its inventory
  5. local function refuelTurtle()
  6.     for slot = 1, 16 do
  7.         turtle.select(slot)
  8.         if turtle.refuel(0) then
  9.             print("Refueled! Current fuel level: " .. turtle.getFuelLevel())
  10.             return true
  11.         end
  12.     end
  13.     print("No valid fuel found!")
  14.     return false
  15. end
  16.  
  17. -- Initialize the turtle
  18. local function initializeTurtle()
  19.     if turtle.getFuelLevel() == 0 then
  20.         print("Out of fuel! Attempting to refuel...")
  21.         if not refuelTurtle() then
  22.             print("Failed to refuel. Add fuel to the turtle's inventory.")
  23.             return false
  24.         end
  25.     end
  26.     return true
  27. end
  28.  
  29. -- Verify the wall material
  30. local function isCorrectWallMaterial()
  31.     local success, data = turtle.inspect()
  32.     if success then
  33.         return data.name == WALL_MATERIAL
  34.     end
  35.     return false
  36. end
  37.  
  38. -- Check if the turtle is in a corner
  39. local function isCorner()
  40.     local frontWall = isCorrectWallMaterial()
  41.     turtle.turnLeft()
  42.     local leftWall = isCorrectWallMaterial()
  43.     turtle.turnRight() -- Reset orientation
  44.     return frontWall and leftWall
  45. end
  46.  
  47. -- Follow the wall until a corner is found
  48. local function findCorner()
  49.     print("Searching for a corner...")
  50.     while true do
  51.         if isCorner() then
  52.             print("Corner found!")
  53.             return true
  54.         end
  55.  
  56.         if isCorrectWallMaterial() then
  57.             -- Wall ahead, turn left to follow it
  58.             print("Wall detected ahead. Turning left to follow the wall.")
  59.             turtle.turnLeft()
  60.         end
  61.  
  62.         -- Attempt to move forward
  63.         if not turtle.forward() then
  64.             print("Blocked while following the wall. Trying to adjust...")
  65.             turtle.turnLeft()
  66.         else
  67.             print("Moved forward while following the wall.")
  68.         end
  69.     end
  70. end
  71.  
  72. -- Measure and traverse one side of the wall
  73. local function measureAndTurn()
  74.     local distance = 0
  75.     while not isCorrectWallMaterial() do
  76.         if not turtle.forward() then
  77.             print("Blocked while measuring side!")
  78.             return -1
  79.         end
  80.         distance = distance + 1
  81.     end
  82.  
  83.     print("Wall detected after " .. distance .. " blocks. Turning to the next wall.")
  84.     turtle.turnRight() -- Turn to the next wall
  85.     return distance
  86. end
  87.  
  88. -- Measure the full perimeter of the room
  89. local function measurePerimeter()
  90.     local distances = {}
  91.  
  92.     for i = 1, 4 do
  93.         print("Measuring wall " .. i .. "...")
  94.         local distance = measureAndTurn()
  95.         if distance <= 0 then
  96.             print("Failed to measure wall " .. i .. ". Aborting!")
  97.             return nil
  98.         end
  99.         table.insert(distances, distance)
  100.     end
  101.  
  102.     return distances[1], distances[2] -- Return the length and width
  103. end
  104.  
  105. -- Move to the starting position offset by 1 block from the corner
  106. local function moveToStartingPosition()
  107.     print("Offsetting by 1 block from the corner...")
  108.     turtle.forward()
  109.     turtle.turnLeft()
  110.     turtle.forward()
  111.     turtle.turnRight()
  112. end
  113.  
  114. -- Break the block below and place a lamp
  115. local function placeLamp()
  116.     turtle.digDown()
  117.     for slot = 1, 16 do
  118.         turtle.select(slot)
  119.         local item = turtle.getItemDetail()
  120.         if item and item.name == "projectred-illumination:lamp" then
  121.             turtle.placeDown()
  122.             return
  123.         end
  124.     end
  125.     print("No 'Inverted Purple Lamp' found in inventory!")
  126. end
  127.  
  128. -- Place lamps in a grid pattern
  129. local function placeGrid(length, width)
  130.     local rows = math.floor((width - 1) / 5)
  131.     local cols = math.floor((length - 1) / 5)
  132.  
  133.     for row = 0, rows do
  134.         for col = 0, cols do
  135.             -- Place a lamp at the current position
  136.             placeLamp()
  137.  
  138.             -- Move to the next column
  139.             if col < cols then
  140.                 for i = 1, 5 do
  141.                     if not turtle.forward() then
  142.                         print("Blocked! Can't move forward.")
  143.                         return
  144.                     end
  145.                 end
  146.             end
  147.         end
  148.  
  149.         -- Move to the next row
  150.         if row < rows then
  151.             if row % 2 == 0 then
  152.                 turtle.turnRight()
  153.                 for i = 1, 5 do turtle.forward() end
  154.                 turtle.turnRight()
  155.             else
  156.                 turtle.turnLeft()
  157.                 for i = 1, 5 do turtle.forward() end
  158.                 turtle.turnLeft()
  159.             end
  160.         end
  161.     end
  162. end
  163.  
  164. -- Main program
  165. local function main()
  166.     print("Starting lamp placement...")
  167.  
  168.     if not initializeTurtle() then
  169.         return
  170.     end
  171.  
  172.     if not findCorner() then
  173.         print("Failed to find a corner. Aborting!")
  174.         return
  175.     end
  176.  
  177.     print("Measuring the room perimeter...")
  178.     local length, width = measurePerimeter()
  179.     if not length or not width then
  180.         print("Failed to measure the room. Aborting!")
  181.         return
  182.     end
  183.  
  184.     print("Room dimensions detected: Length = " .. length .. ", Width = " .. width)
  185.  
  186.     print("Moving to starting position...")
  187.     moveToStartingPosition()
  188.  
  189.     print("Placing lamps in a grid pattern...")
  190.     placeGrid(length, width)
  191.  
  192.     print("Task complete!")
  193. end
  194.  
  195. main()
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement