akihex

autoInfusor

Aug 10th, 2014
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.91 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local mapping = {16,9,10,14,13,12,11,15,19,17,18,8,7}
  4.  
  5. local lp = peripheral.wrap("front")
  6. if not lp.getTurtleConnect() then lp.setTurtleConnect(true) end
  7.  
  8. local ae = peripheral.wrap("bottom")
  9. local aeDir = "up"
  10.  
  11. local dim = peripheral.wrap("back")
  12. --local dimOffset = 6
  13. local dimDir = "south"
  14.  
  15. local wifi = peripheral.wrap("right")
  16. local channel = 45
  17. if #args > 0 then channel = tonumber(args[1]) end
  18. print("Opening wifi channel ",channel)
  19. wifi.open(channel)
  20.  
  21. --local items = {}
  22. -- search for apples
  23. local dummyID = 260
  24. local dummy = lp.getItemIdentifierIDFor(dummyID,0) 
  25. --if not lp.getItemAmount(dummy) or lp.getItemAmount(dummy) < 13 then dummy = nil end
  26. --if not dummy then return -1 end
  27. while not lp.getItemAmount(dummy) or lp.getItemAmount(dummy) < 13 do
  28.   sleep(1)
  29. end
  30.  
  31. -- http://lua-users.org/wiki/SplitJoin
  32. local function split(str, pat)
  33.    local t = {}
  34.    local fpat = "(.-)" .. pat
  35.    local last_end = 1
  36.    local s, e, cap = str:find(fpat, 1)
  37.    while s do
  38.       if s ~= 1 or cap ~= "" then
  39.          table.insert(t,cap)
  40.       end
  41.       last_end = e+1
  42.       s, e, cap = str:find(fpat, last_end)
  43.    end
  44.    if last_end <= #str then
  45.       cap = str:sub(last_end)
  46.       table.insert(t, cap)
  47.    end
  48.    return t
  49. end
  50.  
  51. local function itemInSlot(slot)
  52.   return turtle.getItemCount(slot)
  53. end
  54.  
  55. local function returnItemToAE(slot,amount)
  56.   --return ae.pullItem(aeDir,slot,amount)
  57.   local n
  58.   repeat
  59.     n = itemInSlot(slot)
  60.   until ae.pullItem(aeDir,slot,n) - n == 0
  61. end
  62.  
  63. local function clearInventory()
  64.   for i=1,16 do
  65.     if itemInSlot(i) > 0 then returnItemToAE(i,itemInSlot(i)) end
  66.   end
  67. end
  68.  
  69. local function getFreeSlot()
  70.   local slot = 1
  71.   while slot < 17 and itemInSlot(slot) > 0 do slot = slot + 1 end
  72.   if itemInSlot(slot) > 0 then print("turtle inventory full while trying to request ", name); return 0
  73.   else return slot
  74.   end
  75. end
  76.  
  77. local function requestItemWithLpID(lpID, amount)
  78.   if amount > 64 then return 0 end
  79.   clearInventory()
  80.   if itemInSlot(1) > 0 then return 0 end
  81.   turtle.select(1)
  82.   local result = lp.makeRequest(lpID, amount)
  83.   if result ~= "DONE" then
  84.     print("Failed to get ", amount, " of ", lpID, ", ", lp.getUnlocalizedName(lpID),": ",result)
  85.     return 0
  86.   else
  87.     while itemInSlot(1) < amount do
  88.       sleep(0.2)
  89.     end
  90.     return itemInSlot(1)
  91.   end
  92. end
  93.  
  94. local function requestItemWithID(ID, dmg, amount)
  95.   local lpID = lp.getItemIdentifierIDFor(math.floor(ID),math.floor(dmg))
  96.   if lpID and lpID ~= 0 then return requestItemWithLpID(lpID,amount)
  97.   else return 0 end
  98. end
  99.  
  100. local occupied = {}
  101.  
  102. local function getOccupiedPedestals()
  103.   occupied = {}
  104.   local occ = dim.getAllStacks()
  105.   for i,k in pairs(occ) do
  106.     if k.id == dummyID then occupied[i] = true end
  107.   end
  108. end
  109.  
  110. local function occupyPedestals()
  111.   local request = requestItemWithLpID(dummy,13)
  112.   if request ~= 13 then print(request) end
  113.   if request < 13 then return 0 end
  114.   for i=1,13 do dim.pullItem(dimDir,1,1) end
  115.   getOccupiedPedestals()
  116.   clearInventory()
  117. end
  118.  
  119. local function clearPedestals()
  120.   clearInventory()
  121.   if next(occupied) == nil then
  122.     for i=7,19 do dim.pushItem(dimDir,i,1) end
  123.   else
  124.     for i,k in pairs(occupied) do
  125.       if k then
  126.         dim.pushItem(dimDir,i,1)
  127.         occupied[i] = false
  128.       end
  129.     end
  130.   end
  131.   clearInventory()
  132.   local occ = dim.getAllStacks()
  133.   if next(occ) == nil then return true
  134.   else return false
  135.   end
  136. end
  137.  
  138. local function clearAll()
  139.   occupied = {}
  140.   return clearPedestals()
  141. end
  142.  
  143. local recipes = {}
  144.  
  145. local function readRecipes()
  146.   local recipeFile = fs.open("infusionRecipes","r")
  147.   if not recipeFile then return 0 end
  148.   local line = recipeFile.readLine()
  149.   while line do
  150.     local recipe = split(line,",")
  151.     if #recipe < 14 then break end
  152.     --recipes[tonumber(recipe[1])] = {}
  153.     recipes[recipe[14]] = {}
  154.     for i=1,#recipe do
  155.       --table.insert(recipes[tonumber(recipe[1])],recipe[i])
  156.       table.insert(recipes[recipe[14]],split(recipe[i],":"))
  157.     end
  158.     line = recipeFile.readLine()
  159.   end
  160.   recipeFile.close()
  161. end
  162.  
  163. readRecipes()
  164.  
  165. local function printRecipe(recipe)
  166.   print(math.floor(recipe[1][1]),":",recipe[1][2] and math.floor(recipe[1][2]) or 0," ",recipe[14][1])
  167.   for i=2,13 do
  168.     term.write(math.floor(recipe[i][1])..":"..(recipe[i][2] and math.floor(recipe[i][2]) or 0).." ")
  169.     if (i-1) % 4 == 0 then print("") end
  170.   end
  171.   --print("")
  172. end
  173.  
  174. local function printAllRecipes()
  175.   for _,recipe in pairs(recipes) do printRecipe(recipe) end
  176. end
  177.  
  178. local function readPedestals()
  179.   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},{" "}}
  180.   local pattern = dim.getAllStacks()
  181.   for i=1,13 do
  182.     recipe[i][1] = pattern[mapping[i]] and pattern[mapping[i]].id or 0
  183.     recipe[i][2] = pattern[mapping[i]] and pattern[mapping[i]].dmg or 0
  184.     recipe[i][3] = pattern[mapping[i]] and pattern[mapping[i]].name or ""
  185.   end
  186.   --for i,k in pairs(pattern) do
  187.     --if i < 19 then recipe[i-dimOffset+1] = k.id
  188.     --elseif i == 19 then recipe[1] = k.id
  189.     --end
  190.   --end
  191. --  printRecipe(recipe)
  192.   return recipe
  193. end
  194.  
  195. local function insertRecipe(text)
  196.   local recipe = readPedestals()
  197.   --recipe[14] = text or " "
  198.   recipe[14] = {text or function()
  199.     local hash = ""
  200.     for i=1,13 do
  201.       hash = hash..recipe[i][1]..(recipe[i][2] and recipe[i][2] or 0)
  202.     end
  203.     return hash
  204.   end}
  205.   --recipes[tonumber(recipe[1])] = {}
  206.   recipes[recipe[14][1]] = {}
  207.   --for i=1,#recipe do table.insert(recipes[tonumber(recipe[1])],recipe[i]) end
  208.   for i=1,#recipe do table.insert(recipes[recipe[14][1]],{recipe[i][1],recipe[i][2]}) end
  209. end
  210.  
  211. local function storeRecipes()
  212.   shell.run("rm infusionRecipes")
  213.   local recipeFile = fs.open("infusionRecipes","w")
  214.   if not recipeFile then return 0 end
  215.   local line = ""
  216.   for _,recipe in pairs(recipes) do
  217.     --print(#recipe)
  218.     if #recipe == 14 then
  219.       line = recipe[1][1]..":"..(recipe[1][2] and recipe[1][2] or 0)
  220.       for i=2,#recipe-1 do
  221.         line = line..","..(recipe[i][1] and recipe[i][1] or recipe[i])..":"..(recipe[i][2] and recipe[i][2] or 0)
  222.       end
  223.       line = line..","..recipe[14][1]..(recipe[14][2] and (":"..recipe[14][2]) or "")
  224.       --print(line)
  225.       recipeFile.writeLine(line)
  226.       recipeFile.flush()
  227.     end
  228.   end
  229.   recipeFile.close()
  230. end
  231.  
  232. local function prepareAltar()
  233.   if not clearAll() then print("Infusion altar not ready") end
  234.   occupyPedestals()
  235. end
  236.  
  237. local function addRecipe(text)
  238.   insertRecipe(text)
  239.   storeRecipes()
  240. end
  241.  
  242. local function freePedestal(number)
  243.   getOccupiedPedestals()
  244.   clearInventory()
  245.   local result = dim.pushItem(dimDir,number,1)
  246.   if result == 1 then occupied[number] = nil end
  247.   clearInventory()
  248.   return result == 1
  249. end
  250.  
  251. local function compareAltar(recipe)
  252.   if type(recipe) ~= "table" and #recipe ~= 14 then
  253.     print("invalid parameter: ",recipe," or parameter type: ",type(recipe))
  254.     return false
  255.   end
  256.   local pattern = dim.getAllStacks()
  257.   --local currentRecipe = {0,0,0,0,0,0,0,0,0,0,0,0,0," "}
  258.   --for i=1,13 do
  259.     --currentRecipe[i] = pattern[mapping[i]] and pattern[mapping[i]].id or 0
  260.   --end
  261.   --printRecipe(currentRecipe)
  262.   --printRecipe(recipe)
  263.   local match = false
  264.   local element = 0
  265.   local dmg = 0
  266.   for i=1,13 do
  267.     element = pattern[mapping[i]] and pattern[mapping[i]].id or 0
  268.     dmg = pattern[mapping[i]] and pattern[mapping[i]].dmg or 0
  269.     match = (element == tonumber(recipe[i][1]) and dmg == tonumber(recipe[i][2]))
  270.     if not match then break end
  271.   end
  272.   return match
  273. end
  274.  
  275. local function compareAltarWithRecipe(name)
  276. --  if not recipes[tonumber(number)] then return false
  277. --  else return compareAltar(recipes[tonumber(number)])
  278.   if not recipes[name] then return false
  279.   else return compareAltar(recipes[name])
  280.   end
  281. end
  282.  
  283. local function loadAltar(recipe)
  284.   if not compareAltar(recipe) then
  285.     prepareAltar()
  286.     for i=1,13 do
  287.       if tonumber(recipe[i][1]) ~= 0 then
  288.         --print(recipe[i])
  289.         freePedestal(mapping[i])
  290.         if requestItemWithID(recipe[i][1],recipe[i][2] and recipe[i][2] or 0,1) == 1 then
  291.           print(itemInSlot(1)," item: ",recipe[i][1],(recipe[i][2] and (":"..recipe[i][2]) or ""))
  292.           dim.pullItem(dimDir,1,1)
  293.           --while itemInSlot(1) ~= 0 do
  294.             --sleep(0.2)
  295.           --end
  296.         end
  297.       end
  298.     end
  299.     getOccupiedPedestals()
  300.     clearPedestals()
  301.     return compareAltar(recipe)
  302.   else return true
  303.   end
  304. end
  305.  
  306. local function loadAltarWithRecipe(name)
  307.   if not recipes[name] then return false
  308.   else
  309.     return loadAltar(recipes[name])
  310.   end
  311. end
  312.  
  313. local function getRecipes()
  314.   return recipes
  315. end
  316.  
  317. local switch = {
  318.   ["prep"] = function() return prepareAltar() or 1 end,
  319.   ["clear"] = function() return clearPedestals() or 1 end,
  320.   ["clearall"] = function() return clearAll() or 1 end,
  321.   ["add"] = function(addInfo) return addRecipe(addInfo) or 1 end,
  322.   ["printall"] = function() return printAllRecipes() or 1 end,
  323.   ["free"] = function(addInfo) return freePedestal(addInfo) or 1 end,
  324.   ["cmp"] = function(addInfo) return compareAltar(addInfo) and 1 or 2 end,
  325.   ["load"] = function(addInfo) return loadAltar(addInfo) and 1 or 2 end,
  326.   ["cmpR"] = function(addInfo) return compareAltarWithRecipe(addInfo) and 1 or 2 end,
  327.   ["loadR"] = function(addInfo) return loadAltarWithRecipe(addInfo) and 1 or 2 end,
  328.   ["altar"] = function() return readPedestals() end,
  329.   ["recipes"] = function() return getRecipes() end,
  330.   ["reboot"] = function() os.reboot() end
  331. }
  332.  
  333. while true do
  334.   local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  335.   if message and message.id then
  336.     local addInfo = message.info or " "
  337.     local result = switch[message.id] and switch[message.id](addInfo) or print("message.id not valid: ",message.id) and 0
  338.     print(message.id," ",result)
  339.     wifi.transmit(channel,channel,{["id"]=message.id,["result"]=result})
  340.   end
  341. end
Advertisement
Add Comment
Please, Sign In to add comment