Advertisement
KaosKlaus

tLib

Apr 14th, 2020
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.15 KB | None | 0 0
  1. local tFuel = {
  2.     'charcoal',
  3.     'coal',
  4. }
  5.  
  6. local tInverseDirection = {
  7.     ['north'] = 'south',
  8.     ['east'] = 'west',
  9.     ['south'] = 'north',
  10.     ['west'] = 'east',
  11. }
  12.  
  13. local tDirection = {
  14.     ['north'] = 0, [0] = 'north',
  15.     ['east'] = 1, [1] = 'east',
  16.     ['south'] = 2, [2] = 'south',
  17.     ['west'] = 3, [3] = 'west',
  18. }
  19.  
  20. local direction = tDirection['north']
  21.  
  22. local function setDirection(newDirection)
  23.     if tDirection[newDirection] then
  24.         direction = tDirection[newDirection]
  25.         print('Direction set to ' .. tDirection[direction])
  26.     else
  27.         print('ERROR in setDirection(): "' .. (newDirection or 'nil') .. '" is not a valid argument')
  28.     end
  29. end
  30.  
  31. local function getDirection()
  32.     return tDirection[direction]
  33. end
  34.  
  35. local function setFuel(fuelTable)
  36.     if type(fuelTable) ~= 'table' then
  37.         print('ERROR: fuel table not set!')
  38.         return
  39.     end
  40.     tFuel = {}
  41.     for i,v in ipairs(fuelTable) do
  42.         print('Registered ' .. v .. ' as fuel item.')
  43.         tFuel[i] = v
  44.     end
  45. end
  46.  
  47. local function selectItem(itemName)
  48.     local item
  49.     for slot = 1,16 do
  50.         item = turtle.getItemDetail(slot)
  51.         if item and ((item.name == 'minecraft:' .. itemName) or (item.name == itemName)) then
  52.             turtle.select(slot)
  53.             return true
  54.         end
  55.     end
  56.  
  57.     return false
  58. end
  59.  
  60. local function existItem(itemName)
  61.     local item
  62.     for slot = 1,16 do
  63.         item = turtle.getItemDetail(slot)
  64.         if item and (item.name == 'minecraft:' .. itemName) then
  65.             return true
  66.         end
  67.     end
  68.  
  69.     return false
  70. end
  71.  
  72. local function dropAll(itemName)
  73.     itemName = 'minecraft:' .. itemName
  74.     for slot = 1,16 do
  75.         local item = turtle.getItemDetail(slot)
  76.         if item and (item.name == itemName) then
  77.             turtle.select(slot)
  78.             turtle.drop()
  79.         end
  80.     end
  81. end
  82.  
  83. local function dropAllButOneStack(itemName)
  84.     itemName = 'minecraft:' .. itemName
  85.     local amount = 0
  86.     local slots = {}
  87.     for slot = 16, 1, -1 do
  88.         item = turtle.getItemDetail(slot)
  89.         if item and (item.name == itemName) then
  90.             amount = amount + turtle.getItemCount(slot)
  91.             table.insert(slots, slot)
  92.         end
  93.     end
  94.     if amount > 64 then
  95.         amount = amount - 64
  96.         for i, slot in ipairs(slots) do
  97.             local drop = math.min(amount, turtle.getItemCount(slot))
  98.             turtle.select(slot)
  99.             if turtle.drop(math.min(drop, 64)) then
  100.                 amount = amount - drop
  101.                 if amount == 0 then
  102.                     return
  103.                 end
  104.             end
  105.         end
  106.     end
  107. end
  108.  
  109. local function refuel()
  110.     local fuelLevel = turtle.getFuelLevel()
  111.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  112.         return
  113.     end
  114.  
  115.     selectedSlot = turtle.getSelectedSlot()
  116.    
  117.     local function tryRefuel()
  118.         for _, item in ipairs(tFuel) do
  119.             if selectItem(item) then
  120.                 if turtle.refuel(1) then
  121.                     return true
  122.                 end
  123.             end
  124.         end
  125.         return false
  126.     end
  127.    
  128.     if not tryRefuel() then
  129.             print( 'Add more fuel to continue.' )
  130.             while not tryRefuel() do
  131.                     sleep(1)
  132.             end
  133.             print( 'Resuming work...' )
  134.     end
  135.     turtle.select(selectedSlot)
  136. end
  137.  
  138. local function forward()
  139.     refuel()
  140.     while not turtle.forward() do
  141.             if turtle.detect() then
  142.                     turtle.dig()
  143.             elseif turtle.attack() then
  144.                 -- attack
  145.             else
  146.                     sleep( 0.5 )
  147.             end
  148.     end
  149.     return true
  150. end
  151.  
  152. local function up()
  153.     refuel()
  154.     while not turtle.up() do
  155.             if turtle.detectUp() then
  156.                     turtle.digUp()
  157.             elseif turtle.attackUp() then
  158.                 -- attack
  159.             else
  160.                     sleep( 0.5 )
  161.             end
  162.     end
  163.     return true
  164. end
  165.  
  166. local function down()
  167.     refuel()
  168.     while not turtle.down() do
  169.             if turtle.detectDown() then
  170.                     turtle.digDown()
  171.             elseif turtle.attackDown() then
  172.                 -- attack
  173.             else
  174.                     sleep( 0.5 )
  175.             end
  176.     end
  177.     return true
  178. end
  179.  
  180. local function back()
  181.     refuel()
  182.     if not turtle.back() then
  183.         turtle.turnLeft()
  184.         turtle.turnLeft()
  185.         while not turtle.forward() do
  186.                 if turtle.detect() then
  187.                         turtle.dig()
  188.                 elseif turtle.attack() then
  189.                     -- attack
  190.                 else
  191.                         sleep( 0.5 )
  192.                 end
  193.         end
  194.         turtle.turnLeft()
  195.         turtle.turnLeft()
  196.     end
  197.     return true
  198. end
  199.  
  200. local function turnLeft(turns)
  201.     t = turns or 1
  202.     for n=1, t do
  203.         direction = (direction + 3) % 4
  204.         turtle.turnLeft()
  205.     end
  206. end
  207.  
  208. local function turnRight(turns)
  209.     t = turns or 1
  210.     for n=1, t do
  211.         direction = (direction + 1) % 4
  212.         turtle.turnRight()
  213.     end
  214. end
  215.  
  216. local function turnAround()
  217.     turnRight(2)
  218. end
  219.  
  220. local function turnTo(newDirection)
  221.     print('direction: ' .. direction .. '\tnew direction: '.. tDirection[newDirection])
  222.     turns = direction - tDirection[newDirection]
  223.     if turns == -3 then
  224.         turns = 1
  225.     elseif turns == 3 then
  226.         turns = -1
  227.     end
  228.     if turns < 0 then
  229.         print(turns .. ' turnRight()')
  230.         turnRight(math.abs(turns))
  231.     elseif turns > 0 then
  232.         print(turns .. ' turnLeft()')
  233.         turnLeft(math.abs(turns))
  234.     else
  235.         return
  236.     end
  237.    
  238.     print(tDirection[direction])
  239. end
  240.  
  241. return {
  242.     selectItem = selectItem,
  243.     existItem = existItem,
  244.     dropAll = dropAll,
  245.     dropAllButOneStack = dropAllButOneStack,
  246.     setFuel = setFuel,
  247.     setDirection = setDirection,
  248.     getDirection = getDirection,
  249.     forward = forward,
  250.     up = up,
  251.     down = down,
  252.     back = back,
  253.     tInverseDirection = tInverseDirection,
  254.     turnLeft = turnLeft,
  255.     turnRight = turnRight,
  256.     turnAround = turnAround,
  257.     turnTo = turnTo,
  258.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement