Kevintalph Crusader: How do you make the troops attack the witch?


#1

Hi guys, I’m having trouble with this level. Can you help me? Thanks!
Here’s my code

hero.moveXY(68, 16)
def commandFriends():
    if paladin:
        if witch:
            hero.command(paladin, "attack", witch)
        else:
            hero.command(friend, "attack", enemy)
        if friend.health < friend.maxHealth - 40:
            hero.command(friend, "cast", "heal", friend)
        
loop:
    friend = hero.findNearest(hero.findFriends())
    enemy1 = hero.findNearestEnemy()
    enemyMissile = hero.findNearest(hero.findEnemyMissiles())
    paladin = hero.findNearest(hero.findByType("paladin", hero.findFriends()))
    if enemyMissile and hero.distanceTo(enemyMissile) < 5:
        if hero.isReady("shield"):
            hero.shield()
        else:
            hero.moveXY(12, 12)
    if enemy1 and enemy1.type == "catapult":
        hero.attack(enemy1)
    elif enemy1 > 20:
        hero.attack(enemy1)
    else:
        hero.moveXY(79, 14)

Any suggestions? @UltCombo ? @Harry_the_Wanderer ?


#2

As far as I can see if witch: hero.command(paladin, "attack", witch) You never actually identify what the witch is. You have enemy1 and friend and paladin, so that’s all okay. But where is the witch?


#3

This doesn’t work either. I incorporated your suggestion but it doesn’t work. Any other suggestions? Here is my code:

# You can find friends through walls, but not enemies.
# Watch out for smooth, frictionless ice patches!
hero.moveXY(68, 16)
def commandFriends():
    if paladin:
        witch = friend.findByType("witch")
        if witch:
            hero.command(paladin, "attack", witch)
        else:
            hero.command(friend, "attack", enemy)
        if friend.health < friend.maxHealth - 40:
            hero.command(friend, "cast", "heal", friend)
        
loop:
    friend = hero.findNearest(hero.findFriends())
    enemy1 = hero.findNearestEnemy()
    enemyMissile = hero.findNearest(hero.findEnemyMissiles())
    paladin = hero.findNearest(hero.findByType("paladin", hero.findFriends()))
    if enemyMissile and hero.distanceTo(enemyMissile) < 5:
        if hero.isReady("shield"):
            hero.shield()
        else:
            hero.moveXY(12, 12)
    if enemy1 and enemy1.type == "catapult":
        hero.attack(enemy1)
    elif enemy1 > 20:
        hero.attack(enemy1)
    else:
        hero.moveXY(79, 14)

Thanks!


#4

if friend.health < friend.maxHealth - 40: hero.command(friend, "cast", "heal", friend)
this might be throwing up your code, especially after coming after a Else statement, try switching that up
What error is it giving you now? Also with this code paladin = hero.findNearest(hero.findByType("paladin", hero.findFriends())) try changing that up a bit, i would suggest using something like
paladins = hero.findByType("paladin") nearest = hero.findNearest(paladins)
And what was this supposed to be? elif enemy1 > 20: enemy1 >20 what? IT’s not defining distance, or health. So that could be the problem as well.
As i said, knowing your code issue would make it a lot easier.


#5

There are two problems with the line you just added.

witch = friend.findByType("witch")

First off, findByType() is a function that takes returns an array based on TWO parameters: the object ID and an array of objects.
If you want to find the witch, you’re going to need to search your array of enemies, as such:
witch = friend.findByType("witch", friend.findEnemies())

witch is now storing an array, but you want it to store the first witch IN the array, like this:
witch = friend.findByType("witch", friend.findEnemies())[0]


#6

This is true ^ however! There are more than 2 problems with the code, as well as that, using findByType, there are no witches on your team, so findEnemies is not needed. <- Presumably.


#7

As previously stated, there are multiple problems with the code, knowing the code error would make it a lot easier to determine what is troubling you as of now.


#8

Hi there! I know this topic is old, but I still haven’t solved this level. My troops on this level seem to not obey my commands, and I make it out alive while my not even one of my allies make it.
Here is my code:

# You can find friends through walls, but not enemies.
# Watch out for smooth, frictionless ice patches!
hero.moveXY(68, 16)
def commandFriends():
    if paladin:
        witch = friend.findByType("witch")
        if witch:
            hero.command(paladin, "attack", witch)
        else:
            hero.command(friend, "attack", enemy)
        if friend.health < friend.maxHealth - 40:
            hero.command(friend, "cast", "heal", friend)
        
loop:
    friend = hero.findNearest(hero.findFriends())
    enemy1 = hero.findNearestEnemy()
    enemyMissile = hero.findNearest(hero.findEnemyMissiles())
    paladin = hero.findNearest(hero.findByType("paladin", hero.findFriends()))
    commandFriends()
    if enemyMissile and hero.distanceTo(enemyMissile) < 5:
        if hero.isReady("shield"):
            hero.shield()
        else:
            hero.moveXY(12, 12)
    if enemy1 and enemy1.type == "catapult":
        hero.attack(enemy1)
    elif enemy1 > 20:
        hero.attack(enemy1)
    else:
        hero.moveXY(79, 14)

Thanks!


#9

Instead of finding friends in your loop, you can try finding the friends inside the commandFriends() function.

def commandFriends():
    # get a list of all your friends
    
    # loop through each friend
        # based on your friend's type, give them specialized commands

...

loop:
    # friend = hero.findNearest(hero.findFriends())
    enemy1 = hero.findNearestEnemy() # heroTarget is a clearer name than enemy1
    enemyMissile = hero.findNearest(hero.findEnemyMissiles())
    # paladin = hero.findNearest(hero.findByType("paladin", hero.findFriends()))
    ...

You might also want to find enemies for your friends to attack in the commandFriends() function too.