Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local function addOrInsert(tab, ident, value)
- if tab[ident] == nil then
- tab[ident] = { value }
- else
- table.insert(tab[ident], value)
- end
- end
- local function findRecipes(side)
- local chest = peripheral.wrap(side)
- local data = {
- ["totals"] = {
- ["ccq"] = 0,
- ["block"] = 0,
- ["damaged"] = 0,
- ["chipped"] = 0
- }
- }
- for i=1, chest.size() do
- local details = chest.getItemDetail(i)
- if details ~= nil then
- if details.name == "ae2:charged_certus_quartz_crystal" then
- addOrInsert(data, "ccq", {["slot"] = i, ["count"] = details.count})
- elseif details.name == "ae2:quartz_block" then
- addOrInsert(data, "block", {["slot"] = i, ["count"] = details.count})
- elseif details.name == "ae2:damaged_budding_quartz" then
- addOrInsert(data, "damaged", {["slot"] = i, ["count"] = details.count})
- elseif details.name == "ae2:chipped_budding_quartz" then
- addOrInsert(data, "chipped", {["slot"] = i, ["count"] = details.count})
- end
- end
- end
- if data.ccq ~= nil and #data.ccq > 0 then
- for k,v in ipairs(data.ccq) do
- data.totals.ccq = data.totals.ccq + v.count
- end
- end
- if data.block ~= nil and #data.block > 0 then
- for k,v in ipairs(data.block) do
- data.totals.block = data.totals.block + v.count
- end
- end
- if data.damaged ~= nil and #data.damaged > 0 then
- for k,v in ipairs(data.damaged) do
- data.totals.damaged = data.totals.damaged + v.count
- end
- end
- if data.chipped ~= nil and #data.chipped > 0 then
- for k,v in ipairs(data.chipped) do
- data.totals.chipped = data.totals.chipped + v.count
- end
- end
- return data
- end
- local function suckFromSlot(fromSide, toSide, slot, count)
- local chest = peripheral.wrap(fromSide)
- chest.pushItems(toSide, slot, count, 1)
- if toSide == "top" then
- turtle.suckUp(count)
- elseif toSide == "bottom" then
- turtle.suckDown(count)
- else
- turtle.suck(count)
- end
- end
- local function grabResources(data, level, fromSide, toSide)
- local amount = math.min(data.totals.ccq, data.totals[level], 64)
- turtle.select(1)
- local currentCcqAmount = amount
- for k,v in ipairs(data.ccq) do
- if currentCcqAmount <= 0 then
- break
- end
- local suckAmount = math.min(v.count, currentCcqAmount)
- suckFromSlot(fromSide, toSide, v.slot, suckAmount)
- currentCcqAmount = currentCcqAmount - suckAmount
- end
- turtle.select(2)
- local currentAmount = amount
- for k,v in ipairs(data[level]) do
- if currentAmount <= 0 then
- break
- end
- local suckAmount = math.min(v.count, currentAmount)
- suckFromSlot(fromSide, toSide, v.slot, suckAmount)
- currentAmount = currentAmount - suckAmount
- end
- return amount
- end
- local function turn(direction, inverted)
- if direction == "left" then
- if inverted then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- elseif direction == "right" then
- if inverted then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- end
- end
- local function craft(amount, direction)
- if amount > 0 then
- turn(direction, false)
- for i=1, amount do
- turtle.select(1)
- turtle.drop(1)
- turtle.select(2)
- turtle.drop(1)
- turtle.select(3)
- sleep(4)
- turtle.suck(1)
- end
- turn(direction, true)
- turtle.select(3)
- turtle.drop(amount)
- end
- end
- local function run(fromSide, toSide, direction)
- local data = findRecipes("front")
- local amount = 0
- if data.totals.ccq > 0 then
- if data.totals.chipped > 0 then
- amount = grabResources(data, "chipped", fromSide, toSide)
- elseif data.totals.damaged > 0 then
- amount = grabResources(data, "damaged", fromSide, toSide)
- elseif data.totals.block > 0 then
- amount = grabResources(data, "block", fromSide, toSide)
- end
- craft(amount, direction)
- end
- end
- while true do
- run(args[1], args[2], args[3])
- sleep(5)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement