Advertisement
pedrosgali

BranchMine

Nov 25th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.16 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 y > b do
  297.         digDn()
  298.         dn()
  299.         report("Depth: "..tostring(y))
  300.     end
  301.     report("Bedrock found!")
  302. end
  303.  
  304. --USEFUL FUNCTIONS--
  305.  
  306. function clear(line, column)
  307.     if line == nil then
  308.         line = 1
  309.     end
  310.     if column == nil then
  311.         column = 1
  312.     end
  313.     term.clear()
  314.     term.setCursorPos(line, column)
  315. end
  316.  
  317. function rest(count)
  318.     while count > 0 do
  319.         report("Sleeping... T.R: "..tostring(count).." sec.")
  320.         sleep(1)
  321.         count = count - 1
  322.     end
  323. end
  324.  
  325. function fill(slot)
  326.     turtle.select(slot)
  327.     while turtle.getItemCount(slot) < 64 do
  328.        turtle.suck()
  329.     end
  330.     for i = 1, 16 do
  331.         turtle.select(i)
  332.         if i == slot then
  333.         else
  334.             if turtle.compareTo(slot) then
  335.                 turtle.drop()
  336.             end
  337.         end
  338.     end
  339. end
  340.  
  341. function emptyFrom(slot)
  342.     for i = slot, 16 do
  343.         turtle.select(i)
  344.         turtle.drop()
  345.     end
  346. end
  347.  
  348. --JOB SPECIFIC FUNCTIONS--
  349.  
  350. function unload()
  351.     face(east)
  352.     emptyFrom(5)
  353.     face(north)
  354. end
  355.  
  356. function getFuel()
  357.     face(west)
  358.     fill(fuelSlot)
  359.     face(north)
  360. end
  361.  
  362. function gravel()
  363.     turtle.select(gravelSlot)
  364.     rest(1)
  365.     if turtle.compare() then
  366.         while turtle.compare() do
  367.             report("Encountered Gravel")
  368.             dig()
  369.             rest(1)
  370.         end
  371.     end
  372. end
  373.  
  374. function tossJunk()
  375.     for slot = 1, 16 do
  376.         turtle.select(slot)
  377.         if turtle.getItemCount(slot) == 64 then
  378.             if slot == cobbleSlot then
  379.                 turtle.drop(63)
  380.                 report("Dropping Junk.")
  381.             elseif slot == dirtSlot then
  382.                 turtle.drop(63)
  383.                 report("Dropping Junk.")
  384.             elseif slot == gravelSlot then
  385.                 turtle.drop(63)
  386.                 report("Dropping Junk.")
  387.             else
  388.             end
  389.         end
  390.     end
  391. end
  392.  
  393. function checkFull()
  394.     turtle.select(16)
  395.     if turtle.getItemCount(16) > 1 then
  396.         goHome()
  397.         goBack()
  398.     end
  399.     turtle.select(fuelSlot)
  400.     if turtle.getItemCount(fuelSlot) < 1 then
  401.         goHome()
  402.         goBack()
  403.     end
  404. end
  405.  
  406. function checkDig()
  407.     tossJunk()
  408.     checkFull()
  409.     for slot = stoneSlot, cobbleSlot do
  410.         turtle.select(slot)
  411.         if turtle.compare(slot) then
  412.             if slot == stoneSlot then
  413.                 turtle.select(cobbleSlot)
  414.                 dig()
  415.                 break
  416.             elseif slot == dirtSlot then
  417.                 turtle.select(dirtSlot)
  418.                 dig()
  419.                 break
  420.             elseif slot == gravelSlot then
  421.                 gravel()
  422.                 break
  423.             elseif slot == cobbleSlot then
  424.                 report("Cobble generator found.")
  425.                 goHome()
  426.             end
  427.         else
  428.         end
  429.     end
  430.     dig()
  431. end
  432.  
  433. function evaluate()
  434.     junk = 0
  435.     for slot = stoneSlot, cobbleSlot do
  436.         turtle.select(slot)
  437.         if turtle.compare() then
  438.             junk = junk + 1
  439.         end
  440.     end
  441.     if junk == 0 then
  442.         dig()
  443.         report("Treasure!")
  444.     else
  445.     end
  446. end
  447.  
  448. function findOre()
  449.     report("Scanning...")
  450.     lt()
  451.     evaluate()
  452.     lt()
  453.     lt()
  454.     evaluate()
  455.     lt()
  456. end
  457.  
  458. function digFd()
  459.     checkDig()
  460.     rest(1)
  461.     checkDig()
  462.     fd()
  463. end
  464.  
  465. function branch()
  466.     if h == west then
  467.         nbl = bl - (2 * bl)
  468.         while z > nbl do
  469.             digFd()
  470.             findOre()
  471.         end
  472.     elseif h == east then
  473.         nbl = bl
  474.         while z < nbl do
  475.             digFd()
  476.             findOre()
  477.         end
  478.     end
  479.     findZ(t)
  480. end
  481.  
  482. function trunk()
  483.     findY(b)
  484.     while x <= tl do
  485.         for i = 1, 3 do
  486.             digFd()
  487.         end
  488.         face(west)
  489.         branch()
  490.         face(east)
  491.         branch()
  492.         face(north)
  493.     end
  494.     b = b + 1
  495.     dumpLoc()
  496.     goHome()
  497. end
  498.  
  499. --INTERFACE--
  500.  
  501. function startup()
  502.     clear()
  503.     print("Welcome to the BranchMine program! :)")
  504.     print("please set a few starting variables.")
  505.     sleep(1)
  506.     clear()
  507.     print("How long should the trunk be?")
  508.     print()
  509.     write("Trunk Length: ")
  510.     input = read()
  511.     tl = tonumber(input)
  512.     print()
  513.     print("trunk length set to "..tostring(tl))
  514.     sleep(1)
  515.     clear()
  516.     print("How long should the branches be?")
  517.     print()
  518.     write("Branch Length: ")
  519.     input = read()
  520.     bl = tonumber(input)
  521.     print()
  522.     print("Branch length set to "..tostring(tl))
  523.     sleep(1)
  524.     clear()
  525.     print("Thank you for your input.")
  526.     print("Preparing files for mining operation.")
  527.     dumpLoc()
  528.     report("Getting fuel.")
  529.     getFuel()
  530.     report("Finding Bedrock layer.")
  531.     findBedrock()
  532.     dumpLoc()
  533. end
  534.  
  535. --PROGRAM--
  536.  
  537. getLoc()
  538. goHome()
  539. while b < 0 do
  540.     trunk()
  541. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement