Advertisement
LaniusFNV

CC Miner 2

Feb 10th, 2022
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.71 KB | None | 0 0
  1. local status = {
  2.   "Waiting for Input",
  3.   "Mining",
  4.   "Not enough fuel",
  5.   --"Returning Home", -- Not yet implemented
  6.   --"Storing Items", -- Not yet implemented
  7. }
  8.  
  9. local function tableContainsKey(t, key)
  10.   for k, v in pairs(t) do
  11.     if k == key then return true end
  12.   end
  13.  
  14.   return false
  15. end
  16.  
  17. local function right(orientation)
  18.   turtle.turnRight()
  19.  
  20.   return orientation + 1 % 4
  21. end
  22.  
  23. local function turnTowardsTargetOrientation(orientation, target_orientation)
  24.   repeat orientation = right(orientation) until orientation == target_orientation
  25.  
  26.   return target_orientation
  27. end
  28.  
  29. local function printProgramInfo()
  30.   term.clear()
  31.   term.setCursorPos(1, 1)
  32.   term.write("Miner MkI")
  33. end
  34.  
  35. local function printStatus(status, position, current_fuel, needed_fuel, lower, upper, width, length)
  36.   term.setCursorPos(1, 2)
  37.   term.clearLine()
  38.   term.write(status)
  39. end
  40.  
  41. local function endPosition(starting_position, lower, upper, width, length)
  42.   local sx, sy, sz = starting_position[1], starting_position[2], starting_position[3]
  43.  
  44.   local z = sz
  45.   if width % 2 ~= 0 then
  46.     z = z + length
  47.   end
  48.  
  49.   return sx, upper, z
  50. end
  51.  
  52. local function neededFuel(lower, upper, width, length)
  53.   return width * length * (upper - lower) -- the total area
  54.     + math.max(length, width) -- plus a little extra to maneuver around obstacles
  55. end
  56.  
  57. local function endPositionForLevel(starting_position, width, length)
  58.   return endPosition(starting_position, nil, 0, width, length)
  59. end
  60.  
  61. local function endPositionForLevel(starting_position, length)
  62.   return starting_position[3] == length
  63. end
  64.  
  65. local function offset(orientation)
  66.   if orientation == 0 then
  67.     return 1
  68.   elseif orientation == 2 then
  69.     return -1
  70.   end
  71. end
  72.  
  73. local function orientationVec(orientation)
  74.   if orientation == 0 then
  75.     return vector.new(1, 0, 0)
  76.   elseif orientation == 1 then
  77.     return vector.new(0, 0, 1)
  78.   elseif orientation == 2 then
  79.     return vector.new(-1, 0, 0)
  80.   elseif orientation == 3 then
  81.     return vector.new(0, 0, -1)
  82.   else
  83.     error("[ERROR] (orientationVec) orientation is invalid (not 0..4)")
  84.   end
  85. end
  86.  
  87. local function forward(position, orientation)
  88.   turtle.forward()
  89.  
  90.   local pos_vec = vector.new(position[1], position[2], position[3])
  91.   local res = pos_vec + orientationVec(orientation)
  92.  
  93.   return {res.x, res.y, res.z}
  94. end
  95.  
  96. local function returnToStartPositionForLevel(position, orientation, width, length)
  97.   orientation = turnTowardsTargetOrientation(orientation, 3)
  98.   repeat position = forward(position, orientation) until position[3] == 0
  99.  
  100.   orientation = turnTowardsTargetOrientation(orientation, 2)
  101.   repeat position = forward(position, orientation) until position[1] == 0
  102.  
  103.   orientation = turnTowardsTargetOrientation(orientation, 0)
  104.  
  105.   return position, orientation
  106. end
  107.  
  108. local function up(position)
  109.   turtle.digUp()
  110.   turtle.up()
  111.  
  112.   return {position[1], position[2] + 1, position[3]}
  113. end
  114.  
  115. local function shouldMineBlock(details)
  116.   return not tableContainsKey(details.tags, "forge:ores")
  117. end
  118.  
  119. local function mineBlock(position, orientation)
  120.   local is_block, details = turtle.inspect()
  121.  
  122.   if is_block then
  123.     if shouldMineBlock(details) then
  124.       turtle.dig()
  125.     else
  126.       moveAroundObstacle(position, orientation)
  127.     end
  128.   end
  129. end
  130.  
  131. -- orientation works like this
  132. -- 0 +l
  133. -- 1 +w
  134. -- 2 -l
  135. -- 3 -w
  136. local function move(position, orientation, lower, upper, width, length)
  137.   print(position[1] .." ".. position[3])
  138.   if position[2] < upper then
  139.     if position[3] < width then
  140.       if position[1] < length then
  141.         position = forward(position, orientation)
  142.       else
  143.         orientation = turnTowardsTargetOrientation(orientation, 1)
  144.         mineBlock(position, orientation)
  145.         position = forward(position, orientation)
  146.         orientation = turnTowardsTargetOrientation(orientation, 2)
  147.       end
  148.     else
  149.       position, orientation = returnToStartPositionForLevel(position, orientation, width, length)
  150.       position = up(position)
  151.     end
  152.   end
  153.   print(position[1] .." ".. position[3])
  154.  
  155.   return position
  156. end
  157.  
  158. local function moveAroundObstacle(position, orientation)
  159.   error("[ERROR] (moveAroundObstacle) not yet implemented")
  160. end
  161.  
  162. -- Mines an area, leaving ores or gems as is.
  163. -- That means that (for example) cobblestone and gravel will be mined,
  164. -- but not iron ore or redstone.
  165. --
  166. -- The area looks like this:
  167. -- T (looking towards length)
  168. -- ┌-------> width
  169. -- |
  170. -- |
  171. -- |
  172. -- v
  173. -- length
  174. --
  175. -- and lower and upper represent the y-levels the turtle should mine between.
  176. --
  177. -- Position refers to where the turtle is, but may be omitted in which case position will be set
  178. -- assuming the turtle is at y-level lower.
  179. -- Length will then be how many steps the turtle will go forward for.
  180. local function mine(status, position, orientation, lower, upper, width, length)
  181.   if lower == nil then
  182.     error("[ERROR] (mine) lower must not be nil here!")
  183.   end
  184.  
  185.   local current_fuel = turtle.getFuelLevel()
  186.   local needed_fuel = neededFuel(lower, upper, width, length)
  187.  
  188.   if position == nil then
  189.     position = {0, lower, 0}
  190.   end
  191.  
  192.   if orientation == nil then
  193.     orientation = 0
  194.   end
  195.  
  196.   while position ~= endPosition(position, lower, upper, width, length) do
  197.     printStatus(status[2], position, current_fuel, needed_fuel, lower, upper, width, length)
  198.  
  199.     mineBlock(position, orientation)
  200.     position = move(position, orientation, lower, upper, width, length)
  201.   end
  202. end
  203.  
  204. local function askForHeightRange()
  205.   term.clearLine()
  206.  
  207.   term.setCursorPos(1, 5)
  208.   term.write("Please enter the lower y-level: ")
  209.  
  210.   local lower = nil
  211.   repeat lower = tonumber(io.read()) until lower ~= nil
  212.  
  213.   term.clearLine()
  214.  
  215.   term.setCursorPos(1, 5)
  216.   term.write("Please enter the upper y-level: ")
  217.  
  218.   local upper = nil
  219.   repeat upper = tonumber(io.read()) until upper ~= nil
  220.  
  221.   term.clearLine()
  222.  
  223.   return lower, upper
  224. end
  225.  
  226. local function askForArea()
  227.   term.clearLine()
  228.  
  229.   term.setCursorPos(1, 5)
  230.   term.write("Please enter the length: ")
  231.  
  232.   local length = nil
  233.   repeat length = tonumber(io.read()) until length ~= nil
  234.  
  235.   term.clearLine()
  236.  
  237.   term.setCursorPos(1, 5)
  238.   term.write("Please enter the width: ")
  239.  
  240.   local width = nil
  241.   repeat width = tonumber(io.read()) until width ~= nil
  242.  
  243.   term.clearLine()
  244.  
  245.   return width, length
  246. end
  247.  
  248.  
  249. local function main()
  250.   local config_file = nil -- this should be set to a file path if you would like not to have input height or area all the time
  251.  
  252.   local status = status[1]
  253.   printProgramInfo()
  254.   printStatus(status, nil, nil, nil, nil, nil)
  255.   local lower, upper = askForHeightRange(config_file)
  256.   local width, length = askForArea(config_file)
  257.  
  258.   mine(status, nil, nil, lower, upper, width, length)
  259. end
  260.  
  261. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement