LivioBrunner

ComputerCraft Minerv3.5

Oct 24th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.13 KB | None | 0 0
  1. local version = "3.5"
  2. local l = 0
  3. local allitems = 0
  4. local selectcounter = 0
  5. local maxprogram = 3
  6. local forwardcounter = 0
  7. local fullblocksize = 0
  8. local i = 0
  9. local j = 0
  10. local k = 0
  11. local width = 0
  12. local height = 0
  13. local length = 0
  14. local actx = 0
  15. local acty = 0
  16. local actz = 0
  17. local actyaw = 0
  18. local lastx = 0
  19. local lasty = 0
  20. local lastz = 0
  21. local lastyaw = 0
  22. local anwser = false
  23. turtle.select(1)
  24. --SAVE INIT--
  25.   fs.makeDir("saves")
  26.   if fs.exists("saves/fuelslot") ~= true then
  27.     save(0, "saves/fuelslot")
  28.     save(0, "saves/height")
  29.     save(0, "saves/width")
  30.     save(0, "saves/length")
  31.     save(0, "saves/direction")
  32.     save(0, "saves/fback")
  33.     save(0, "saves/fbclear")
  34.   end
  35. --/SAVE INIT--
  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. function startMessage()
  48.   print("----Welcome to the miner".. version .."----")
  49. end
  50. function items()
  51.   for i = 1, 16 do
  52.     actitem = turtle.getItemCount(i)
  53.     allitems = allitems + actitem
  54.   end
  55.   return allitems
  56. end
  57. function clearInv()
  58.   for i = 1,16 do
  59.     turtle.select(i)
  60.     turtle.drop(turtle.getItemCount(i))
  61.   end
  62.   turtle.select(1)
  63. end
  64. function clearterm()
  65.   term.clear()
  66.   term.setCursorPos(1,1)
  67. end
  68. function usefuel(slotnum)
  69.   turtle.select(slotnum)
  70.   turtle.refuel(1)
  71. end
  72. function setPositionVars()
  73.   lastx = actx
  74.   lasty = acty
  75.   lastz = actz
  76.   lastyaw = actyaw
  77. end
  78. function turtledig()
  79.    forwardcounter = forwardcounter + 1
  80.    blocksleft = fullblocksize - forwardcounter
  81.    writeterm(blocksleft.." Blocks left until end", 0)
  82.    writeterm(turtle.getFuelLevel().." Fuellevel", 1)
  83.    blocksleft = 0
  84.    turtle.dig()
  85.    if turtle.getItemCount(16) > 0 then
  86.     setPositionVars()
  87.     findBack(0,0,0,0,true)
  88.  
  89.    end
  90. end
  91. function turtledigup()
  92.    forwardcounter = forwardcounter + 1
  93.    blocksleft = fullblocksize - forwardcounter
  94.    writeterm(blocksleft.." Blocks left until end", 0)
  95.    writeterm(turtle.getFuelLevel().." Fuellevel", 1)
  96.    blocksleft = 0
  97.    turtle.digUp()
  98.    if turtle.getItemCount(16) > 0 then
  99.       setPositionVars()
  100.       findBack(0,0,0,0,true)
  101.    end
  102. end
  103. function turtledigdown()
  104.    forwardcounter = forwardcounter + 1
  105.    blocksleft = fullblocksize - forwardcounter
  106.    writeterm(blocksleft.." Blocks left until end", 0)
  107.    writeterm(turtle.getFuelLevel().." Fuellevel", 1)
  108.    blocksleft = 0
  109.    turtle.digDown()
  110.   if turtle.getItemCount(16) > 0 then
  111.     setPositionVars()
  112.     findBack(0,0,0,0,true)
  113.    end
  114. end
  115. function turtleup()
  116.   if turtle.up() == false then
  117.       while turtle.up() == false do
  118.          turtledigup()
  119.       end
  120.   end
  121.   acty = acty + 1
  122.   writeterm("x : ".. actx .. ", y : ".. acty .. ", z : ".. actz .. ", yaw : ".. actyaw, 2)
  123. end
  124. function turtledown()
  125.   if turtle.down() == false then
  126.       while turtle.down() == false do
  127.          turtledigdown()
  128.       end
  129.   end
  130.   acty = acty - 1
  131. end
  132. function goforward()
  133.   if turtle.forward() == false then
  134.       while turtle.forward() == false do
  135.          turtledig()
  136.       end
  137.   end
  138.   if actyaw == 0 then
  139.     actx = actx + 1
  140.   elseif actyaw == 1 then
  141.     actz = actz - 1
  142.   elseif actyaw == 2 then
  143.     actx = actx - 1
  144.   elseif actyaw == 3 then
  145.     actz = actz + 1
  146.   end
  147.   writeterm("x : ".. actx .. ", y : ".. acty .. ", z : ".. actz .. ", yaw : ".. actyaw, 2)
  148. end
  149. function findBack(x, y, z, yaw, anwser)
  150.   if anwser ~= true then
  151.     anwser = false
  152.   end
  153.   if x < actx then
  154.     while actyaw ~= 2 do
  155.       turtleleft()
  156.     end
  157.   elseif x > actx then
  158.     while actyaw ~= 0 do
  159.       turtleleft()
  160.     end
  161.   end
  162.   while actx ~= x do
  163.     goforward()
  164.   end
  165.   if z > actz then
  166.     while actyaw ~= 3 do
  167.       turtleleft()
  168.     end
  169.   elseif z < actz then
  170.     while actyaw ~= 1 do
  171.       turtleleft()
  172.     end
  173.   end
  174.   while actz ~= z do
  175.     goforward()
  176.   end
  177.   if y < acty then
  178.     while acty ~= y do
  179.       turtledown()
  180.     end
  181.   elseif y > acty then
  182.     while acty ~= y do
  183.       turtleup()
  184.     end
  185.   end
  186.   while actyaw ~= yaw do
  187.       turtleleft()
  188.     end
  189.   if anwser == true then
  190.     turtleleft()
  191.     turtleleft()
  192.     clearInv()
  193.     turtleleft()
  194.     turtleleft()
  195.     findBack(lastx, lasty, lastz, lastyaw, false)
  196.   end
  197. end
  198. function turtleright()
  199.   turtle.turnRight()
  200.   actyaw = actyaw + 1
  201.   if actyaw > 3 then
  202.     actyaw = 0
  203.   end
  204.   writeterm("x : ".. actx .. ", y : ".. acty .. ", z : ".. actz .. ", yaw : ".. actyaw, 2)
  205. end
  206. function turtleleft()
  207.   turtle.turnLeft()
  208.   actyaw = actyaw - 1
  209.   if actyaw < 0 then
  210.     actyaw = 3
  211.   end
  212.   writeterm("x : ".. actx .. ", y : ".. acty .. ", z : ".. actz .. ", yaw : ".. actyaw, 2)
  213. end
  214. function turnright()
  215.   turtleright()
  216.   turtledig()
  217.   goforward()
  218.   turtleright()
  219. end
  220. function turnleft()
  221.   turtleleft()
  222.   turtledig()
  223.   goforward()
  224.   turtleleft()
  225. end
  226. function manual()
  227.         clearterm()
  228.         startMessage()
  229.         write("Fuel slot:")
  230.         fuelslotnum = tonumber(read())
  231.         usefuel(fuelslotnum)
  232.         write("Heigth:")
  233.         high = tonumber(read())
  234.         write("Width:")
  235.         width = tonumber(read())
  236.         write("Length:")
  237.         length = tonumber(read())
  238.         write("up/down:")
  239.         direction = read()
  240.   clearterm()
  241.   createterm()
  242.   mine(high,width,length, fuelslotnum, direction)
  243. end
  244. function createterm()
  245.   term.setCursorPos(1,6)
  246.   term.clearLine()
  247.   print("------X Blocks left until end------")
  248.   print("------X Fuellevel------")
  249. end
  250. function writeterm(msg,cursorlevel)
  251.   cursorlevel = 6 + cursorlevel
  252.   term.setCursorPos(1,cursorlevel)
  253.   term.clearLine()
  254.   print("------"..msg.."------")
  255. end
  256. function control()
  257.   clearterm()
  258. startMessage()
  259. print("[p] = stop")
  260. print("[w] = forward")
  261. print("[f] = down")
  262. print("[r] = up")
  263.   while true do
  264.     local event, param1 = os.pullEvent("char")
  265.     if param1 == "p" then
  266.       break
  267.     elseif param1 == "w" then
  268.       goforward()
  269.     elseif param1 == "d" then
  270.       turtle.turnRight()
  271.     elseif param1 == "a" then
  272.       turtle.turnLeft()
  273.     elseif param1 == "s" then
  274.       turtle.back()
  275.     elseif param1 == "f" then
  276.       turtle.down()
  277.     elseif param1 == "r" then
  278.       turtle.up()
  279.     end
  280.   end
  281. end
  282. function path()
  283.   new = load("saves/fuelslot")
  284.   if new == 0 then
  285.         clearterm()
  286.         startMessage()
  287.         write("Fuel slot (number 1-16):")
  288.         fuelslotnum = tonumber(read())
  289.         usefuel(fuelslotnum)
  290.         write("Heigth (number):")
  291.         high = tonumber(read())
  292.         write("Width (number):")
  293.         width = tonumber(read())
  294.         write("Length (number):")
  295.         length = tonumber(read())
  296.         write("Direction (up/down):")
  297.         direction = read()
  298.         write("Walk back to start position (true/false):")
  299.         fback = read()
  300.         if fback == "true" then
  301.           write("Walk back to start position & clear Inventory - buggy (true/false):")
  302.           fbclear = read()
  303.         end
  304.         save(fuelslotnum, "saves/fuelslot")
  305.         save(high, "saves/height")
  306.         save(width, "saves/width")
  307.         save(length, "saves/length")
  308.         save(direction, "saves/direction")
  309.         save(fback, "saves/fback")
  310.         save(fbclear, "saves/fbclear")
  311.         clearterm()
  312.         startMessage()
  313.         mainmenu()
  314.   else
  315.     fuelslotnum = load("saves/fuelslot")
  316.     high = load("saves/height")
  317.     width = load("saves/width")
  318.     length = load("saves/length")
  319.     direction = load("saves/direction")
  320.     fback = load("saves/fback")
  321.     fbclear = load("saves/fbclear")
  322.     clearterm()
  323.     createterm()
  324.     mine(high,width,length, fuelslotnum, direction, fback, fbclear)
  325.   end
  326.  
  327.  
  328.  
  329. end
  330. function refuel()
  331.   clearterm()
  332.   startMessage()
  333.   createterm()
  334.   for i = 1,16 do
  335.   if turtle.getItemCount(i) > 0 then
  336.     for j = 1,turtle.getItemCount(i) do
  337.       usefuel(i)
  338.         writeterm("", 0)
  339.         writeterm(turtle.getFuelLevel().." Fuellevel", 1)
  340.     end
  341.   end
  342.   end
  343. end
  344. function mainmenu()
  345.         print("Choose Program:")
  346.         print("[Set own Path]")
  347.         print(" Manual ")
  348.         print(" Control Turtle ")
  349.         print(" Refuel ")
  350.         print()
  351.         print("------"..turtle.getFuelLevel().." Fuellevel------")
  352.         while true do
  353.                 local event1, param2 = os.pullEvent("key")
  354.                 if param2 == 200 then
  355.                         selectcounter = selectcounter - 1
  356.                        changeSelector(selectcounter)
  357.                 elseif param2 == 208 then
  358.                         selectcounter = selectcounter + 1
  359.                         changeSelector(selectcounter)
  360.                 elseif param2 == 28 then
  361.                         if selectcounter == 0 then
  362.                                 path()
  363.                         elseif selectcounter == 1 then
  364.                                 manual()
  365.                         elseif selectcounter == 2 then
  366.                                 control()
  367.                         elseif selectcounter == 3 then
  368.                                 refuel()
  369.                         end
  370.                 end
  371.         end
  372. end
  373. function changeSelector(selectcounter)
  374.   if selectcounter < 0 then
  375.                         selectcounter = maxprogram
  376.                 elseif selectcounter > maxprogram then
  377.                         selectcounter = 0
  378.                 end
  379.                 if selectcounter == 0 then
  380.                         term.setCursorPos(1,3)
  381.                         term.clearLine()
  382.                         print("[Set own path]")
  383.                         term.setCursorPos(1,4)
  384.                         term.clearLine()
  385.                         print(" Manual ")
  386.                         term.setCursorPos(1,5)
  387.                         term.clearLine()
  388.                         print(" Control Turtle ")
  389.                         term.setCursorPos(1,6)
  390.                         term.clearLine()
  391.                         print(" Refuel ")
  392.                 elseif selectcounter == 1 then
  393.                         term.setCursorPos(1,3)
  394.                         term.clearLine()
  395.                         print(" Set own path ")
  396.                         term.setCursorPos(1,4)
  397.                         term.clearLine()
  398.                         print("[Manual]")
  399.                         term.setCursorPos(1,5)
  400.                         term.clearLine()
  401.                         print(" Control Turtle ")
  402.                         term.setCursorPos(1,6)
  403.                         term.clearLine()
  404.                         print(" Refuel ")
  405.                 elseif selectcounter == 2 then
  406.                         term.setCursorPos(1,3)
  407.                         term.clearLine()
  408.                         print(" Set own path ")
  409.                         term.setCursorPos(1,4)
  410.                         term.clearLine()
  411.                         print(" Manual ")
  412.                         term.setCursorPos(1,5)
  413.                         term.clearLine()
  414.                         print("[Control Turtle]")
  415.                         term.setCursorPos(1,6)
  416.                         term.clearLine()
  417.                         print(" Refuel ")
  418.                 elseif selectcounter == 3 then
  419.                         term.setCursorPos(1,3)
  420.                         term.clearLine()
  421.                         print(" Set own path ")
  422.                         term.setCursorPos(1,4)
  423.                         term.clearLine()
  424.                         print(" Manual ")
  425.                         term.setCursorPos(1,5)
  426.                         term.clearLine()
  427.                         print(" Control Turtle ")
  428.                         term.setCursorPos(1,6)
  429.                         term.clearLine()
  430.                         print("[Refuel]")
  431.                 end
  432. end
  433. function mine(high, width, length, fuelslotnum, direction, fback, fbclear)
  434. l = 0
  435. if fback == nil then
  436.   fback = false
  437. end
  438. if fbclear == nil then
  439.   fbclear = false
  440. end
  441. fullblocksize = high * width * length
  442.   for i = 1, high do
  443.     for j = 1, width do
  444.       for k = 1, length do
  445.         if turtle.refuel(1) == true then
  446.           usefuel(fuelslotnum)
  447.         end
  448.         turtledig()
  449.         goforward()
  450.       end
  451.       if j < width then
  452.       l = l + 1
  453.       if l < 2 then
  454.         turnright()
  455.       else
  456.         turnleft()
  457.       l = 0
  458.       end
  459.       end
  460.     end
  461.     if direction == "down" then
  462.       turtledigdown()
  463.       turtledown()
  464.     else
  465.       turtledigup()
  466.       turtleup()
  467.     end
  468.     turtleleft()
  469.     turtleleft()
  470.   end
  471.   if fback == "true" and fbclear == "false" then
  472.     findBack(0,0,0,0)
  473.   elseif fback == "true" and fbclear == "true" then
  474.     findBack(0,0,0,1)
  475.   end
  476.   clearterm()
  477.   startMessage()
  478.   mainmenu()
  479. end
  480. --INIT
  481.   clearterm()
  482.   startMessage()
  483.   mainmenu()
  484.  
  485. --/INIT
Advertisement
Add Comment
Please, Sign In to add comment