feat: Improve exploration logic with enhanced stuck detection and direction prioritization
This commit is contained in:
179
turtle.lua
179
turtle.lua
@@ -606,7 +606,9 @@ local function exploreStep()
|
||||
-- Check if stuck in same position
|
||||
if isStuckInSamePosition() then
|
||||
state.stuckInHoleCounter = state.stuckInHoleCounter + 1
|
||||
print("Stuck counter: " .. state.stuckInHoleCounter)
|
||||
if state.stuckInHoleCounter > 5 then
|
||||
print("⚠️ Stuck for " .. state.stuckInHoleCounter .. " steps!")
|
||||
end
|
||||
else
|
||||
state.stuckInHoleCounter = 0
|
||||
end
|
||||
@@ -620,126 +622,127 @@ local function exploreStep()
|
||||
}
|
||||
end
|
||||
|
||||
-- If stuck in hole for too long, try to climb out
|
||||
if state.stuckInHoleCounter > 3 or isInHole() then
|
||||
if tryClimbOut() then
|
||||
return -- Successfully climbed out
|
||||
end
|
||||
end
|
||||
|
||||
-- Check all directions for valuable ores first (priority)
|
||||
for _, direction in ipairs({"forward", "up", "down"}) do
|
||||
local dug, valuable, blockName = smartDig(direction)
|
||||
if valuable then
|
||||
print("Found: " .. blockName)
|
||||
print("💎 Found: " .. blockName)
|
||||
broadcastStatus()
|
||||
end
|
||||
end
|
||||
|
||||
-- Smart movement decision making
|
||||
local canGoUp = canMoveInDirection("up")
|
||||
local canGoForward = canMoveInDirection("forward")
|
||||
local canGoDown = canMoveInDirection("down")
|
||||
|
||||
-- If we're too deep (below y=0) or stuck, prioritize going up
|
||||
if state.position and (state.position.y < 0 or state.stuckInHoleCounter > 1) then
|
||||
if canGoUp then
|
||||
if smartUp() then
|
||||
print("Climbing up (too deep or stuck)")
|
||||
return
|
||||
end
|
||||
else
|
||||
smartDig("up")
|
||||
if smartUp() then
|
||||
print("Dug up and climbed")
|
||||
return
|
||||
end
|
||||
-- Only try to climb out if REALLY stuck (not just after a few steps)
|
||||
if state.stuckInHoleCounter > 8 then
|
||||
print("Trying to escape - stuck for too long")
|
||||
if tryClimbOut() then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
-- Intelligent direction selection based on visited positions
|
||||
-- Check what's around us
|
||||
local hasBlockUp, dataUp = turtle.inspectUp()
|
||||
local hasBlockForward, dataForward = turtle.inspect()
|
||||
local hasBlockDown, dataDown = turtle.inspectDown()
|
||||
|
||||
-- Calculate positions for each direction
|
||||
local forwardPos = nil
|
||||
local upPos = nil
|
||||
local downPos = nil
|
||||
|
||||
if state.position then
|
||||
-- Forward position
|
||||
forwardPos = {x = state.position.x, y = state.position.y, z = state.position.z}
|
||||
if facing == 0 then forwardPos.z = forwardPos.z - 1
|
||||
elseif facing == 1 then forwardPos.x = forwardPos.x + 1
|
||||
elseif facing == 2 then forwardPos.z = forwardPos.z + 1
|
||||
elseif facing == 3 then forwardPos.x = forwardPos.x - 1
|
||||
end
|
||||
|
||||
-- Up and down positions
|
||||
upPos = {x = state.position.x, y = state.position.y + 1, z = state.position.z}
|
||||
downPos = {x = state.position.x, y = state.position.y - 1, z = state.position.z}
|
||||
end
|
||||
|
||||
-- Prefer unvisited directions
|
||||
-- Check which directions have been visited
|
||||
local forwardVisited = hasVisited(forwardPos)
|
||||
local upVisited = hasVisited(upPos)
|
||||
local downVisited = hasVisited(downPos)
|
||||
|
||||
-- Decision tree for exploration
|
||||
-- Main exploration logic - heavily favor horizontal movement
|
||||
local r = math.random(1, 100)
|
||||
|
||||
if forwardVisited and r < 60 then
|
||||
-- If forward is visited, turn to find new direction (60% chance)
|
||||
local turnAttempts = 0
|
||||
while turnAttempts < 4 do
|
||||
if math.random() > 0.5 then
|
||||
smartTurnRight()
|
||||
else
|
||||
smartTurnLeft()
|
||||
end
|
||||
|
||||
-- Check new forward position
|
||||
if state.position then
|
||||
forwardPos = {x = state.position.x, y = state.position.y, z = state.position.z}
|
||||
if facing == 0 then forwardPos.z = forwardPos.z - 1
|
||||
elseif facing == 1 then forwardPos.x = forwardPos.x + 1
|
||||
elseif facing == 2 then forwardPos.z = forwardPos.z + 1
|
||||
elseif facing == 3 then forwardPos.x = forwardPos.x - 1
|
||||
end
|
||||
|
||||
if not hasVisited(forwardPos) then
|
||||
break -- Found unvisited direction
|
||||
end
|
||||
end
|
||||
turnAttempts = turnAttempts + 1
|
||||
end
|
||||
elseif r < 20 and canGoUp then
|
||||
-- Go up 20% of time to avoid getting too deep
|
||||
if not smartUp() then
|
||||
smartDig("up")
|
||||
smartUp()
|
||||
end
|
||||
elseif r < 30 and canGoDown and state.position and state.position.y > 10 then
|
||||
-- Go down only if not too deep (30% of time, above y=10)
|
||||
if not smartDown() then
|
||||
smartDig("down")
|
||||
smartDown()
|
||||
end
|
||||
else
|
||||
-- Try to move forward (main exploration)
|
||||
if canGoForward then
|
||||
if not smartForward() then
|
||||
-- Blocked, try to dig
|
||||
-- 75% of the time: horizontal exploration (forward movement + turns)
|
||||
if r < 75 then
|
||||
-- Try to move forward
|
||||
if not hasBlockForward or not forwardVisited then
|
||||
if hasBlockForward then
|
||||
smartDig("forward")
|
||||
if not smartForward() then
|
||||
-- Still blocked, turn
|
||||
if math.random() > 0.5 then
|
||||
smartTurnRight()
|
||||
else
|
||||
smartTurnLeft()
|
||||
end
|
||||
if smartForward() then
|
||||
-- Successfully moved forward
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
-- If forward is blocked or visited, try turning to find unvisited direction
|
||||
if forwardVisited or hasBlockForward then
|
||||
local turnAttempts = 0
|
||||
local bestDirection = nil
|
||||
|
||||
-- Check all 4 directions to find best unvisited one
|
||||
for i = 1, 4 do
|
||||
if state.position then
|
||||
local testPos = {x = state.position.x, y = state.position.y, z = state.position.z}
|
||||
if facing == 0 then testPos.z = testPos.z - 1
|
||||
elseif facing == 1 then testPos.x = testPos.x + 1
|
||||
elseif facing == 2 then testPos.z = testPos.z + 1
|
||||
elseif facing == 3 then testPos.x = testPos.x - 1
|
||||
end
|
||||
|
||||
if not hasVisited(testPos) then
|
||||
-- Found unvisited direction!
|
||||
if not turtle.inspect() then
|
||||
smartForward()
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Turn and check next direction
|
||||
smartTurnRight()
|
||||
end
|
||||
else
|
||||
-- Can't move forward, dig and try
|
||||
|
||||
-- All directions visited, just pick one and go
|
||||
smartDig("forward")
|
||||
if not smartForward() then
|
||||
-- Failed, turn to new direction
|
||||
if math.random() > 0.5 then
|
||||
smartTurnRight()
|
||||
else
|
||||
smartTurnLeft()
|
||||
end
|
||||
end
|
||||
smartForward()
|
||||
end
|
||||
|
||||
-- 15% down (only if safe and not too deep)
|
||||
elseif r < 90 and state.position and state.position.y > 15 and not downVisited then
|
||||
if hasBlockDown then
|
||||
smartDig("down")
|
||||
end
|
||||
smartDown()
|
||||
|
||||
-- 10% up (only if needed - not visited or somewhat deep)
|
||||
elseif r < 100 and (not upVisited or (state.position and state.position.y < 10)) then
|
||||
if hasBlockUp then
|
||||
smartDig("up")
|
||||
end
|
||||
smartUp()
|
||||
|
||||
-- Fallback: just move forward
|
||||
else
|
||||
if hasBlockForward then
|
||||
smartDig("forward")
|
||||
end
|
||||
if not smartForward() then
|
||||
-- Turn if can't move
|
||||
smartTurnRight()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Command handling
|
||||
local commands = {
|
||||
|
||||
Reference in New Issue
Block a user