Library Tactician Summoning Problem


#1

Hello, I’m on the Library Tactician level and I’m having problems with summoning soldiers in a loop.

When I try to summon soldiers inside of the loop it does it only once but if I put multiple:

if self.gold >= self.costOf("soldier"):
    self.summon("soldier")

it does it twice.

Here’s my full 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)
    self.findFriends()
    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.
    if enemies.health > mostHealth:
        mostHealth = enemy.health
        bestTarget = enemy


# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
def commandArcher(archer):
    enemies = self.findEnemies()
    closest = archer.findNearestEnemy
    if archerTarget:
        self.command(archer, "attack", archerTarget)
    elif closest:
        self.command(archer, "attack", nearest)

archerTarget = None

loop:
    # If archerTarget is dead or doesn't exist, find a new one.
    if self.gold >= self.costOf("soldier"):
        self.summon("soldier")

    elif 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, soldier in enumerate(soldiers):
        commandSoldier(soldier, i, len(soldiers));
    # use commandArcher() to command your archers
    commandArcher(friends)
    commandSoldier(friends)

Is there any way to fix this? Thank you.


#2

I don’t understand why this is a problem for you, but here is the background:

By now you have the Boss Star II. Now, this Boss Star gives you 40 gold. In the first frame that the level runs, it reads all your functions, and the first command. In this case, it is an if-statement. A soldier costs 20 gold, so you do have enough to summon a soldier. Therefore, you summon one. There are no enemies in the first frame, so you command your friends and continue to the next iteration. Check for gold: there’s 20 gold now, so you summon another soldier. That’s the basic idea.


#3

Oh thanks for the help but I fixed this a while ago and I dont know how to delete this post. Sorry


#4

A post was split to a new topic: Clash of Clones help