Advertisement
Guest User

autocraft.lua

a guest
Dec 25th, 2018
215
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local robot = require("robot");
  2. local computer = require("computer");
  3. local component = require("component");
  4. local term = require("term");
  5. local inv = component.inventory_controller;
  6. local crafting = component.crafting;
  7. local InvSide = 3;
  8.  
  9. local iSize = inv.getInventorySize(InvSide);
  10. local rSize = robot.inventorySize();
  11.  
  12. function GetStack(Label, num)
  13.   robot.select(16);
  14.   robot.drop();
  15.   local i = 0;
  16.   for i=1, iSize do
  17.     if not (inv.getStackInSlot(InvSide, i) == nil) then
  18.       if inv.getStackInSlot(InvSide, i).label == Label then
  19.         return inv.suckFromSlot(InvSide, i, num);    
  20.       end
  21.     end
  22.   end
  23.   return false;
  24. end
  25.  
  26. function Find(Label, side)
  27.   local i = 0;
  28.   for i=1, inv.getInventorySize(side) do    
  29.     if not (inv.getStackInSlot(side, i) == nil) then
  30.       if inv.getStackInSlot(side, i).label == Label then
  31.         return i, inv.getStackInSlot(side, i).size;
  32.       end
  33.     end
  34.   end
  35.   return 0, 0;
  36. end
  37.  
  38. function Put()
  39.   local i = 0;
  40.   for i=1,rSize do
  41.     robot.select(i);
  42.     robot.drop();
  43.   end
  44. end
  45.  
  46. function WriteTemplate()
  47.   local f = io.open("Templates.acr", "a");
  48.   if (f == nil) then
  49.     print("Failed to open the file Templates.acr");
  50.     return -1;
  51.   end
  52.  
  53.   robot.select(16);
  54.   robot.drop();
  55.   local SourceLbl = {};
  56.   local i = 0;
  57.   local j = 0;
  58.  
  59.   for i=0,2 do
  60.     for j=1,3 do    
  61.       if inv.getStackInInternalSlot(4*i+j) == nil then
  62.         SourceLbl[3*i+j] = "-";
  63.       else
  64.         SourceLbl[3*i+j] = inv.getStackInInternalSlot(4*i+j).label;
  65.       end
  66.     end
  67.   end
  68.  
  69.   if (crafting.craft(1) == false) or (robot.count() <= 0) then
  70.     print("Pattern validation failed!");
  71.     f:close();
  72.     return -2;
  73.   end
  74.  
  75.   f:write("\n".."TMP::"..inv.getStackInInternalSlot(16).label.."\n");
  76.   f:write(robot.count().."\n");
  77.  
  78.   for i=1,9 do
  79.     f:write(SourceLbl[i].."\n");
  80.   end
  81.  
  82.   f:close();
  83.   print("Successfully created a pattern for "..inv.getStackInInternalSlot(16).label);
  84.   return 0;
  85. end
  86.  
  87. function Craft(Label, num)
  88.   local f = io.open("Templates.acr", "r");
  89.   if (f == nil) then
  90.     print("Failed to open the file Templates.acr");
  91.     return -1;
  92.   end
  93.  
  94.   local i = 0;
  95.   local j = 0;
  96.   local tmp = "";
  97.   local reqLabel = "TMP::"..Label;
  98.  
  99.   -- Find the required pattern
  100.   repeat
  101.     tmp = f:read();
  102.     if (tmp == nil) then
  103.       print("Cannot find component or pattern: "..Label);
  104.       f:close();
  105.       return -2;
  106.     end
  107.   until
  108.     tmp == reqLabel;
  109.  
  110.   print("Successfully found the pattern for "..Label);
  111.   local tempnum = 0;
  112.   local outnum = tonumber(f:read());
  113.   if (outnum == nil) or (outnum == 0) then
  114.     print("Error in Templates.acr file format!");
  115.   end
  116.   tempnum = math.ceil(num/outnum);
  117.  
  118.   -- Read the components
  119.   local SourceLbl = {};
  120.   local MatTable = {};
  121.   for i=1,9 do
  122.     SourceLbl[i] = f:read();
  123.     if not (SourceLbl[i] == "-") then
  124.       if (MatTable[SourceLbl[i]] == nil) then
  125.         MatTable[SourceLbl[i]] = 1;
  126.       else
  127.         MatTable[SourceLbl[i]] = MatTable[SourceLbl[i]] + 1;
  128.       end
  129.     end
  130.   end
  131.   f:close();
  132.  
  133.   -- Check if all the components are present, craft some if necessary
  134.   local sl = 0;
  135.   local cnt = 0;
  136.   for k,v in pairs(MatTable) do
  137.     sl, cnt = Find(k, InvSide);
  138.     if (cnt < v*tempnum) then
  139.       print("Not enough "..k.." ("..cnt.."/"..v*tempnum..")! Trying to craft");
  140.       if (Craft(k, v*tempnum-cnt) < 0) then
  141.         return -3;
  142.       end
  143.       sl, cnt = Find(k, InvSide);
  144.       if (cnt < v*tempnum) then
  145.         print("Error crafting "..k);
  146.         return -4;
  147.       end
  148.   end
  149.   robot.select(16);
  150.   inv.suckFromSlot(InvSide, sl, v*tempnum);
  151.   robot.dropDown();
  152.   end
  153.  
  154.   -- Place the components into the grid
  155.   print("All components found for "..Label..". Placing them in the grid");
  156.   for i=0,2 do
  157.     for j=1,3 do
  158.       if not (SourceLbl[3*i+j] == "-") then
  159.         sl, cnt = Find(SourceLbl[3*i+j], 0);
  160.         if (cnt < tempnum) then
  161.           print("Strange, I only have "..cnt.."/"..tempnum.." of "..SourceLbl[3*i+j]);
  162.           return -5;
  163.         end
  164.         robot.select(4*i+j);
  165.         if inv.suckFromSlot(0, sl, tempnum) == false then
  166.           print("Failed to extract "..SourceLbl[3*i+j].."! Check my permissions!");
  167.           return -6;
  168.         end
  169.       end
  170.     end
  171.   end
  172.  
  173.   -- Finally crafting something!
  174.   print("Crafting "..Label.."!");
  175.   robot.select(16);
  176.   --print(math.ceil(num/outnum)*outnum);
  177.   crafting.craft();
  178.  
  179.   -- Cleaning some garbage
  180.   print("Successfully crafted "..num.." of "..Label);
  181.   Put();
  182.   return 0;  
  183. end
  184.  
  185. function GetCraft(Label, num)
  186.   --Put();
  187.   robot.select(16);
  188.   robot.drop();
  189.   local slot, cnt = Find(Label, InvSide);
  190.   if (cnt < num) then
  191.     print("Found "..cnt.."/"..num.." of "..Label..". Crafting the remaining");
  192.     if Craft(Label, num-cnt) < 0 then
  193.       print("Failure to craft "..Label.."!");
  194.       computer.beep(600);
  195.       computer.beep(600);
  196.       computer.beep(400);
  197.       return false;
  198.     else
  199.       slot, cnt = Find(Label, InvSide);
  200.       if (cnt == 0) then
  201.         print("That's curious, but someone stole my "..Label);
  202.         computer.beep(800);
  203.         computer.beep(400);
  204.         return false;
  205.       end
  206.     end
  207.   end
  208.   inv.suckFromSlot(InvSide, slot, num);
  209.   computer.beep(800);
  210.   computer.beep(800);
  211.   computer.beep(1000);
  212.   return true;
  213. end
  214.  
  215. function DisplayCat()
  216.   local f = io.open("Templates.acr", "r");
  217.   if (f == nil) then
  218.     print("Failed to open the file Templates.acr!");
  219.     return false;
  220.   end
  221.  
  222.   local catDB = {};
  223.   local txt = "";
  224.   local ind = 0;
  225.   repeat
  226.     txt = f:read();
  227.     if (txt == nil) then
  228.       break;
  229.     end
  230.     if (string.sub(txt, 1, 5) == "TMP::") then
  231.       ind=ind+1;
  232.       catDB[ind] = string.sub(txt, 6);
  233.     end
  234.   until
  235.     txt == nil;
  236.   f:close()
  237.   term.clear();
  238.   print("Your catalogue of "..ind.." items is ready");
  239.   print("The catalogue consists of "..math.ceil(ind/10).." pages");
  240.   print("Enter 'a' and 'd' to list through the pages");
  241.   print("Enter 'page' to go to specific page");
  242.   print("Enter 'craft' to craft a specific item");
  243.   print("Enter 'q' to quit");
  244.  
  245.   local pg = 0;
  246.   local tsk = 0;
  247.   while true do
  248.     io.write("> ");
  249.     txt = io.read();
  250.     if txt == "q" then
  251.       return true;
  252.     elseif txt == "a" then
  253.       if (pg > 1) then
  254.         pg = pg - 1;
  255.       end
  256.     elseif txt == "d" then
  257.       if (pg < math.ceil(ind/10)) then
  258.         pg = pg + 1;
  259.       end
  260.     elseif string.sub(txt, 1, 4) == "page" then
  261.       tsk = tonumber(string.sub(txt, 6));
  262.       if (not (tsk == nil)) and (tsk > 0) and (tsk <= math.ceil(ind/10)) then
  263.         pg = tsk;
  264.       end
  265.     elseif string.sub(txt, 1, 5) == "craft" then
  266.       tsk = tonumber(string.sub(txt, 7));
  267.       if (not (tsk == nil)) and (tsk > 0) and (tsk <= ind) then
  268.         io.write("Please enter the quantity: ");
  269.         pg = io.read("*n");
  270.         if (pg == nil) or (pg <= 0) then
  271.           pg = 1;
  272.         else
  273.           GetCraft(catDB[tsk], pg);
  274.         end
  275.       end
  276.     end
  277.    
  278.     term.clear();
  279.     for i=10*(pg-1)+1,10*(pg) do
  280.       if catDB[i] == nil then
  281.         break;
  282.       end
  283.       print(i..": "..catDB[i]);
  284.     end
  285.     print("Page "..pg.." of "..math.ceil(ind/10));
  286.   end
  287. end
  288.  
  289. while true do
  290.   io.write("Enter your request: ");
  291.   local cmd = io.read();
  292.   local num = 0;
  293.   if (cmd == "Put") then
  294.     Put();
  295.   elseif (cmd == "Get") then
  296.     io.write("Enter the name of the item required: ");
  297.     cmd = io.read();
  298.     io.write("Enter the required amount: ");
  299.     num = io.read("*n");
  300.     if (num == nil) then
  301.       print("Quantity should be a number!");  
  302.     else
  303.       GetCraft(cmd, num);
  304.     end
  305.   elseif (cmd == "Temp") then
  306.     print("Place all the components on the grid and press Enter");
  307.     io.read();
  308.     WriteTemplate();
  309.   elseif (cmd == "Cat") then
  310.   DisplayCat();
  311.   elseif (cmd == "Quit") then
  312.     os.exit();
  313.   else
  314.   print("'Put' to clear the robot's inventory");
  315.   print("'Get' to get a specific item from the chest, or craft if the item is missing");
  316.   print("'Temp' to store a new template");
  317.   print("'Cat' to display the full catalogue of craftable items");
  318.   print("'Quit' to quit the program");
  319.   end
  320. end
Advertisement
RAW Paste Data Copied
Advertisement