ComputerCraft/Tree_Farm/treeline.lua

98 lines
2.9 KiB
Lua
Raw Permalink Normal View History

2020-12-20 21:58:06 +00:00
--[[
treeline program by The_Lux
This program checks a tree farm with given length and chops the tree down
when it finds one.
]]
os.loadAPI('modules.lua') -- Load the modules module
-- Handle command line arguments
local cliArgs = {...}
local num_of_trees = tonumber(cliArgs[1])
local tree_distance = tonumber(cliArgs[2])
local tree_sapling = cliArgs[3]
if num_of_trees == nil or tree_distance == nil or tree_sapling == nil or cliArgs[1] == '?' then
print('Usage: treeline.lua <num_of_trees> <tree_distance> <tree_sapling>')
print('num_of_trees = How many trees a in a line.')
print('tree_distance = How much space (+1) are in between the trees.')
print('tree_sapling = What sapling to use.')
return
end
-- Empty everything except for the saplings
for i = 1, 16 do
local item = turtle.getItemDetail(i)
if item ~= nil and item['name'] ~= 'minecraft:' .. tree_sapling .. '_sapling' then
turtle.select(i)
print('Dropping ' .. item['name'])
turtle.drop()
end
end
-- Check if we have saplings in our inventory
if not modules.select_item('minecraft:' .. tree_sapling .. '_sapling') then
print('Out of ' .. tree_sapling .. ' saplings. Please provide additional saplings')
print('Press Enter when additional sapling are filled.')
read()
end
-- Check if we have enough fuel to make our way and come back to start
needed_fuel = (num_of_trees * tree_distance) * 2
while turtle.getFuelLevel() <= needed_fuel do
print('Not enough fuel to farm and come back.')
print('We need ' .. (needed_fuel - turtle.getFuelLevel()) .. ' additional fuel.')
print('Please put fuel in slot 1 and press a enter.')
read()
turtle.select(1)
turtle.refuel()
end
for i = 1, num_of_trees do
for j = 1, tree_distance do
-- check if something is in your way and dig it up
if i ~= (tree_distance * num_of_trees) then
turtle.dig()
end
turtle.forward()
end
turtle.turnLeft()
blockExists, item = turtle.inspect()
if blockExists and item['name'] == 'minecraft:' .. tree_sapling .. '_log' then
print('Cutting tree...')
turtle.dig()
print('Planting seed.')
modules.select_item('minecraft:' .. tree_sapling .. '_sapling')
turtle.place()
elseif not blockExists then
turtle.forward()
local success, underneath = turtle.inspectDown()
if underneath['name'] ~= 'minecraft:dirt' and underneath['name'] ~= 'minecraft:grass_block' then
print('Error. No Dirt or Grass underneath to plant a tree. Found: ' .. underneath['name'])
turtle.back()
else
turtle.back()
print('Planting seed.')
modules.select_item('minecraft:' .. tree_sapling .. '_sapling')
turtle.place()
end
end
turtle.turnRight()
end
-- Move forward to the chest again
turtle.turnLeft()
turtle.turnLeft()
print('Heading back to start.')
for i = 1, (tree_distance * num_of_trees) do
if i ~= (tree_distance * num_of_trees) then
turtle.dig()
end
turtle.forward()
turtle.turnLeft()
turtle.turnLeft()
end