DuckStrom

CC Thaumcraft ME Aspect Scanner

Feb 6th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.77 KB | None | 0 0
  1. -- set this to the dir of the chest relative to the interface
  2. chest_dir = "north"
  3. scan_wait = 1.2
  4.  
  5. learners = {
  6.   {['aspect']='lux', ['minecraft:torch']={0}},
  7.   {['aspect']='potentia', ['minecraft:coal']={0},['minecraft:coal_ore']={0}},
  8.   {['aspect']='herba', ['minecraft:grass']={0}},
  9.   {['aspect']='motus & arbor', ['minecraft:trapdoor']={0}},
  10.   {['aspect']='vacuos', ['minecraft:bowl']={0}},
  11.   {['aspect']='vitreus', ['minecraft:glass']={0}},
  12.   {['aspect']='mortuus & praecantatio', ['minecraft:potion']={8200,8232,8264}},
  13.   {['aspect']='volatus', ['minecraft:feather']={0}},
  14.   {['aspect']='bestia', ['minecraft:egg']={0}},
  15.   {['aspect']='spiritus & vinculum', ['minecraft:soul_sand']={0},['minecraft:skull']={-1}},
  16.   {['aspect']='cognitio', ['minecraft:paper']={0}},
  17.   {['aspect']='humanus&corpus', ['minecraft:rotten_flesh']={0}},
  18.   {['aspect']='fames & messis', ['minecraft:wheat']={0},['minecraft:bread']={0},['minecraft:apple']={0},['minecraft:carrot']={0},['minecraft:potato']={0}},
  19.   {['aspect']='instrumentum', ['minecraft:flint']={0}},
  20.   {['aspect']='tenebrae', ['minecraft:obsidian']={0}},
  21.   {['aspect']='sensus', ['minecraft:red_flower']={-1},['minecraft:dye']={-1}},
  22.   {['aspect']='fabrico & pannus', ['minecraft:wool']={0}},
  23.   {['aspect']='venenum', ['minecraft:spider_eye']={0}},
  24.   {['aspect']='gelum', ['minecraft:snowball']={0},['minecraft:snow_layer']={0},['minecraft:ice']={0},['minecraft:packed_ice']={0}},
  25.   {['aspect']='sano', ['minecraft:milk_bucket']={0}},
  26.   {['aspect']='meto', ['minecraft:wooden_hoe']={-1},['minecraft:stone_hoe']={-1},['minecraft:iron_hoe']={-1},['minecraft:diamond_hoe']={-1},['minecraft:iron_hoe']={-1},['minecraft:shears']={-1},['ThermalFoundation:tool.shearsWood']={-1},['ThermalFoundation:tool.shearsStone']={-1}},
  27.   {['aspect']='perfodio', ['minecraft:iron_pickaxe']={-1},['minecraft:stone_pickaxe']={-1},['minecraft:iron_pickaxe']={-1},['minecraft:diamond_pickaxe']={-1}},
  28.   {['aspect']='telum', ['minecraft:arrow']={0}},
  29.   {['aspect']='tutamen', ['minecraft:leather']={0},['minecraft:iron_helmet']={-1},['minecraft:iron_chestplate']={-1},['minecraft:iron_leggings']={-1},['minecraft:iron_boots']={-1}},
  30.   {['aspect']='permutatio', ['minecraft:hopper']={0},['minecraft:wheat_seeds']={0},['Thaumcraft:blockCustomOre']={0}},
  31.   {['aspect']='machina & iter', ['minecraft:fence_gate']={0}},
  32.   {['aspect']='metallum', ['minecraft:iron_ore']={0},['minecraft:iron_ingot']={0},['minecraft:iron_block']={0}},
  33.   {['aspect']='lucrum', ['minecraft:gold_ore']={0},['minecraft:gold_ingot']={0},['minecraft:gold_block']={0}},
  34.   {['aspect']='exanimis', ['Thaumcraft:ItemZombieBrain']={0}},
  35.   {['aspect']='alienis', ['minecraft:ender_pearl']={0},['ThaumCraft:blockCosmeticSolid']={0}},
  36.   {['aspect']='limus', ['minecraft:slime_ball']={0}},
  37.   {['aspect']='vitium', ['Thaumcraft:ItemResource']={11,12}},
  38.   {['aspect']='auram', ['Thaumcraft:ItemWispEssence']={0}}
  39. }
  40.  
  41. -- reformats AE item table to 'name:meta'=v,
  42. -- and 'name'.size=(total size for all metas),
  43. -- and 'name'.fingerprints={all fingerprints}
  44. function keyAvailableItems(inv)
  45.   local out = {}
  46.   for k,v in ipairs(inv) do
  47.     local id = v.fingerprint.id
  48.     local meta = v.fingerprint.dmg
  49.     if out[id] ~= nil then
  50.       out[id].size = out[id].size + v.size
  51.     else
  52.       out[id] = {['size'] = v.size}
  53.       out[id].fingerprints = {}
  54.     end
  55.     out[id].fingerprints[#out[id].fingerprints+1] = v.fingerprint
  56.     out[id..':'..meta] = v
  57.   end
  58.   return out
  59. end
  60.  
  61. function moveForScanning(fingerprint)
  62.   pcall(me.exportItem, fingerprint, chest_dir, 1)
  63.   sleep(scan_wait)
  64.   retrieveItems()
  65. end
  66.  
  67. function retrieveItems()
  68.   for i=1,4 do
  69.     pcall(me.pullItem, chest_dir, i, i)
  70.   end
  71. end
  72.  
  73. function startRoutine(event_id)
  74.   curr_mode = event_id
  75.   os.queueEvent('start', event_id)
  76.   os.pullEvent('finish') --wait for whatever function that caught this to finish
  77. end
  78.  
  79. function learnerRoutine()
  80.   retrieveItems()
  81.  
  82.   local inv = me.getAvailableItems()
  83.   local inv_keyed = keyAvailableItems(inv)
  84.  
  85.   local available = {}
  86.  
  87.   local missing = {}
  88.   local missing_ct = 0
  89.  
  90.   for k,v in ipairs(learners) do
  91.     local fingerprint
  92.  
  93.     for k2,v2 in pairs(v) do
  94.       if k2 == 'aspect' then
  95.         --pass
  96.       elseif v2[1] == -1 then
  97.         if inv_keyed[k2] ~= nil then
  98.           fingerprint = inv_keyed[k2].fingerprints[1]
  99.         end
  100.       else
  101.         for _,v3 in ipairs(v2) do
  102.           if inv_keyed[k2..':'..v3] ~= nil then
  103.             fingerprint = inv_keyed[k2..':'..v3].fingerprint
  104.           end
  105.         end
  106.       end
  107.     end
  108.  
  109.     if fingerprint ~= nil then
  110.       available[k] = fingerprint
  111.     else
  112.       missing_ct = missing_ct + 1
  113.       local aspect = v.aspect
  114.       missing[aspect] = ''
  115.       for k2,v2 in pairs(v) do
  116.         if k2 ~= 'aspect' then
  117.           local s = k2 .. ':'
  118.           for _,v3 in ipairs(v2) do
  119.             s = s .. v3 .. ','
  120.           end
  121.  
  122.           missing[aspect] = missing[aspect] .. s .. '; '
  123.         end
  124.       end
  125.     end
  126.   end
  127.  
  128.   if missing_ct > 0 then
  129.     print('Missing:')
  130.     for k,v in pairs(missing) do
  131.       print('  ' .. k .. '=' .. v)
  132.     end
  133.     print('Continue anyway? [y/n]')
  134.     local keyloop = true
  135.     while keyloop do
  136.       local event, key = os.pullEvent('key')
  137.       if key == keys.n then
  138.         return
  139.       elseif key == keys.y then
  140.         keyloop = false
  141.       end
  142.     end
  143.   else
  144.     print('All items accounted for. Continuing.')
  145.   end
  146.  
  147.   print('Delaying 3s to get scanner ready.')
  148.   sleep(3)
  149.  
  150.   for k,v in pairs(available) do
  151.     print('  scanning ' .. v.id)
  152.     moveForScanning(v)
  153.   end
  154. end
  155.  
  156. function scannerRoutine()
  157.   local inv = me.getAvailableItems()
  158.  
  159.   print('Delaying 3s to get scanner ready.')
  160.   sleep(3)
  161.   local inv_size = #inv
  162.   local last_id = 0
  163.   local last_meta = 0
  164.  
  165.   while offset <= inv_size do
  166.     local f = inv[offset].fingerprint
  167.     local id = f.id
  168.     local meta = f.dmg
  169.  
  170.     if last_id ~= id or last_meta ~= meta then
  171.       print('  scanning #'..offset..'='..f.id)
  172.       moveForScanning(f)
  173.       while paused do
  174.         sleep(1)
  175.       end
  176.  
  177.       if stop then
  178.         return
  179.       end
  180.     end
  181.  
  182.     last_id = id
  183.     last_meta = meta
  184.     offset = offset + 1
  185.   end
  186. end
  187.  
  188. function mainLoop()
  189.   while true do
  190.     local event, id = os.pullEvent('start')
  191.     retrieveItems()
  192.     if id == 0 then
  193.       scannerRoutine()
  194.       os.queueEvent('finish')
  195.     elseif id == 1 then
  196.       learnerRoutine()
  197.       print('learnerRoutine() returned')
  198.       os.queueEvent('finish')
  199.     end
  200.   end
  201. end
  202.  
  203. function offsetLoop()
  204.   while true do
  205.     local event, key = os.pullEvent('key')
  206.     if key == keys.enter or key == keys.pause then
  207.       paused = paused == false
  208.       print(paused and 'Paused' or 'Unpaused')
  209.       retrieveItems()
  210.     elseif key == keys['end'] then
  211.       print('Halting')
  212.       retrieveItems()
  213.       stop = true
  214.     else
  215.       local last_offset = offset
  216.       if key == keys.up then
  217.         offset = offset+1
  218.       elseif key == keys.down then
  219.         offset = offset-1
  220.       elseif key == keys.pageUp or key == keys.right then
  221.         offset = offset+64
  222.       elseif key == keys.pageDown or key == keys.left then
  223.         offset = offset-64
  224.       end
  225.       offset = math.max(1,offset)
  226.       if offset ~= last_offset then
  227.         print('New offset: ' .. offset)
  228.       end
  229.     end
  230.   end
  231. end
  232.  
  233. function uiLoop()
  234.   while true do
  235.     term.setCursorPos(1,1)
  236.     term.clear()
  237.     stop = false
  238.     print('Waiting for command...\n  "l" to start learner routine\n  "s" to start scanner routine')
  239.     local event, key = os.pullEvent('key')
  240.     if key == keys.s then
  241.       startRoutine(0)
  242.     elseif key == keys.l then
  243.       startRoutine(1)
  244.     end
  245.   end
  246. end
  247.  
  248. offset = 0
  249. curr_mode = 0
  250.  
  251. me = peripheral.find("tileinterface")
  252.  
  253. parallel.waitForAll(mainLoop,offsetLoop,uiLoop)
Add Comment
Please, Sign In to add comment