Why aren't my soldiers attacking on Kithgard Brawl?


#1

Here is my code:

# If you lose, you must wait a day to re-submit.
# Each time you submit gives a new random seed.
def commandSoldiers(nearest):
    for friend in self.findFriends():
        if friend and nearest:
            self.command(friend, "attack", nearest)
loop:
    if item:
        self.move(item.pos)
    nearest = self.findNearest(self.findEnemies())
    if nearest:
        if self.isReady("chain-lightning"):
            self.cast("chain-lightning", nearest)
        if self.isReady("cleave"):
            self.cleave(nearest)
        self.attack(nearest)
    item = self.findNearest(self.findItems())
    if item:
        self.move(item.pos)
    if self.gold>=20:
        self.summon("soldier")
    commandSoldiers(nearest)

When I run it, my soldiers don’t attack! The only reason I can think of for my soldiers not attacking is that commandSoldiers does not work. Can anybody tell me why?

Note:This isuue happens with every replayable level.


#2

You placed commandSoldiers outside the block where you check if nearest exists. Move it inside.


#3

But commandSoldiers checks whether nearest exists or not.


#4

Ok, a lot of things are wrong

To answer your questions, you search for the nearest enemy and then you order your hero to attack it ( up to 3 times using all his abilities in a row). By the time you reach the solider command code your nearest enemy is probably dead.

  1. Your enemy and your hero can act in parallel. But everything your hero does takes time (attacking, moving, summoning, special powers, shielding)

For example

loop:
    var i
    for(i=0;i<10;i++)
        enemy=self.findNearestEnemy();
        self.attack(enemy);
   }
   self.commandSoldiers();

here your hero takes 10 attacks before commanding the soldiers. At 0.5seconds per attack the soliders will spend almost 5 seconds on each loop looking at the grass grow.

Take only one action per loop

You want this type of code:

loop:
    self.commandSoldiers()
    if cleave and enemy
        cleave enemy
        continue
   if enemy
       attack enemy
       continue
   if potion
        collect potion
        continue

The continue skips to the end of the loop so you’ll be able to command your soldiers again before taking another action

=============================================================================

  1. Asking all your soldiers to attack the same enemy is a bad idea, unless you want to focus fire.
    The soldiers will push each other away and spend most of the time running to get to your enemy.

With the archers you do not have this problem, but it can still result into an overkill and attacks lost on a dying enemy

Your troops have their own friend.findNearestEnemy, friend.findEnemies, friend.distanceTo
Use that


#5

I followed your instructions and now my soldiers are attacking… some of the time. Here is my new code:

# Survive the waves of ogres.
# If you win, the level gets harder, and gives more rewards.
# If you lose, you must wait a day to re-submit.
# Each time you submit gives a new random seed.
def commandSoldiers():
    for friend in self.findFriends():
        nearest=friend.findNearest(friend.findEnemies())
        if friend and nearest:
            self.command(friend, "attack", nearest)

loop:
    commandSoldiers()
    item = self.findNearest(self.findItems())
    if item:
        self.move(item.pos)
    if self.gold>=20:
        self.summon("soldier")
    nearest = self.findNearest(self.findEnemies())
    if self.isReady("invisibility"):
        self.cast("invisibility", self)
        continue
    if self.hasEffect("hide"):
        continue
    if nearest:
        if self.isReady("chain-lightning"):
            self.cast("chain-lightning", nearest)
            continue
        if self.isReady("cleave"):
            self.cleave(nearest)
            continue
        self.attack(nearest)
        

#6
  1. You might want to add a continue inside the if after self.move(item.pos) and self.summon
if item:
   self.move(item.pos)
   continue
  1. With high level glasses, you can see lots of enemies that are to far for your solders to see. Use your sight to detect the enemies and let the soldiers only pick the nearest:
enemies=this.findEnemies()
commandSoldiers(enemies)

and

def commandSoldiers(enemies):
...........
    nearest=friend.findNearest(enemies)

#7

My code still doesn’t work. Did I follow your instructions correctly?

# Survive the waves of ogres.
# If you win, the level gets harder, and gives more rewards.
# If you lose, you must wait a day to re-submit.
# Each time you submit gives a new random seed.
def commandSoldiers(enemies):
    for friend in self.findFriends():
        nearest=friend.findNearest(enemies)
        if friend and nearest:
            self.command(friend, "attack", nearest)

loop:
    commandSoldiers(self.findEnemies())
    
    item = self.findNearest(self.findItems())
    if item:
        self.move(item.pos)
        continue
    if self.gold>=20:
        self.summon("soldier")
    nearest = self.findNearest(self.findEnemies())
    if self.isReady("invisibility"):
        self.cast("invisibility", self)
        continue
    if self.hasEffect("hide"):
        continue
    if nearest:
        if self.isReady("chain-lightning"):
            self.cast("chain-lightning", nearest)
            continue
        if self.isReady("cleave"):
            self.cleave(nearest)
            continue
        self.attack(nearest)


    


#8

It works very well for me. The only portion where the soldiers are not attacking is when your hero tries to attack an enemy which is far away.

Try this (in the place of your last cleave-attack part):

distance=self.distanceTo(enemy)
if (distance > 6):
     self.move(enemy)
else
     if self.isReady("cleave"):
          self.cleave(nearest)
          continue
    self.attack(nearest)

You need boots with move (not moveXY) for this.

  1. Remove this part:
if self.hasEffect("hide"):
        continue

it tells your hero not to do anything at all if invisible


#9

If I attack while invisible, the invisibility goes away.


#10

Oh, learned something new. But the idea is to kill enemy as fast as possible: in brawls the enemies keep spawning so if you do not kill them fast enough you’ll have more and more enemies on you.

Invisibility then should be used only for strategic purposes, for example if you have a squishy wizard and you want to hide & run.


#11

what level are you on? i am on level 4 and i think its impossible to finish this level


#12

Preparing to win level 7.