Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- CC
- --- Turtle sorter
- --- Input amount of rows and height of chests
- -- User input
- -- Initialized settings for the area the turtle while sort
- local chest_width = 5
- local chest_height = 2
- -- If the turtles backs up to sort the chests ar to it's "chest_side"
- local chest_side = 'Left'
- -- The way the face of the turtle is oriented
- -- N = 0; E = 1; S = 2; W = 3
- local turtle_face = 1
- -- vector from 0 to 'home'.
- local home_cords = vector.new(89, 85, 172)
- -- Script vars
- -- Chest
- local chest = peripheral.wrap('front')
- local copy_side = chest_side
- -- Positioning
- local pos = vector.new(gps.locate())
- local diff = pos:sub(home_cords)
- -- accepted fuels
- local accepted_fuel = {
- 'minecraft:coal_block',
- 'minecraft:coal',
- }
- -- Helper functions
- local function table_to_set(table)
- --- Converts a table to a set where there are no double keys.
- -- Create the set.
- local a_set = {}
- -- For each item in the table iterate trough it.
- for i in ipairs(table) do
- -- The key is the 'name' and the value, a place holder, is the last
- -- 'count' value.
- a_set[table[i]['name']] = table[i]['count']
- end
- -- Print function remove.
- for key, value in pairs(a_set) do
- print( key, value)
- end
- -- Return the set.
- return a_set
- end
- local function around()
- turtle.turnLeft()
- turtle.turnLeft()
- if turtle_face + 2 == 4 then
- turtle_face = 0
- elseif turtle_face + 2 == 5 then
- turtle_face = 1
- else
- turtle_face = turtle_face + 2
- end
- end
- local function Left()
- turtle.turnLeft()
- if turtle_face - 1 == -1 then
- turtle_face = 3
- else
- turtle_face = turtle_face - 1
- end
- end
- local function Right()
- turtle.turnRight()
- if turtle_face + 1 == 4 then
- turtle_face = 0
- else
- turtle_face = turtle_face + 1
- end
- end
- local function n_forward(n)
- for x=1,n do
- turtle.forward()
- end
- end
- local function pick_up()
- for i in pairs(chest.list()) do
- turtle.suck()
- end
- return true
- end
- local function refuel()
- local current_fuel = turtle.getFuelLevel()
- if current_fuel <= 0 then
- while current_fuel <= 0 do
- turtle.select(1)
- for x=1, #accepted_fuel do
- if turtle.getItemDetail().name == accepted_fuel[x] then
- turtle.refuel()
- end
- end
- end
- end
- end
- local function update(cords)
- pos = vector.new(gps.locate())
- diff = pos:sub(cords)
- end
- local function go_to(cords)
- --- Cords is a table, from the vector API, in {x='x', y='y', z='z'}
- update(home_cords)
- while diff.x ~= 0 or diff.y ~= 0 or diff.z ~= 0 do
- pos = vector.new(gps.locate())
- -- For the x-field
- if diff.x < 0 then
- if turtle_face ~= 1 then
- while turtle_face ~= 1 do
- Right()
- end
- end
- while not turtle.detect() and diff.x < 0 and turtle_face == 1 do
- turtle.forward()
- update(cords)
- end
- elseif diff.x > 0 then
- if turtle_face ~= 3 then
- while turtle_face ~= 3 do
- Right()
- end
- end
- while not turtle.detect() and diff.x > 0 and turtle_face == 3 do
- turtle.forward()
- update(cords)
- end
- end
- -- For the y-field
- if diff.y < 0 then
- while not turtle.detectUp() and diff.y < 0 do
- turtle.up()
- update(cords)
- end
- elseif diff.y > 0 then
- while not turtle.detectDown() and diff.y > 0 do
- turtle.down()
- update(cords)
- end
- end
- -- For the z-field
- if diff.z < 0 then
- if turtle_face ~= 0 then
- while turtle_face ~= 0 do
- Right()
- end
- end
- while not turtle.detect() and diff.z < 0 and turtle_face == 0 do
- turtle.forward()
- update(cords)
- end
- elseif diff.z > 0 then
- if turtle_face ~= 2 then
- while turtle_face ~= 2 do
- Right()
- end
- end
- while not turtle.detect() and diff.z > 0 and turtle_face == 2 do
- turtle.forward()
- update(cords)
- end
- end
- end
- end
- -- Main functions
- -- Compare function
- local function drop(compare)
- --- Compare/sort/drop function
- --- To-do
- --- Use .getItemDetail(slot) / .size() instead of .list() for the
- --- display / support of nil slots
- -- If the turtle does not have to check if the current slot item is
- -- in it's inventory.
- if compare == false then
- -- For all the items, except fuel, drop it in the chest infront of it.
- for x=2,16 do
- turtle.select(x)
- turtle.drop()
- end
- -- The turtle does need to compare.
- else
- -- for the amount of unique blocks in the chest.
- if table_to_set(chest.list()) ~= nil then
- for i in pairs(table_to_set(chest.list())) do
- -- For the inventorty of a turtle.
- for x=2,16 do
- -- Select the slot
- turtle.select(x)
- -- If the slot and the inventory items match, drop the item in
- -- the chest
- if i ~= nil and turtle.getItemDetail() ~= nil then
- if i ==
- turtle.getItemDetail()['name'] then
- turtle.drop()
- end
- end
- end
- end
- end
- end
- turtle.select(1)
- end
- local function move()
- -- Move from 'home' to chests
- around()
- n_forward(2)
- -- If the chests are on it's right side, now they will be on the left.n_forward
- if chest_side == 'Right' then
- Left()
- turtle.forward()
- elseif chest_side == 'Left' then
- Right()
- turtle.forward()
- end
- -- Place the items in the right chests
- for x=1,chest_height do
- for y=1,chest_width do
- refuel()
- if chest_side == 'Left' then
- Right()
- drop(true)
- Left()
- elseif chest_side == 'Right' then
- Left()
- drop(true)
- Right()
- end
- turtle.forward()
- end
- turtle.up()
- around()
- if chest_side == 'Left' then
- chest_side = 'Right'
- else
- chest_side = 'Left'
- end
- end
- go_to(home_cords)
- end
- local function main()
- pos = vector.new(gps.locate())
- diff = pos:sub(home_cords)
- local inventory_full = true
- -- Check if the turtle is 'Home'
- if diff.x == 0 and diff.y == 0 and diff.z == 0 then
- -- Check fuel level and refuel if needed
- refuel()
- -- Check if the turtle's inventory is full
- for x=2,16 do
- turtle.select(x)
- if turtle.getItemDetail() == nil then
- inventory_full = false
- end
- end
- -- If there is space in the inventory don't sort but wait for a full
- -- inventory.
- while not inventory_full do
- inventory_full = pick_up()
- end
- move()
- end
- end
- -- Main loop
- while true do
- main()
- end
Add Comment
Please, Sign In to add comment