better turtle.equip
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
{
|
||||
required = {
|
||||
'core',
|
||||
'turtle',
|
||||
},
|
||||
title = 'Programs for farming resources',
|
||||
repository = 'kepler155c/opus-apps/{{OPUS_BRANCH}}/farms',
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
local Peripheral = require('peripheral')
|
||||
local Equipper = require('turtle.equipper')
|
||||
local Point = require('point')
|
||||
local Util = require('util')
|
||||
|
||||
@@ -18,34 +18,14 @@ local Runners = {
|
||||
Blaze = false,
|
||||
}
|
||||
|
||||
local function equip(side, item, rawName)
|
||||
local equipped = Peripheral.lookup('side/' .. side)
|
||||
Equipper.equipLeft('minecraft:diamond_sword')
|
||||
Equipper.equipRight('plethora:module:2', 'plethora:scanner')
|
||||
|
||||
if equipped and equipped.type == item then
|
||||
return true
|
||||
end
|
||||
|
||||
if not turtle.equip(side, rawName or item) then
|
||||
if not turtle.selectSlotWithQuantity(0) then
|
||||
error('No slots available')
|
||||
end
|
||||
turtle.equip(side)
|
||||
if not turtle.equip(side, item) then
|
||||
error('Unable to equip ' .. item)
|
||||
end
|
||||
end
|
||||
|
||||
turtle.select(1)
|
||||
end
|
||||
|
||||
equip('left', 'minecraft:diamond_sword')
|
||||
|
||||
equip('right', 'plethora:scanner', 'plethora:module:2')
|
||||
local scanner = device['plethora:scanner']
|
||||
local facing = scanner.getBlockMeta(0, 0, 0).state.facing
|
||||
turtle.point.heading = Point.facings[facing].heading
|
||||
|
||||
equip('right', 'plethora:sensor', 'plethora:module:3')
|
||||
Equipper.equipRight('plethora:module:3', 'plethora:sensor')
|
||||
local sensor = device['plethora:sensor']
|
||||
|
||||
turtle.setMovementStrategy('goto')
|
||||
@@ -55,9 +35,9 @@ local function findChests()
|
||||
if chest then
|
||||
return { chest }
|
||||
end
|
||||
equip('right', 'plethora:scanner', 'plethora:module:2')
|
||||
Equipper.equipRight('plethora:module:2', 'plethora:scanner')
|
||||
local chests = scanner.scan()
|
||||
equip('right', 'plethora:sensor', 'plethora:module:3')
|
||||
Equipper.equipRight('plethora:module:3', 'plethora:sensor')
|
||||
|
||||
Util.filterInplace(chests, function(b)
|
||||
if b.name == 'minecraft:chest' or
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
local Equipper = require('turtle.equipper')
|
||||
local Point = require('point')
|
||||
local Util = require('util')
|
||||
|
||||
@@ -17,9 +18,8 @@ local FUEL = Util.transpose {
|
||||
'minecraft:blaze_rod:0',
|
||||
}
|
||||
|
||||
local scanner = device['plethora:scanner'] or
|
||||
turtle.equip('right', 'plethora:module:2') and device['plethora:scanner'] or
|
||||
error('Plethora scanner required')
|
||||
Equipper.equipRight('plethora:module:2', 'plethora:scanner')
|
||||
local scanner = device['plethora:scanner']
|
||||
|
||||
local crops = Util.readTable(CONFIG_FILE) or {
|
||||
['minecraft:wheat'] =
|
||||
@@ -126,7 +126,7 @@ local function scan()
|
||||
end
|
||||
|
||||
local function harvest(blocks)
|
||||
turtle.equip('right', 'minecraft:diamond_pickaxe')
|
||||
Equipper.equipRight('minecraft:diamond_pickaxe')
|
||||
|
||||
local dropped
|
||||
|
||||
@@ -185,11 +185,11 @@ local function harvest(blocks)
|
||||
|
||||
elseif b.action == 'bump' then
|
||||
if turtle.faceAgainst(b) then
|
||||
turtle.equip('right', 'plethora:module:3')
|
||||
Equipper.equipRight('plethora:module:3', 'plethora:sensor')
|
||||
os.sleep(.5)
|
||||
-- search the ground for the dropped cactus
|
||||
local sensed = peripheral.call('right', 'sense')
|
||||
turtle.equip('right', 'minecraft:diamond_pickaxe')
|
||||
Equipper.equipRight('minecraft:diamond_pickaxe')
|
||||
Util.filterInplace(sensed, function(s)
|
||||
if s.displayName == 'item.tile.cactus' then
|
||||
s.x = Util.round(s.x) + turtle.point.x
|
||||
@@ -217,7 +217,7 @@ local function harvest(blocks)
|
||||
end
|
||||
end
|
||||
end)
|
||||
turtle.equip('right', 'plethora:module:2')
|
||||
Equipper.equipRight('plethora:module:2', 'plethora:scanner')
|
||||
end
|
||||
|
||||
local s, m = turtle.run(function()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
local Adapter = require('core.chestAdapter18')
|
||||
local Config = require('config')
|
||||
local Peripheral = require('peripheral')
|
||||
local Equipper = require('turtle.equipper')
|
||||
local Util = require('util')
|
||||
|
||||
local device = _G.device
|
||||
@@ -30,28 +30,8 @@ local ANIMALS = {
|
||||
|
||||
local animal = ANIMALS[config.animal]
|
||||
|
||||
local function equip(side, item, rawName)
|
||||
local equipped = Peripheral.lookup('side/' .. side)
|
||||
|
||||
if equipped and equipped.type == item then
|
||||
return true
|
||||
end
|
||||
|
||||
if not turtle.equip(side, rawName or item) then
|
||||
if not turtle.selectSlotWithQuantity(0) then
|
||||
error('No slots available')
|
||||
end
|
||||
turtle.equip(side)
|
||||
if not turtle.equip(side, item) then
|
||||
error('Unable to equip ' .. item)
|
||||
end
|
||||
end
|
||||
|
||||
turtle.select(1)
|
||||
end
|
||||
|
||||
equip('left', 'minecraft:diamond_sword')
|
||||
equip('right', 'plethora:sensor', 'plethora:module:3')
|
||||
Equipper.equipLeft('minecraft:diamond_sword')
|
||||
Equipper.equipRight('plethora:module:3', 'plethora:sensor')
|
||||
|
||||
local sensor = device['plethora:sensor']
|
||||
|
||||
@@ -84,7 +64,7 @@ local function getAnimalCount()
|
||||
end
|
||||
|
||||
local function butcher()
|
||||
turtle.equip('right', 'minecraft:diamond_sword')
|
||||
Equipper.equipRight('minecraft:diamond_sword')
|
||||
turtle.select(1)
|
||||
|
||||
turtle.attack()
|
||||
@@ -92,7 +72,7 @@ local function butcher()
|
||||
turtle.turnRight()
|
||||
turtle.attack()
|
||||
end
|
||||
turtle.equip('right', 'plethora:module:3')
|
||||
Equipper.equipRight('plethora:module:3', 'plethora:sensor')
|
||||
|
||||
turtle.eachFilledSlot(function(slot)
|
||||
if not retain[slot.name] then
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
local Peripheral = require('peripheral')
|
||||
local Equipper = require('turtle.equipper')
|
||||
local Point = require('point')
|
||||
local Util = require('util')
|
||||
|
||||
@@ -11,29 +11,8 @@ local STARTUP_FILE = 'usr/autorun/spawner.lua'
|
||||
|
||||
local mobTypes = { }
|
||||
|
||||
local function equip(side, item, rawName)
|
||||
local equipped = Peripheral.lookup('side/' .. side)
|
||||
|
||||
if equipped and equipped.type == item then
|
||||
return true
|
||||
end
|
||||
|
||||
if not turtle.equip(side, rawName or item) then
|
||||
if not turtle.selectSlotWithQuantity(0) then
|
||||
error('No slots available')
|
||||
end
|
||||
turtle.equip(side)
|
||||
if not turtle.equip(side, item) then
|
||||
error('Unable to equip ' .. item)
|
||||
end
|
||||
end
|
||||
|
||||
turtle.select(1)
|
||||
end
|
||||
|
||||
equip('left', 'minecraft:diamond_sword')
|
||||
|
||||
equip('right', 'plethora:scanner', 'plethora:module:2')
|
||||
Equipper.equipLeft('minecraft:diamond_sword')
|
||||
Equipper.equipRight('plethora:module:2', 'plethora:scanner')
|
||||
local scanner = device['plethora:scanner']
|
||||
|
||||
turtle.reset()
|
||||
@@ -51,7 +30,7 @@ Util.filterInplace(data, function(b)
|
||||
end)
|
||||
local chest = Point.closest(spawner, data) or error('missing drop off chest')
|
||||
|
||||
equip('right', 'plethora:sensor', 'plethora:module:3')
|
||||
Equipper.equipRight('plethora:module:3', 'plethora:sensor')
|
||||
local sensor = device['plethora:sensor']
|
||||
|
||||
if not fs.exists(STARTUP_FILE) then
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
local Equipper = require('turtle.equipper')
|
||||
local GPS = require('gps')
|
||||
local Point = require('point')
|
||||
local Util = require('util')
|
||||
@@ -469,7 +470,7 @@ local function makeKey(b)
|
||||
end
|
||||
|
||||
local function findDroppedSaplings()
|
||||
equip('left', 'plethora:sensor', SENSOR)
|
||||
Equipper.equipLeft(SENSOR, 'plethora:sensor')
|
||||
local raw = peripheral.call('left', 'sense')
|
||||
|
||||
local sensed = Util.reduce(raw, function(acc, b)
|
||||
@@ -490,7 +491,7 @@ end
|
||||
local function scan(pt, filter, blocks)
|
||||
turtle.pathfind(pt)
|
||||
|
||||
equip('left', 'plethora:scanner', SCANNER)
|
||||
Equipper.equipLeft(SCANNER, 'plethora:scanner')
|
||||
local raw = peripheral.call('left', 'scan')
|
||||
|
||||
return Util.reduce(raw, function(acc, b)
|
||||
@@ -595,7 +596,7 @@ local function fell()
|
||||
local pt = Util.shallowCopy(HOME_PT)
|
||||
while Util.any(blocks, function(b) return b.y > pt.y + 6 end) do
|
||||
-- tree might be above low scan range, do a scan higher up
|
||||
equip('left', PICKAXE)
|
||||
Equipper.equipLeft(PICKAXE)
|
||||
pt.y = pt.y + 8
|
||||
blocks = scan(pt, filter, blocks)
|
||||
end
|
||||
@@ -605,7 +606,7 @@ local function fell()
|
||||
-- add any locations that need saplings
|
||||
getPlantLocations(blocks)
|
||||
|
||||
equip('left', PICKAXE)
|
||||
Equipper.equipLeft(PICKAXE)
|
||||
if not Util.empty(blocks) then
|
||||
print('Chopping')
|
||||
|
||||
@@ -657,12 +658,12 @@ end
|
||||
local function findHome()
|
||||
local pt = GPS.getPoint(2) or error('GPS not found')
|
||||
|
||||
equip('left', SCANNER)
|
||||
Equipper.equipLeft(SCANNER, 'plethora:scanner')
|
||||
|
||||
local facing = peripheral.call('left', 'getBlockMeta', 0, 0, 0).state.facing
|
||||
pt.heading = Point.facings[facing].heading
|
||||
|
||||
equip('left', PICKAXE)
|
||||
Equipper.equipLeft(PICKAXE)
|
||||
|
||||
if not state.home then
|
||||
setState('home', pt)
|
||||
@@ -716,8 +717,8 @@ local function updateClock()
|
||||
end
|
||||
|
||||
local function startupCheck()
|
||||
equip('right', 'modem', MODEM)
|
||||
equip('left', PICKAXE)
|
||||
Equipper.equipRight(MODEM, 'modem')
|
||||
Equipper.equipLeft(PICKAXE)
|
||||
|
||||
local slots = turtle.getSummedInventory()
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
{
|
||||
required = {
|
||||
'turtle',
|
||||
},
|
||||
title = 'Turtle mining programs',
|
||||
repository = 'kepler155c/opus-apps/{{OPUS_BRANCH}}/miners',
|
||||
description = [[Provides two types of automated mining:
|
||||
|
||||
@@ -136,7 +136,7 @@ local function run(member)
|
||||
for _ = 1, 3 do
|
||||
Sound.play('block.note.pling')
|
||||
os.sleep(.3)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
turtle.go({ y = pt.y })
|
||||
|
||||
@@ -9,11 +9,12 @@
|
||||
Plethora scanner
|
||||
Bucket
|
||||
--]]
|
||||
local Event = require('event')
|
||||
local GPS = require('gps')
|
||||
local Point = require('point')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
local Event = require('event')
|
||||
local Equipper = require('turtle.equipper')
|
||||
local GPS = require('gps')
|
||||
local Point = require('point')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
local fs = _G.fs
|
||||
@@ -316,26 +317,10 @@ local function collectDrops(suckAction)
|
||||
end
|
||||
end
|
||||
|
||||
local function equip(side, item)
|
||||
if not turtle.equip(side, item) then
|
||||
if not turtle.selectSlotWithQuantity(0) then
|
||||
ejectTrash()
|
||||
end
|
||||
if not turtle.selectSlotWithQuantity(0) then
|
||||
turtle.select(16)
|
||||
turtle.drop()
|
||||
end
|
||||
turtle.equip(side)
|
||||
if not turtle.equip(side, item) then
|
||||
error('Unable to equip ' .. item)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function scan()
|
||||
equip('left', 'plethora:module:2')
|
||||
Equipper.equipLeft('plethora:module:2', 'plethora:scanner')
|
||||
local blocks = peripheral.call('left', 'scan')
|
||||
equip('left', 'minecraft:diamond_pickaxe')
|
||||
Equipper.equipLeft('minecraft:diamond_pickaxe')
|
||||
local throttle = Util.throttle()
|
||||
|
||||
local bedrock = -256
|
||||
@@ -507,11 +492,8 @@ end
|
||||
|
||||
-- in plethora code, we can override initialize with a scanner version
|
||||
turtle.initialize = function()
|
||||
if turtle.isEquipped('modem') ~= 'right' then
|
||||
equip('right', 'computercraft:advanced_modem')
|
||||
end
|
||||
|
||||
equip('left', 'minecraft:diamond_pickaxe')
|
||||
Equipper.equipRight('computercraft:advanced_modem', 'modem')
|
||||
Equipper.equipLeft('minecraft:diamond_pickaxe')
|
||||
|
||||
local function verify(item)
|
||||
if not turtle.has(item) then
|
||||
@@ -526,11 +508,11 @@ turtle.initialize = function()
|
||||
|
||||
--os.sleep(5)
|
||||
local pt = GPS.getPoint(2) or error('GPS not found')
|
||||
equip('left', 'plethora:module')
|
||||
Equipper.equipLeft('plethora:module:2', 'plethora:scanner')
|
||||
local facing = peripheral.call('left', 'getBlockMeta', 0, 0, 0).state.facing
|
||||
pt.heading = Point.facings[facing].heading
|
||||
turtle.setPoint(pt, true)
|
||||
equip('left', 'minecraft:diamond_pickaxe')
|
||||
Equipper.equipLeft('minecraft:diamond_pickaxe')
|
||||
end
|
||||
|
||||
local function main()
|
||||
|
||||
109
turtle/apis/equipper.lua
Normal file
109
turtle/apis/equipper.lua
Normal file
@@ -0,0 +1,109 @@
|
||||
local peripheral = _G.peripheral
|
||||
local turtle = _G.turtle
|
||||
|
||||
local Equipper = { }
|
||||
|
||||
local SCANNER_EQUIPPED = 'plethora:scanner'
|
||||
local SCANNER_INV = 'plethora:module:2'
|
||||
|
||||
local reversed = {
|
||||
left = 'right',
|
||||
right = 'left'
|
||||
}
|
||||
|
||||
local function getEquipped()
|
||||
Equipper.equipped = { }
|
||||
Equipper.equipped.left = peripheral.getType('left')
|
||||
Equipper.equipped.right = peripheral.getType('right')
|
||||
|
||||
if not Equipper.equipped.left or not Equipper.equipped.right then
|
||||
-- try to detect non-peripheral type items - such as minecraft:diamond_pickaxe
|
||||
local side = Equipper.isEquipped(SCANNER_EQUIPPED)
|
||||
local meta
|
||||
if side then
|
||||
meta = peripheral.call(side, 'getBlockMeta', 0, 0, 0)
|
||||
|
||||
elseif turtle.has(SCANNER_INV) then
|
||||
local swapSide = peripheral.getType('right') == 'modem' and 'left' or 'right'
|
||||
turtle.equip(swapSide, SCANNER_INV)
|
||||
meta = peripheral.call(swapSide, 'getBlockMeta', 0, 0, 0)
|
||||
end
|
||||
|
||||
if meta then
|
||||
if not Equipper.equipped.left then
|
||||
Equipper.equipped.left = meta.turtle.left and meta.turtle.left.id
|
||||
end
|
||||
if not Equipper.equipped.right then
|
||||
Equipper.equipped.right = meta.turtle.right and meta.turtle.right.id
|
||||
end
|
||||
|
||||
elseif not Equipper.equipped.left then
|
||||
local slot = Equipper.uneqip('left')
|
||||
if slot then
|
||||
Equipper.equipped.left = slot.name .. ':' .. slot.damage
|
||||
end
|
||||
|
||||
elseif not Equipper.equipped.right then
|
||||
local slot = Equipper.uneqip('right')
|
||||
if slot then
|
||||
Equipper.equipped.right = slot.name .. ':' .. slot.damage
|
||||
end
|
||||
end
|
||||
end
|
||||
_debug("Detected Equipped")
|
||||
_debug(Equipper.equipped)
|
||||
end
|
||||
|
||||
function Equipper.unequip(side)
|
||||
local slot = turtle.selectOpenSlot()
|
||||
if not slot then
|
||||
error('No slots available')
|
||||
end
|
||||
turtle.equip(side)
|
||||
Equipper.equipped[side] = nil
|
||||
return turtle.getItemDetail(slot)
|
||||
end
|
||||
|
||||
function Equipper.isEquipped(name)
|
||||
if not Equipper.equipped then
|
||||
getEquipped()
|
||||
end
|
||||
|
||||
return Equipper.equipped.left == name and 'left' or
|
||||
Equipper.equipped.right == name and 'right'
|
||||
end
|
||||
|
||||
function Equipper.equip(side, invName, equippedName)
|
||||
if not Equipper.equipped then
|
||||
getEquipped()
|
||||
end
|
||||
|
||||
-- is it already equipped ?
|
||||
if Equipper.equipped[side] == (equippedName or invName) then
|
||||
return
|
||||
end
|
||||
-- is it equipped on other side ?
|
||||
if Equipper.equipped[reversed[side]] == (equippedName or invName) then
|
||||
Equipper.unequip(reversed[side])
|
||||
end
|
||||
|
||||
local s, m = turtle.equip(side, invName)
|
||||
if not s then
|
||||
error(string.format('Unable to equip %s\n%s', (equippedName or invName), m))
|
||||
end
|
||||
|
||||
Equipper.equipped[side] = peripheral.getType(side) or invName
|
||||
|
||||
_debug("Equipped: " .. invName)
|
||||
_debug(Equipper.equipped)
|
||||
end
|
||||
|
||||
function Equipper.equipLeft(invName, equippedName)
|
||||
Equipper.equip('left', invName, equippedName)
|
||||
end
|
||||
|
||||
function Equipper.equipRight(invName, equippedName)
|
||||
Equipper.equip('right', invName, equippedName)
|
||||
end
|
||||
|
||||
return Equipper
|
||||
@@ -1,7 +1,9 @@
|
||||
local Point = require('point')
|
||||
local Equipper = require('turtle.equipper')
|
||||
local Point = require('point')
|
||||
|
||||
local device = _G.device
|
||||
local turtle = _G.turtle
|
||||
local device = _G.device
|
||||
local peripheral = _G.device
|
||||
local turtle = _G.turtle
|
||||
|
||||
local MAX_FUEL = turtle.getFuelLimit()
|
||||
|
||||
@@ -13,9 +15,10 @@ if not turtle.has('minecraft:bucket') then
|
||||
error('bucket required')
|
||||
end
|
||||
|
||||
local scanner = device['plethora:scanner'] or
|
||||
turtle.equip('left', 'plethora:module:2') and device['plethora:scanner'] or
|
||||
error('Plethora scanner required')
|
||||
local swapSide = peripheral.getType('right') == 'modem' and 'left' or 'right'
|
||||
Equipper.equip(swapSide, 'plethora:module:2', 'plethora:scanner')
|
||||
|
||||
local scanner = device['plethora:scanner']
|
||||
|
||||
if not turtle.select('minecraft:bucket') then
|
||||
error('bucket required')
|
||||
@@ -60,7 +63,7 @@ turtle.gotoY(0)
|
||||
turtle.go({ x = 0, y = 0, z = 0 })
|
||||
|
||||
turtle.set({ status = 'idle' })
|
||||
turtle.unequip('left')
|
||||
turtle.unequip(swapSide)
|
||||
print('Fuel: ' .. turtle.getFuelLevel())
|
||||
|
||||
if not s and m then
|
||||
|
||||
Reference in New Issue
Block a user