diff --git a/turtle.lua b/turtle.lua index 04eb36e..028c37e 100644 --- a/turtle.lua +++ b/turtle.lua @@ -598,7 +598,36 @@ end -- Exploration algorithm with intelligent navigation 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 local dug, valuable, blockName = smartDig(direction) if valuable then @@ -607,27 +636,106 @@ local function exploreStep() end end - -- Random exploration with preference for going deeper - local r = math.random(1, 10) - if r <= 3 then - -- Go down 30% of the time + -- 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 + 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 + 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 - elseif r <= 5 then - -- Turn - if math.random() > 0.5 then - smartTurnRight() - else - smartTurnLeft() - end else - -- Move forward - if not smartForward() then + -- Try to move forward (main exploration) + if canGoForward then + if not smartForward() then + -- Blocked, try to dig + smartDig("forward") + if not smartForward() then + -- Still blocked, turn + if math.random() > 0.5 then + smartTurnRight() + else + smartTurnLeft() + end + end + end + else + -- Can't move forward, dig and try smartDig("forward") if not smartForward() then - smartTurnRight() + -- Failed, turn to new direction + if math.random() > 0.5 then + smartTurnRight() + else + smartTurnLeft() + end end end end