Advertisement
Guest User

startup

a guest
Feb 23rd, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. prfCrystal = peripheral.wrap("crystallizer_1")
  2. prfChest = peripheral.wrap("container_chest_0")
  3. dirChest = "north"
  4. dirCrystal = "south"
  5.  
  6. function getInv(prfSource)
  7.   return prfSource.getAllStacks()
  8. end
  9.  
  10. function filterInv(prfSource, intId, intDmg)
  11.   intDmg = intDmg or 0
  12.   arrSource = getInv(prfSource)
  13.   arrInv = {}
  14.  
  15.   for i,v in pairs(arrSource) do
  16.     if (v["id"] == intId and v["dmg"] == intDmg) then
  17.       arrInv[i] = v["qty"]
  18.     end
  19.   end
  20.  
  21.   return arrInv
  22. end
  23.  
  24. function countId(prfSource, intId, intDmg) -- return the number of intId:intDmg in inventory prfSource
  25.   intCount = 0
  26.   intDmg = intDmg or 0
  27.   arrInv = getInv(prfSource)
  28.  
  29.   for i,v in pairs(arrInv) do
  30.     if (v["id"] == intId and v["dmg"] == intDmg) then
  31.       intCount = intCount + v["qty"]
  32.     end
  33.   end
  34.  
  35.   return intCount
  36. end
  37.  
  38. function filterInCrystallizer(prfSource, intItem, intDmg)
  39.   intDmg = intDmg or 0
  40.   arrContent = getInv(prfSource)
  41.   arrInv = {0,0,0,0,0}
  42.  
  43.   for i=1, 5 do
  44.     if arrContent[i] then
  45.       if (arrContent[i]["id"] == intItem and arrContent[i]["dmg"] == intDmg) then
  46.         arrInv[i] = arrContent[i]["qty"]
  47.       end
  48.     end
  49.   end
  50.  
  51.   return arrInv
  52. end
  53.  
  54. function inCrystallizer(prfSource)
  55.   arrContent = getInv(prfSource)
  56.   arrInv= {}
  57.   tmp = {}
  58.   for i=1, 5 do
  59.     if arrContent[i] then
  60.       tmp["id"] = arrContent[i]["id"]
  61.       tmp["dmg"] = arrContent[i]["dmg"]
  62.       arrInv[i] = tmp
  63.     end
  64.   end
  65.   return arrInv
  66. end
  67.  
  68. function equalize(prfSource, prfDest)
  69.   intQty = 0
  70.   intSplit = 0
  71.   intId = 0
  72.   intDmg = 0
  73.   intOdd = 0
  74.   intPullList = {0,0,0,0,0}
  75.  
  76.   destInv = inCrystallizer(prfDest)
  77.   prfSource.condenseItems()
  78.  
  79.   if table.getn(destInv) == 0 then
  80.     if table.getn(getInv(prfSource)) == 0 then return end
  81.     tgt = prfSource.getStackInSlot(1)
  82.     intId = tgt["id"]
  83.     intDmg = tgt["dmg"]
  84.    
  85.   else
  86.     for _,v in pairs(destInv) do
  87.       intId = v["id"]
  88.       intDmg = v["dmg"]
  89.     end
  90.   end
  91.  
  92.   intQty = countId(prfSource, intId, intDmg) + countId(prfDest, intId, intDmg)
  93.   intSplit = math.min(64, math.floor(intQty / 5))
  94.   if intSplit ~= 64 then
  95.     intOdd = intQty % 5
  96.   end
  97.  
  98.   term.clear()
  99.   print (intId .. ":" .. intDmg .. " " .. intQty .. " " .. intSplit .. "*5 + " .. intOdd)
  100.  
  101.   for i=1, 5 do
  102.     tmpPull = intSplit - filterInCrystallizer(prfDest, intId, intDmg)[i]
  103.     if i <= intOdd then tmpPull = tmpPull + 1 end
  104.    
  105.     sourceInv = filterInv(prfSource, intId, intDmg)
  106.     for j,k in pairs(sourceInv) do
  107.       tmpPull = tmpPull - prfDest.pullItem(dirCrystal, j, tmpPull, i)
  108.       if tmpPull == 0 then break end
  109.     end
  110.   end
  111.  
  112.  
  113.  
  114.  
  115.  
  116. end
  117.  
  118. -- main loop
  119. while true do
  120.   equalize(prfChest, prfCrystal)
  121.   sleep(0.5)
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement