tpboyle

bobbybj-mine.lua

Mar 31st, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.99 KB | None | 0 0
  1.  
  2.  
  3. -- globals
  4.  
  5. args = {...}
  6.  
  7. if table.getn(args) > 0 then
  8.     width = args[1]
  9.     height = args[2]
  10. else
  11.     -- default values
  12.     width = 10
  13.     height = 4
  14. end
  15.  
  16. torch_slot = 15
  17. fuel_slot = 16
  18.  
  19. x_between_torches = 4
  20. z_between_torches = 4
  21. z_since_last_torch = 0
  22.  
  23. common_items = {
  24.     "minecraft:cobblestone",
  25.     "minecraft:stone"
  26. }
  27.  
  28. end_of_inventory = 14
  29.  
  30.  
  31. -- globals - positioning
  32.  
  33. unit_vectors = {}
  34. unit_vectors['deeper'] = {x = 0, y = 0, z = 1}
  35. unit_vectors['out'] = {x = 0, y = 0, z = -1}
  36. unit_vectors['left'] = {x = -1, y = 0, z = 0}
  37. unit_vectors['right'] = {x = 1, y = 0, z = 0}
  38.  
  39. turning = {}
  40. turning['deeper'] = {left = 'left', right = 'right'}
  41. turning['out'] = {left = 'right', right = 'left'}
  42. turning['left'] = {left = 'out', right = 'deeper'}
  43. turning['right'] = {left = 'deeper', right = 'out'}
  44.  
  45. facing = "deeper"
  46.  
  47. pos = {x = 1, y = 1, z = 1}
  48.  
  49. -- main
  50.  
  51. function main()
  52.     origin = copy_position()
  53.     empty_inventory()
  54.     face("deeper")
  55.     while true do
  56.         dig_slice("up")
  57.         dig_slice("down")
  58.         refuel_if_needed()
  59.         if not has_empty_slot() then
  60.             empty_inventory()
  61.         end
  62.     end
  63. end
  64.  
  65.  
  66. -- digging
  67.  
  68. function dig_slice(z_direction)
  69.     face("deeper")
  70.     dig_into_slice(z_direction)
  71.     z_since_last_torch = z_since_last_torch + 1
  72.     for i=1,height-1 do
  73.         dig_row()
  74.         dig_and_move(z_direction)
  75.         turn_around()
  76.     end
  77.  
  78.     if z_direction == "down" and z_since_last_torch > z_between_torches then
  79.         dig_row_and_place_torches()
  80.         z_since_last_torch = 1
  81.     else
  82.         dig_row()
  83.     end
  84. end
  85.  
  86. function dig_into_slice(z_direction)
  87.     dig_and_move("forward")
  88.     if height % 2 == 0 then
  89.         face("right")
  90.     else
  91.         face("left")
  92.     end
  93. end
  94.  
  95. function dig_row()
  96.     for i=1,(width-1) do
  97.         dig_and_move("forward")
  98.     end
  99. end
  100.  
  101. function dig_and_move(direction)
  102.     dig(direction)
  103.     local success = move(direction)
  104.     if not success then
  105.         dig_and_move(direction)
  106.     end
  107. end
  108.  
  109. function dig(direction)
  110.     if direction == "forward" then
  111.         turtle.dig()
  112.     elseif direction == "up" then
  113.         turtle.digUp()
  114.     elseif direction == "down" then
  115.         turtle.digDown()
  116.     end
  117. end
  118.  
  119. function dig_row_and_place_torches()
  120.     for i=1,(width-1) do
  121.         turtle.dig()
  122.         if ((i > 1) and (i % x_between_torches == 0)) then
  123.             turn_around()
  124.             place_torch()
  125.             turn_around()
  126.         end
  127.         move("forward")
  128.     end
  129. end
  130.  
  131. function place_torch()
  132.     print_position()
  133.     turtle.select(torch_slot)
  134.     turtle.place()
  135. end
  136.  
  137.  
  138. -- refueling
  139.  
  140. function refuel_if_needed()
  141.     if needs_fuel() then
  142.         turtle.select(fuel_slot)
  143.         turtle.refuel()
  144.     end
  145. end
  146.  
  147. function needs_fuel()
  148.     slice_size = width * height
  149.     spaces_back_to_fuel = math.abs(pos.x) + math.abs(pos.y) + math.abs(pos.z)
  150.     if turtle.getFuelLevel() < 5 * spaces_back_to_fuel then
  151.         return true
  152.     else
  153.         return false
  154.     end
  155. end
  156.  
  157.  
  158.  
  159. -- inventory
  160.  
  161. function has_empty_slot()
  162.     for i=1,end_of_inventory do
  163.         if turtle.getItemDetail(i) == nil then
  164.             return true
  165.         end
  166.     end
  167.     return false
  168. end
  169.  
  170. function empty_inventory()
  171.     prev_pos = copy_position()
  172.     prev_facing = facing
  173.  
  174.     navigate_to(origin)
  175.     destroy_commons()
  176.     empty_chestables()
  177.    
  178.     navigate_to(prev_pos)
  179.     face(prev_facing)
  180. end
  181.  
  182. function empty_chestables()
  183.     -- assumes turtle has already destroyed commons so only good shit left
  184.     face("out")
  185.     print("emptying chestables!")
  186.     for j=1,end_of_inventory do
  187.         item = turtle.getItemDetail(j)
  188.         if not (item == nil) then
  189.             drop(j)
  190.         end
  191.     end
  192. end
  193.  
  194. function drop(i)
  195.     turtle.select(i)
  196.     turtle.drop()
  197. end
  198.  
  199. function destroy_commons()
  200.     -- assumes turtle is currently at chest
  201.     navigate_to_inventory("lava")
  202.     for i=1,16 do
  203.         item = turtle.getItemDetail(i)
  204.         if ((not (item == nil)) and (is_common_item(item.name))) then
  205.             drop(i)
  206.         end
  207.     end
  208.     navigate_to_inventory("chest")
  209. end
  210.  
  211. function is_common_item(item_name)
  212.     for _, common_name in pairs(common_items) do
  213.         if item_name == common_name then
  214.             return true
  215.         end
  216.     end
  217.     return false
  218. end
  219.  
  220. function navigate_to_inventory(destination)
  221.     if destination == "lava" then
  222.         face("out")
  223.     elseif destination == "chest" then
  224.         face("deeper")
  225.     end
  226.     move("up")
  227.     move("forward")
  228.     move("forward")
  229.     move("down")
  230.     move("down") -- ensure we're on the ground
  231.    
  232.     if destination == "lava" then
  233.     elseif destination == "chest" then
  234.         face("out")
  235.     end
  236. end
  237.  
  238.  
  239.  
  240. -- movement & positioning
  241.  
  242. function copy_position()
  243.     return {x = pos.x, y = pos.y, z = pos.z}
  244. end
  245.  
  246. function print_position()
  247.     local x_str = tostring(pos.x)
  248.     local y_str = tostring(pos.y)
  249.     local z_str = tostring(pos.z)
  250.     print("pos: (" .. x_str .. ", " .. y_str .. ", " .. z_str .. ")  facing: " .. facing)
  251. end
  252.  
  253. function move(direction)
  254.     local success = false
  255.     if direction == "forward" then
  256.         success = turtle.forward()
  257.         if success then
  258.             pos.x = pos.x + unit_vectors[facing]["x"]
  259.             pos.z = pos.z + unit_vectors[facing]["z"]
  260.         end
  261.     elseif direction == "up" then
  262.         success = turtle.up()
  263.         if success then
  264.             pos.y = pos.y + 1
  265.         end
  266.     elseif direction == "down" then
  267.         success = turtle.down()
  268.         if success then
  269.             pos.y = pos.y - 1
  270.         end
  271.     end
  272.     return success
  273. end
  274.  
  275. function turn(direction)
  276.     if direction == "left" then
  277.         facing = turning[facing].left
  278.         turtle.turnLeft()
  279.     elseif direction == "right" then
  280.         facing = turning[facing].right
  281.         turtle.turnRight()
  282.     end
  283. end
  284.  
  285. function face(direction)
  286.     while not (facing == direction) do
  287.         turn("right")
  288.     end
  289. end
  290.  
  291. function turn_around()
  292.     turn("right")
  293.     turn("right")
  294. end
  295.  
  296. function navigate_to(new_pos)
  297.     -- assumes space at new_pos is empty
  298.     navigate_to_z_coord(new_pos.z)
  299.     navigate_to_y_coord(new_pos.y)
  300.     navigate_to_x_coord(new_pos.x)
  301. end
  302.  
  303. function navigate_to_x_coord(new_x)
  304.     if (new_x > pos.x) then
  305.         face("right")
  306.     elseif (new_x < pos.x) then
  307.         face("left")
  308.     end
  309.     while not (pos.x == new_x) do
  310.         move("forward")
  311.     end
  312. end
  313.  
  314. function navigate_to_y_coord(new_y)
  315.     while pos.y < new_y do
  316.         move("up")
  317.     end
  318.     while pos.y > new_y do
  319.         move("down")
  320.     end
  321. end
  322.  
  323. function navigate_to_z_coord(new_z)
  324.     if (new_z > pos.z) then
  325.         face("deeper")
  326.     elseif (new_z < pos.z) then
  327.         face("out")
  328.     end
  329.     while not (pos.z == new_z) do
  330.         move("forward")
  331.     end
  332. end
  333.  
  334.  
  335. -- utility
  336.  
  337. function sleep(n)  -- seconds
  338.     local clock = os.clock
  339.     local t0 = clock()
  340.     while clock() - t0 <= n do end
  341. end
  342.  
  343.  
  344. -- exec
  345.  
  346. main()
Add Comment
Please, Sign In to add comment