sCCripts/jlib/move.lua

51 lines
1.2 KiB
Lua
Raw Normal View History

2020-12-28 14:45:50 +01:00
-- Created by Jef Roosens
--
-- Utilities to ease movement using a turtle
local dig = require("jlib.mine").dig
local module = {}
module.logger = require("jlib.log").Logger:new{level = -1}
-- Safely move n blocks. This means accounting for gravel, trees growing...
--
-- @param dir direction to move (u, d, f)
-- @param n number of blocks to move. If nil (or not supplied), 1 is taken as
-- @param side_effect a function/table of functions which gets called after the
-- turtle has moved 1 block. This can be used to extend the functionality of
-- the function without having to re-write it.
function module.move(dir, n, side_effect)
-- Default value
n = n or 1
local funcs = {
u = turtle.up,
d = turtle.down,
f = turtle.forward
}
for _ = 1, n do
dig(dir)
funcs[dir]()
-- Run the side-effect if given
if side_effect then
if type(side_effect) == "table" then
for _, f in pairs(side_effect) do
f()
end
else
side_effect()
end
end
end
module.logger:info("Moved " .. n .. " blocks")
end
return module