gur111

platform.lua

Jun 7th, 2025 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.78 KB | None | 0 0
  1. -- Global variable to store the name of the block to place
  2. local requiredBlockName = nil
  3.  
  4. -- Helper function to perform a turtle action and check for success, pausing if needed
  5. -- If the action fails, it will print an error, pause, and retry every second until it succeeds.
  6. -- Returns true once the action successfully completes.
  7. local function perform_action(action_func, error_message)
  8.     while not action_func() do
  9.         print("PAUSED: " .. error_message .. " Waiting for obstruction to clear...")
  10.         sleep(1) -- Wait for 1 second before retrying
  11.     end
  12.     print("Resumed: Obstruction cleared.")
  13.     return true
  14. end
  15.  
  16. -- Function to find and select the required block type in the inventory.
  17. -- It prioritizes the currently selected slot if it contains the required block.
  18. -- Otherwise, it searches all other slots.
  19. -- Returns true if the required block is found and selected, false otherwise.
  20. local function findAndSelectRequiredBlock()
  21.     local currentSlot = turtle.getSelectedSlot()
  22.     local currentItem = turtle.getItemDetail(currentSlot)
  23.  
  24.     -- Check if the current slot already has the required block and it's not empty
  25.     if currentItem and currentItem.name == requiredBlockName and currentItem.count > 0 then
  26.         return true
  27.     end
  28.  
  29.     -- Search all slots for the required block
  30.     for i = 1, 16 do
  31.         local item = turtle.getItemDetail(i)
  32.         if item and item.name == requiredBlockName and item.count > 0 then
  33.             turtle.select(i)
  34.             print("Selected slot " .. i .. " containing '" .. requiredBlockName .. "'.")
  35.             return true
  36.         end
  37.     end
  38.  
  39.     return false -- Required block not found in any slot
  40. end
  41.  
  42. -- 1. place: Places a block under the turtle.
  43. -- This function is robust:
  44. -- - It first ensures the required block type is available and selected, pausing if not.
  45. -- - Then, it attempts to place the block. If obstructed, it tries to clear the space
  46. --   (attack for entities, dig for blocks) and retries placement, pausing if necessary.
  47. -- Returns true once a block is successfully placed.
  48. local function place()
  49.     -- Step 1: Ensure we have the required block selected.
  50.     -- This loop will pause the program until the required block is found in the inventory.
  51.     while not findAndSelectRequiredBlock() do
  52.         print("PAUSED: Out of required block type '" .. requiredBlockName .. "'. Waiting for more blocks...")
  53.         sleep(1) -- Wait for 1 second before re-checking inventory
  54.     end
  55.     print("Resumed: Required blocks found.")
  56.  
  57.     -- Step 2: Attempt to place the block, clearing space if obstructed.
  58.     -- This loop will pause the program until the block can be placed.
  59.     while not turtle.placeDown() do
  60.         print("PAUSED: Failed to place block. Attempting to clear space below...")
  61.         -- Try attacking first (for entities)
  62.         if turtle.attackDown() then
  63.             print("Attacked entity below.")
  64.         else
  65.             -- If attacking failed or no entity, try digging (for blocks)
  66.             if turtle.digDown() then
  67.                 print("Dug block below.")
  68.             else
  69.                 print("Could not clear space below. Retrying...")
  70.             end
  71.         end
  72.         sleep(1) -- Wait for 1 second before retrying placement
  73.     end
  74.     print("Block placed successfully.")
  75.     return true
  76. end
  77.  
  78. -- 2. line: Places a line of blocks of a given length.
  79. -- The turtle will move forward relative to its current facing direction.
  80. -- 'len': The length of the line.
  81. -- Returns true on success (all blocks placed and movements completed).
  82. local function line(len)
  83.     for l = 1, len do
  84.         if not place() then
  85.             -- This should theoretically not be reached due to the robust 'place' function,
  86.             -- but kept for logical completeness.
  87.             return false
  88.         end
  89.  
  90.         if l < len then -- Don't move after placing the last block in the line
  91.             if not perform_action(turtle.forward, "Failed to move forward. Obstruction ahead.") then
  92.                 -- This should theoretically not be reached due to the robust 'perform_action' function.
  93.                 return false
  94.             end
  95.         end
  96.     end
  97.     return true
  98. end
  99.  
  100. -- 3. platform: Builds a platform of specified length and width.
  101. -- 'len': The length of the platform (along the turtle's initial forward direction).
  102. -- 'wid': The width of the platform (perpendicular to the initial forward direction).
  103. -- Returns true on success (platform fully built).
  104. local function platform(len, wid)
  105.     -- This variable tracks if the current line is being built in the same direction
  106.     -- as the turtle's initial forward direction (e.g., +Z) or the opposite direction (e.g., -Z).
  107.     local current_line_direction_is_initial_forward = true
  108.  
  109.     for w = 1, wid do
  110.         -- Build the current line. The 'line' function always moves turtle.forward().
  111.         -- The 'platform' function ensures the turtle is facing the correct direction before this call.
  112.         if not line(len) then
  113.             return false
  114.         end
  115.  
  116.         if w < wid then -- If not the last row, prepare for the next row
  117.             if current_line_direction_is_initial_forward then
  118.                 -- Finished a line moving in the initial forward direction.
  119.                 -- Turtle is at the end of the line, facing that direction.
  120.                 -- To move to the next row and then face opposite for the next line:
  121.                 if not perform_action(turtle.turnRight, "Failed to turn right.") then return false end
  122.                 if not perform_action(turtle.forward, "Failed to move forward to next row.") then return false end
  123.                 if not perform_action(turtle.turnRight, "Failed to turn right.") then return false end
  124.                 current_line_direction_is_initial_forward = false
  125.             else -- current_line_direction_is_initial_forward is false (finished line moving in opposite direction)
  126.                 -- Finished a line moving in the opposite direction.
  127.                 -- Turtle is at the end of the line, facing that direction.
  128.                 -- To move to the next row and then face initial forward for the next line:
  129.                 if not perform_action(turtle.turnLeft, "Failed to turn left.") then return false end
  130.                 if not perform_action(turtle.forward, "Failed to move forward to next row.") then return false end
  131.                 if not perform_action(turtle.turnLeft, "Failed to turn left.") then return false end
  132.                 current_line_direction_is_initial_forward = true
  133.             end
  134.         end
  135.     end
  136.  
  137.     return true
  138. end
  139.  
  140. -- Main program logic
  141. local args = { ... }
  142. local length = tonumber(args[1])
  143. local width = tonumber(args[2])
  144.  
  145. -- --- Initialization ---
  146. -- Get the block type from the currently selected slot (usually slot 1 by default)
  147. local initialSlot = turtle.getSelectedSlot()
  148. local initialItem = turtle.getItemDetail(initialSlot)
  149.  
  150. if not initialItem then
  151.     print("Error: No item found in the currently selected slot (" .. initialSlot .. ").")
  152.     print("Please place the desired building block in this slot before running the program.")
  153.     return
  154. end
  155. requiredBlockName = initialItem.name
  156. print("Building with block type: '" .. requiredBlockName .. "' (from slot " .. initialSlot .. ").")
  157.  
  158. -- Validate command-line arguments
  159. if not length or not width or length < 1 or width < 1 then
  160.     print("Usage: buildplatform <length> <width>")
  161.     print("  <length>: The length of the platform (positive number)")
  162.     print("  <width>: The width of the platform (positive number)")
  163.     return
  164. end
  165.  
  166. -- --- Start Construction ---
  167. print("Starting platform construction for " .. length .. "x" .. width .. " platform...")
  168. if platform(length, width) then
  169.     print("Platform built successfully!")
  170. else
  171.     print("Platform construction failed.")
  172. end
  173.  
Add Comment
Please, Sign In to add comment