JustMark

Area miner

Jul 20th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.27 KB | None | 0 0
  1. function tobool(value)
  2.     return value == 'true'
  3. end
  4.  
  5. math.sign = math.sign or function(x)
  6.     return x < 0 and -1 or x > 0 and 1 or 0
  7. end
  8.  
  9. table.shallowCopy = function(t)
  10.   local t2 = {}
  11.   for k,v in pairs(t) do
  12.     t2[k] = v
  13.   end
  14.   return t2
  15. end
  16.  
  17. function printMessage(line)
  18.     term.clear()
  19.     print(line)
  20. end
  21.  
  22. function printSleep(line)
  23.     printMessage(line)
  24.     sleep(1)
  25.     term.clear()
  26. end
  27.  
  28. local arg = { ... }
  29. local width = tonumber(arg[1])
  30. local height = tonumber(arg[2])
  31. local length = tonumber(arg[3])
  32. local down = tobool(arg[4])
  33.  
  34. local startPosition = {
  35.     x = 1;
  36.     y = down == false and 1 or -1;
  37.     z = 1;
  38. }
  39.  
  40. local currentPosition = table.shallowCopy(startPosition)
  41.  
  42. local currentDirection = 4
  43.  
  44. local persisted = false
  45.  
  46. local currentSlot = nil
  47.  
  48. local fuelWhitelist = {
  49.     'minecraft:coal',
  50.     'minecraft:charcoal'
  51. }
  52.  
  53. function readData(name)
  54.     if fs.exists('/data/'..name) then
  55.         local handle = fs.open('/data/'..name, 'r')
  56.         local stuff = handle.readAll()
  57.         handle.close()
  58.         return textutils.unserialize(stuff)
  59.     end
  60.  
  61.     return
  62. end
  63.  
  64. function writeData(name, data)
  65.     if not fs.exists('/data') then
  66.         fs.makeDir('/data')
  67.     end
  68.  
  69.     local handle = fs.open('/data/'..name, 'w')
  70.     handle.write(textutils.serialize(data))
  71.     handle.close()
  72. end
  73.  
  74. function loadData()
  75.     local loadedPosition = readData('position')
  76.     local loadedDirection = readData('direction')
  77.  
  78.     if (loadedPosition ~= nil) or (loadedDirection ~= nil) then
  79.         currentPosition = loadedPosition or currentPosition
  80.         currentDirection = loadedDirection or currentDirection
  81.  
  82.         persisted = true
  83.     end
  84. end
  85.  
  86. function updatePosition(deltaX, deltaY, deltaZ)
  87.     currentPosition.x = currentPosition.x + deltaX
  88.     currentPosition.y = currentPosition.y + deltaY
  89.     currentPosition.z = currentPosition.z + deltaZ
  90.  
  91.     writeData('position', currentPosition)
  92. end
  93.  
  94. function updateDirection(deltaD)
  95.     currentDirection = (currentDirection + deltaD - 1) % 4 + 1
  96.  
  97.     writeData('direction', currentDirection)
  98. end
  99.  
  100. function directionToXZ(direction)
  101.     return {
  102.         x = (direction- 2) * (direction% 2),
  103.         z = (direction- 3) * ((direction+ 1) % 2)
  104.     }
  105. end
  106.  
  107. function moveForward(steps, force)
  108.     steps = steps or 1
  109.  
  110.     for i = 1, steps do  
  111.         handleFuel()
  112.  
  113.         if force == true then
  114.             while turtle.detect() do
  115.                 turtle.dig()
  116.             end
  117.         end
  118.    
  119.         moved = turtle.forward()
  120.  
  121.         if moved == true then
  122.             deltaXZ = directionToXZ(currentDirection)
  123.  
  124.             updatePosition(deltaXZ.x, 0, deltaXZ.z)
  125.         else
  126.             printSleep('turtle is stuck, please remove the block in front')
  127.             i = i - 1
  128.         end
  129.     end
  130. end
  131.  
  132. function moveUp(steps, force)
  133.     steps = steps or 1
  134.  
  135.     for i = 1, steps do  
  136.         handleFuel()
  137.    
  138.         if force == true then
  139.           while turtle.detectUp() do
  140.                turtle.digUp()
  141.             end
  142.         end
  143.  
  144.         moved = turtle.up()
  145.  
  146.         if moved == true then
  147.             updatePosition(0, 1, 0)
  148.         else
  149.             printSleep('turtle is stuck, please remove the block above')
  150.             i = i - 1
  151.         end
  152.     end
  153. end
  154.  
  155. function moveDown(steps, force)
  156.     steps = steps or 1
  157.  
  158.     for i = 1, steps do  
  159.         handleFuel()
  160.  
  161.         if force == true then
  162.             while turtle.detectDown() do
  163.                 turtle.digDown()
  164.             end
  165.         end
  166.  
  167.         moved = turtle.down()
  168.  
  169.         if moved == true then
  170.             updatePosition(0, -1, 0)
  171.         else
  172.             printSleep('turtle is stuck, please remove the block below')
  173.             i = i - 1
  174.         end
  175.     end
  176. end
  177.  
  178. function moveBack(steps, force, maintaintDirection)
  179.     steps = steps or 1
  180.  
  181.     for i = 1, steps do  
  182.         handleFuel()
  183.  
  184.         moved = turtle.back()
  185.  
  186.         if moved == true then
  187.             deltaXZ = directionToXZ(currentDirection)
  188.  
  189.             updatePosition(-deltaXZ.x, 0, -deltaXZ.z)
  190.         else
  191.             if force == true then
  192.                 turnAround()
  193.                 moveForward(steps - i + 1, true)
  194.  
  195.                 if maintaintDirection == true then
  196.                     turnAround()
  197.                 end
  198.  
  199.                 return
  200.             else
  201.                 printSleep('turtle is stuck, please remove the block behind')
  202.                 i = i - 1
  203.             end
  204.         end
  205.     end
  206. end
  207.  
  208. function turnLeft()
  209.     turtle.turnLeft()
  210.     updateDirection(1)
  211. end
  212.  
  213. function turnRight()
  214.     turtle.turnRight()
  215.     updateDirection(-1)
  216. end
  217.  
  218. function turnAround()
  219.     turnRight()
  220.     turnRight()
  221. end
  222.  
  223. function turnTo(direction)
  224.     if direction == nil or direction == currentDirection then
  225.         return
  226.     end
  227.  
  228.     local deltaDir = currentDirection - direction
  229.    
  230.     local temp = (currentDirection % 4) - (direction % 4)
  231.     if math.abs(temp) < math.abs(deltaDir) then
  232.         deltaDir = temp
  233.     end
  234.  
  235.     if deltaDir < 0 then
  236.         for i = 1, -deltaDir do
  237.             turnLeft()
  238.         end
  239.     elseif deltaDir > 0 then
  240.         for i = 1, deltaDir do
  241.             turnRight()
  242.         end
  243.     end
  244. end
  245.  
  246. function turnToXAxis(preferedSign)
  247.     if currentDirection == 4 then
  248.         if preferedSign == -1 then
  249.             turnLeft()
  250.         else
  251.             turnRight()
  252.         end
  253.     elseif currentDirection == 2 then
  254.         if preferedSign == -1 then
  255.             turnRight()
  256.         else
  257.             turnLeft()
  258.         end
  259.     end
  260. end
  261.  
  262. function turnToZAxis(preferedSign)
  263.     if currentDirection == 1 then
  264.         if preferedSign == -1 then
  265.             turnLeft()
  266.         else
  267.             turnRight()
  268.         end
  269.     elseif currentDirection == 3 then
  270.         if preferedSign == -1 then
  271.             turnRight()
  272.         else
  273.             turnLeft()
  274.         end
  275.     end
  276. end
  277.  
  278. function moveOnXAxis(delta, force, maintaintDirection)
  279.     if delta == 0 or delta == nil then
  280.         return
  281.     end
  282.  
  283.     local startDir = currentDirection
  284.     local distance = math.abs(delta)
  285.     local sign = math.sign(delta)
  286.  
  287.     turnToXAxis(sign)
  288.    
  289.     local currentSign = (currentDirection == 3) and 1 or -1
  290.  
  291.     if sign * currentSign == 1 then
  292.         moveForward(distance, force)
  293.     else
  294.         moveBack(distance, force, false)
  295.     end
  296.  
  297.     if maintainDirection then
  298.         turnTo(startDir)
  299.     end
  300. end
  301.  
  302. function moveOnYAxis(delta, force)
  303.     if delta == 0 or delta == nil then
  304.         return
  305.     end
  306.  
  307.     local distance = math.abs(delta)
  308.     local sign = math.sign(delta)
  309.  
  310.     if sign == 1 then
  311.         moveUp(distance, force)
  312.     else
  313.         moveDown(distance, force)
  314.     end
  315. end
  316.  
  317. function moveOnZAxis(delta, force, maintaintDirection)
  318.     if delta == 0 or delta == nil then
  319.         return
  320.     end
  321.  
  322.     local startDir = currentDirection
  323.     local distance = math.abs(delta)
  324.     local sign = math.sign(delta)
  325.  
  326.     turnToZAxis(sign)
  327.    
  328.     local currentSign = (currentDirection == 4) and 1 or -1
  329.  
  330.     if sign * currentSign == 1 then
  331.         moveForward(distance, force)
  332.     else
  333.         moveBack(distance, force, false)
  334.     end
  335.  
  336.     if maintainDirection then
  337.         turnTo(startDir)
  338.     end
  339. end
  340.  
  341. function moveToX(x, force, maintaintDirection)
  342.     if x == nil then
  343.         return
  344.     end
  345.  
  346.     local delta = x - currentPosition.x
  347.     moveOnXAxis(delta, force, maintainDirection)
  348. end
  349.  
  350. function moveToY(y, force)
  351.     if y == nil then
  352.         return
  353.     end
  354.    
  355.     local delta = y - currentPosition.y
  356.     moveOnYAxis(delta, force, maintainDirection)
  357. end
  358.  
  359. function moveToZ(z, force, maintaintDirection)
  360.     if z == nil then
  361.         return
  362.     end
  363.  
  364.     local delta = z - currentPosition.z
  365.     moveOnZAxis(delta, force, maintainDirection)
  366. end
  367.  
  368. function moveTo(x, y, z, force, maintaintDirection)
  369.     local startDir = currentDirection
  370.  
  371.     if currentPosition.y < 0 then
  372.         moveToY(y, force)
  373.  
  374.         if math.abs(currentPosition.x) > math.abs(currentPosition.z) then
  375.             moveToX(x, force, false)
  376.             moveToZ(z, force, false)
  377.         else
  378.             moveToZ(z, force, false)
  379.             moveToX(x, force, false)
  380.         end
  381.     else
  382.         if math.abs(currentPosition.x) > math.abs(currentPosition.z) then
  383.             moveToX(x, force, false)
  384.             moveToZ(z, force, false)
  385.         else
  386.             moveToZ(z, force, false)
  387.             moveToX(x, force, false)
  388.         end
  389.  
  390.         moveToY(y, force)
  391.     end
  392.  
  393.     if maintainDirection == true then
  394.         turnTo(startDir)
  395.     end
  396. end
  397.  
  398. function digForward()
  399.     turtle.dig()
  400.     moveForward(1, true)
  401. end    
  402.  
  403. function digUp()
  404.     turtle.digUp()
  405.     moveUp(1, true)
  406. end
  407.  
  408. function digDown()
  409.     turtle.digDown()
  410.     moveDown(1, true)
  411. end
  412.  
  413. function refuel()
  414.     selectFuel()
  415.     return turtle.refuel(1)
  416. end
  417.  
  418. function handleFuel()
  419.     local fuel = turtle.getFuelLevel()
  420.    
  421.     if fuel == 0 then
  422.         while not refuel() do
  423.             printSleep('turtle is out of fuel, please add some fuel')
  424.         end
  425.     end
  426. end
  427.  
  428. function getItemName(slot)
  429.     if slot == nil then
  430.         return nil
  431.     end
  432.    
  433.     local detail = turtle.getItemDetail(slot)
  434.  
  435.     return detail and detail.name or nil
  436. end
  437.  
  438. function select(slot)
  439.     if slot == nil then
  440.         return
  441.     end
  442.  
  443.     turtle.select(slot)
  444.     currentSlot = slot
  445. end
  446.  
  447. function selectFuel()
  448.     local holdingFuel = isFuel(getItemName(currentSlot))
  449.  
  450.     if holdingFuel == false then
  451.         for i = 1, 16 do
  452.             if i ~= currentSlot then
  453.                 local foundFuel = isFuel(getItemName(i))
  454.  
  455.                 if foundFuel == true then
  456.                     select(i)
  457.                     return true
  458.                 end
  459.             end
  460.         end
  461.  
  462.         return false
  463.     end
  464.  
  465.     return true
  466. end
  467.  
  468. function isFuel(name)
  469.     for i = 1, #fuelWhitelist do
  470.         if (name == fuelWhitelist[i]) then
  471.             return true
  472.         end
  473.     end
  474.  
  475.     return false
  476. end
  477.  
  478. function main()
  479.     for y = 1, height do
  480.         for x = 1, width do
  481.             local curX = y % 2 == 1 and x or width+1 - x
  482.             for z = 1, length do
  483.                 local curZ = (curX + (y % 2) + 1) % 2 == 1 and z or length+1 - z
  484.  
  485.                 moveTo(curX, down == false and y or -y, curZ, true, false)
  486.             end
  487.         end
  488.     end
  489.  
  490.     moveTo(startPosition.x, startPosition.y, startPosition.z)
  491. end
  492.  
  493. main()
Add Comment
Please, Sign In to add comment