Summit's Gate Help

def attack():
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)

def commandArcher(archer):
    for friend in hero.findByType("archer"):
        tower = hero.findByType("tower")
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)

def commandSoldier(soldier):
    friends = hero.findFriends()
    for friend in hero.findByType("soldier"):
        catapult = friend.findNearest(hero.findByType("catapult"))
        if catapult:
            hero.command(friend, "move", {'x': catapult.pos.x, 'y': catapult.pos.y})

def commandPaladin(paladin):
    enemy = hero.findNearestEnemy()
    friends = hero.findFriends()
    friend = hero.findNearest(friends)
    if paladin.canCast("heal") and hero.health <= 1000:
        hero.command(paladin,"cast", "heal", hero)
    elif enemy:
        hero.command(paladin, "attack", enemy)

def commandFriends():
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "paladin":
            commandPaladin(friend)
        if friend.type == "archer":
            commandArcher(friend)
        if friend.type == "soldier":
            commandSoldier(friend)

while True:
    commandFriends()
    attack()

This is my code and whenever I run it, the soldiers run after the catapult and attack it but my hero dies and the level fails. I’ve been stuck on this for days and I can’t figure out how to remove the catapults

What hero are you using and with what gear? I’ve tried your code with two heroes and the best armor and can pass the first section with the catapults and just barely the towers.

One thing I noticed, your hero targets the door first in the tower room and you attack that taking a lot of damage from the towers. If you are using a warrior class hero, once the attack command runs it won’t stop until it attacks the enemy opening you up to some damage if the enemy is far away. To prevent this, you can add a distance check and move to the enemy to get within range before calling the attack.

I’m using tharin. Here’s a screenshot of my character.


Also, whenever I run it, my hero dies right before the outer gate falls.

Is your hero being killed by the fire bombs? Just for fun, I commented out the attack() function and your hero doesn’t take any damage standing in the back. One of the tricks I learned on this level, let your team do the fighting while you hang back and command. Also, try to segment out the different sections with specific code of attack. One way to do that is wait to attack the next door until the current section is complete, heal as many as you can and also summon more. The second section, I became the distraction in the middle with a good shield and let my archers attack from a distance and paladins continue to heal. Although, your current shield probably won’t hold up well enough for that tactic.

My hero rushes with the other soldiers and attacks the catapults and dies while fighting the catapult. What I’m about to try to do is to have the hero stand back like you said earlier. I’ll definitely report back on how it went.

So I added some changes. Some errors i’m still experiencing is that all my other friends are dying, and then my hero is either not moving, or being pinned against the wall by a thrower. It seems to happen every time I test.

def attack():
    enemies = hero.findEnemies()
    for enemy in enemies:
        if enemy:
            hero.attack(enemy)
    

def commandArcher(archer):
    for friend in hero.findByType("archer"):
        tower = hero.findByType("tower")
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)

def commandSoldier(soldier):
    friends = hero.findFriends()
    for friend in hero.findByType("soldier"):
        catapult = friend.findNearest(hero.findByType("catapult"))
        if catapult:
            hero.command(friend, "move", {'x': catapult.pos.x, 'y': catapult.pos.y})

def commandPaladin(paladin):
    enemy = hero.findNearestEnemy()
    friends = hero.findFriends()
    friend = hero.findNearest(friends)
    if paladin.canCast("heal") and hero.health <= 1000:
        hero.command(paladin,"cast", "heal", hero)
    elif enemy:
        hero.command(paladin, "attack", enemy)

def commandFriends():
    friends = hero.findFriends()
    for friend in friends:
        if friend:
            if friend.type == "paladin":
                commandPaladin(friend)
            if friend.type == "archer":
                commandArcher(friend)
            if friend.type == "soldier":
                commandSoldier(friend)

while True:
    commandFriends()
    attack()

Your adjustment to the code creates a problem by finding all the enemies and then attacking them one by one, but by the time you go to attack the second one, it is already dead and so are most of the group. Usually your hero will say “But it’s dead” to let you know you targeted a dead enemy.

def attack():
    enemies = hero.findEnemies() # one list of enemies
    for enemy in enemies: # goes through each one until he attacks them
        if enemy: # there was an enemy, but now it's dead
            hero.attack(enemy) # no one to attack and seems to get stuck

Use a for loop to run through all the enemies and check for some aspect you want to target to pick the best target. Then attack that one specific target.

    d = 999
    for enemy in enemies:
        if enemy.type == "catapult" or enemy.type == "door": # won't target these 
            continue
        elif hero.distanceTo(enemy) < d and enemy.health >100: # looking for bigger and the closest
            target = enemy
            d = hero.distanceTo(enemy)
    if target:
        while target.health > 0:
            hero.attack(target)

Add another else statement at the end if you want to attack other targets.