Advertisement
Guest User

advancedorefinder

a guest
Feb 27th, 2013
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.06 KB | None | 0 0
  1. -- Advanced Ore Finder by Henness
  2. -- Version 3.0 2/5/2013
  3.  
  4. -- Config
  5. local program = "Advanced Orefinder"
  6. local version = "v3.0"
  7. local author = "Henness"
  8. screenw,screenh = term.getSize()
  9.  
  10. -- Functions
  11.  
  12. --[[
  13.     Most of the loc movement functions were written by Mikeemoo,
  14.     for his killer program. If you would like to see his program
  15.     you can download it from: http://pastebin.com/UCES36jD
  16. ]]
  17. local loc = {}
  18. loc = {
  19.     paused = false,
  20.    
  21.     x = 0,
  22.     y = 0,
  23.     z = 0,
  24.     facing = 0,
  25.    
  26.     facingToAxis = {
  27.         [0] = { axis = 'z', unit = 1 },
  28.         [1] = { axis = 'x', unit = -1 },
  29.         [2] = { axis = 'z', unit = -1 },
  30.         [3] = { axis = 'x', unit = 1 }
  31.     },
  32.    
  33.     pause = function()
  34.         while loc.paused do
  35.             sleep(1)
  36.         end
  37.     end,
  38.    
  39.     getAxisForFacing = function()
  40.         return loc.facingToAxis[loc.facing]['axis'], loc.facingToAxis[loc.facing]['unit']
  41.     end,
  42.  
  43.     turnLeft = function()
  44.         loc.pause()
  45.         loc.facing = (loc.facing - 1) % 4
  46.         turtle.turnLeft()
  47.     end,
  48.  
  49.     turnRight = function()
  50.         loc.pause()
  51.         loc.facing = (loc.facing + 1) % 4
  52.         turtle.turnRight()
  53.     end,
  54.  
  55.     moveForward = function()
  56.         loc.pause()
  57.         if turtle.forward() then
  58.             local axis, unit = loc.getAxisForFacing()
  59.             loc[axis] = loc[axis] + unit
  60.             return true
  61.         end
  62.         return false
  63.     end,
  64.  
  65.     face = function(faceto)
  66.         local dirdif = (faceto - loc.facing) % 4
  67.         if dirdif == 1 then
  68.             loc.turnRight()
  69.         elseif dirdif == 2 then
  70.             loc.turnRight()
  71.             loc.turnRight()
  72.         elseif dirdif == 3 then
  73.             loc.turnLeft()
  74.         end
  75.     end,
  76.  
  77.     movez = function(d)
  78.         if d < loc.z then loc.face(2) elseif d > loc.z then loc.face(0) end
  79.         return loc.moveForward()
  80.     end,
  81.  
  82.     movex = function(d)
  83.         if d < loc.x then loc.face(1) elseif d > loc.x then loc.face(3) end
  84.         return loc.moveForward()
  85.     end,
  86.  
  87.     movey = function(d)
  88.         if d < loc.y then
  89.             return loc.moveDown()
  90.         end
  91.         return loc.moveUp()
  92.     end,
  93.  
  94.     moveUp = function()
  95.         loc.pause()
  96.         if turtle.up() then
  97.             loc.y = loc.y + 1
  98.             return true
  99.         end
  100.         return false
  101.     end,
  102.  
  103.     moveDown = function()
  104.         loc.pause()
  105.         if turtle.down() then
  106.             loc.y = loc.y - 1
  107.             return true
  108.         end
  109.         return false
  110.     end,
  111.  
  112.     moveTo = function(x, y, z, facing)
  113.         if y > loc.y then
  114.             while loc.y < y do
  115.                 while not loc.moveUp() do
  116.                     if turtle.detectUp() then
  117.                         turtle.select(1)
  118.                         turtle.digUp()
  119.                     else
  120.                         turtle.attackUp()
  121.                     end
  122.                 end
  123.             end
  124.         end
  125.         while x ~= loc.x do
  126.             while not loc.movex(x) do
  127.                 if turtle.detect() then
  128.                     turtle.select(1)
  129.                     turtle.dig()
  130.                 else
  131.                     turtle.attack()
  132.                 end
  133.             end
  134.         end
  135.         while z ~= loc.z do
  136.             while not loc.movez(z) do
  137.                 if turtle.detect() then
  138.                     turtle.select(1)
  139.                     turtle.dig()
  140.                 else
  141.                     turtle.attack()
  142.                 end
  143.             end
  144.         end
  145.         if y < loc.y then
  146.             while loc.y > y do
  147.                 while not loc.moveDown() do
  148.                     if turtle.detectDown() then
  149.                         turtle.select(1)
  150.                         turtle.digDown()
  151.                     else
  152.                         turtle.attackDown()
  153.                     end
  154.                 end
  155.             end
  156.         end
  157.         loc.face(facing)
  158.         return true
  159.     end,
  160.    
  161.     moveToPos = function(pos)
  162.         return loc.moveTo(pos.x, pos.y, pos.z, pos.facing)
  163.     end,
  164.  
  165.     distanceTo = function(x, y, z)
  166.         return math.abs(x) - loc.x + math.abs(y) - loc.y + math.abs(z) - loc.z
  167.     end,
  168.  
  169.     distanceToPos = function(pos)
  170.         return loc.distanceTo(pos.x, pos.y, pos.z)
  171.     end
  172. }
  173.  
  174. -- Orefinder
  175. function saveTable(table,name)
  176.     local file = fs.open(name,"w")
  177.     file.write(textutils.serialize(table))
  178.     file.close()
  179. end
  180.  
  181. function loadTable(name)
  182.     local file = fs.open(name,"r")
  183.     local data = file.readAll()
  184.     file.close()
  185.     return textutils.unserialize(data)
  186. end
  187.  
  188. function moveDown(h)
  189.     while not loc.movey(h) do
  190.         if turtle.detectDown() then
  191.             turtle.digDown()
  192.             if turtle.detectDown() then
  193.                 return false
  194.             end
  195.         else
  196.             turtle.attackDown()
  197.         end
  198.     end
  199.     return true
  200. end
  201.  
  202. function compareForward(a, b)
  203.     loc.pause()
  204.     for i=a,b do
  205.         turtle.select(i)
  206.         if turtle.compare() then
  207.             return false
  208.         end            
  209.     end
  210.     return true
  211. end
  212.  
  213. function findDown(d)
  214.     if not d then d = loc.y end
  215.     local h = loc.y - d
  216.     while loc.y ~= h do
  217.         if not moveDown(h) then
  218.             break
  219.         end
  220.         for j=1,4 do
  221.             if compareForward(1, _tSave.ignore) then
  222.                 if not space() then
  223.                     returnToChest()
  224.                 end
  225.                 turtle.select(1)
  226.                 turtle.dig()
  227.                 ores_mined = ores_mined + 1
  228.             end
  229.             loc.turnRight()
  230.         end
  231.     end
  232. end
  233.  
  234. function refuel(ammount)
  235.     if turtle.getFuelLevel() == "unlimited" then
  236.         return true
  237.     end
  238.     local needed = ammount or loc.distanceToPos(_tSave.startPos) * 2 + 1
  239.     if turtle.getFuelLevel() < needed then
  240.         for n=_tSave.ignore+1,16 do
  241.             loc.pause()
  242.             if turtle.getItemCount(n) > 0 then
  243.                 turtle.select(n)
  244.                 if turtle.refuel(1) then
  245.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  246.                         turtle.refuel(1)
  247.                     end
  248.                     if turtle.getFuelLevel() >= needed then
  249.                         turtle.select(1)
  250.                         return true
  251.                     end
  252.                 end
  253.             end
  254.         end
  255.         turtle.select(1)
  256.         return false
  257.     end
  258.     return true
  259. end
  260.  
  261. function unload()
  262.     for n=1,16 do
  263.         loc.pause()
  264.         if turtle.getItemCount(n) > 0 then
  265.             turtle.select(n)
  266.             if n <= _tSave.ignore then
  267.                 if (not _tSave.safemode  and n == 1) or n > 1 then
  268.                     turtle.drop(turtle.getItemCount(n) - 1)
  269.                 end
  270.             else
  271.                 turtle.drop()
  272.             end
  273.         end
  274.     end
  275.     turtle.select(1)
  276. end
  277.  
  278. function space()
  279.     for n=1,16 do
  280.         if turtle.getItemCount(n) == 0 then
  281.             return true
  282.         end
  283.     end
  284.     return false
  285. end
  286.  
  287. function returnToChest()
  288.     local returnPos = {x = loc.x, y = loc.y, z = loc.z, facing = loc.facing}
  289.     local fuelNeeded = loc.distanceToPos(_tSave.startPos) * 2 + 1
  290.     loc.moveToPos(_tSave.startPos)
  291.     loc.face((loc.facing + 2) % 4)
  292.     if not refuel(fuelNeeded) then
  293.         unload()
  294.         while not refuel(fuelNeeded) do
  295.             loc.pause()
  296.             sleep(1)
  297.         end
  298.     else
  299.         unload()   
  300.     end
  301.     loc.moveToPos(returnPos)
  302. end
  303.  
  304. function forward()
  305.     if not space() then
  306.         returnToChest()
  307.     end
  308.     while not loc.moveForward() do
  309.         if turtle.detect() then
  310.             turtle.select(1)
  311.             turtle.dig()
  312.         else
  313.             turtle.attack()
  314.         end
  315.     end
  316. end
  317.  
  318. function oreFinder()
  319.     loc.moveToPos(_tSave.returnPos)
  320.     while _tSave.w <= _tSave.width do
  321.         while _tSave.l <= _tSave.length do
  322.             if not refuel() or not space() then
  323.                 returnToChest()
  324.             end
  325.             if math.fmod((loc.x + 4) - (loc.z - 1) * 2, 5) == 0 then
  326.                 _tSave.returnPos = {x = loc.x, y = loc.y, z = loc.z, facing = loc.facing}
  327.                 saveTable( _tSave, _sFile )
  328.                 findDown()
  329.                 loc.moveToPos(_tSave.returnPos)
  330.                 if _tSave.safemode then
  331.                     turtle.select(1)
  332.                     turtle.placeDown()
  333.                 end
  334.             end
  335.             turtle.digUp()
  336.             if _tSave.l ~= _tSave.length then
  337.                 forward()
  338.             elseif _tSave.l == _tSave.length and _tSave.w ~= _tSave.width then
  339.                 if loc.facing == _tSave.startPos.facing then
  340.                     loc.turnRight()
  341.                     forward()
  342.                     loc.turnRight()
  343.                 else
  344.                     loc.turnLeft()
  345.                     forward()
  346.                     loc.turnLeft()
  347.                 end
  348.             end
  349.             _tSave.l = _tSave.l + 1
  350.         end
  351.         _tSave.w = _tSave.w + 1
  352.         _tSave.l = 1
  353.     end
  354.     loc.moveToPos(_tSave.startPos)
  355.     returnToChest()
  356.     fs.delete(_sFile)
  357.     return true
  358. end
  359.  
  360. -- GUI
  361. function printNormal(str, xpos, ypos)
  362.     term.setCursorPos(xpos, ypos)
  363.     term.write (str)
  364. end
  365.  
  366. function printReverse(str, xpos, ypos)
  367.     term.setCursorPos(xpos - (#str-1), ypos)
  368.     term.write (str)
  369. end
  370.  
  371. function printLeft(str, ypos)
  372.     term.setCursorPos(1, ypos)
  373.     term.write(str)
  374. end
  375.  
  376. function printCentered(str, ypos)
  377.     term.setCursorPos(screenw/2 - #str/2 + 1, ypos)
  378.     term.write(str)
  379. end
  380.  
  381. function printRight(str, ypos)
  382.     term.setCursorPos(screenw - #str, ypos)
  383.     term.write(str)
  384. end
  385.  
  386. function readLines( sLines, _sReplaceChar )
  387.     term.setCursorBlink( true )
  388.     local w, h = term.getSize()
  389.     local nPos = 0
  390.     local nSelect = 1
  391.     local vSelect = 1
  392.     local function redraw( _sCustomReplaceChar )
  393.         local nScroll = 0
  394.         if sLines[nSelect].sx + nPos >= w then
  395.             nScroll = (sLines[nSelect].sx + nPos) - w
  396.         end
  397.         term.setCursorPos( sLines[nSelect].sx, sLines[nSelect].sy )
  398.         local sReplace = _sCustomReplaceChar or _sReplaceChar
  399.         if sReplace then
  400.             term.write( string.rep(sReplace, string.len(sLines[nSelect].text) - nScroll) )
  401.         else
  402.             term.write( string.sub( sLines[nSelect].text, nScroll + 1 ) )
  403.         end
  404.         term.setCursorPos( sLines[nSelect].sx + nPos - nScroll, sLines[nSelect].sy )
  405.     end
  406.     for nVar, tVar in ipairs(sLines) do
  407.         nSelect = nVar
  408.         redraw()
  409.         if tVar.text ~= "" then
  410.             vSelect = nVar
  411.         end
  412.     end
  413.     nSelect = vSelect
  414.     nPos = #sLines[nSelect].text
  415.     redraw()
  416.     while true do
  417.         local sEvent, param = os.pullEvent()
  418.         if sEvent == "char" then
  419.             sLines[nSelect].text = string.sub( sLines[nSelect].text, 1, nPos ) .. param .. string.sub( sLines[nSelect].text, nPos + 1 )
  420.             nPos = nPos + 1
  421.             redraw()
  422.         elseif sEvent == "key" then
  423.             if param == keys.enter then
  424.                 -- Enter
  425.                 if nSelect < #sLines then
  426.                     nSelect = nSelect + 1
  427.                     if sLines[nSelect].text == "" then
  428.                         nPos = 0
  429.                     else
  430.                         nPos = #sLines[nSelect].text
  431.                     end
  432.                     redraw()
  433.                 else
  434.                     break
  435.                 end
  436.             elseif param == keys.left then
  437.                 -- Left
  438.                 if nPos > 0 then
  439.                     nPos = nPos - 1
  440.                     redraw()
  441.                 end
  442.             elseif param == keys.right then
  443.                 -- Right               
  444.                 if nPos < string.len(sLines[nSelect].text) then
  445.                     nPos = nPos + 1
  446.                     redraw()
  447.                 end
  448.             elseif param == keys.up or param == keys.down then
  449.                 -- Up or down
  450.                 if param == keys.up then
  451.                     -- Up
  452.                     if nSelect > 1 then
  453.                         nSelect = nSelect - 1
  454.                         if sLines[nSelect].text == "" then
  455.                             nPos = 0
  456.                         else
  457.                             nPos = #sLines[nSelect].text
  458.                         end
  459.                         redraw()
  460.                     end
  461.                 else
  462.                     -- Down
  463.                     if nSelect < #sLines then
  464.                         nSelect = nSelect + 1
  465.                         if sLines[nSelect].text == "" then
  466.                             nPos = 0
  467.                         else
  468.                             nPos = #sLines[nSelect].text
  469.                         end
  470.                         redraw()
  471.                     else
  472.                         break
  473.                     end
  474.                 end
  475.                 redraw()
  476.             elseif param == keys.backspace then
  477.                 -- Backspace
  478.                 if nPos > 0 then
  479.                     redraw(" ");
  480.                     sLines[nSelect].text = string.sub( sLines[nSelect].text, 1, nPos - 1 ) .. string.sub( sLines[nSelect].text, nPos + 1 )
  481.                     nPos = nPos - 1                
  482.                     redraw()
  483.                 end
  484.             elseif param == keys.home then
  485.                 -- Home
  486.                 nPos = 0
  487.                 redraw()       
  488.             elseif param == keys.delete then
  489.                 if nPos < string.len(sLines[nSelect].text) then
  490.                     redraw(" ");
  491.                     sLines[nSelect].text = string.sub( sLines[nSelect].text, 1, nPos ) .. string.sub( sLines[nSelect].text, nPos + 2 )             
  492.                     redraw()
  493.                 end
  494.             elseif param == keys["end"] then
  495.                 -- End
  496.                 nPos = string.len(sLines[nSelect].text)
  497.                 redraw()
  498.             end
  499.         end
  500.     end
  501.     term.setCursorBlink( false )
  502.     return sLine
  503. end
  504.  
  505. local function locate()
  506.     local x,y,z,facing
  507.     if 1==1 then
  508.         printCentered("Receiving coordinates from host...", 4)
  509.         x,y,z = gps.locate(3)
  510.         if x and y and z then
  511.             forward()
  512.             local tmpx,tmpy,tmpz = gps.locate(3)
  513.             if tmpx > x then
  514.                 facing = 3
  515.             elseif tmpx < x then
  516.                 facing = 1
  517.             elseif tmpz > z then
  518.                 facing = 0
  519.             elseif tmpz < z then
  520.                 facing = 2
  521.             end
  522.    printCentered("At:"..x.."/"..y.."/"..z.."/"..facing,3)
  523.    loc.turnLeft()
  524.    loc.turnLeft()
  525.    forward()
  526.    loc.turnLeft()
  527.    loc.turnLeft()
  528.             return x,y,z,facing
  529.         else
  530.             printCentered("Unable to get GPS cords.  Please,", 3)
  531.             printCentered("enter the turtles cords manually.", 4)
  532.         end
  533.     else
  534.         printCentered("No wireless modem found, please", 3)
  535.         printCentered("enter the cords manualy.", 4)
  536.     end
  537.     printLeft("     x: ", 6)
  538.     printLeft("     y: ", 7)
  539.     printLeft("     z: ", 8)
  540.     printLeft("  face: ", 9)
  541.     while true do
  542.         readLines( tCordsInput )
  543.         x,y,z,facing = tonumber(tCordsInput[1].text),tonumber(tCordsInput[2].text),tonumber(tCordsInput[3].text),tonumber(tCordsInput[4].text)
  544.         if x ~= nil and y ~= nil and y > 0 and y < 256 and z ~= nil and facing ~= nil and facing < 4 and facing >= 0 then
  545.             return x,y,z,facing
  546.         end
  547.     end
  548. end
  549.  
  550. local function drawHeader()
  551.     printCentered(program, 1)
  552.     printLeft(string.rep("-", screenw), 2)
  553.     printLeft("["..version.."]"..string.rep("-", screenw-#author-#version-7).."[by "..author.."]", screenh)
  554. end
  555.  
  556. local function drawMainMenu()
  557.     term.clear()
  558.     drawHeader()
  559.     printLeft(string.rep("-", screenw), screenh-2)
  560.     if loc.paused then
  561.         if select == 1 then
  562.             printCentered("[RESUME]  REFUEL   SAVE   EXIT ", screenh-1)
  563.         elseif select == 2 then
  564.             printCentered(" RESUME  [REFUEL]  SAVE   EXIT ", screenh-1)
  565.         elseif select == 3 then
  566.             printCentered(" RESUME   REFUEL  [SAVE]  EXIT ", screenh-1)
  567.         elseif select == 4 then
  568.             printCentered(" RESUME   REFUEL   SAVE  [EXIT]", screenh-1)
  569.         end
  570.     else
  571.         printCentered("[PAUSE]", screenh-1)
  572.     end
  573. end
  574.  
  575. local function drawGPSMenu()
  576.     if select == 2 then
  577.         printCentered("[DONE]", screenh-1)
  578.     elseif select == 1 then
  579.         term.clear()
  580.         drawHeader()
  581.         printLeft(string.rep("-", screenw), screenh-2)
  582.         printCentered(" DONE ", screenh-1)
  583.         loc.x,loc.y,loc.z,loc.facing = locate()
  584.         select = 2
  585.         drawGPSMenu()
  586.     end
  587. end
  588.  
  589. local function drawRefuelMenu()
  590.     local refuelList = {
  591.         "+-------------------------------+",
  592.         "|    Are you sure you want to   |",
  593.         "|       refuel the turtle.      |",
  594.         "|                               |",
  595.         "|        YES          NO        |",
  596.         "+-------------------------------+"
  597.     }
  598.     for i=1,#refuelList do
  599.         if i == #refuelList-1 then
  600.             if select == 1 then
  601.                 printCentered("|       [YES]         NO        |", 3 + i)
  602.             elseif select == 2 then
  603.                 printCentered("|        YES         [NO]       |", 3 + i)
  604.             end
  605.         else
  606.             printCentered(refuelList[i], 3 + i)
  607.         end
  608.     end
  609. end
  610.  
  611. local function drawRefuel()
  612.     local refuelList = {
  613.         "+-------------------------------+",
  614.         "|      Turtle fuel level is     |",
  615.         "|                               |",
  616.         "|                               |",
  617.         "|              OK               |",
  618.         "+-------------------------------+"
  619.     }
  620.     for i=1,#refuelList do
  621.         if i == #refuelList-1 then
  622.             printCentered("|             [OK]              |", 3 + i)
  623.         elseif i == #refuelList-3 then
  624.             printCentered(refuelList[i], 3 + i)
  625.             printCentered(fuel_level, 3 + i)
  626.         else
  627.             printCentered(refuelList[i], 3 + i)
  628.         end
  629.     end
  630. end
  631.  
  632. local function drawSaveMenu()
  633.     local saveList = {
  634.         "+-------------------------------+",
  635.         "|    Would you like to save?    |",
  636.         "|                               |",
  637.         "|                               |",
  638.         "|        YES          NO        |",
  639.         "+-------------------------------+"
  640.     }
  641.     for i=1,#saveList do
  642.         if i == #saveList-1 then
  643.             if select == 1 then
  644.                 printCentered("|       [YES]         NO        |", 3 + i)
  645.             elseif select == 2 then
  646.                 printCentered("|        YES         [NO]       |", 3 + i)
  647.             end
  648.         else
  649.             printCentered(saveList[i], 3 + i)
  650.         end
  651.     end
  652. end
  653.  
  654. local function drawExitMenu()
  655.     local exitList = {
  656.         "+-------------------------------+",
  657.         "|    Would you like to save     |",
  658.         "|        before exiting?        |",
  659.         "|                               |",
  660.         "|        YES          NO        |",
  661.         "+-------------------------------+"
  662.     }
  663.     for i=1,#exitList do
  664.         if i == #exitList-1 then
  665.             if select == 1 then
  666.                 printCentered("|       [YES]         NO        |", 3 + i)
  667.             elseif select == 2 then
  668.                 printCentered("|        YES         [NO]       |", 3 + i)
  669.             end
  670.         else
  671.             printCentered(exitList[i], 3 + i)
  672.         end
  673.     end
  674. end
  675.  
  676. local function drawResultMenu()
  677.     local resultList = {
  678.         "+-------------------------------+",
  679.         "|                               |",
  680.         "|    Ores mined this session!   |",
  681.         "|                               |",
  682.         "|             [OK]              |",
  683.         "+-------------------------------+"
  684.     }
  685.     for i=1,#resultList do
  686.         if i == #resultList-1 then
  687.             printCentered("|             [OK]              |", 3 + i)
  688.         elseif i == #resultList-4 then
  689.             printCentered(resultList[i], 2 + i)
  690.             printCentered(ores_mined, 2 + i)
  691.         else
  692.             printCentered(resultList[i], 3 + i)
  693.         end
  694.     end
  695. end
  696.  
  697. local function runOreFinder(_sSave)
  698.     _tSave = {}
  699.     _tSave = loadTable(_sSave)
  700.     local menustate = "gpsmenu"
  701.     local function rungui()
  702.         local mopt = {
  703.             ["main"] = {
  704.                 options = {"toggle_pause", "refuelmenu", "savemenu", "exitmenu"},
  705.                 draw = drawMainMenu
  706.             },
  707.             ["gpsmenu"] = {
  708.                 options = {"locate", "exit"},
  709.                 draw = drawGPSMenu
  710.             },
  711.             ["refuelmenu"] = {
  712.                 options = {"refuel", "main"},
  713.                 draw = drawRefuelMenu
  714.             },
  715.             ["refuel"] = {
  716.                 options = {"main"},
  717.                 draw = drawRefuel
  718.             },
  719.             ["savemenu"] = {
  720.                 options = {"save", "main"},
  721.                 draw = drawSaveMenu
  722.             },
  723.             ["exitmenu"] = {
  724.                 options = {"save_exit", "exit"},
  725.                 draw = drawExitMenu
  726.             },
  727.             ["resultmenu"] = {
  728.                 options = {"exit"},
  729.                 draw = drawResultMenu
  730.             }
  731.         }
  732.         tCordsInput = {
  733.             {text = "", sx = 9, sy = 6},
  734.             {text = "", sx = 9, sy = 7},
  735.             {text = "", sx = 9, sy = 8},
  736.             {text = "", sx = 9, sy = 9}
  737.         }
  738.         select = 1
  739.         fuel_level = tostring(turtle.getFuelLevel())
  740.         ores_mined = 0
  741.         while true do
  742.             mopt[menustate].draw()
  743.             local sEvent, param = os.pullEvent()
  744.             if sEvent == "key" then
  745.                 if param == keys.up and select > 1 then
  746.                     select = select-1
  747.                 elseif param == keys.down and select < #mopt[menustate].options then
  748.                     if loc.paused then
  749.                         select = select+1
  750.                     end
  751.                 elseif param == keys.left and select > 1 then
  752.                     select = select-1
  753.                 elseif param == keys.right and select < #mopt[menustate].options then
  754.                     if loc.paused then
  755.                         select = select+1
  756.                     end
  757.                 elseif param == keys.enter then
  758.                     if mopt[menustate].options[select] == "save" or mopt[menustate].options[select] == "save_exit" then
  759.                         saveTable(_tSave, _sSave)
  760.                         if mopt[menustate].options[select] == "save_exit" then
  761.                             break
  762.                         end
  763.                         menustate = "main"
  764.                     elseif mopt[menustate].options[select] == "exit" then
  765.                         break
  766.                     elseif mopt[menustate].options[select] == "toggle_pause" then
  767.                         if loc.paused then
  768.                             loc.paused = false
  769.                         else
  770.                             loc.paused = true
  771.                         end
  772.                     elseif mopt[menustate].options[select] == "refuel" then
  773.                         if turtle.getFuelLevel() ~= "unlimited" then
  774.                             for i=_tSave.ignore+1,16 do
  775.                                 if turtle.getItemCount(i) > 0 then
  776.                                     turtle.select(i)
  777.                                     turtle.refuel(turtle.getItemCount(i))
  778.                                 end
  779.                             end
  780.                             turtle.select(1)
  781.                         end
  782.                         fuel_level = tostring(turtle.getFuelLevel())
  783.                         menustate = mopt[menustate].options[select]
  784.                         select = 1
  785.                     else
  786.                         menustate = mopt[menustate].options[select]
  787.                         select = 1
  788.                     end
  789.                 end
  790.             end
  791.         end
  792.     end
  793.     rungui()
  794.     menustate = "main"
  795.     if not _tSave.startPos then
  796.         _tSave.startPos = {x = loc.x, y = loc.y, z = loc.z, facing = loc.facing}
  797.         _tSave.returnPos = {x = loc.x, y = loc.y, z = loc.z, facing = loc.facing}
  798.     end
  799.     saveTable(_tSave, _sFile)
  800.     parallel.waitForAny(oreFinder, rungui)
  801.     if not fs.exists(_sFile) then
  802.         menustate = "resultmenu"
  803.         rungui()
  804.     end
  805. end
  806.  
  807. local tArgs = { ... }
  808. if turtle.getFuelLevel() > 0 or turtle.getFuelLevel() == "unlimited" then
  809.     if #tArgs == 1 then
  810.         _sFile = tostring(tArgs[1])
  811.         if fs.exists(_sFile) then
  812.             runOreFinder(_sFile)
  813.             term.setCursorPos(1,1)
  814.             term.clear()
  815.         end
  816.     else
  817.         print("Usage: " .. shell.getRunningProgram() .. " <save file>")
  818.     end
  819. else
  820.     print("Error, there must be fuel in the turtle!")
  821. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement