feat: Enhance exploration algorithm with intelligent navigation and stuck detection
This commit is contained in:
130
turtle.lua
130
turtle.lua
@@ -598,7 +598,36 @@ end
|
|||||||
|
|
||||||
-- Exploration algorithm with intelligent navigation
|
-- Exploration algorithm with intelligent navigation
|
||||||
local function exploreStep()
|
local function exploreStep()
|
||||||
-- Check all directions for valuable ores
|
-- Mark current position as visited
|
||||||
|
if state.position then
|
||||||
|
markVisited(state.position)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Check if stuck in same position
|
||||||
|
if isStuckInSamePosition() then
|
||||||
|
state.stuckInHoleCounter = state.stuckInHoleCounter + 1
|
||||||
|
print("Stuck counter: " .. state.stuckInHoleCounter)
|
||||||
|
else
|
||||||
|
state.stuckInHoleCounter = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Save current position for next check
|
||||||
|
if state.position then
|
||||||
|
state.lastPosition = {
|
||||||
|
x = state.position.x,
|
||||||
|
y = state.position.y,
|
||||||
|
z = state.position.z
|
||||||
|
}
|
||||||
|
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
|
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
|
||||||
@@ -607,27 +636,106 @@ local function exploreStep()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Random exploration with preference for going deeper
|
-- Smart movement decision making
|
||||||
local r = math.random(1, 10)
|
local canGoUp = canMoveInDirection("up")
|
||||||
if r <= 3 then
|
local canGoForward = canMoveInDirection("forward")
|
||||||
-- Go down 30% of the time
|
local canGoDown = canMoveInDirection("down")
|
||||||
if not smartDown() then
|
|
||||||
smartDig("down")
|
-- If we're too deep (below y=0) or stuck, prioritize going up
|
||||||
smartDown()
|
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
|
end
|
||||||
elseif r <= 5 then
|
else
|
||||||
-- Turn
|
smartDig("up")
|
||||||
|
if smartUp() then
|
||||||
|
print("Dug up and climbed")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Intelligent direction selection based on visited positions
|
||||||
|
local forwardPos = nil
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Prefer unvisited directions
|
||||||
|
local forwardVisited = hasVisited(forwardPos)
|
||||||
|
|
||||||
|
-- Decision tree for exploration
|
||||||
|
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
|
if math.random() > 0.5 then
|
||||||
smartTurnRight()
|
smartTurnRight()
|
||||||
else
|
else
|
||||||
smartTurnLeft()
|
smartTurnLeft()
|
||||||
end
|
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
|
else
|
||||||
-- Move forward
|
-- Try to move forward (main exploration)
|
||||||
|
if canGoForward then
|
||||||
if not smartForward() then
|
if not smartForward() then
|
||||||
|
-- Blocked, try to dig
|
||||||
smartDig("forward")
|
smartDig("forward")
|
||||||
if not smartForward() then
|
if not smartForward() then
|
||||||
|
-- Still blocked, turn
|
||||||
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user