Advertisement
skypop

CC repair

Aug 1st, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.33 KB | None | 0 0
  1.  
  2. -- Repair
  3. -- by SukaiPoppuGo
  4. -- Require mod Plethora
  5. -- Program for crafty turtle
  6.  
  7. local temp = peripheral.wrap("front")
  8. local input = peripheral.wrap("bottom")
  9. local output = peripheral.wrap("top")
  10.  
  11. local w,h = term.getSize()
  12. local function printLine(...)
  13.     local x,y = term.getCursorPos()
  14.     term.setCursorPos(1,h)
  15.     term.clearLine()
  16.     term.write(table.concat({...}," "))
  17.     term.setCursorPos(x,y)
  18. end
  19.  
  20. local function _count(t)
  21.     local count = 0
  22.     for _,__ in pairs(t) do
  23.         count = count+1
  24.     end
  25.     return count
  26. end
  27.  
  28. local function distance(A, B)
  29.     return math.sqrt(math.pow(B - A, 2))
  30. end
  31.  
  32. local function keySort( t )
  33.     local tkeys = {}
  34.     for k in pairs( t ) do table.insert(tkeys, k) end -- populate the table that holds the keys
  35.     table.sort( tkeys ) -- sort the keys
  36.     local tTable = {}
  37.     for _, k in ipairs( tkeys ) do table.insert( tTable, t[k] ) end -- use the keys to retrieve the values in the sorted order
  38.     return tTable
  39. end
  40.  
  41. local function _select(slot)
  42.     if slot ~= turtle.getSelectedSlot() then
  43.         turtle.select(slot)
  44.     end
  45. end
  46.  
  47. local function detail(slot)
  48.     slot = slot or turtle.getSelectedSlot()
  49.     return turtle.getItemDetail(slot)
  50. end
  51.  
  52. local function clearInventory()
  53.     printLine("Clear inventory")
  54.     for slot = 1, 16 do
  55.         local item = detail(slot)
  56.         if item and item.damage > 0 then
  57.             _select(slot)
  58.             turtle.drop()
  59.         elseif item then
  60.             _select(slot)
  61.             turtle.dropUp()
  62.         end
  63.     end
  64.     _select(1)
  65. end
  66.  
  67. -- Get items whith max qty
  68. local function choose()
  69.     printLine("choose")
  70.     local score = {}
  71.     local maxScore = 0
  72.     local choice = "none"
  73.     for _,item in pairs(input.list()) do
  74.         score[item.name] = score[item.name] and score[item.name]+1 or 1
  75.         maxScore = math.max(maxScore, score[item.name])
  76.         if maxScore == score[item.name] then
  77.             choice = item.name
  78.         end
  79.     end
  80.     return choice, maxScore
  81. end
  82.    
  83.  
  84. local function buffering()
  85.     printLine("buffering")
  86.     local choice, score = choose()
  87.     local usetTemp = false
  88.    
  89.     local list = temp.list()
  90.     local listCount = _count(list)
  91.     if listCount > 0 and listCount >= score then
  92.         printLine("Use buffer chest")
  93.         useTemp = true
  94.         score = listCount
  95.         slot, item = next(list)
  96.         choice = item.name
  97.     else
  98.         printLine("Dump buffer chest")
  99.         for slot, item in pairs(list) do
  100.             temp.pushItems("down", slot)
  101.         end
  102.     end
  103.    
  104.     local count = useTemp and listCount or 0
  105.     printLine("Load buffer chest")
  106.     for slot, item in pairs(input.list()) do
  107.         if item and item.damage == 0 then
  108.             input.pushItems("top",slot,nil,turtle.getSelectedSlot())
  109.             turtle.dropUp()
  110.         elseif item.name == choice and item.damage > 0 then
  111.             input.pushItems("top",slot,nil,turtle.getSelectedSlot())
  112.             if not turtle.drop() then
  113.                 turtle.dropDown()
  114.                 break
  115.             end
  116.             count = count+1
  117.         end
  118.     end
  119.     return count > 0
  120. end
  121.  
  122. -- Sort by damage values
  123. local function sortBuffer()
  124.     local list = temp.list()
  125.     local tDamage = {}
  126.     local maxDamage
  127.     for slot, item in pairs(list) do
  128.         local damage = item.damage
  129.         maxDamage = item.maxDamage
  130.         if not tDamage[damage] then
  131.             tDamage[damage] = {}
  132.         end
  133.         table.insert(tDamage[damage], slot)
  134.     end
  135.     tDamage = keySort(tDamage)
  136.     local toSlot = temp.size()
  137.     for damage,slotList in pairs(tDamage) do
  138.         for i,slot in ipairs(slotList) do
  139.             local r = temp.pullItems("self", slot, 1, toSlot)
  140.             toSlot = toSlot-1
  141.         end
  142.     end
  143. end
  144.  
  145. --Place dans le premier slot l'item à la damage-_value la plus complémentaire
  146. local function pickupItem()
  147.     local ref = detail()
  148.     if not ref then
  149.         return false
  150.     end
  151.    
  152.     --ref.damage (nb de points manquants pour être réparé à neuf)
  153.     --ref.lifeDuration (nb de points restants)
  154.     local maxDamage = ref.damage + ref.lifeDuration
  155.    
  156.     local bestScore, bestSlot = math.huge, nil
  157.     for slot, item in pairs(temp.list()) do
  158.         local score = distance(item.damage + ref.damage, maxDamage)
  159.         bestScore = math.min(score, bestScore)
  160.         if bestScore == score then
  161.             bestSlot = slot
  162.         end
  163.         if score == 0 then
  164.             break
  165.         end
  166.     end
  167.     if not bestSlot then
  168.         return false
  169.     else
  170.         if temp.pullItems("self", bestSlot, 1, 1) then
  171.             return turtle.suck(1)
  172.         else
  173.             return false
  174.         end
  175.     end
  176. end
  177.  
  178. local function craft()
  179.     clearInventory()
  180.     printLine("Crafting")
  181.     _select(1)
  182.     sortBuffer()
  183.     if not turtle.suck(1) then
  184.         printLine("Main item not found")
  185.         return false
  186.     end
  187.     while pickupItem() do
  188.         if not turtle.craft() then
  189.             printLine("Fail to craft")
  190.             return false
  191.         end
  192.         local item = detail()
  193.         if item.damage == 0 then
  194.             printLine("Item repaired "..item.name)
  195.             clearInventory()
  196.             _select(1)
  197.             if not turtle.suck(1) then
  198.                 return true
  199.             end
  200.             printLine("Crafting")
  201.         end
  202.         sleep()
  203.     end
  204.     turtle.dropDown()
  205.     printLine("Craft ending")
  206. end
  207.  
  208. local function sortBow()
  209.     printLine("Sort bows")
  210.     for slot, item in pairs(output.list()) do
  211.         if item.name == "minecraft:bow" then
  212.             if not output.pushItems("top",slot) then
  213.                 printLine("Bow chest is full")
  214.                 return
  215.             end
  216.         end
  217.         sleep()
  218.     end
  219. end
  220.  
  221. local function repair()
  222.     clearInventory()
  223.     if buffering() then
  224.         local list = temp.list()
  225.         local listCount = _count(list)
  226.         if listCount < 2 then
  227.             printLine("Not enough items: "..listCount)
  228.         else
  229.             craft()
  230.             sortBow()
  231.         end
  232.     end
  233. end
  234.  
  235.  
  236. local function display(running)
  237.     running = running or false
  238.  
  239.     term.setCursorBlink(running)
  240.     if running then
  241.         term.setCursorPos(1,3)
  242.         term.clearLine()
  243.         term.blit("repair ","fffffff","0000000")
  244.         term.setCursorPos(2,4)
  245.         term.clearLine()
  246.         term.setCursorPos(1,5)
  247.         term.clearLine()
  248.         term.blit("Process","8888888","fffffff")
  249.         term.setCursorPos(4,7)
  250.         term.clearLine()
  251.     else
  252.         term.setCursorPos(1,3)
  253.         term.clearLine()
  254.         term.setCursorPos(2,4)
  255.         term.clearLine()
  256.         term.blit("\143\143\143\143\143\143\143\143\143","fffffffff","000000000")
  257.         term.setCursorPos(2,5)
  258.         term.clearLine()
  259.         term.blit("[ Start ]","777777777","000000000")
  260.         term.setCursorPos(4,7)
  261.         term.clearLine()
  262.     end
  263.     term.setTextColor(colors.white)
  264.    
  265.     local mon = peripheral.find("monitor")
  266.     if mon then
  267.         mon.setCursorBlink(false)
  268.         mon.clear()
  269.         mon.setTextScale(0.5)
  270.         if running then
  271.             mon.setCursorPos(1,1)
  272.             mon.blit("repair ","fffffff","0000000")
  273.             mon.setCursorPos(1,3)
  274.             mon.blit("Process","8888888","fffffff")
  275.             mon.setCursorPos(4,5)
  276.             mon.setCursorBlink(true)
  277.         else
  278.             mon.setCursorPos(1,3)
  279.             mon.blit("repair ","7777777","0000000")
  280.         end
  281.     end
  282. end
  283.  
  284. term.clear()
  285. term.setCursorPos(1,1)
  286. term.blit(
  287.     "Repair"..string.rep(" ", w-6),
  288.     string.rep("7", w),
  289.     string.rep("0", w)
  290. )
  291.  
  292. while true do
  293.     display(false)
  294.     printLine("Press [S] to start")
  295.     while true do
  296.         local e, p, cx, cy = os.pullEvent()
  297.         if e == "monitor_touch" then
  298.             break
  299.         elseif e == "key" and p == keys.s then
  300.             local e2, p2 = os.pullEvent("key_up")
  301.             if p == p2 then
  302.                 break
  303.             end
  304.         elseif e == "mouse_click" and cy >= 4 and cy <= 5 then
  305.             break
  306.         end
  307.         sleep()
  308.     end
  309.     display(true)
  310.     repair()
  311.     sleep()
  312. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement