Library Tactician - Problem



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 > mostHealth:
            mostHealth =
            bestTarget = enemy
    if bestTarget and > 15:
        return bestTarget
        return None

def commandArcher(archer):

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

archerTarget = None

    if not archerTarget or < 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))




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


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.


Wow! Thank you very much! :slight_smile:


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