Advertisement
pedrosgali

BranchMineII

Feb 17th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.20 KB | None | 0 0
  1. --rednet.open("right")
  2.  
  3. --GLOBAL VARIABLES--
  4.  
  5. fuelSlot = 1
  6. stoneSlot = 2
  7. dirtSlot = 3
  8. gravelSlot = 4
  9. cobbleSlot = 5
  10. home = tonumber(0)
  11. north = 0
  12. east = 1
  13. south = 2
  14. west = 3
  15. t = tonumber(0)
  16.  
  17. --REDNET FUNCTIONS--
  18.  
  19. function report(msg)
  20.     --rednet.open("right")
  21.     clear()
  22.     --rednet.broadcast(tostring(msg))
  23.     print(msg)
  24. end
  25.  
  26. --FILING FUNCTIONS--
  27.  
  28. function getLoc()
  29.     if fs.exists("loc") then
  30.         data = fs.open("loc", "r")
  31.         x = tonumber(data.readLine())
  32.         y = tonumber(data.readLine())
  33.         z = tonumber(data.readLine())
  34.         h = tonumber(data.readLine())
  35.         b = tonumber(data.readLine())
  36.         tl = tonumber(data.readLine())
  37.         bl = tonumber(data.readLine())
  38.         data.close()
  39.     else
  40.         x = 0
  41.         y = 0
  42.         z = 0
  43.         h = 0
  44.         b = 0
  45.         tl = 0
  46.         bl = 0
  47.         startup()
  48.     end
  49. end
  50.  
  51. function getTempLoc()
  52.         data = fs.open("tempLoc", "r")
  53.         tgtx = tonumber(data.readLine())
  54.         tgty = tonumber(data.readLine())
  55.         tgtz = tonumber(data.readLine())
  56.         tgth = tonumber(data.readLine())
  57.         data.close()
  58. end
  59.  
  60. function dumpLoc()
  61.     if fs.exists("loc") then
  62.         fs.delete("loc")
  63.     end
  64.     report("Opening LOC data file.")
  65.     data = fs.open("loc", "w")
  66.     data.writeLine(tostring(x))
  67.     data.writeLine(tostring(y))
  68.     data.writeLine(tostring(z))
  69.     data.writeLine(tostring(h))
  70.     data.writeLine(tostring(b))
  71.     data.writeLine(tostring(tl))
  72.     data.writeLine(tostring(bl))
  73.     data.close()
  74.     report("New LOC data written to file.")
  75. end
  76.  
  77. function dumpTempLoc()
  78.         report("Opening temporary LOC data file.")
  79.         data = fs.open("tempLoc", "w")
  80.         data.writeLine(tostring(x))
  81.         data.writeLine(tostring(y))
  82.         data.writeLine(tostring(z))
  83.         data.writeLine(tostring(h))
  84.         data.close()
  85.         report("New LOC data written to file.")
  86. end
  87.  
  88. --MOVEMENT FUNCTIONS--
  89.  
  90. function refuel(reqFuel)
  91.     if reqFuel == nil then
  92.         reqFuel = 80
  93.     end
  94.     fuel = turtle.getFuelLevel()
  95.     while fuel < reqFuel do
  96.         if turtle.getItemCount(fuelSlot) == 0 then
  97.             goHome()
  98.             goBack()
  99.             break
  100.         else
  101.             turtle.refuel(1)
  102.             fuel = turtle.getFuelLevel()
  103.         end
  104.     end
  105. end
  106.  
  107. function fd()
  108.     getLoc()
  109.     refuel()
  110.     if turtle.forward() then
  111.         if h == 0 then
  112.              x = x + 1
  113.         elseif h == 1 then
  114.              z = z + 1
  115.         elseif h == 2 then
  116.              x = x - 1
  117.         elseif h == 3 then
  118.              z = z - 1
  119.         end
  120.         report("Moving.")
  121.     else
  122.         report("Blocked")
  123.     end
  124.     dumpLoc()
  125. end
  126.  
  127. function up()
  128.     getLoc()
  129.     refuel()
  130.     if turtle.up() then
  131.         y = y + 1
  132.         report("Moving up.")
  133.     else
  134.         report("Blocked")
  135.     end
  136.     dumpLoc()
  137. end
  138.  
  139. function dn()
  140.     getLoc()
  141.     refuel()
  142.     if turtle.down() then
  143.         y = y - 1
  144.         report("Moving down.")
  145.     else
  146.         report("Blocked")
  147.     end
  148.     dumpLoc()
  149. end
  150.  
  151. function rt()
  152.     getLoc()
  153.     report("Turning.")
  154.     turtle.turnRight()
  155.     report("Updating data file.")
  156.     h = (h + 1) % 4
  157.     dumpLoc()
  158. end
  159.  
  160. function lt()
  161.     getLoc()
  162.     turtle.turnLeft()
  163.     h = (h - 1) % 4
  164.     while h < 0 do
  165.         h = h + 4
  166.     end
  167.     dumpLoc()
  168. end
  169.  
  170. function dig()
  171.     turtle.dig()
  172. end
  173.        
  174. function digUp()
  175.     turtle.digUp()
  176. end
  177.        
  178. function digDn()
  179.     turtle.digDown()
  180. end
  181.  
  182. --ADVANCED MOVEMENT FUNCTIONS--
  183.  
  184. function checkStuck()
  185.     getLoc()
  186.     dumpTempLoc()
  187.     digFd()
  188.     if x == tarx and z == tarz then
  189.         face(south)
  190.         digFd()
  191.         digFd()
  192.         digFd()
  193.         goHome()
  194.     end
  195. end
  196.  
  197. function gofd(dist)
  198.     if dist == nil then
  199.         fd()
  200.     else
  201.         while dist > 0 do
  202.             fd()
  203.             dist = dist - 1
  204.         end
  205.     end
  206. end
  207.  
  208. function face(tar)
  209.     getLoc()
  210.     while true do
  211.         if tar == h then
  212.             break
  213.         else
  214.             getLoc()
  215.             rt()
  216.             dumpLoc()
  217.             report("Turning. "..tostring(h))
  218.         end
  219.     end
  220. end
  221.  
  222. function findX(pos)
  223.     getLoc()
  224.     if x > pos then
  225.         face(south)
  226.         while x > pos do
  227.             fd()
  228.             getLoc()
  229.         end
  230.     elseif x < pos then
  231.         face(north)
  232.         while x < pos do
  233.             fd()
  234.             getLoc()
  235.         end
  236.     else
  237.     end
  238. end
  239.  
  240. function findZ(pos)
  241.     getLoc()
  242.     if z > pos then
  243.         face(west)
  244.         while z > pos do
  245.             fd()
  246.             getLoc()
  247.         end
  248.     elseif z < pos then
  249.         face(east)
  250.         while z < pos do
  251.             fd()
  252.             getLoc()
  253.         end
  254.     else
  255.     end
  256. end
  257.  
  258. function findY(height)
  259.     if y > height then
  260.         while y > height do
  261.             dn()
  262.             getLoc()
  263.         end
  264.     elseif y < height then
  265.         while y < height do
  266.             up()
  267.             getLoc()
  268.         end
  269.     else
  270.     end
  271. end
  272.  
  273. function moveTo(newx, newy, newz)
  274.     findY(newx)
  275.     findX(newz)
  276.     findZ(newy)
  277. end
  278.  
  279. function goHome()
  280.     getLoc()
  281.     dumpTempLoc()
  282.     findZ(tonumber(0))
  283.     findX(tonumber(0))
  284.     findY(tonumber(0))
  285.     face(north)
  286.     unload()
  287.     getFuel()
  288. end
  289.  
  290. function goBack()
  291.     getTempLoc()
  292.     moveTo(tarx, tary, tarz)
  293. end
  294.  
  295. function findBedrock()
  296.     while true do
  297.         turtle.digDown()
  298.         if not turtle.down() then
  299.             break
  300.         end
  301.         y = y - 1
  302.         report(y)
  303.     end
  304.     findY(y + 5)
  305.     b = y
  306.     report("Bedrock found!")
  307. end
  308.  
  309. --USEFUL FUNCTIONS--
  310.  
  311. function clear(line, column)
  312.     if line == nil then
  313.         line = 1
  314.     end
  315.     if column == nil then
  316.         column = 1
  317.     end
  318.     term.clear()
  319.     term.setCursorPos(line, column)
  320. end
  321.  
  322. function rest(count)
  323.     while count > 0 do
  324.         report("Sleeping... T.R: "..tostring(count).." sec.")
  325.         sleep(1)
  326.         count = count - 1
  327.     end
  328. end
  329.  
  330. function fill(slot)
  331.     turtle.select(slot)
  332.     while turtle.getItemCount(slot) < 64 do
  333.        turtle.suck()
  334.     end
  335.     for i = 1, 16 do
  336.         turtle.select(i)
  337.         if i == slot then
  338.         else
  339.             if turtle.compareTo(slot) then
  340.                 turtle.drop()
  341.             end
  342.         end
  343.     end
  344. end
  345.  
  346. function emptyFrom(slot)
  347.     for i = slot, 16 do
  348.         turtle.select(i)
  349.         turtle.drop()
  350.     end
  351. end
  352.  
  353. --JOB SPECIFIC FUNCTIONS--
  354.  
  355. function unload()
  356.     face(east)
  357.     emptyFrom(5)
  358.     face(north)
  359. end
  360.  
  361. function getFuel()
  362.     face(west)
  363.     fill(fuelSlot)
  364.     face(north)
  365. end
  366.  
  367. function gravel()
  368.     turtle.select(gravelSlot)
  369.     rest(1)
  370.     if turtle.compare() then
  371.         while turtle.compare() do
  372.             report("Encountered Gravel")
  373.             dig()
  374.             rest(1)
  375.         end
  376.     end
  377. end
  378.  
  379. function tossJunk()
  380.     for slot = 1, 16 do
  381.         turtle.select(slot)
  382.         if turtle.getItemCount(slot) == 64 then
  383.             if slot == cobbleSlot then
  384.                 turtle.drop(63)
  385.                 report("Dropping Junk.")
  386.             elseif slot == dirtSlot then
  387.                 turtle.drop(63)
  388.                 report("Dropping Junk.")
  389.             elseif slot == gravelSlot then
  390.                 turtle.drop(63)
  391.                 report("Dropping Junk.")
  392.             else
  393.             end
  394.         end
  395.     end
  396. end
  397.  
  398. function checkFull()
  399.     turtle.select(16)
  400.     if turtle.getItemCount(16) > 1 then
  401.         goHome()
  402.         goBack()
  403.     end
  404.     turtle.select(fuelSlot)
  405.     if turtle.getItemCount(fuelSlot) < 1 then
  406.         goHome()
  407.         goBack()
  408.     end
  409. end
  410.  
  411. function checkDig()
  412.     tossJunk()
  413.     checkFull()
  414.     for slot = stoneSlot, cobbleSlot do
  415.         turtle.select(slot)
  416.         if turtle.compare(slot) then
  417.             if slot == stoneSlot then
  418.                 turtle.select(cobbleSlot)
  419.                 dig()
  420.                 break
  421.             elseif slot == dirtSlot then
  422.                 turtle.select(dirtSlot)
  423.                 dig()
  424.                 break
  425.             elseif slot == gravelSlot then
  426.                 gravel()
  427.                 break
  428.             elseif slot == cobbleSlot then
  429.                 report("Cobble generator found.")
  430.                 goHome()
  431.             end
  432.         else
  433.         end
  434.     end
  435.     dig()
  436. end
  437.  
  438. function evaluate()
  439.     junk = 0
  440.     for slot = stoneSlot, cobbleSlot do
  441.         turtle.select(slot)
  442.         if turtle.compare() then
  443.             junk = junk + 1
  444.         end
  445.     end
  446.     if junk == 0 then
  447.         dig()
  448.         report("Treasure!")
  449.     else
  450.     end
  451. end
  452.  
  453. function findOre()
  454.     report("Scanning...")
  455.     lt()
  456.     evaluate()
  457.     lt()
  458.     lt()
  459.     evaluate()
  460.     lt()
  461. end
  462.  
  463. function digFd()
  464.     checkDig()
  465.     rest(1)
  466.     checkDig()
  467.     fd()
  468. end
  469.  
  470. function branch()
  471.     if h == west then
  472.         nbl = bl - (2 * bl)
  473.         while z > nbl do
  474.             digFd()
  475.             findOre()
  476.         end
  477.     elseif h == east then
  478.         nbl = bl
  479.         while z < nbl do
  480.             digFd()
  481.             findOre()
  482.         end
  483.     end
  484.     findZ(t)
  485. end
  486.  
  487. function trunk()
  488.     findY(b)
  489.     while x <= tl do
  490.         for i = 1, 3 do
  491.             digFd()
  492.         end
  493.         face(west)
  494.         branch()
  495.         face(east)
  496.         branch()
  497.         face(north)
  498.     end
  499.     b = b + 1
  500.     dumpLoc()
  501.     goHome()
  502. end
  503.  
  504. --INTERFACE--
  505.  
  506. function startup()
  507.     clear()
  508.     print("Welcome to the BranchMine program! :)")
  509.     print("please set a few starting variables.")
  510.     sleep(1)
  511.     clear()
  512.     print("How long should the trunk be?")
  513.     print()
  514.     write("Trunk Length: ")
  515.     input = read()
  516.     tl = tonumber(input)
  517.     print()
  518.     print("trunk length set to "..tostring(tl))
  519.     sleep(1)
  520.     clear()
  521.     print("How long should the branches be?")
  522.     print()
  523.     write("Branch Length: ")
  524.     input = read()
  525.     bl = tonumber(input)
  526.     print()
  527.     print("Branch length set to "..tostring(tl))
  528.     sleep(1)
  529.     clear()
  530.     print("Thank you for your input.")
  531.     print("Preparing files for mining operation.")
  532.     dumpLoc()
  533.     report("Getting fuel.")
  534.     getFuel()
  535.     report("Finding Bedrock layer.")
  536.     findBedrock()
  537.     dumpLoc()
  538. end
  539.  
  540. --PROGRAM--
  541.  
  542. getLoc()
  543. goHome()
  544. while b < -5 do
  545.     trunk()
  546. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement