-- right_edge_rules.lua
-- IDs to match
local IS_DIRT = { ["minecraft:dirt"]=true, ["minecraft:coarse_dirt"]=true }
local IS_BRICKS = { ["minecraft:bricks"]=true }
-- inventory
local function selectAny()
for i=1,16 do if turtle.getItemCount(i)>0 then turtle.select(i); return true end end
return false
end
-- placement helpers
local function placeDownIfAir()
if turtle.detectDown() then return end
if selectAny() then turtle.placeDown() end
end
local function placeBehindIfAir()
turtle.turnRight(); turtle.turnRight()
local ok,_ = turtle.inspect()
if not ok then if selectAny() then turtle.place() end end
turtle.turnRight(); turtle.turnRight()
end
-- motion
local function safeForward()
local tries=0
while not turtle.forward() do
if turtle.detect() then turtle.dig() else turtle.attack() end
tries=tries+1; if tries>10 then return false end
sleep(0.1)
end
return true
end
-- sensing
local function aheadBlocked()
return turtle.detect()
end
local function aboveIsBricks()
local ok,up = turtle.inspectUp()
return ok and IS_BRICKS[up.name] or false
end
-- right lane checks:
-- 1) is the ground to the right a dirt tile?
-- 2) is the space to the right at turtle level free to turn into?
local function rightGroundIsDirt()
turtle.turnRight()
local ok,down = turtle.inspectDown()
local isDirt = ok and IS_DIRT[down.name] or false
turtle.turnLeft()
return isDirt
end
local function rightSpaceIsAir()
turtle.turnRight()
local blocked = turtle.detect()
turtle.turnLeft()
return not blocked
end
-- main step applying your rules
local function step()
-- Rule 1: if the block ahead is not air, stop
if aheadBlocked() then return false end
local upBricks = aboveIsBricks()
local rightDirt = rightGroundIsDirt()
local rightAir = rightSpaceIsAir()
-- Rule 2: if above is bricks OR right-ground is dirt
if upBricks or rightDirt then
placeDownIfAir()
placeBehindIfAir()
return safeForward()
end
-- Rule 3: if above and right are air -> behind, turn right, forward
if (not upBricks) and rightAir then
placeBehindIfAir()
turtle.turnRight()
return safeForward()
end
-- Fallback (interprets your unfinished line as "otherwise, go straight"):
-- place below+behind, then forward.
placeDownIfAir()
placeBehindIfAir()
return safeForward()
end
-- loop
local function run()
while true do
if not step() then
print("Halt.")
break
end
end
end
run()
Comments