Advertisement
Guest User

mining.lua

a guest
Dec 13th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.61 KB | None | 0 0
  1. -- Advanced Ore Finder by TheAbyss
  2. -- V 1.0
  3.  
  4. -- Config
  5. local program = "Advanced Orefinder"
  6. local version = "v1.0"
  7. local author = "TheAbyss"
  8. local tSave = {}
  9.  
  10. local ores_mined, tCordsInput, select, screenw, screenh, fuel_level, _sFile
  11.  
  12. screenw,screenh = term.getSize()
  13.  
  14. -- Functions
  15.  
  16. local loc = {}
  17. loc = {
  18.     paused = false,
  19.    
  20.     x = 0,
  21.     y = 0,
  22.     z = 0,
  23.     facing = 0,
  24.    
  25.     facingToAxis = {
  26.         [0] = {axis = 'z', unit = 1},
  27.         [1] = {axis = 'x', unit = -1},
  28.         [2] = {axis = 'z', unit = -1},
  29.         [3] = {axis = 'x', unit = 1}
  30.     },
  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.     moveBack = function()
  66.         loc.pause()
  67.         if turtle.back() then
  68.             local axis, unit = loc.getAxisForFacing()
  69.             loc[axis] = loc[axis] - unit
  70.             return true
  71.         end
  72.         return false
  73.     end,
  74.  
  75.     face = function(faceto)
  76.         local dirdif = (faceto - loc.facing) % 4
  77.         if dirdif == 1 then
  78.             loc.turnRight()
  79.         elseif dirdif == 2 then
  80.             loc.turnRight()
  81.             loc.turnRight()
  82.         elseif dirdif == 3 then
  83.             loc.turnLeft()
  84.         end
  85.     end,
  86.  
  87.     movez = function(d)
  88.         if d < loc.z then loc.face(2) elseif d > loc.z then loc.face(0) end
  89.         return loc.moveForward()
  90.     end,
  91.  
  92.     movex = function(d)
  93.         if d < loc.x then loc.face(1) elseif d > loc.x then loc.face(3) end
  94.         return loc.moveForward()
  95.     end,
  96.  
  97.     movey = function(d)
  98.         if d < loc.y then
  99.             return loc.moveDown()
  100.         end
  101.         return loc.moveUp()
  102.     end,
  103.  
  104.     moveUp = function()
  105.         loc.pause()
  106.         if turtle.up() then
  107.             loc.y = loc.y + 1
  108.             return true
  109.         end
  110.         return false
  111.     end,
  112.  
  113.     moveDown = function()
  114.         loc.pause()
  115.         if turtle.down() then
  116.             loc.y = loc.y - 1
  117.             return true
  118.         end
  119.         return false
  120.     end,
  121.  
  122.     moveTo = function(x,y,z,facing)
  123.         if y > loc.y then
  124.             while loc.y < y do
  125.                 while not loc.moveUp() do
  126.                     if turtle.detectUp() then
  127.                         turtle.select(1)
  128.                         turtle.digUp()
  129.                     else
  130.                         turtle.attackUp()
  131.                     end
  132.                 end
  133.             end
  134.         end
  135.         while x ~= loc.x do
  136.             while not loc.movex(x) 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.         while z ~= loc.z do
  146.             while not loc.movez(z) do
  147.                 if turtle.detect() then
  148.                     turtle.select(1)
  149.                     turtle.dig()
  150.                 else
  151.                     turtle.attack()
  152.                 end
  153.             end
  154.         end
  155.         if y < loc.y then
  156.             while loc.y > y do
  157.                 while not loc.moveDown() do
  158.                     if turtle.detectDown() then
  159.                         turtle.select(1)
  160.                         turtle.digDown()
  161.                     else
  162.                         turtle.attackDown()
  163.                     end
  164.                 end
  165.             end
  166.         end
  167.         loc.face(facing)
  168.         return true
  169.     end,
  170.  
  171.     moveToPos = function(pos)
  172.         return loc.moveTo(pos.x,pos.y,pos.z,pos.facing)
  173.     end,
  174.  
  175.     distanceToPos = function(pos)
  176.         return loc.fistanceTo(pos.x, pos.y, pos.z)
  177.     end
  178. }
  179.  
  180. -- Orefinder
  181. function saveTable(table,name)
  182.     local file = fs.open(name,"w")
  183.     file.write(textutils.serialize(table))
  184.     file.close()
  185. end
  186.  
  187. function loadTable(name)
  188.     local file = fs.open(name,"r")
  189.     local data = file.readAll()
  190.     file.close()
  191.     return textutils.unserialize(data)
  192. end
  193.  
  194. function moveDown(h)
  195.     while not loc.movey(h) do
  196.         if turtle.detectDown() then
  197.             turtle.digDown()
  198.             if turtle.detectDown() then
  199.                 return false
  200.             end
  201.         else
  202.             turtle.attackDown()
  203.         end
  204.     end
  205.     return true
  206. end
  207.  
  208. function compareForward(a, b)
  209.     loc.pause()
  210.     for i=a,b do
  211.         turtle.select(i)
  212.         if turtle.compare() then
  213.             return false
  214.         end
  215.     end
  216.     return true
  217. end
  218.  
  219. function findDown(d)
  220.     if not d then d = loc.y end
  221.     local h = loc.y - d
  222.     while loc.y ~= h do
  223.         if not moveDown(h) then
  224.             break
  225.         end
  226.         for j=1,4 do
  227.             if compareForward(1, t.Save.ignore) then
  228.                 if not space() then
  229.                     returnToChest()
  230.                 end
  231.                 turtle.select(1)
  232.                 turtle.dig()
  233.                 ores_mined = ores_mined + 1
  234.             end
  235.             loc.turnRight()
  236.         end
  237.     end
  238. end
  239.  
  240. function refuel(ammount)
  241.     if turtle.getFuelLevel()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement