-- Minecraft Open Computers String Farm Robot
-- Code by WolpertingerLP.de
-- Have fun :)
local robot = require("robot")
local os = require("os")
local component = require("component")
local waitTime = 100
local maintainSlot = robot.inventorySize()
local nextToolSlot = maintainSlot - 1
local compareSlot = nextToolSlot - 1
local buttonSlot = compareSlot - 1
local currentDirection = nil
local searchForButtonDirectionForth = "left"
local searchForButtonDirectionBack = "right"
local searchForLeavesDirectionForth = "left"
local searchForLeavesDirectionBack = "right"
function updateDirection()
if currentDirection == nil then
currentDirection = "forth"
elseif currentDirection == "forth" then
currentDirection = "back"
elseif currentDirection == "back" then
currentDirection = "forth"
end
end
function checkDirection()
updateDirection()
turns = 0
turnDirection = nil
if currentDirection == "forth" then
turnDirection = searchForLeavesDirectionForth
elseif currentDirection == "back" then
turnDirection = searchForLeavesDirectionBack
end
robot.select(compareSlot)
for x = 0, 4 do
if robot.compare() then
if turns < 3 then
if currentDirection == "forth" then
if turnDirection == "left" then
searchForLeavesDirectionForth = "left"
elseif turnDirection == "right" then
searchForLeavesDirectionForth = "right"
end
elseif currentDirection == "back" then
if turnDirection == "left" then
searchForLeavesDirectionBack = "left"
elseif turnDirection == "right" then
searchForLeavesDirectionBack = "right"
end
end
else
if currentDirection == "forth" then
if turnDirection == "left" then
searchForLeavesDirectionForth = "right"
print("Changing search for leaves direction to right")
elseif turnDirection == "right" then
searchForLeavesDirectionForth = "left"
print("Changing search for leaves direction to left")
end
elseif currentDirection == "back" then
if turnDirection == "left" then
searchForLeavesDirectionBack = "right"
print("Changing search for leaves direction to right")
elseif turnDirection == "right" then
searchForLeavesDirectionBack = "left"
print("Changing search for leaves direction to left")
end
end
end
robot.select(1)
return
else
turns = turns + 1
if turnDirection == "left" then
robot.turnLeft()
elseif turnDirection == "right" then
robot.turnRight()
end
end
end
print("Could not determine correct direction :(")
end
function pressButton()
robot.select(buttonSlot)
turns = 0
turnDirection = nil
if currentDirection == "forth" then
turnDirection = searchForButtonDirectionForth
elseif currentDirection == "back" then
turnDirection = searchForButtonDirectionBack
end
for x = 0, 4 do
if robot.compare() then
if turns < 3 then
if currentDirection == "forth" then
if turnDirection == "left" then
searchForButtonDirectionForth = "left"
elseif turnDirection == "right" then
searchForButtonDirectionForth = "right"
end
elseif currentDirection == "back" then
if turnDirection == "left" then
searchForButtonDirectionBack = "left"
elseif turnDirection == "right" then
searchForButtonDirectionBack = "right"
end
end
else
if currentDirection == "forth" then
if turnDirection == "left" then
searchForButtonDirectionForth = "right"
print("Changing search for button direction to right")
elseif turnDirection == "right" then
searchForButtonDirectionForth = "left"
print("Changing search for button direction to left")
end
elseif currentDirection == "back" then
if turnDirection == "left" then
searchForButtonDirectionBack = "right"
print("Changing search for button direction to right")
elseif turnDirection == "right" then
searchForButtonDirectionBack = "left"
print("Changing search for button direction to left")
end
end
end
robot.use()
break
else
turns = turns + 1
if turnDirection == "left" then
robot.turnLeft()
elseif turnDirection == "right" then
robot.turnRight()
end
end
end
end
function isNextToolAlreadyInInventory()
toolItem = component.inventory_controller.getStackInInternalSlot(nextToolSlot)
if toolItem == nil then return false end
if toolItem.label == "Shears" then
return true
end
return false
end
function getToolItem()
robot.select(maintainSlot)
component.inventory_controller.equip()
toolItem = component.inventory_controller.getStackInInternalSlot(maintainSlot)
component.inventory_controller.equip()
robot.select(1)
return toolItem
end
function checkTool()
if isNextToolAlreadyInInventory() == false then
robot.select(nextToolSlot)
if robot.suckDown(1) then
print("Equipping next tool from external storage")
end
end
robot.select(maintainSlot)
toolItem = getToolItem()
if toolItem == nil then
robot.select(nextToolSlot)
if component.inventory_controller.equip() then
print("New tool equipped in tool slot")
else
print("I´m not happy with my job. Without tools i will quit... for now")
end
else
print(string.format("Current tool damage: %s of %s",toolItem.damage, toolItem.maxDamage))
end
end
function dropAll()
for x = 1, buttonSlot-1 do
robot.select(x)
robot.drop()
end
robot.select(1)
end
function rest()
step = waitTime / 10
print(string.format("\nSleeping for %s seconds...",waitTime))
for x = 0,9 do
os.sleep(step)
print(string.format("Still sleeping for about %s seconds",waitTime - (x+1)*step))
end
print("\nGood Morning little SchoolGirl")
print("A new day another chance\n")
end
function doWork()
haveWork = true
while haveWork do
checkTool()
robot.select(compareSlot)
somethingToDo = robot.compare()
robot.select(1)
if somethingToDo then
robot.swing()
robot.forward()
else
robot.forward()
haveWork = false
end
end
end
function startUp()
print("Starting string farm program ...\n")
os.sleep(1)
print("Checking inventory ...")
print("Warmup finished/n")
while true do
checkDirection()
doWork()
dropAll()
pressButton()
rest()
end
end
startUp()
Comments