Advertisement
Guest User

runes

a guest
May 3rd, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.07 KB | None | 0 0
  1. file = "runes.data"
  2. chest_side = "right"
  3. vacuum_side = "bottom"
  4. altar_dir = "down"
  5. vacuum_push_dir = "south"
  6. config_sides = {"right", "left"}
  7. config_width = 12
  8.  
  9. function print_r ( t )  
  10.     s = ""
  11.     local print_r_cache={}
  12.     local function sub_print_r(t,indent)
  13.         if (print_r_cache[tostring(t)]) then
  14.             s = s .. indent.."*"..tostring(t) .. "\n"
  15.         else
  16.             print_r_cache[tostring(t)]=true
  17.             if (type(t)=="table") then
  18.                 for pos,val in pairs(t) do
  19.                     if (type(val)=="table") then
  20.                         s = s .. indent.."["..pos.."] => "..tostring(t).." {" .. "\n"
  21.                         sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
  22.                         s = s .. indent..string.rep(" ",string.len(pos)+6).."}" .. "\n"
  23.                     elseif (type(val)=="string") then
  24.                         s = s .. indent.."["..pos..'] => "'..val..'"' .. "\n"
  25.                     else
  26.                         s = s .. indent.."["..pos.."] => "..tostring(val) .. "\n"
  27.                     end
  28.                 end
  29.             else
  30.                 s = s .. indent..tostring(t) .. "\n"
  31.             end
  32.         end
  33.     end
  34.     if (type(t)=="table") then
  35.         s = s .. tostring(t).." {" .. "\n"
  36.         sub_print_r(t,"  ")
  37.         s = s .. "}" .. "\n"
  38.     else
  39.         sub_print_r(t,"  ")
  40.     end
  41.     s = s .. "\n"
  42.     return s
  43. end
  44. function debug(o)
  45.     local h = fs.open("debug", "a")
  46.     h.write(print_r(o))
  47.     h.write('\n')
  48.     h.close()
  49. end
  50.  
  51. function config()
  52.     runes = {}
  53.     if fs.exists(file) then
  54.         h = fs.open(file, "r")
  55.         runes = textutils.unserialize(h.readAll())
  56.         h.close()
  57.     end
  58.  
  59.     for _, side in pairs(config_sides) do
  60.         s = peripheral.wrap(side)
  61.         for i=1, s.getInventorySize() do
  62.             stack = s.getStackInSlot(i)
  63.             if stack ~= nil then
  64.                 if i % config_width == 1 then
  65.                     rune = stack['raw_name']
  66.                     runes[rune] = {}
  67.                 else
  68.                     table.insert(runes[rune], {stack['qty'], stack['mod_id'], stack['raw_name']})
  69.                 end
  70.             end
  71.         end
  72.     end
  73.     h = fs.open(file, "w")
  74.     h.write(textutils.serialize(runes))
  75.     h.write('\n')
  76.     h.close()
  77. end
  78.  
  79. function check(c, rune)
  80.     local present = 0
  81.     c.condenseItems()
  82.     sleep(0.1)
  83.     for j, item in pairs(rune) do
  84.         local all_stacks = c.getAllStacks()
  85.         for i, stack in pairs(all_stacks) do
  86.             local all = stack.all()
  87.             if all['mod_id'] == item[2] and all['raw_name'] == item[3] and all['qty'] >= item[1] then
  88.                 present = present + 1
  89.             end
  90.         end
  91.     end
  92.     if present == #rune then
  93.         return true
  94.     else
  95.         return false
  96.     end
  97. end
  98.  
  99. function craft(c, rune)
  100.     for j, item in pairs(rune) do
  101.         local all_stacks = c.getAllStacks()
  102.         for i, stack in pairs(all_stacks) do
  103.             local all = stack.all()
  104.             if all['mod_id'] == item[2] and all['raw_name'] == item[3] and all['qty'] >= item[1] then
  105.                 c.pushItem(altar_dir, i, item[1])
  106.             end
  107.         end
  108.     end
  109.     sleep(5)
  110.     local all_stacks = c.getAllStacks()
  111.     for i, stack in pairs(all_stacks) do
  112.         local all = stack.all()
  113.         if all['id'] == "Botania:livingrock" then
  114.             c.pushItem(altar_dir, i, 1)
  115.             break
  116.         end
  117.     end
  118.     redstone.setOutput(vacuum_side, true)
  119.     local v = peripheral.wrap(vacuum_side)
  120.     stacks = #v.getAllStacks()
  121.     while stacks == 0 do
  122.         sleep(0.5)
  123.         stacks = #v.getAllStacks()
  124.         if stacks ~= 0 then
  125.             break
  126.         end
  127.     end
  128.     sleep(0.1)
  129.     redstone.setOutput(vacuum_side, false)
  130.     for i, stack in pairs(v.getAllStacks()) do
  131.         v.pushItem(vacuum_push_dir, i)
  132.     end
  133.     sleep(0.5)
  134. end
  135.  
  136. function cleanup()
  137.     redstone.setOutput(vacuum_side, false)
  138.     local v = peripheral.wrap(vacuum_side)
  139.     for i, stack in pairs(v.getAllStacks()) do
  140.         v.pushItem(vacuum_push_dir, i)
  141.     end
  142.     sleep(1)
  143. end
  144.  
  145. function run()
  146.     c = peripheral.wrap(chest_side)
  147.     cleanup(c)
  148.     if fs.exists(file) then
  149.         h = fs.open(file, "r")
  150.         runes = textutils.unserialize(h.readAll())
  151.         h.close()
  152.     else
  153.         print("ERROR: no data found! Run with argument 'config' to do that now.")
  154.         shell.exit()
  155.     end
  156.     while true do
  157.         local all_stacks = c.getAllStacks()
  158.         for i, stack in pairs(all_stacks) do
  159.             for j, rune in pairs(runes) do
  160.                 local all = stack.all()
  161.                 if all['mod_id'] == rune[1][2] and all['raw_name'] == rune[1][3] and all['qty'] >= rune[1][1] then
  162.                     print("Found rune: " .. i-1)
  163.                     if check(c, rune) then
  164.                         craft(c, rune)
  165.                     end
  166.                 end
  167.             end
  168.         end
  169.         sleep(1)
  170.     end
  171. end
  172.  
  173. action = ...
  174. if action == "config" then
  175.     config()
  176. else
  177.     cleanup()
  178.     run()
  179. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement