spaces->tab, equipper improvements, supertreefarm rewrite, follow improvements, sensor cleanup, milo multiple items allowed in recipes, remote canvas access

This commit is contained in:
kepler155c@gmail.com
2019-06-18 15:23:20 -04:00
parent 3b9b509429
commit 045b32884f
162 changed files with 20448 additions and 20286 deletions

View File

@@ -1,117 +1,147 @@
local Config = require('config')
local peripheral = _G.peripheral
local turtle = _G.turtle
local Equipper = { }
local equipmentList = Config.load('equipment', {
[ 'plethora:scanner' ] = 'plethora:module:2',
[ 'plethora:sensor' ] = 'plethora:module:3',
[ 'plethora:laser' ] = 'plethora:module:1',
[ 'plethora:introspection' ] = 'plethora:module:0',
[ 'plethora:kinetic' ] = 'plethora:module:4',
[ 'advanced_modem' ] = 'computercraft:advanced_modem:0',
[ 'standard_modem' ] = 'computercraft:peripheral:1',
})
local SCANNER_EQUIPPED = 'plethora:scanner'
local SCANNER_INV = 'plethora:module:2'
local SCANNER_INV = equipmentList[SCANNER_EQUIPPED] or 'unknown'
local reversed = {
left = 'right',
right = 'left'
left = 'right',
right = 'left'
}
local function getEquipped()
Equipper.equipped = { }
Equipper.equipped.left = peripheral.getType('left')
Equipper.equipped.right = peripheral.getType('right')
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)
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)
Equipper.equipped[swapSide] = 'plethora:scanner'
meta = peripheral.call(swapSide, 'getBlockMeta', 0, 0, 0)
end
elseif turtle.has(SCANNER_INV) then
local swapSide = peripheral.getType('right') == 'modem' and 'left' or 'right'
turtle.equip(swapSide, SCANNER_INV)
Equipper.equipped[swapSide] = 'plethora:scanner'
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
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.unequip('left')
if slot then
turtle.equip('left', slot.name .. ':' .. slot.damage)
Equipper.equipped.left = slot.name .. ':' .. slot.damage
end
elseif not Equipper.equipped.left then
local slot = Equipper.unequip('left')
if slot then
turtle.equip('left', slot.name .. ':' .. slot.damage)
Equipper.equipped.left = slot.name .. ':' .. slot.damage
end
elseif not Equipper.equipped.right then
local slot = Equipper.unequip('right')
if slot then
turtle.equip('right', slot.name .. ':' .. slot.damage)
Equipper.equipped.right = slot.name .. ':' .. slot.damage
end
end
end
elseif not Equipper.equipped.right then
local slot = Equipper.unequip('right')
if slot then
turtle.equip('right', slot.name .. ':' .. slot.damage)
Equipper.equipped.right = slot.name .. ':' .. slot.damage
end
end
end
end
local function matches(left, right)
-- return a match for 'minecraft:diamond_sword:0' with 'minecraft:diamond_sword'
if left and right then
return left:match(right)
end
-- return a match for 'minecraft:diamond_sword:0' with 'minecraft:diamond_sword'
if left and right then
return left:match(right)
end
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)
local slot = turtle.selectOpenSlot()
if not slot then
error('No slots available')
end
turtle.equip(side)
if Equipper.equipped then
Equipper.equipped[side] = nil
end
return turtle.getItemDetail(slot)
end
function Equipper.isEquipped(name)
if not Equipper.equipped then
getEquipped()
end
if not Equipper.equipped then
getEquipped()
end
return Equipper.equipped.left == name and 'left' or
Equipper.equipped.right == name and 'right'
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 matches(Equipper.equipped[side], equippedName or invName) then
return peripheral.getType(side) and peripheral.wrap(side)
end
-- is it equipped on other side ?
if matches(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
return peripheral.getType(side) and peripheral.wrap(side)
-- so convoluted - needs it's own function
function Equipper.equipModem(side)
if peripheral.getType(side) ~= 'modem' then
if peripheral.getType(reversed[side]) then
Equipper.unequip(reversed[side])
end
if turtle.has(equipmentList['advanced_modem']) then
return Equipper.equip(side, equipmentList['advanced_modem'])
end
if turtle.has(equipmentList['standard_modem']) then
return Equipper.equip(side, equipmentList['standard_modem'])
end
error('Missing modem')
end
end
function Equipper.equipLeft(invName, equippedName)
return Equipper.equip('left', invName, equippedName)
function Equipper.equip(side, item)
if not Equipper.equipped then
getEquipped()
end
-- is it already equipped ?
if matches(Equipper.equipped[side], item) then
return peripheral.getType(side) and peripheral.wrap(side)
end
-- is it equipped on other side ?
if matches(Equipper.equipped[reversed[side]], item) then
Equipper.unequip(reversed[side])
end
local s, m = turtle.equip(side, equipmentList[item] or item)
if not s then
error(string.format('Unable to equip %s\n%s', item, m))
end
Equipper.equipped[side] = peripheral.getType(side) or item
return peripheral.getType(side) and peripheral.wrap(side)
end
function Equipper.equipRight(invName, equippedName)
return Equipper.equip('right', invName, equippedName)
function Equipper.equipLeft(item)
return Equipper.equip('left', item)
end
function Equipper.equipRight(item)
return Equipper.equip('right', item)
end
return Equipper

View File

@@ -1,5 +1,5 @@
if not _G.turtle then
return
return
end
-- update
@@ -20,37 +20,37 @@ fs.mount('sys/apps/system/turtle.lua', 'linkfs', 'packages/turtle/system/turtle.
-- provide a turtle function for scanning
function turtle.scan(whitelist, blacklist)
local pt = turtle.point
local pt = turtle.point
local scanner = device['plethora:scanner'] or error('Scanner not equipped')
local scanner = device['plethora:scanner'] or error('Scanner not equipped')
if not whitelist and not blacklist then
return Util.each(scanner.scan(), function(b)
b.x = pt.x + b.x
b.y = pt.y + b.y
b.z = pt.z + b.z
end)
end
if not whitelist and not blacklist then
return Util.each(scanner.scan(), function(b)
b.x = pt.x + b.x
b.y = pt.y + b.y
b.z = pt.z + b.z
end)
end
if whitelist then
return Util.filter(scanner.scan(), function(b)
if whitelist[b.name] then
b.x = pt.x + b.x
b.y = pt.y + b.y
b.z = pt.z + b.z
return true
end
end)
end
if whitelist then
return Util.filter(scanner.scan(), function(b)
if whitelist[b.name] then
b.x = pt.x + b.x
b.y = pt.y + b.y
b.z = pt.z + b.z
return true
end
end)
end
return Util.filter(scanner.scan(), function(b)
if not blacklist[b.name] then
b.x = pt.x + b.x
b.y = pt.y + b.y
b.z = pt.z + b.z
return true
end
end)
return Util.filter(scanner.scan(), function(b)
if not blacklist[b.name] then
b.x = pt.x + b.x
b.y = pt.y + b.y
b.z = pt.z + b.z
return true
end
end)
end
local function getHeading(apt)
@@ -62,34 +62,34 @@ local function getHeading(apt)
local bpt
repeat
if not turtle.inspect() and turtle.forward() then
bpt = GPS.locate()
if not turtle.inspect() and turtle.forward() then
bpt = GPS.locate()
break
end
turtle.turnRight()
until turtle.getHeading() == heading
until turtle.getHeading() == heading
if not bpt then
repeat
if not peripheral.getType('front') then
turtle.dig()
if turtle.forward() then
bpt = GPS.locate()
break
end
end
turtle.turnRight()
until turtle.point.heading == heading
end
if not bpt then
repeat
if not peripheral.getType('front') then
turtle.dig()
if turtle.forward() then
bpt = GPS.locate()
break
end
end
turtle.turnRight()
until turtle.point.heading == heading
end
if not bpt then
return false
end
if not bpt then
return false
end
local turns = (turtle.point.heading - heading) % 4
local turns = (turtle.point.heading - heading) % 4
turtle.back()
turtle.setHeading(heading)
turtle.back()
turtle.setHeading(heading)
if apt.x < bpt.x then
return (0 - turns) % 4
@@ -135,19 +135,19 @@ end
-- return to home location if configured to do so
if _G.device.wireless_modem then
local config = Config.load('gps')
local config = Config.load('gps')
if config.home then
if not turtle.enableGPS(2) then
error('Unable to get GPS position')
end
if config.home then
if not turtle.enableGPS(2) then
error('Unable to get GPS position')
end
if config.destructive then
turtle.set({ attackPolicy = 'attack', digPolicy = 'turtleSafe' })
end
if config.destructive then
turtle.set({ attackPolicy = 'attack', digPolicy = 'turtleSafe' })
end
if not turtle.pathfind(config.home) then
error('Failed to return home')
end
end
if not turtle.pathfind(config.home) then
error('Failed to return home')
end
end
end

View File

@@ -15,7 +15,7 @@ if not turtle.has('minecraft:bucket') then
end
local swapSide = peripheral.getType('right') == 'modem' and 'left' or 'right'
local scanner = Equipper.equip(swapSide, 'plethora:module:2', 'plethora:scanner')
local scanner = Equipper.equip(swapSide, 'plethora:scanner')
if not turtle.select('minecraft:bucket') then
error('bucket required')

View File

@@ -8,99 +8,99 @@ local checkedNodes, nodes
local function addNode(node)
for i = 0, 3 do
local hi = turtle.getHeadingInfo(i)
local testNode = { x = node.x + hi.xd, z = node.z + hi.zd }
for i = 0, 3 do
local hi = turtle.getHeadingInfo(i)
local testNode = { x = node.x + hi.xd, z = node.z + hi.zd }
local key = table.concat({ testNode.x, testNode.z }, ':')
if not checkedNodes[key] then
nodes[key] = testNode
end
end
local key = table.concat({ testNode.x, testNode.z }, ':')
if not checkedNodes[key] then
nodes[key] = testNode
end
end
end
local function findObsidian()
repeat
local node = { x = turtle.point.x, z = turtle.point.z }
local key = table.concat({ node.x, node.z }, ':')
repeat
local node = { x = turtle.point.x, z = turtle.point.z }
local key = table.concat({ node.x, node.z }, ':')
checkedNodes[key] = true
nodes[key] = nil
checkedNodes[key] = true
nodes[key] = nil
local _,b = turtle.inspectDown()
if b and (b.name == 'minecraft:lava' or b.name == 'minecraft:flowing_lava') then
if turtle.select('minecraft:water_bucket') then
while true do
if turtle.up() then
break
end
print('stuck')
end
turtle.placeDown()
os.sleep(2)
turtle.placeDown()
turtle.down()
turtle.select(1)
_, b = turtle.inspectDown()
end
end
local _,b = turtle.inspectDown()
if b and (b.name == 'minecraft:lava' or b.name == 'minecraft:flowing_lava') then
if turtle.select('minecraft:water_bucket') then
while true do
if turtle.up() then
break
end
print('stuck')
end
turtle.placeDown()
os.sleep(2)
turtle.placeDown()
turtle.down()
turtle.select(1)
_, b = turtle.inspectDown()
end
end
if turtle.getItemCount(16) > 0 then
print('Inventory full')
print('Enter to continue...')
_G.read()
end
if turtle.getItemCount(16) > 0 then
print('Inventory full')
print('Enter to continue...')
_G.read()
end
if b and b.name == 'minecraft:obsidian' then
turtle.digDown()
addNode(node)
else
turtle.digDown()
end
if b and b.name == 'minecraft:obsidian' then
turtle.digDown()
addNode(node)
else
turtle.digDown()
end
print(string.format('%d nodes remaining', Util.size(nodes)))
print(string.format('%d nodes remaining', Util.size(nodes)))
if Util.size(nodes) == 0 then
break
end
if Util.size(nodes) == 0 then
break
end
node = Point.closest(turtle.point, nodes)
if not turtle.go(node) then
break
end
until turtle.isAborted()
node = Point.closest(turtle.point, nodes)
if not turtle.go(node) then
break
end
until turtle.isAborted()
end
turtle.run(function()
turtle.reset()
turtle.set({ digPolicy = 'dig' })
turtle.reset()
turtle.set({ digPolicy = 'dig' })
local s, m = pcall(function()
repeat
checkedNodes = { }
nodes = { }
local s, m = pcall(function()
repeat
checkedNodes = { }
nodes = { }
local _,b = turtle.inspectDown()
if not b or b.name ~= 'minecraft:obsidian' then
break
end
local _,b = turtle.inspectDown()
if not b or b.name ~= 'minecraft:obsidian' then
break
end
findObsidian()
if not turtle.select('minecraft:water_bucket') then
break
end
turtle.go({ x = 0, z = 0 })
turtle.placeDown()
os.sleep(2)
turtle.placeDown()
turtle.down()
turtle.select(1)
until turtle.isAborted()
end)
findObsidian()
if not turtle.select('minecraft:water_bucket') then
break
end
turtle.go({ x = 0, z = 0 })
turtle.placeDown()
os.sleep(2)
turtle.placeDown()
turtle.down()
turtle.select(1)
until turtle.isAborted()
end)
if not s and m then
error(m)
end
if not s and m then
error(m)
end
turtle.go({ x = 0, y = 0, z = 0, heading = 0 })
turtle.go({ x = 0, y = 0, z = 0, heading = 0 })
end)

View File

@@ -2,92 +2,92 @@ local turtle = _G.turtle
local function doCommand(command, moves)
local function format(value)
if type(value) == 'boolean' then
if value then return 'true' end
return 'false'
end
if type(value) ~= 'table' then
return value
end
local str
for k,v in pairs(value) do
if not str then
str = '{ '
else
str = str .. ', '
end
str = str .. k .. '=' .. tostring(v)
end
if str then
str = str .. ' }'
else
str = '{ }'
end
local function format(value)
if type(value) == 'boolean' then
if value then return 'true' end
return 'false'
end
if type(value) ~= 'table' then
return value
end
local str
for k,v in pairs(value) do
if not str then
str = '{ '
else
str = str .. ', '
end
str = str .. k .. '=' .. tostring(v)
end
if str then
str = str .. ' }'
else
str = '{ }'
end
return str
end
return str
end
local function runCommand(fn, arg)
local r = { fn(arg) }
if r[2] then
print(format(r[1]) .. ': ' .. format(r[2]))
elseif r[1] then
print(format(r[1]))
end
return r[1]
end
local function runCommand(fn, arg)
local r = { fn(arg) }
if r[2] then
print(format(r[1]) .. ': ' .. format(r[2]))
elseif r[1] then
print(format(r[1]))
end
return r[1]
end
local cmds = {
[ 's' ] = turtle.select,
[ 'rf' ] = turtle.refuel,
[ 'gh' ] = function() turtle.pathfind({ x = 0, y = 0, z = 0, heading = 0}) end,
}
local cmds = {
[ 's' ] = turtle.select,
[ 'rf' ] = turtle.refuel,
[ 'gh' ] = function() turtle.pathfind({ x = 0, y = 0, z = 0, heading = 0}) end,
}
local repCmds = {
[ 'u' ] = turtle.up,
[ 'd' ] = turtle.down,
[ 'f' ] = turtle.forward,
[ 'r' ] = turtle.turnRight,
[ 'l' ] = turtle.turnLeft,
[ 'ta' ] = turtle.turnAround,
[ 'el' ] = turtle.equipLeft,
[ 'er' ] = turtle.equipRight,
[ 'DD' ] = turtle.digDown,
[ 'DU' ] = turtle.digUp,
[ 'D' ] = turtle.dig,
[ 'p' ] = turtle.place,
[ 'pu' ] = turtle.placeUp,
[ 'pd' ] = turtle.placeDown,
[ 'b' ] = turtle.back,
[ 'gfl' ] = turtle.getFuelLevel,
[ 'gp' ] = turtle.getPoint,
[ 'R' ] = function() turtle.setPoint({x = 0, y = 0, z = 0, heading = 0}) return turtle.point end
}
local repCmds = {
[ 'u' ] = turtle.up,
[ 'd' ] = turtle.down,
[ 'f' ] = turtle.forward,
[ 'r' ] = turtle.turnRight,
[ 'l' ] = turtle.turnLeft,
[ 'ta' ] = turtle.turnAround,
[ 'el' ] = turtle.equipLeft,
[ 'er' ] = turtle.equipRight,
[ 'DD' ] = turtle.digDown,
[ 'DU' ] = turtle.digUp,
[ 'D' ] = turtle.dig,
[ 'p' ] = turtle.place,
[ 'pu' ] = turtle.placeUp,
[ 'pd' ] = turtle.placeDown,
[ 'b' ] = turtle.back,
[ 'gfl' ] = turtle.getFuelLevel,
[ 'gp' ] = turtle.getPoint,
[ 'R' ] = function() turtle.setPoint({x = 0, y = 0, z = 0, heading = 0}) return turtle.point end
}
if cmds[command] then
runCommand(cmds[command], moves)
elseif repCmds[command] then
for _ = 1, moves do
if not runCommand(repCmds[command]) then
break
end
end
end
if cmds[command] then
runCommand(cmds[command], moves)
elseif repCmds[command] then
for _ = 1, moves do
if not runCommand(repCmds[command]) then
break
end
end
end
end
local args = {...}
if #args > 0 then
doCommand(args[1], args[2] or 1)
doCommand(args[1], args[2] or 1)
else
print('Enter command (q to quit):')
while true do
local cmd = _G.read()
if cmd == 'q' then break
end
args = { }
cmd:gsub('%w+', function(w) table.insert(args, w) end)
doCommand(args[1], args[2] or 1)
end
print('Enter command (q to quit):')
while true do
local cmd = _G.read()
if cmd == 'q' then break
end
args = { }
cmd:gsub('%w+', function(w) table.insert(args, w) end)
doCommand(args[1], args[2] or 1)
end
end