HANDZ

Draconic crafting automation (2 turtles - first turtle)

Aug 24th, 2021 (edited)
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.23 KB | None | 0 0
  1. --[[
  2. interface in the front in blocking mode
  3. inventory that goes to injectors on the bottom
  4. inventory that goes to crafting core on the top
  5.  
  6. program recipes and done
  7. (you dont have to use the dummy item)
  8. --]]
  9.  
  10. local dummy = "minecraft:cobblestone"
  11. local second_turtle_id = 12
  12.  
  13. local recipes = {
  14.     -- draconic
  15.     -- awakend draconium
  16.     {
  17.         ingredients = {
  18.             {"draconicevolution:draconic_core", 6}, {"draconicevolution:draconium_block", 4}, {"draconicevolution:dragon_heart", 1}
  19.         },
  20.         core = {"draconicevolution:draconium_block", 4},
  21.         result = {"draconicevolution:draconic_block", 4}
  22.     },
  23.     -- awakend core
  24.     {
  25.         ingredients = {
  26.             {"minecraft:nether_star", 1}, {"draconicevolution:wyvern_core", 4}, {"draconicevolution:draconic_ingot", 5}
  27.         },
  28.         core = {"minecraft:nether_star", 1},
  29.         result = {"draconicevolution:awakened_core", 1}
  30.     },
  31.     -- chaotic core
  32.     {
  33.         ingredients = {
  34.             {"draconicevolution:chaos_shard", 1}, {"draconicevolution:awakened_core", 4}, {"draconicevolution:draconic_ingot", 4}
  35.         },
  36.         core = {"draconicevolution:chaos_shard", 1},
  37.         result = {"draconicevolution:chaotic_core", 1}
  38.     },
  39.     -- draconic energy relay crystal
  40.     {
  41.         ingredients = {
  42.             {"minecraft:diamond", 4}, {"draconicevolution:wyvern_energy_core", 4}, {"draconicevolution:wyvern_core", 1}, {"draconicevolution:energy_crystal", 4},
  43.         },
  44.         core = {"draconicevolution:energy_crystal", 4},
  45.         result = {"draconicevolution:energy_crystal", 4}
  46.     },
  47.     -- projecte
  48.     -- collectors
  49.     {
  50.         ingredients = {
  51.             {"minecraft:glass", 1}, {"minecraft:diamond_block", 1}, {"minecraft:glowstone", 6}, {"minecraft:furnace", 1}
  52.         },
  53.         core = {"minecraft:furnace", 1},
  54.         result = {"projecte:collector_mk1", 1}
  55.     },
  56.     {
  57.         ingredients = {
  58.             {"projecte:item.pe_matter", 1}, {"minecraft:glowstone", 7}, {"projecte:collector_mk1", 1}
  59.         },
  60.         core = {"projecte:collector_mk1", 1},
  61.         result = {"projecte:collector_mk2", 1}
  62.     },
  63.     {
  64.         ingredients = {
  65.             {"projecte:item.pe_matter", 1}, {"minecraft:glowstone", 7}, {"projecte:collector_mk2", 1}
  66.         },
  67.         core = {"projecte:collector_mk2", 1},
  68.         result = {"projecte:collector_mk3", 1}
  69.     },
  70.     -- relayes
  71.     {
  72.         ingredients = {
  73.             {"minecraft:glass", 1}, {"minecraft:diamond_block", 1}, {"minecraft:obsidian", 6}, {"minecraft:furnace", 1}
  74.         },
  75.         core = {"minecraft:furnace", 1},
  76.         result = {"projecte:relay_mk1", 1}
  77.     },
  78.     {
  79.         ingredients = {
  80.             {"projecte:item.pe_matter", 1}, {"minecraft:obsidian", 7}, {"projecte:relay_mk1", 1}
  81.         },
  82.         core = {"projecte:relay_mk1", 1},
  83.         result = {"projecte:relay_mk2", 1}
  84.     },
  85.     {
  86.         ingredients = {
  87.             {"projecte:item.pe_matter", 1}, {"minecraft:obsidian", 7}, {"projecte:relay_mk2", 1}
  88.         },
  89.         core = {"projecte:relay_mk2", 1},
  90.         result = {"projecte:relay_mk3", 1}
  91.     },
  92.     -- energy condenser
  93.     {
  94.         ingredients = {
  95.             {"projecte:item.pe_matter", 4}, {"projecte:item.pe_matter", 4}, {"ironchest:iron_chest", 1}, {"ironchest:iron_chest", 1}, {"projecte:alchemical_chest", 1}
  96.         },
  97.         core = {"projecte:alchemical_chest", 1},
  98.         result = {"projecte:condenser_mk1", 1}
  99.     }
  100. }
  101.  
  102. -- RF upgrade tier1
  103. local rf_upgrade_items = {"draconicevolution:draconium_capacitor"}
  104. for k, v in pairs(rf_upgrade_items) do
  105.     table.insert(recipes,
  106.         {
  107.             ingredients = {
  108.                 {"minecraft:golden_apple", 2}, {"minecraft:diamond", 2}, {"minecraft:ender_eye", 2},{"draconicevolution:draconic_core", 1}, {v, 1}
  109.             },
  110.             core = {v, 1},
  111.             result = {v, 1},
  112.             second_turtle = { name = "draconicevolution:tool_upgrade", damage = 0 }
  113.         }
  114.     )
  115. end
  116.  
  117. -----------------------------------------
  118.  
  119. -- open rednet
  120. rednet.open("left")
  121.  
  122. -- sort recipes
  123. for i = 1, #recipes do
  124.     table.sort(recipes[i].ingredients, function (a, b)
  125.         return a[1] < b[1]
  126.     end)
  127. end
  128.  
  129. -- check if inventory is the same as recipe ingredients
  130. local function is_equal (a, b)
  131.     if #a ~= #b then
  132.         return false
  133.     end
  134.  
  135.     for i = 1, #a do
  136.         if a[i][1] ~= b[i][1] or a[i][2] ~= b[i][2] then
  137.             return false
  138.         end
  139.     end
  140.  
  141.     return true
  142. end
  143.  
  144. -- run all the time
  145. while true do
  146.     local using_dummy = false
  147.  
  148.     -- get items in inventory without dummy
  149.     local inv = {}
  150.     for i=1,16 do
  151.         local slot = turtle.getItemDetail(i)
  152.         if not slot then
  153.             break
  154.         end
  155.         if slot.name ~= dummy then
  156.             table.insert(inv, {slot.name, slot.count})
  157.         else
  158.             using_dummy = true
  159.         end
  160.     end
  161.  
  162.     if #inv > 0 then
  163.         -- sort inventory for checking
  164.         table.sort(inv, function (a, b)
  165.             return a[1] < b[1]
  166.         end)
  167.  
  168.         local to_craft = {}
  169.         -- check for valid recipe
  170.         for i = 1, #recipes do
  171.             if is_equal(recipes[i].ingredients, inv) then
  172.                 to_craft = recipes[i]
  173.                 break
  174.             end
  175.         end
  176.  
  177.         if to_craft["ingredients"] then
  178.             print("Crafting: " .. to_craft.result[2] .. " x " .. to_craft.result[1])
  179.  
  180.             local turtle_error = false
  181.             -- send command to 2nd turtle
  182.             if to_craft.second_turtle then
  183.                 print("Sending item info to 2nd turtle")
  184.                 rednet.send(second_turtle_id, to_craft.second_turtle)
  185.  
  186.                 local waiting_for_response = true
  187.                 while waiting_for_response do
  188.                     local id, msg = rednet.receive()
  189.                     if id == second_turtle_id then
  190.                         if msg == "DONE" then
  191.                             print("Second turtle procesed info successfully")
  192.                         else
  193.                             print("Message from 2nd turtle: " .. msg)
  194.                             turtle_error = true
  195.                         end
  196.                         break
  197.                     end
  198.                 end
  199.             end
  200.  
  201.             if not turtle_error then
  202.                 -- send items to inventories
  203.                 print("Sending items to inventories")
  204.                 for i=1,16 do
  205.                     local slot = turtle.getItemDetail(i)
  206.                     if not slot then
  207.                         break
  208.                     end
  209.                     if slot.name ~= dummy then
  210.                         turtle.select(i)
  211.                         if slot.name == to_craft.core[1] then
  212.                             turtle.dropUp(to_craft.core[2])
  213.                             turtle.dropDown()
  214.                         else
  215.                             turtle.dropDown()
  216.                         end
  217.                     end
  218.                 end
  219.  
  220.                 -- wait for output
  221.                 print("Waiting for output")
  222.                 local done_crafting = false
  223.                 while not done_crafting do
  224.                     for i=1,16 do
  225.                         local slot = turtle.getItemDetail(i)
  226.                         if not slot then
  227.                             break
  228.                         end
  229.                         if slot.name == to_craft.result[1] and slot.count >= to_craft.result[2] then
  230.                             done_crafting = true
  231.                             print("Sending output to inventory")
  232.                             turtle.select(i)
  233.                             turtle.drop(to_craft.result[2])
  234.                             break
  235.                         end
  236.                     end
  237.                     sleep(0.1)
  238.                 end
  239.  
  240.                 -- send command to 2nd turtle
  241.                 if to_craft.second_turtle then
  242.                     print("Sending retrieve command to 2nd turtle")
  243.                     rednet.send(second_turtle_id, "RETRIEVE")
  244.  
  245.                     local waiting_for_response = true
  246.                     while waiting_for_response do
  247.                         local id, msg = rednet.receive()
  248.                         if id == second_turtle_id then
  249.                             if msg == "DONE" then
  250.                                 print("Second turtle procesed command successfully")
  251.                             else
  252.                                 print("Message from 2nd turtle: " .. msg)
  253.                             end
  254.                             break
  255.                         end
  256.                     end
  257.                 end
  258.  
  259.                 -- remove dummy item (in blocking mode there should be only dummy item left)
  260.                 if using_dummy then
  261.                     print("Removing dummy item")
  262.                     for i=1,16 do
  263.                         local slot = turtle.getItemDetail(i)
  264.                         if slot then
  265.                             turtle.select(i)
  266.                             turtle.drop()
  267.                         end
  268.                     end
  269.                 end
  270.  
  271.                 print("Finished crafting\n\n")
  272.             end
  273.         else
  274.             print("Recipe not found")
  275.         end
  276.     end
  277.     sleep(0.1)
  278. end
Advertisement
Add Comment
Please, Sign In to add comment