Advertisement
guitarplayer616

refill peripheral fix

Jan 30th, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.75 KB | None | 0 0
  1. --slots = textutils.unserialize( fs.open("slots","r").readAll() )
  2. --nObjective = 1
  3. --instructions = textutils.unserialize( fs.open("instructions","r").readAll() )
  4.  
  5.  
  6.  
  7. --instructions[n] = {x,y,z,id,data}
  8.  
  9. function getLength(checklist)
  10.     local c = 0
  11.     for i,v in pairs(checklist) do
  12.         for k,l in pairs(v) do
  13.             if l then
  14.                 c = c + 1
  15.             end
  16.         end
  17.     end
  18.     return c
  19. end
  20.  
  21. function getSlotsUsed(checklist)
  22.     local c = 0
  23.     local count = 0
  24.     for id,table in pairs(checklist) do
  25.         for data,count in pairs(table) do
  26.             c = c + math.ceil(count/64)
  27.         end
  28.     end
  29.     return c
  30. end
  31.  
  32. function sort()
  33.     items = {}
  34.     for i = 1,16 do
  35.         local item = turtle.getItemDetail(i)
  36.         if item then
  37.             local pSlot = nil
  38.             if items[item.name] then
  39.                 pSlot = items[item.name][item.damage]
  40.             end
  41.             if pSlot then
  42.                 turtle.select(i)
  43.                 turtle.transferTo(pSlot)
  44.                 if turtle.getItemCount(pSlot) == 64 then
  45.                     if turtle.getItemCount(i) > 0 then
  46.                         items[item.name][item.damage] = i
  47.                     else
  48.                         items[item.name][item.damage] = nil
  49.                     end
  50.                 end
  51.             elseif turtle.getItemCount(i) < 64 then
  52.                 if not items[item.name] then
  53.                     items[item.name] = {}
  54.                 end
  55.                 items[item.name][item.damage] = i  
  56.             end
  57.         end
  58.     end
  59. end
  60.  
  61. local function add2checklist(checklist,name,damage,slots_available)
  62.     --checklist[id][data] = count version
  63.     --INPUT: item, checklist
  64.     --OUTPUT: added to checklist if there's space
  65.     slots_available = slots_available or 15
  66.     local numCheckList = getSlotsUsed(checklist)
  67.    
  68.     if name == nil then
  69.         return checklist
  70.     end
  71.    
  72.     if checklist[name] and checklist[name][damage] then
  73.         checklist[name][damage] = checklist[name][damage] + 1
  74.         if getSlotsUsed(checklist) > slots_available then
  75.             checklist[name][damage] = checklist[name][damage] - 1
  76.             return checklist,true
  77.         else
  78.             return checklist
  79.         end
  80.     end
  81.    
  82.     if numCheckList < slots_available then
  83.         if checklist[name] then
  84.             checklist[name][damage] = 1
  85.         else
  86.             checklist[name] = {}
  87.             checklist[name][damage] = 1
  88.         end
  89.     else
  90.         --error("checklist full")
  91.         return checklist,true
  92.     end
  93.     return checklist
  94. end
  95.  
  96. function checklistMaker(nObjective,instructions,slots)
  97.     --INPUT: nObjective,instructions,slots
  98.     --OUTPUT: next 15 invo spaces worth of blocks required in checklist[id][data] = count
  99.     local checklist = {}
  100.    
  101.     for i = nObjective,#instructions do
  102.         local name,damage  = unpack(slots[ instructions[i][4] ][ instructions[i][5] ])
  103.         checklist,bFull = add2checklist(checklist,name,damage,15)
  104.         if bFull then
  105.             return checklist
  106.         end
  107.     end
  108.  
  109.     return checklist
  110. end
  111.  
  112. local function insert_rubbish(rubbishList,id,data,count)
  113.     if not id then
  114.             return
  115.     end
  116.     if not rubbishList[id] then
  117.         rubbishList[id]= {}
  118.     end
  119.     if not rubbishList[id][data] then
  120.         rubbishList[id][data] = 0
  121.     end
  122.     rubbishList[id][data] = rubbishList[id][data] + count
  123.     return rubbishList
  124. end
  125.  
  126. local function flatten_checklist(checklist,rubbishList)
  127.     --get rid of negative count in checklist
  128.     for id,table in pairs(checklist) do
  129.         for data,count in pairs(table) do
  130.             if count < 0 then
  131.                 rubbishList = insert_rubbish(rubbishList,id,data,math.abs(count))
  132.                 checklist[id][data] = 0
  133.             end
  134.         end
  135.     end
  136.     return checklist,rubbishList
  137. end
  138.  
  139. function subtractInventory(checklist)
  140.     local rubbishList = {}
  141.     for i = 1,16 do
  142.         local deets = turtle.getItemDetail(i)
  143.        
  144.         if deets then
  145.             if checklist[deets.name] and checklist[deets.name][deets.damage] then
  146.                 checklist[deets.name][deets.damage] = checklist[deets.name][deets.damage] - deets.count
  147.             elseif (not deets.name:lower():find("wrench")) and (not deets.name:lower():find("hammer")) then
  148.             --not a wrench, not on the list
  149.                 insert_rubbish(rubbishList,deets.name,deets.damage,deets.count)
  150.             end
  151.         end
  152.     end
  153.     checklist,rubbishList = flatten_checklist(checklist,rubbishList)
  154.     return checklist,rubbishList
  155. end
  156.  
  157. function find_in_turtle(id,data)
  158.     for i = 1,16 do
  159.         local deets = turtle.getItemDetail(i)
  160.         if deets and (deets.name == id and deets.damage == data) then
  161.             return i,deets.count
  162.         end
  163.     end
  164.     return nil
  165. end
  166.  
  167. function throw_away(rubbishList,intoSlot)
  168.     for id,table in pairs(rubbishList) do
  169.         for data,count in pairs(table) do
  170.             if count > 0 then
  171.                 local slot, amount = find_in_turtle(id,data)
  172.                 if amount >= count then
  173.                     --peripheral.call("bottom","pushItem","down",slot,count,intoSlot)
  174.                     rubbishList[id][data] = 0
  175.                 else
  176.                     local mem = turtle.getSelectedSlot()
  177.                     while amount < count do
  178.                         local i,amount_transferred = find_in_turtle(id,data)
  179.                         if i then
  180.                             turtle.select(i)
  181.                             turtle.transferTo(mem)
  182.                             local remainder = turtle.getItemCount()
  183.                             amount = amount + (amount_transferred-remainder)
  184.                         end
  185.                        
  186.                     end
  187.                     turtle.select(mem)
  188.                 end
  189.             --pushItem(direction,slot,maxAmount?,intoSlot?)
  190.             peripheral.call("bottom","pushItem","down",slot,amount,intoSlot)
  191.             end
  192.         end
  193.     end
  194. end
  195.  
  196.  
  197.  
  198. function save(table,filename)
  199.     local h = fs.open(filename,"w")
  200.     if fs.exists("textutilsFIX") then
  201.         os.loadAPI("textutilsFIX")
  202.         h.write(textutilsFIX.serialize(table))
  203.     else
  204.         h.write(textutils.serialize(table))
  205.     end
  206.     h.close()
  207. end
  208. --throwAway file
  209.  
  210. function db(entity,name)
  211.     local h = fs.open("console","a")
  212.     name = name or ""
  213.     os.loadAPI("textutilsFIX")
  214.     if type(entity) == "table" then
  215.         h.writeLine(name..": "..textutilsFIX.serialize(table))
  216.     else
  217.         h.writeLine(name..": "..tostring(entity))
  218.     end
  219.     h.close()
  220. end
  221.  
  222. function find_in_turtle(id,data)
  223.     for i = 1,16 do
  224.         local deets = turtle.getItemDetail(i)
  225.         if deets and (deets.name == id and deets.damage == data) then
  226.             return i,deets.count
  227.         end
  228.     end
  229.     return nil
  230. end
  231.  
  232. function findEmptySlots(chest)
  233.     local randomTable = {}
  234.     for i,v in pairs(chest) do
  235.         table.insert(randomTable,i)
  236.     end
  237.     return #randomTable
  238. end
  239.  
  240. function findEmptySlotsOLD(chest,inv)
  241.     local emptySlots = 0
  242.     for i = 1,inv do
  243.         if chest[i] == nil then
  244.             emptySlots = emptySlots + 1
  245.         end
  246.     end
  247.     return emptySlots
  248. end
  249.  
  250. function throwAway2(rubbishList,chest,invType)
  251.     local emptySlots = findEmptySlots(chest)
  252.     assert(emptySlots)
  253.     --print("emptySlots = ",emptySlots)
  254.     for i = 1,16 do
  255.         local count = nil
  256.         local deets = turtle.getItemDetail(i)
  257.         if rubbishList and deets and rubbishList[deets.name] then
  258.             count = rubbishList[deets.name][deets.damage]
  259.         end
  260.         if (count and count > 0) and (emptySlots > 0 or invType == "TileInterface") then
  261.             turtle.select(i)
  262.             local amount = turtle.getItemCount(i)
  263.             if amount > count then
  264.                 turtle.dropDown(count)
  265.                 count = 0
  266.             elseif count <= 64 then
  267.                 turtle.dropDown(count)
  268.                 count = 0
  269.             else
  270.                 turtle.dropDown()
  271.                 count = count - 64
  272.             end
  273.             emptySlots = emptySlots - 1
  274.             rubbishList[deets.name][deets.damage] = count
  275.         end
  276.     end
  277.     return rubbishList
  278. end
  279.  
  280. function take(chest_slot,amount,me,chest)
  281.     sort()
  282.     if me then
  283.         peripheral.call("bottom","exportItem",chest[chest_slot].fingerprint,"up",amount)
  284.     else
  285.         peripheral.call("bottom","pushItem","up",chest_slot,amount)
  286.     end
  287. end
  288.  
  289. function take2(chest_slot,amount)
  290.     assert(chest_slot)
  291.     amount = amount or 64
  292.     peripheral.call("bottom","swapStacks",chest_slot,1)
  293.     turtle.suckDown(amount)
  294. end
  295.  
  296. function pullBlocks(checklist,chest,invType)
  297.     local me = false
  298.     if invType == "TileInterface" then
  299.         me = true
  300.     end
  301.     assert(checklist)
  302.     assert(chest)
  303.     for chest_slot,func in pairs(chest) do
  304.         local name,damage,qty
  305.         if me then
  306.             name = func.fingerprint.id
  307.             damage = func.fingerprint.dmg
  308.             qty = func.size
  309.         else
  310.             name = func.basic().id
  311.             db(name,chest_slot)
  312.             damage = func.basic().dmg
  313.             db(damage,"dmg")
  314.             qty = func.basic().qty
  315.             db(qty,"qty")
  316.         end
  317.         local checklist_count = nil
  318.         if checklist and checklist[name] then
  319.             checklist_count = checklist[name][damage]
  320.         end
  321.         if checklist_count and checklist_count > 0 then
  322.             if checklist_count <= qty then
  323.                 --take checklist_count amount
  324.                 take(chest_slot,checklist_count,me,chest)
  325.                 checklist_count = 0
  326.                 db(name,"    name")
  327.                 db(checklist_count,"    checklist_count")
  328.                 db(qty,"    qty")
  329.             elseif checklist_count>=64 then
  330.                 --take 64
  331.                 take(chest_slot,64,me,chest)
  332.                 checklist_count = checklist_count - 64
  333.                 db(name,"    name")
  334.                 db(checklist_count,"    checklist_count")
  335.                 db(qty,"    qty")
  336.             elseif checklist_count > qty then
  337.                 --take qty
  338.                 take(chest_slot,qty,me,chest)
  339.                 checklist_count = checklist_count - qty
  340.                 db(name,"    name")
  341.                 db(checklist_count,"    checklist_count")
  342.                 db(qty,"    qty")
  343.             else
  344.                 db(name,"    NOT COUNTED")
  345.                 db(damage)
  346.                 db(qty)
  347.             end
  348.            
  349.            
  350.             checklist[name][damage] = checklist_count
  351.         end
  352.     end
  353.     return checklist
  354. end
  355.  
  356. function check_chests(checklist,rubbishList)   
  357.     goto(reference.finalx+1,reference.returny,reference.returnz)
  358.     goto(reference.returnx,reference.returny,reference.returnz)
  359.  
  360.     local i = 1
  361.     local noInvo = 0
  362.     local numChests = 0
  363.     while true do
  364.         goto(reference.returnx,reference.returny,reference.returnz-i+1)    
  365.  
  366.         local invType = false
  367.         local lookSee,lookData = turtle.inspectDown()
  368.         if lookSee then
  369.             if lookData.name:find("chest") then
  370.                 invType = true
  371.             elseif lookData.name:find("tile.BlockInterface") then
  372.                 invType = "TileInterface"
  373.             end
  374.         end
  375.  
  376.  
  377.         if invType then
  378.             local bool,chest
  379.             if invType == "TileInterface" then
  380.                 bool,chest = pcall(peripheral.call,"bottom","getAvailableItems")
  381.             else
  382.                 bool,chest = pcall(peripheral.call,"bottom","getAllStacks")
  383.             end
  384.             if not bool then
  385.                 print(chest)
  386.                 if invType == "TileInterface" then
  387.                     turtle.select(1)
  388.                     turtle.dropDown(64)
  389.                     turtle.digDown()
  390.                     turtle.placeDown()
  391.                     sleep(0.5)
  392.                 end
  393.                 os.reboot()
  394.             end
  395.             rubbishList = throwAway2(rubbishList,chest,invType)
  396.             checklist = pullBlocks(checklist,chest,invType)
  397.             noInvo = 0
  398.             numChests = numChests + 1
  399.             if invType == "TileInterface" and not reference.numChests then
  400.                 reference.numChests = numChests
  401.                 updateVar(reference.filename,"reference.numChests",numChest)
  402.                 break
  403.             end
  404.            
  405.             if reference.numChests == numChests then
  406.                 --print("ok stop here")
  407.                 break
  408.             end
  409.            
  410.         else
  411.             noInvo = noInvo + 1
  412.             if noInvo == 2 and numChests == 0 then
  413.                 error("needs refill chest within 2 blocks behind start location under the turtle")
  414.             end
  415.             if noInvo == 3 then
  416.                 reference.numChests = numChests
  417.                 updateVar(reference.filename,"reference.numChests",numChest) --needs testing
  418.                 break
  419.             end
  420.         end
  421.        
  422.         i = i + 1
  423.     end
  424.     save(rubbishList,"rubbishList")
  425.     save(checklist,"checklist")
  426.     --local x,y,z,id,data = unpack(instructions[nObjective])
  427.     --goto(reference.finalx+1,y,z)
  428. end
  429.  
  430.  
  431. --checklist = checklistMaker(1,instructions,slots)
  432. --checklist, rubbishList = subtractInventory(checklist)
  433.  
  434. --save(checklist,"checklist")
  435. --save(rubbishList,"rubbishList")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement