Advertisement
guitarplayer616

refillAPI

Jan 16th, 2017
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.14 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,inv)
  251.     local emptySlots = findEmptySlots(chest,inv)
  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 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.             --take
  323.            
  324.             if checklist_count <= qty then
  325.                 --take checklist_count amount
  326.                 take(chest_slot,checklist_count,me,chest)
  327.                 checklist_count = 0
  328.                 db(name,"    name")
  329.                 db(checklist_count,"    checklist_count")
  330.                 db(qty,"    qty")
  331.             elseif checklist_count>=64 then
  332.                 --take 64
  333.                 take(chest_slot,64,me,chest)
  334.                 checklist_count = checklist_count - 64
  335.                 db(name,"    name")
  336.                 db(checklist_count,"    checklist_count")
  337.                 db(qty,"    qty")
  338.             elseif checklist_count > qty then
  339.                 --take qty
  340.                 take(chest_slot,qty,me,chest)
  341.                 checklist_count = checklist_count - qty
  342.                 db(name,"    name")
  343.                 db(checklist_count,"    checklist_count")
  344.                 db(qty,"    qty")
  345.             else
  346.                 db(name,"    NOT COUNTED")
  347.                 db(damage)
  348.                 db(qty)
  349.             end
  350.            
  351.            
  352.             checklist[name][damage] = checklist_count
  353.         end
  354.     end
  355.     return checklist
  356. end
  357.  
  358. function check_chests(checklist,rubbishList)   
  359.     --if openperipherals
  360.     --if reference.multiturtle and reference.returnx and reference.returny and reference.returnz then
  361.         goto(reference.finalx+1,reference.returny,reference.returnz)
  362.         goto(reference.returnx,reference.returny,reference.returnz)
  363.     --else
  364.         --goto(heightPos,reference.starty,reference.startz-1)
  365.         --goto(reference.startx,reference.starty,reference.startz-1)
  366.     --end
  367.     local i = 1
  368.     local noInvo = 0
  369.     local numChests = 0
  370.     while true do
  371.         --if reference.multiturtle then
  372.             goto(reference.returnx,reference.returny,reference.returnz-i+1)
  373.         --else
  374.             --goto(reference.startx,reference.starty,reference.startz-i)
  375.         --end
  376.         --local inv = peripheral.call("bottom","getInventorySize")
  377.         --local invType = peripheral.call("bottom","getInventoryName")
  378.         local invType = false
  379.         local lookSee,lookData = turtle.inspectDown()
  380.         if lookSee then
  381.             if lookData.name:find("chest") then
  382.                 invType = true
  383.             elseif lookData.name:find("tile.BlockInterface") then
  384.                 invType = "TileInterface"
  385.             end
  386.         end
  387.  
  388.  
  389.         if invType then
  390.             local chest
  391.             if invType == "TileInterface" then
  392.                 chest = peripheral.call("bottom","getAvailableItems")
  393.             else
  394.                 chest = peripheral.call("bottom","getAllStacks")
  395.             end
  396.             --lookForFuel(chest)
  397.             --pushTrash(chest)
  398.             rubbishList = throwAway2(rubbishList,chest)
  399.             --pullBlocks(chest)
  400.             checklist = pullBlocks(checklist,chest,invType)
  401.             noInvo = 0
  402.             numChests = numChests + 1
  403.             --if (everything checked off) then
  404.                 --go back to building
  405.             --end
  406.             if invType == "TileInterface" and not reference.numChests then
  407.                 reference.numChests = numChests
  408.                 updateVar(reference.filename,"reference.numChests",numChest) --needs testing
  409.                 break
  410.             end
  411.            
  412.             if reference.numChests == numChests then
  413.                 --print("ok stop here")
  414.                 break
  415.             end
  416.            
  417.         else
  418.             noInvo = noInvo + 1
  419.             if noInvo == 2 and numChests == 0 then
  420.                 error("needs refill chest within 2 blocks behind start location under the turtle")
  421.             end
  422.             if noInvo == 3 then
  423.                 reference.numChests = numChests
  424.                 updateVar(reference.filename,"reference.numChests",numChest) --needs testing
  425.                 break
  426.             end
  427.         end
  428.        
  429.         i = i + 1
  430.     end
  431.     --print("stop")
  432.     save(rubbishList,"rubbishList")
  433.     save(checklist,"checklist")
  434.     --local x,y,z,id,data = unpack(instructions[nObjective])
  435.     --goto(reference.finalx+1,y,z)
  436. end
  437.  
  438.  
  439. --checklist = checklistMaker(1,instructions,slots)
  440. --checklist, rubbishList = subtractInventory(checklist)
  441.  
  442. --save(checklist,"checklist")
  443. --save(rubbishList,"rubbishList")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement