Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- globals
- args = {...}
- if table.getn(args) > 0 then
- width = args[1]
- height = args[2]
- else
- -- default values
- width = 10
- height = 4
- end
- default_item_slot = 1
- end_of_inventory = 14
- torch_slot = 15
- fuel_slot = 16
- x_between_torches = 2
- z_between_torches = 0
- z_since_last_torch = 0
- common_items = {
- "minecraft:cobblestone",
- "minecraft:stone"
- }
- -- globals - positioning
- unit_vectors = {}
- unit_vectors['deeper'] = {x = 0, y = 0, z = 1}
- unit_vectors['out'] = {x = 0, y = 0, z = -1}
- unit_vectors['left'] = {x = -1, y = 0, z = 0}
- unit_vectors['right'] = {x = 1, y = 0, z = 0}
- turning = {}
- turning['deeper'] = {left = 'left', right = 'right'}
- turning['out'] = {left = 'right', right = 'left'}
- turning['left'] = {left = 'out', right = 'deeper'}
- turning['right'] = {left = 'deeper', right = 'out'}
- facing = "deeper"
- pos = {x = 1, y = 1, z = 1}
- -- main
- function main()
- -- origin = copy_position()
- empty_inventory()
- face("deeper")
- -- while true do
- -- dig_slice("up")
- -- dig_slice("down")
- -- refuel_if_needed()
- -- if not has_empty_slot() then
- -- empty_inventory()
- -- end
- -- end
- end
- -- inventory
- function has_empty_slot()
- for i=1,end_of_inventory do
- if turtle.getItemDetail(i) == nil then
- return true
- end
- end
- return false
- end
- function empty_inventory()
- prev_pos = copy_position()
- prev_facing = facing
- navigate_to(origin)
- destroy_commons()
- empty_chestables()
- navigate_to(prev_pos)
- face(prev_facing)
- end
- function empty_chestables()
- -- assumes turtle has already destroyed commons so only good shit left
- face("out")
- print("emptying chestables!")
- for j=1,end_of_inventory do
- item = turtle.getItemDetail(j)
- if not (item == nil) then
- drop(j)
- end
- end
- end
- function drop(i)
- turtle.select(i)
- turtle.drop()
- turtle.select(default_item_slot)
- end
- function destroy_commons()
- -- assumes turtle is currently at chest
- navigate_to_inventory("lava")
- for i=1,end_of_inventory do
- item = turtle.getItemDetail(i)
- if ((not (item == nil)) and (is_common_item(item.name))) then
- drop(i)
- end
- end
- navigate_to_inventory("chest")
- end
- function is_common_item(item_name)
- for _, common_name in pairs(common_items) do
- if item_name == common_name then
- return true
- end
- end
- return false
- end
- function navigate_to_inventory(destination)
- if destination == "lava" then
- face("out")
- elseif destination == "chest" then
- face("deeper")
- end
- move("up")
- move("forward")
- move("forward")
- move("down")
- move("down") -- ensure we're on the ground
- if destination == "lava" then
- elseif destination == "chest" then
- face("out")
- end
- end
- -- movement & positioning
- function copy_position()
- return {x = pos.x, y = pos.y, z = pos.z}
- end
- function print_position()
- local x_str = tostring(pos.x)
- local y_str = tostring(pos.y)
- local z_str = tostring(pos.z)
- print("pos: (" .. x_str .. ", " .. y_str .. ", " .. z_str .. ") facing: " .. facing)
- end
- function move(direction)
- local success = false
- if direction == "forward" then
- success = turtle.forward()
- if success then
- pos.x = pos.x + unit_vectors[facing]["x"]
- pos.z = pos.z + unit_vectors[facing]["z"]
- end
- elseif direction == "up" then
- success = turtle.up()
- if success then
- pos.y = pos.y + 1
- end
- elseif direction == "down" then
- success = turtle.down()
- if success then
- pos.y = pos.y - 1
- end
- end
- return success
- end
- function turn(direction)
- if direction == "left" then
- facing = turning[facing].left
- turtle.turnLeft()
- elseif direction == "right" then
- facing = turning[facing].right
- turtle.turnRight()
- end
- end
- function face(direction)
- while not (facing == direction) do
- turn("right")
- end
- end
- function turn_around()
- turn("right")
- turn("right")
- end
- function navigate_to(new_pos)
- -- assumes space at new_pos is empty
- navigate_to_z_coord(new_pos.z)
- navigate_to_y_coord(new_pos.y)
- navigate_to_x_coord(new_pos.x)
- end
- function navigate_to_x_coord(new_x)
- if (new_x > pos.x) then
- face("right")
- elseif (new_x < pos.x) then
- face("left")
- end
- while not (pos.x == new_x) do
- move("forward")
- end
- end
- function navigate_to_y_coord(new_y)
- while pos.y < new_y do
- move("up")
- end
- while pos.y > new_y do
- move("down")
- end
- end
- function navigate_to_z_coord(new_z)
- if (new_z > pos.z) then
- face("deeper")
- elseif (new_z < pos.z) then
- face("out")
- end
- while not (pos.z == new_z) do
- move("forward")
- end
- end
- -- utility
- function sleep(n) -- seconds
- local clock = os.clock
- local t0 = clock()
- while clock() - t0 <= n do end
- end
- -- exec
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement