Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Requires imgui: 5AcWhbc8
- os.loadAPI('imgui')
- local function with(base, props)
- local new = {}
- for k, v in pairs(base) do
- new[k] = v
- end
- -- TODO: maybe?
- setmetatable(new, getmetatable(base))
- for k, v in pairs(props) do
- new[k] = v
- end
- return new
- end
- local function concat(a, b)
- local res = {}
- for i, val in ipairs(a) do
- res[#res + 1] = val
- end
- for i, val in ipairs(b) do
- res[#res + 1] = val
- end
- return res
- end
- local function disp(val, visited)
- if type(visited) ~= 'table' then
- visited = {}
- end
- if visited[val] == true then
- return 'Circular: ' .. tostring(val)
- elseif type(val) == 'string' then
- return textutils.serialize(val)
- elseif type(val) == 'number' or type(val) == 'boolean' then
- return tostring(val)
- elseif type(val) == 'table' then
- local subVisited = with(visited, {[val] = true})
- local str = tostring(val) .. ' -- {'
- local namedEntries = {}
- local numEntries = {}
- for i, v in ipairs(val) do
- numEntries[i] = disp(v, subVisited)
- end
- for k, v in pairs(val) do
- if type(numEntries[k]) ~= 'string' then
- namedEntries[#namedEntries + 1] = {k, disp(v, subVisited)}
- end
- end
- -- if #namedEntries == 0 and #numEntries <= 2 then
- -- str = str .. ' ' .. table.concat(numEntries, ', ') .. ' '
- -- else
- for i, v in ipairs(numEntries) do
- str = str .. '\t'
- for line in v:gmatch('([^\n\r]+)') do
- str = str .. '\n\t' .. line .. ''
- end
- str = str .. ','
- end
- for i, kv in ipairs(namedEntries) do
- local k = kv[1]
- local v = kv[2]
- if type(k) ~= 'string' or not k:match('^[%a_][%a%d_]*$') then
- k = '[' .. disp(k, subVisited) .. ']'
- end
- str = str .. '\n\t' .. k .. ' = '
- local first = true
- for line in v:gmatch('([^\n\r]+)') do
- if first then
- str = str .. line .. '\n'
- else
- str = str .. '\t' .. line .. '\n'
- end
- first = false
- end
- str = str:sub(1, -2) .. ';'
- end
- str = str .. '\n'
- -- end
- str = str .. '}'
- return str
- end
- end
- local function checkType(types)
- for i, itemType in ipairs(types) do
- if turtle.compareTo(itemType.slot) then
- return i
- end
- end
- local typesI = #types + 1
- types[typesI] = {
- slot = turtle.getSelectedSlot();
- qty = 0;
- slots = {};
- stacksize = turtle.getItemCount(turtle.getSelectedSlot()) + turtle.getItemSpace(turtle.getSelectedSlot());
- }
- return typesI
- end
- fs.open('log', 'w').close()
- local function log(...)
- local log = fs.open('log', 'a')
- log.write(table.concat({...}, ' ') .. '\n')
- log.close()
- end
- local allSlots = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
- local craftingSlots = {1,2,3,5,6,7,9,10,11}
- local ringSlots = {1,2,3,7,11,10,9,5}
- local outputSlot = 8
- local function getItems(slots)
- local types = {}
- local items = {}
- for index, i in ipairs(slots) do
- turtle.select(i)
- local itemType = types[checkType(types)]
- local qty = turtle.getItemCount(i)
- itemType.qty = itemType.qty + qty
- itemType.slots[i] = qty
- items[i] = {
- slot = i;
- qty = qty;
- type = itemType;
- }
- end
- return items, types
- end
- local width, height = term.getSize()
- local off = 0
- local buttonWidth = 5
- local cursorSlot = 6
- local n = nil
- local success = nil
- local function swirl()
- local items, types = getItems(ringSlots)
- if items[1].qty >= 1 and
- items[2].qty == 0 and
- items[3].qty == 0 and
- items[5].qty == 0 and
- items[7].qty == 0 and
- items[9].qty == 0 and
- items[10].qty == 0 and
- items[11].qty == 0 then
- turtle.select(1)
- local qty = items[1].qty
- local leftover = qty % 8
- local each = (qty - leftover) / 8
- local amts = {
- [2] = 1; --each + math.min(math.max(0, leftover - 1), 1);
- [3] = 1; --each + math.min(math.max(0, leftover - 2), 1);
- [5] = 1; --each + math.min(math.max(0, leftover - 3), 1);
- [7] = 1; --each + math.min(math.max(0, leftover - 4), 1);
- [9] = 1; --each + math.min(math.max(0, leftover - 5), 1);
- [10] = 1; --each + math.min(math.max(0, leftover - 6), 1);
- [11] = 1; --each + math.min(math.max(0, leftover - 7), 1);
- }
- -- print(disp(amts))
- for i, amt in pairs(amts) do
- if type(i) == 'number' then
- turtle.transferTo(i, amt)
- end
- end
- turtle.select(cursorSlot)
- elseif items[1].qty >= 1 and
- items[2].qty >= 1 and
- items[3].qty == 0 and
- items[5].qty == 0 and
- items[7].qty == 0 and
- items[9].qty == 0 and
- items[10].qty == 0 and
- items[11].qty == 0 then
- turtle.select(1)
- local qty = items[1].qty
- local leftover = qty % 4
- local each = (qty - leftover) / 4
- local amts = {
- [3] = 1; --each + math.min(math.max(0, leftover - 1), 1);
- [9] = 1; --each + math.min(math.max(0, leftover - 2), 1);
- [11] = 1; --each + math.min(math.max(0, leftover - 3), 1);
- }
- for i, amt in pairs(amts) do
- if type(i) == 'number' then
- turtle.transferTo(i, amt)
- end
- end
- turtle.select(2)
- local qty = items[2].qty
- local leftover = qty % 4
- local each = (qty - leftover) / 4
- local amts = {
- [5] = 1; --each + math.min(math.max(0, leftover - 1), 1);
- [7] = 1; --each + math.min(math.max(0, leftover - 2), 1);
- [10] = 1; --each + math.min(math.max(0, leftover - 3), 1);
- }
- for i, amt in pairs(amts) do
- if type(i) == 'number' then
- turtle.transferTo(i, amt)
- end
- end
- turtle.select(cursorSlot)
- else
- turtle.select(1); turtle.transferTo(16)
- turtle.select(5); turtle.transferTo(1)
- turtle.select(9); turtle.transferTo(5)
- turtle.select(10); turtle.transferTo(9)
- turtle.select(11); turtle.transferTo(10)
- turtle.select(7); turtle.transferTo(11)
- turtle.select(3); turtle.transferTo(7)
- turtle.select(2); turtle.transferTo(3)
- turtle.select(16); turtle.transferTo(2)
- turtle.select(cursorSlot)
- end
- end
- local function drop()
- for index, slot in ipairs(concat(craftingSlots, {outputSlot})) do
- turtle.select(slot)
- turtle.dropUp()
- end
- turtle.select(cursorSlot)
- end
- local function craft()
- turtle.select(outputSlot)
- if n == nil then
- success = turtle.craft()
- else
- success = turtle.craft(n)
- end
- turtle.select(cursorSlot)
- end
- local function balance()
- local items, types = getItems(craftingSlots)
- for i, itemType in ipairs(types) do
- if itemType.qty > 0 then
- local nSlots = 0
- for slot, qty in pairs(itemType.slots) do
- nSlots = nSlots + 1
- end
- local each = itemType.qty / nSlots
- for slot, qty in pairs(itemType.slots) do
- if qty > each then
- for lowerSlot, lowerQty in pairs(itemType.slots) do
- if lowerQty < each then
- turtle.select(slot)
- local transfer = math.min(qty - each, each - lowerQty)
- turtle.transferTo(lowerSlot, transfer)
- qty = qty - transfer
- if qty <= each then
- break
- end
- end
- end
- end
- end
- end
- end
- end
- local function fill()
- local items, types = getItems(craftingSlots)
- turtle.select(cursorSlot)
- for slot, item in pairs(items) do
- if item.qty == 0 then
- turtle.transferTo(item.slot, 1)
- end
- end
- end
- local function nButton(num, x, y)
- if imgui.button { text = tostring(num), x = x, y = y, bgColor = colors.green, height = 1, width = buttonWidth } then
- if n == nil then
- n = 0
- end
- n = (n * 10) + num
- end
- end
- local function gui()
- local str = 'Crafting Station'
- imgui.label {
- text = string.rep(' ', (width / 2) - (#str / 2) + 1) .. str .. string.rep(' ', (width / 2) - (#str / 2) + 1);
- x = 1;
- y = 1;
- bgColor = colors.gray;
- }
- if imgui.button { text = 'X', x = width, y = 1, bgColor = colors.gray, width = 1, height = 1 } then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- error('Exiting', 0)
- end
- for i = 2, height - 1 do
- imgui.label {
- text = string.rep(' ', width - 2);
- x = 2;
- y = i;
- bgColor = colors.lightGray;
- }
- end
- nButton(1, off + 3, 5)
- nButton(2, off + 3 + buttonWidth + 1, 5)
- nButton(3, off + 3 + buttonWidth * 2 + 2, 5)
- nButton(4, off + 3, 7)
- nButton(5, off + 3 + buttonWidth + 1, 7)
- nButton(6, off + 3 + buttonWidth * 2 + 2, 7)
- nButton(7, off + 3, 9)
- nButton(8, off + 3 + buttonWidth + 1, 9)
- nButton(9, off + 3 + buttonWidth * 2 + 2, 9)
- if imgui.button { text = '+1', x = off + 3, y = 11, bgColor = colors.green, height = 1, width = buttonWidth } then
- n = n + 1
- end
- nButton(0, off + 3 + buttonWidth + 1, 11)
- if imgui.button { text = '-1', x = off + 3 + buttonWidth * 2 + 2, y = 11, bgColor = colors.green, height = 1, width = buttonWidth } then
- n = n - 1
- end
- if imgui.button { text = 'All', x = off + 3 + buttonWidth * 3 + 3, y = 5, bgColor = colors.green, height = 1, width = buttonWidth } then
- n = nil
- end
- if imgui.button { text = 'Clear', x = off + 3 + buttonWidth * 4 + 4, y = 5, bgColor = colors.green, height = 1, width = buttonWidth + 2 } then
- n = 0
- end
- if imgui.button { text = 'Drop', x = off + 3 + buttonWidth * 3 + 3, y = 7, bgColor = colors.green, height = 1, width = buttonWidth } then
- drop()
- end
- if imgui.button { text = 'Swirl', x = off + 3 + buttonWidth * 4 + 4, y = 7, bgColor = colors.green, height = 1, width = buttonWidth + 2 } then
- swirl()
- end
- if imgui.button { text = 'Bal', x = off + 3 + buttonWidth * 3 + 3, y = 9, bgColor = colors.green, height = 1, width = buttonWidth } then
- balance()
- end
- if imgui.button { text = 'Fill', x = off + 3 + buttonWidth * 3 + 3, y = 11, bgColor = colors.green, height = 1, width = buttonWidth } then
- fill()
- end
- local craftColor
- if success then
- craftColor = colors.green
- success = nil
- elseif success == nil then
- craftColor = colors.gray
- else
- craftColor = colors.red
- end
- if imgui.button { text = 'Craft', x = off + 3 + buttonWidth * 4 + 4, y = 9, bgColor = craftColor, height = 3, width = buttonWidth * 2 } then
- craft()
- if n == 0 then
- os.queueEvent('update')
- end
- os.startTimer(0.5)
- end
- local nstr
- if n == nil then
- nstr = ' All'
- else
- nstr = ' ' .. tostring(n)
- end
- imgui.label {
- text = nstr .. string.rep(' ', width - 4 - #nstr);
- x = 3;
- y = 3;
- bgColor = colors.gray;
- }
- end
- turtle.select(cursorSlot)
- while true do
- gui()
- term.setBackgroundColor(colors.brown)
- term.clear()
- imgui.draw()
- turtle.select(cursorSlot)
- local ev = {imgui.pullEvent()}
- if ev[1] == 'key' then
- if ev[2] == keys.c then
- swirl()
- elseif ev[2] == keys.x then
- drop()
- elseif ev[2] == keys.b then
- balance()
- elseif ev[2] == keys.f then
- fill()
- elseif ev[2] == keys.enter then
- craft()
- elseif ev[2] == keys.a then
- n = nil
- elseif ev[2] == keys.left then
- if (cursorSlot - 1) % 4 ~= 0 then
- cursorSlot = cursorSlot - 1
- end
- turtle.select(cursorSlot)
- elseif ev[2] == keys.right then
- if (cursorSlot - 1) % 4 ~= 2 then
- cursorSlot = cursorSlot + 1
- end
- turtle.select(cursorSlot)
- elseif ev[2] == keys.up then
- if cursorSlot > 4 then
- cursorSlot = cursorSlot - 4
- end
- elseif ev[2] == keys.down then
- if cursorSlot < 8 then
- cursorSlot = cursorSlot + 4
- end
- end
- elseif ev[1] == 'char' then
- local num = tonumber(ev[2])
- if type(num) == 'number' then
- if n == nil then
- n = 0
- end
- n = (n * 10) + num
- end
- else
- log(textutils.serialize(ev))
- end
- turtle.select(cursorSlot)
- end
Advertisement
Add Comment
Please, Sign In to add comment