The logic is a bit obtuse, but basically: Covemonty has two states, normal and concerned. In Normal state, Covemonty will look for the shortest path to the goal, ignoring everything except walls. If that fails, i.e. Covemonty gets stuck, they'll become concerned and try again, this time avoiding all obstacles, water included. If even that fails, they'll get tired and stop altogether.