Stranded in the dunes without flags [python]

Hi there! I’ve taken it upon myself to complete the Stranded in the Dunes level without using flags to avoid obstacles. Here is the current version of my code.

Current Version
def searchAndDestroy(target):
    if hero.canCast("lightning-bolt"):
        hero.cast("lightning-bolt", target)
    elif hero.canCast("chain-lightning"):
        hero.cast("chain-lightning", target)

def treadLightly(target):
    safety = hero.distanceTo(target)
    if safety < 8:
        if (hero.pos.y >= target.pos.y + 3) or (hero.pos.y <= target.pos.y - 3):
                hero.moveXY(target.pos.x + 5, hero.pos.y)
            hero.moveXY(hero.pos.x, hero.pos.y + 8)

while True:
    enemy = hero.findNearestEnemy()
    mine = hero.findNearestItem()
    if < hero.maxHealth - 100 and hero.canCast("regen"):
        hero.cast("regen", self)
    elif enemy:
        if enemy.type != "sand-yak":
    elif mine:
        hero.moveXY(hero.pos.x + 5, hero.pos.y)

This works well enough to move my hero through the level alive except for 2 issues.

Issue 1: Yaks thankfully have ran around my hero in all the tests, but ideally I’d want to be able to check the position of the yak herd in reference to the hero’s position so he can dodge those as well while still making sure not to run into fire-traps.
Issue 2: The big one. If terrain gets in the way (which it does on the last part of the stage or if while dodging fire-traps on the edge of the map) the hero just gets stuck and can’t proceed. So how can I have my program check for impassible terrain so that I can have him maneuver past it?

Issue 1: You could use try the findNearestByType function to find the distance between the nearest yak and you. Then, in your code, if the yak’s Y position is too close to yours, move up or move down your y position by using hero.moveXY(hero.pos.x, hero.pos.y - [insert distance]);

Issue 2: Since you only get stuck on the final part, maybe in your code say something such as: if( equals “Skeleton King” (this will check if the nearest enemy’s name is that), then you know you are on the final part and stop moving right into an impassible terrain.