# 你的目标是保护 Reynaldo

# 找到生命值最低的武士
def lowestHealthPaladin():
    lowestHealth = 99999
    lowestFriend = None
    friends = hero.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 commandPeasant(peasant):
    item = peasant.findNearestItem()
    if item:
        hero.command(peasant, "move", item.pos)

def commandGriffin(griffin):
    target = hero.findNearest(hero.findByType('warlock'))
    if target:
        hero.command(griffin, "attack", target)
    else:
        target = griffin.findNearestEnemy()

def commandPaladin(paladin):
    # 使用函数 lowestHealthPaladin() 找到生命值最低的武士,并治疗
    # 你能使用 paladin.canCast("heal") 和 command(paladin, "cast", "heal", target)
    # 武士也能防御:command(paladin, "shield")
    # 不要忘了他们还能***
    target = hero.findNearest(hero.findByType('warlock'))
    if paladin.health < 250:
        hero.command(paladin, "shield")
    else:
        if paladin.canCast("heal"):
            healtarget = lowestHealthPaladin()
            # and healtarget and healtarget.health < 400:
            if healtarget:
                hero.command(paladin, "cast", "heal", healtarget)
        
        else:
            target = paladin.findNearestEnemy()
            hero.command(paladin, "attack", target)
            


def commandFriends():
    # 指挥你的队友
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "peasant":
            commandPeasant(friend)
            
        if friend.type == "griffin-rider":
            commandGriffin(friend)
            
        if friend.type == "paladin":
            commandPaladin(friend)

while True:
    commandFriends()
    # 召唤 格里芬骑士
    if hero.gold >= hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")