Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- * sfDown
- -- This function will perform a safe downward move Checking for Blocks / Chests / Entities
- -- and returning a value as follows:
- -- -2 = Bedrock is reached No further movement possible.
- -- -1 = Inventory Full
- -- 0 = Failed to move downward
- -- 1 = Success
- local function sfDown(lastInvSlot)
- local ret = true;
- -- Ensure Inventory Has Space.
- if (turtle.getItemCount(lastInvSlot) > 0) then
- return -1;
- end
- --Check for inventory Below
- while (turtle.suckDown()) do
- --Check Inventory
- if (turtle.getItemCount(lastInvSlot) > 0) then
- return -1;
- end
- end
- --If there is a block below.
- if turtle.detectDown() then
- ret = turtle.digDown();
- --If we have hit bedrock.
- if ret==false then
- return -2;
- end
- end
- --No blocks below try and move.
- if (turtle.down()) then
- return 1;
- else
- --Failed to move check for a mob.
- while(ret) do
- ret = turtle.attackDown();
- end
- if (turtle.down()) then
- return 1;
- end
- end
- --Failed to move for unknown reason.
- return 0;
- end
- -- * sfUp
- -- This function will perform a safe move up checking for Mobs
- -- and returning as follows:
- local function sfUp()
- --While we are unable to move attack up.
- while (not turtle.up()) do
- turtle.attackUp();
- end
- end
- -- * sfDig
- -- This function will perform a safe dig checking for Blocks / Inventories
- -- and returning as follows:
- -- -2 = Bedrock
- -- -1 = Inventory Full
- -- 0 = Nothing to Dig or the block is in the skip list.
- -- 1 = Success.
- local function sfDig(lastInvSlot,lastDummySlot)
- -- Ensure Inventory Has Space.
- if (turtle.getItemCount(lastInvSlot) > 0) then
- return -1;
- end
- --Check for inventory
- while (turtle.suck()) do
- --Check Inventory
- if (turtle.getItemCount(lastInvSlot) > 0) then
- return -1;
- end
- end
- --If there is a block.
- if turtle.detect() then
- --Make sure it is worth our time to mine it.
- for i=1,lastDummySlot do
- turtle.select(i);
- if (turtle.compare()) then
- turtle.select(1);
- return 0;
- end
- end
- turtle.select(1);
- if (turtle.dig()==false) then
- return -2;
- end
- return 1;
- end
- --Nothing to dig.
- return 0;
- end
- -- * chkFuel
- -- This function checks the fuel level of the turtle and returns as follows:
- -- 0 = Return to surface Fuel is low.
- -- 1 = Fuel Ok.
- local function chkFuel(dist,fuelReserve)
- --Make sure there is always more than enough to return to starting position.
- if (turtle.getFuelLevel() < (dist + fuelReserve)) then
- return 0;
- end
- return 1;
- end
- -- * fllFuel
- -- This function will use fuel from slot 16 if there is some and will wait for fuel to be added if there isn't.
- local function fllFuel(curDepth,rsvFuel)
- turtle.select(16);
- print("Add fuel to slot 16 in order to continue.");
- --Ensure that there is atleast enough fuel to return below and back up.
- while (chkFuel(curDepth*2,rsvFuel)==0) do
- os.sleep(5);
- turtle.refuel();
- end
- turtle.select(1);
- end
- -- * rtrnDeep
- -- This function returns the turtle to the depths provided it has the appropriate amount of fuel
- -- and returns the following:
- -- 0 = Not enough fuel to return to depths.
- -- 1 = Turtle Returned to depths.
- local function rtrnDeep(depth,rsvFuel)
- print("Returning to Depths...");
- if (turtle.getFuelLevel() < (depth + rsvFuel)) then
- fllFuel(depth,rsvFuel)
- end
- for i=1,depth do
- sfDown(16);
- end
- return 1;
- end
- -- * rtrnSurface
- -- This function returns the turtle to the surface and returns the following:
- -- 0 = Failure
- -- 1 = Success
- local function rtrnSurface(depth)
- print("Returning to Surface...");
- for i=1,depth do
- sfUp();
- end
- return 1;
- end
- -- * dmpInventory
- -- This function dumps the turtles inventory into the chest above and returns the following:
- -- 0 = Failure
- -- 1 = Success
- local function dmpInventory(lastDummySlot)
- print("Dumping Inventory...");
- local lastSlot=0
- for i=(lastDummySlot+1),16 do
- turtle.select(i);
- -- If we are unable to drop the current slot and there is inventory to be dropped.
- while ((not turtle.dropUp()) and (turtle.getItemCount(i)>0)) do
- if (lastSlot==i) then print("No free space in drop off chest. Pausing for 5 seconds."); end
- os.sleep(5)
- end
- end
- turtle.select(1);
- return 1;
- end
- -- * scnDummy
- -- This function scans the turtle inventory dummy blocks
- -- returns the slot number of the last dummy block.
- -- If There are not enough dummy blocks in the first slot it will print a message and return -1.
- local function scnDummy()
- local lstDummy = 0;
- --Loop over items from inventory looking for first empty slot.
- for i=1,16 do
- if (turtle.getItemCount(i)==0) then
- lstDummy=i-1;
- break;
- end
- end
- -- Ensure that there are enough dummy blocks of the first type to fill the hole when done.
- turtle.select(1);
- if (turtle.getItemCount(1) < 5) then
- print("Not enough dummy blocks in the first slot to fill hole when complete. (5 minimum)");
- return -1;
- end
- return lstDummy;
- end
- -- * fllHole
- -- This function will use the item in slot one to fill the hole left by mining.
- local function fllHole()
- turtle.select(1);
- for i=1,4 do
- --If No block place a block.
- if not turtle.detect() then
- turtle.place();
- end
- --No need to turn on the last one.
- if i==4 then break; end
- turtle.turnRight();
- end
- --Move up the last bit and fill the hole below.
- sfUp()
- turtle.placeDown();
- end
- local dbg = 0; --Set to 1 to see debug printing
- local curDepth = 0; -- Current Depth Mining At.
- local curState = 1; -- 1 Move Down / 2 Mine front / 3 Mine right / 4 Mine 180 / 5 Mine left / 6 Drop Goods
- local prvState = 0; -- Previous State in mining.
- local lstDummy = 1; -- Last slot containing dummy blocks.
- local rsvFuel = 10; -- Amount of fuel to hold in reserve.
- local retValue; -- Return Value from Functions
- --Scan the Dummy Blocks Before starting.
- lstDummy = scnDummy()
- if lstDummy==-1 then
- return;
- end
- --Handle Multiple States
- while curState>0 do
- if dbg==1 then
- print(" State: "..curState);
- print("pState: "..prvState);
- print(" Depth: "..curDepth);
- end
- if (chkFuel(curDepth,rsvFuel)==0) then
- prvState=curState;
- curState=7;
- end
- if curState==1 then
- retValue = sfDown(16);
- --Inventory Full
- if (retValue==-1) then
- prvState=curState;
- curState=6;
- --Failed to Move down or Bedrock. Done.
- elseif (retValue==0) or (retValue==-2) then
- curState=0;
- else
- curDepth=curDepth+1;
- curState=2
- end
- elseif (curState>1 and curState<6) then
- if (sfDig(16,lstDummy)==-1) then
- prvState=curState;
- curState=6;
- else
- if curState==5 then
- curState=1;
- else
- curState=curState+1;
- turtle.turnRight();
- end
- end
- elseif curState==6 then
- rtrnSurface(curDepth);
- dmpInventory(lstDummy);
- rtrnDeep(curDepth,rsvFuel);
- curState=prvState;
- elseif curState==7 then
- rtrnSurface(curDepth);
- dmpInventory(lstDummy);
- fllFuel(curDepth,rsvFuel);
- rtrnDeep(curDepth,rsvFuel);
- curState=prvState;
- end
- end
- --Done Mining Move to surface-1 and Fill hole.
- rtrnSurface(curDepth-1);
- fllHole();
- dmpInventory(lstDummy);
Advertisement
Add Comment
Please, Sign In to add comment