1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-14 04:34:49 +00:00
opus/sys/apis/turtle/pathfind.lua

237 lines
6.0 KiB
Lua
Raw Normal View History

2017-10-08 21:45:01 +00:00
_G.requireInjector()
2017-10-28 00:24:48 +00:00
local Grid = require('jumper.grid')
local Pathfinder = require('jumper.pathfinder')
local Point = require('point')
local Util = require('util')
2016-12-11 19:24:52 +00:00
2017-10-08 21:45:01 +00:00
local turtle = _G.turtle
2017-10-26 22:56:55 +00:00
local function addBlock(grid, b, dim)
if Point.inBox(b, dim) then
local node = grid:getNodeAt(b.x, b.y, b.z)
if node then
node.walkable = 1
2016-12-11 19:24:52 +00:00
end
end
end
-- map shrinks/grows depending upon blocks encountered
-- the map will encompass any blocks encountered, the turtle position, and the destination
2017-10-26 22:56:55 +00:00
local function mapDimensions(dest, blocks, boundingBox, dests)
2017-10-28 00:24:48 +00:00
local box = Point.makeBox(turtle.point, turtle.point)
2016-12-11 19:24:52 +00:00
2017-10-28 00:24:48 +00:00
Point.expandBox(box, dest)
2016-12-11 19:24:52 +00:00
2017-10-26 22:56:55 +00:00
for _,d in pairs(dests) do
2017-10-28 00:24:48 +00:00
Point.expandBox(box, d)
2017-10-26 22:56:55 +00:00
end
for _,b in pairs(blocks) do
2017-10-28 00:24:48 +00:00
Point.expandBox(box, b)
2016-12-11 19:24:52 +00:00
end
-- expand one block out in all directions
if boundingBox then
2017-10-28 00:24:48 +00:00
box.x = math.max(box.x - 1, boundingBox.x)
box.z = math.max(box.z - 1, boundingBox.z)
box.y = math.max(box.y - 1, boundingBox.y)
box.ex = math.min(box.ex + 1, boundingBox.ex)
box.ez = math.min(box.ez + 1, boundingBox.ez)
box.ey = math.min(box.ey + 1, boundingBox.ey)
else
2017-10-28 00:24:48 +00:00
box.x = box.x - 1
box.z = box.z - 1
box.y = box.y - 1
box.ex = box.ex + 1
box.ez = box.ez + 1
box.ey = box.ey + 1
end
2016-12-11 19:24:52 +00:00
2017-10-28 00:24:48 +00:00
return box
2016-12-11 19:24:52 +00:00
end
2017-10-26 22:56:55 +00:00
local function nodeToPoint(node)
2017-10-28 00:24:48 +00:00
return { x = node.x, y = node.y, z = node.z, heading = node.heading }
2016-12-11 19:24:52 +00:00
end
2017-10-28 00:24:48 +00:00
local function heuristic(n, node)
return Point.calculateMoves(node, n)
-- { x = node.x, y = node.y, z = node.z, heading = node.heading },
-- { x = n.x, y = n.y, z = n.z, heading = n.heading })
2016-12-11 19:24:52 +00:00
end
local function dimsAreEqual(d1, d2)
return d1.ex == d2.ex and
d1.ey == d2.ey and
d1.ez == d2.ez and
2017-10-26 22:56:55 +00:00
d1.x == d2.x and
d1.y == d2.y and
d1.z == d2.z
2016-12-11 19:24:52 +00:00
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
2017-10-26 22:56:55 +00:00
local function selectDestination(pts, box, grid)
2017-09-15 05:08:04 +00:00
while #pts > 0 do
local pt = Point.closest(turtle.point, pts)
2017-10-26 22:56:55 +00:00
if box and not Point.inBox(pt, box) then
Util.removeByValue(pts, pt)
else
if grid:isWalkableAt(pt.x, pt.y, pt.z) then
return pt
end
2017-09-15 05:08:04 +00:00
Util.removeByValue(pts, pt)
end
end
end
local function pathTo(dest, options)
2017-09-15 05:08:04 +00:00
local blocks = options.blocks or turtle.getState().blocks or { }
local dests = options.dest or { dest } -- support alternative destinations
local box = options.box or turtle.getState().box
2017-10-28 00:24:48 +00:00
local lastDim
local grid
2016-12-11 19:24:52 +00:00
2017-09-15 05:08:04 +00:00
if box then
box = Point.normalizeBox(box)
end
2016-12-11 19:24:52 +00:00
-- Creates a pathfinder object
2017-10-28 00:24:48 +00:00
local finder = Pathfinder(heuristic)
2016-12-11 19:24:52 +00:00
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
2017-10-26 22:56:55 +00:00
local dim = mapDimensions(dest, blocks, box, dests)
2016-12-11 19:24:52 +00:00
-- reuse map if possible
if not lastDim or not dimsAreEqual(dim, lastDim) then
-- Creates a grid object
2017-10-26 22:56:55 +00:00
grid = Grid(dim)
2017-10-28 00:24:48 +00:00
finder:setGrid(grid)
2016-12-11 19:24:52 +00:00
lastDim = dim
end
2017-10-26 22:56:55 +00:00
for _,b in pairs(blocks) do
addBlock(grid, b, dim)
2016-12-11 19:24:52 +00:00
end
2017-10-26 22:56:55 +00:00
dest = selectDestination(dests, box, grid)
2017-09-15 05:08:04 +00:00
if not dest then
2017-09-18 18:46:46 +00:00
return false, 'failed to reach destination'
2017-09-15 05:08:04 +00:00
end
if turtle.point.x == dest.x and turtle.point.z == dest.z and turtle.point.y == dest.y then
break
end
2016-12-11 19:24:52 +00:00
-- Define start and goal locations coordinates
2017-10-26 22:56:55 +00:00
local startPt = turtle.point
2016-12-11 19:24:52 +00:00
-- Calculates the path, and its length
2017-10-28 00:24:48 +00:00
local path = finder:getPath(
2017-10-08 21:45:01 +00:00
startPt.x, startPt.y, startPt.z, turtle.point.heading,
2017-11-15 05:08:42 +00:00
dest.x, dest.y, dest.z, dest.heading)
2016-12-11 19:24:52 +00:00
if not path then
2017-09-15 05:08:04 +00:00
Util.removeByValue(dests, dest)
else
2017-10-28 00:24:48 +00:00
path:filter()
2017-10-08 21:45:01 +00:00
for node in path:nodes() do
2017-10-26 22:56:55 +00:00
local pt = nodeToPoint(node)
2017-10-28 00:24:48 +00:00
if turtle.isAborted() then
return false, 'aborted'
end
2017-10-28 00:24:48 +00:00
--if this is the next to last node
--and we are traveling up or down, then the
--heading for this node should be the heading of the last node
--or, maybe..
--if last node is up or down (or either?)
-- use single turn method so the turtle doesn't turn around
2017-10-28 00:24:48 +00:00
-- when encountering obstacles
2017-11-15 05:08:42 +00:00
if not turtle.gotoSingleTurn(pt.x, pt.y, pt.z, pt.heading) then
2017-10-28 00:24:48 +00:00
local bpt = Point.nearestTo(turtle.point, pt)
table.insert(blocks, bpt)
-- really need to check if the block we ran into was a turtle.
-- if so, this block should be temporary (1-2 secs)
--local side = turtle.getSide(turtle.point, pt)
--if turtle.isTurtleAtSide(side) then
-- pt.timestamp = os.clock() + ?
--end
-- if dim has not changed, then need to update grid with
-- walkable = nil (after time has elapsed)
2017-10-08 21:45:01 +00:00
--if device.turtlesensorenvironment then
-- addSensorBlocks(blocks, device.turtlesensorenvironment.sonicScan())
--end
break
end
2016-12-11 19:24:52 +00:00
end
end
end
if dest.heading then
turtle.setHeading(dest.heading)
end
return dest
2016-12-11 19:24:52 +00:00
end
2017-09-15 05:08:04 +00:00
return {
pathfind = function(dest, options)
options = options or { }
--if not options.blocks and turtle.gotoPoint(dest) then
-- return dest
--end
return pathTo(dest, options)
end,
-- set a global bounding box
-- box can be overridden by passing box in pathfind options
setBox = function(box)
turtle.getState().box = box
end,
setBlocks = function(blocks)
turtle.getState().blocks = blocks
end,
2017-10-26 22:56:55 +00:00
addBlock = function(block)
if turtle.getState().blocks then
table.insert(turtle.getState().blocks, block)
end
end,
2017-09-15 05:08:04 +00:00
reset = function()
turtle.getState().box = nil
turtle.getState().blocks = nil
end,
}