akihex

autoCrucible

Aug 7th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. local channel = 11
  2.  
  3. local recipes = {}
  4.  
  5. local lp = peripheral.wrap("right")
  6. if not lp.getTurtleConnect() then lp.setTurtleConnect(true) end
  7.  
  8. local wifi = peripheral.find("modem")
  9. if not wifi.isWireless() then wifi = nil end
  10. wifi.open(channel)
  11.  
  12. local ae = peripheral.wrap("top")
  13.  
  14. -- http://lua-users.org/wiki/SplitJoin
  15. local function split(str, pat)
  16.    local t = {}
  17.    local fpat = "(.-)" .. pat
  18.    local last_end = 1
  19.    local s, e, cap = str:find(fpat, 1)
  20.    while s do
  21.       if s ~= 1 or cap ~= "" then
  22.      table.insert(t,cap)
  23.       end
  24.       last_end = e+1
  25.       s, e, cap = str:find(fpat, last_end)
  26.    end
  27.    if last_end <= #str then
  28.       cap = str:sub(last_end)
  29.       table.insert(t, cap)
  30.    end
  31.    return t
  32. end
  33.  
  34. local beans = {}
  35.  
  36. local function scanBeans()
  37.   local allItems = lp.getAvailableItems()
  38.   --print(#allItems, " items in lp system")
  39.   for i,item in pairs(allItems) do
  40.     if lp.getItemID(item[1]) == 25366 then
  41.       local nbt = lp.getNBTTagCompound(item[1])
  42.       if nbt then
  43.         local name = nbt.value.Aspects.value[1].value.key.value
  44.         if not beans[name] then beans[name] = {lpID=item[1], qty=item[2]}
  45.         else print("duplicate bean: ",name) end
  46.       end
  47.     end
  48.   end
  49. end
  50.  
  51. local function readRecipes()
  52.   local recipeFile = fs.open("recipes","r")
  53.   if not recipeFile then return 0 end
  54.   local line = recipeFile.readLine()
  55.   while line do
  56.     local recipe = split(line,",")
  57.     if #recipe % 2 ~= 1 then break end
  58.     recipes[recipe[1]] = {}
  59.     for i=2,#recipe,2 do
  60.       table.insert(recipes[recipe[1]],{type=recipe[i],qty=recipe[i+1]})
  61.     end
  62.     line = recipeFile.readLine()
  63.   end
  64.   recipeFile.close()
  65. end
  66.  
  67. scanBeans()
  68. readRecipes()
  69. --for i,k in pairs(recipes) do print(i); for n=1,#k do for j,l in pairs(k[n]) do print(j," ",l) end end end
  70. --for i,k in pairs(recipes) do print(i," ",k) end
  71.  
  72. local function clearInventory()
  73.   for i=1,16 do
  74.     local n = turtle.getItemCount(i)
  75.     if ae.pullItem("down",i,n) - n ~= 0 then i = i - 1 end
  76.   end
  77. end
  78.  
  79. clearInventory()
  80.  
  81. while true do
  82.   os.pullEvent("turtle_inventory")
  83.   --print("inventory's changed")
  84.   wifi.transmit(channel,channel,"getJobList")
  85.   local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  86.   --for i,k in pairs(message[1]) do print(i," ",k) end
  87.   --if message and message[1] and message[1].name then print(message[1].name) end
  88.   if message then
  89.     for i=1,#message do
  90.       if message[i] and message[i].name and message[i].qty and recipes[message[i].name] then print(message[i].name," ",message[i].qty) end
  91.     end
  92.   end
  93. end
Advertisement
Add Comment
Please, Sign In to add comment