I need help with this library tactician


#1

My characters keep dying please help.

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)
self.command(soldier, “defend”, defendPos);

Find the strongest target (most health)

This function returns something! When you call the function, you will get some value back.

def findStrongestTarget():
mostHealth = 0
bestTarget = None
enemies = self.findEnemies()
bestTarget = enemies
# Figure out which enemy has the most health, and set bestTarget to be that enemy.

# Only focus archers' fire if there is a big ogre.
if bestTarget and bestTarget.health > 15:
    return bestTarget
else:
    return None

If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.

def commandArcher(archer):
nearest = archer.findNearestEnemy()
if archerTarget:
self.command(archer, “attack”, archerTarget)
elif nearest:
self.command(archer, “attack”, nearest)

archerTarget = None

while True:
# If archerTarget is dead or doesn’t exist, find a new one.
if not archerTarget or archerTarget.health <= 0:
# Set archerTarget to be the target that is returned by findStrongestTarget()
archerTarget = findStrongestTarget()

friends = self.findFriends()
soldiers = self.findByType("soldier")

for i in range(len(soldiers)):
    soldier = soldiers[i]
    commandSoldier(soldier, i, len(soldiers));

# use commandArcher() to command your archers

commandArcher(archer)
commandSoldier(soldier)

#2

You need to command every single archer. To do this you need to combine 3 things:

  1. Find a list of archers. For soldiers you do:
soldiers = self.findByType("soldier")

How do you think you have to do for archers?

  1. Loop over every item in the archers array, For soldiers you do:
for i in range(len(soldiers)):
    soldier = soldiers[i]

How about for archers?

  1. Call the command archer function INSIDE the loop
#this is inside the loop so it is indented 
    commandArcher(archer)

Note: you do not need the last commandSoldier(soldier) line, delete it


#3

Uploading… I still die.


#4

Paste your code, properly formatted.
Tell us exactly what is the issue: archers not attacking, attacking the wrong target etc.

The enemy AI is designed to throw a large number of enemies at you if your archers are not staying inside the solider circle. This will result in automatic defeat.


#5

sorry this is my first post I not sure how to properly format It. It’s not the archers that are dying it’s the soldiers


#6

Try to call the commandArcher inside a loop as mentioned. Paste the code here
Format the code by surrounding it by 3 backquotes (top left on your keyboard

formated code here;


#7

My soldiers are still dying


#8

Code? …


#9



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)
    self.command(soldier, "defend", defendPos);


def findStrongestTarget():
    mostHealth = 0
    bestTarget = None
    enemies = self.findEnemies()
    bestTarget = enemies
   
 
    if bestTarget and bestTarget.health > 15:
        return bestTarget
    else:
        return None



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

archerTarget = None

while True:
    # If archerTarget is dead or doesn't exist, find a new one.
    if not archerTarget or archerTarget.health <= 0:
        # Set archerTarget to be the target that is returned by findStrongestTarget()
        archerTarget = findStrongestTarget()

    friends = self.findFriends()
    soldiers = self.findByType("soldier")

    
    for i in range(len(soldiers)):
        soldier = soldiers[i]
        commandSoldier(soldier, i, len(soldiers));
        
    
    archers = self.findByType("archer")
    for i in range(len(archers)):
        archer = archers[i]
        commandArcher(archer, i, len(archers));
        
    commandArcher(archer)
      
    

```

#10

Your error is here:

def findStrongestTarget():
    mostHealth = 0
    bestTarget = None
    enemies = self.findEnemies()
    bestTarget = enemies
   
 
    if bestTarget and bestTarget.health > 15:
        return bestTarget
    else:
        return None

This function needs to select the enemy with the most health from a list.
Go to the Mad Maxer levels (desert, top left) to learn how to get the item with most “stuff” out of a list.
There is a level designed especially for most health.

commandArcher does not need i and len(archers). Only commandSoldier needs them because them have to go in a circle.
Also the last commandArcher is not necessary. So:

for i in range(len(archers)):
    archer = archers[i]
    # bad arguments commandArcher(archer, i, len(archers));
    commandArcher(archer)
    
# not needed commandArcher(archer)