mirror of
https://github.com/kepler155c/opus
synced 2025-10-21 10:47:40 +00:00
treefarm + turtle improvements + cleanup
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
local Util = require('util')
|
||||
|
||||
local Event = {
|
||||
uid = 1, -- unique id for handlers
|
||||
routines = { }, -- coroutines
|
||||
@@ -26,7 +24,7 @@ end
|
||||
function Routine:resume(event, ...)
|
||||
|
||||
if not self.co then
|
||||
error('Cannot resume a dead routine\n' .. Util.tostring(self))
|
||||
error('Cannot resume a dead routine')
|
||||
end
|
||||
|
||||
if not self.filter or self.filter == event or event == "terminate" then
|
||||
@@ -41,7 +39,7 @@ function Routine:resume(event, ...)
|
||||
end
|
||||
|
||||
if not s and event ~= 'terminate' then
|
||||
error('\n' .. (m or 'Error processing event') .. '\n' .. Util.tostring(self))
|
||||
error('\n' .. (m or 'Error processing event'))
|
||||
end
|
||||
|
||||
return s, m
|
||||
|
@@ -1,3 +1,5 @@
|
||||
local Util = require('util')
|
||||
|
||||
local Point = { }
|
||||
|
||||
function Point.copy(pt)
|
||||
@@ -10,6 +12,14 @@ function Point.same(pta, ptb)
|
||||
pta.z == ptb.z
|
||||
end
|
||||
|
||||
function Point.above(pt)
|
||||
return { x = pt.x, y = pt.y + 1, z = pt.z, heading = pt.heading }
|
||||
end
|
||||
|
||||
function Point.below(pt)
|
||||
return { x = pt.x, y = pt.y - 1, z = pt.z, heading = pt.heading }
|
||||
end
|
||||
|
||||
function Point.subtract(a, b)
|
||||
a.x = a.x - b.x
|
||||
a.y = a.y - b.y
|
||||
@@ -123,30 +133,17 @@ function Point.closest(reference, pts)
|
||||
return lpt
|
||||
end
|
||||
|
||||
-- find the closest block
|
||||
-- * favor same plane
|
||||
-- * going backwards only if the dest is above or below
|
||||
function Point.closest2(reference, pts)
|
||||
local lpt, lm -- lowest
|
||||
for _,pt in pairs(pts) do
|
||||
local m = Point.turtleDistance(reference, pt)
|
||||
local h = Point.calculateHeading(reference, pt)
|
||||
local t = Point.calculateTurns(reference.heading, h)
|
||||
if pt.y ~= reference.y then -- try and stay on same plane
|
||||
m = m + .01
|
||||
end
|
||||
if t ~= 2 or pt.y == reference.y then
|
||||
m = m + t
|
||||
if t > 0 then
|
||||
m = m + .01
|
||||
end
|
||||
end
|
||||
if not lm or m < lm then
|
||||
lpt = pt
|
||||
lm = m
|
||||
function Point.eachClosest(spt, ipts, fn)
|
||||
|
||||
local pts = Util.shallowCopy(ipts)
|
||||
while #pts > 0 do
|
||||
local pt = Point.closest(spt, pts)
|
||||
local r = fn(pt)
|
||||
if r then
|
||||
return r
|
||||
end
|
||||
Util.removeByValue(pts, pt)
|
||||
end
|
||||
return lpt
|
||||
end
|
||||
|
||||
function Point.adjacentPoints(pt)
|
||||
@@ -159,26 +156,28 @@ function Point.adjacentPoints(pt)
|
||||
return pts
|
||||
end
|
||||
|
||||
return Point
|
||||
|
||||
--[[
|
||||
function Point.toBox(pt, width, length, height)
|
||||
return { ax = pt.x,
|
||||
ay = pt.y,
|
||||
az = pt.z,
|
||||
bx = pt.x + width - 1,
|
||||
by = pt.y + height - 1,
|
||||
bz = pt.z + length - 1
|
||||
}
|
||||
function Point.normalizeBox(box)
|
||||
return {
|
||||
x = math.min(box.x, box.ex),
|
||||
y = math.min(box.y, box.ey),
|
||||
z = math.min(box.z, box.ez),
|
||||
ex = math.max(box.x, box.ex),
|
||||
ey = math.max(box.y, box.ey),
|
||||
ez = math.max(box.z, box.ez),
|
||||
}
|
||||
end
|
||||
|
||||
function Point.inBox(pt, box)
|
||||
return pt.x >= box.ax and
|
||||
pt.z >= box.az and
|
||||
pt.x <= box.bx and
|
||||
pt.z <= box.bz
|
||||
return pt.x >= box.x and
|
||||
pt.y >= box.y and
|
||||
pt.z >= box.z and
|
||||
pt.x <= box.ex and
|
||||
pt.z <= box.ez
|
||||
end
|
||||
|
||||
return Point
|
||||
|
||||
--[[
|
||||
Box = { }
|
||||
|
||||
function Box.contain(boundingBox, containedBox)
|
||||
|
@@ -1,121 +0,0 @@
|
||||
local Util = require('util')
|
||||
|
||||
local Process = { }
|
||||
|
||||
function Process:init(args)
|
||||
self.args = { }
|
||||
self.uid = 0
|
||||
self.threads = { }
|
||||
Util.merge(self, args)
|
||||
self.name = self.name or 'Thread:' .. self.uid
|
||||
end
|
||||
|
||||
function Process:isDead()
|
||||
return coroutine.status(self.co) == 'dead'
|
||||
end
|
||||
|
||||
function Process:terminate()
|
||||
print('terminating ' .. self.name)
|
||||
self:resume('terminate')
|
||||
end
|
||||
|
||||
function Process:threadEvent(...)
|
||||
|
||||
for _,key in pairs(Util.keys(self.threads)) do
|
||||
local thread = self.threads[key]
|
||||
if thread then
|
||||
thread:resume(...)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Process:addThread(fn, ...)
|
||||
return self:newThread(nil, fn, ...)
|
||||
end
|
||||
|
||||
-- deprecated
|
||||
function Process:newThread(name, fn, ...)
|
||||
|
||||
self.uid = self.uid + 1
|
||||
|
||||
local thread = { }
|
||||
setmetatable(thread, { __index = Process })
|
||||
thread:init({
|
||||
fn = fn,
|
||||
name = name,
|
||||
uid = self.uid,
|
||||
})
|
||||
|
||||
local args = { ... }
|
||||
thread.co = coroutine.create(function()
|
||||
|
||||
local s, m = pcall(function() fn(unpack(args)) end)
|
||||
if not s and m then
|
||||
if m == 'Terminated' then
|
||||
--printError(thread.name .. ' terminated')
|
||||
else
|
||||
printError(m)
|
||||
end
|
||||
end
|
||||
|
||||
--print('thread died ' .. thread.name)
|
||||
self.threads[thread.uid] = nil
|
||||
|
||||
thread:threadEvent('terminate')
|
||||
|
||||
return s, m
|
||||
end)
|
||||
|
||||
self.threads[thread.uid] = thread
|
||||
|
||||
thread:resume()
|
||||
|
||||
return thread
|
||||
end
|
||||
|
||||
function Process:resume(event, ...)
|
||||
|
||||
-- threads get a chance to process the event regardless of the main process filter
|
||||
self:threadEvent(event, ...)
|
||||
|
||||
if not self.filter or self.filter == event or event == "terminate" then
|
||||
local ok, result = coroutine.resume(self.co, event, ...)
|
||||
if ok then
|
||||
self.filter = result
|
||||
end
|
||||
return ok, result
|
||||
end
|
||||
|
||||
return true, self.filter
|
||||
end
|
||||
|
||||
-- confusing...
|
||||
|
||||
-- pull either one event if no filter or until event matches filter
|
||||
-- or until terminated (regardless of filter)
|
||||
function Process:pullEvent(filter)
|
||||
while true do
|
||||
local e = { os.pullEventRaw() }
|
||||
self:threadEvent(unpack(e))
|
||||
|
||||
if not filter or e[1] == filter or e[1] == 'terminate' then
|
||||
return unpack(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- pull events until either the filter is matched or terminated
|
||||
function Process:pullEvents(filter)
|
||||
while true do
|
||||
local e = { os.pullEventRaw() }
|
||||
self:threadEvent(unpack(e))
|
||||
if (filter and e[1] == filter) or e[1] == 'terminate' then
|
||||
return unpack(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local process = { }
|
||||
setmetatable(process, { __index = Process })
|
||||
process:init({ name = 'Main', co = coroutine.running() })
|
||||
return process
|
@@ -1,58 +0,0 @@
|
||||
local function resolveFile(filename, dir, lua_path)
|
||||
|
||||
local ch = string.sub(filename, 1, 1)
|
||||
if ch == "/" then
|
||||
return filename
|
||||
end
|
||||
|
||||
if dir then
|
||||
local path = fs.combine(dir, filename)
|
||||
if fs.exists(path) and not fs.isDir(path) then
|
||||
return path
|
||||
end
|
||||
end
|
||||
|
||||
if lua_path then
|
||||
for dir in string.gmatch(lua_path, "[^:]+") do
|
||||
local path = fs.combine(dir, filename)
|
||||
if fs.exists(path) and not fs.isDir(path) then
|
||||
return path
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local modules = { }
|
||||
|
||||
return function(filename)
|
||||
|
||||
local dir = DIR
|
||||
if not dir and shell and type(shell.dir) == 'function' then
|
||||
dir = shell.dir()
|
||||
end
|
||||
|
||||
local fname = resolveFile(filename:gsub('%.', '/') .. '.lua',
|
||||
dir or '', LUA_PATH or '/sys/apis')
|
||||
|
||||
if not fname or not fs.exists(fname) then
|
||||
error('Unable to load: ' .. filename, 2)
|
||||
end
|
||||
|
||||
local rname = fname:gsub('%/', '.'):gsub('%.lua', '')
|
||||
|
||||
local module = modules[rname]
|
||||
if not module then
|
||||
|
||||
local f, err = loadfile(fname)
|
||||
if not f then
|
||||
error(err)
|
||||
end
|
||||
setfenv(f, getfenv(1))
|
||||
|
||||
module = f(rname)
|
||||
|
||||
modules[rname] = module
|
||||
end
|
||||
|
||||
return module
|
||||
end
|
166
sys/apis/turtle/craft.lua
Normal file
166
sys/apis/turtle/craft.lua
Normal file
@@ -0,0 +1,166 @@
|
||||
local itemDB = require('itemDB')
|
||||
local Util = require('util')
|
||||
|
||||
local Craft = { }
|
||||
|
||||
local function clearGrid(chestProvider)
|
||||
for i = 1, 16 do
|
||||
local count = turtle.getItemCount(i)
|
||||
if count > 0 then
|
||||
chestProvider:insert(i, count)
|
||||
if turtle.getItemCount(i) ~= 0 then
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function splitKey(key)
|
||||
local t = Util.split(key, '(.-):')
|
||||
local item = { }
|
||||
if #t[#t] > 2 then
|
||||
item.nbtHash = table.remove(t)
|
||||
end
|
||||
item.damage = tonumber(table.remove(t))
|
||||
item.name = table.concat(t, ':')
|
||||
return item
|
||||
end
|
||||
|
||||
local function getItemCount(items, key)
|
||||
local item = splitKey(key)
|
||||
for _,v in pairs(items) do
|
||||
if v.name == item.name and
|
||||
v.damage == item.damage and
|
||||
v.nbtHash == item.nbtHash then
|
||||
return v.count
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
local function turtleCraft(recipe, qty, chestProvider)
|
||||
|
||||
clearGrid(chestProvider)
|
||||
|
||||
for k,v in pairs(recipe.ingredients) do
|
||||
local item = splitKey(v)
|
||||
chestProvider:provide({ id = item.name, dmg = item.damage, nbt_hash = item.nbtHash }, qty, k)
|
||||
if turtle.getItemCount(k) == 0 then -- ~= qty then
|
||||
-- FIX: ingredients cannot be stacked
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
return turtle.craft()
|
||||
end
|
||||
|
||||
function Craft.craftRecipe(recipe, count, chestProvider)
|
||||
|
||||
local items = chestProvider:listItems()
|
||||
|
||||
local function sumItems(items)
|
||||
-- produces { ['minecraft:planks:0'] = 8 }
|
||||
local t = {}
|
||||
for _,item in pairs(items) do
|
||||
t[item] = (t[item] or 0) + 1
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
count = math.ceil(count / recipe.count)
|
||||
|
||||
local maxCount = recipe.maxCount or math.floor(64 / recipe.count)
|
||||
local summedItems = sumItems(recipe.ingredients)
|
||||
|
||||
for key,icount in pairs(summedItems) do
|
||||
local itemCount = getItemCount(items, key)
|
||||
if itemCount < icount * count then
|
||||
local irecipe = Craft.recipes[key]
|
||||
if irecipe then
|
||||
Util.print('Crafting %d %s', icount * count - itemCount, key)
|
||||
if not Craft.craftRecipe(irecipe,
|
||||
icount * count - itemCount,
|
||||
chestProvider) then
|
||||
turtle.select(1)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
repeat
|
||||
if not turtleCraft(recipe, math.min(count, maxCount), chestProvider) then
|
||||
turtle.select(1)
|
||||
return false
|
||||
end
|
||||
count = count - maxCount
|
||||
until count <= 0
|
||||
|
||||
turtle.select(1)
|
||||
return true
|
||||
end
|
||||
|
||||
-- given a certain quantity, return how many of those can be crafted
|
||||
function Craft.getCraftableAmount(recipe, count, items)
|
||||
|
||||
local function sumItems(recipe, items, summedItems, count)
|
||||
|
||||
local canCraft = 0
|
||||
|
||||
for i = 1, count do
|
||||
for _,item in pairs(recipe.ingredients) do
|
||||
local summedItem = summedItems[item] or getItemCount(items, item)
|
||||
|
||||
local irecipe = Craft.recipes[item]
|
||||
if irecipe and summedItem <= 0 then
|
||||
summedItem = summedItem + sumItems(irecipe, items, summedItems, 1)
|
||||
end
|
||||
if summedItem <= 0 then
|
||||
return canCraft
|
||||
end
|
||||
summedItems[item] = summedItem - 1
|
||||
end
|
||||
canCraft = canCraft + recipe.count
|
||||
end
|
||||
|
||||
return canCraft
|
||||
end
|
||||
|
||||
return sumItems(recipe, items, { }, math.ceil(count / recipe.count))
|
||||
end
|
||||
|
||||
function Craft.canCraft(item, count, items)
|
||||
return Craft.getCraftableAmount(Craft.recipes[item], count, items) == count
|
||||
end
|
||||
|
||||
function Craft.setRecipes(recipes)
|
||||
Craft.recipes = recipes
|
||||
end
|
||||
|
||||
function Craft.getCraftableAmountTest()
|
||||
local results = { }
|
||||
Craft.setRecipes(Util.readTable('sys/etc/recipes.db'))
|
||||
|
||||
local items = {
|
||||
{ name = 'minecraft:planks', damage = 0, count = 5 },
|
||||
{ name = 'minecraft:log', damage = 0, count = 2 },
|
||||
}
|
||||
results[1] = { item = 'chest', expected = 1, got = Craft.getCraftableAmount(Craft.recipes['minecraft:chest:0'], 2, items) }
|
||||
|
||||
items = {
|
||||
{ name = 'minecraft:log', damage = 0, count = 1 },
|
||||
{ name = 'minecraft:coal', damage = 1, count = 1 },
|
||||
}
|
||||
results[2] = { item = 'torch', expected = 4, got = Craft.getCraftableAmount(Craft.recipes['minecraft:torch:0'], 4, items) }
|
||||
|
||||
return results
|
||||
end
|
||||
|
||||
function Craft.craftRecipeTest(name, count)
|
||||
local ChestProvider = require('chestProvider18')
|
||||
local chestProvider = ChestProvider({ wrapSide = 'top', direction = 'down' })
|
||||
Craft.setRecipes(Util.readTable('usr/etc/recipes.db'))
|
||||
return { Craft.craftRecipe(Craft.recipes[name], count, chestProvider) }
|
||||
end
|
||||
|
||||
return Craft
|
160
sys/apis/turtle/level.lua
Normal file
160
sys/apis/turtle/level.lua
Normal file
@@ -0,0 +1,160 @@
|
||||
local Point = require('point')
|
||||
local Util = require('util')
|
||||
|
||||
local checkedNodes = { }
|
||||
local nodes = { }
|
||||
local box = { }
|
||||
local oldCallback
|
||||
|
||||
local function toKey(pt)
|
||||
return table.concat({ pt.x, pt.y, pt.z }, ':')
|
||||
end
|
||||
|
||||
local function addNode(node)
|
||||
|
||||
for i = 0, 5 do
|
||||
local hi = turtle.getHeadingInfo(i)
|
||||
local testNode = { x = node.x + hi.xd, y = node.y + hi.yd, z = node.z + hi.zd }
|
||||
|
||||
if Point.inBox(testNode, box) then
|
||||
local key = toKey(testNode)
|
||||
if not checkedNodes[key] then
|
||||
nodes[key] = testNode
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function dig(action)
|
||||
|
||||
local directions = {
|
||||
top = 'up',
|
||||
bottom = 'down',
|
||||
}
|
||||
|
||||
-- convert to up, down, north, south, east, west
|
||||
local direction = directions[action.side] or
|
||||
turtle.getHeadingInfo(turtle.point.heading).direction
|
||||
|
||||
local hi = turtle.getHeadingInfo(direction)
|
||||
local node = { x = turtle.point.x + hi.xd, y = turtle.point.y + hi.yd, z = turtle.point.z + hi.zd }
|
||||
if Point.inBox(node, box) then
|
||||
|
||||
local key = toKey(node)
|
||||
checkedNodes[key] = true
|
||||
nodes[key] = nil
|
||||
|
||||
if action.dig() then
|
||||
addNode(node)
|
||||
repeat until not action.dig() -- sand, etc
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function move(action)
|
||||
if action == 'turn' then
|
||||
dig(turtle.getAction('forward'))
|
||||
elseif action == 'up' then
|
||||
dig(turtle.getAction('up'))
|
||||
elseif action == 'down' then
|
||||
dig(turtle.getAction('down'))
|
||||
elseif action == 'back' then
|
||||
dig(turtle.getAction('up'))
|
||||
dig(turtle.getAction('down'))
|
||||
end
|
||||
|
||||
if oldCallback then
|
||||
oldCallback(action)
|
||||
end
|
||||
end
|
||||
|
||||
-- find the closest block
|
||||
-- * favor same plane
|
||||
-- * going backwards only if the dest is above or below
|
||||
function closestPoint(reference, pts)
|
||||
local lpt, lm -- lowest
|
||||
for _,pt in pairs(pts) do
|
||||
local m = Point.turtleDistance(reference, pt)
|
||||
local h = Point.calculateHeading(reference, pt)
|
||||
local t = Point.calculateTurns(reference.heading, h)
|
||||
if pt.y ~= reference.y then -- try and stay on same plane
|
||||
m = m + .01
|
||||
end
|
||||
if t ~= 2 or pt.y == reference.y then
|
||||
m = m + t
|
||||
if t > 0 then
|
||||
m = m + .01
|
||||
end
|
||||
end
|
||||
if not lm or m < lm then
|
||||
lpt = pt
|
||||
lm = m
|
||||
end
|
||||
end
|
||||
return lpt
|
||||
end
|
||||
|
||||
local function getAdjacentPoint(pt)
|
||||
local t = { }
|
||||
table.insert(t, pt)
|
||||
for i = 0, 5 do
|
||||
local hi = turtle.getHeadingInfo(i)
|
||||
local heading
|
||||
if i < 4 then
|
||||
heading = (hi.heading + 2) % 4
|
||||
end
|
||||
table.insert(t, { x = pt.x + hi.xd, z = pt.z + hi.zd, y = pt.y + hi.yd, heading = heading })
|
||||
end
|
||||
|
||||
return closestPoint(turtle.getPoint(), t)
|
||||
end
|
||||
|
||||
return function(startPt, endPt, firstPt, verbose)
|
||||
|
||||
checkedNodes = { }
|
||||
nodes = { }
|
||||
box = { }
|
||||
|
||||
box.x = math.min(startPt.x, endPt.x)
|
||||
box.y = math.min(startPt.y, endPt.y)
|
||||
box.z = math.min(startPt.z, endPt.z)
|
||||
box.ex = math.max(startPt.x, endPt.x)
|
||||
box.ey = math.max(startPt.y, endPt.y)
|
||||
box.ez = math.max(startPt.z, endPt.z)
|
||||
|
||||
turtle.pathfind(firstPt)
|
||||
|
||||
turtle.setPolicy("attack", { dig = dig }, "assuredMove")
|
||||
|
||||
oldCallback = turtle.getMoveCallback()
|
||||
turtle.setMoveCallback(move)
|
||||
|
||||
repeat
|
||||
local key = toKey(turtle.point)
|
||||
|
||||
checkedNodes[key] = true
|
||||
nodes[key] = nil
|
||||
|
||||
dig(turtle.getAction('down'))
|
||||
dig(turtle.getAction('up'))
|
||||
dig(turtle.getAction('forward'))
|
||||
|
||||
if verbose then
|
||||
print(string.format('%d nodes remaining', Util.size(nodes)))
|
||||
end
|
||||
|
||||
if Util.size(nodes) == 0 then
|
||||
break
|
||||
end
|
||||
|
||||
local node = closestPoint(turtle.point, nodes)
|
||||
node = getAdjacentPoint(node)
|
||||
if not turtle.gotoPoint(node) then
|
||||
break
|
||||
end
|
||||
until turtle.abort
|
||||
|
||||
turtle.resetState()
|
||||
turtle.setMoveCallback(oldCallback)
|
||||
end
|
233
sys/apis/turtle/pathfind.lua
Normal file
233
sys/apis/turtle/pathfind.lua
Normal file
@@ -0,0 +1,233 @@
|
||||
requireInjector(getfenv(1))
|
||||
|
||||
local Grid = require ("jumper.grid")
|
||||
local Pathfinder = require ("jumper.pathfinder")
|
||||
local Point = require('point')
|
||||
local Util = require('util')
|
||||
|
||||
local WALKABLE = 0
|
||||
|
||||
local function createMap(dim)
|
||||
local map = { }
|
||||
for z = 1, dim.ez do
|
||||
local row = {}
|
||||
for x = 1, dim.ex do
|
||||
local col = { }
|
||||
for y = 1, 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
|
||||
if boundingBox then
|
||||
sx = math.max(sx - 1, boundingBox.x)
|
||||
sz = math.max(sz - 1, boundingBox.z)
|
||||
sy = math.max(sy - 1, boundingBox.y)
|
||||
ex = math.min(ex + 1, boundingBox.ex)
|
||||
ez = math.min(ez + 1, boundingBox.ez)
|
||||
ey = math.min(ey + 1, boundingBox.ey)
|
||||
else
|
||||
sx = sx - 1
|
||||
sz = sz - 1
|
||||
sy = sy - 1
|
||||
ex = ex + 1
|
||||
ez = ez + 1
|
||||
ey = ey + 1
|
||||
end
|
||||
|
||||
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, options)
|
||||
|
||||
local blocks = options.blocks or { }
|
||||
local allDests = options.dest or { } -- support alternative destinations
|
||||
|
||||
local lastDim = nil
|
||||
local map = nil
|
||||
local grid = nil
|
||||
|
||||
-- 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, options.box)
|
||||
|
||||
-- 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
|
||||
Util.removeByValue(allDests, dest)
|
||||
dest = Point.closest(turtle.point, allDests)
|
||||
|
||||
if not dest then
|
||||
return false, 'failed to recalculate'
|
||||
end
|
||||
else
|
||||
|
||||
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 -- IS THIS RIGHT ??
|
||||
if not turtle.gotoSingleTurn(pt.x, pt.z, pt.y, node.heading) then
|
||||
table.insert(blocks, pt)
|
||||
if #allDests > 0 then
|
||||
dest = Point.closest(turtle.point, allDests)
|
||||
end
|
||||
--if device.turtlesensorenvironment then
|
||||
-- addSensorBlocks(blocks, device.turtlesensorenvironment.sonicScan())
|
||||
--end
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if dest.heading then
|
||||
turtle.setHeading(dest.heading)
|
||||
end
|
||||
return dest
|
||||
end
|
||||
|
||||
return function(dest, options)
|
||||
options = options or { }
|
||||
if not options.blocks and turtle.gotoPoint(dest) then
|
||||
return dest
|
||||
end
|
||||
return pathTo(dest, options)
|
||||
end
|
@@ -343,7 +343,7 @@ function Manager:click(button, x, y)
|
||||
|
||||
if button == 1 then
|
||||
local c = os.clock()
|
||||
if self.doubleClickTimer and (c - self.doubleClickTimer < 1.5) and
|
||||
if self.doubleClickTimer and (c - self.doubleClickTimer < 1.9) and
|
||||
self.doubleClickX == x and self.doubleClickY == y and
|
||||
self.doubleClickElement == clickEvent.element then
|
||||
button = 3
|
||||
|
@@ -243,6 +243,15 @@ function Util.size(list)
|
||||
return 0
|
||||
end
|
||||
|
||||
function Util.removeByValue(t, e)
|
||||
for k,v in pairs(t) do
|
||||
if v == e then
|
||||
table.remove(t, k)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Util.each(list, func)
|
||||
for index, value in pairs(list) do
|
||||
func(value, index, list)
|
||||
|
Reference in New Issue
Block a user