Skip to content

Instantly share code, notes, and snippets.

@benphelps
Last active September 30, 2019 21:35
Show Gist options
  • Save benphelps/7ef0ac603504bbe76299f63c78b97b02 to your computer and use it in GitHub Desktop.
Save benphelps/7ef0ac603504bbe76299f63c78b97b02 to your computer and use it in GitHub Desktop.
A computer craft turtle program.
--[[
How about no.
]]
local CHEST_SLOT = 16
local PIT_WIDTH = 3
local PIT_LENGTH = 3
local PIT_DEPTH = 45
local function log(str, ...)
print(string.format(str, ...))
end
local function transfer_inventory()
local move_above_chest = turtle.up()
if move_above_chest then
turtle.select(CHEST_SLOT)
local chest_slot_info = turtle.getItemDetail()
if chest_slot_info and chest_slot_info.name == 'enderstorage:ender_storage' then
local place_chest_belove = turtle.placeDown(CHEST_SLOT)
if place_chest_belove then
for slot = 1, 15 do
turtle.select(slot)
local slot_items = turtle.getItemCount(slot)
if slot_items > 0 then
turtle.select(slot)
local transfered_item = turtle.dropDown(slot_items)
if transfered_item then
log('Transfered %i items from slot %i to ME system.', slot_items, slot)
end
end
end
turtle.select(16)
turtle.digDown()
for slot = 1, 16 do
turtle.select(slot)
local chest_info = turtle.getItemDetail()
if chest_info and chest_info.name == 'enderstorage:ender_storage' then
turtle.transferTo(CHEST_SLOT)
break
end
end
else
log('Unable to place our Ender Chest below us, what happened?')
end
else
log("Place an Ender Chest in slot 16!")
end
turtle.down()
end
end
local function check_inventory()
local total_slots = 15
local used_slots = 0
for slot = 1, total_slots do
local count = turtle.getItemCount(slot)
if count > 0 then
used_slots = used_slots + 1
end
end
if used_slots >= total_slots then
log('Inventory full, transfering...')
transfer_inventory()
end
end
local function forward()
local success = turtle.forward()
if not success then
turtle.dig()
local second_try = turtle.forward()
return second_try
end
return success
end
local function pit()
local direction = 'forwards'
local moving = 'right'
local first = true
local start_x, start_z, start_y = gps.locate(5)
log('Starting at layer %i', start_y)
log('Going to layer %i', PIT_DEPTH)
for y = start_y, PIT_DEPTH, -1 do
for x = 1, PIT_WIDTH do
for z = 1, PIT_LENGTH - 1 do
check_inventory()
turtle.digDown()
forward()
end
turtle.digDown()
if x < PIT_WIDTH then
if moving == 'right' then
if direction == 'forwards' then
turtle.turnRight()
forward()
turtle.digDown()
turtle.turnRight()
direction = 'backwards'
else
turtle.turnLeft()
forward()
turtle.digDown()
turtle.turnLeft()
direction = 'forwards'
end
else
if direction == 'forwards' then
turtle.turnLeft()
forward()
turtle.digDown()
turtle.turnLeft()
direction = 'backwards'
else
turtle.turnRight()
forward()
turtle.digDown()
turtle.turnRight()
direction = 'forwards'
end
end
end
end
if moving == 'right' then
if direction == 'forwards'then
turtle.turnRight()
turtle.turnRight()
direction = 'backwards'
moving = 'left'
else
turtle.turnLeft()
turtle.turnLeft()
direction = 'forwards'
moving = 'right'
end
else
if direction == 'forwards'then
turtle.turnLeft()
turtle.turnLeft()
direction = 'backwards'
moving = 'left'
else
turtle.turnRight()
turtle.turnRight()
direction = 'forwards'
moving = 'right'
end
end
if y > PIT_DEPTH then
log('Moving down, starting layer %i', y - 1)
turtle.down()
end
end
local end_x, end_z, end_y = gps.locate(5)
for z = end_y, start_y do
if z > start_y then
turtle.up()
end
end
transfer_inventory()
end
pit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment