trainworld_computercraft/computer/3/pathfinding2.lua
2025-05-22 20:51:25 +02:00

40 lines
1,005 B
Lua

up = vector.new(0,1,0)
function greedystep(target)
--rednet.broadcast(position)
vec = target-position
if target == position then
return false
end
if vec.y > 0 then
if turtle.up() then
_G.position = position + up
return true
end
elseif vec.y < 0 then
if turtle.down() then
_G.position = position - up
return true
end
end
if vec:dot(facing) > 0 then
if turtle.forward() then
_G.position = position + facing
return true
end
elseif vec:dot(facing) < 0 then
if turtle.back() then
_G.position = position - facing
return true
end
end
if vec:dot(facing:cross(up)) > 0 then
turtle.turnRight()
_G.facing = facing:cross(up)
return true
else
turtle.turnLeft()
_G.facing = - facing:cross(up)
return true
end
end
return {greedystep = greedystep}