Advertisement
rhn

ItemSortStore

rhn
Mar 24th, 2020
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.16 KB | None | 0 0
  1. ---------------------------------------------Definition---------------------------------------------------------------
  2. shutdown=false --initialization of shutdown parameter
  3.  
  4. orelist={{"minecraft:iron_ore",0},{"minecraft:gold_ore",0},{"immersiveengineering:ore",0},{"immersiveengineering:ore",1},{"immersiveengineering:ore",2},{"immersiveengineering:ore",4},{"immersiveengineering:ore",5},{"projectred-exploration:ore",3},{"projectred-exploration:ore",4},{"projectred-exploration:ore",5},{"railcraft:ore_metal",0},{"railcraft:ore_metal",1},{"railcraft:ore_metal",2},{"railcraft:ore_metal",3},{"railcraft:ore_metal",4},{"railcraft:ore_metal",5}}
  5.  
  6. ignorelist={{"thaumcraft:crystal_essence",0}}
  7.  
  8. ---------------------------------------------First time user entry:---------------------------------------------------------------
  9. --Check if this is first time run, no saved choices on file
  10. if (fs.exists("choices") == false) then
  11.     while true do
  12.             print("Please input pastebin ID of modemlist")
  13.  
  14.             local answer = read()
  15.             if #answer~=8 then
  16.                 print("-Wrong answer, not correct length of pastebin ID-")
  17.             else
  18.                     pasteID=answer
  19.  
  20.                     break
  21.             end
  22.     end
  23. --Save choices to file for future restarts
  24. local file = fs.open("choices","w")
  25. file.writeLine("pasteID=".."\""..pasteID.."\"")
  26. file.close()
  27. end
  28.  
  29. -- Read choices from file
  30. os.loadAPI("choices")
  31. pasteID = choices.pasteID
  32. --print(pasteID)
  33.  
  34. --Load network modem IDs from pastebin file
  35. if (fs.exists("modemfile") == true) then
  36.     shell.run("delete","modemfile")
  37. end
  38. shell.run("pastebin","get",pasteID,"modemfile")--pasteID
  39. os.loadAPI("modemfile") -- Reads choices from file
  40. local storetype = modemfile.storetype --type of single item containers used for bulk storage
  41. local modemlist = modemfile.modemlist
  42. local storetypeRandom=modemfile.storetypeRandom --type of multi item containers used for random storage
  43. local modemlistRandom=modemfile.modemlistRandom
  44.  
  45. --Combine modemlist with full network name
  46. for i=1,#modemlist do
  47.     for j=1,#modemlist[1] do
  48.         modemlist[i][j]=storetype..tostring(modemlist[i][j])
  49.         --print(modemlist[i][j])
  50.     end
  51. end
  52. for i=1,#modemlistRandom do
  53.     for j=1,#modemlistRandom[1] do
  54.         modemlistRandom[i][j]=storetypeRandom..tostring(modemlistRandom[i][j])
  55.         --print(modemlistRandom[i][j])
  56.     end
  57. end
  58.  
  59. ---------------------------------------------Peripheral wrap---------------------------------------------------------------
  60. --List all networked peripherals
  61. local peripheralList = peripheral.getNames()
  62. --Initialize crate modem peripheral list
  63. local cratemodem={}
  64. for i=1,#modemlist do
  65.     cratemodem[i]={}
  66. end
  67. --wrap crate modems
  68. for i=1,#modemlist do
  69.     for j=1,#modemlist[1] do
  70.         for index = 1, #peripheralList do
  71.             if peripheralList[index] == modemlist[i][j] then --scan to see if names match
  72.                 cratemodem[i][j]=peripheral.wrap(peripheralList[index]) --wrap crate modems to array
  73.             end
  74.         end
  75.     end
  76. end
  77. --Initialize chest modem peripheral list
  78. local chestmodem={}
  79. for i=1,#modemlistRandom do
  80.     chestmodem[i]={}
  81. end
  82. --wrap chest modems
  83. --print(textutils.serialize(modemlistRandom))
  84.  
  85. for i=1,#modemlistRandom do
  86.     for j=1,#modemlistRandom[1] do
  87.         for index = 1, #peripheralList do
  88.             if peripheralList[index] == modemlistRandom[i][j] then --scan to see if names match
  89.                 chestmodem[i][j]=peripheral.wrap(peripheralList[index]) --wrap chest modems to array
  90.             end
  91.         end
  92.     end
  93. end
  94. --print(textutils.serialize(chestmodem))
  95.  
  96. --Wrap inputchest:
  97. inputChest=peripheral.wrap("top")
  98.  
  99. ---------------------------------------------Functions---------------------------------------------------------------
  100. local function StorageScan() --Scan crates and build array of items
  101. -- Initialize item information tables
  102. local itemMapName={}
  103. local itemMapCount={}
  104. local itemMapDamage={}
  105. for i=1,#modemlist do
  106.     itemMapName[i]={}
  107.     itemMapCount[i]={}
  108.     itemMapDamage[i]={}
  109. end
  110. --Start scan
  111. for i=1,#cratemodem do
  112.     for j=1,#cratemodem[1] do
  113.         local iteminfo=cratemodem[i][j].list()
  114.         if  iteminfo[1]~= nil then
  115.             itemMapName[i][j]=iteminfo[1].name
  116.             itemMapCount[i][j]=iteminfo[1].count
  117.             itemMapDamage[i][j]=iteminfo[1].damage
  118.         else
  119.             itemMapName[i][j]="noitem"
  120.             itemMapCount[i][j]=0
  121.             itemMapDamage[i][j]=0
  122.         end
  123.         --print(i..","..j..": "..itemMapCount[i][j].." "..itemMapName[i][j].."/"..itemMapDamage[i][j])
  124.     end
  125. end
  126. return itemMapName,itemMapCount,itemMapDamage
  127. end
  128.  
  129.  
  130. local function StorageScanRandom()
  131. local itemMapNameRandom={}
  132. local itemMapCountRandom={}
  133. local itemMapDamageRandom={}
  134.  
  135. --Initialize "3d" tables
  136. for i=1,#modemlistRandom do
  137.         itemMapNameRandom[i]={}
  138.         itemMapCountRandom[i]={}
  139.         itemMapDamageRandom[i]={}
  140. end
  141. for i=1,#modemlistRandom do
  142.     for j=1,#modemlistRandom[1] do
  143.         itemMapNameRandom[i][j]={}
  144.         itemMapCountRandom[i][j]={}
  145.         itemMapDamageRandom[i][j]={}
  146.     end
  147. end
  148. --save name, count and damage for all items in random storage
  149. for i=1,#chestmodem do
  150.     for j=1,#chestmodem[1] do
  151.         chestlist=chestmodem[i][j].list()
  152.         for k=1,chestmodem[i][j].size() do
  153.             if  chestlist[k]~= nil then
  154.                 itemMapNameRandom[i][j][k]=chestlist[k].name
  155.                 itemMapCountRandom[i][j][k]=chestlist[k].count
  156.                 itemMapDamageRandom[i][j][k]=chestlist[k].damage
  157.             else
  158.                 itemMapNameRandom[i][j][k]="noitem"
  159.                 itemMapCountRandom[i][j][k]=0
  160.                 itemMapDamageRandom[i][j][k]=0
  161.             end
  162.         end
  163.     end
  164. end
  165. --print(textutils.serialize(itemMapNameRandom))
  166. return itemMapNameRandom,itemMapCountRandom,itemMapDamageRandom
  167. end --end function
  168.  
  169.  
  170. --Find first empty in crate
  171. local function findEmpty(itemMapName)
  172.     for j=1,#itemMapName[1] do
  173.         for i=1,#itemMapName do
  174.             if itemMapName[i][j] == "noitem" then
  175.                 return i,j,true
  176.             end
  177.         end
  178.     end
  179.     return 0,0,false
  180. end
  181.  
  182.  
  183. --Find first empty in chest
  184. local function findEmptyRandom(itemMapNameRandom)
  185.     for j=1,#itemMapNameRandom[1] do
  186.         for i=1,#itemMapNameRandom do
  187.             for k=1,#itemMapNameRandom[1][1] do
  188.                 if itemMapNameRandom[i][j][k] == "noitem" then
  189.                     return i,j,k,true
  190.                 end
  191.             end
  192.         end
  193.     end
  194.     return 0,0,0,false
  195. end
  196.  
  197.  
  198. ---------------------------------------------Rednet prep---------------------------------------------------------------
  199. --Rednet initialization
  200. rednet.open("right")
  201. sleep(10) --Wait for turtles
  202. --Find rednet IDs for crate turtles
  203. local IDs={}
  204. if rednet.lookup("ItemSortCrate") then
  205.     for i=1,#modemlist do
  206.         IDs[i]=rednet.lookup("ItemSortCrate","row"..i)
  207.         --print(IDs[i])
  208.     end
  209. else
  210.     print("No crate turtles found, shutting down!")
  211.     shutdown=true
  212. end
  213. --Find IDs for random turtles
  214. local Randomsearch={rednet.lookup("ItemSortRandom")}
  215. if Randomsearch~=nil then
  216.     IDsRandom={}
  217.     for i=1,#Randomsearch do
  218.         if rednet.lookup("ItemSortRandom","random"..i) then
  219.             IDsRandom[i]=Randomsearch[i]
  220.             print("Random turtle "..tostring(IDsRandom[i]).." found")
  221.         end
  222.     end
  223. else
  224. print("No random storage turtles found")
  225. end
  226.  
  227.  
  228. ---------------------------------------------Execution---------------------------------------------------------------
  229. if shutdown==false then
  230.     print("Starting sorting operation")
  231. end
  232.  
  233. -- Main loop
  234. while shutdown==false do
  235.  
  236. if #inputChest.list() > 0 then --test if anything in inputchest
  237.  
  238. --print("Starting crate sort")
  239. --Scan network for items and build arrays of names and count
  240. itemMapName,itemMapCount,itemMapDamage = StorageScan()
  241.  
  242.  
  243. --Scan inputchest
  244.  
  245. --print("Input chest contents:")
  246. local sortlist={}  -- Format: sortlist={{itemname,damage,count},{itemname,damage,count},...}
  247. local sortlistRandom={}
  248. local sortcounter=1
  249. local sortcounterRandom=1
  250. for i=1,inputChest.size() do
  251.     local ignored=false
  252.     local ore=false
  253.     inputItem=inputChest.getItemMeta(i)
  254.     if  inputItem~= nil then
  255.         --Test if on ignorelist:
  256.         if #ignorelist>0 then
  257.             for j=1,#ignorelist do
  258.                 if ignorelist[j][1]==inputItem.name and ignorelist[j][2]==inputItem.damage then
  259.                     ignored=true
  260.                 end
  261.             end
  262.         end
  263.         --Test if on orelist   
  264.         if #orelist>0 then
  265.             for j=1,#orelist do
  266.                 if orelist[j][1]==inputItem.name and orelist[j][2]==inputItem.damage then
  267.                     ore=true
  268.                 end
  269.             end
  270.         end
  271.  
  272.         --Decide what to happen with item:
  273.         if inputItem.maxCount==1 then --if item doesnt stack send straight to random storage
  274.             --print("Item: "..inputItem.name.."/"..inputItem.damage.." does not stack")
  275.             sortlistRandom[sortcounterRandom]={inputItem.name,inputItem.damage,1,1}
  276.             sortcounterRandom=sortcounterRandom+1
  277.         elseif ignored==true then --if item is on ignorelist send straight to random storage
  278.             sortlistRandom[sortcounterRandom]={inputItem.name,inputItem.damage,inputItem.count,inputItem.maxCount}
  279.             sortcounterRandom=sortcounterRandom+1
  280.         elseif ore==true then --if item is on orelist eject ore to oreprocessing
  281.             inputChest.pushItems("up",i)
  282.         else
  283.             --print(inputItem.name.."/"..inputItem.damage)
  284.             sortlist[sortcounter]={inputItem.name,inputItem.damage,inputItem.count,inputItem.maxCount}
  285.             sortcounter=sortcounter+1
  286.         end
  287.     end
  288. end
  289.  
  290. --Initialize sendlist
  291. sendlist={}
  292. for i=1,#modemlist do -------
  293.     sendlist[i]={}
  294. end
  295.  
  296. --Compare input chest with crates and add to sendlist if match is found
  297. sortfound={}
  298. for k=1,#sortlist do
  299.     sortfound[k]=false
  300. end
  301. for i=1,#itemMapName do
  302.     for j=1,#itemMapName[1] do
  303.         for index = 1, #sortlist do
  304.             if itemMapName[i][j] == sortlist[index][1] and itemMapDamage[i][j] == sortlist[index][2] then
  305.                 if #sendlist[i]==nil then --Check if first entry in sendlist
  306.                     sendlist[i][1]={sortlist[index][1],sortlist[index][2],j}--Send itemname/damage/position j to turtle in row i
  307.                 else
  308.                     sendlist[i][#sendlist[i]+1]={sortlist[index][1],sortlist[index][2],j}
  309.                 end
  310.                 print(sortlist[index][1].."/"..sortlist[index][2].." to crate "..i..","..j)
  311.                 sortfound[index]=true
  312.             end
  313.         end
  314.     end
  315. end
  316.  
  317. --If no existing crate was found for item, assign new crate
  318. for k=1,#sortlist do
  319.     if sortfound[k]==false then
  320.         ie,je,freecrate=findEmpty(itemMapName)
  321.         if freecrate==true then
  322.             sendlist[ie][#sendlist[ie]+1]={sortlist[k][1],sortlist[k][2],je}
  323.             itemMapName[ie][je]="reserved"
  324.             for ks=k,#sortlist do
  325.                 if sortlist[ks][1]==sortlist[k][1] and sortlist[ks][2]==sortlist[k][2] then
  326.                     sendlist[ie][#sendlist[ie]+1]={sortlist[ks][1],sortlist[ks][2],je}
  327.                     sortfound[ks]=true
  328.                 end
  329.             end
  330.  
  331.  
  332.  
  333.             print(sortlist[k][1].."/"..sortlist[k][2].." to empty crate: "..ie..","..je)
  334.         else
  335.             print("No more empty crates!") --Sending "..sortlist[k][1].."/"..sortlist[k][2].." to random storage!")
  336.             sortlistRandom[sortcounterRandom]={sortlist[k][1],sortlist[k][2],sortlist[k][3],sortlist[k][4]}
  337.             sortcounterRandom=sortcounterRandom+1
  338.         end
  339.     end
  340. end
  341. --print(textutils.serialize(sortlistRandom)) --Prints list of items to be sorted to random storage
  342.  
  343. --Send sorting instructions for crates to turtles:
  344. for i=1,#sendlist do
  345.     rednet.send(IDs[i],sendlist[i])
  346. end
  347. --print("Ending crate sort")
  348.  
  349. if #sortlistRandom>0 then
  350. --print("Starting chest sort")
  351. --local x = os.clock()
  352. --Build sendlistRandom for random storage
  353. itemMapNameRandom,itemMapCountRandom,itemMapDamageRandom = StorageScanRandom()
  354. --print(textutils.serialize(itemMapNameRandom))
  355. --Initialize sendlistRandom
  356. sendlistRandom={}
  357. --Find chest and slot for random storage items
  358. for s = 1, #sortlistRandom do
  359.     local sortfoundrandom=false
  360.     if sortlistRandom[s][4]>1 then
  361.         for i=1,#itemMapNameRandom do
  362.             for j=1,#itemMapNameRandom[1] do
  363.                 for k=1,#itemMapNameRandom[1][1] do
  364.                     --if partly full stack exists
  365.                     if itemMapNameRandom[i][j][k] == sortlistRandom[s][1]
  366.                                                 and itemMapDamageRandom[i][j][k] == sortlistRandom[s][2]
  367.                                                             and itemMapCountRandom[i][j][k]<sortlistRandom[s][4] then
  368.                         if sortlistRandom[s][3] <= sortlistRandom[s][4]-itemMapCountRandom[i][j][k] then --Input count < free space
  369.                             sendAmount=sortlistRandom[s][3]
  370.                             sortfoundrandom=true
  371.                         else
  372.                             sendAmount=sortlistRandom[s][4]-itemMapCountRandom[i][j][k]--Input count > free space
  373.                             sortlistRandom[s][3]=sortlistRandom[s][3]-sendAmount
  374.                         end
  375.  
  376.                         itemMapCountRandom[i][j][k]=itemMapCountRandom[i][j][k]+sendAmount --Update scanned amount to new count
  377.  
  378.                         if #sendlistRandom==nil then --Check if first entry in sendlist
  379.                             --name,damage,amount,i,j
  380.                             sendlistRandom[1]={sortlistRandom[s][1],sortlistRandom[s][2],sendAmount,i,j,k}
  381.                         else
  382.                             sendlistRandom[#sendlistRandom+1]={sortlistRandom[s][1],sortlistRandom[s][2],sendAmount,i,j,k}
  383.                         end
  384.                         print(sortlistRandom[s][1].."/"..sortlistRandom[s][2].." to chest: "..i..","..j.." slot "..k)
  385.                         breaker=true
  386.                         break
  387.                     end-----
  388.                 end
  389.                 if sortfoundrandom then break end
  390.             end
  391.             if sortfoundrandom then break end
  392.         end
  393.         if sortfoundrandom==false then
  394.             ie,je,ke,freechest=findEmptyRandom(itemMapNameRandom)
  395.             if freechest==true then
  396.                 print(sortlistRandom[s][1].."/"..sortlistRandom[s][2].." to chest: "..ie..","..je.." slot "..ke)
  397.                 if #sendlistRandom==nil then --Check if first entry in sendlist
  398.                     sendlistRandom[1]={sortlistRandom[s][1],sortlistRandom[s][2],sortlistRandom[s][3],ie,je,ke}
  399.                 else
  400.                     sendlistRandom[#sendlistRandom+1]={sortlistRandom[s][1],sortlistRandom[s][2],sortlistRandom[s][3],ie,je,ke}
  401.                 end
  402.                 itemMapNameRandom[ie][je][ke]="reserved"
  403.             else
  404.                 print("Warning: No available storage for "..sortlistRandom[s][1].."/"..sortlistRandom[s][2].." found!")
  405.             end
  406.         end
  407.     else --Item doesn't stack, send to empty slot
  408.         ie,je,ke,freechest=findEmptyRandom(itemMapNameRandom)
  409.         if freechest==true then
  410.             print(sortlistRandom[s][1].."/"..sortlistRandom[s][2].." to chest: "..ie..","..je.." slot "..ke)
  411.             if #sendlistRandom==nil then --Check if first entry in sendlist
  412.                 sendlistRandom[1]={sortlistRandom[s][1],sortlistRandom[s][2],sortlistRandom[s][3],ie,je,ke}
  413.             else
  414.                 sendlistRandom[#sendlistRandom+1]={sortlistRandom[s][1],sortlistRandom[s][2],sortlistRandom[s][3],ie,je,ke}
  415.             end
  416.             itemMapNameRandom[ie][je][ke]="reserved"
  417.         else
  418.             print("Warning: No available storage for "..sortlistRandom[s][1].."/"..sortlistRandom[s][2].." found!")
  419.         end
  420.     end
  421. end
  422. --local xs=os.clock() - x
  423. --print("Ending chest sort. Elapsed time: "..xs)
  424. end
  425.  
  426. --Send sorting instructions to random storage turtles:
  427. if Randomsearch~=nil and sendlistRandom~=nil then
  428.     --print(textutils.serialize(sendlistRandom))
  429.     rednet.send(IDsRandom[1],sendlistRandom)
  430.     sendlistRandom=nil
  431. end
  432.  
  433. end -- end of test if anything in inputchest
  434. sleep(5)--#modemlist[1]*2+3)
  435. end --end main loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement