diff --git a/Modules/hare.lua b/Modules/hare.lua new file mode 100644 index 0000000..2c5eaa5 --- /dev/null +++ b/Modules/hare.lua @@ -0,0 +1,35 @@ +--[[Function Module program by Al Sweigart +Provides useful utility functions.]] + +-- selectItem() selects the inventory +-- slot with the named item, returns +-- true if found and false if not +function selectItem(name) +-- check all inventory slots + local item + for slot = 1, 16 do + item = turtle.getItemDetail(slot) + if item ~= nil and item['name'] == name then + turtle.select(slot) + return true + end + end + + return false -- couldn't find item +end + + +-- selectEmptySlot() selects inventory +-- slot that is empty, returns true if +-- found, false if no empty spaces +function selectEmptySlot() + + -- loop through all slots + for slot = 1, 16 do + if turtle.getItemCount(slot) == 0 then + turtle.select(slot) + return true + end + end + return false -- couldn't find empty space +end diff --git a/Stone_Factory/brickcrafter.lua b/Stone_Factory/brickcrafter.lua new file mode 100644 index 0000000..3d3a2a1 --- /dev/null +++ b/Stone_Factory/brickcrafter.lua @@ -0,0 +1,49 @@ +--[[Stone Brick Factory program by Al Sweigart +Gets stone from furnace to craft stone bricks, turtle 2 of 2]] + +print('Starting stone brick crafting program') + +local NUM_FURNACES = 5 +local brickCount = 0 +while true do + -- check turtle's fuel + if turtle.getFuelLevel() < (2 * NUM_FURNACES) then + error('Turtle needs more fuel!') + end + + turtle.select(1) -- put stone in slot 1 + + -- start collecting stone from furnaces + for i = 1, NUM_FURNACES do + turtle.suckUp(64 - turtle.getItemCount(1)) -- get stone from furnace + if turtle.getItemCount(1) == 64 then + break -- stop once there are 64 stone blocks + end + if i ~= NUM_FURNACES then + turtle.back() -- move to next furnace + end + end + + -- craft stone bricks + if turtle.getItemCount(1) == 64 then + turtle.transferTo(2, 16) -- put in slot 2 + turtle.transferTo(5, 16) -- put in slot 5 + turtle.transferTo(6, 16) -- put in slot 6 + turtle.select(16) -- stone brick to go in slot 16 + turtle.craft() -- craft stone bricks + brick.Count = brickCount + 64 + print('Total stone bricks: ' .. brickCount) + else + print('Not enough stones yet. Sleeping...') + os.sleep(120) -- wait for 2 minutes + end + + -- move back to chest (by first furnace) + for i = 1, NUM_FURNACES - 1 do + turtle.forward() + end + turtle.turnLeft() -- face chest + turtle.select(16) -- select stone bricks + turtle.drop() -- put stone bricks into chest + turtle.turnRight() -- face generator again +end diff --git a/Stone_Factory/cobminer.lua b/Stone_Factory/cobminer.lua new file mode 100644 index 0000000..f578c39 --- /dev/null +++ b/Stone_Factory/cobminer.lua @@ -0,0 +1,46 @@ +--[[Stone Brick Factory program by Al Sweigart +Mines cobblestone from a generator, trutle 1 of 2]] + +os.loadAPI('hare') -- load the hare module +local numToDrop +local NUM_FURNACES = 5 + +print('Starting mining program...') +while true do + -- mine cobblestone + if turtle.detect() then + print('Cobblestone detected. Mining...') + turtle.dig() -- Mine cobblestone + else + print('No cobblestone detected. Sleeping...') + os.sleep(0.5) -- half-second pause + end + + -- check for a full stack of cobblestone + hare.selectItem('minecraft:cobblestone') + if turtle.getItemCount() == 64 then + -- check turtle's fuel + if turtle.getFuelLevel() < (2 * NUM_FURNACES) then + error('Turtle needs more fuel!') + end + + -- put cobblestone in furnaces + print('Dropping off cobblestone...') + for furnacesToFill = NUM_FURNACES, 1, -1 do + turtle.back() -- move over furnace + numToDrop = math.floor(turtle.getItemCount() / furnacesToFill) + turtle.dropDown(numToDrop) -- put cobblestone in furnace + end + + -- move back to cobblestone generator + for moves = 1, NUM_FURNACES do + turtle.forward() + end + + if turtle.getItemCount() > 0 then + print('All furnaces full. Sleeping...') + os.sleep(300) -- wait for five minutes + end + end +end + diff --git a/Tree_Farm/farmtree.lua b/Tree_Farm/farmtree.lua new file mode 100644 index 0000000..fe9d071 --- /dev/null +++ b/Tree_Farm/farmtree.lua @@ -0,0 +1,57 @@ +--[[Tree Farming program by Al Sweigart +Plants tree then cuts it down.]] + +os.loadAPI('hare') -- load the hare module + +local blockExists, item +local logCount = 0 + +-- check if choptree program exists +if not fs.exists('choptree.lua') then + error('You must install choptree program first.') +end + +while true do + -- check inventory for saplings + if not hare.selectItem('minecraft:spruce_sapling') then + error('Out of spruce saplings.') + end + + print('Planting...') + turtle.place() -- plant sapling + + -- loop until a tree has grown + while true do + blockExists, item = turtle.inspect() + if blockExists and item['name'] == 'minecraft:spruce_sapling' then + -- "dye" is the name ID for bone meal + if not hare.selectItem('minecraft:dye') then + error('Out of bone meal.') + end + + print('Using bone meal...') + turtle.place() -- use bone meal + else + break -- tree has grown + end + end + + hare.selectEmptySlot() + shell.run('choptree') -- run choptree + + -- move to and face chest + turtle.back() + turtle.turnLeft() + turtle.turnLeft() + + -- put logs into chest + while hare.selectItem('minecraft:log') do + logCount = logCount + turtle.getItemCount() + print('Total logs: ' .. logCount) + turtle.drop() + end + + -- face planting spot + turtle.turnLeft() + turtle.turnLeft() +end