Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Repair
- -- by SukaiPoppuGo
- -- Require mod Plethora
- -- Program for crafty turtle
- local temp = peripheral.wrap("front")
- local input = peripheral.wrap("bottom")
- local output = peripheral.wrap("top")
- local w,h = term.getSize()
- local function printLine(...)
- local x,y = term.getCursorPos()
- term.setCursorPos(1,h)
- term.clearLine()
- term.write(table.concat({...}," "))
- term.setCursorPos(x,y)
- end
- local function _count(t)
- local count = 0
- for _,__ in pairs(t) do
- count = count+1
- end
- return count
- end
- local function distance(A, B)
- return math.sqrt(math.pow(B - A, 2))
- end
- local function keySort( t )
- local tkeys = {}
- for k in pairs( t ) do table.insert(tkeys, k) end -- populate the table that holds the keys
- table.sort( tkeys ) -- sort the keys
- local tTable = {}
- for _, k in ipairs( tkeys ) do table.insert( tTable, t[k] ) end -- use the keys to retrieve the values in the sorted order
- return tTable
- end
- local function _select(slot)
- if slot ~= turtle.getSelectedSlot() then
- turtle.select(slot)
- end
- end
- local function detail(slot)
- slot = slot or turtle.getSelectedSlot()
- return turtle.getItemDetail(slot)
- end
- local function clearInventory()
- printLine("Clear inventory")
- for slot = 1, 16 do
- local item = detail(slot)
- if item and item.damage > 0 then
- _select(slot)
- turtle.drop()
- elseif item then
- _select(slot)
- turtle.dropUp()
- end
- end
- _select(1)
- end
- -- Get items whith max qty
- local function choose()
- printLine("choose")
- local score = {}
- local maxScore = 0
- local choice = "none"
- for _,item in pairs(input.list()) do
- score[item.name] = score[item.name] and score[item.name]+1 or 1
- maxScore = math.max(maxScore, score[item.name])
- if maxScore == score[item.name] then
- choice = item.name
- end
- end
- return choice, maxScore
- end
- local function buffering()
- printLine("buffering")
- local choice, score = choose()
- local usetTemp = false
- local list = temp.list()
- local listCount = _count(list)
- if listCount > 0 and listCount >= score then
- printLine("Use buffer chest")
- useTemp = true
- score = listCount
- slot, item = next(list)
- choice = item.name
- else
- printLine("Dump buffer chest")
- for slot, item in pairs(list) do
- temp.pushItems("down", slot)
- end
- end
- local count = useTemp and listCount or 0
- printLine("Load buffer chest")
- for slot, item in pairs(input.list()) do
- if item and item.damage == 0 then
- input.pushItems("top",slot,nil,turtle.getSelectedSlot())
- turtle.dropUp()
- elseif item.name == choice and item.damage > 0 then
- input.pushItems("top",slot,nil,turtle.getSelectedSlot())
- if not turtle.drop() then
- turtle.dropDown()
- break
- end
- count = count+1
- end
- end
- return count > 0
- end
- -- Sort by damage values
- local function sortBuffer()
- local list = temp.list()
- local tDamage = {}
- local maxDamage
- for slot, item in pairs(list) do
- local damage = item.damage
- maxDamage = item.maxDamage
- if not tDamage[damage] then
- tDamage[damage] = {}
- end
- table.insert(tDamage[damage], slot)
- end
- tDamage = keySort(tDamage)
- local toSlot = temp.size()
- for damage,slotList in pairs(tDamage) do
- for i,slot in ipairs(slotList) do
- local r = temp.pullItems("self", slot, 1, toSlot)
- toSlot = toSlot-1
- end
- end
- end
- --Place dans le premier slot l'item à la damage-_value la plus complémentaire
- local function pickupItem()
- local ref = detail()
- if not ref then
- return false
- end
- --ref.damage (nb de points manquants pour être réparé à neuf)
- --ref.lifeDuration (nb de points restants)
- local maxDamage = ref.damage + ref.lifeDuration
- local bestScore, bestSlot = math.huge, nil
- for slot, item in pairs(temp.list()) do
- local score = distance(item.damage + ref.damage, maxDamage)
- bestScore = math.min(score, bestScore)
- if bestScore == score then
- bestSlot = slot
- end
- if score == 0 then
- break
- end
- end
- if not bestSlot then
- return false
- else
- if temp.pullItems("self", bestSlot, 1, 1) then
- return turtle.suck(1)
- else
- return false
- end
- end
- end
- local function craft()
- clearInventory()
- printLine("Crafting")
- _select(1)
- sortBuffer()
- if not turtle.suck(1) then
- printLine("Main item not found")
- return false
- end
- while pickupItem() do
- if not turtle.craft() then
- printLine("Fail to craft")
- return false
- end
- local item = detail()
- if item.damage == 0 then
- printLine("Item repaired "..item.name)
- clearInventory()
- _select(1)
- if not turtle.suck(1) then
- return true
- end
- printLine("Crafting")
- end
- sleep()
- end
- turtle.dropDown()
- printLine("Craft ending")
- end
- local function sortBow()
- printLine("Sort bows")
- for slot, item in pairs(output.list()) do
- if item.name == "minecraft:bow" then
- if not output.pushItems("top",slot) then
- printLine("Bow chest is full")
- return
- end
- end
- sleep()
- end
- end
- local function repair()
- clearInventory()
- if buffering() then
- local list = temp.list()
- local listCount = _count(list)
- if listCount < 2 then
- printLine("Not enough items: "..listCount)
- else
- craft()
- sortBow()
- end
- end
- end
- local function display(running)
- running = running or false
- term.setCursorBlink(running)
- if running then
- term.setCursorPos(1,3)
- term.clearLine()
- term.blit("repair ","fffffff","0000000")
- term.setCursorPos(2,4)
- term.clearLine()
- term.setCursorPos(1,5)
- term.clearLine()
- term.blit("Process","8888888","fffffff")
- term.setCursorPos(4,7)
- term.clearLine()
- else
- term.setCursorPos(1,3)
- term.clearLine()
- term.setCursorPos(2,4)
- term.clearLine()
- term.blit("\143\143\143\143\143\143\143\143\143","fffffffff","000000000")
- term.setCursorPos(2,5)
- term.clearLine()
- term.blit("[ Start ]","777777777","000000000")
- term.setCursorPos(4,7)
- term.clearLine()
- end
- term.setTextColor(colors.white)
- local mon = peripheral.find("monitor")
- if mon then
- mon.setCursorBlink(false)
- mon.clear()
- mon.setTextScale(0.5)
- if running then
- mon.setCursorPos(1,1)
- mon.blit("repair ","fffffff","0000000")
- mon.setCursorPos(1,3)
- mon.blit("Process","8888888","fffffff")
- mon.setCursorPos(4,5)
- mon.setCursorBlink(true)
- else
- mon.setCursorPos(1,3)
- mon.blit("repair ","7777777","0000000")
- end
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- term.blit(
- "Repair"..string.rep(" ", w-6),
- string.rep("7", w),
- string.rep("0", w)
- )
- while true do
- display(false)
- printLine("Press [S] to start")
- while true do
- local e, p, cx, cy = os.pullEvent()
- if e == "monitor_touch" then
- break
- elseif e == "key" and p == keys.s then
- local e2, p2 = os.pullEvent("key_up")
- if p == p2 then
- break
- end
- elseif e == "mouse_click" and cy >= 4 and cy <= 5 then
- break
- end
- sleep()
- end
- display(true)
- repair()
- sleep()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement