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
|
-- 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
|
||||||
|
return
|
||||||
-- 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
|
|
||||||
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
|
||||||
local turnAttempts = 0
|
-- Try to move forward
|
||||||
while turnAttempts < 4 do
|
if not hasBlockForward or not forwardVisited then
|
||||||
if math.random() > 0.5 then
|
if hasBlockForward 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
|
|
||||||
smartDig("forward")
|
smartDig("forward")
|
||||||
if not smartForward() then
|
end
|
||||||
-- Still blocked, turn
|
if smartForward() then
|
||||||
if math.random() > 0.5 then
|
-- Successfully moved forward
|
||||||
smartTurnRight()
|
return
|
||||||
else
|
end
|
||||||
smartTurnLeft()
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Turn and check next direction
|
||||||
|
smartTurnRight()
|
||||||
end
|
end
|
||||||
else
|
|
||||||
-- Can't move forward, dig and try
|
-- All directions visited, just pick one and go
|
||||||
smartDig("forward")
|
smartDig("forward")
|
||||||
if not smartForward() then
|
smartForward()
|
||||||
-- Failed, turn to new direction
|
end
|
||||||
if math.random() > 0.5 then
|
|
||||||
smartTurnRight()
|
-- 15% down (only if safe and not too deep)
|
||||||
else
|
elseif r < 90 and state.position and state.position.y > 15 and not downVisited then
|
||||||
smartTurnLeft()
|
if hasBlockDown then
|
||||||
end
|
smartDig("down")
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- Command handling
|
-- Command handling
|
||||||
local commands = {
|
local commands = {
|
||||||
|
|||||||
Reference in New Issue
Block a user