feat: Improve exploration logic with enhanced stuck detection and direction prioritization

This commit is contained in:
MayaTheShy
2026-02-19 21:30:35 -05:00
parent d43887ed41
commit 7003620ef7

View File

@@ -606,7 +606,9 @@ local function exploreStep()
-- Check if stuck in same position -- Check if stuck in same position
if isStuckInSamePosition() then if isStuckInSamePosition() then
state.stuckInHoleCounter = state.stuckInHoleCounter + 1 state.stuckInHoleCounter = state.stuckInHoleCounter + 1
print("Stuck counter: " .. state.stuckInHoleCounter) if state.stuckInHoleCounter > 5 then
print("⚠️ Stuck for " .. state.stuckInHoleCounter .. " steps!")
end
else else
state.stuckInHoleCounter = 0 state.stuckInHoleCounter = 0
end end
@@ -620,126 +622,127 @@ local function exploreStep()
} }
end 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) -- Check all directions for valuable ores first (priority)
for _, direction in ipairs({"forward", "up", "down"}) do for _, direction in ipairs({"forward", "up", "down"}) do
local dug, valuable, blockName = smartDig(direction) local dug, valuable, blockName = smartDig(direction)
if valuable then if valuable then
print("Found: " .. blockName) print("💎 Found: " .. blockName)
broadcastStatus() broadcastStatus()
end end
end end
-- Smart movement decision making -- Only try to climb out if REALLY stuck (not just after a few steps)
local canGoUp = canMoveInDirection("up") if state.stuckInHoleCounter > 8 then
local canGoForward = canMoveInDirection("forward") print("Trying to escape - stuck for too long")
local canGoDown = canMoveInDirection("down") if tryClimbOut() then
-- 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 return
end end
end 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 forwardPos = nil
local upPos = nil
local downPos = nil
if state.position then if state.position then
-- Forward position
forwardPos = {x = state.position.x, y = state.position.y, z = state.position.z} forwardPos = {x = state.position.x, y = state.position.y, z = state.position.z}
if facing == 0 then forwardPos.z = forwardPos.z - 1 if facing == 0 then forwardPos.z = forwardPos.z - 1
elseif facing == 1 then forwardPos.x = forwardPos.x + 1 elseif facing == 1 then forwardPos.x = forwardPos.x + 1
elseif facing == 2 then forwardPos.z = forwardPos.z + 1 elseif facing == 2 then forwardPos.z = forwardPos.z + 1
elseif facing == 3 then forwardPos.x = forwardPos.x - 1 elseif facing == 3 then forwardPos.x = forwardPos.x - 1
end 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 end
-- Prefer unvisited directions -- Check which directions have been visited
local forwardVisited = hasVisited(forwardPos) 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) local r = math.random(1, 100)
if forwardVisited and r < 60 then -- 75% of the time: horizontal exploration (forward movement + turns)
-- If forward is visited, turn to find new direction (60% chance) if r < 75 then
-- Try to move forward
if not hasBlockForward or not forwardVisited then
if hasBlockForward then
smartDig("forward")
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 turnAttempts = 0
while turnAttempts < 4 do local bestDirection = nil
if math.random() > 0.5 then
smartTurnRight()
else
smartTurnLeft()
end
-- Check new forward position -- Check all 4 directions to find best unvisited one
for i = 1, 4 do
if state.position then if state.position then
forwardPos = {x = state.position.x, y = state.position.y, z = state.position.z} local testPos = {x = state.position.x, y = state.position.y, z = state.position.z}
if facing == 0 then forwardPos.z = forwardPos.z - 1 if facing == 0 then testPos.z = testPos.z - 1
elseif facing == 1 then forwardPos.x = forwardPos.x + 1 elseif facing == 1 then testPos.x = testPos.x + 1
elseif facing == 2 then forwardPos.z = forwardPos.z + 1 elseif facing == 2 then testPos.z = testPos.z + 1
elseif facing == 3 then forwardPos.x = forwardPos.x - 1 elseif facing == 3 then testPos.x = testPos.x - 1
end end
if not hasVisited(forwardPos) then if not hasVisited(testPos) then
break -- Found unvisited direction -- Found unvisited direction!
if not turtle.inspect() then
smartForward()
return
end end
end end
turnAttempts = turnAttempts + 1
end end
elseif r < 20 and canGoUp then
-- Go up 20% of time to avoid getting too deep -- Turn and check next direction
if not smartUp() then smartTurnRight()
smartDig("up")
smartUp()
end 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) -- All directions visited, just pick one and go
if not smartDown() then smartDig("forward")
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") smartDig("down")
end
smartDown() 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 end
smartUp()
-- Fallback: just move forward
else else
-- Try to move forward (main exploration) if hasBlockForward then
if canGoForward then
if not smartForward() then
-- Blocked, try to dig
smartDig("forward") smartDig("forward")
end
if not smartForward() then if not smartForward() then
-- Still blocked, turn -- Turn if can't move
if math.random() > 0.5 then
smartTurnRight() smartTurnRight()
else
smartTurnLeft()
end
end
end
else
-- Can't move forward, dig and try
smartDig("forward")
if not smartForward() then
-- Failed, turn to new direction
if math.random() > 0.5 then
smartTurnRight()
else
smartTurnLeft()
end
end
end end
end end
end end
end
-- Command handling -- Command handling
local commands = { local commands = {