Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] I need help with Library Tactician please

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()
    # Figure out which enemy has the most health, and set bestTarget to be that enemy.
    for enemy in enemies:
        if enemy.health > mostHealth:
            mostHealth = enemy.health
            bestTarget = enemy
    # Only focus archers' fire if there is a big ogre.
    if bestTarget and bestTarget.health > 15:
        return bestTarget
    else:
        return None

def commandArcher(archer):
    nearest = archer.findNearestEnemy()
    if archerTarget:
        hero.command(archer, "attack", archerTarget)
    elif nearest:
        hero.command(archer, "attack", nearest)

archerTarget = None

while True:
    if not archerTarget or archerTarget.health <= 0:
        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 j in range(len(archers)):
        archers = archers[j]
        commandArcher(archers)

it says that i can’t have my archers find the nearest enemy

    for j in range(len(archers)):
        archers = archers[j]
        commandArcher(archers)

Looks like you’re getting archer and archers mixed up in this phrase…?

1 Like

hi @jka2706, thanks for that but it still says that my archers can’t findNearestEnemy()

would it help if i sent a screenshot?

Sometimes friends can’t findNearest(), so you need to put in something like hero.findNearest(friend.findEnemies()). But some of the time friends can findNearestEnemy(). I haven’t worked this one out yet.

1 Like

i tried it but the same result came out
here’s the new 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):
    nearest = hero.findNearest(archer.findNearestEnemy())
    if archerTarget:
        hero.command(archer, "attack", archerTarget)
    elif nearest:
        hero.command(archer, "attack", nearest)

archerTarget = None

while True:
    if not archerTarget or archerTarget.health >= 0:
        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 j in range(len(archers)):
        archer = archers[j]
        commandArcher(archers)

Try commandArcher(archer) instead - you only want to give the instruction to one archer at a time.

1 Like

hi again. i put archers but they still have the same problem (they just attack the nearest enemy)

If you do what @jka2706 says it will work, I’ve tested it. Archers is currently an array. You can’t get an array to find a nearest enemy. You have to use the variable you’ve created: archer (singular)
Danny

1 Like

hi, i beat the level. thanks a lot @jka2706 and @Deadpool198. although there’s this one ogre who got stuck next to the forest :joy:

Hey, well done!

I think I had a stuck ogre as well :woman_shrugging:.

1 Like

Yes, sorry. I probably caused more confusion than I solved :grin:
Danny

1 Like