Advertisement
Calame

advanced_turtle.lua

Feb 7th, 2021 (edited)
986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.17 KB | None | 0 0
  1. -----------------------------
  2. -- global turtle functions --
  3. -----------------------------
  4. if not turtle then
  5.     return
  6. end
  7.  
  8. local valid_fuel = {
  9.     "minecraft:charcoal",
  10.     "minecraft:coal",
  11. }
  12.  
  13. local DO_NOT_MINE = {
  14.     "forbidden_arcanus:stella_arcanum",
  15.     "minecraft:diamond_ore",
  16.     "mysticalworld:amethyst_ore",
  17. }
  18.  
  19. NORTH = 0
  20. EAST = 1
  21. SOUTH = 2
  22. WEST = 3
  23. LEFT = 4
  24. RIGHT = 5
  25.  
  26. turtle.x = 0
  27. turtle.y = 0
  28. turtle.z = 0
  29. turtle.dz = -1
  30. turtle.dx = 0
  31.  
  32. local forward =   turtle.forward
  33. local back =      turtle.back
  34. local up =        turtle.up
  35. local down =      turtle.down
  36. local turnLeft =  turtle.turnLeft
  37. local turnRight = turtle.turnRight
  38.  
  39. function turtle.reverseDir( direction )
  40.     if     direction == "forward" then return "back"
  41.     elseif direction == "down"    then return "up"
  42.     elseif direction == "back"    then return "forward"
  43.     elseif direction == "up"      then return "down"
  44.     end
  45.     error( "turtle.reverseDir invalid direction!" )
  46. end
  47.  
  48. function turtle.position()
  49.     return vector.new( x, y, z )
  50. end
  51.  
  52. function turtle.facing()
  53.     if     dz == -1 and dx ==  0 then return NORTH
  54.     elseif dz ==  0 and dx == -1 then return WEST
  55.     elseif dz ==  0 and dx ==  1 then return EAST
  56.     elseif dz ==  1 and dx ==  0 then return SOUTH
  57.     end
  58.     error( "turtle.facing invalid direction!" )
  59. end
  60.  
  61. -- settings --
  62. function turtle.load_position()
  63.     local position = settings.get( "position" )
  64.    
  65.     if position then
  66.         turtle.x = position[ 1 ].x or 0
  67.         turtle.y = position[ 1 ].y or 0
  68.         turtle.z = position[ 1 ].z or 0
  69.         turtle.dz = position[ 2 ] or -1
  70.         turtle.dx = position[ 3 ] or 0
  71.     end
  72. end
  73.  
  74. function turtle.save_position()
  75.     settings.set( "position", { { x = turtle.x, y = turtle.y, z = turtle.z }, turtle.dz, turtle.dx } )
  76.     settings.save()
  77. end
  78.  
  79. function turtle.set_position( x, y, z, dir )
  80.     turtle.x = x
  81.     turtle.y = y
  82.     turtle.z = z
  83.  
  84.     if     dir == NORTH then turtle.dz = -1 turtle.dx =  0
  85.     elseif dir == WEST  then turtle.dz =  0 turtle.dx = -1
  86.     elseif dir == EAST  then turtle.dz =  0 turtle.dx =  1
  87.     elseif dir == SOUTH then turtle.dz =  1 turtle.dx =  0
  88.     end
  89.     turtle.save_position()
  90. end
  91.  
  92.  
  93. --- Movement ---
  94. -- Forward --
  95. function turtle.forward()
  96.     turtle.try_refuel()
  97.     if not forward() then return false end
  98.     turtle.x = turtle.x + turtle.dx
  99.     turtle.z = turtle.z + turtle.dz
  100.     turtle.save_position()
  101.     return true
  102. end
  103.  
  104. function turtle.wait_forward() while not turtle.forward() do os.sleep( 0.5 ) end end
  105. function turtle.force_forward( block_to_break ) turtle.force_move( "forward", block_to_break ) end
  106.  
  107. -- Down --
  108. function turtle.down()
  109.     turtle.try_refuel()
  110.     if not down() then return false end
  111.     turtle.y = turtle.y - 1
  112.     turtle.save_position()
  113.     return true
  114. end
  115.  
  116. function turtle.wait_down() while not turtle.down() do os.sleep( 0.5 ) end end
  117. function turtle.force_down( block_to_break ) turtle.force_move( "down", block_to_break ) end
  118.  
  119. -- Back --
  120. function turtle.back()
  121.     turtle.try_refuel()
  122.     if not back() then return false end
  123.     turtle.x = turtle.x - turtle.dx
  124.     turtle.z = turtle.z - turtle.dz
  125.     turtle.save_position()
  126.     return true
  127. end
  128.  
  129. function turtle.wait_back() while not turtle.back() do os.sleep( 0.5 ) end end
  130. function turtle.force_back( block_to_break ) turtle.force_move( "back", block_to_break ) end
  131.  
  132. -- Up --
  133. function turtle.up()
  134.     turtle.try_refuel()
  135.     if not up() then return false end
  136.     turtle.y = turtle.y + 1
  137.     turtle.save_position()
  138.     return true
  139. end
  140.  
  141. function turtle.wait_up() while not turtle.up() do os.sleep( 0.5 ) end end
  142. function turtle.force_up( block_to_break ) turtle.force_move( "up", block_to_break ) end
  143.  
  144. -- Move Direction --
  145. function turtle.moveDir( direction )
  146.     if     direction == "forward" then return turtle.forward()
  147.     elseif direction == "down"    then return turtle.down()
  148.     elseif direction == "back"    then return turtle.back()
  149.     elseif direction == "up"      then return turtle.up()
  150.     end
  151.     error( "turtle.moveDir direction unknown!" )
  152. end
  153.  
  154. -- Reverse --
  155. function turtle.reverse( direction ) return turtle.moveDir( turtle.reverseDir( direction ) ) end
  156. function turtle.force_reverse( direction ) turtle.force_move( turtle.reverseDir( direction ) ) end
  157.  
  158. function turtle.wait_move( direction )
  159.     while not turtle.move( direction ) do
  160.         os.sleep( 1 )
  161.     end
  162. end
  163.  
  164. -- Move --
  165. function turtle.move( direction, block_to_break )
  166.     turtle.try_refuel()
  167.     local moved = turtle.moveDir( direction )
  168.     if not moved and block_to_break and ( turtle.is_block_tag( direction, block_to_break ) or turtle.is_block_name( direction, block_to_break ) ) then
  169.         turtle.digDir( direction )
  170.         return turtle.moveDir( direction )
  171.     end
  172.     return moved
  173. end
  174.  
  175. function turtle.force_move( direction, block_to_break )
  176.     if direction ~= "back" then
  177.         for b = 1, #DO_NOT_MINE do
  178.             if turtle.is_block_name( direction, DO_NOT_MINE[ b ] ) then
  179.                 error( "I am scared of this " .. DO_NOT_MINE[ b ] )
  180.             end
  181.         end
  182.     end
  183.  
  184.     while( not turtle.moveDir( direction ) ) do
  185.         local s, d = turtle.inspectDir( direction )
  186.         if s and string.find( d.name, "turtle" ) then
  187.             os.sleep( 0.5 )
  188.         elseif not block_to_break or turtle.is_block_tag( direction, block_to_break ) or turtle.is_block_name( direction, block_to_break ) then
  189.             turtle.digDir( direction )
  190.         end
  191.     end
  192. end
  193.  
  194. --- Turning ---
  195. function turtle.turnRight()
  196.     turnRight()
  197.     local old_dx = turtle.dx
  198.     turtle.dx = -turtle.dz
  199.     turtle.dz = old_dx
  200.     turtle.save_position()
  201.     return true
  202. end
  203.  
  204. function turtle.turnLeft()
  205.     turnLeft()
  206.     local old_dx = turtle.dx
  207.     turtle.dx = turtle.dz
  208.     turtle.dz = -old_dx
  209.     turtle.save_position()
  210.     return true
  211. end
  212.  
  213. function turtle.turn180()
  214.     if math.random ( 2 ) == 1 then
  215.         turtle.turnLeft()
  216.         turtle.turnLeft()
  217.     else
  218.         turtle.turnRight()
  219.         turtle.turnRight()
  220.     end
  221. end
  222.  
  223. function turtle.turnDir( direction )
  224.     if     direction == LEFT  then return turtle.turnLeft()
  225.     elseif direction == RIGHT then return turtle.turnRight()
  226.     end
  227.     error( "turtle.turnDir invalid direction!" )
  228. end
  229.  
  230. function turtle.turn( direction )
  231.     local facing = turtle.facing()
  232.     if facing == direction then
  233.         return
  234.     end
  235.  
  236.     if direction > 3 then
  237.         turtle.turnDir( direction )
  238.     else
  239.         if math.abs( facing - direction ) == 2 then
  240.             turtle.turn180()
  241.         else
  242.             if ( facing - direction ) % 4 == 1 then
  243.                 turtle.turnLeft()
  244.             else
  245.                 turtle.turnRight()
  246.             end
  247.         end
  248.     end
  249. end
  250.  
  251. --- Dig ---
  252. function turtle.digBack()
  253.     turtle.turn180()
  254.     turtle.dig()
  255.     turtle.turn180()
  256. end
  257.  
  258. function turtle.dig_all( direction ) while turtle.digDir( direction ) do sleep( 0.05 ) end end
  259.  
  260. function turtle.digDir( direction )
  261.     if     direction == "forward"  then return turtle.dig()
  262.     elseif direction == "up"       then return turtle.digUp()
  263.     elseif direction == "down"     then return turtle.digDown()
  264.     elseif direction == "back"     then return turtle.digBack()
  265.     end
  266.     error( "turtle.digDir invalid direction" )
  267. end
  268.  
  269. -- Detect --
  270. function turtle.detectBack()
  271.     turtle.turn180()
  272.     local success = turtle.detect()
  273.     turtle.turn180()
  274.     return success
  275. end
  276.  
  277. function turtle.detectDir( direction )
  278.     if     direction == "forward"  then return turtle.detect()
  279.     elseif direction == "up"       then return turtle.detectUp()
  280.     elseif direction == "down"     then return turtle.detectDown()
  281.     elseif direction == "back"     then return turtle.detectBack()
  282.     end
  283.     error( "turtle.detectDir invalid direction!" )
  284. end
  285.  
  286. -- Inspect --
  287. function turtle.inspectBack()
  288.     turtle.turn180()
  289.     local success, data = turtle.inspect()
  290.     turtle.turn180()
  291.     return success, data
  292. end
  293.  
  294. function turtle.inspectDir( direction )
  295.     if     direction == "up"      then return turtle.inspectUp()
  296.     elseif direction == "down"    then return turtle.inspectDown()
  297.     elseif direction == "forward" then return turtle.inspect()
  298.     elseif direction == "back"    then return turtle.inspectBack()
  299.     end
  300.     error( "inspectDir direction unknown!" )
  301. end
  302.  
  303. -- Place --
  304. function turtle.placeDir( direction )
  305.     if     direction == "forward"  then return turtle.place()
  306.     elseif direction == "up"       then return turtle.placeUp()
  307.     elseif direction == "down"     then return turtle.placeDown()
  308.     end
  309.     error( "turtle.placeDir invalid direction" )
  310. end
  311.  
  312. function turtle.wait_place() while not turtle.place() do os.sleep( 1 ) end end
  313.  
  314. -- Return succes and if false, the name of the block
  315. function turtle.move_inspect( direction )
  316.     if turtle.moveDir( direction ) then
  317.         return true, nil
  318.     end
  319.  
  320.     local s, d = turtle.inspectDir( direction )
  321.     return false, d.name
  322. end
  323.  
  324. function turtle.move_toward( destination )
  325.     local distance = destination - turtle.position()
  326.  
  327.     if distance.x ~= 0 then
  328.         if distance.x > 0 then turtle.turn( EAST ) else turtle.turn( WEST ) end
  329.         return turtle.move_inspect( "forward" )
  330.     end
  331.  
  332.     if distance.z ~= 0 then
  333.         if distance.z > 0 then turtle.turn( SOUTH ) else turtle.turn( NORTH ) end
  334.         return turtle.move_inspect( "forward" )
  335.     end
  336.  
  337.     if distance.y ~= 0 then
  338.         if distance.y > 0 then return turtle.move_inspect( "up" ) else return turtle.move_inspect( "down" ) end
  339.     end
  340.  
  341.     return true
  342. end
  343.  
  344. function turtle.dig_toward( destination )
  345.     local distance = destination - turtle.position()
  346.  
  347.     if distance.x ~= 0 then
  348.         if distance.x > 0 then turtle.turn( EAST ) else turtle.turn( WEST ) end
  349.         return turtle.force_move( "forward" )
  350.     end
  351.  
  352.     if distance.z ~= 0 then
  353.         if distance.z > 0 then turtle.turn( SOUTH ) else turtle.turn( NORTH ) end
  354.         return turtle.force_move( "forward" )
  355.     end
  356.  
  357.     if distance.y ~= 0 then
  358.         if distance.y > 0 then return turtle.force_move( "up" ) else return turtle.force_move( "down" ) end
  359.     end
  360.  
  361.     return true
  362. end
  363.  
  364. -- array of vector
  365. function turtle.follow_path( path, can_dig )
  366.     for i = 1, #path do
  367.         if ( can_dig ) then
  368.             turtle.dig_toward( path[ i ] )
  369.         else
  370.             local s, n = turtle.move_toward( path[ i ] )
  371.  
  372.             if not s then
  373.                 map_add( path[ i ], n )
  374.                 save_map()
  375.                 return false
  376.             end
  377.         end
  378.     end
  379.  
  380.     return true
  381. end
  382.  
  383. function turtle.pathfind_to( destination, can_dig )
  384.     print( "Going to: " .. tostring( destination ) )
  385.     local path = turtle.A_Star( turtle.position(), destination )
  386.    
  387.     while not follow_path( path, can_dig ) do
  388.         print( "recalculating a path.")
  389.         path = turtle.A_Star( turtle.position(), destination )
  390.     end
  391.  
  392.     print( "ARRIVED !")
  393. end
  394.  
  395. -- Inspect --
  396. function turtle.is_block_name( direction, block_name )
  397.     local s, d = turtle.inspectDir( direction )
  398.     return s and d.name == block_name
  399. end
  400.  
  401. function turtle.is_block_tag( direction, tag )
  402.     if direction == "back" then return false end
  403.     if not turtle.detectDir( direction ) then return false end
  404.     local success, data = turtle.inspectDir( direction )
  405.     return success and data.tags[ tag ]
  406. end
  407.  
  408. -- Inventory --
  409. function turtle.getInventory()
  410.     local inv = {}
  411.  
  412.     for i = 1, 16 do
  413.         inv[ i ] = turtle.getItemDetail( i )
  414.     end
  415.  
  416.     return inv
  417. end
  418.  
  419. function turtle.get_item_index( name )
  420.     for i = 1, 16 do
  421.         local item = turtle.getItemDetail( i )
  422.         if item and string.find( item.name, name ) then
  423.             return i
  424.         end
  425.     end
  426.     return -1
  427. end
  428.  
  429. function turtle.has_items()
  430.     for i = 1, 16 do
  431.         if turtle.getItemCount( i ) > 0 then
  432.             return true
  433.         end
  434.     end
  435.     return false
  436. end
  437.  
  438. function turtle.is_inventory_full()
  439.     for i = 1, 16 do
  440.         if turtle.getItemCount( i ) == 0 then
  441.             return false
  442.         end
  443.     end
  444.     return true
  445. end
  446.  
  447. function turtle.drop_in_enderchest( stuff_to_keep )
  448.     local enderchest_index = turtle.get_item_index( "enderstorage:ender_chest" )
  449.  
  450.     if enderchest_index == -1 then return end
  451.  
  452.     local to_keep = {}
  453.     for k, v in pairs( stuff_to_keep ) do
  454.         to_keep[ k ] = v
  455.     end
  456.  
  457.     turtle.dig_all( "up" )
  458.     turtle.select( enderchest_index )
  459.     while not turtle.placeUp() do
  460.         os.sleep( 0.1 )
  461.     end
  462.  
  463.     for i = 1, 16 do
  464.         local item = turtle.getItemDetail( i )
  465.  
  466.         if item then
  467.             print( item.name )
  468.             print( to_keep[ item.name ] )
  469.             if to_keep[ item.name ] and to_keep[ item.name ] > 0 then
  470.                 to_keep[ item.name ] = to_keep[ item.name ] - 1
  471.             else
  472.                 turtle.select( i )
  473.                 turtle.dropUp()
  474.             end
  475.         end
  476.     end
  477.  
  478.     turtle.select( 1 )
  479.     turtle.digUp()
  480. end
  481.  
  482. -- Fuel --
  483. function turtle.get_valid_fuel_index()
  484.     for i = 1, 16 do
  485.         local item = turtle.getItemDetail( i )
  486.  
  487.         for f = 1, #valid_fuel do
  488.             if item and string.find( item.name, valid_fuel[ f ] ) then
  489.                 return i
  490.             end
  491.         end
  492.     end
  493.  
  494.     return -1
  495. end
  496.  
  497. function turtle.is_valid_fuel( item_name )
  498.     for f = 1, #valid_fuel do
  499.         if item_name == valid_fuel[ f ] then
  500.             return true
  501.         end
  502.     end
  503.  
  504.     return false
  505. end
  506.  
  507. function turtle.try_refuel()
  508.     if turtle.getFuelLevel() < 100 then
  509.         local fuel_index = turtle.get_valid_fuel_index()
  510.  
  511.         if fuel_index == -1 then
  512.             print( "Give me fuel please!" )
  513.             print( "Valid fluel:" )
  514.  
  515.             for f = 1, #valid_fuel do
  516.                 print( valid_fuel[ f ] )
  517.             end
  518.  
  519.             while fuel_index == -1 do
  520.                 os.sleep( 1 )
  521.                 fuel_index = turtle.get_valid_fuel_index()
  522.             end
  523.         end
  524.  
  525.         print( "Eating Some Fuel." )
  526.         turtle.select( fuel_index )
  527.         turtle.refuel( 2 )
  528.     end
  529. end
  530.  
  531. turtle.load_position()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement