trainworld_computercraft/computer/18/rail.lua
2025-06-01 02:23:34 +02:00

65 lines
1.2 KiB
Lua

_G.facing = "south"
local rightOf = {
south = "west",
west = "north",
north = "east",
east = "south"
}
local leftOf = {
west = "south",
north = "west",
east = "north",
south = "east"
}
local opposite = {
west = "east",
east = "west",
south = "north",
north = "south"
}
function step()
-- exists, block = turtle.inspectDown()
--if not exists then
repeat
turtle.down()
exists, block = turtle.inspectDown()
until exists
-- end
if block.name == "minecraft:rail" then
shape = block.state.shape
if shape == "ascending_" .. _G.facing then
turtle.up()
turtle.forward()
elseif string.match(shape, _G.facing) or shape == "ascending_" .. opposite[_G.facing] then
turtle.forward()
elseif string.match(shape, leftOf[_G.facing]) then
_G.facing = leftOf[_G.facing]
turtle.turnLeft()
elseif string.match(shape, rightOf[_G.facing]) then
_G.facing = rightOf[_G.facing]
turtle.turnRight()
else
error("i'm lost\n".. shape)
end
return false
elseif block.name == "minecraft:iron_block" then
return "done"
else
return "lost"
end
end
function follow_rail(init_dir)
_G.facing = init_dir
repeat
state = step()
until state
print(state)
end
print("what direction am i facing?")
follow_rail(read())