Timber Turncoat help

My code is fine, it fineshes the level (as long as those death frisbee thingies don’t hit a peasant) but it glitches out and my hero does this wierd turning back and fourth thing, as if he stein one direction and then another.

c = 0
while True:
    for friend in hero.findFriends():
        if friend.type == "soldier":
            enemy = friend.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", {"x" : 80, "y" : 40})
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    # If you have enough gold, summon a soldier.
    if hero.gold >= hero.costOf("soldier") and c < 5:
        hero.summon("soldier")
        c += 1
    elif c > 4:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
        else:
            hero.move({"x" : 80, "y" : 40})
          

i tried putting “if item and c < 5” in the code but then the hero loses the level, even thought this should make him arrive to fight earlier. What is wrong here?

also all the if c > or if c < stuff is to make certain parts of the code only run at certain times

c = 0
while True:
    array = hero.findFriends()
    
    for friend in hero.findFriends():
        if friend.type == "soldier":
            enemy = friend.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", {"x" : 80, "y" : 40})
    item = hero.findNearestItem()
    if item and len(array) < 16:
        hero.move(item.pos)
    # If you have enough gold, summon a soldier.
    if hero.gold >= hero.costOf("soldier") and c < 5:
        hero.summon("soldier")
        c += 1
    elif c > 4:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
        else:
            hero.move({"x" : 80, "y" : 40})
            

fixed the code but id still like to make it make more sense, what i did here is count the peasants (11) and make 5 soldiers (total 16, which is why array has to be less than 16) which i shouldn’t have to do because the variable c should work the same yet it doesnt.

also the code will mess up and make the hero collect gold if a peasant is killed but if that happens i lost anyways

c = 0
array = hero.findFriends()
while True:
    for friend in hero.findFriends():
        if friend.type == "soldier":
            enemy = friend.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", {"x" : 80, "y" : 40})
    item = hero.findNearestItem()
    if item and len(array) < 16:
        hero.move(item.pos)
    if hero.gold >= hero.costOf("soldier") and c < 5:
        hero.summon("soldier")
        c += 1
    elif c > 4:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
        else:
            hero.move({"x" : 80, "y" : 40})
    if len(array) < 16:
        array = hero.findFriends()   

i also just locked the code so if len(array) ever equals 16 the game will never reset it to 15 or lower (like if a soldier gets killed)

also one more thing i try to get the hero to stand in the way of the death to peasant frisbee things but he just moves on no matter what

The second to last code you posted pretty much solves the level, you just need to keep the hero on the left side of the map. If you move the hero to the right then headhunters and a shaman appear, whereas if you just stay on the left collecting gold, only scouts turn up, and one by one over the level so that the soldiers can easily kill them.
Danny

A shaman appears both ways

also,
the last peice of code I posted now works for some reason. I solved it days ago but that was just replaying the same code over and over again. It now works without pressing the ‘submit’ button.