Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local robot = require("robot");
- local computer = require("computer");
- local component = require("component");
- local term = require("term");
- local inv = component.inventory_controller;
- local crafting = component.crafting;
- local InvSide = 3;
- local iSize = inv.getInventorySize(InvSide);
- local rSize = robot.inventorySize();
- function GetStack(Label, num)
- robot.select(16);
- robot.drop();
- local i = 0;
- for i=1, iSize do
- if not (inv.getStackInSlot(InvSide, i) == nil) then
- if inv.getStackInSlot(InvSide, i).label == Label then
- return inv.suckFromSlot(InvSide, i, num);
- end
- end
- end
- return false;
- end
- function Find(Label, side)
- local i = 0;
- for i=1, inv.getInventorySize(side) do
- if not (inv.getStackInSlot(side, i) == nil) then
- if inv.getStackInSlot(side, i).label == Label then
- return i, inv.getStackInSlot(side, i).size;
- end
- end
- end
- return 0, 0;
- end
- function Put()
- local i = 0;
- for i=1,rSize do
- robot.select(i);
- robot.drop();
- end
- end
- function WriteTemplate()
- local f = io.open("Templates.acr", "a");
- if (f == nil) then
- print("Failed to open the file Templates.acr");
- return -1;
- end
- robot.select(16);
- robot.drop();
- local SourceLbl = {};
- local i = 0;
- local j = 0;
- for i=0,2 do
- for j=1,3 do
- if inv.getStackInInternalSlot(4*i+j) == nil then
- SourceLbl[3*i+j] = "-";
- else
- SourceLbl[3*i+j] = inv.getStackInInternalSlot(4*i+j).label;
- end
- end
- end
- if (crafting.craft(1) == false) or (robot.count() <= 0) then
- print("Pattern validation failed!");
- f:close();
- return -2;
- end
- f:write("\n".."TMP::"..inv.getStackInInternalSlot(16).label.."\n");
- f:write(robot.count().."\n");
- for i=1,9 do
- f:write(SourceLbl[i].."\n");
- end
- f:close();
- print("Successfully created a pattern for "..inv.getStackInInternalSlot(16).label);
- return 0;
- end
- function Craft(Label, num)
- local f = io.open("Templates.acr", "r");
- if (f == nil) then
- print("Failed to open the file Templates.acr");
- return -1;
- end
- local i = 0;
- local j = 0;
- local tmp = "";
- local reqLabel = "TMP::"..Label;
- -- Find the required pattern
- repeat
- tmp = f:read();
- if (tmp == nil) then
- print("Cannot find component or pattern: "..Label);
- f:close();
- return -2;
- end
- until
- tmp == reqLabel;
- print("Successfully found the pattern for "..Label);
- local tempnum = 0;
- local outnum = tonumber(f:read());
- if (outnum == nil) or (outnum == 0) then
- print("Error in Templates.acr file format!");
- end
- tempnum = math.ceil(num/outnum);
- -- Read the components
- local SourceLbl = {};
- local MatTable = {};
- for i=1,9 do
- SourceLbl[i] = f:read();
- if not (SourceLbl[i] == "-") then
- if (MatTable[SourceLbl[i]] == nil) then
- MatTable[SourceLbl[i]] = 1;
- else
- MatTable[SourceLbl[i]] = MatTable[SourceLbl[i]] + 1;
- end
- end
- end
- f:close();
- -- Check if all the components are present, craft some if necessary
- local sl = 0;
- local cnt = 0;
- for k,v in pairs(MatTable) do
- sl, cnt = Find(k, InvSide);
- if (cnt < v*tempnum) then
- print("Not enough "..k.." ("..cnt.."/"..v*tempnum..")! Trying to craft");
- if (Craft(k, v*tempnum-cnt) < 0) then
- return -3;
- end
- sl, cnt = Find(k, InvSide);
- if (cnt < v*tempnum) then
- print("Error crafting "..k);
- return -4;
- end
- end
- robot.select(16);
- inv.suckFromSlot(InvSide, sl, v*tempnum);
- robot.dropDown();
- end
- -- Place the components into the grid
- print("All components found for "..Label..". Placing them in the grid");
- for i=0,2 do
- for j=1,3 do
- if not (SourceLbl[3*i+j] == "-") then
- sl, cnt = Find(SourceLbl[3*i+j], 0);
- if (cnt < tempnum) then
- print("Strange, I only have "..cnt.."/"..tempnum.." of "..SourceLbl[3*i+j]);
- return -5;
- end
- robot.select(4*i+j);
- if inv.suckFromSlot(0, sl, tempnum) == false then
- print("Failed to extract "..SourceLbl[3*i+j].."! Check my permissions!");
- return -6;
- end
- end
- end
- end
- -- Finally crafting something!
- print("Crafting "..Label.."!");
- robot.select(16);
- --print(math.ceil(num/outnum)*outnum);
- crafting.craft();
- -- Cleaning some garbage
- print("Successfully crafted "..num.." of "..Label);
- Put();
- return 0;
- end
- function GetCraft(Label, num)
- --Put();
- robot.select(16);
- robot.drop();
- local slot, cnt = Find(Label, InvSide);
- if (cnt < num) then
- print("Found "..cnt.."/"..num.." of "..Label..". Crafting the remaining");
- if Craft(Label, num-cnt) < 0 then
- print("Failure to craft "..Label.."!");
- computer.beep(600);
- computer.beep(600);
- computer.beep(400);
- return false;
- else
- slot, cnt = Find(Label, InvSide);
- if (cnt == 0) then
- print("That's curious, but someone stole my "..Label);
- computer.beep(800);
- computer.beep(400);
- return false;
- end
- end
- end
- inv.suckFromSlot(InvSide, slot, num);
- computer.beep(800);
- computer.beep(800);
- computer.beep(1000);
- return true;
- end
- function DisplayCat()
- local f = io.open("Templates.acr", "r");
- if (f == nil) then
- print("Failed to open the file Templates.acr!");
- return false;
- end
- local catDB = {};
- local txt = "";
- local ind = 0;
- repeat
- txt = f:read();
- if (txt == nil) then
- break;
- end
- if (string.sub(txt, 1, 5) == "TMP::") then
- ind=ind+1;
- catDB[ind] = string.sub(txt, 6);
- end
- until
- txt == nil;
- f:close()
- term.clear();
- print("Your catalogue of "..ind.." items is ready");
- print("The catalogue consists of "..math.ceil(ind/10).." pages");
- print("Enter 'a' and 'd' to list through the pages");
- print("Enter 'page' to go to specific page");
- print("Enter 'craft' to craft a specific item");
- print("Enter 'q' to quit");
- local pg = 0;
- local tsk = 0;
- while true do
- io.write("> ");
- txt = io.read();
- if txt == "q" then
- return true;
- elseif txt == "a" then
- if (pg > 1) then
- pg = pg - 1;
- end
- elseif txt == "d" then
- if (pg < math.ceil(ind/10)) then
- pg = pg + 1;
- end
- elseif string.sub(txt, 1, 4) == "page" then
- tsk = tonumber(string.sub(txt, 6));
- if (not (tsk == nil)) and (tsk > 0) and (tsk <= math.ceil(ind/10)) then
- pg = tsk;
- end
- elseif string.sub(txt, 1, 5) == "craft" then
- tsk = tonumber(string.sub(txt, 7));
- if (not (tsk == nil)) and (tsk > 0) and (tsk <= ind) then
- io.write("Please enter the quantity: ");
- pg = io.read("*n");
- if (pg == nil) or (pg <= 0) then
- pg = 1;
- else
- GetCraft(catDB[tsk], pg);
- end
- end
- end
- term.clear();
- for i=10*(pg-1)+1,10*(pg) do
- if catDB[i] == nil then
- break;
- end
- print(i..": "..catDB[i]);
- end
- print("Page "..pg.." of "..math.ceil(ind/10));
- end
- end
- while true do
- io.write("Enter your request: ");
- local cmd = io.read();
- local num = 0;
- if (cmd == "Put") then
- Put();
- elseif (cmd == "Get") then
- io.write("Enter the name of the item required: ");
- cmd = io.read();
- io.write("Enter the required amount: ");
- num = io.read("*n");
- if (num == nil) then
- print("Quantity should be a number!");
- else
- GetCraft(cmd, num);
- end
- elseif (cmd == "Temp") then
- print("Place all the components on the grid and press Enter");
- io.read();
- WriteTemplate();
- elseif (cmd == "Cat") then
- DisplayCat();
- elseif (cmd == "Quit") then
- os.exit();
- else
- print("'Put' to clear the robot's inventory");
- print("'Get' to get a specific item from the chest, or craft if the item is missing");
- print("'Temp' to store a new template");
- print("'Cat' to display the full catalogue of craftable items");
- print("'Quit' to quit the program");
- end
- end
Advertisement
RAW Paste Data
Copied
Advertisement