Why aren't my paladins shielding?


#1

I am on Grim Determination and my paladins are not shielding. Here is my code:

# Your goal is to protect Reynaldo

# Find the paladin with the lowest health.
def lowestHealthPaladin():
    lowestHealth = 99999
    lowestFriend = None
    friends = self.findFriends()
    for friend in friends:
        if friend.type != "paladin":
            continue
        if friend.health < lowestHealth and friend.health < friend.maxHealth:
            lowestHealth = friend.health
            lowestFriend = friend

    return lowestFriend

def commandPaladin(paladin):
    # Heal the paladin with the lowest health using lowestHealthPaladin()
    # You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    # Paladins can also shield: command(paladin, "shield")
    # And don't forget, they can attack, too!
    lowestFriend=lowestHealthPaladin()
    
    if lowestFriend and paladin.canCast("heal",lowestFriend):
        self.command(paladin, "cast","heal", lowestFriend)
    enemy=paladin.findNearest(paladin.findEnemies())
    if enemy:
        self.command(paladin, "attack", enemy)
    self.command(paladin, "shield", enemy)
        
         
pass

def commandFriends():
    # Command your friends.
    friends = self.findFriends()
    for friend in friends:
        if friend.type == "peasant":
            item = friend.findNearest(friend.findItems())
            self.command(friend, "move", item.pos)
            pass
        elif friend.type == "griffin-rider":
            enemy=friend.findNearest(friend.findEnemies())
            if enemy:
                self.command(friend, "attack", enemy)
            pass
        elif friend.type == "paladin":
            commandPaladin(friend)

loop:
    commandFriends()
    if self.gold>self.costOf("griffin-rider"):
        self.summon("griffin-rider")


#2

Your paladins are shielding all the time. What they are not doing is attacking and healing :slightly_smiling:
You are always ending your commandPaladin with a shield command which will overwrite your previous command:
IF you give 2 commands to the same paladin in the same loop, only the last one will be followed:
attack + shield = shield
heal + shield = shield

You want something like this:

def commandPaladin(paladin):
    if should_i_heal:
        self.command( heal )
        return
   if  should_i_attack:
       self.command( attack)
       return
   if should_i_shield
       self.command ( shield )

Notice the return inside each if - it will immediately terminate the commandPaladin function, ensuring you can only give one command per function call