Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function save(tbl, name)
- local file = fs.open(name, "w")
- file.write(textutils.serialize(tbl))
- file.close()
- end
- function load(name)
- local file = fs.open(name, "r")
- if not file then
- save({}, name)
- return load(name)
- else
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- end
- end
- function go_to(target)
- if me.y ~= target.y then
- go_to({x = 0, y = me.y, z = 0})
- while me.y < target.y do
- if turtle.up() then me.y = me.y + 1 end
- end
- while me.y > target.y do
- if turtle.down() then me.y = me.y - 1 end
- end
- end
- if me.x < target.x then
- turtle.turnRight()
- while me.x < target.x do
- if turtle.forward() then me.x = me.x + 1 end
- end
- turtle.turnLeft()
- end
- if me.x > target.x then
- turtle.turnLeft()
- while me.x > target.x do
- if turtle.forward() then me.x = me.x - 1 end
- end
- turtle.turnRight()
- end
- while me.z < target.z do
- if turtle.forward() then me.z = me.z + 1 end
- end
- while me.z > target.z do
- if turtle.back() then me.z = me.z - 1 end
- end
- end
- function next_position(position)
- local new_position = {x = position.x, y = position.y, z = position.z, side = position.side}
- if position.x == 5 then
- new_position.x = -5
- if position.z == 5 then
- new_position.z = -5
- new_position.y = position.y + 3
- else
- new_position.z = position.z + 1
- end
- else
- if position.x == -1 and position.z == 0 then
- new_position.x = position.x + 2
- else
- new_position.x = position.x + 1
- end
- end
- return new_position
- end
- function get_enderchest(group)
- turtle.select(16)
- turtle.placeDown()
- end
- function safe_string(text)
- local new_text = {}
- for i = 1, #text do
- local val = string.byte(text, i)
- new_text[i] = (val > 31 and val < 127) and val or 32
- end
- return string.char(unpack(new_text))
- end
- function earliest_order(position_1, position_2)
- if position_1.y > position_2.y then
- return true
- elseif position_1.y == position_2.y then
- if position_1.z > position_2.z then
- return true
- elseif position_1.z == position_2.z then
- if position_1.x > position_2.x then
- return true
- elseif position_1.x == position_2.x then
- if position_1.side == "up" then
- return true
- end
- end
- end
- end
- end
- function next_open_position()
- if #open_positions > 1 then
- table.sort(open_positions, earliest_order)
- return table.remove(open_positions)
- else
- local position = table.remove(open_positions)
- table.insert(next_position(position))
- return position
- end
- end
- function register(stack)
- local item = nil
- stack.string_id = safe_string(stack.id..stack.dmg..stack.raw_name..stack.display_name)
- for _, existing_item in ipairs(items) do
- if existing_item.string_id == stack.string_id then
- item = existing_item
- break
- end
- end
- if not item then
- item = {}
- item.string_id = stack.string_id
- item.damage = stack.dmg
- item.max_damage = stack.max_dmg
- item.max_stack = stack.max_size
- item.recipes = {}
- item.quantity = 0
- item.position = next_open_position()
- item.slot = nil
- table.insert(items, item)
- end
- if item.position == nil then
- item.position = next_open_position()
- end
- save(open_positions, "open_positions")
- end
- function check_inventory()
- local items_to_sort = {}
- local inventory_items = inventory.getAllStacks()
- for i = 1, #inventory_items - 1 do
- table.insert(items_to_sort, register(inventory_items[i].basic()))
- end
- return items_to_sort
- end
- function distance_away(position)
- local distance = math.abs(position.y - me.y)
- distance = distance + math.abs(me.x) + math.abs(me.z)
- distance = distance + math.abs(position.x) + math.abs(position.z)
- return distance
- end
- function closest_order(item_1, item_2)
- return distance_away(item_1.position) > distance_away(item_2.position)
- end
- function give_item_slots(items_to_sort)
- for i = 1, #items_to_sort do
- items_to_sort[i].slot = i
- end
- end
- function deposit_item(item)
- go_to(item.position)
- local amount = inventory.push(item.position.side, item.slot)
- item.quantity = item.quantity + amount
- save(items, "items")
- end
- function sort_items(items_to_sort)
- give_item_slots(items_to_sort)
- while #items_to_sort > 0 do
- local closest_item = table.sort(items_to_sort, closest_order)[#items_to_sort]
- deposit_item(closest_item)
- table.remove(items_to_sort)
- end
- end
- function condense_inventory()
- inventory.condenseItems()
- inventory.swapStacks(#inventory.getAllStacks(), 16)
- end
- function begin_depositing()
- go_to({0, me.y, 0})
- local at_home = turtle.detectDown()
- condense_inventory()
- if not at_home then
- get_enderchest("deposit")
- end
- while turtle.suckDown() do end
- chest.condenseItems()
- if #chest.getAllStacks() > 0 then
- table.insert(queue, 2, begin_depositing)
- end
- if not at_home then
- turtle.digDown()
- end
- local items_to_sort = check_inventory()
- sort_items(items_to_sort)
- end
- function start()
- me = {x=0,y=0,z=0}
- queue = {}
- while not chest do
- chest = peripheral.wrap("bottom")
- sleep(3)
- end
- inventory = peripheral.wrap("right")
- items = load("items")
- open_positions = load("open_positions")
- end
- function do_queue()
- local i = 1
- while i <= #queue do
- queue[i]()
- i = i + 1
- end
- end
- while true do
- command = io.read()
- if command == ">" then
- table.insert(queue, begin_depositing)
- elseif command == "" then
- do_queue()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement