Summit's Gate infinite loop issue

Hi All,

I’m trying to do the Summit’s Gate level, but after destroying the second catapult everything seizes up due to ‘running slow or infinite loop’

I have got farther that this previously in the level but my main problem I thing is not knowing how to debug these infinite loop issues and what is causing them. Can anyone give me some tips please about the best ways. I’ve tried putting hero.say things in but it seems like the problem must be elsewhere in the code (maybe not?)

Thanks for any help.

# Fight your way into the Inner Sanctum of the ogre chieftain, and defeat her.
# You need 2 archers and 2 soldiers.
def att(enemy):
    
    if enemy and hero.distanceTo(enemy)>5 and hero.isReady("jump") and enemy.type!='tower':
        hero.jumpTo(enemy.pos)
    elif enemy and enemy.health>10 and hero.canCast("chain-lightning", enemy):
        hero.cast("chain-lightning", enemy)
        if hero.isReady("shield"):
            hero.shield()
    elif enemy and hero.isReady("cleave") and hero.distanceTo(enemy)<5:
        hero.cleave(enemy)
        if hero.isReady("shield"):
            hero.shield()
    elif enemy and hero.isReady("bash") and hero.distanceTo(enemy)<3:
        hero.bash(enemy)
        if hero.isReady("shield"):
            hero.shield()
    elif enemy:
        hero.attack(enemy)
    elif enemy and hero.canCast("chain-lightning", enemy):
        hero.cast("chain-lightning", enemy)
        if hero.isReady("shield"):
            hero.shield()
            
            
# This function can be helpful.
def summonAndSend(type, x, y):
    while hero.gold>hero.costOf(type):
        hero.summon(type)
        unit = hero.built[len(hero.built)-1]
        hero.command(unit, "move", {"x": x, "y": y})

# Find the paladin with the lowest health.
def lowestHealthPaladin():
    lowestHealth = 99999
    lowestFriend = None
    friends = hero.findByType("paladin", hero.findFriends())
    for friend in friends:
        if friend.health < lowestHealth and friend.health < friend.maxHealth:
            lowestHealth = friend.health
            lowestFriend = friend
    return lowestFriend

def commandPaladin():
    # 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!
    paladins=hero.findByType("paladin", hero.findFriends())
    for paladin in paladins:
        if paladin.canCast("heal"):
            needhealing=lowestHealthPaladin()
            if hero.health<hero.maxHealth:
                hero.command(paladin, "cast", "heal", hero)
            elif needhealing:
                #hero.say('Paladin healing: '+needhealing)
                hero.command(paladin, "cast", "heal", needhealing)
        enemy=paladin.findNearestEnemy()
        if enemy:
            #hero.say('Paladin should be attacking: '+enemy)
            hero.command(paladin, "attack",enemy)
            #hero.command(paladin, "shield")
        elif paladin.health<200:
            hero.command(paladin, "shield")
        pass

def commandSoldier():
    for soldier in soldiers:
        target = soldier.findNearestEnemy()
        if target:
            hero.command(soldier, "attack", target)

def commandGriffin():
    griffins=hero.findByType("griffin-riders", hero.findFriends())
    for griffin in griffins:
        enemies=griffin.findEnemies()
        for enemy in enemies:
            if enemy.type=='warlock':
                hero.command(friend, "attack", enemy)
                break
            else:
                enemy=griffin.findNearestEnemy()
                hero.command(griffin, "attack", enemy)
        

def commandArcher():
    archers=hero.findByType('archer',hero.findFriends())
    for archer in archers:
        enemy=archer.findNearestEnemy()
    if enemy:
        hero.command(archer, "attack", enemy)

def commandPeasant(friend):
    coins=friend.findItems()
    coin=friend.findNearestItem()
    hero.command(friend, "move", coin.pos)

def commandFriends():
    # Command your friends.
    commandGriffin()
    commandArcher()
    commandSoldier()
    commandPaladin()
  
def commandFriendsMoveTo(targetLoc):
    friends=hero.findFriends()
    for friend in friends:
        #hero.say('Move: '+friend)
        hero.command(friend, "move", targetLoc)
        
def commandTypeMoveTo(targetLoc,ftype):
    friends=hero.findByType(ftype, hero.findFriends())
    for friend in friends:
        #hero.say('Move: '+friend)
        hero.command(friend, "move", targetLoc)

def killByType(enemyType):
    enemies=hero.findByType(enemyType, hero.findEnemies())  
    for enemy in enemies:
        while enemy.health>0:
            att(enemy)
            
def getNearestByType(enemyType):
    enemies=hero.findByType(enemyType, hero.findEnemies()) 
    nearest=None
    nearDist=9999
    for enemy in enemies:
        dist=hero.distanceTo(enemy)
        if dist<nearDist:
            nearDist=dist
            nearest=enemy
    return nearest
        
            
def palsHealHero():
    hero.say('about to be healed')
    paladins=hero.findByType('paladin', hero.findFriends()) 
    if len(paladins)<2 and hero.gold>hero.costOf('paladin'):
        hero.say('summoning paladin')
        hero.summon('paladin')
    paladins=hero.findByType('paladin', hero.findFriends()) 
    counter=0
    while hero.health<hero.maxHealth and len(paladins)>0:
        paladins=hero.findByType('paladin', hero.findFriends())
        for paladin in paladins:
            healthLev=hero.health
            if hero.health<hero.maxHealth and paladin.canCast("heal") and hero.distanceTo(paladin)<15:
                hero.say('being healed'+counter)
                hero.command(paladin, "cast", "heal", hero)
                counter+=1
            elif hero.health<hero.maxHealth and paladin.canCast("heal") and hero.distanceTo(paladin)>=15:
                hero.command(paladin, "move", hero.pos)

def everyoneAttackSkeletons():
    #except paladins and griffin-riders
    allies=hero.findFriends()
    for ally in allies:
        enemy=ally.findNearestEnemy()
        if enemy and ally.type!='griffin-rider' and ally.type!='paladin' and enemy.type=='skeleton':
            hero.command(ally, "attack", target)

ftype='griffin-rider'     
commandFriendsMoveTo({"x":3,"y":40})
killByType("catapult")
killByType("scout")
killByType("munchkin")
hero.say('about to move killed all sect1')
commandFriendsMoveTo({"x":67,"y":40})
hero.say('moved friends')
hero.moveXY(92, 36)

paladins=hero.findByType("paladin", hero.findFriends())
palsHealHero()
summonAndSend('griffin-rider', 67, 40)

commandFriendsMoveTo({"x":50,"y":37})

#going to breakdown nearest door
door=getNearestByType('door')
while door.health>0:
    att(door)

hero.moveXY(60, 33)
killByType("thrower")
killByType("munchkin")

palsHealHero()
summonAndSend('griffin-rider', 50, 40)

commandFriendsMoveTo({"x":50,"y":37})

#Kill towers
hero.moveXY(124, 4)
enemies=hero.findByType("tower")
while enemies[0].health>0:
    att(enemies[0])

#get healed
commandFriendsMoveTo({"x":90,"y":37})
hero.moveXY(93,35)
palsHealHero()
summonAndSend('griffin-rider', 90, 40)

#kill other tower
hero.moveXY(122, 64)
enemies=hero.findByType("tower")
while enemies[0].health>0:
    att(enemies[0])

commandFriendsMoveTo({"x":127,"y":34})
hero.moveXY(136,35)
palsHealHero()
summonAndSend('griffin-rider', 127, 40)

#going to breakdown nearest door 2
door=getNearestByType('door')
while door.health>0:
    att(door)
    
    
#Lure Ogres out
commandFriendsMoveTo({"x":127,"y":34})
hero.moveXY(167, 35)
killByType("ogre")
hero.moveXY(167, 35)
killByType("ogre")


commandFriendsMoveTo({"x":219,"y":15}) 
palsHealHero()
summonAndSend('griffin-rider', 219, 15)


#Draw out warlocks
while hero.pos.x<273:
    hero.move({"x":277,"y":34})
hero.say('where the warlocks are')
warlocks=hero.findByType("warlock", hero.findEnemies())
#run away
while hero.pos.x>230:
    hero.move({"x":228,"y":15})
    
    
#Hopefully kill warlocks with griffins and everything else with hero and others
while True:
    commandGriffin()
    summonAndSend('griffin-rider', 241, 16)
    commandFriends()
    commandGriffin()
    enemy=hero.findNearestEnemy()
    if enemy:
        att(enemy)

I think the issue was in the pals heal hero function.

I put a hero.say in a while loop that is checking if the hero can be further healed and the code didn’t slow down. Maybe because there is a long wait before healings too many iterations of the loop are occurring and it’s not liked? (Maybe not?)

Welcome back @charliefik,
I think you are misusing this line. “find by type” method takes one argument, which is type, so you need to put it in. For example: paladin = hero.findByType("paladin")

Same goes here:

instead, you can put enemies = hero.findEnemies() or if you want to specify type: munchkin = hero.findByType("munchkin")

You should check all the other hero.findByType("type") lines and enter only one argument.

Hopefully that helps.

1 Like

Hi Aya,

Thanks for the suggestion.

I know you can use hero.findByType(“paladin”) as you suggested.

but if you do it using hero.findByType(enemyType, hero.findFriends()) it narrows down the group of units you are searching for by, in this example friendly units (optional parameter). I think this is to help in situations where there might be friendly and unfriendly paladins for example on the field.

The Methods section in CodeCombat has an example.

I think I am using it correctly so the problem may be somewhere else.

1 Like

I am using enchanted lenses (which may be different to the glasses you are using so may have different methods) I’m thinking about getting the xray ones so I can see enemies hidden behind walls (otherwise it’s hard to attack them)