Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------Definition---------------------------------------------------------------
- shutdown=false --initialization of shutdown parameter
- 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}}
- ignorelist={{"thaumcraft:crystal_essence",0}}
- ---------------------------------------------First time user entry:---------------------------------------------------------------
- --Check if this is first time run, no saved choices on file
- if (fs.exists("choices") == false) then
- while true do
- print("Please input pastebin ID of modemlist")
- local answer = read()
- if #answer~=8 then
- print("-Wrong answer, not correct length of pastebin ID-")
- else
- pasteID=answer
- break
- end
- end
- --Save choices to file for future restarts
- local file = fs.open("choices","w")
- file.writeLine("pasteID=".."\""..pasteID.."\"")
- file.close()
- end
- -- Read choices from file
- os.loadAPI("choices")
- pasteID = choices.pasteID
- --print(pasteID)
- --Load network modem IDs from pastebin file
- if (fs.exists("modemfile") == true) then
- shell.run("delete","modemfile")
- end
- shell.run("pastebin","get",pasteID,"modemfile")--pasteID
- os.loadAPI("modemfile") -- Reads choices from file
- local storetype = modemfile.storetype --type of single item containers used for bulk storage
- local modemlist = modemfile.modemlist
- local storetypeRandom=modemfile.storetypeRandom --type of multi item containers used for random storage
- local modemlistRandom=modemfile.modemlistRandom
- --Combine modemlist with full network name
- for i=1,#modemlist do
- for j=1,#modemlist[1] do
- modemlist[i][j]=storetype..tostring(modemlist[i][j])
- --print(modemlist[i][j])
- end
- end
- for i=1,#modemlistRandom do
- for j=1,#modemlistRandom[1] do
- modemlistRandom[i][j]=storetypeRandom..tostring(modemlistRandom[i][j])
- --print(modemlistRandom[i][j])
- end
- end
- ---------------------------------------------Peripheral wrap---------------------------------------------------------------
- --List all networked peripherals
- local peripheralList = peripheral.getNames()
- --Initialize crate modem peripheral list
- local cratemodem={}
- for i=1,#modemlist do
- cratemodem[i]={}
- end
- --wrap crate modems
- for i=1,#modemlist do
- for j=1,#modemlist[1] do
- for index = 1, #peripheralList do
- if peripheralList[index] == modemlist[i][j] then --scan to see if names match
- cratemodem[i][j]=peripheral.wrap(peripheralList[index]) --wrap crate modems to array
- end
- end
- end
- end
- --Initialize chest modem peripheral list
- local chestmodem={}
- for i=1,#modemlistRandom do
- chestmodem[i]={}
- end
- --wrap chest modems
- --print(textutils.serialize(modemlistRandom))
- for i=1,#modemlistRandom do
- for j=1,#modemlistRandom[1] do
- for index = 1, #peripheralList do
- if peripheralList[index] == modemlistRandom[i][j] then --scan to see if names match
- chestmodem[i][j]=peripheral.wrap(peripheralList[index]) --wrap chest modems to array
- end
- end
- end
- end
- --print(textutils.serialize(chestmodem))
- --Wrap inputchest:
- inputChest=peripheral.wrap("top")
- ---------------------------------------------Functions---------------------------------------------------------------
- local function StorageScan() --Scan crates and build array of items
- -- Initialize item information tables
- local itemMapName={}
- local itemMapCount={}
- local itemMapDamage={}
- for i=1,#modemlist do
- itemMapName[i]={}
- itemMapCount[i]={}
- itemMapDamage[i]={}
- end
- --Start scan
- for i=1,#cratemodem do
- for j=1,#cratemodem[1] do
- local iteminfo=cratemodem[i][j].list()
- if iteminfo[1]~= nil then
- itemMapName[i][j]=iteminfo[1].name
- itemMapCount[i][j]=iteminfo[1].count
- itemMapDamage[i][j]=iteminfo[1].damage
- else
- itemMapName[i][j]="noitem"
- itemMapCount[i][j]=0
- itemMapDamage[i][j]=0
- end
- --print(i..","..j..": "..itemMapCount[i][j].." "..itemMapName[i][j].."/"..itemMapDamage[i][j])
- end
- end
- return itemMapName,itemMapCount,itemMapDamage
- end
- local function StorageScanRandom()
- local itemMapNameRandom={}
- local itemMapCountRandom={}
- local itemMapDamageRandom={}
- --Initialize "3d" tables
- for i=1,#modemlistRandom do
- itemMapNameRandom[i]={}
- itemMapCountRandom[i]={}
- itemMapDamageRandom[i]={}
- end
- for i=1,#modemlistRandom do
- for j=1,#modemlistRandom[1] do
- itemMapNameRandom[i][j]={}
- itemMapCountRandom[i][j]={}
- itemMapDamageRandom[i][j]={}
- end
- end
- --save name, count and damage for all items in random storage
- for i=1,#chestmodem do
- for j=1,#chestmodem[1] do
- chestlist=chestmodem[i][j].list()
- for k=1,chestmodem[i][j].size() do
- if chestlist[k]~= nil then
- itemMapNameRandom[i][j][k]=chestlist[k].name
- itemMapCountRandom[i][j][k]=chestlist[k].count
- itemMapDamageRandom[i][j][k]=chestlist[k].damage
- else
- itemMapNameRandom[i][j][k]="noitem"
- itemMapCountRandom[i][j][k]=0
- itemMapDamageRandom[i][j][k]=0
- end
- end
- end
- end
- --print(textutils.serialize(itemMapNameRandom))
- return itemMapNameRandom,itemMapCountRandom,itemMapDamageRandom
- end --end function
- --Find first empty in crate
- local function findEmpty(itemMapName)
- for j=1,#itemMapName[1] do
- for i=1,#itemMapName do
- if itemMapName[i][j] == "noitem" then
- return i,j,true
- end
- end
- end
- return 0,0,false
- end
- --Find first empty in chest
- local function findEmptyRandom(itemMapNameRandom)
- for j=1,#itemMapNameRandom[1] do
- for i=1,#itemMapNameRandom do
- for k=1,#itemMapNameRandom[1][1] do
- if itemMapNameRandom[i][j][k] == "noitem" then
- return i,j,k,true
- end
- end
- end
- end
- return 0,0,0,false
- end
- ---------------------------------------------Rednet prep---------------------------------------------------------------
- --Rednet initialization
- rednet.open("right")
- sleep(10) --Wait for turtles
- --Find rednet IDs for crate turtles
- local IDs={}
- if rednet.lookup("ItemSortCrate") then
- for i=1,#modemlist do
- IDs[i]=rednet.lookup("ItemSortCrate","row"..i)
- --print(IDs[i])
- end
- else
- print("No crate turtles found, shutting down!")
- shutdown=true
- end
- --Find IDs for random turtles
- local Randomsearch={rednet.lookup("ItemSortRandom")}
- if Randomsearch~=nil then
- IDsRandom={}
- for i=1,#Randomsearch do
- if rednet.lookup("ItemSortRandom","random"..i) then
- IDsRandom[i]=Randomsearch[i]
- print("Random turtle "..tostring(IDsRandom[i]).." found")
- end
- end
- else
- print("No random storage turtles found")
- end
- ---------------------------------------------Execution---------------------------------------------------------------
- if shutdown==false then
- print("Starting sorting operation")
- end
- -- Main loop
- while shutdown==false do
- if #inputChest.list() > 0 then --test if anything in inputchest
- --print("Starting crate sort")
- --Scan network for items and build arrays of names and count
- itemMapName,itemMapCount,itemMapDamage = StorageScan()
- --Scan inputchest
- --print("Input chest contents:")
- local sortlist={} -- Format: sortlist={{itemname,damage,count},{itemname,damage,count},...}
- local sortlistRandom={}
- local sortcounter=1
- local sortcounterRandom=1
- for i=1,inputChest.size() do
- local ignored=false
- local ore=false
- inputItem=inputChest.getItemMeta(i)
- if inputItem~= nil then
- --Test if on ignorelist:
- if #ignorelist>0 then
- for j=1,#ignorelist do
- if ignorelist[j][1]==inputItem.name and ignorelist[j][2]==inputItem.damage then
- ignored=true
- end
- end
- end
- --Test if on orelist
- if #orelist>0 then
- for j=1,#orelist do
- if orelist[j][1]==inputItem.name and orelist[j][2]==inputItem.damage then
- ore=true
- end
- end
- end
- --Decide what to happen with item:
- if inputItem.maxCount==1 then --if item doesnt stack send straight to random storage
- --print("Item: "..inputItem.name.."/"..inputItem.damage.." does not stack")
- sortlistRandom[sortcounterRandom]={inputItem.name,inputItem.damage,1,1}
- sortcounterRandom=sortcounterRandom+1
- elseif ignored==true then --if item is on ignorelist send straight to random storage
- sortlistRandom[sortcounterRandom]={inputItem.name,inputItem.damage,inputItem.count,inputItem.maxCount}
- sortcounterRandom=sortcounterRandom+1
- elseif ore==true then --if item is on orelist eject ore to oreprocessing
- inputChest.pushItems("up",i)
- else
- --print(inputItem.name.."/"..inputItem.damage)
- sortlist[sortcounter]={inputItem.name,inputItem.damage,inputItem.count,inputItem.maxCount}
- sortcounter=sortcounter+1
- end
- end
- end
- --Initialize sendlist
- sendlist={}
- for i=1,#modemlist do -------
- sendlist[i]={}
- end
- --Compare input chest with crates and add to sendlist if match is found
- sortfound={}
- for k=1,#sortlist do
- sortfound[k]=false
- end
- for i=1,#itemMapName do
- for j=1,#itemMapName[1] do
- for index = 1, #sortlist do
- if itemMapName[i][j] == sortlist[index][1] and itemMapDamage[i][j] == sortlist[index][2] then
- if #sendlist[i]==nil then --Check if first entry in sendlist
- sendlist[i][1]={sortlist[index][1],sortlist[index][2],j}--Send itemname/damage/position j to turtle in row i
- else
- sendlist[i][#sendlist[i]+1]={sortlist[index][1],sortlist[index][2],j}
- end
- print(sortlist[index][1].."/"..sortlist[index][2].." to crate "..i..","..j)
- sortfound[index]=true
- end
- end
- end
- end
- --If no existing crate was found for item, assign new crate
- for k=1,#sortlist do
- if sortfound[k]==false then
- ie,je,freecrate=findEmpty(itemMapName)
- if freecrate==true then
- sendlist[ie][#sendlist[ie]+1]={sortlist[k][1],sortlist[k][2],je}
- itemMapName[ie][je]="reserved"
- for ks=k,#sortlist do
- if sortlist[ks][1]==sortlist[k][1] and sortlist[ks][2]==sortlist[k][2] then
- sendlist[ie][#sendlist[ie]+1]={sortlist[ks][1],sortlist[ks][2],je}
- sortfound[ks]=true
- end
- end
- print(sortlist[k][1].."/"..sortlist[k][2].." to empty crate: "..ie..","..je)
- else
- print("No more empty crates!") --Sending "..sortlist[k][1].."/"..sortlist[k][2].." to random storage!")
- sortlistRandom[sortcounterRandom]={sortlist[k][1],sortlist[k][2],sortlist[k][3],sortlist[k][4]}
- sortcounterRandom=sortcounterRandom+1
- end
- end
- end
- --print(textutils.serialize(sortlistRandom)) --Prints list of items to be sorted to random storage
- --Send sorting instructions for crates to turtles:
- for i=1,#sendlist do
- rednet.send(IDs[i],sendlist[i])
- end
- --print("Ending crate sort")
- if #sortlistRandom>0 then
- --print("Starting chest sort")
- --local x = os.clock()
- --Build sendlistRandom for random storage
- itemMapNameRandom,itemMapCountRandom,itemMapDamageRandom = StorageScanRandom()
- --print(textutils.serialize(itemMapNameRandom))
- --Initialize sendlistRandom
- sendlistRandom={}
- --Find chest and slot for random storage items
- for s = 1, #sortlistRandom do
- local sortfoundrandom=false
- if sortlistRandom[s][4]>1 then
- for i=1,#itemMapNameRandom do
- for j=1,#itemMapNameRandom[1] do
- for k=1,#itemMapNameRandom[1][1] do
- --if partly full stack exists
- if itemMapNameRandom[i][j][k] == sortlistRandom[s][1]
- and itemMapDamageRandom[i][j][k] == sortlistRandom[s][2]
- and itemMapCountRandom[i][j][k]<sortlistRandom[s][4] then
- if sortlistRandom[s][3] <= sortlistRandom[s][4]-itemMapCountRandom[i][j][k] then --Input count < free space
- sendAmount=sortlistRandom[s][3]
- sortfoundrandom=true
- else
- sendAmount=sortlistRandom[s][4]-itemMapCountRandom[i][j][k]--Input count > free space
- sortlistRandom[s][3]=sortlistRandom[s][3]-sendAmount
- end
- itemMapCountRandom[i][j][k]=itemMapCountRandom[i][j][k]+sendAmount --Update scanned amount to new count
- if #sendlistRandom==nil then --Check if first entry in sendlist
- --name,damage,amount,i,j
- sendlistRandom[1]={sortlistRandom[s][1],sortlistRandom[s][2],sendAmount,i,j,k}
- else
- sendlistRandom[#sendlistRandom+1]={sortlistRandom[s][1],sortlistRandom[s][2],sendAmount,i,j,k}
- end
- print(sortlistRandom[s][1].."/"..sortlistRandom[s][2].." to chest: "..i..","..j.." slot "..k)
- breaker=true
- break
- end-----
- end
- if sortfoundrandom then break end
- end
- if sortfoundrandom then break end
- end
- if sortfoundrandom==false then
- ie,je,ke,freechest=findEmptyRandom(itemMapNameRandom)
- if freechest==true then
- print(sortlistRandom[s][1].."/"..sortlistRandom[s][2].." to chest: "..ie..","..je.." slot "..ke)
- if #sendlistRandom==nil then --Check if first entry in sendlist
- sendlistRandom[1]={sortlistRandom[s][1],sortlistRandom[s][2],sortlistRandom[s][3],ie,je,ke}
- else
- sendlistRandom[#sendlistRandom+1]={sortlistRandom[s][1],sortlistRandom[s][2],sortlistRandom[s][3],ie,je,ke}
- end
- itemMapNameRandom[ie][je][ke]="reserved"
- else
- print("Warning: No available storage for "..sortlistRandom[s][1].."/"..sortlistRandom[s][2].." found!")
- end
- end
- else --Item doesn't stack, send to empty slot
- ie,je,ke,freechest=findEmptyRandom(itemMapNameRandom)
- if freechest==true then
- print(sortlistRandom[s][1].."/"..sortlistRandom[s][2].." to chest: "..ie..","..je.." slot "..ke)
- if #sendlistRandom==nil then --Check if first entry in sendlist
- sendlistRandom[1]={sortlistRandom[s][1],sortlistRandom[s][2],sortlistRandom[s][3],ie,je,ke}
- else
- sendlistRandom[#sendlistRandom+1]={sortlistRandom[s][1],sortlistRandom[s][2],sortlistRandom[s][3],ie,je,ke}
- end
- itemMapNameRandom[ie][je][ke]="reserved"
- else
- print("Warning: No available storage for "..sortlistRandom[s][1].."/"..sortlistRandom[s][2].." found!")
- end
- end
- end
- --local xs=os.clock() - x
- --print("Ending chest sort. Elapsed time: "..xs)
- end
- --Send sorting instructions to random storage turtles:
- if Randomsearch~=nil and sendlistRandom~=nil then
- --print(textutils.serialize(sendlistRandom))
- rednet.send(IDsRandom[1],sendlistRandom)
- sendlistRandom=nil
- end
- end -- end of test if anything in inputchest
- sleep(5)--#modemlist[1]*2+3)
- end --end main loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement