Advertisement
vvood_vvolf

Ore Finder

Jan 14th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.45 KB | None | 0 0
  1. -- Advanced Ore Finder by Henness
  2. -- Version 2.0 12/16/2012
  3.  
  4. -- Config
  5. local version = "v2.0"
  6. local author = "[by Henness]"
  7. screenw,screenh = term.getSize()
  8.  
  9. -- Functions
  10. function version()
  11.     return Version
  12. end
  13.  
  14. function printCentered(str, ypos)
  15.     term.setCursorPos(screenw/2 - #str/2, ypos)
  16.     term.write(str)
  17. end
  18.  
  19. function printLeft(str, ypos)
  20.     term.setCursorPos(1, ypos)
  21.     term.write(str)
  22. end
  23.  
  24. function drawHeader()
  25.     printCentered("Advanced OreFinder", 1)
  26.     printCentered(string.rep("-", screenw), 2)
  27.     printCentered(string.rep("-", screenw-#author)..author, screenh)
  28. end
  29.  
  30. function skip()
  31.     while true do
  32.         local id, key = os.pullEvent("key")
  33.         if key == 57 then
  34.             break
  35.         end
  36.     end
  37. end
  38.  
  39. function saveTable(table,name)
  40.     local file = fs.open(name,"w")
  41.     file.write(textutils.serialize(table))
  42.     file.close()
  43. end
  44.  
  45. function loadTable(name)
  46.     local file = fs.open(name,"r")
  47.     local data = file.readAll()
  48.     file.close()
  49.     return textutils.unserialize(data)
  50. end
  51.  
  52. function save()
  53.     ofsave["currentPos"]["x"] = x
  54.     ofsave["currentPos"]["y"] = y
  55.     ofsave["currentPos"]["z"] = z
  56.     ofsave["currentPos"]["face"] = face
  57.     if fs.exists("ofsave") then
  58.         fs.delete("ofsave")
  59.     end
  60.     saveTable(ofsave, "ofsave")
  61. end
  62.  
  63. function turnLeft(a)
  64.     if not a then
  65.         a = 1
  66.     end
  67.     for i=1,math.abs(a),1 do
  68.         turtle.turnLeft()
  69.         if face==0 then
  70.             face=3
  71.         else
  72.             face=face-1
  73.         end
  74.     end
  75.     return true
  76. end
  77.  
  78. function turnRight(a)
  79.     if not a then
  80.         a = 1
  81.     end
  82.     for i=1,math.abs(a),1 do
  83.         turtle.turnRight()
  84.         if face==3 then
  85.             face=0
  86.         else
  87.             face=face+1
  88.         end
  89.     end
  90.     return true
  91. end
  92.  
  93. function setFace(a)
  94.     local turn = face-a
  95.     local done = false
  96.     if face == a then
  97.         done = true
  98.     elseif turn == -1 or turn == 3 then
  99.         turnRight()
  100.         done = true
  101.     elseif turn == 1 or turn == -3 then
  102.         turnLeft()
  103.         done = true
  104.     elseif math.abs(turn) == 2 then
  105.         turnRight(2)
  106.         done = true
  107.     end
  108.     return done
  109. end
  110.  
  111. function up(n) -- Moves the turtle up "n" distance
  112.     local moved
  113.     if not n then
  114.         n=1
  115.     end
  116.     if y<255 then
  117.         for k=1,n,1 do
  118.             moved = turtle.up()
  119.             if moved then
  120.                 y=y+1
  121.                 save()
  122.             else
  123.                 break
  124.             end
  125.         end
  126.     end
  127.     return moved
  128. end
  129.  
  130. function down(n) -- Moves the turtle down "n" distance
  131.     local moved
  132.     if not n then
  133.         n=1
  134.     end
  135.     if y>1 then
  136.         for k=1,n,1 do
  137.             moved = turtle.down()
  138.             if moved then
  139.                 y=y-1
  140.                 save()
  141.             else
  142.                 break
  143.             end
  144.         end
  145.     end
  146.     return moved
  147. end
  148.  
  149. function forward(n) -- Moves the turtle forward "n" distance
  150.     local moved
  151.     if not n then
  152.         n=1
  153.     end
  154.     if face==0 then
  155.         for k=1,n,1 do
  156.             moved=turtle.forward()
  157.             if moved then
  158.                 z=z+1
  159.                 save()
  160.             else
  161.                 break
  162.             end
  163.         end
  164.     elseif face==1 then
  165.         for k=1,n,1 do
  166.             moved=turtle.forward()
  167.             if moved then
  168.                 x=x-1
  169.                 save()
  170.             else
  171.                 break
  172.             end
  173.         end
  174.     elseif face==2 then
  175.         for k=1,n,1 do
  176.             moved=turtle.forward()
  177.             if moved then
  178.                 z=z-1
  179.                 save()
  180.             else
  181.                 break
  182.             end
  183.         end
  184.     elseif face==3 then
  185.         for k=1,n,1 do
  186.             moved=turtle.forward()
  187.             if moved then
  188.                 x=x+1
  189.                 save()
  190.             else
  191.                 break
  192.             end
  193.         end
  194.     end
  195.     return moved
  196. end
  197.  
  198. function dig()
  199.     return turtle.dig()
  200. end
  201.  
  202. function digDown()
  203.     return turtle.digDown()
  204. end
  205.  
  206. function digUp()
  207.     return turtle.digUp()
  208. end
  209.  
  210. function compareForward(startSlot, endSlot)
  211.     if not startSlot then
  212.         startSlot = 3
  213.     end
  214.     if not endSlot then
  215.         endSlot = ofsave["ignore"] + 2
  216.     end
  217.     Ore = true
  218.     for i=startSlot,endSlot do
  219.         turtle.select(i)
  220.         if turtle.compare() then
  221.             Ore = false
  222.             break
  223.         end            
  224.     end
  225.     if Ore then
  226.         dig()
  227.     end
  228. end
  229.  
  230. function findDown(n) -- Finds all ores bellow the turtle for "n" distance
  231.     if not n then
  232.         n=y-1
  233.     end
  234.     for i=1,n,1 do
  235.         local moved = down()
  236.         if not moved and turtle.detectDown() then
  237.             digDown()
  238.             if not down() then
  239.                 break
  240.             end
  241.         elseif not moved and not turtle.detectDown() then
  242.             repeat turtle.attackDown() until down()
  243.         end
  244.         for j=1,4 do
  245.             compareForward()
  246.             turnRight()
  247.         end
  248.     end
  249. end
  250.  
  251. function returnLast()
  252.     local returnPosx = ofsave["returnPos"]["x"]
  253.     local returnPosy = ofsave["returnPos"]["y"]
  254.     local returnPosz = ofsave["returnPos"]["z"]
  255.     local returnPosface = ofsave["returnPos"]["face"]
  256.     moveToPos(returnPosx, returnPosy, returnPosz, returnPosface)
  257. end
  258.  
  259. function returnStart()
  260.     local startPosx = ofsave["startPos"]["x"]
  261.     local startPosy = ofsave["startPos"]["y"]
  262.     local startPosz = ofsave["startPos"]["z"]
  263.     moveToPos(startPosx, startPosy, startPosz, faceB)
  264. end
  265.  
  266. function moveToPos(A, B, C, Face)
  267.     a=A-x
  268.     b=B-y
  269.     c=C-z
  270.     if b>0 then
  271.         for i=1,math.abs(b) do
  272.             local moved = up()
  273.             if not moved and turtle.detectUp() then
  274.                 repeat digUp() until up()
  275.                 local moved = true
  276.             elseif not moved and not turtle.detectUp() then
  277.                 repeat turtle.attackUp() until up()
  278.             end
  279.         end
  280.     end
  281.     if a<0 then
  282.         setFace(1)
  283.     elseif a>0 then
  284.         setFace(3)
  285.     end
  286.     if a ~= 0 then
  287.         for i=1,math.abs(a) do
  288.             local moved = forward()
  289.             if not moved and turtle.detect() then
  290.                 repeat dig() until forward()
  291.                 local moved = true
  292.             elseif not moved and not turtle.detect() then
  293.                 repeat turtle.attack() until forward()
  294.             end
  295.         end
  296.     end
  297.     if c<0 then
  298.         setFace(2)
  299.     elseif c>0 then
  300.         setFace(0)
  301.     end
  302.     if c ~= 0 then
  303.         for i=1,math.abs(c) do
  304.             local moved = forward()
  305.             if not moved and turtle.detect() then
  306.                 repeat dig() until forward()
  307.                 local moved = true
  308.             elseif not moved and not turtle.detect() then
  309.                 repeat turtle.attack() until forward()
  310.             end
  311.         end
  312.     end
  313.     if b<0 then
  314.         for i=1,math.abs(b) do
  315.             local moved = down()
  316.             if not moved and turtle.detectDown() then
  317.                 repeat digDown() until down()
  318.                 local moved = true
  319.             elseif not moved and not turtle.detectDown() then
  320.                 repeat turtle.attackDown() until down()
  321.             end
  322.         end
  323.     end
  324.     setFace(Face)
  325. end
  326.  
  327. function checkReturn()
  328.     if turtle.getFuelLevel() < 2 * y + ofsave["length"] + ofsave["width"] then
  329.         returnStart()
  330.         for i = 3, ofsave["ignore"]+2 do
  331.             turtle.select(i)
  332.             turtle.drop(turtle.getItemCount(i)-1)
  333.         end
  334.         for i = ofsave["ignore"]+3, 16 do
  335.             turtle.select(i)
  336.             turtle.drop()
  337.         end
  338.         turtle.select(1)
  339.         repeat
  340.             if turtle.getItemCount(1) > 1 then
  341.                 turtle.refuel(1)
  342.             end
  343.             sleep(0.1)
  344.         until turtle.getFuelLevel() > 2 * y + ofsave["length"] + ofsave["width"]
  345.         returnLast()
  346.     elseif turtle.getItemCount(16) > 0 then
  347.         returnStart()
  348.         for i = 3, ofsave["ignore"]+2 do
  349.             turtle.select(i)
  350.             turtle.drop(turtle.getItemCount(i)-1)
  351.         end
  352.         for i = ofsave["ignore"]+3, 16 do
  353.             turtle.select(i)
  354.             turtle.drop()
  355.         end
  356.         turtle.select(1)
  357.         returnLast()
  358.     end
  359. end
  360.  
  361. function oreFinder()
  362.     while ofsave["w"] <= ofsave["width"] do
  363.         while ofsave["l"] <= ofsave["length"] do
  364.             checkReturn()
  365.             if math.fmod((ofsave["returnPos"]["x"]+4)-2*(ofsave["returnPos"]["z"]-1), 5) == 0 then
  366.                 findDown()
  367.                 returnLast()
  368.                 turtle.select(2)
  369.                 turtle.placeDown()
  370.             end
  371.             if ofsave["l"] == ofsave["length"] then
  372.                 if face == faceF then
  373.                     turnRight()
  374.                     ofsave["right"] = true
  375.                 elseif face == faceB then
  376.                     turnLeft()
  377.                     ofsave["left"] = true
  378.                 end
  379.                 ofsave["returnPos"]["x"] = x
  380.                 ofsave["returnPos"]["z"] = z
  381.                 ofsave["returnPos"]["face"] = face
  382.                 if fs.exists("ofsave") then
  383.                     fs.delete("ofsave")
  384.                 end
  385.                 saveTable(ofsave, "ofsave")
  386.             end
  387.             if not ofsave["forward"] then
  388.                 local moved = forward()
  389.                 if not moved and turtle.detect() then
  390.                     repeat dig() until forward()
  391.                     local moved = true
  392.                 elseif not moved and not turtle.detect() then
  393.                     repeat turtle.attack() until forward()
  394.                 end
  395.                 digUp()
  396.                 ofsave["returnPos"]["x"] = x
  397.                 ofsave["returnPos"]["z"] = z
  398.                 ofsave["returnPos"]["face"] = face
  399.                 ofsave["forward"] = true
  400.                 if fs.exists("ofsave") then
  401.                     fs.delete("ofsave")
  402.                 end
  403.                 saveTable(ofsave, "ofsave")
  404.             end
  405.             if ofsave["right"] then
  406.                 turnRight()
  407.                 ofsave["right"] = false
  408.             elseif ofsave["left"] then
  409.                 turnLeft()
  410.                 ofsave["left"] = false
  411.             end
  412.             local l = ofsave["l"] + 1
  413.             ofsave["l"] = l
  414.             ofsave["returnPos"]["x"] = x
  415.             ofsave["returnPos"]["z"] = z
  416.             ofsave["returnPos"]["face"] = face
  417.             ofsave["forward"] = false
  418.             if fs.exists("ofsave") then
  419.                 fs.delete("ofsave")
  420.             end
  421.             saveTable(ofsave, "ofsave")
  422.         end
  423.         local w = ofsave["w"] + 1
  424.         ofsave["w"] = w
  425.         ofsave["l"] = 1
  426.         if fs.exists("ofsave") then
  427.             fs.delete("ofsave")
  428.         end
  429.         saveTable(ofsave, "ofsave")
  430.     end
  431.     returnStart()
  432.     setFace(faceF)
  433.     fs.delete("ofsave")
  434. end
  435.  
  436. function runOreFinder(width, length, ignore)
  437.     printCentered("Receiving coordinates from host...", 4)
  438.     rednet.open("right")
  439.     x,y,z = gps.locate(5)      
  440.     if not x and not y and not z then
  441.         while true do
  442.             while true do
  443.                 while true do
  444.                     while true do
  445.                         term.clear()
  446.                         drawHeader()
  447.                         printCentered("Unable to get GPS cords.  Please,", 4)
  448.                         printCentered("enter the turtles cords manually.", 5)
  449.                         printLeft("     x: ", 7)
  450.                         printLeft("     y: ", 8)
  451.                         printLeft("     z: ", 9)
  452.                         printLeft("  face: ", 11)
  453.                         term.setCursorPos(9, 7)
  454.                         x = tonumber(read())
  455.                         if x ~= nil then
  456.                             break
  457.                         end
  458.                     end
  459.                     term.setCursorPos(9, 8)
  460.                     y = tonumber(read())
  461.                     if y ~= nil and y > 0 and y < 256 then
  462.                         break
  463.                     end
  464.                 end
  465.                 term.setCursorPos(9, 9)
  466.                 z = tonumber(read())
  467.                 if z ~= nil then
  468.                     break
  469.                 end
  470.             end
  471.             term.setCursorPos(9, 11)
  472.             face = tonumber(read())
  473.             if face ~= nil and face < 4 and face >= 0 then
  474.                 break
  475.             end
  476.         end
  477.     else
  478.         local moved = turtle.forward()
  479.         if not moved and turtle.detect() then
  480.             repeat dig() until turtle.forward()
  481.             local moved = true
  482.         elseif not moved and not turtle.detect() then
  483.             repeat turtle.attack() until turtle.forward()
  484.         end
  485.         local tmpx,tmpy,tmpz = gps.locate(5)
  486.         turtle.back()
  487.         if tmpx > x then
  488.             face = 3
  489.         elseif tmpx < x then
  490.             face = 1
  491.         elseif tmpz > z then
  492.             face = 0
  493.         elseif tmpz < z then
  494.             face = 2
  495.         end
  496.     end
  497.     if width and length and ignore then
  498.         term.clear()
  499.         drawHeader()
  500.         printCentered("Starting a new excavation,", 4)
  501.         printCentered("press space at any time to pause.", 5)
  502.         ofsave = {
  503.             ["startPos"] = {
  504.                 ["x"] = x, ["y"] = y, ["z"] = z, ["face"] = face
  505.             },
  506.             ["currentPos"] = {
  507.                 ["x"] = x, ["y"] = y, ["z"] = z, ["face"] = face
  508.             },
  509.             ["returnPos"] = {
  510.                 ["x"] = x, ["y"] = y, ["z"] = z, ["face"] = face
  511.             },
  512.             ["ignore"] = ignore,
  513.             ["forward"] = false,
  514.             ["left"] = false,
  515.             ["right"] = false,
  516.             ["length"] = length,
  517.             ["width"] = width,
  518.             ["l"] = 1,
  519.             ["w"] = 1
  520.         }
  521.         if fs.exists("ofsave") then
  522.             fs.delete("ofsave")
  523.         end
  524.         saveTable(ofsave, "ofsave")
  525.     else
  526.         term.clear()
  527.         drawHeader()
  528.         printCentered("Continuing from previous excavation,", 4)
  529.         printCentered("press space at any time to pause.", 5)
  530.         ofsave = loadTable("ofsave")
  531.         moveToPos(ofsave["currentPos"]["x"],ofsave["currentPos"]["y"],ofsave["currentPos"]["z"],ofsave["returnPos"]["face"])
  532.     end
  533.     faceF = ofsave["startPos"]["face"]
  534.     if faceF<=1 then
  535.         faceB = faceF+2
  536.     else
  537.         faceB = faceF-2
  538.     end
  539.     parallel.waitForAny(oreFinder, skip)
  540. end
  541.  
  542. local tArgs = { ... }
  543. term.clear()
  544. drawHeader()
  545. term.setCursorPos(1,1)
  546. if #tArgs == 3 then
  547.     width = tonumber(tArgs[1])
  548.     length = tonumber(tArgs[2])
  549.     ignore = tonumber(tArgs[3])
  550.     runOreFinder(width, length, ignore)
  551. elseif fs.exists("ofsave") and #tArgs == 0 then
  552.     runOreFinder()
  553. else
  554.     print("Usage: " .. shell.getRunningProgram() .. " <width> <length> <ignore>")
  555. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement