CC-navigation/location
2016-02-07 18:20:06 +00:00

131 lines
2.5 KiB
Plaintext

-- a slightly modified version of an API I found somewhere
-- the headings have been altered to use the Minecraft directions
local location = {
add = function(self, o)
return vector.new(
self.x + o.x,
self.y + o.y,
self.z + o.z
)
end,
sub = function(self, o)
return vector.new(
self.x - o.x,
self.y - o.y,
self.z - o.z
)
end,
mul = function(self, m)
return vector.new(
self.x * m,
self.y * m,
self.z * m
)
end,
div = function(self, d)
return vector.new(
self.x / d,
self.y / d,
self.z / d
)
end,
left = function(self)
if turtle.turnLeft() then
self.h = (self.h - 1) % 4
return true
end
return false
end,
right = function(self)
if turtle.turnRight() then
self.h = (self.h + 1) % 4
return true
end
return false
end,
forward = function(self)
if turtle.forward() then
self.x = self.x + (self.h - 2) * (self.h % 2)
self.z = self.z + (1 - self.h) * ((self.h + 1) % 2)
return true
end
return false
end,
back = function(self)
if turtle.back() then
self.x = self.x - (self.h - 2) * (self.h % 2)
self.z = self.z - (1 - self.h) * ((self.h + 1) % 2)
return true
end
return false
end,
up = function(self)
if turtle.up() then
self.y = self.y + 1
return true
end
return false
end,
down = function(self)
if turtle.down() then
self.y = self.y - 1
return true
end
return false
end,
moveDeltas = function(self)
return (self.h - 2) * (self.h % 2), (1 - self.h) * ((self.h + 1) % 2)
end,
setHeading = function(self, heading)
if not heading or heading < 0 or heading > 3 then return nil, "Heading Not in Range" end
while self.h ~= heading do
if (self.h + 1) % 4 == heading % 4 then
self:right()
else
self:left()
end
end
return true
end,
getHeading = function(self)
return self.h
end,
tovector = function(self)
if vector then
return vector.new(
self.x,
self.y,
self.z
)
else
return nil
end
end,
tostring = function(self)
return self.x..","..self.y..","..self.z..","..self.h
end,
value = function(self)
return self.x, self.y, self.z, self.h
end,
}
local lmetatable = {
__index = location,
__add = location.add,
__sub = location.sub,
__mul = location.mul,
__div = location.div,
__unm = function(l) return l:mul(-1) end,
__tostring = function(l) return l:tostring() end,
}
function new( x, y, z, h )
local l = {
x = x or 0,
y = y or 0,
z = z or 0,
h = h or 1
}
setmetatable( l, lmetatable )
return l
end