Initial files
This commit is contained in:
parent
1378189ba2
commit
6dc2343ee5
11 changed files with 758 additions and 0 deletions
50
jlib/move.lua
Normal file
50
jlib/move.lua
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
-- 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue