HAJ523

Mining Well

Mar 17th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.08 KB | None | 0 0
  1. -- * sfDown
  2. -- This function will perform a safe downward move Checking for Blocks / Chests / Entities
  3. -- and returning a value as follows:
  4. --   -2 = Bedrock is reached No further movement possible.
  5. --   -1 = Inventory Full
  6. --    0 = Failed to move downward
  7. --    1 = Success
  8. local function sfDown(lastInvSlot)
  9.     local ret = true;
  10.    
  11.     -- Ensure Inventory Has Space.
  12.     if (turtle.getItemCount(lastInvSlot) > 0) then
  13.         return -1;
  14.     end
  15.    
  16.     --Check for inventory Below
  17.     while (turtle.suckDown()) do
  18.         --Check Inventory
  19.         if (turtle.getItemCount(lastInvSlot) > 0) then
  20.             return -1;
  21.         end
  22.     end
  23.    
  24.     --If there is a block below.
  25.     if turtle.detectDown() then
  26.         ret = turtle.digDown();
  27.         --If we have hit bedrock.
  28.         if ret==false then
  29.             return -2;
  30.         end
  31.     end
  32.    
  33.     --No blocks below try and move.
  34.     if (turtle.down()) then
  35.         return 1;
  36.     else
  37.         --Failed to move check for a mob.
  38.         while(ret) do
  39.             ret = turtle.attackDown();
  40.         end
  41.        
  42.         if (turtle.down()) then
  43.             return 1;
  44.         end
  45.     end
  46.    
  47.     --Failed to move for unknown reason.
  48.     return 0;
  49. end
  50.  
  51. -- * sfUp
  52. -- This function will perform a safe move up checking for Mobs
  53. -- and returning as follows:
  54. local function sfUp()
  55.     --While we are unable to move attack up.
  56.     while (not turtle.up()) do
  57.         turtle.attackUp();
  58.     end
  59. end
  60.  
  61. -- * sfDig
  62. -- This function will perform a safe dig checking for Blocks / Inventories
  63. -- and returning as follows:
  64. --   -2 = Bedrock
  65. --   -1 = Inventory Full
  66. --    0 = Nothing to Dig or the block is in the skip list.
  67. --    1 = Success.
  68. local function sfDig(lastInvSlot,lastDummySlot)
  69.  
  70.     -- Ensure Inventory Has Space.
  71.     if (turtle.getItemCount(lastInvSlot) > 0) then
  72.         return -1;
  73.     end
  74.    
  75.     --Check for inventory
  76.     while (turtle.suck()) do
  77.         --Check Inventory
  78.         if (turtle.getItemCount(lastInvSlot) > 0) then
  79.             return -1;
  80.         end
  81.     end
  82.    
  83.     --If there is a block.
  84.     if turtle.detect() then
  85.         --Make sure it is worth our time to mine it.
  86.         for i=1,lastDummySlot do
  87.             turtle.select(i);
  88.             if (turtle.compare()) then
  89.                 turtle.select(1);
  90.                 return 0;
  91.             end
  92.         end
  93.         turtle.select(1);
  94.    
  95.         if (turtle.dig()==false) then
  96.             return -2;
  97.         end
  98.         return 1;
  99.     end
  100.    
  101.     --Nothing to dig.
  102.     return 0;
  103. end
  104.  
  105. -- * chkFuel
  106. -- This function checks the fuel level of the turtle and returns as follows:
  107. --    0 = Return to surface Fuel is low.
  108. --    1 = Fuel Ok.
  109. local function chkFuel(dist,fuelReserve)
  110.     --Make sure there is always more than enough to return to starting position.
  111.     if (turtle.getFuelLevel() < (dist + fuelReserve)) then
  112.         return 0;
  113.     end
  114.     return 1;
  115. end
  116.  
  117. -- * fllFuel
  118. -- This function will use fuel from slot 16 if there is some and will wait for fuel to be added if there isn't.
  119. local function fllFuel(curDepth,rsvFuel)
  120.     turtle.select(16);
  121.     print("Add fuel to slot 16 in order to continue.");
  122.     --Ensure that there is atleast enough fuel to return below and back up.
  123.     while (chkFuel(curDepth*2,rsvFuel)==0) do
  124.         os.sleep(5);
  125.         turtle.refuel();
  126.     end
  127.     turtle.select(1);
  128. end
  129.  
  130. -- * rtrnDeep
  131. -- This function returns the turtle to the depths provided it has the appropriate amount of fuel
  132. -- and returns the following:
  133. --    0 = Not enough fuel to return to depths.
  134. --    1 = Turtle Returned to depths.
  135. local function rtrnDeep(depth,rsvFuel)
  136.     print("Returning to Depths...");
  137.     if (turtle.getFuelLevel() < (depth + rsvFuel)) then
  138.         fllFuel(depth,rsvFuel)
  139.     end
  140.     for i=1,depth do
  141.         sfDown(16);
  142.     end
  143.     return 1;
  144. end
  145.  
  146. -- * rtrnSurface
  147. -- This function returns the turtle to the surface and returns the following:
  148. --    0 = Failure
  149. --    1 = Success
  150. local function rtrnSurface(depth)
  151.     print("Returning to Surface...");
  152.     for i=1,depth do
  153.         sfUp();
  154.     end
  155.     return 1;
  156. end
  157.  
  158. -- * dmpInventory
  159. -- This function dumps the turtles inventory into the chest above and returns the following:
  160. --    0 = Failure
  161. --    1 = Success
  162. local function dmpInventory(lastDummySlot)
  163.     print("Dumping Inventory...");
  164.     local lastSlot=0
  165.     for i=(lastDummySlot+1),16 do
  166.         turtle.select(i);
  167.         -- If we are unable to drop the current slot and there is inventory to be dropped.
  168.         while ((not turtle.dropUp()) and (turtle.getItemCount(i)>0)) do
  169.             if (lastSlot==i) then print("No free space in drop off chest. Pausing for 5 seconds."); end
  170.             os.sleep(5)
  171.         end
  172.     end
  173.     turtle.select(1);
  174.     return 1;
  175. end
  176.  
  177. -- * scnDummy
  178. -- This function scans the turtle inventory dummy blocks
  179. -- returns the slot number of the last dummy block.
  180. -- If There are not enough dummy blocks in the first slot it will print a message and return -1.
  181. local function scnDummy()
  182.     local lstDummy = 0;
  183.     --Loop over items from inventory looking for first empty slot.
  184.     for i=1,16 do
  185.         if (turtle.getItemCount(i)==0) then
  186.             lstDummy=i-1;
  187.             break;
  188.         end
  189.     end
  190.     -- Ensure that there are enough dummy blocks of the first type to fill the hole when done.
  191.     turtle.select(1);
  192.     if (turtle.getItemCount(1) < 5) then
  193.         print("Not enough dummy blocks in the first slot to fill hole when complete. (5 minimum)");
  194.         return -1;
  195.     end
  196.     return lstDummy;
  197. end
  198.  
  199. -- * fllHole
  200. -- This function will use the item in slot one to fill the hole left by mining.
  201. local function fllHole()
  202.     turtle.select(1);
  203.     for i=1,4 do
  204.         --If No block place a block.
  205.         if not turtle.detect() then
  206.             turtle.place();
  207.         end
  208.         --No need to turn on the last one.
  209.         if i==4 then break; end
  210.         turtle.turnRight();
  211.     end
  212.     --Move up the last bit and fill the hole below.
  213.     sfUp()
  214.     turtle.placeDown();
  215. end
  216.  
  217. local dbg = 0; --Set to 1 to see debug printing
  218. local curDepth = 0; -- Current Depth Mining At.
  219. local curState = 1; -- 1 Move Down / 2 Mine front / 3 Mine right / 4 Mine 180 / 5 Mine left / 6 Drop Goods
  220. local prvState = 0; -- Previous State in mining.
  221. local lstDummy = 1; -- Last slot containing dummy blocks.
  222. local rsvFuel = 10; -- Amount of fuel to hold in reserve.
  223. local retValue;     -- Return Value from Functions
  224.  
  225. --Scan the Dummy Blocks Before starting.
  226. lstDummy = scnDummy()
  227. if lstDummy==-1 then
  228.     return;
  229. end
  230.  
  231. --Handle Multiple States
  232. while curState>0 do
  233.     if dbg==1 then
  234.         print(" State: "..curState);
  235.         print("pState: "..prvState);
  236.         print(" Depth: "..curDepth);
  237.     end
  238.     if (chkFuel(curDepth,rsvFuel)==0) then
  239.         prvState=curState;
  240.         curState=7;
  241.     end
  242.     if curState==1 then
  243.         retValue = sfDown(16);
  244.         --Inventory Full
  245.         if (retValue==-1) then
  246.             prvState=curState;
  247.             curState=6;
  248.         --Failed to Move down or Bedrock. Done.
  249.         elseif (retValue==0) or (retValue==-2) then
  250.             curState=0;
  251.         else
  252.             curDepth=curDepth+1;
  253.             curState=2
  254.         end
  255.     elseif (curState>1 and curState<6) then
  256.         if (sfDig(16,lstDummy)==-1) then
  257.             prvState=curState;
  258.             curState=6;
  259.         else
  260.             if curState==5 then
  261.                 curState=1;
  262.             else
  263.                 curState=curState+1;
  264.                 turtle.turnRight();
  265.             end
  266.         end
  267.     elseif curState==6 then
  268.         rtrnSurface(curDepth);
  269.         dmpInventory(lstDummy);
  270.         rtrnDeep(curDepth,rsvFuel);
  271.         curState=prvState;
  272.     elseif curState==7 then
  273.         rtrnSurface(curDepth);
  274.         dmpInventory(lstDummy);
  275.         fllFuel(curDepth,rsvFuel);
  276.         rtrnDeep(curDepth,rsvFuel);
  277.         curState=prvState;
  278.     end
  279. end
  280.  
  281. --Done Mining Move to surface-1 and Fill hole.
  282. rtrnSurface(curDepth-1);
  283. fllHole();
  284. dmpInventory(lstDummy);
Advertisement
Add Comment
Please, Sign In to add comment