Advertisement
Guest User

witherfarm.lua

a guest
Jan 16th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.37 KB | None | 0 0
  1. -- Time to wait between checking if we can spawn (in seconds)
  2. local checkDelayS = 10 -- 10 seconds
  3.  
  4. -- Time to wait between spawns (in seconds)
  5. local spawnDelayS = 60 * 2 -- 2 minutes
  6.  
  7. -- Slots
  8. skullSlot = 1
  9. soulSandSlot = 2
  10.  
  11. -- Bring in required modules.
  12. local robot = require("robot")
  13. local os = require("os")
  14. local term = require("term")
  15. local sides = require("sides")
  16. local component = require("component")
  17.  
  18.  
  19. -- Function to keep trying to move
  20. -- forward until success.
  21. local function moveForward(count)
  22.   for i=1, count do
  23.     while not (robot.forward()) do
  24.       os.sleep(1)
  25.     end
  26.   end
  27. end
  28.  
  29. -- Function to keep trying to move
  30. -- back until success.
  31. local function moveBack(count)
  32.   for i=1, count do
  33.     while not (robot.back()) do
  34.       os.sleep(1)
  35.     end
  36.   end
  37. end
  38.  
  39. -- Function to keep trying to move
  40. -- up until success.
  41. local function moveUp(count)
  42.   for i=1, count do
  43.     while not (robot.up()) do
  44.       os.sleep(1)
  45.     end
  46.   end
  47. end
  48.  
  49. -- Function to keep trying to move
  50. -- down until success.
  51. local function moveDown(count)
  52.   for i=1, count do
  53.     while not (robot.down()) do
  54.       os.sleep(1)
  55.     end
  56.   end
  57. end
  58.  
  59. -- Function to keep trying to place
  60. -- until success.
  61. local function placeBlock(inventorySlot, side)
  62.   robot.select(inventorySlot)
  63.   while not (robot.place(side)) do
  64.     os.sleep()
  65.   end
  66. end
  67.  
  68. local function gatherMaterial(inventoryLocation, slotNumber, desiredAmount)
  69.   -- Select desired slot
  70.   robot.select(slotNumber)
  71.  
  72.   -- Get the current amount
  73.   local currentCount = robot.count()
  74.  
  75.   -- Loop until we have enough (we'll return if we get to a point where we can't retrieve any more
  76.   while currentCount < desiredAmount do
  77.     -- Determine how much more we need
  78.     local remainingNeeded = desiredAmount - currentCount
  79.    
  80.     -- Try to get some more
  81.     local gotItems = component.robot.suck(inventoryLocation, remainingNeeded)    
  82.    
  83.     -- Return failure if we got nothing
  84.     if gotItems == false then
  85.       return false
  86.     end
  87.    
  88.     -- Update count
  89.     currentCount = robot.count()
  90.   end
  91.  
  92.   -- Success!  
  93.   return true
  94. end
  95.  
  96. local function moveIntoPosition()
  97.   moveUp(1)
  98.   moveForward(1)
  99.   robot.turnLeft()
  100.   moveForward(3)
  101.   moveDown(2)
  102.   moveForward(1)
  103.   robot.turnRight()
  104.   moveForward(3)
  105.   robot.turnRight()
  106.   moveForward(2)
  107.   robot.turnLeft()
  108. end
  109.  
  110. local function constructWither()
  111.      
  112.   placeBlock(soulSandSlot, sides.front)
  113.   moveUp(1)
  114.   placeBlock(soulSandSlot, sides.front)
  115.   robot.turnLeft()
  116.   moveForward(1)
  117.   robot.turnRight()
  118.   placeBlock(soulSandSlot, sides.front)
  119.   robot.turnRight()
  120.   moveForward(2)
  121.   robot.turnLeft()
  122.   placeBlock(soulSandSlot, sides.front)
  123.   moveUp(1)
  124.   placeBlock(skullSlot, sides.front)
  125.   robot.turnLeft()
  126.   moveForward(1)
  127.   robot.turnRight()
  128.   placeBlock(skullSlot, sides.front)
  129.   robot.turnLeft()
  130.   moveForward(1)
  131.   robot.turnRight()
  132.   placeBlock(skullSlot, sides.front)
  133. end
  134.  
  135. local function retreat()
  136.   moveBack(1)
  137.   moveDown(2)
  138.   robot.turnLeft()
  139.   moveForward(1)
  140.   robot.turnLeft()
  141.   moveForward(2)
  142.   robot.turnLeft()
  143.   moveForward(1)
  144.   moveUp(2)
  145.   moveForward(3)
  146.   robot.turnLeft()
  147.   moveBack(1)
  148.   moveDown(1)
  149. end
  150.  
  151. local function checkIfEnabled()
  152.   -- Check for redstone signal on left side of robot
  153.   rs = component.redstone
  154.   enableSignal = rs.getInput(sides.left)
  155.   if enableSignal > 7 then
  156.     return true
  157.   end
  158.  
  159.   return false
  160. end
  161.  
  162. -- Clear screen
  163. term.clear()
  164. term.setCursor(1, 1)
  165.  
  166. -- Header
  167. term.write("Wither Farm\n\n")
  168.  
  169. -- Loop forever
  170. while true do
  171.  
  172.   -- Check if we are enabled
  173.   term.write("Checking status\n")
  174.   enabled = checkIfEnabled()
  175.   if enabled then
  176.     term.write("Spawn enabled\n")
  177.    
  178.     -- Gather materials
  179.     term.write("Gathering materials\n")
  180.     enoughSkulls = gatherMaterial(sides.bottom, skullSlot, 3)
  181.     enoughSoulSand = gatherMaterial(sides.front, soulSandSlot, 4)
  182.    
  183.     -- Do we have enough?
  184.     if enoughSkulls and enoughSoulSand then
  185.       -- Move into position
  186.       term.write("Moving into position")
  187.       moveIntoPosition()
  188.    
  189.       -- Construct the wither
  190.       term.write("Constructing wither\n")
  191.       constructWither()
  192.      
  193.       -- Retreat
  194.       term.write("Retreating\n")
  195.       retreat()
  196.      
  197.       -- Wait awhile before spawning again
  198.       term.write("Waiting for spawn cooldown\n")
  199.       os.sleep(spawnDelayS)
  200.     else
  201.       term.write("Not enough materials yet\n")
  202.     end
  203.    
  204.   else
  205.     term.write("Spawn disabled!\n")
  206.   end
  207.  
  208.   -- Check delay  
  209.   term.write("Waiting before next check\n")
  210.   os.sleep(checkDelayS)
  211.  
  212. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement