Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tunnelLength, branchLength, fuelNeeded, numBranch, enderchest, net, home, enderSlot, bucketSlot, receiverId, percentage, phase, phaseColor, fuelFound, homeFuel, startFuel;
- local arg={ ... };
- enderchest=false;
- net=false;
- home=false;
- enderSlot=14;
- torchSlot=15
- bucketSlot=16;
- receiverId=-1;
- percentage=0;
- fuelFound=0;
- homeFuel=0;
- startFuel=turtle.getFuelLevel();
- phase="";
- phaseColor=colors.white;
- local function setup() --Setup for the program
- local event,char
- print("How long the tunnel should be?");
- tunnelLength=tonumber(io.read());
- print("How long a branch should be?");
- branchLength=tonumber(io.read());
- if (enderchest)
- then
- if (turtle.getItemCount(enderSlot)==0)
- then
- print("\nPut an EnderChest (from EnderStorage) in slot "..tostring(enderSlot).."!\nPress any key to continue.");
- os.pullEvent("key");
- end
- else
- print("\nPut a large chest to the left of the turtle!\nPress any key to continue.");
- os.pullEvent("key");
- end
- if (turtle.getItemCount(bucketSlot)==0)
- then
- print("\nPut an empty bucket in slot "..tostring(bucketSlot).."!\nPress any key to continue.");
- os.pullEvent("key");
- end
- if (turtle.getItemCount(torchSlot)==0)
- then
- print("\nPut some torches in slot "..tostring(torchSlot).."!\nPress any key to continue.");
- os.pullEvent("key");
- end
- sleep(0.2);
- print("\nMain tunnel length: "..tunnelLength.."\nIs this correct? Y/N");
- event,char=os.pullEvent("char");
- if(char=="n")
- then
- print("How long the tunnel should be?");
- tunnelLength=tonumber(io.read());
- end
- sleep(0.2);
- print("\nTunnel branches length: "..branchLength.."\nIs this correct? Y/N");
- event, char=os.pullEvent("char");
- if(char=="n")
- then
- print("How long a branch should be?");
- branchLength=tonumber(io.read());
- end
- end
- local function help() --Print the help guide
- print("To use this program the turtle must have a pickaxe on the left and a wireless modem to the right.")
- print("\nArguments:");
- print("\nhelp:\nDisplay this help guide.");
- print("\nstart:\nStart the program.");
- os.pullEvent("key");
- print("\nAdditional arguments:");
- print("\nenderchest:\nWhen the inventory is full, the Turtle places an Ender Chest to empty its inventory, otherwise it returns to home to empty its inventory. (Place a chest to the left of the turtle before running the program)");
- os.pullEvent("key");
- print("\nrednet id:\nSends data over the rednet. Replace \"id\" with the id of the receiver computer. Get the receiver command: pastebin get R89UWx10 BMReceiver");
- os.pullEvent("key");
- print("\nhome:\nMakes the turtle go at the start position after a server restart.");
- os.pullEvent("key");
- print("\n\nExample:\nBranchMine start enderchest rednet 10");
- print("\nThe Turtle will place down the EnderChest (from EnderStorage) to empty its inventory, and the data will be sent to the pc with id 10.")
- end
- local function sendData() --Send the data to the receiver
- if (not net)
- then
- return
- end
- percentage=(((startFuel-turtle.getFuelLevel())-homeFuel)*100)/fuelNeeded;
- msg=""..percentage..","..phase..","..phaseColor..","..turtle.getFuelLevel()..","..fuelFound;
- rednet.send(receiverId, msg);
- end
- local function checkFull() --Check if inventory is full
- if (not enderchest)
- then
- numb=14;
- else
- numb=13;
- end
- for i=1,numb
- do
- if (turtle.getItemCount(i)==0)
- then
- return false;
- end
- end
- return true;
- end
- local function updateFile() --Update the file
- local file, str, _net, _enderchest;
- if (not home)
- then
- return;
- end
- _net=0;
- _enderchest=0;
- if (net)
- then
- _net=1;
- end
- if (enderchest)
- then
- _enderchest=1;
- end
- file=fs.open("BMStatus", "w");
- str=advMovAPI.getX()..","..advMovAPI.getY()..","..advMovAPI.getDir()..",".._enderchest..",".._net..","..receiverId..","..fuelFound..","..percentage;
- file.write(str);
- file.close();
- end
- local function turnL()
- advMovAPI.turnL();
- updateFile();
- end
- local function turnR()
- advMovAPI.turnR();
- updateFile();
- end
- local function returnBack(x, y, dir) --Go to coords
- while(advMovAPI.getY()~=y)
- do
- advMovAPI.forward(true);
- homeFuel=homeFuel+1;
- end
- if (advMovAPI.getX()<x)
- then
- turnR();
- elseif(advMovAPI.getX()>x)
- then
- turnL();
- end
- while(advMovAPI.getX()~=x)
- do
- advMovAPI.forward(true);
- homeFuel=homeFuel+1;
- end
- while(advMovAPI.getDir()>dir)
- do
- turnR();
- end
- while(advMovAPI.getDir()<dir)
- do
- turnL();
- end
- end
- local function dropAll(ender) --Drop all item in the inventory
- local slot, numb, prevSlot, prevPhase;
- prevSlot=turtle.getSelectedSlot();
- if (not ender)
- then
- numb=14;
- else
- numb=13;
- end
- for slot=1,numb
- do
- turtle.select(slot);
- phaseColor=colors.yellow;
- phase="Droppping Item";
- sendData();
- if (ender)
- then
- prevPhase=phase;
- while (not turtle.drop())
- do
- if (turtle.getItemCount(slot)==0)
- then
- break;
- end
- sleep(2);
- phaseColor=colors.red;
- phase="EnderChest full!";
- sendData();
- phase=prevPhase;
- end
- else
- prevPhase=phase;
- while (not turtle.drop())
- do
- if (turtle.getItemCount(slot)==0)
- then
- break;
- end
- sleep(2);
- phaseColor=colors.red;
- phase="Home chest full!";
- sendData();
- phase=prevPhase;
- end
- end
- end
- phaseColor=colors.white;
- turtle.select(prevSlot);
- end
- local function returnHome() --return home to do different tasks
- local x, y, dir;
- x=advMovAPI.getX();
- y=advMovAPI.getY();
- dir=advMovAPI.getDir();
- if (advMovAPI.getX()>0)
- then
- if (advMovAPI.getDir()==0)
- then
- turnL();
- turnL();
- elseif (advMovAPI.getDir()==1)
- then
- turnL();
- elseif (advMovAPI.getDir()==3)
- then
- turnR();
- end
- elseif (advMovAPI.getX()<0)
- then
- if (advMovAPI.getDir()==2)
- then
- turnL();
- turnL();
- elseif (advMovAPI.getDir()==1)
- then
- turnR();
- elseif (advMovAPI.getDir()==3)
- then
- turnL();
- end
- end
- while(advMovAPI.getX()~=0)
- do
- advMovAPI.forward(true);
- homeFuel=homeFuel+1;
- end
- if (advMovAPI.getY()>0)
- then
- if (advMovAPI.getDir()==1)
- then
- turnL();
- turnL();
- elseif (advMovAPI.getDir()==2)
- then
- turnL();
- elseif (advMovAPI.getDir()==0)
- then
- turnR();
- end
- elseif (advMovAPI.getY()<0)--This is useless...
- then
- if (advMovAPI.getDir()==3)
- then
- turnL();
- turnL();
- elseif (advMovAPI.getDir()==2)
- then
- turnR();
- elseif (advMovAPI.getDir()==0)
- then
- turnL();
- end
- end
- while(advMovAPI.getY()~=0)
- do
- advMovAPI.forward(true);
- homeFuel=homeFuel+1;
- end
- if (advMovAPI.getDir()==0)
- then
- turnL();
- elseif (advMovAPI.getDir()==2)
- then
- turnR();
- elseif (advMovAPI.getDir()==3)
- then
- turnR();
- turnR();
- end
- turnL();
- dropAll(false);
- turnR();
- returnBack(x, y, dir);
- end
- local function pickUp() --Pick up all item from an inventory *unused
- local slot
- slot=1;
- print("Taking seeds...");
- for slot=1,16
- do
- turtle.select(slot);
- turtle.suck(64);
- end
- end
- local function select() --Select the first not empty slot *unused
- local slot
- for slot=1,16
- do
- if (turtle.getItemCount(slot)>0)
- then
- turtle.select(slot);
- return;
- end
- end
- end
- local function placeTorch()
- local prevSlot;
- prevSlot=turtle.getSelectedSlot();
- turtle.select(torchSlot);
- turtle.placeUp();
- turtle.select(prevSlot);
- end
- local function checkForLiquid() --Check for lava in front of it
- local prevSlot,full,fuel;
- prevSlot=turtle.getSelectedSlot();
- turtle.select(bucketSlot);
- if (tonumber(turtle.getFuelLimit())==nil)
- then
- return;
- end
- if (turtle.place())
- then
- fuel=turtle.getFuelLevel();
- if (turtle.refuel())
- then
- if (not full)
- then
- startFuel=startFuel+(turtle.getFuelLevel()-fuel);
- fuelFound=fuelFound+(turtle.getFuelLevel()-fuel);
- end
- else
- turtle.place();
- end
- end
- turtle.select(prevSlot);
- end
- local function refresh() --Do different tasks
- local prevSlot;
- updateFile();
- sendData();
- checkForLiquid();
- if (checkFull())
- then
- if (enderchest)
- then
- prevSlot=turtle.getSelectedSlot();
- turtle.select(enderSlot);
- while(not turtle.place())
- do
- turtle.dig();
- turtle.attack();
- end
- dropAll(true);
- turtle.dig();
- turtle.select(prevSlot);
- else
- returnHome();
- end
- end
- end
- local function digTunnel() --Dig the main tunnel
- phase="Main tunnel";
- for i=0,tunnelLength
- do
- if (i<tunnelLength)
- then
- turtle.dig();
- end
- turtle.digUp();
- if (i<tunnelLength)
- then
- advMovAPI.forward(true);
- refresh();
- end
- end
- turnR();
- turtle.dig();
- advMovAPI.forward(true);
- turnR();
- refresh();
- for i=0,tunnelLength
- do
- if (i<tunnelLength)
- then
- turtle.dig();
- end
- turtle.digUp();
- if (i<tunnelLength)
- then
- advMovAPI.forward(true);
- refresh();
- end
- end
- turnR();
- advMovAPI.forward(true);
- turnR();
- refresh();
- end
- local function digBranch() --Dig a branch
- torchTimer=10;
- for i=0,branchLength
- do
- if (i<branchLength)
- then
- turtle.dig();
- end
- turtle.digUp();
- if (i<branchLength)
- then
- advMovAPI.forward(true);
- refresh();
- end
- end
- turnL();
- turnL();
- while (advMovAPI.getX()~=0)
- do
- if (torchTimer>=11)
- then
- placeTorch();
- torchTimer=0;
- end
- torchTimer=torchTimer+1;
- if (advMovAPI.getX()==2 or advMovAPI.getX()==-1)
- then
- placeTorch();
- end
- advMovAPI.forward(true);
- refresh();
- end
- end
- local function digBranches() --Steps to dig branches consecutively
- for i=0,numBranch-1
- do
- phase="Branch number "..tostring(i+1);
- turnL();
- digBranch();
- advMovAPI.forward(true)
- digBranch();
- turnR();
- refresh();
- if (i<numBranch-1)
- then
- advMovAPI.forward(true);
- advMovAPI.forward(true);
- advMovAPI.forward(true);
- end
- refresh();
- end
- while (advMovAPI.getY()>0)
- do
- advMovAPI.back();
- refresh();
- end
- end
- ------------------------------------------------------
- term.clear();
- term.setCursorPos(1, 1);
- if (#arg>0)
- then
- if (arg[1]=="help")
- then
- help();
- return;
- elseif(arg[1]=="start")
- then
- for i=2,#arg
- do
- if (arg[i]=="enderchest")
- then
- enderchest=true;
- elseif (arg[i]=="rednet")
- then
- net=true;
- if (arg[i+1]~=nil)
- then
- receiverId=tonumber(arg[i+1]);
- i=i+2;
- else
- printError("You have to specify the id of the computer receiver after the argument \"rednet\".");
- return;
- end
- rednet.open("right");
- elseif (arg[i]=="home")
- then
- home=true;
- if (not fs.exists("startup"))
- then
- shell.run("pastebin", "get", "QSG5bWKr", "startup");
- end
- end
- end
- else
- error("Wrong arguments");
- end
- else
- printError("Wrong number of arguments")
- print("Use \"BranchMine help\" to display an help guide.")
- return;
- end
- if (not fs.exists("SolidAPI/advMovAPI"))
- then
- printError("Could not find the API advMovAPI.");
- print("Trying to download it from pastebin.\nPress any key to continue.");
- os.pullEvent("key");
- term.clear();
- term.setCursorPos(1, 1);
- shell.run("pastebin", "get", "4XhYtBvC", "SolidAPI/advMovAPI");
- print("Press any key to continue.");
- os.pullEvent("char");
- term.clear();
- term.setCursorPos(1, 1);
- if (not fs.exists("SolidAPI/advMovAPI"))
- then
- error("Could not find advMovAPI. An error has occurred while downloading the API.");
- end
- if (os.loadAPI("SolidAPI/advMovAPI"))
- then
- advMovAPI.setCoords(0, 0, 0);
- advMovAPI.setDir(1);
- else
- fs.delete("SolidAPI/advMovAPI");
- error("Could not load API \"advMovAPI\". Deleting the API...");
- end
- else
- if (os.loadAPI("SolidAPI/advMovAPI"))
- then
- advMovAPI.setCoords(0, 0, 0);
- advMovAPI.setDir(1);
- else
- fs.delete("SolidAPI/advMovAPI");
- error("Could not load API \"advMovAPI\". Deleting the API...");
- end
- end
- term.clear();
- term.setCursorPos(1, 1);
- setup();
- term.clear();
- term.setCursorPos(1, 1);
- numBranch=math.ceil(tunnelLength/3);
- fuelNeeded=(tunnelLength*2+2)+(2*numBranch)+((numBranch-1)*3)+((numBranch-1)*3)+(numBranch*branchLength*4)+2;--ft+2+fb
- if (turtle.getFuelLevel()<fuelNeeded)
- then
- print("The fuel level is too low to run the program.\n");
- print("Needed: "..fuelNeeded);
- print("Current: "..turtle.getFuelLevel());
- print("\nIt is suggested to put more fuel than necessary to prevent an occasionally return to the starting position.");
- return;
- end
- digTunnel();
- sendData();
- advMovAPI.forward(true);
- digBranches();
- sendData();
- if (not enderchest)
- then
- turnL();
- dropAll(false);
- turnR();
- else
- turtle.select(enderSlot);
- while(not turtle.place())
- do
- turtle.dig();
- turtle.attack();
- end
- dropAll(true);
- turtle.dig();
- end
- phase="Completed";
- phaseColor=colors.lime;
- sendData();
- fs.delete("BMStatus");
- --TODO: more data send
- --Original script by SolidSnake96AS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement