# Library Tactician - Problem

Hi,

I’ve got a problem in this level. After 30-40 seconds the archers stop attacking and the error “Statement execution limit reached.” appears. Can someone help me please?

Here is my code:

``````def commandSoldier(soldier, soldierIndex, numSoldiers):

angle = Math.PI * 2 * soldierIndex / numSoldiers
defendPos = {"x": 41, "y": 40}
defendPos.x += 10 * Math.cos(angle)
defendPos.y += 10 * Math.sin(angle)
hero.command(soldier, "defend", defendPos);

def findStrongestTarget():

mostHealth = 0
bestTarget = None
enemies = hero.findEnemies()
for enemy in enemies:
if enemy.health > mostHealth:
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None

def commandArcher(archer):

if archerTarget:
hero.command(archer, "attack", archerTarget)
else:
nearest = archer.findNearest(archer.findEnemies())
if nearest:
hero.command(archer, "attack", nearest)

archerTarget = None
loop:

if not archerTarget or archerTarget.health < 1:
archerTarget = findStrongestTarget()
friends = hero.findFriends()
soldiers = hero.findByType("soldier")
archers = hero.findByType("archer")
for i in range(len(soldiers)):
soldier = soldiers[i]
commandSoldier(soldier, i, len(soldiers))
for i in range(len(archers)):
archer = archers[i]
commandArcher(archer, i, len(archers))
``````
2 Likes

again?

1 Like

Well, I have tried it more than 20 times, so…

1 Like

You could try reducing execution of code with a couple of optimizations (though the first one is the important one):

1. It seems like you only need to command the soldiers to defend once; so you can move the necessary code to before the main loop.
2. You don’t need to call `findFriends()` if `findByType()` gives you enough information.
3. You only need to find your friends once, because every call after that returns the same result.
4. `commandArcher(archer)` only takes in an `archer` parameter, so no need to pass in extra arguments.
1 Like

Wow! Thank you very much!

1 Like

It runs so much better and faster with those changes. I would suggest the base code be changed around a bit, maybe?