Ubidibity

wither.lua

May 15th, 2025 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- local id=1 -- obviously set per turtle's position as explained above, moved into a .txt config file assigned below
  2.  
  3. -- 7 turtles, id set's it's position
  4. -- 7 6 5
  5. -- 4 3 2
  6. --   1
  7. -- 1 just places soul sand if it's block is clear
  8. -- 2,3, and 4 place soul sand if they're block is clear and then blip a redstone signal up
  9. -- 5,6, and 7 wait for redstone blip on bottom and then place wither skull's
  10. -- Note there's no need to verify inventory since they don't pick stuff up, just fill the inventory with sand for 1-4
  11. -- and skulls for 5-7 and it should construct withers.
  12. -- This really only works if explosions are disabled in your claim...
  13. -- I added the requirement for a redstone signal at the back to allow a way to turn it on and off.
  14. -- I also added a dig command to clear out the block in front of it, this helps 'reset' things in case the skulls are placed first
  15. -- (possibly due to running out of soulsand or something)
  16. -- I added a dig at the 'off' redstone state to make accessing the turtles easier since there's a block in the back to transmit the
  17. -- redstone signal and with the sandstone in the front accessing turtle 3 in particular was a bother
  18.  
  19. local function getTurtleID()
  20.     local file = fs.open("turtleID.txt", "r")
  21.     if not file then
  22.         error("Failed to open turtleID.txt. Please create the file with a number from 1 to 7.")
  23.     end
  24.     local id = tonumber(file.readLine())
  25.     file.close()
  26.     if not id or id < 1 or id > 7 then
  27.         error("Invalid turtle ID in turtleID.txt. Must be a number from 1 to 7.")
  28.     end
  29.     return id
  30. end
  31.  
  32. local turtleID = getTurtleID()
  33.  
  34. function findNextItem()
  35.     local currentSlot = turtle.getSelectedSlot()
  36.     local totalSlots = 16  -- Turtle has 16 slots
  37.    
  38.     -- Check all slots, starting from current
  39.     for i = 0, totalSlots - 1 do
  40.         local slotToCheck = (currentSlot + i - 1) % totalSlots + 1
  41.         turtle.select(slotToCheck)
  42.         if turtle.getItemCount() > 0 then
  43.             return true  -- Found a non-empty slot
  44.         end
  45.     end
  46.    
  47.     -- No non-empty slots found
  48.     turtle.select(currentSlot)  -- Return to original slot
  49.     return false
  50. end
  51.    
  52.  
  53. -- debug function
  54. local function pauseMe()
  55.   print("Press any key...")
  56.   os.pullEvent("key")
  57. end
  58.  
  59. local function placeForward()
  60.   local has_block, data = turtle.inspect()
  61.   if not has_block then
  62.     turtle.place()
  63.     return 1
  64.   end
  65.   return 0
  66. end
  67.  
  68. while findNextItem() do -- might as well exit if we run out of stuff
  69.  
  70.   while redstone.getInput("back")==true do
  71.     if turtleID>4 then
  72.       local loop=0
  73.       print("Waiting for redstone signal from below.")
  74.       while redstone.getInput("bottom")==false do
  75.         sleep(.5) -- wait for go signal
  76.         -- periodically the skulls get placed too soon, so we'll let #6 do an intermittent dig to reset things
  77.         if turtleID==6 then
  78.           loop=loop+1 -- 20 loops should be ~10 seconds in .5 second steps
  79.           if loop==20 then
  80.             turtle.dig()
  81.             turtle.place()
  82.             loop=0
  83.           end
  84.         end
  85.       end
  86.       print("Redstone signal received.")
  87.       redstone.setOutput("bottom",true)
  88.       while redstone.getInput("bottom")==true do
  89.         sleep(.5) -- wait for signal to stop=ack below
  90.       end
  91.       print("Redstone signal acknowledged.")
  92.       redstone.setOutput("bottom",false)
  93.       while placeForward()==0 do
  94.         sleep(.5) -- try until successful
  95.  -- the loop==20 above should cover this, if this fails it's probably empty the outer loop should catch that  turtle.dig() -- if it's failing try to clear the blockage
  96.       end
  97.       print("Block placed forward")
  98.     else
  99.       if placeForward()==1 then
  100.         print("placed inventory forward")
  101.         if turtleID>1 and turtleID<5 then -- need to exclude 1 since it doesn't get signaled back by turtleID 3 (not necessary)
  102.           redstone.setOutput("top",true)
  103.           while redstone.getInput("top")==false do
  104.             sleep(.5)
  105.           end
  106.           redstone.setOutput("top",false) -- wait for turtle above to acknowledge signal
  107.         end
  108.       else
  109.         print("Space blocked")
  110.       end
  111.       sleep(5)
  112. -- still not need with loop=20 above      turtle.dig() -- not 100% sure this may require a mining turtle...so splurge and invest in a diamond pick
  113.     end
  114.  
  115.     print("Turtle ",turtleID," Waiting for redstone startup at 5 second intervals")
  116.     sleep(5)
  117.  
  118.   -- redstone signal present loop
  119.   end
  120.  
  121. print("available item slot selected (or exit)")
  122. -- inventory not empty loop
  123. end
  124. print("exit it is.")
  125. pauseMe()
  126.  
Add Comment
Please, Sign In to add comment