Skip to content

Instantly share code, notes, and snippets.

@Semior001
Last active May 17, 2024 20:21
Show Gist options
  • Save Semior001/d71b873840baa0c89b9b240dcfccde86 to your computer and use it in GitHub Desktop.
Save Semior001/d71b873840baa0c89b9b240dcfccde86 to your computer and use it in GitHub Desktop.
local nav = require("navigation")
local Bot = {
pos = { x = 0, y = 0, z = 0 },
facing = nav.DIRECTION.NORTH,
init = function(self, pos, direction)
self.pos = pos
self.facing = direction
end,
currentPos = function(self)
return self.pos
end,
turnLeft = function(self)
if not turtle.turnLeft() then
return false
end
self.facing = (self.facing - 1) % 4
end,
turnRight = function(self)
if not turtle.turnRight() then
return false
end
self.facing = (self.facing + 1) % 4
end,
forward = function(self)
if not turtle.forward() then
return false
end
if self.facing == nav.DIRECTION.NORTH then
self.pos.z = self.pos.z + 1
elseif self.facing == nav.DIRECTION.EAST then
self.pos.x = self.pos.x + 1
elseif self.facing == nav.DIRECTION.SOUTH then
self.pos.z = self.pos.z - 1
elseif self.facing == nav.DIRECTION.WEST then
self.pos.x = self.pos.x - 1
end
end,
back = function(self)
if not turtle.back() then
return false
end
if self.facing == nav.DIRECTION.NORTH then
self.pos.z = self.pos.z - 1
elseif self.facing == nav.DIRECTION.EAST then
self.pos.x = self.pos.x - 1
elseif self.facing == nav.DIRECTION.SOUTH then
self.pos.z = self.pos.z + 1
elseif self.facing == nav.DIRECTION.WEST then
self.pos.x = self.pos.x + 1
end
end,
up = function(self)
if not turtle.up() then
return false
end
self.pos.y = self.pos.y + 1
end,
down = function(self)
if not turtle.down() then
return false
end
self.pos.y = self.pos.y - 1
end
}
return {Bot = Bot}
local Digger = {
bot = nil,
init = function(bot)
self.bot = bot
end,
line = function(self, height, length)
for i = 1, length do
self.bot.forward()
end
end,
}
local function tableToString(tbl)
local result = {}
for k, v in pairs(tbl) do
if type(v) == "table" then
v = tableToString(v)
else
v = tostring(v)
end
table.insert(result, string.format("%s = %s", tostring(k), v))
end
return "{" .. table.concat(result, ", ") .. "}"
end
-- Printf is a simple logging function that prints to the console
-- @param format: string: the message to log
-- @param ...: any: any additional arguments to pass to string.format
local function Printf(format, ...)
local args = {...}
local formattedArgs = {}
for i, v in ipairs(args) do
if type(v) == "table" then
table.insert(formattedArgs, tostring(v))
else
formattedArgs[i] = v
end
end
format = format:gsub("%%v", "%%s")
time = os.date("%H:%M:%S")
print(time .. " " .. string.format(format, table.unpack(formattedArgs)))
end
return { Printf = Printf }
local function tableToString(tbl)
local result = {}
for k, v in pairs(tbl) do
if type(v) == "table" then
v = tableToString(v)
else
v = tostring(v)
end
table.insert(result, string.format("%s = %s", tostring(k), v))
end
return "{" .. table.concat(result, ", ") .. "}"
end
-- Printf is a simple logging function that prints to the console
-- @param format: string: the message to log
-- @param ...: any: any additional arguments to pass to string.format
local function Printf(format, ...)
local args = {...}
local formattedArgs = {}
for i, v in ipairs(args) do
if type(v) == "table" then
table.insert(formattedArgs, tostring(v))
else
formattedArgs[i] = v
end
end
format = format:gsub("%%v", "%%s")
time = os.date("%H:%M:%S")
print(time .. " " .. string.format(format, table.unpack(formattedArgs)))
end
return { Printf = Printf }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment