:3
This commit is contained in:
parent
491112768c
commit
68ec37f994
66 changed files with 6591 additions and 10096 deletions
124
computer/26/path.lua
Normal file
124
computer/26/path.lua
Normal file
|
@ -0,0 +1,124 @@
|
|||
pp = require("cc.pretty").pretty_print
|
||||
|
||||
_G.facing = _G.facing or "south"
|
||||
_G.pos = _G.pos or vector.new(0,0,0)
|
||||
|
||||
local up = vector.new(0,1,0)
|
||||
|
||||
local rightOf = {
|
||||
south = "west",
|
||||
west = "north",
|
||||
north = "east",
|
||||
east = "south"
|
||||
}
|
||||
|
||||
local leftOf = {
|
||||
west = "south",
|
||||
north = "west",
|
||||
east = "north",
|
||||
south = "east"
|
||||
}
|
||||
|
||||
local vecOf = {
|
||||
north = vector.new(0,0,-1),
|
||||
south = vector.new(0,0,1),
|
||||
east = vector.new(1,0,0),
|
||||
west = vector.new(-1,0,0),
|
||||
}
|
||||
|
||||
function goUp()
|
||||
if turtle.up() then
|
||||
_G.pos.y = _G.pos.y + 1
|
||||
return true
|
||||
end
|
||||
printError("failed to go up")
|
||||
printError(pos)
|
||||
return false
|
||||
end
|
||||
|
||||
function goDown()
|
||||
if turtle.down() then
|
||||
_G.pos.y = _G.pos.y - 1
|
||||
return true
|
||||
end
|
||||
printError("failed to go down")
|
||||
printError(pos)
|
||||
return false
|
||||
end
|
||||
|
||||
function goLeft()
|
||||
turtle.turnLeft()
|
||||
_G.facing = leftOf[_G.facing]
|
||||
end
|
||||
|
||||
function goRight()
|
||||
turtle.turnRight()
|
||||
_G.facing = rightOf[_G.facing]
|
||||
end
|
||||
|
||||
function goForward()
|
||||
if turtle.forward() then
|
||||
_G.pos = _G.pos + vecOf[_G.facing]
|
||||
return true
|
||||
end
|
||||
-- printError("failed to go forward")
|
||||
-- printError(pos)
|
||||
return false
|
||||
end
|
||||
|
||||
function goBack()
|
||||
if turtle.back() then
|
||||
_G.pos = _G.pos - vecOf[_G.facing]
|
||||
return true
|
||||
end
|
||||
printError("failed to go backward")
|
||||
printError(pos)
|
||||
return false
|
||||
end
|
||||
|
||||
function stepTo(target, dig)
|
||||
local delta = target - _G.pos
|
||||
-- print(delta)
|
||||
if delta.y > 0 then
|
||||
if dig then
|
||||
repeat turtle.digUp() until goUp()
|
||||
else
|
||||
goUp()
|
||||
end
|
||||
elseif delta.y < 0 then
|
||||
if dig then
|
||||
turtle.digDown()
|
||||
end
|
||||
goDown()
|
||||
elseif delta:dot(vecOf[_G.facing]) > 0 then
|
||||
if dig then
|
||||
repeat turtle.dig() until goForward()
|
||||
else
|
||||
goForward()
|
||||
end
|
||||
elseif delta:dot(vecOf[_G.facing]:cross(up)) > 0 then
|
||||
goRight()
|
||||
else
|
||||
goLeft()
|
||||
end
|
||||
end
|
||||
|
||||
function goTo(target, face, dig)
|
||||
while target ~= _G.pos do
|
||||
stepTo(target, dig)
|
||||
end
|
||||
if face and face ~= _G.facing then
|
||||
if rightOf[_G.facing] == face then
|
||||
goRight()
|
||||
elseif leftOf[_G.facing] == face then
|
||||
goLeft()
|
||||
else
|
||||
goRight()
|
||||
goRight()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function goHome()
|
||||
goTo(vector.new(0,0,0), "south")
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue