LivioBrunner

Miner v4

Nov 11th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.22 KB | None | 0 0
  1. local version = "4"
  2. --MENU
  3. local menu = {"Set own Path", "Manual", "Logger", "Refuel", "Settings" }
  4. local menufunction = {path, manual, logger, refuel, settings}
  5. local settingsmenu = {"Pre Settings", "Set own Path"}
  6. local settingsmenufunction = {presettings, newpath}
  7. --/MENU
  8. --DEFINITION
  9. local l = 0
  10. local allitems = 0
  11. local selectcounter = 1
  12. local forwardcounter = 0
  13. local fullblocksize = 0
  14. local i = 0
  15. local j = 0
  16. local k = 0
  17. local width = 0
  18. local height = 0
  19. local length = 0
  20. local actx = 0
  21. local acty = 0
  22. local actz = 0
  23. local actyaw = 0
  24. local lastx = 0
  25. local lasty = 0
  26. local lastz = 0
  27. local lastyaw = 0
  28. local anwser = false
  29. local cutCounter = 0
  30. local modemid = 0
  31. local modemmode = false
  32. local menucount = 0
  33. --/DEFINITION
  34. turtle.select(1)
  35. --SAVES
  36. function load(name)
  37.   local file = fs.open(name,"r")
  38.   local data = file.readAll()
  39.   file.close()
  40.   return textutils.unserialize(data)
  41. end
  42. function save(table,name)
  43.   local file = fs.open(name,"w")
  44.   file.write(textutils.serialize(table))
  45.   file.close()
  46. end
  47. --/SAVES
  48. --TERM
  49. function startMessage()
  50.   print("------Welcome to the miner".. version .."------")
  51. end
  52. function clearterm()
  53.   term.clear()
  54.   term.setCursorPos(1,1)
  55. end
  56. function createterm()
  57.   term.setCursorPos(1,6)
  58.   term.clearLine()
  59.   print("--------X Blocks left until end-------")
  60.   print("--------X Fuellevel-------")
  61. end
  62. function writeterm(msg,cursorlevel)
  63.   cursorlevel = 6 + cursorlevel
  64.   term.setCursorPos(1,cursorlevel)
  65.   term.clearLine()
  66.   print("--------"..msg.."-------")
  67. end
  68. --/TERM
  69. --SAVE INIT
  70.   fs.makeDir("saves")
  71.   if fs.exists("saves/modemmode") then
  72.     if load("saves/modemmode") == "true" then
  73.         rednet.open("right")
  74.     end
  75.   end
  76.   if fs.exists("saves/fuelslot") ~= true then
  77.     startMessage()
  78.     clearterm()
  79.     print("--Pre-settings--")
  80.     write("Modem mode(true/false):")
  81.     modemmode = read()
  82.     if modemmode == "true" then
  83.      write("Computer ID(number):")
  84.     modemid = tonumber(read())
  85.         rednet.open("right")
  86.     end
  87.     save(modemmode, "saves/modemmode")
  88.     save(modemid, "saves/modemid")
  89.     save(0, "saves/fuelslot")
  90.     save(0, "saves/height")
  91.     save(0, "saves/width")
  92.     save(0, "saves/length")
  93.     save(0, "saves/direction")
  94.     save(0, "saves/fback")
  95.     save(0, "saves/fbclear")
  96.   end
  97. --/SAVE INIT--
  98. --FUNCTIONS
  99. function tableLength(T)
  100.   local count = 0
  101.   for _ in pairs(T) do count = count + 1 end
  102.   return count
  103. end
  104. function items()
  105.   for i = 1, 16 do
  106.     actitem = turtle.getItemCount(i)
  107.     allitems = allitems + actitem
  108.   end
  109.   return allitems
  110. end
  111. function clearInv()
  112.   for i = 1,16 do
  113.     turtle.select(i)
  114.     turtle.drop(turtle.getItemCount(i))
  115.   end
  116.   turtle.select(1)
  117. end
  118.  
  119. function usefuel(slotnum)
  120.   turtle.select(slotnum)
  121.   turtle.refuel(1)
  122.    modemmode = load("saves/modemmode")
  123.    modemid = load("saves/modemid")
  124.    if modemmode == "true" then
  125.     rednet.send(modemid, turtle.getFuelLevel())
  126.    end
  127. end
  128. function setPositionVars()
  129.   lastx = actx
  130.   lasty = acty
  131.   lastz = actz
  132.   lastyaw = actyaw
  133. end
  134. function countDown(time, fuelslot)
  135.     for p = time,0,-1 do
  136.         writeterm("Sleep mode for :" ..p.. " seconds", 2)
  137.                 if fuelslot ~= nil then
  138.             usefuel(fuelslot)
  139.         end
  140.         sleep(1)   
  141.     end
  142. end
  143. --/FUNCTIONS
  144. --WALKING
  145. function turtledig()
  146.    turtle.suck()
  147.    modemmode = load("saves/modemmode")
  148.    modemid = load("saves/modemid")
  149.    if modemmode == "true" then
  150.     rednet.send(modemid, turtle.getFuelLevel())
  151.    end
  152.    forwardcounter = forwardcounter + 1
  153.    blocksleft = fullblocksize - forwardcounter
  154.    writeterm(blocksleft.." Blocks left until end", 0)
  155.    writeterm(turtle.getFuelLevel().." Fuellevel", 1)
  156.    blocksleft = 0
  157.    turtle.dig()
  158.    if turtle.getItemCount(16) > 0 then
  159.     setPositionVars()
  160.     findBack(0,0,0,0,true)
  161.  
  162.    end
  163. end
  164. function turtledigup()
  165.    turtle.suck()
  166.    forwardcounter = forwardcounter + 1
  167.    blocksleft = fullblocksize - forwardcounter
  168.    writeterm(blocksleft.." Blocks left until end", 0)
  169.    writeterm(turtle.getFuelLevel().." Fuellevel", 1)
  170.    blocksleft = 0
  171.    turtle.digUp()
  172.    if turtle.getItemCount(16) > 0 then
  173.       setPositionVars()
  174.       findBack(0,0,0,0,true)
  175.    end
  176. end
  177. function turtledigdown()
  178.   turtle.suck()
  179.    forwardcounter = forwardcounter + 1
  180.    blocksleft = fullblocksize - forwardcounter
  181.    writeterm(blocksleft.." Blocks left until end", 0)
  182.    writeterm(turtle.getFuelLevel().." Fuellevel", 1)
  183.    blocksleft = 0
  184.    turtle.digDown()
  185.   if turtle.getItemCount(16) > 0 then
  186.     setPositionVars()
  187.     findBack(0,0,0,0,true)
  188.    end
  189. end
  190. function turtleup()
  191.  turtle.suck()
  192.   if turtle.up() == false then
  193.       while turtle.up() == false do
  194.          turtledigup()
  195.          turtle.suck()
  196.       end
  197.   end
  198.   acty = acty + 1
  199.   writeterm("x : ".. actx .. ", y : ".. acty .. ", z : ".. actz .. ", yaw : ".. actyaw, 2)
  200. end
  201. function turtledown()
  202.   turtle.suck()
  203.   if turtle.down() == false then
  204.       while turtle.down() == false do
  205.          turtledigdown()
  206.      turtle.suck()
  207.       end
  208.   end
  209.   acty = acty - 1
  210. end
  211. function goforward()
  212.   turtle.suck()
  213.   modemmode = load("saves/modemmode")
  214.    modemid = load("saves/modemid")
  215.    if modemmode == "true" then
  216.     rednet.send(modemid, turtle.getFuelLevel())
  217.    end
  218.   if turtle.forward() == false then
  219.       while turtle.forward() == false do
  220.          turtledig()
  221.          turtle.suck()
  222.       end
  223.   end
  224.   if actyaw == 0 then
  225.     actx = actx + 1
  226.   elseif actyaw == 1 then
  227.     actz = actz - 1
  228.   elseif actyaw == 2 then
  229.     actx = actx - 1
  230.   elseif actyaw == 3 then
  231.     actz = actz + 1
  232.   end
  233.   writeterm("x : ".. actx .. ", y : ".. acty .. ", z : ".. actz .. ", yaw : ".. actyaw, 2)
  234. end
  235. function findBack(x, y, z, yaw, anwser)
  236.   if anwser ~= true then
  237.     anwser = false
  238.   end
  239.   if x < actx then
  240.     while actyaw ~= 2 do
  241.       turtleleft()
  242.     end
  243.   elseif x > actx then
  244.     while actyaw ~= 0 do
  245.       turtleleft()
  246.     end
  247.   end
  248.   while actx ~= x do
  249.     goforward()
  250.   end
  251.   if z > actz then
  252.     while actyaw ~= 3 do
  253.       turtleleft()
  254.     end
  255.   elseif z < actz then
  256.     while actyaw ~= 1 do
  257.       turtleleft()
  258.     end
  259.   end
  260.   while actz ~= z do
  261.     goforward()
  262.   end
  263.   if y < acty then
  264.     while acty ~= y do
  265.       turtledown()
  266.     end
  267.   elseif y > acty then
  268.     while acty ~= y do
  269.       turtleup()
  270.     end
  271.   end
  272.   while actyaw ~= yaw do
  273.       turtleleft()
  274.     end
  275.   if anwser == true then
  276.     turtleleft()
  277.     turtleleft()
  278.     clearInv()
  279.     turtleleft()
  280.     turtleleft()
  281.     findBack(lastx, lasty, lastz, lastyaw, false)
  282.   end
  283. end
  284. function turtleright()
  285.   turtle.turnRight()
  286.   actyaw = actyaw + 1
  287.   if actyaw > 3 then
  288.     actyaw = 0
  289.   end
  290.   writeterm("x : ".. actx .. ", y : ".. acty .. ", z : ".. actz .. ", yaw : ".. actyaw, 2)
  291. end
  292. function turtleleft()
  293.   turtle.turnLeft()
  294.   actyaw = actyaw - 1
  295.   if actyaw < 0 then
  296.     actyaw = 3
  297.   end
  298.   writeterm("x : ".. actx .. ", y : ".. acty .. ", z : ".. actz .. ", yaw : ".. actyaw, 2)
  299. end
  300. function turnright()
  301.   turtleright()
  302.   turtledig()
  303.   goforward()
  304.   turtleright()
  305. end
  306. function turnleft()
  307.   turtleleft()
  308.   turtledig()
  309.   goforward()
  310.   turtleleft()
  311. end
  312. --/WALKING
  313. --MENUFUNCTIONS
  314. function manual()
  315.         clearterm()
  316.         startMessage()
  317.         write("Fuel slot:")
  318.         fuelslotnum = tonumber(read())
  319.         usefuel(fuelslotnum)
  320.         write("Heigth:")
  321.         high = tonumber(read())
  322.         write("Width:")
  323.         width = tonumber(read())
  324.         write("Length:")
  325.         length = tonumber(read())
  326.         write("up/down:")
  327.         direction = read()
  328.         write("Walk back to start position (true/false):")
  329.         fback = read()
  330.         if fback == "true" then
  331.           write("Walk back to start position & clear Inventory - buggy (true/false):")
  332.           fbclear = read()
  333.         end
  334.   clearterm()
  335.   createterm()
  336.   mine(high,width,length, fuelslotnum, direction, fback, fbclear)
  337. end
  338. function logger()
  339.   clearterm()
  340.   write("Fuel slot (1,4-16):")
  341.   lfuelslotnum = tonumber(read())
  342.   write("Saplings (left):")
  343.   saplingsleft = tonumber(read())
  344.   write("Saplings (front):")
  345.   saplingsfront = tonumber(read())
  346.   write("Sleep Timer:")
  347.   sleepTimer = tonumber(read())
  348.   clearterm()
  349.   startMessage()
  350. while true do
  351.   for m = 0, saplingsfront do  
  352.   cutCounter = cutCounter + 1
  353.   if cutCounter > 1 then
  354.     cutCounter = 0
  355.   end
  356.   findBack(actx + 1,0,actz,0,false)
  357.   turtle.select(3)
  358.   if turtle.compare() then
  359.     mineTree(actx,actz)
  360.   end
  361.   turtle.select(2)
  362.   turtle.place()
  363.  
  364.   for n = 0,saplingsleft do
  365.     if cutCounter == 1 then
  366.         findBack(actx,0,actz + 3,0,false)
  367.         else
  368.            findBack(actx,0,actz - 3,0,false)   
  369.     end
  370.   turtle.select(3)
  371.   if turtle.compare() then
  372.     mineTree(actx,actz)
  373.   end
  374.   turtle.select(2)
  375.   turtle.place()
  376.   end
  377.   findBack(actx,0,actz + 1,0,false)
  378.   findBack(actx + 2,0,actz,0,false)
  379.   findBack(actx,0,actz - 1,0,false)
  380.  end
  381.   findBack(actx,0,actz+2,0,false)
  382.   findBack(0,0,0,2,false)
  383.   turtle.select(3)
  384.   turtle.drop(turtle.getItemCount(3)-1)
  385.   for i = 4,16 do
  386.     turtle.select(i)
  387.     turtle.drop(turtle.getItemCount(i))
  388.   end
  389.   turtle.select(1)
  390.   countDown(sleepTimer, lfuelslotnum)
  391.   turtleleft()
  392.   turtleleft()
  393. end
  394. end
  395. function mineTree(x,z)
  396.    x = x + 1
  397.    findBack(x,6,z,0,false)
  398.    x = x - 1
  399.    findBack(x,0,z,0,false)
  400. end
  401. function path()
  402.   new = load("saves/fuelslot")
  403.   if new == 0 then
  404.         clearterm()
  405.         startMessage()
  406.         write("Fuel slot (number 1-16):")
  407.         fuelslotnum = tonumber(read())
  408.         usefuel(fuelslotnum)
  409.         write("Heigth (number):")
  410.         high = tonumber(read())
  411.         write("Width (number):")
  412.         width = tonumber(read())
  413.         write("Length (number):")
  414.         length = tonumber(read())
  415.         write("Direction (up/down):")
  416.         direction = read()
  417.         write("Walk back to start position (true/false):")
  418.         fback = read()
  419.         if fback == "true" then
  420.           write("Walk back to start position & clear Inventory - buggy (true/false):")
  421.           fbclear = read()
  422.         end
  423.         save(fuelslotnum, "saves/fuelslot")
  424.         save(high, "saves/height")
  425.         save(width, "saves/width")
  426.         save(length, "saves/length")
  427.         save(direction, "saves/direction")
  428.         save(fback, "saves/fback")
  429.         save(fbclear, "saves/fbclear")
  430.         clearterm()
  431.         startMessage()
  432.         mainmenu()
  433.   else
  434.     fuelslotnum = load("saves/fuelslot")
  435.     high = load("saves/height")
  436.     width = load("saves/width")
  437.     length = load("saves/length")
  438.     direction = load("saves/direction")
  439.     fback = load("saves/fback")
  440.     fbclear = load("saves/fbclear")
  441.     clearterm()
  442.     createterm()
  443.     mine(high,width,length, fuelslotnum, direction, fback, fbclear)
  444.   end
  445. end
  446. function refuel()
  447.   clearterm()
  448.   startMessage()
  449.   createterm()
  450.   for i = 1,16 do
  451.   if turtle.getItemCount(i) > 0 then
  452.     for j = 1,turtle.getItemCount(i) do
  453.       usefuel(i)
  454.         writeterm("", 0)
  455.         writeterm(turtle.getFuelLevel().." Fuellevel", 1)
  456.     end
  457.   end
  458.   end
  459.     clearterm()
  460.     createterm()
  461. end
  462.  
  463.  
  464. function settings()
  465.     clearterm()
  466.     startMessage()
  467.     mainmenu(settingsmenu, settingsmenufunction)
  468. end
  469. --/MENUFUNCTIONS
  470. --MENUSETTINGSFUNCTIONS
  471. function newpath()
  472.     save(0, "saves/fuelslot")
  473.     path()
  474.    
  475. end
  476. function presettings()
  477.     startMessage()
  478.         clearterm()
  479.         print("--Pre-settings--")
  480.         write("Modem mode(true/false):")
  481.         modemmode = read()
  482.         if modemmode == "true" then
  483.         write("Computer ID(number):")
  484.         modemid = tonumber(read())
  485.             rednet.open("right")
  486.         end
  487.         save(modemmode, "saves/modemmode")
  488.         save(modemid, "saves/modemid")
  489.     clearterm()
  490.     startMessage()
  491.     mainmenu()
  492. end
  493. --/MENUSETTINGSFUNCTIONS
  494.  
  495. function mainmenu(T, T2)
  496.     selectcounter = 1
  497.     menucount = 0
  498.     if T == nil then
  499.         T = menu
  500.     end
  501.     if T2 == nil then
  502.         T2 = menufunction
  503.     end
  504.         print("Choose Program:")
  505.      for _ in pairs(T) do
  506.                 menucount = menucount + 1
  507.                 term.setCursorPos(1,2 + menucount)
  508.                             term.clearLine()
  509.                 if menucount == selectcounter then
  510.                     print("["..T[menucount].."]")
  511.                 else
  512.                     print(" "..T[menucount].." ")
  513.                 end
  514.                
  515.             end    
  516.         print()
  517.         print("------"..turtle.getFuelLevel().." Fuellevel------")
  518.         while true do
  519.                 local event1, param2 = os.pullEvent("key")
  520.                 if param2 == 200 then
  521.                         selectcounter = selectcounter - 1
  522.                        changeSelector(selectcounter, T)
  523.                 elseif param2 == 208 then
  524.                         selectcounter = selectcounter + 1
  525.                         changeSelector(selectcounter, T)
  526.                 elseif param2 == 28 then
  527.                         T2[selectcounter]()
  528.                 end
  529.         end
  530. end
  531. function changeSelector(selectcounter, T)
  532.     if selectcounter < 1 then
  533.             selectcounter = maxprogram
  534.         elseif selectcounter > tableLength(T) then
  535.                 selectcounter = 1
  536.         end
  537.     menucount = 0
  538.     for _ in pairs(T) do
  539.         menucount = menucount + 1
  540.         term.setCursorPos(1,2 + menucount)
  541.                 term.clearLine()
  542.         if menucount == selectcounter then
  543.             print("["..T[menucount].."]")
  544.         else
  545.             print(" "..T[menucount].." ")
  546.         end  
  547.     end  
  548.     print()                                
  549. end
  550. function mine(high, width, length, fuelslotnum, direction, fback, fbclear)
  551. l = 0
  552. if fback == nil then
  553.   fback = false
  554. end
  555. if fbclear == nil then
  556.   fbclear = false
  557. end
  558. fullblocksize = high * width * length
  559.   for i = 1, high do
  560.     for j = 1, width do
  561.       for k = 2, length do
  562.         if turtle.refuel(1) == true then
  563.           usefuel(fuelslotnum)
  564.         end
  565.         turtledig()
  566.         goforward()
  567.       end
  568.       if j < width then
  569.       l = l + 1
  570.       if l < 2 then
  571.         turnright()
  572.       else
  573.         turnleft()
  574.       l = 0
  575.       end
  576.       end
  577.     end
  578.     if direction == "down" then
  579.       turtledigdown()
  580.       turtledown()
  581.     else
  582.       turtledigup()
  583.       turtleup()
  584.     end
  585.     turtleleft()
  586.     turtleleft()
  587.   end
  588.   if fback == "true" and fbclear == "false" then
  589.     findBack(0,0,0,0)
  590.   elseif fback == "true" and fbclear == "true" then
  591.     findBack(0,0,0,1)
  592.   end
  593.   clearterm()
  594.   startMessage()
  595.   mainmenu()
  596. end
  597. --INIT
  598.   clearterm()
  599.   startMessage()
  600.   mainmenu()
  601.  
  602. --/INIT
Advertisement
Add Comment
Please, Sign In to add comment