Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tFuel = {
- 'charcoal',
- 'coal',
- }
- local tInverseDirection = {
- ['north'] = 'south',
- ['east'] = 'west',
- ['south'] = 'north',
- ['west'] = 'east',
- }
- local tDirection = {
- ['north'] = 0, [0] = 'north',
- ['east'] = 1, [1] = 'east',
- ['south'] = 2, [2] = 'south',
- ['west'] = 3, [3] = 'west',
- }
- local direction = tDirection['north']
- local function setDirection(newDirection)
- if tDirection[newDirection] then
- direction = tDirection[newDirection]
- print('Direction set to ' .. tDirection[direction])
- else
- print('ERROR in setDirection(): "' .. (newDirection or 'nil') .. '" is not a valid argument')
- end
- end
- local function getDirection()
- return tDirection[direction]
- end
- local function setFuel(fuelTable)
- if type(fuelTable) ~= 'table' then
- print('ERROR: fuel table not set!')
- return
- end
- tFuel = {}
- for i,v in ipairs(fuelTable) do
- print('Registered ' .. v .. ' as fuel item.')
- tFuel[i] = v
- end
- end
- local function selectItem(itemName)
- local item
- for slot = 1,16 do
- item = turtle.getItemDetail(slot)
- if item and ((item.name == 'minecraft:' .. itemName) or (item.name == itemName)) then
- turtle.select(slot)
- return true
- end
- end
- return false
- end
- local function existItem(itemName)
- local item
- for slot = 1,16 do
- item = turtle.getItemDetail(slot)
- if item and (item.name == 'minecraft:' .. itemName) then
- return true
- end
- end
- return false
- end
- local function dropAll(itemName)
- itemName = 'minecraft:' .. itemName
- for slot = 1,16 do
- local item = turtle.getItemDetail(slot)
- if item and (item.name == itemName) then
- turtle.select(slot)
- turtle.drop()
- end
- end
- end
- local function dropAllButOneStack(itemName)
- itemName = 'minecraft:' .. itemName
- local amount = 0
- local slots = {}
- for slot = 16, 1, -1 do
- item = turtle.getItemDetail(slot)
- if item and (item.name == itemName) then
- amount = amount + turtle.getItemCount(slot)
- table.insert(slots, slot)
- end
- end
- if amount > 64 then
- amount = amount - 64
- for i, slot in ipairs(slots) do
- local drop = math.min(amount, turtle.getItemCount(slot))
- turtle.select(slot)
- if turtle.drop(math.min(drop, 64)) then
- amount = amount - drop
- if amount == 0 then
- return
- end
- end
- end
- end
- end
- local function refuel()
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" or fuelLevel > 0 then
- return
- end
- selectedSlot = turtle.getSelectedSlot()
- local function tryRefuel()
- for _, item in ipairs(tFuel) do
- if selectItem(item) then
- if turtle.refuel(1) then
- return true
- end
- end
- end
- return false
- end
- if not tryRefuel() then
- print( 'Add more fuel to continue.' )
- while not tryRefuel() do
- sleep(1)
- end
- print( 'Resuming work...' )
- end
- turtle.select(selectedSlot)
- end
- local function forward()
- refuel()
- while not turtle.forward() do
- if turtle.detect() then
- turtle.dig()
- elseif turtle.attack() then
- -- attack
- else
- sleep( 0.5 )
- end
- end
- return true
- end
- local function up()
- refuel()
- while not turtle.up() do
- if turtle.detectUp() then
- turtle.digUp()
- elseif turtle.attackUp() then
- -- attack
- else
- sleep( 0.5 )
- end
- end
- return true
- end
- local function down()
- refuel()
- while not turtle.down() do
- if turtle.detectDown() then
- turtle.digDown()
- elseif turtle.attackDown() then
- -- attack
- else
- sleep( 0.5 )
- end
- end
- return true
- end
- local function back()
- refuel()
- if not turtle.back() then
- turtle.turnLeft()
- turtle.turnLeft()
- while not turtle.forward() do
- if turtle.detect() then
- turtle.dig()
- elseif turtle.attack() then
- -- attack
- else
- sleep( 0.5 )
- end
- end
- turtle.turnLeft()
- turtle.turnLeft()
- end
- return true
- end
- local function turnLeft(turns)
- t = turns or 1
- for n=1, t do
- direction = (direction + 3) % 4
- turtle.turnLeft()
- end
- end
- local function turnRight(turns)
- t = turns or 1
- for n=1, t do
- direction = (direction + 1) % 4
- turtle.turnRight()
- end
- end
- local function turnAround()
- turnRight(2)
- end
- local function turnTo(newDirection)
- print('direction: ' .. direction .. '\tnew direction: '.. tDirection[newDirection])
- turns = direction - tDirection[newDirection]
- if turns == -3 then
- turns = 1
- elseif turns == 3 then
- turns = -1
- end
- if turns < 0 then
- print(turns .. ' turnRight()')
- turnRight(math.abs(turns))
- elseif turns > 0 then
- print(turns .. ' turnLeft()')
- turnLeft(math.abs(turns))
- else
- return
- end
- print(tDirection[direction])
- end
- return {
- selectItem = selectItem,
- existItem = existItem,
- dropAll = dropAll,
- dropAllButOneStack = dropAllButOneStack,
- setFuel = setFuel,
- setDirection = setDirection,
- getDirection = getDirection,
- forward = forward,
- up = up,
- down = down,
- back = back,
- tInverseDirection = tInverseDirection,
- turnLeft = turnLeft,
- turnRight = turnRight,
- turnAround = turnAround,
- turnTo = turnTo,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement