1
0
mirror of https://github.com/kepler155c/opus synced 2026-06-03 19:22:10 +00:00

treefarm + turtle improvements + cleanup

This commit is contained in:
kepler155c@gmail.com
2017-09-12 23:04:44 -04:00
parent e50e6da700
commit 9aca96cc3e
21 changed files with 3652 additions and 1190 deletions
-224
View File
@@ -1,224 +0,0 @@
if not turtle or turtle.pathfind then
return
end
requireInjector(getfenv(1))
local Grid = require ("jumper.grid")
local Pathfinder = require ("jumper.pathfinder")
local Point = require('point')
local WALKABLE = 0
local function createMap(dim)
local map = { }
for z = 0, dim.ez do
local row = {}
for x = 0, dim.ex do
local col = { }
for y = 0, dim.ey do
table.insert(col, WALKABLE)
end
table.insert(row, col)
end
table.insert(map, row)
end
return map
end
local function addBlock(map, dim, b)
map[b.z + dim.oz][b.x + dim.ox][b.y + dim.oy] = 1
end
-- map shrinks/grows depending upon blocks encountered
-- the map will encompass any blocks encountered, the turtle position, and the destination
local function mapDimensions(dest, blocks, boundingBox)
local sx, sz, sy = turtle.point.x, turtle.point.z, turtle.point.y
local ex, ez, ey = turtle.point.x, turtle.point.z, turtle.point.y
local function adjust(pt)
if pt.x < sx then
sx = pt.x
end
if pt.z < sz then
sz = pt.z
end
if pt.y < sy then
sy = pt.y
end
if pt.x > ex then
ex = pt.x
end
if pt.z > ez then
ez = pt.z
end
if pt.y > ey then
ey = pt.y
end
end
adjust(dest)
for _,b in ipairs(blocks) do
adjust(b)
end
-- expand one block out in all directions
sx = math.max(sx - 1, boundingBox.sx)
sz = math.max(sz - 1, boundingBox.sz)
sy = math.max(sy - 1, boundingBox.sy)
ex = math.min(ex + 1, boundingBox.ex)
ez = math.min(ez + 1, boundingBox.ez)
ey = math.min(ey + 1, boundingBox.ey)
return {
ex = ex - sx + 1,
ez = ez - sz + 1,
ey = ey - sy + 1,
ox = -sx + 1,
oz = -sz + 1,
oy = -sy + 1
}
end
local function nodeToString(n)
return string.format('%d:%d:%d:%d', n._x, n._y, n._z, n.__heading or 9)
end
-- shifting and coordinate flipping
local function pointToMap(dim, pt)
return { x = pt.x + dim.ox, z = pt.y + dim.oy, y = pt.z + dim.oz }
end
local function nodeToPoint(dim, node)
return { x = node:getX() - dim.ox, z = node:getY() - dim.oz, y = node:getZ() - dim.oy }
end
local heuristic = function(n, node)
local m, h = Point.calculateMoves(
{ x = node._x, z = node._y, y = node._z, heading = node._heading },
{ x = n._x, z = n._y, y = n._z, heading = n._heading })
return m, h
end
local function dimsAreEqual(d1, d2)
return d1.ex == d2.ex and
d1.ey == d2.ey and
d1.ez == d2.ez and
d1.ox == d2.ox and
d1.oy == d2.oy and
d1.oz == d2.oz
end
-- turtle sensor returns blocks in relation to the world - not turtle orientation
-- so cannot figure out block location unless we know our orientation in the world
-- really kinda dumb since it returns the coordinates as offsets of our location
-- instead of true coordinates
local function addSensorBlocks(blocks, sblocks)
for _,b in pairs(sblocks) do
if b.type ~= 'AIR' then
local pt = { x = turtle.point.x, y = turtle.point.y + b.y, z = turtle.point.z }
pt.x = pt.x - b.x
pt.z = pt.z - b.z -- this will only work if we were originally facing west
local found = false
for _,ob in pairs(blocks) do
if pt.x == ob.x and pt.y == ob.y and pt.z == ob.z then
found = true
break
end
end
if not found then
table.insert(blocks, pt)
end
end
end
end
local function pathTo(dest, blocks, maxRadius)
blocks = blocks or { }
maxRadius = maxRadius or 1000000
local lastDim = nil
local map = nil
local grid = nil
local boundingBox = {
sx = math.min(turtle.point.x, dest.x) - maxRadius,
sy = math.min(turtle.point.y, dest.y) - maxRadius,
sz = math.min(turtle.point.z, dest.z) - maxRadius,
ex = math.max(turtle.point.x, dest.x) + maxRadius,
ey = math.max(turtle.point.y, dest.y) + maxRadius,
ez = math.max(turtle.point.z, dest.z) + maxRadius,
}
-- Creates a pathfinder object
local myFinder = Pathfinder(grid, 'ASTAR', walkable)
myFinder:setMode('ORTHOGONAL')
myFinder:setHeuristic(heuristic)
while turtle.point.x ~= dest.x or turtle.point.z ~= dest.z or turtle.point.y ~= dest.y do
-- map expands as we encounter obstacles
local dim = mapDimensions(dest, blocks, boundingBox)
-- reuse map if possible
if not lastDim or not dimsAreEqual(dim, lastDim) then
map = createMap(dim)
-- Creates a grid object
grid = Grid(map)
myFinder:setGrid(grid)
myFinder:setWalkable(WALKABLE)
lastDim = dim
end
for _,b in ipairs(blocks) do
addBlock(map, dim, b)
end
-- Define start and goal locations coordinates
local startPt = pointToMap(dim, turtle.point)
local endPt = pointToMap(dim, dest)
-- Calculates the path, and its length
local path = myFinder:getPath(startPt.x, startPt.y, startPt.z, turtle.point.heading, endPt.x, endPt.y, endPt.z, dest.heading)
if not path then
return false, 'failed to recalculate'
end
for node, count in path:nodes() do
local pt = nodeToPoint(dim, node)
if turtle.abort then
return false, 'aborted'
end
-- use single turn method so the turtle doesn't turn around when encountering obstacles
if not turtle.gotoSingleTurn(pt.x, pt.z, pt.y) then
table.insert(blocks, pt)
--if device.turtlesensorenvironment then
-- addSensorBlocks(blocks, device.turtlesensorenvironment.sonicScan())
--end
break
end
end
end
if dest.heading then
turtle.setHeading(dest.heading)
end
return true
end
turtle.pathfind = function(dest, blocks, maxRadius)
if not blocks and turtle.gotoPoint(dest) then
return true
end
return pathTo(dest, blocks, maxRadius)
end
-86
View File
@@ -1,86 +0,0 @@
if not turtle or turtle.abortAction then
return
end
requireInjector(getfenv(1))
local Util = require('util')
local Scheduler = {
uid = 0,
queue = { },
idle = true,
}
function turtle.abortAction()
if turtle.status ~= 'idle' then
turtle.abort = true
os.queueEvent('turtle_abort')
end
Util.clear(Scheduler.queue)
os.queueEvent('turtle_ticket', 0, true)
end
local function getTicket(fn, ...)
Scheduler.uid = Scheduler.uid + 1
if Scheduler.idle then
Scheduler.idle = false
turtle.status = 'busy'
os.queueEvent('turtle_ticket', Scheduler.uid)
else
table.insert(Scheduler.queue, Scheduler.uid)
end
return Scheduler.uid
end
local function releaseTicket(id)
for k,v in ipairs(Scheduler.queue) do
if v == id then
table.remove(Scheduler.queue, k)
return
end
end
local id = table.remove(Scheduler.queue, 1)
if id then
os.queueEvent('turtle_ticket', id)
else
Scheduler.idle = true
turtle.status = 'idle'
end
end
function turtle.run(fn, ...)
local ticketId = getTicket()
if type(fn) == 'string' then
fn = turtle[fn]
end
while true do
local e, id, abort = os.pullEventRaw('turtle_ticket')
if e == 'terminate' then
releaseTicket(ticketId)
os.queueEvent('turtle_response')
error('Terminated')
end
if abort then
-- the function was queued, but the queue was cleared
os.queueEvent('turtle_response')
return false, 'aborted'
end
if id == ticketId then
turtle.abort = false
turtle.resetState()
local args = { ... }
local s, m = pcall(function() fn(unpack(args)) end)
turtle.abort = false
releaseTicket(ticketId)
os.queueEvent('turtle_response')
if not s and m then
printError(m)
end
return s, m
end
end
end
+13 -5
View File
@@ -3,7 +3,9 @@ if not turtle or turtle.enableGPS then
end
requireInjector(getfenv(1))
local GPS = require('gps')
local GPS = require('gps')
local Config = require('config')
function turtle.enableGPS(timeout)
if turtle.point.gps then
@@ -18,17 +20,23 @@ function turtle.enableGPS(timeout)
end
function turtle.gotoGPSHome()
local homePt = turtle.loadLocation('gpsHome')
if homePt then
local config = { }
Config.load('gps', config)
if config.home then
if turtle.enableGPS() then
turtle.pathfind(homePt)
turtle.pathfind(config.home)
end
end
end
function turtle.setGPSHome()
local config = { }
Config.load('gps', config)
if turtle.enableGPS() then
turtle.storeLocation('gpsHome', turtle.point)
config.home = turtle.point
Config.update('gps', config)
turtle.gotoPoint(turtle.point)
end
end
+321 -37
View File
@@ -4,7 +4,10 @@ end
requireInjector(getfenv(1))
local Util = require('util')
local Point = require('point')
local synchronized = require('sync')
local Util = require('util')
turtle.pathfind = require('turtle.pathfind')
local function noop() end
@@ -147,6 +150,29 @@ function turtle.getHeadingInfo(heading)
end
-- [[ Basic turtle actions ]] --
local function inventoryAction(fn, name, qty)
local slots = turtle.getFilledSlots()
local s
for _,slot in pairs(slots) do
if slot.key == name or slot.name == name then
turtle.native.select(slot.index)
if not qty then
s = fn()
else
s = fn(math.min(qty, slot.count))
qty = qty - slot.count
if qty < 0 then
break
end
end
end
end
if not s then
return false, 'No items found'
end
return s
end
local function _attack(action)
if action.attack() then
repeat until not action.attack()
@@ -193,25 +219,24 @@ function turtle.place(slot) return _place(actions.forward, slot) end
function turtle.placeUp(slot) return _place(actions.up, slot) end
function turtle.placeDown(slot) return _place(actions.down, slot) end
local function _drop(action, count, indexOrId)
if indexOrId then
local slot = turtle.getSlot(indexOrId)
if not slot or slot.qty == 0 then
return false, 'No items to drop'
end
turtle.select(slot.index)
local function _drop(action, qtyOrName, qty)
if not qtyOrName or type(qtyOrName) == 'number' then
return action.drop(qtyOrName)
end
if not count then
return action.drop() -- wtf
end
return action.drop(count)
return inventoryAction(action.drop, qtyOrName, qty)
end
function turtle.drop(count, slot) return _drop(actions.forward, count, slot) end
function turtle.dropUp(count, slot) return _drop(actions.up, count, slot) end
function turtle.dropDown(count, slot) return _drop(actions.down, count, slot) end
function turtle.refuel(qtyOrName, qty)
if not qtyOrName or type(qtyOrName) == 'number' then
return turtle.native.refuel(qtyOrName)
end
return inventoryAction(turtle.native.refuel, qtyOrName, qty)
end
--[[
function turtle.dig() return state.dig(actions.forward) end
function turtle.digUp() return state.dig(actions.up) end
@@ -277,7 +302,7 @@ turtle.movePolicies = {
return false
end
local oldStatus = turtle.status
print('stuck')
print('assured move: stuck')
turtle.status = 'stuck'
repeat
os.sleep(1)
@@ -328,6 +353,7 @@ function turtle.setDigPolicy(policy) state.digPolicy = policy end
function turtle.setAttackPolicy(policy) state.attackPolicy = policy end
function turtle.setMoveCallback(cb) state.moveCallback = cb end
function turtle.clearMoveCallback() state.moveCallback = noop end
function turtle.getMoveCallback() return state.moveCallback end
-- [[ Locations ]] --
function turtle.getLocation(name)
@@ -751,31 +777,47 @@ function turtle.getSlot(indexOrId, slots)
local detail = turtle.getItemDetail(indexOrId)
if detail then
return {
name = detail.name,
damage = detail.damage,
count = detail.count,
key = detail.name .. ':' .. detail.damage,
index = indexOrId,
-- deprecate
qty = detail.count,
dmg = detail.damage,
id = detail.name,
iddmg = detail.name .. ':' .. detail.damage,
index = indexOrId,
}
end
return {
qty = 0,
qty = 0, -- deprecate
count = 0,
index = indexOrId,
}
end
function turtle.selectSlot(indexOrId)
function turtle.select(indexOrId)
if type(indexOrId) == 'number' then
return turtle.native.select(indexOrId)
end
local s = turtle.getSlot(indexOrId)
if s then
turtle.select(s.index)
turtle.native.select(s.index)
return s
end
return false, 'Inventory does not contain item'
end
function turtle.selectSlot(indexOrId) -- deprecated
return turtle.select(indexOrId)
end
function turtle.getInventory(slots)
slots = slots or { }
for i = 1, 16 do
@@ -784,11 +826,38 @@ function turtle.getInventory(slots)
return slots
end
function turtle.getSummedInventory()
local slots = turtle.getFilledSlots()
local t = { }
for _,slot in pairs(slots) do
local entry = t[slot.iddmg]
if not entry then
entry = {
count = 0,
damage = slot.damage,
name = slot.name,
key = slot.key,
-- deprecate
qty = 0,
dmg = slot.dmg,
id = slot.id,
iddmg = slot.iddmg,
}
t[slot.iddmg] = entry
end
entry.qty = entry.qty + slot.qty
entry.count = entry.qty
end
return t
end
function turtle.emptyInventory(dropAction)
dropAction = dropAction or turtle.drop
for i = 1, 16 do
turtle.emptySlot(i, dropAction)
end
turtle.select(1)
end
function turtle.emptySlot(slot, dropAction)
@@ -857,28 +926,243 @@ function turtle.selectSlotWithQuantity(qty, startSlot)
end
end
function turtle.condense(startSlot)
startSlot = startSlot or 1
local aslots = turtle.getInventory()
for _,slot in ipairs(aslots) do
if slot.qty < 64 then
for i = slot.index + 1, 16 do
local fslot = aslots[i]
if fslot.qty > 0 then
if slot.qty == 0 or slot.iddmg == fslot.iddmg then
turtle.select(fslot.index)
turtle.transferTo(slot.index, 64)
local transferred = turtle.getItemCount(slot.index) - slot.qty
slot.qty = slot.qty + transferred
fslot.qty = fslot.qty - transferred
slot.iddmg = fslot.iddmg
if slot.qty == 64 then
break
end
function turtle.condense()
local slots = turtle.getInventory()
for i = 16, 1, -1 do
if slots[i].count > 0 then
for j = 1, i - 1 do
if slots[j].count == 0 or slots[i].key == slots[j].key then
turtle.select(i)
turtle.transferTo(j, 64)
local transferred = slots[i].qty - turtle.getItemCount(i)
slots[j].count = slots[j].count + transferred
slots[i].count = slots[i].count - transferred
slots[j].key = slots[i].key
if slots[i].count == 0 then
break
end
end
end
end
end
return true
end
function turtle.getItemCount(idOrName)
if type(idOrName) == 'number' then
return turtle.native.getItemCount(idOrName)
end
local slots = turtle.getFilledSlots()
local count = 0
for _,slot in pairs(slots) do
if slot.iddmg == idOrName or slot.name == idOrName then
count = count + slot.qty
end
end
return count
end
function turtle.equip(side, item)
if item then
if not turtle.select(item) then
return false, 'Unable to equip ' .. item
end
end
if side == 'left' then
return turtle.equipLeft()
end
return turtle.equipRight()
end
function turtle.run(fn, ...)
local args = { ... }
local s, m
if type(fn) == 'string' then
fn = turtle[fn]
end
synchronized(turtle, function()
turtle.abort = false
turtle.status = 'busy'
turtle.resetState()
s, m = pcall(function() fn(unpack(args)) end)
turtle.abort = false
turtle.status = 'idle'
if not s and m then
printError(m)
end
end)
return s, m
end
function turtle.abortAction()
if turtle.status ~= 'idle' then
turtle.abort = true
os.queueEvent('turtle_abort')
end
end
-- [[ Pathing ]] --
function turtle.faceAgainst(pt) -- 4 sided
local pts = { }
for i = 0, 3 do
local hi = turtle.getHeadingInfo(i)
table.insert(pts, {
x = pt.x + hi.xd,
z = pt.z + hi.zd,
y = pt.y + hi.yd,
heading = (hi.heading + 2) % 4,
})
end
return turtle.pathfind(Point.closest(turtle.point, pts), { dest = pts })
end
function turtle.moveAgainst(pt) -- 6 sided
local pts = { }
for i = 0, 5 do
local hi = turtle.getHeadingInfo(i)
local heading, direction
if i < 4 then
heading = (hi.heading + 2) % 4
direction = 'forward'
elseif i == 4 then
direction = 'down'
elseif i == 5 then
direction = 'up'
end
table.insert(pts, {
x = pt.x + hi.xd,
z = pt.z + hi.zd,
y = pt.y + hi.yd,
direction = direction,
heading = heading,
})
end
return turtle.pathfind(Point.closest(turtle.point, pts), { dest = pts })
end
local actionsAt = {
detect = {
up = turtle.detectUp,
down = turtle.detectDown,
forward = turtle.detect,
},
dig = {
up = turtle.digUp,
down = turtle.digDown,
forward = turtle.dig,
},
move = {
up = turtle.moveUp,
down = turtle.moveDown,
forward = turtle.move,
},
attack = {
up = turtle.attackUp,
down = turtle.attackDown,
forward = turtle.attack,
},
place = {
up = turtle.placeUp,
down = turtle.placeDown,
forward = turtle.place,
},
drop = {
up = turtle.dropUp,
down = turtle.dropDown,
forward = turtle.drop,
},
suck = {
up = turtle.suckUp,
down = turtle.suckDown,
forward = turtle.suck,
},
compare = {
up = turtle.compareUp,
down = turtle.compareDown,
forward = turtle.compare,
},
inspect = {
up = turtle.inspectUp,
down = turtle.inspectDown,
forward = turtle.inspect,
},
}
local function _actionAt(action, pt, ...)
local pt = turtle.moveAgainst(pt)
if pt then
return action[pt.direction](...)
end
end
function _actionDownAt(action, pt, ...)
if turtle.pathfind(Point.above(pt)) then
return action.down(...)
end
end
function _actionForwardAt(action, pt, ...)
if turtle.faceAgainst(pt) then
return action.forward(...)
end
end
function _actionUpAt(action, pt, ...)
if turtle.pathfind(Point.below(pt)) then
return action.up(...)
end
end
function turtle.detectAt(pt) return _actionAt(actionsAt.detect, pt) end
function turtle.detectDownAt(pt) return _actionDownAt(actionsAt.detect, pt) end
function turtle.detectForwardAt(pt) return _actionForwardAt(actionsAt.detect, pt) end
function turtle.detectUpAt(pt) return _actionUpAt(actionsAt.detect, pt) end
function turtle.digAt(pt) return _actionAt(actionsAt.dig, pt) end
function turtle.digDownAt(pt) return _actionDownAt(actionsAt.dig, pt) end
function turtle.digForwardAt(pt) return _actionForwardAt(actionsAt.dig, pt) end
function turtle.digUpAt(pt) return _actionUpAt(actionsAt.dig, pt) end
function turtle.attackAt(pt) return _actionAt(actionsAt.attack, pt) end
function turtle.attackDownAt(pt) return _actionDownAt(actionsAt.attack, pt) end
function turtle.attackForwardAt(pt) return _actionForwardAt(actionsAt.attack, pt) end
function turtle.attackUpAt(pt) return _actionUpAt(actionsAt.attack, pt) end
function turtle.placeAt(pt, arg) return _actionAt(actionsAt.place, pt, arg) end
function turtle.placeDownAt(pt, arg) return _actionDownAt(actionsAt.place, pt, arg) end
function turtle.placeForwardAt(pt, arg) return _actionForwardAt(actionsAt.place, pt, arg) end
function turtle.placeUpAt(pt, arg) return _actionUpAt(actionsAt.place, pt, arg) end
function turtle.dropAt(pt, ...) return _actionAt(actionsAt.drop, pt, ...) end
function turtle.dropDownAt(pt, ...) return _actionDownAt(actionsAt.drop, pt, ...) end
function turtle.dropForwardAt(pt, ...) return _actionForwardAt(actionsAt.drop, pt, ...) end
function turtle.dropUpAt(pt, ...) return _actionUpAt(actionsAt.drop, pt, ...) end
function turtle.suckAt(pt, qty) return _actionAt(actionsAt.suck, pt, qty) end
function turtle.suckDownAt(pt, qty) return _actionDownAt(actionsAt.suck, pt, qty) end
function turtle.suckForwardAt(pt, qty) return _actionForwardAt(actionsAt.suck, pt, qty) end
function turtle.suckUpAt(pt, qty) return _actionUpAt(actionsAt.suck, pt, qty) end
function turtle.compareAt(pt) return _actionAt(actionsAt.compare, pt) end
function turtle.compareDownAt(pt) return _actionDownAt(actionsAt.compare, pt) end
function turtle.compareForwardAt(pt) return _actionForwardAt(actionsAt.compare, pt) end
function turtle.compareUpAt(pt) return _actionUpAt(actionsAt.compare, pt) end
function turtle.inspectAt(pt) return _actionAt(actionsAt.inspect, pt) end
function turtle.inspectDownAt(pt) return _actionDownAt(actionsAt.inspect, pt) end
function turtle.inspectForwardAt(pt) return _actionForwardAt(actionsAt.inspect, pt) end
function turtle.inspectUpAt(pt) return _actionUpAt(actionsAt.inspect, pt) end