Advertisement
fatboychummy

DigDug.lua

Feb 14th, 2020 (edited)
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.29 KB | None | 0 0
  1. local tArgs = table.pack(...)
  2. local totalMoves = 100000
  3.  
  4. -- print the usage, along with a small information thing if the inputs are wrong
  5. local function printUsage(i)
  6.   print("Usage:")
  7.   print("dig <right:number> <down:number> <forward:number>")
  8.   if i then
  9.     local char = i == 1 and 'right' or i == 2 and 'down' or i == 3 and 'forward'
  10.     print(string.format("Input %s is not a number", char))
  11.   end
  12. end
  13.  
  14. -- If there wasn't three inputs (x y z), print the usage
  15. if tArgs.n ~= 3 then
  16.   printUsage()
  17.   return
  18. end
  19.  
  20. -- Grab the inputs, convert to numbers
  21. local x, y, z = table.unpack(tArgs, 1, n)
  22. x, y, z = tonumber(x), tonumber(y), tonumber(z)
  23.  
  24. -- if one is not a number, print error
  25. if not x or not y or not z then
  26.   printUsage(not x and 1 or not y and 2 or not z and 3)
  27.   return
  28. end
  29.  
  30. local function ensure(func, ...)
  31.   local args = table.pack(...)
  32.   while not func() do
  33.     for i = 1, args.n do
  34.       args[i]()
  35.     end
  36.   end
  37. end
  38.  
  39. local mx, my = term.getSize()
  40.  
  41. local line = string.rep(' ', mx)
  42.  
  43. term.setBackgroundColor(colors.black)
  44. term.setTextColor(colors.white)
  45. term.clear()
  46.  
  47. term.setBackgroundColor(colors.gray)
  48. term.setCursorPos(1, 1)
  49. io.write(line)
  50. term.setCursorPos(1, 1)
  51. print("Information")
  52. local infoWindow = window.create(term.current(), 1, 2, mx, 6)
  53.  
  54. term.setCursorPos(1, 8)
  55. io.write(line)
  56. term.setCursorPos(1, 8)
  57. io.write("Positioning")
  58. local posWindow = window.create(term.current(), 1, 9, mx, 1)
  59.  
  60. term.setCursorPos(1, 10)
  61. io.write(line)
  62. term.setCursorPos(1, 10)
  63. io.write("Fuel")
  64. local fuelWindow = window.create(term.current(), 1, 11, mx, 1)
  65.  
  66. term.setCursorPos(1, 12)
  67. io.write(line)
  68. term.setCursorPos(1, 12)
  69. io.write("Completion")
  70. local compWindow = window.create(term.current(), 1, 13, mx, 1)
  71.  
  72. local origTerm = term.redirect(infoWindow)
  73. local imx, imy = term.getSize()
  74.  
  75. local count = 1
  76. local function print(...)
  77.   term.redirect(infoWindow)
  78.   local args = table.pack(...)
  79.   local out = table.concat(args, " ")
  80.   local _, y = term.getCursorPos()
  81.   if y == imy then
  82.     term.scroll(1)
  83.     term.setCursorPos(1, y)
  84.   else
  85.     term.setCursorPos(1, y + 1)
  86.   end
  87.   write(string.format("[%d] %s", count, out))
  88.   count = count + 1
  89. end
  90.  
  91. term.setBackgroundColor(colors.black)
  92. -- mini pathfinding "algorithm".  Doesn't save location, just uses it for returning
  93. -- to base.
  94. local homePos = {0,0,0}
  95.  
  96. local pos = {0, 0, 0, f = 0}
  97. local function posu()
  98.   term.redirect(posWindow)
  99.   write(string.format("\nx: %d | y: %d | z: %d | facing: %s", pos[1], pos[2], pos[3],
  100.                       pos.f == 0 and "forward" or pos.f == 1 and "right"
  101.                       or pos.f == 2 and "back" or pos.f == 3 and "left"))
  102. end
  103.  
  104. local function fuelu()
  105.   term.redirect(fuelWindow)
  106.   write(string.format("\n%d / %d", turtle.getFuelLevel(), turtle.getFuelLimit()))
  107. end
  108. fuelu()
  109.  
  110. local moves = 0
  111. local function compu(x)
  112.   term.redirect(compWindow)
  113.   moves = moves + (x or 1)
  114.   local perc = moves / totalMoves
  115.   perc = math.floor(perc * mx)
  116.  
  117.   term.setBackgroundColor(colors.black)
  118.   term.setCursorPos(1, 1)
  119.   local space = string.rep(' ', mx)
  120.   write(space)
  121.  
  122.   term.setBackgroundColor(colors.lightGray)
  123.   term.setCursorPos(1, 1)
  124.   space = string.rep(' ', perc)
  125.   write(space)
  126.  
  127.   local toWrite = string.format("%d / %d", moves, totalMoves)
  128.   if toWrite:len() > perc then
  129.     term.setBackgroundColor(colors.black)
  130.     term.setCursorPos(mx - toWrite:len() + 1, 1)
  131.   else
  132.     term.setCursorPos(1, 1)
  133.   end
  134.   write(toWrite)
  135.  
  136.   term.setBackgroundColor(colors.black)
  137. end
  138.  
  139. -- "overrides" for turtle commands
  140. local function forward()
  141.   local ok, err = turtle.forward()
  142.   if ok then
  143.     if pos.f == 0 then
  144.       -- forward on z axis (forward)
  145.       pos[3] = pos[3] + 1
  146.     elseif pos.f == 1 then
  147.       -- forward on x axis (right)
  148.       pos[1] = pos[1] + 1
  149.     elseif pos.f == 2 then
  150.       -- back on z axis (back)
  151.       pos[3] = pos[3] - 1
  152.     elseif pos.f == 3 then
  153.       -- back on x axis (left)
  154.       pos[1] = pos[1] - 1
  155.     end
  156.   end
  157.  
  158.   posu()
  159.   fuelu()
  160.   return ok, err
  161. end
  162.  
  163. local function up()
  164.   local ok, err = turtle.up()
  165.   if ok then
  166.     -- up on y axis
  167.     pos[2] = pos[2] + 1
  168.   end
  169.  
  170.   posu()
  171.   fuelu()
  172.   return ok, err
  173. end
  174.  
  175. local function down()
  176.   local ok, err = turtle.down()
  177.   if ok then
  178.     -- down on y axis
  179.     pos[2] = pos[2] - 1
  180.   end
  181.   posu()
  182.   fuelu()
  183.   return ok, err
  184. end
  185.  
  186. local function left()
  187.   local ok, err = turtle.turnLeft()
  188.   if ok then
  189.     pos.f = pos.f - 1
  190.     if pos.f < 0 then pos.f = 3 end
  191.   end
  192.   posu()
  193.   return ok, err
  194. end
  195.  
  196. local function right()
  197.   local ok, err = turtle.turnRight()
  198.   if ok then
  199.     pos.f = (pos.f + 1) % 4
  200.   end
  201.   posu()
  202.   return ok, err
  203. end
  204.  
  205. -- face a certain direction.
  206. local function face(x)
  207.   local lf = ((x + 1) % 4 == pos.f)
  208.   local rf = (x - 1 == pos.f) or (x - 1 + 4 == pos.f)
  209.  
  210.   if lf then
  211.     ensure(left)
  212.   elseif rf then
  213.     ensure(right)
  214.   else
  215.     ensure(left)
  216.     ensure(left)
  217.   end
  218. end
  219.  
  220. -- calculate the fuel required using the same "movements" as the actual run func
  221. local function calcFuel()
  222.   local fuelNeeded = 0
  223.   totalMoves = 0
  224.   for i = 1, y, 2 do
  225.     for j = 1, x do
  226.       for k = 1, j ~= 1 and z - 1 or i == 1 and z or z - 1 do
  227.         if i ~= y then
  228.           totalMoves = totalMoves + 1
  229.         end
  230.         totalMoves = totalMoves + 2
  231.         if i ~= y and k == z - 1 then
  232.           totalMoves = totalMoves + 1
  233.         end
  234.         fuelNeeded = fuelNeeded + 1
  235.       end
  236.       if j ~= x then
  237.         if i ~= y then
  238.           totalMoves = totalMoves + 1
  239.         end
  240.         fuelNeeded = fuelNeeded + 1
  241.         totalMoves = totalMoves + 4
  242.       end
  243.     end
  244.     if i < y - 1 then
  245.       fuelNeeded = fuelNeeded + 2
  246.       totalMoves = totalMoves + 6
  247.     end
  248.   end
  249.  
  250.   -- estimation of the fuel needed for returning to the base when inventory is full
  251.   fuelNeeded = fuelNeeded + ((x + y + z) * 2)
  252.   return fuelNeeded
  253. end
  254.  
  255. local fuelReq = calcFuel()
  256. local fuelCalc = fuelReq
  257. local current = turtle.getFuelLevel()
  258. print(string.format("fuel: %d/%d required", current, fuelReq))
  259. if current < fuelReq then
  260.   print()
  261.   error(string.format("Not enough fuel. Need %d more.", fuelReq - current), -1)
  262. else
  263.   print(string.format("We're good to go. We'll have %d fuel left after this.",
  264.                       current - fuelReq))
  265. end
  266.  
  267. local turnDir = right
  268. local facing = true
  269.  
  270. local function dropItems(flerp)
  271.   local cpos = {pos[1], pos[2], pos[3], f = pos.f}
  272.   print(string.format("Returning home from x:%d, y:%d, z:%d.", pos[1], pos[2], pos[3]))
  273.  
  274.   -- get home
  275.  
  276.   -- y axis
  277.   while pos[2] < 0 do
  278.     ensure(up, turtle.digUp, turtle.attackUp)
  279.   end
  280.  
  281.   -- z axis
  282.   face(3)
  283.   while pos[1] > 0 do
  284.     ensure(forward, turtle.dig, turtle.attack)
  285.   end
  286.  
  287.   -- x axis
  288.   face(2)
  289.   while pos[3] > 0 do
  290.     ensure(forward, turtle.dig, turtle.attack)
  291.   end
  292.  
  293.   -- drop items
  294.   print("Dropping items.")
  295.   for i = 1, 16 do
  296.     if turtle.getItemCount(i) > 0 then
  297.       turtle.select(i)
  298.       turtle.drop()
  299.     end
  300.   end
  301.   turtle.select(1)
  302.  
  303.   if not flerp then
  304.     -- return to last location
  305.     -- x axis
  306.     print(string.format("Returning to x:%d, y:%d, z:%d.", cpos[1], cpos[2], cpos[3]))
  307.     face(0)
  308.     while pos[3] < cpos[3] do
  309.       ensure(forward, turtle.dig, turtle.attack)
  310.     end
  311.  
  312.     -- z axis
  313.     face(1)
  314.     while pos[1] < cpos[1] do
  315.       ensure(forward, turtle.dig, turtle.attack)
  316.     end
  317.  
  318.     -- y axis
  319.     while pos[2] > cpos[2] do
  320.       ensure(down, turtle.digDown, turtle.attackDown)
  321.     end
  322.  
  323.     -- restore original facing
  324.     face(cpos.f)
  325.   end
  326. end
  327.  
  328. local function checkItems()
  329.   local sum = 0
  330.   for i = 1, 16 do
  331.     sum = sum + (turtle.getItemCount(i) > 0 and 1 or 0)
  332.   end
  333.   return sum
  334. end
  335.  
  336. local function swap(t)
  337.   turnDir = turnDir == left and right or left
  338.   facing = not facing
  339. end
  340.  
  341. local function run()
  342.   for i = 1, y, 2 do
  343.     for j = 1, x do
  344.       for k = 1, j ~= 1 and z - 1 or i == 1 and z or z - 1 do
  345.         -- only mine the second block if we aren't on the block we need to stop on
  346.         if i ~= y then
  347.           turtle.digDown()
  348.           compu(1)
  349.         end
  350.         -- mine and move
  351.         ensure(forward, turtle.dig, turtle.attack)
  352.         compu(2)
  353.         if i ~= y and k == z - 1 then
  354.           turtle.digDown()
  355.           compu(1)
  356.         end
  357.         if k % 5 == 0 then
  358.           if checkItems() > 10 then
  359.             dropItems(false)
  360.           end
  361.         end
  362.       end
  363.  
  364.       if j ~= x then
  365.         -- turn around and start the next line
  366.         if i ~= y then
  367.           turtle.digDown()
  368.           compu(1)
  369.         end
  370.         ensure(turnDir)
  371.         ensure(forward, turtle.dig, turtle.attack)
  372.         ensure(turnDir)
  373.         compu(4)
  374.       end
  375.       -- swap the way we turn
  376.       swap()
  377.     end
  378.     -- move up two spots
  379.     if i < y - 1 then
  380.       ensure(down, turtle.digDown, turtle.attackUp)
  381.       ensure(down, turtle.digDown, turtle.attackUp)
  382.       ensure(turnDir)
  383.       ensure(turnDir)
  384.       compu(6)
  385.       swap()
  386.     end
  387.   end
  388.   dropItems(true)
  389.   print("Digging complete.")
  390. end
  391.  
  392. local ok, err = pcall(run)
  393.  
  394. if not ok then
  395.   term.redirect(origTerm)
  396.   term.setBackgroundColor(colors.black)
  397.   term.setTextColor(colors.white)
  398.   term.clear()
  399.   term.setCursorPos(1, 1)
  400.   printError(err)
  401.   return
  402. end
  403. term.redirect(origTerm)
  404. os.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement