Skip to content

Instantly share code, notes, and snippets.

@devilholk
Created March 28, 2017 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devilholk/c0d51ab676262f9d974b68fe2bf2465d to your computer and use it in GitHub Desktop.
Save devilholk/c0d51ab676262f9d974b68fe2bf2465d to your computer and use it in GitHub Desktop.
Unfinished script for turtle in minecraft/computercraft
-- Inledande tester
-- Config
ender_chest_slot = 2
bucket_slot = 1
my_inventory = peripheral.wrap('right') -- Assumes we have a duck antenna equipped to the right
dofile('navigation.lua')
function mine_up()
--returns success of operation and if anything was mined and failure reason
while true do
move_count, failure_reason = nav:go_up(1)
if move_count == 1 then
return true, false
else
--We failed to go forward, either something is obstructing us or we are out of fuel
if failure_reason == 'Out of fuel' then
return false, false, failure_reason
elseif failure_reason == 'Movement obstructed' then
dig_success, dig_failure_reason = turtle.digUp()
if dig_success == true then
--Since we managed to dig we will simply do nothing here
--This causes our loop to try to move again and will handle any issues related to that
else
if dig_failure_reason == 'Nothing to dig here' then
-- Do nothing, this might be a temporary obstruction that moved
-- But we should have some kind of retry count to not get stuck in a loop
else
print("Dig failure: ", dig_failure_reason)
return false, false, dig_failure_reason
end
end
else
print("Unhandled failure: ", failure_reason)
return false, false, failure_reason
end
end
end
end
function get_table_count(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
function slots_used()
return get_table_count(my_inventory.getAllStacks())
end
function dump_table(t)
for key, value in pairs(t) do
print(key, ": ", value)
end
end
function try_to_empty_inventory()
turtle.select(ender_chest_slot)
place_succeeded, place_failure = turtle.place()
if place_succeeded == false then
return false, place_failure
end
--Note, we don't need the stuff under here because we don't need to wrap
-- ender_chest = peripheral.wrap("front")
-- if ender_chest == nil then
-- return false, 'Could not wrap ender chest'
-- end
-- if not ender_chest.getInventoryName() == 'Ender Chest' then
-- return false, 'Not an ender chest' -- This check might be removed, we might want to use tesseracts or other stuff
-- end
-- --TODO recover placed object if we fail etc
chest_dir = nil
for slot, stuff in pairs(my_inventory.getAllStacks()) do
if slot == bucket_slot then --Ignore bucket and ender chest slot
elseif slot == ender_chest_slot then
else
if chest_dir == nil then
for index, dir in ipairs({'north', 'east', 'south', 'west'}) do
transfer_succeeded, info = pcall(my_inventory.pushItem, dir, slot)
if transfer_succeeded == true then
chest_dir = dir
break
end
end
else
transfer_succeeded, info = pcall(my_inventory.pushItem, chest_dir, slot)
if transfer_succeeded == false then
print("warning, failed to transfer items because: ", info)
end
end
--dump_table(stuff.all());
--print(slot, ": ", stuff)
end
end
dig_succeded, dig_failure_reason = turtle.dig()
if dig_succeded then
return true
else
return false, dig_failure_reason
end
end
function mine_forward()
--returns success of operation and if anything was mined and failure reason
while true do
--Check if it is empty in front of us, the way the loop of this function is constructed
--this check will be performed after each dig
if turtle.detect() == false then
--print("DEBUG: Used slots: ", slots_used())
if slots_used() > 12 then
--print("DEBUG: Trying to empty inventory")
try_to_empty_inventory()
end
end
move_count, failure_reason = nav:go_forward(1)
if move_count == 1 then
return true, false
else
--We failed to go forward, either something is obstructing us or we are out of fuel
if failure_reason == 'Out of fuel' then
return false, false, failure_reason
elseif failure_reason == 'Movement obstructed' then
dig_success, dig_failure_reason = turtle.dig()
if dig_success == true then
--Since we managed to dig we will simply do nothing here
--This causes our loop to try to move again and will handle any issues related to that
else
if dig_failure_reason == 'Nothing to dig here' then
-- Do nothing, this might be a temporary obstruction that moved
-- But we should have some kind of retry count to not get stuck in a loop
else
print("Dig failure: ", dig_failure_reason)
return false, false, dig_failure_reason
end
end
else
print("Unhandled failure: ", failure_reason)
return false, false, failure_reason
end
end
end
end
LUT_OPPOSITE_DIR = {N='S', S='N', E='W', W='E'}
function mine_streak(count)
for mine_count=1,count do
mine_success, move_success, mine_failure = mine_forward()
if mine_success == false then
return false, mine_failure
end
end
return true
end
function mine_streak_up(count)
for mine_count=1,count do
mine_success, move_success, mine_failure = mine_up()
if mine_success == false then
return false, mine_failure
end
end
return true
end
function mine_tunnel(args)
setmetatable(args, {__index={width=3, height=3, depth=10}})
nav:reset_position() --we preted we always face north when we begin, we will consider north to be forward
nav:print_position()
sideral_direction = 'E'
mining_direction = 'N'
for mine_up_count=args.height-1,0,-1 do
for mine_side_count=args.width-1,0,-1 do
mining_succeeded, mining_failure = mine_streak(args.depth)
if mining_succeeded == false then
return false, mining_failure
end
if mine_side_count == 0 then
if mine_up_count == 0 then
--We are done. TODO: perhaps return?
--Reset position to lower left corner
-- Note that here we might get stuck because of fallen sand/gravel etc
nav:go_down(args.height-1)
nav:face_cardinal_dir('W')
nav:go_forward(args.width-1)
nav:face_cardinal_dir('N')
return true
else
mining_succeeded, mining_failure = mine_streak_up(1)
if mining_succeeded == false then
return false, mining_failure
end
sideral_direction = LUT_OPPOSITE_DIR[sideral_direction]
end
else
nav:face_cardinal_dir(sideral_direction) --TODO check return
mining_succeeded, mining_failure = mine_streak(1)
if mining_succeeded == false then
return false, mining_failure
end
end
mining_direction = LUT_OPPOSITE_DIR[mining_direction]
nav:face_cardinal_dir(mining_direction) --TODO check return
end
end
end
function mine_tunnel_old(args)
setmetatable(args, {__index={width=3, height=3, depth=10}})
-- print(args.width, " ", args.height, " ", args.depth)
for mine_right_count=0,args.width-1 do
mining_direction = nav.dir
print("Mine ", args.depth, " meters ", mining_direction)
for mine_forward_count=0,args.depth-1 do
mine_success, move_success, mine_failure = mine_forward()
if mine_success == false then
print("Failed to mine: ", mine_failure)
--TODO handle problems better
return
end
end
--Face east (remember east may not be east, it is just in relation to the tunnel facing "north")
print("Go east one")
nav:face_cardinal_dir('E') --TODO check return status
-- Mine one step
mine_success, move_success, mine_failure = mine_forward()
if mine_success == false then
print("Failed to mine: ", mine_failure)
--TODO handle problems better
return false, mine_failure
end
print("Prepare to go other way")
if mining_direction == 'S' then
new_mining_direction = 'N'
elseif mining_direction == 'N' then
new_mining_direction = 'S'
else
return false, "Got lost"-- TODO handle better
end
nav:face_cardinal_dir(new_mining_direction) --TODO check return status
end
nav:go_down(args.height-1)
nav:face_cardinal_dir('W')
nav:go_forward(args.width-1)
nav:face_cardinal_dir('N')
-- for go_right_count=1,width-1 do
-- for go_down_count=1,height-1 do
-- print("Mine forward")
-- print("Mine down")
-- print("Turn around 180°")
--
-- end
-- print("Mine right")
--
-- end
end
--mine_succeded, mine_failure = mine_tunnel({depth=3})
--print(mine_succeded, ', ', mine_failure)
--a, b = try_to_empty_inventory()
--print(a, b)
--try_to_empty_inventory()
--mine_succeded, mine_failure = mine_tunnel({width=3, height=3, depth=3})
--print(mine_succeded, ', ', mine_failure)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment