Guest User

Untitled

a guest
May 29th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.93 KB | None | 0 0
  1. -- 'Hole' program, version 1.1
  2. -- Created by Pyro_
  3. -- Credit:
  4. --      theoriginalbit
  5. -- Also:
  6. --      BigSHinyToys
  7. --      BombMan
  8.  
  9. local version = 1.1
  10.  
  11. -- Stores data about current mode
  12. local speedy = false
  13. local noFuel = false
  14. local fuelChest = false
  15.  
  16. -- Stores turtle status data
  17. local isRefuelling = false
  18. local fuelLevel = 0
  19.  
  20. -- Stores dimensions for the hole
  21. local sizeX, sizeZ, depth = 0,0,0
  22.  
  23. -- Stores current turtle position, relative to start point
  24. local xPos, yPos, zPos = 0,0,0
  25.  
  26.  
  27. -- Stores current direction, positive x is directly forwards from start point
  28. -- positive z is directly right
  29. local xDir, zDir = 1,0
  30.  
  31. -- Stores last saved position (Save 1)
  32. local xSav1, ySav1, zSav1 = 0,0,0
  33. local xDirSav1, zDirSav1 = 0,0
  34. local save1Mutex = 0
  35.  
  36. -- Save 2
  37. local xSav2, ySav2, zSav2 = 0,0,0
  38. local xDirSav2, zDirSav2 = 0,0
  39. local save2Mutex = 0
  40.  
  41. -- This is required in movement functions
  42. -- Calculates distance to home, with small buffer
  43. local function distToHome()
  44.     local distance
  45.     -- Y is negative because the y axis increases as it goes up
  46.     distance = xPos + (-yPos) + zPos + 5
  47.     return distance
  48. end
  49.  
  50. -- **************************************************
  51. -- Movement / Turning functions
  52. local function moveForwardRF()
  53.     local retVal = true
  54.     -- Account for falling blocks, such as sand and gravel
  55.     if not speedy then
  56.         while turtle.detect() do
  57.             turtle.dig()
  58.             sleep(0.8)
  59.         end
  60.     else
  61.         turtle.dig()
  62.     end
  63.     -- If there is any mob infront of the turtle, or we are out of fuel
  64.     -- DEV NOTE: Will be modified later to refuel, or message over rednet wifi
  65.     while not turtle.forward() do
  66.         turtle.attack()
  67.     end
  68.    
  69.     -- Update position
  70.     xPos = xPos + xDir
  71.     zPos = zPos + zDir
  72.    
  73.     if not noFuel then
  74.         -- Update fuel info
  75.         fuelLevel = fuelLevel - 1
  76.    
  77.         -- Fuel checking
  78.         if (distToHome() >= fuelLevel) and not isRefuelling then
  79.             retVal = false
  80.         end
  81.     end
  82.    
  83.     return retVal
  84. end
  85.  
  86. local function moveUpRF()
  87.     local retVal = true
  88.     -- Account for falling blocks
  89.     if not speedy then
  90.         while turtle.detectUp() do
  91.             turtle.digUp()
  92.             sleep(0.8)
  93.         end
  94.     else
  95.         turtle.digUp()
  96.     end
  97.     --Update position
  98.     yPos = yPos + 1
  99.    
  100.     while not turtle.up() do
  101.         turtle.attackUp()
  102.     end
  103.    
  104.     if not noFuel then
  105.         -- Update fuel info
  106.         fuelLevel = fuelLevel - 1
  107.    
  108.         -- Fuel checking
  109.         if (distToHome() >= fuelLevel) and not isRefuelling then
  110.             retVal = false
  111.         end
  112.     end
  113.    
  114.     return retVal
  115. end
  116.  
  117. local function moveDownRF()
  118.     local retVal = true
  119.     -- No need to account for falling blocks
  120.     turtle.digDown()
  121.    
  122.     -- Update position
  123.     yPos = yPos - 1
  124.    
  125.     while not turtle.down() do
  126.         turtle.attackDown()
  127.     end
  128.    
  129.     if not noFuel then
  130.         -- Update fuel info
  131.         fuelLevel = fuelLevel - 1
  132.        
  133.         -- Fuel checking (skip if already refuelling)
  134.         if (distToHome() >= fuelLevel) and not isRefuelling then
  135.             retVal = false
  136.         end
  137.     end
  138.    
  139.     return retVal
  140. end
  141.  
  142. -- Move functions, with fuel checking (will be ignored if noFuel mode is selected)
  143. local function moveForward()
  144.     if not moveForwardRF() then
  145.         refuel()
  146.     end
  147. end
  148.  
  149. local function moveUp()
  150.     if not moveUpRF() then
  151.         refuel()
  152.     end
  153. end
  154.  
  155. local function moveDown()
  156.     if not moveDownRF() then
  157.         refuel()
  158.     end
  159. end
  160.  
  161. local function turnLeft()
  162.     -- Update direction vector
  163.     zDir, xDir = -xDir, zDir
  164.    
  165.     turtle.turnLeft()
  166. end
  167.  
  168. local function turnRight()
  169.     -- Update direction vector
  170.     zDir, xDir = xDir, -zDir
  171.    
  172.     turtle.turnRight()
  173. end
  174.  
  175. local function turnAround()
  176.     -- Update direction
  177.     zDir, xDir = -zDir, -xDir
  178.    
  179.     turtle.turnLeft()
  180.     turtle.turnLeft()
  181. end
  182.  
  183. -- Turns the turtle to the prompted direction
  184. local function turnToDir(x, z)
  185.     if x == 1 and xDir == 0 then
  186.         if zDir == 1 then
  187.             turnLeft()
  188.         else
  189.             turnRight()
  190.         end
  191.     elseif x == 1 and xDir == -1 then
  192.         turnAround()
  193.     elseif x == -1 and xDir == 0 then
  194.         if zDir == 1 then
  195.             turnRight()
  196.         else
  197.             turnLeft()
  198.         end
  199.     elseif x == -1 and xDir == 1 then
  200.         turnAround()
  201.     elseif z == 1 and zDir == 0 then
  202.         if xDir == 1 then
  203.             turnRight()
  204.         else
  205.             turnLeft()
  206.         end
  207.     elseif z == 1 and zDir == -1 then
  208.         turnAround()
  209.     elseif z == -1 and zDir == 0 then
  210.         if xDir == 1 then
  211.             turnLeft()
  212.         else
  213.             turnRight()
  214.         end
  215.     elseif z == -1 and zDir == 1 then
  216.         turnAround()
  217.     else
  218.         return false
  219.     end
  220.     return true
  221. end
  222.  
  223. -- Takes in a coordinate and moves the turtle there
  224. -- IMPLEMENTATION NOTE: Turtle will be facing in whatever direction it finishes in.
  225. --                      Keep this in mind, as it might affect what happens next.
  226. local function moveToPos(x, y, z)
  227.  
  228.     -- Move the turtle to the required X value
  229.         if x > xPos then
  230.             turnToDir(1, 0)
  231.             while x > xPos do
  232.                 moveForward()
  233.             end
  234.         elseif x < xPos then
  235.             turnToDir(-1, 0)
  236.             while x < xPos do
  237.                 moveForward()
  238.             end
  239.         end
  240.    
  241.     -- Move the turtle to the required Z value
  242.         if z > zPos then
  243.             turnToDir(0, 1)
  244.             while z > zPos do
  245.                 moveForward()
  246.             end
  247.         elseif z < zPos then
  248.             turnToDir(0, -1)
  249.             while z < zPos do
  250.                 moveForward()
  251.             end
  252.         end
  253.        
  254.     -- Move the turtle to the required Y value
  255.         if y > yPos then
  256.             while y > yPos do
  257.                 moveUp()
  258.             end
  259.         else
  260.             while y < yPos do
  261.                 moveDown()
  262.             end
  263.         end
  264. end
  265.  
  266. -- End of movement functions
  267. -- **************************************************
  268.    
  269. -- ++++++++++++++++++++++++++++++++++++++++++++++++++
  270. -- Logic functions
  271.  
  272. -- Saves the current dig position and direction
  273. local function saveDig()
  274.     local retVal = 0
  275.     if save1Mutex == 0 then
  276.         xSav1 = xPos
  277.         ySav1 = yPos
  278.         zSav1 = zPos
  279.         xDirSav1 = xDir
  280.         zDirSav1 = zDir
  281.         save1Mutex = 1
  282.         retVal = 1
  283.     elseif save2Mutex == 0 then
  284.         xSav2 = xPos
  285.         ySav2 = yPos
  286.         zSav2 = zPos
  287.         xDirSav2 = xDir
  288.         zDirSav2 = zDir
  289.         save2Mutex = 1
  290.         retVal = 2
  291.     end
  292.     return retVal
  293. end
  294.  
  295. -- Dumps the load into a chest or thin air
  296. local function dumpLoad()
  297.     for n = 1, 16 do
  298.         turtle.select(n)
  299.         turtle.drop()
  300.     end
  301. end
  302.  
  303.  
  304. -- Moves the turtle back to the last saved position and direction
  305. local function resumeDig(save)
  306.     if save == 1 then
  307.         moveToPos(xSav1, ySav1, zSav1)
  308.         turnToDir(xDirSav1, zDirSav1)
  309.         save1Mutex = 0
  310.     elseif save == 2 then
  311.         moveToPos(xSav2, ySav2, zSav2)
  312.         turnToDir(xDirSav2, xDirSav2)
  313.         save2Mutex = 0
  314.     end
  315. end
  316.  
  317. -- Move the turtle to the home and fuel chests, dump load and refuel, then
  318. -- resume digging
  319. function refuel()
  320. print("refuelling")
  321.     local save
  322.    
  323.     isRefuelling = 1
  324.    
  325.     save = saveDig()
  326.     moveToPos(0, 0, 0)
  327.     turnToDir(-1, 0)
  328.     dumpLoad()
  329.     turnToDir(0, -1)
  330.     turtle.select(1)
  331.     turtle.suck()
  332.     turtle.refuel()
  333.    
  334.     fuelLevel = turtle.getFuelLevel()
  335.    
  336.     resumeDig(save)
  337.    
  338.     isRefuelling = 0
  339. end
  340.  
  341.  
  342. -- Simply checks if the turtle is full
  343. -- DEV NOTE: this is kind of brute-forcish, I wonder if there's a better way?
  344. local function isFull()
  345.     local retVal = true
  346.     for n = 1, 16 do
  347.         if turtle.getItemCount(n) == 0 then
  348.             retVal = false
  349.         end
  350.     end
  351.     return retVal
  352. end
  353.  
  354. -- Digs the current strip in the current layer (parallel to X axis)
  355. local function digStrip()
  356.     for i = 2, sizeX do
  357.         moveForward()
  358.         if isFull() then
  359.             local save
  360.             save = saveDig()
  361.             moveToPos(0,0,0)
  362.             turnToDir(-1, 0)
  363.             dumpLoad()
  364.             resumeDig(save)
  365.         end
  366.     end
  367. end
  368.  
  369. -- Turns the turtle in the correct direction at the end of digging a strip
  370. local function stripTurn(strips)
  371. -- This is a bit bizzarre, since when we have an odd number of strips,
  372. -- then the turns are the same for every level, but when we have an
  373. -- even number of strips, they flip every other level
  374.     if strips ~= sizeZ - 1 and sizeZ % 2 == 1 then
  375.         if strips % 2 == 1 then
  376.             turnLeft()
  377.             moveForward()
  378.             turnLeft()
  379.         else
  380.             turnRight()
  381.             moveForward()
  382.             turnRight()
  383.         end
  384. -- DEV NOTE: is there a better way of doing this?
  385.     elseif strips ~= sizeZ -1 and sizeZ % 2 == 0 then
  386.         if yPos % 2 == 0 and strips % 2 == 0 then
  387.             turnRight()
  388.             moveForward()
  389.             turnRight()
  390.         elseif yPos % 2 == 1 and strips % 2 == 1 then
  391.             turnRight()
  392.             moveForward()
  393.             turnRight()
  394.         else
  395.             turnLeft()
  396.             moveForward()
  397.             turnLeft()
  398.         end
  399.     end
  400. end
  401.  
  402. -- Digs the current layer
  403. local function digLayer()
  404.     local strips = 0
  405.     print("Beginning layer: "..(-yPos))
  406.     for i = 1, sizeZ do
  407.         digStrip()
  408.         stripTurn(strips)
  409.         strips = strips + 1
  410.     end
  411. end
  412.  
  413.  
  414. -- This function controls the digging of the hole
  415. local function digOut()
  416. -- This for loop controls how many layers we are going to dig
  417.     for n = 1, depth do
  418.         digLayer()
  419.         -- If this was no the last layer, then start digging the next one, otherwise,
  420.         -- head home and empty contents into the chest
  421.         if n ~= depth then
  422.             turnAround()
  423.             moveDown()
  424.         elseif n == depth then
  425.             moveToPos(0, 0, 0)
  426.             turnToDir(-1, 0)
  427.             dumpLoad()
  428.             turnToDir(1, 0)
  429.         end
  430.     end
  431. end
  432.  
  433. -- Converts the dimension arguments to numbers
  434. local function argToNumber(argv)
  435.     argv[1] = tonumber(argv[1])
  436.     argv[2] = tonumber(argv[2])
  437.     argv[3] = tonumber(argv[3])
  438. end
  439.  
  440. -- Validate the dimensions passed to the program (so the first three arguments)
  441. local function validateDimensions(argv)
  442.     local retVal = true
  443.     -- Convert the flags to usable numbers
  444.     argToNumber(argv)
  445.     -- Check that they exist, and are numbers
  446.     if argv[1] == nil or not type(argv[1], "number") then
  447.         retVal = false
  448.     elseif argv[2] == nil or not type(argv[2], "number") then
  449.         retVal = false
  450.     elseif argv[3] == nil or not type(argv[3], "number") then
  451.         retVal = false
  452.     -- Check that the numbers are valid
  453.     elseif argv[1] < 0 then
  454.         retVal = false
  455.     elseif argv[2] < 0 then
  456.         retVal = false
  457.     elseif argv[3] < 0 then
  458.         retVal = false
  459.     end
  460.     return retVal
  461. end
  462.  
  463. -- Will validate the flags (if any) that were passed to the program
  464. -- They are also set here, as it turns out to be easier
  465. local function validateAndParseFlags(argc, argv)
  466.     local retVal = true
  467.     if not type(argv[4], "string") or not argv[4] then
  468.         retVal = false
  469.     elseif not type(argv[5], "string") or not argv[4] then
  470.         retVal = false
  471.     else
  472.         for i = 4, argc do
  473.             if argv[i] == "-s" then
  474.                 speedy = true
  475.                 print("Speed mode")
  476.             elseif argv[i] == "-fc" then
  477.                 fuelChest = true
  478.                 print("Fuel chest mode")
  479.             else
  480.                 retVal = false
  481.             end
  482.         end
  483.     end
  484.     return retVal
  485. end
  486.  
  487. -- Prints usage information to the terminal
  488. local function printUsage()
  489.     term.clear()
  490.     term.setCursorPos(1, 1)
  491.     print("Usage:")
  492.     print("hole [<size X> <size Z> <depth> -s -fc] | help")
  493.     print("")
  494.     print("-s : speed mode")
  495.     print("-fc : fuel chest mode")
  496.     print("")
  497.     print("Using 'hole help' will display help pages")
  498. end
  499.  
  500. -- Just prints author info
  501. local function printInfo()
  502.     print("Created by: Pyro_")
  503.     print("Credit to: theoriginalbit, BigSHinyToys and BombMan")
  504. end
  505.  
  506. -- Just writes help information to the terminal, one of these
  507. -- days I will have it write a help file for the help program,
  508. -- then call the help program or something
  509. local function printHelp()
  510.     term.clear()
  511.     term.setCursorPos(1, 2)
  512.     print("Information:")
  513.     print("An ongoing development project by Pyro_")
  514.     print("Credit to:")
  515.     print("theoriginalbit, BigSHinyToys and BombMan")
  516.     print("Come check us out on the CC forums")
  517.     print("")
  518.     print("Version: ".. version)
  519.     print("OS Version: " .. os.version())
  520.     -- Keep page number and any key prompt in the same spot
  521.     -- this way they don't interfere with spacing
  522.     term.setCursorPos(x - string.len("Page 1"), 12)
  523.     print("Page 1")
  524.     term.setCursorPos(1, 13)
  525.     print("Press any key to continue")
  526.     os.pullEvent("key")
  527.     term.clear()
  528.     term.setCursorPos(1, 2)
  529.     -- Why not?
  530.     printUsage()
  531.     term.setCursorPos(x - string.len("Page 2"), 12)
  532.     print("Page 2")
  533.     term.setCursorPos(1, 13)
  534.     print("Press any key to continue")
  535.     os.pullEvent("key")
  536.     term.clear()
  537.     term.setCursorPos(1, 2)
  538.     print("All flags with '-' preceding them are optional")
  539.     print("All flags surrounded by '<>' are required")
  540.     print("")
  541.     print("Using -s will skip checking for blocks that may fall in front of the turtle,")
  542.     print("so ensure this won't happen before you use this flag")
  543.     term.setCursorPos(x - string.len("Page 3"), 12)
  544.     print("Page 3")
  545.     term.setCursorPos(1, 13)
  546.     print("Press any key to continue")
  547.     os.pullEvent("key")
  548.     term.clear()
  549.     term.setCursorPos(1, 2)
  550.     print("Using -fc will tell the turtle that")
  551.     print("there's a chest of fuel located to it's")
  552.     print("immediate left when it initially starts")
  553.     print("it will use the fuel inside the chest")
  554.     print("to refuel when it gets low")
  555.     print("")
  556.     print("Note: The turtle will pull one whole")
  557.     print("stack from a slot in the chest, so")
  558.     print("budget fuel accordingly; so that it")
  559.     print("doesn't end up eating fuel it will")
  560.     print("never use...")
  561.     term.setCursorPos(x - string.len("Page 4"), 12)
  562.     print("Page 4")
  563.     term.setCursorPos(1, 13)
  564.     print("Press any key to return")
  565.     os.pullEvent("key")
  566.     term.clear()
  567.     term.setCursorPos(1, 1)
  568. end
  569.  
  570. -- Checks if the user has requested the help page
  571. local function isFlagHelp(argv)
  572.     local retVal = false
  573.     if argv[1] == "help" then
  574.         retVal = true
  575.     end
  576.     return retVal
  577. end
  578.    
  579. -- End of logic functions
  580. -- ++++++++++++++++++++++++++++++++++++++++++++++++++
  581.  
  582. -- Main function controls flow of program
  583. local function main(argc, argv)
  584.    
  585.     if isFlagHelp(argv) then
  586.         printHelp()
  587.     elseif not validateDimensions(argv) then
  588.         printUsage()
  589.         printInfo()
  590.     elseif not validateAndParseFlags(argc, argv) then
  591.     -- Test if the 'speedy' or 'fuel chest' flags were set
  592.         printUsage()
  593.         printInfo()
  594.     else
  595.    
  596.         -- If we aren't in fuel chest mode, then ignore fuel checking
  597.         if fuelChest == false then
  598.             noFuel = true
  599.         end
  600.         -- Backwards compatability; think C ternary operation
  601.         -- fuelLevel will be ignored if noFuel flag is set
  602.         fuelLevel = turtle.getFuelLevel and turtle.getFuelLevel() or "unlimited"
  603.    
  604.         -- Will skip fuel checking if turtleNeedsFuel is 0 in config
  605.         if fuelLevel == "unlimited" then
  606.             noFuel = true
  607.         end
  608.    
  609.         sizeX = argv[1]
  610.         sizeZ = argv[2]
  611.         depth = argv[3]
  612.    
  613.         local dims = string.format("Dimensions: X: %i Z: %i Depth: %i", sizeX, sizeZ, depth)
  614.    
  615.         print("Beginning dig...")
  616.         print(dims)
  617.         digOut()
  618.         print("Finished dig...")
  619.     end
  620. end
  621.  
  622. main(#{...}, {...})
Advertisement
Add Comment
Please, Sign In to add comment