Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local mapping = {16,9,10,14,13,12,11,15,19,17,18,8,7}
- local lp = peripheral.wrap("front")
- if not lp.getTurtleConnect() then lp.setTurtleConnect(true) end
- local ae = peripheral.wrap("bottom")
- local aeDir = "up"
- local dim = peripheral.wrap("back")
- --local dimOffset = 6
- local dimDir = "south"
- local wifi = peripheral.wrap("right")
- local channel = 45
- if #args > 0 then channel = tonumber(args[1]) end
- print("Opening wifi channel ",channel)
- wifi.open(channel)
- --local items = {}
- -- search for apples
- local dummyID = 260
- local dummy = lp.getItemIdentifierIDFor(dummyID,0)
- --if not lp.getItemAmount(dummy) or lp.getItemAmount(dummy) < 13 then dummy = nil end
- --if not dummy then return -1 end
- while not lp.getItemAmount(dummy) or lp.getItemAmount(dummy) < 13 do
- sleep(1)
- end
- -- http://lua-users.org/wiki/SplitJoin
- local function split(str, pat)
- local t = {}
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- local function itemInSlot(slot)
- return turtle.getItemCount(slot)
- end
- local function returnItemToAE(slot,amount)
- --return ae.pullItem(aeDir,slot,amount)
- local n
- repeat
- n = itemInSlot(slot)
- until ae.pullItem(aeDir,slot,n) - n == 0
- end
- local function clearInventory()
- for i=1,16 do
- if itemInSlot(i) > 0 then returnItemToAE(i,itemInSlot(i)) end
- end
- end
- local function getFreeSlot()
- local slot = 1
- while slot < 17 and itemInSlot(slot) > 0 do slot = slot + 1 end
- if itemInSlot(slot) > 0 then print("turtle inventory full while trying to request ", name); return 0
- else return slot
- end
- end
- local function requestItemWithLpID(lpID, amount)
- if amount > 64 then return 0 end
- clearInventory()
- if itemInSlot(1) > 0 then return 0 end
- turtle.select(1)
- local result = lp.makeRequest(lpID, amount)
- if result ~= "DONE" then
- print("Failed to get ", amount, " of ", lpID, ", ", lp.getUnlocalizedName(lpID),": ",result)
- return 0
- else
- while itemInSlot(1) < amount do
- sleep(0.2)
- end
- return itemInSlot(1)
- end
- end
- local function requestItemWithID(ID, dmg, amount)
- local lpID = lp.getItemIdentifierIDFor(math.floor(ID),math.floor(dmg))
- if lpID and lpID ~= 0 then return requestItemWithLpID(lpID,amount)
- else return 0 end
- end
- local occupied = {}
- local function getOccupiedPedestals()
- occupied = {}
- local occ = dim.getAllStacks()
- for i,k in pairs(occ) do
- if k.id == dummyID then occupied[i] = true end
- end
- end
- local function occupyPedestals()
- local request = requestItemWithLpID(dummy,13)
- if request ~= 13 then print(request) end
- if request < 13 then return 0 end
- for i=1,13 do dim.pullItem(dimDir,1,1) end
- getOccupiedPedestals()
- clearInventory()
- end
- local function clearPedestals()
- clearInventory()
- if next(occupied) == nil then
- for i=7,19 do dim.pushItem(dimDir,i,1) end
- else
- for i,k in pairs(occupied) do
- if k then
- dim.pushItem(dimDir,i,1)
- occupied[i] = false
- end
- end
- end
- clearInventory()
- local occ = dim.getAllStacks()
- if next(occ) == nil then return true
- else return false
- end
- end
- local function clearAll()
- occupied = {}
- return clearPedestals()
- end
- local recipes = {}
- local function readRecipes()
- local recipeFile = fs.open("infusionRecipes","r")
- if not recipeFile then return 0 end
- local line = recipeFile.readLine()
- while line do
- local recipe = split(line,",")
- if #recipe < 14 then break end
- --recipes[tonumber(recipe[1])] = {}
- recipes[recipe[14]] = {}
- for i=1,#recipe do
- --table.insert(recipes[tonumber(recipe[1])],recipe[i])
- table.insert(recipes[recipe[14]],split(recipe[i],":"))
- end
- line = recipeFile.readLine()
- end
- recipeFile.close()
- end
- readRecipes()
- local function printRecipe(recipe)
- print(math.floor(recipe[1][1]),":",recipe[1][2] and math.floor(recipe[1][2]) or 0," ",recipe[14][1])
- for i=2,13 do
- term.write(math.floor(recipe[i][1])..":"..(recipe[i][2] and math.floor(recipe[i][2]) or 0).." ")
- if (i-1) % 4 == 0 then print("") end
- end
- --print("")
- end
- local function printAllRecipes()
- for _,recipe in pairs(recipes) do printRecipe(recipe) end
- end
- local function readPedestals()
- local recipe = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{" "}}
- local pattern = dim.getAllStacks()
- for i=1,13 do
- recipe[i][1] = pattern[mapping[i]] and pattern[mapping[i]].id or 0
- recipe[i][2] = pattern[mapping[i]] and pattern[mapping[i]].dmg or 0
- recipe[i][3] = pattern[mapping[i]] and pattern[mapping[i]].name or ""
- end
- --for i,k in pairs(pattern) do
- --if i < 19 then recipe[i-dimOffset+1] = k.id
- --elseif i == 19 then recipe[1] = k.id
- --end
- --end
- -- printRecipe(recipe)
- return recipe
- end
- local function insertRecipe(text)
- local recipe = readPedestals()
- --recipe[14] = text or " "
- recipe[14] = {text or function()
- local hash = ""
- for i=1,13 do
- hash = hash..recipe[i][1]..(recipe[i][2] and recipe[i][2] or 0)
- end
- return hash
- end}
- --recipes[tonumber(recipe[1])] = {}
- recipes[recipe[14][1]] = {}
- --for i=1,#recipe do table.insert(recipes[tonumber(recipe[1])],recipe[i]) end
- for i=1,#recipe do table.insert(recipes[recipe[14][1]],{recipe[i][1],recipe[i][2]}) end
- end
- local function storeRecipes()
- shell.run("rm infusionRecipes")
- local recipeFile = fs.open("infusionRecipes","w")
- if not recipeFile then return 0 end
- local line = ""
- for _,recipe in pairs(recipes) do
- --print(#recipe)
- if #recipe == 14 then
- line = recipe[1][1]..":"..(recipe[1][2] and recipe[1][2] or 0)
- for i=2,#recipe-1 do
- line = line..","..(recipe[i][1] and recipe[i][1] or recipe[i])..":"..(recipe[i][2] and recipe[i][2] or 0)
- end
- line = line..","..recipe[14][1]..(recipe[14][2] and (":"..recipe[14][2]) or "")
- --print(line)
- recipeFile.writeLine(line)
- recipeFile.flush()
- end
- end
- recipeFile.close()
- end
- local function prepareAltar()
- if not clearAll() then print("Infusion altar not ready") end
- occupyPedestals()
- end
- local function addRecipe(text)
- insertRecipe(text)
- storeRecipes()
- end
- local function freePedestal(number)
- getOccupiedPedestals()
- clearInventory()
- local result = dim.pushItem(dimDir,number,1)
- if result == 1 then occupied[number] = nil end
- clearInventory()
- return result == 1
- end
- local function compareAltar(recipe)
- if type(recipe) ~= "table" and #recipe ~= 14 then
- print("invalid parameter: ",recipe," or parameter type: ",type(recipe))
- return false
- end
- local pattern = dim.getAllStacks()
- --local currentRecipe = {0,0,0,0,0,0,0,0,0,0,0,0,0," "}
- --for i=1,13 do
- --currentRecipe[i] = pattern[mapping[i]] and pattern[mapping[i]].id or 0
- --end
- --printRecipe(currentRecipe)
- --printRecipe(recipe)
- local match = false
- local element = 0
- local dmg = 0
- for i=1,13 do
- element = pattern[mapping[i]] and pattern[mapping[i]].id or 0
- dmg = pattern[mapping[i]] and pattern[mapping[i]].dmg or 0
- match = (element == tonumber(recipe[i][1]) and dmg == tonumber(recipe[i][2]))
- if not match then break end
- end
- return match
- end
- local function compareAltarWithRecipe(name)
- -- if not recipes[tonumber(number)] then return false
- -- else return compareAltar(recipes[tonumber(number)])
- if not recipes[name] then return false
- else return compareAltar(recipes[name])
- end
- end
- local function loadAltar(recipe)
- if not compareAltar(recipe) then
- prepareAltar()
- for i=1,13 do
- if tonumber(recipe[i][1]) ~= 0 then
- --print(recipe[i])
- freePedestal(mapping[i])
- if requestItemWithID(recipe[i][1],recipe[i][2] and recipe[i][2] or 0,1) == 1 then
- print(itemInSlot(1)," item: ",recipe[i][1],(recipe[i][2] and (":"..recipe[i][2]) or ""))
- dim.pullItem(dimDir,1,1)
- --while itemInSlot(1) ~= 0 do
- --sleep(0.2)
- --end
- end
- end
- end
- getOccupiedPedestals()
- clearPedestals()
- return compareAltar(recipe)
- else return true
- end
- end
- local function loadAltarWithRecipe(name)
- if not recipes[name] then return false
- else
- return loadAltar(recipes[name])
- end
- end
- local function getRecipes()
- return recipes
- end
- local switch = {
- ["prep"] = function() return prepareAltar() or 1 end,
- ["clear"] = function() return clearPedestals() or 1 end,
- ["clearall"] = function() return clearAll() or 1 end,
- ["add"] = function(addInfo) return addRecipe(addInfo) or 1 end,
- ["printall"] = function() return printAllRecipes() or 1 end,
- ["free"] = function(addInfo) return freePedestal(addInfo) or 1 end,
- ["cmp"] = function(addInfo) return compareAltar(addInfo) and 1 or 2 end,
- ["load"] = function(addInfo) return loadAltar(addInfo) and 1 or 2 end,
- ["cmpR"] = function(addInfo) return compareAltarWithRecipe(addInfo) and 1 or 2 end,
- ["loadR"] = function(addInfo) return loadAltarWithRecipe(addInfo) and 1 or 2 end,
- ["altar"] = function() return readPedestals() end,
- ["recipes"] = function() return getRecipes() end,
- ["reboot"] = function() os.reboot() end
- }
- while true do
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- if message and message.id then
- local addInfo = message.info or " "
- local result = switch[message.id] and switch[message.id](addInfo) or print("message.id not valid: ",message.id) and 0
- print(message.id," ",result)
- wifi.transmit(channel,channel,{["id"]=message.id,["result"]=result})
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment