Advertisement
tpboyle

bobbybj-test.lua

Apr 1st, 2020
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.23 KB | None | 0 0
  1.  
  2. -- globals
  3.  
  4. args = {...}
  5.  
  6. if table.getn(args) > 0 then
  7.     width = args[1]
  8.     height = args[2]
  9. else
  10.     -- default values
  11.     width = 10
  12.     height = 4
  13. end
  14.  
  15. default_item_slot = 1
  16. end_of_inventory = 14
  17. torch_slot = 15
  18. fuel_slot = 16
  19.  
  20. x_between_torches = 2
  21. z_between_torches = 0
  22. z_since_last_torch = 0
  23.  
  24. common_items = {
  25.     "minecraft:cobblestone",
  26.     "minecraft:stone"
  27. }
  28.  
  29.  
  30. -- globals - positioning
  31.  
  32. unit_vectors = {}
  33. unit_vectors['deeper'] = {x = 0, y = 0, z = 1}
  34. unit_vectors['out'] = {x = 0, y = 0, z = -1}
  35. unit_vectors['left'] = {x = -1, y = 0, z = 0}
  36. unit_vectors['right'] = {x = 1, y = 0, z = 0}
  37.  
  38. turning = {}
  39. turning['deeper'] = {left = 'left', right = 'right'}
  40. turning['out'] = {left = 'right', right = 'left'}
  41. turning['left'] = {left = 'out', right = 'deeper'}
  42. turning['right'] = {left = 'deeper', right = 'out'}
  43.  
  44. facing = "deeper"
  45.  
  46. pos = {x = 1, y = 1, z = 1}
  47.  
  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. -- inventory
  67.  
  68. function has_empty_slot()
  69.     for i=1,end_of_inventory do
  70.         if turtle.getItemDetail(i) == nil then
  71.             return true
  72.         end
  73.     end
  74.     return false
  75. end
  76.  
  77. function empty_inventory()
  78.     prev_pos = copy_position()
  79.     prev_facing = facing
  80.  
  81.     navigate_to(origin)
  82.     destroy_commons()
  83.     empty_chestables()
  84.    
  85.     navigate_to(prev_pos)
  86.     face(prev_facing)
  87. end
  88.  
  89. function empty_chestables()
  90.     -- assumes turtle has already destroyed commons so only good shit left
  91.     face("out")
  92.     print("emptying chestables!")
  93.     for j=1,end_of_inventory do
  94.         item = turtle.getItemDetail(j)
  95.         if not (item == nil) then
  96.             drop(j)
  97.         end
  98.     end
  99. end
  100.  
  101. function drop(i)
  102.     turtle.select(i)
  103.     turtle.drop()
  104.     turtle.select(default_item_slot)
  105. end
  106.  
  107. function destroy_commons()
  108.     -- assumes turtle is currently at chest
  109.     navigate_to_inventory("lava")
  110.     for i=1,end_of_inventory do
  111.         item = turtle.getItemDetail(i)
  112.         if ((not (item == nil)) and (is_common_item(item.name))) then
  113.             drop(i)
  114.         end
  115.     end
  116.     navigate_to_inventory("chest")
  117. end
  118.  
  119. function is_common_item(item_name)
  120.     for _, common_name in pairs(common_items) do
  121.         if item_name == common_name then
  122.             return true
  123.         end
  124.     end
  125.     return false
  126. end
  127.  
  128. function navigate_to_inventory(destination)
  129.     if destination == "lava" then
  130.         face("out")
  131.     elseif destination == "chest" then
  132.         face("deeper")
  133.     end
  134.     move("up")
  135.     move("forward")
  136.     move("forward")
  137.     move("down")
  138.     move("down") -- ensure we're on the ground
  139.    
  140.     if destination == "lava" then
  141.     elseif destination == "chest" then
  142.         face("out")
  143.     end
  144. end
  145.  
  146.  
  147. -- movement & positioning
  148.  
  149. function copy_position()
  150.     return {x = pos.x, y = pos.y, z = pos.z}
  151. end
  152.  
  153. function print_position()
  154.     local x_str = tostring(pos.x)
  155.     local y_str = tostring(pos.y)
  156.     local z_str = tostring(pos.z)
  157.     print("pos: (" .. x_str .. ", " .. y_str .. ", " .. z_str .. ")  facing: " .. facing)
  158. end
  159.  
  160. function move(direction)
  161.     local success = false
  162.     if direction == "forward" then
  163.         success = turtle.forward()
  164.         if success then
  165.             pos.x = pos.x + unit_vectors[facing]["x"]
  166.             pos.z = pos.z + unit_vectors[facing]["z"]
  167.         end
  168.     elseif direction == "up" then
  169.         success = turtle.up()
  170.         if success then
  171.             pos.y = pos.y + 1
  172.         end
  173.     elseif direction == "down" then
  174.         success = turtle.down()
  175.         if success then
  176.             pos.y = pos.y - 1
  177.         end
  178.     end
  179.     return success
  180. end
  181.  
  182. function turn(direction)
  183.     if direction == "left" then
  184.         facing = turning[facing].left
  185.         turtle.turnLeft()
  186.     elseif direction == "right" then
  187.         facing = turning[facing].right
  188.         turtle.turnRight()
  189.     end
  190. end
  191.  
  192. function face(direction)
  193.     while not (facing == direction) do
  194.         turn("right")
  195.     end
  196. end
  197.  
  198. function turn_around()
  199.     turn("right")
  200.     turn("right")
  201. end
  202.  
  203. function navigate_to(new_pos)
  204.     -- assumes space at new_pos is empty
  205.     navigate_to_z_coord(new_pos.z)
  206.     navigate_to_y_coord(new_pos.y)
  207.     navigate_to_x_coord(new_pos.x)
  208. end
  209.  
  210. function navigate_to_x_coord(new_x)
  211.     if (new_x > pos.x) then
  212.         face("right")
  213.     elseif (new_x < pos.x) then
  214.         face("left")
  215.     end
  216.     while not (pos.x == new_x) do
  217.         move("forward")
  218.     end
  219. end
  220.  
  221. function navigate_to_y_coord(new_y)
  222.     while pos.y < new_y do
  223.         move("up")
  224.     end
  225.     while pos.y > new_y do
  226.         move("down")
  227.     end
  228. end
  229.  
  230. function navigate_to_z_coord(new_z)
  231.     if (new_z > pos.z) then
  232.         face("deeper")
  233.     elseif (new_z < pos.z) then
  234.         face("out")
  235.     end
  236.     while not (pos.z == new_z) do
  237.         move("forward")
  238.     end
  239. end
  240.  
  241.  
  242. -- utility
  243.  
  244. function sleep(n)  -- seconds
  245.     local clock = os.clock
  246.     local t0 = clock()
  247.     while clock() - t0 <= n do end
  248. end
  249.  
  250.  
  251.  
  252. -- exec
  253.  
  254. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement