TheFogIOF

Autocraft

Apr 30th, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.08 KB | None | 0 0
  1. peripheral.find("modem", rednet.open)
  2. local monitor = peripheral.find("monitor")
  3.  
  4. local turtle = peripheral.wrap("turtle_4")
  5. local storage = peripheral.wrap("extended_drawers:access_point_2")
  6. local buffer_storage = peripheral.wrap("minecraft:chest_3")
  7. local barrel = peripheral.wrap("minecraft:barrel_0")
  8. local turtle_name = peripheral.getName(turtle)
  9. local storage_name = peripheral.getName(storage)
  10. local buffer_storage_name = peripheral.getName(buffer_storage)
  11. local barrel_name = peripheral.getName(barrel)
  12.  
  13. local f_filesys = {} local f_screen = {} local f_libs = {} local f_craft = {}
  14.  
  15. local turtle_slots = {1,2,3,5,6,7,9,10,11}
  16.  
  17. monitor.clear()
  18. monitor.setBackgroundColor(colors.black)
  19.  
  20. local modnames = {"minecraft:","simpleframes:","extended_drawers:","modern_industrialization:","create:"}
  21. local filename = "data"
  22. local itemList = {}
  23. craft_list = {}
  24. start_craft_list = 1
  25. craft_page = 0
  26. toCraft = {}
  27. cancraft_y = 0
  28. iterations_count = 0
  29.  
  30. function table.contains(list, value)
  31.     for _, v in pairs(list) do
  32.       if v == value then
  33.         return true
  34.       end
  35.     end
  36.     return false
  37. end
  38.  
  39. function table.reverse(list)
  40.     local n, m = #list, #list/2
  41.     for i=1, m do
  42.         list[i], list[n-i+1] = list[n-i+1], list[i]
  43.     end
  44.     return list
  45. end
  46.  
  47. function f_filesys.write_to_file(filename,data)
  48.     local file = fs.open(filename, "w")
  49.     if file then
  50.         file.write(textutils.serialize(data))
  51.         file.close()
  52.     else
  53.         print("Error when opening the file")
  54.     end
  55. end
  56.  
  57. function f_filesys.read_from_file(filename)
  58.     local file = fs.open(filename, "r")
  59.     local data = file.readAll()
  60.     file.close()
  61.     return textutils.unserialize(data)
  62. end
  63.  
  64. function f_screen.draw(string,color,x,y)
  65.     monitor.setBackgroundColor(colors.black)
  66.     monitor.setCursorPos(x,y)
  67.     monitor.setBackgroundColor(color)
  68.     monitor.write(string)
  69.     monitor.setBackgroundColor(colors.black)
  70. end
  71.  
  72. function f_libs.parseid(string, type)
  73.     for i=1,#modnames do
  74.         if type == "full" then
  75.             string = string.gsub(string, "||(%d+)","")
  76.         elseif type == "amount" then
  77.             string = string.gsub(string, "(%S+):(%S+)||","")
  78.             str = tonumber(string) if str then return str else return string end
  79.         else
  80.             string = string.gsub(string,modnames[i],"")
  81.             string = string.gsub(string,"||(%d+)","")
  82.         end
  83.     end
  84.     return string
  85. end
  86.  
  87.  
  88. function f_craft.write_recipe()
  89.     local recipe = {}
  90.     local craft_result = ""
  91.     local barrel_slots = {4,5,6,13,14,15,22,23,24}
  92.     for i, slots in pairs(barrel_slots) do
  93.         item_slot = barrel.list()[slots]
  94.         if item_slot == nil then
  95.             table.insert(recipe,"")
  96.             f_screen.draw(string.rep(" ",35),colors.black,1,i)
  97.             f_screen.draw(i..".*empty*",colors.black,1,i)
  98.         else
  99.             local name = barrel.list()[slots]["name"]
  100.             local count = barrel.list()[slots]["count"]
  101.             local item = name.."||"..count
  102.             table.insert(recipe,item)
  103.             barrel.pushItems(turtle_name,slots,count,turtle_slots[i])
  104.             f_screen.draw(string.rep(" ",35),colors.black,1,i)
  105.             f_screen.draw(i.."."..name.."*"..count,colors.black,1,i)
  106.         end
  107.     end
  108.     rednet.send(turtle.getID(),"craft_64")
  109.     local id, msg = rednet.receive(nil,2)
  110.     if msg == "error" then
  111.         print("Error")
  112.         f_screen.draw(string.rep(" ",35),colors.black,1,10)
  113.         f_screen.draw("10.Error",colors.black,1,10)
  114.         for i, _ in pairs(turtle_slots) do
  115.             buffer_storage.pullItems(turtle_name,_,64,1)
  116.         end
  117.         for i, item in pairs(buffer_storage.list()) do
  118.             storage.pullItem(buffer_storage_name,item.name,64)
  119.         end
  120.     else
  121.         craft_result = msg
  122.         print(craft_result)
  123.         f_screen.draw(string.rep(" ",35),colors.black,1,10)
  124.         f_screen.draw("10."..f_libs.parseid(craft_result,"full").."*"..f_libs.parseid(craft_result,"amount"),colors.black,1,10)
  125.         storage.pullItem(turtle_name,f_libs.parseid(craft_result,"full"),64)
  126.     end
  127.     return craft_result, recipe
  128. end
  129.  
  130.  
  131. function f_screen.show_recipes(start, type)
  132.     if type == "main_button" then
  133.         f_screen.draw("Show Recipes",colors.green,3,11)
  134.         os.sleep(0.1)
  135.     end
  136.     f_screen.draw("Show Recipes",colors.gray,3,11)
  137.     f_screen.draw("<<",colors.black,1,26)
  138.     f_screen.draw(">>",colors.black,4,26)
  139.     all_recipes = f_filesys.read_from_file(filename)
  140.     for i=1,13 do
  141.         f_screen.draw(string.rep(" ",35),colors.black,1,i+12)
  142.     end
  143.     for i=0,#craft_list do
  144.         if craft_list[i+start] ~= nil then
  145.             f_screen.draw(f_libs.parseid(craft_list[i+start]),colors.black,1,i+13)
  146.             page = tostring(craft_page)
  147.             f_screen.draw(page,colors.black,7,26)
  148.         else break end
  149.         if i == 12 then break end
  150.     end
  151. end
  152.  
  153. function f_craft.hascraft(item_id) -- returns true of false
  154.     return all_recipes[item_id] ~= nil
  155. end
  156.  
  157. function f_craft.hasitem(item_id)
  158.     local temp_table = {}
  159.     for _i, _ in pairs(storage.items()) do
  160.         for i, item_table in pairs(_) do
  161.             if i == "name" then
  162.                 table.insert(temp_table, item_table)
  163.             end
  164.         end
  165.     end
  166.     if table.contains(temp_table, item_id) then
  167.         return true else return false
  168.     end
  169. end
  170.  
  171. function returnRecipe(str)
  172.     for i=1, #craft_list do
  173.         if f_libs.parseid(craft_list[i],"full") == f_libs.parseid(str,"full") then
  174.             return craft_list[i]
  175.         end
  176.     end
  177. end
  178.  
  179. function f_craft.can_craft(result,recipe,amount,list)
  180.     local newRecipe = {}
  181.     local n = {}
  182.     local count = 1
  183.  
  184.     if next(toCraft) == nil then
  185.         result = f_libs.parseid(result, "full").."||"..f_libs.parseid(result, "amount")*amount
  186.     else
  187.         result = f_libs.parseid(result, "full").."||"..tonumber(f_libs.parseid(result, "amount"))
  188.     end
  189.  
  190.     table.insert(toCraft,result)
  191.  
  192.     for i=1, #recipe do
  193.         local _count = 1
  194.         if recipe[i] ~= "" then
  195.             for _i=i+1, #recipe do
  196.                 if recipe[i]==recipe[_i] then
  197.                     _count = _count + 1
  198.                     recipe[_i] = ""
  199.                 end
  200.             end
  201.             newRecipe[count] = f_libs.parseid(recipe[i], "full").."||".._count*math.ceil(tonumber(f_libs.parseid(result,"amount"))/f_libs.parseid(returnRecipe(result),"amount"))
  202.             count = count + 1
  203.         end
  204.     end
  205.  
  206.     for i=1, #newRecipe do
  207.         n[i] = 0
  208.         for _i, item in ipairs(list) do
  209.             if item.name == f_libs.parseid(newRecipe[i],"full") then
  210.                 if item.count >= f_libs.parseid(newRecipe[i],"amount") then
  211.                     if f_craft.hasitem(item.name) then
  212.                         n[i] = 1
  213.                         item.count = item.count - f_libs.parseid(newRecipe[i],"amount")
  214.                         item.count = item.count + f_libs.parseid(returnRecipe(result), "amount")
  215.                         break
  216.                     end
  217.                 end
  218.             end
  219.         end
  220.     end
  221.     for i=1,#newRecipe do
  222.         if n[i] == 0 then
  223.             if f_craft.hascraft(returnRecipe(newRecipe[i])) then
  224.                 if f_craft.can_craft(newRecipe[i],all_recipes[returnRecipe(newRecipe[i])],amount,list) then
  225.                     n[i] = 1
  226.                 else
  227.                     print("Not found:"..f_libs.parseid(newRecipe[i],"full").." "..tonumber(f_libs.parseid(newRecipe[i],"amount")).."x")
  228.                 end
  229.             else
  230.                 print("Not found:"..f_libs.parseid(newRecipe[i],"full").." "..tonumber(f_libs.parseid(newRecipe[i],"amount")).."x")
  231.             end
  232.         end
  233.     end
  234.     local toReturn = 1
  235.     for i=1,#n do
  236.         if n[i] == 0 then
  237.             toReturn = toReturn - 1
  238.         end
  239.     end
  240.     if toReturn == 1 then return true
  241.     else return false end
  242. end
  243.  
  244. function f_craft.craft(craft,count)
  245.     local craft_table = f_filesys.read_from_file(filename)
  246.     for i=1, #craft_table[craft] do
  247.         if craft_table[craft][i] == "" then
  248.         else
  249.             storage.pushItem(buffer_storage_name, f_libs.parseid(craft_table[craft][i],"full"), math.ceil(f_libs.parseid(craft_table[craft][i],"amount")*count/f_libs.parseid(craft,"amount")))
  250.             buffer_storage.pushItems(turtle_name, 1, math.ceil(f_libs.parseid(craft_table[craft][i],"amount")*count/f_libs.parseid(craft,"amount")), turtle_slots[i])
  251.         end
  252.     end
  253.     rednet.send(turtle.getID(),"craft_"..count)
  254.     sleep(0.5)
  255.     storage.pullItem(turtle_name,f_libs.parseid(craft,"full"),64)
  256.     print("Crafted:",f_libs.parseid(craft,"full").."||"..f_libs.parseid(returnRecipe(craft),"amount")*math.ceil(count/f_libs.parseid(craft,"amount")))
  257.     sleep(0.5)
  258. end
  259.  
  260. function f_craft.craft_by_table(list)
  261.     table.reverse(list)
  262.     for i, craft in pairs(list) do
  263.         f_craft.craft(returnRecipe(f_libs.parseid(craft,"full")),f_libs.parseid(craft,"amount"))
  264.     end
  265. end
  266.  
  267. function f_libs.update_craft_list()
  268.     craft_list = {}
  269.     for i, key in pairs(all_recipes) do
  270.         for _i, _ in pairs(key) do
  271.             if _i % 8 == 0 then
  272.                 table.insert(craft_list,i)
  273.             end
  274.         end
  275.     end
  276.     table.sort(craft_list)
  277. end
  278.  
  279. print("Amount:")
  280. amount = read()
  281. term.clear()
  282. term.setCursorPos(1,1)
  283. all_recipes = f_filesys.read_from_file(filename)
  284.  
  285. f_libs.update_craft_list()
  286.  
  287. while true do
  288.     f_screen.draw("|Read Recipe|",colors.gray,20,11)
  289.     f_screen.draw("Save",colors.gray,25,12)
  290.     f_screen.draw("Show Recipes",colors.gray,3,11)
  291.     event, side, x, y = os.pullEvent("monitor_touch")
  292.     if x > 20 and x < 34 and y == 11 then
  293.         f_screen.draw("|Read Recipe|",colors.green,20,11)
  294.         rep,recipe = f_craft.write_recipe()
  295.         os.sleep(0.25)
  296.         f_screen.draw("|Read Recipe|",colors.gray,20,11)
  297.     elseif x > 24 and x < 29 and y == 12 then
  298.         f_screen.draw("save",colors.green,25,12)
  299.         all_recipes[rep] = recipe
  300.         os.sleep(0.25)
  301.         f_screen.draw("save",colors.gray,25,12)
  302.         f_filesys.write_to_file(filename,all_recipes)
  303.         f_screen.draw("done",colors.green,25,13)
  304.         os.sleep(0.25)
  305.         f_screen.draw("    ",colors.black,25,13)
  306.     elseif x > 3 and x < 16 and y == 11 then
  307.         f_libs.update_craft_list()
  308.         f_screen.show_recipes(1,"main_button")
  309.     elseif x > 0 and y > 12 and y < 26 and craft_list[y-13+start_craft_list] ~= nil and x < string.len(f_libs.parseid(craft_list[y-13+start_craft_list])) then
  310.         itemList = storage.items()
  311.         toCraft = {}
  312.         iterations_count = 0
  313.         all_recipes = f_filesys.read_from_file(filename)
  314.         if f_craft.can_craft(craft_list[y-13+start_craft_list],all_recipes[craft_list[y-13+start_craft_list]],amount,itemList) then
  315.             cancraft_y = y
  316.             f_screen.show_recipes(start_craft_list)
  317.             f_screen.draw("craft",colors.green,string.len(f_libs.parseid(craft_list[y-13+start_craft_list]))+2,y)
  318.             f_screen.draw(f_libs.parseid(craft_list[y-13+start_craft_list]),colors.gray,1,y)
  319.         else
  320.             cancraft_y = 0
  321.             f_screen.show_recipes(start_craft_list)
  322.             f_screen.draw("cant craft",colors.red,string.len(f_libs.parseid(craft_list[y-13+start_craft_list]))+2,y)
  323.             f_screen.draw(f_libs.parseid(craft_list[y-13+start_craft_list]),colors.gray,1,y)
  324.             os.sleep(0.5)
  325.             f_screen.draw(f_libs.parseid(craft_list[y-13+start_craft_list]),colors.black,1,y)
  326.             f_screen.draw("          ",colors.black,string.len(f_libs.parseid(craft_list[y-13+start_craft_list]))+2,y)
  327.         end
  328.         for i=1, #toCraft do
  329.            print("toCraft["..i.."] "..toCraft[i])
  330.         end
  331.     elseif y > 12 and y < 26 and craft_list[y-13+start_craft_list] ~= nil and x > string.len(f_libs.parseid(craft_list[y-13+start_craft_list]))+2 and x < string.len(f_libs.parseid(craft_list[y-13+start_craft_list]))+7 and cancraft_y == y then
  332.         os.sleep(0.1)
  333.         f_screen.draw("     ",colors.black,string.len(f_libs.parseid(craft_list[y-13+start_craft_list]))+2,y)
  334.         all_recipes = f_filesys.read_from_file(filename)
  335.         f_craft.craft_by_table(toCraft)
  336.         print("Final Item Crafted:"..f_libs.parseid(craft_list[y-13+start_craft_list]).." x"..tonumber(f_libs.parseid(craft_list[y-13+start_craft_list], "amount"))*amount)
  337.         os.sleep(3)
  338.         f_screen.draw("Crafted, check your computer",colors.red,1,y)
  339.         print("Amount:")
  340.         amount = read()
  341.         term.clear()
  342.         term.setCursorPos(1,1)
  343.         all_recipes = f_filesys.read_from_file(filename)
  344.         f_libs.update_craft_list()
  345.         f_screen.show_recipes(1)
  346.     elseif x < 4 and y == 26 and start_craft_list-13 >= 1 then
  347.         start_craft_list = start_craft_list-13
  348.         craft_page = craft_page-1
  349.         f_screen.show_recipes(start_craft_list)
  350.     elseif x > 3 and x < 6 and y == 26 and start_craft_list < #craft_list then
  351.         start_craft_list = start_craft_list+13
  352.         craft_page = craft_page+1
  353.         f_screen.show_recipes(start_craft_list)
  354.     else
  355.         print("*empty*")
  356.     end
  357. end
Add Comment
Please, Sign In to add comment