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)