Help with prioritizing enemies

Hello once again!

I have been running into issues using arrays and loops to prioritize enemies on a couple different levels…mainly Clash of Clones and the Trials. The following is a brief snippet of the method I have been using (this is based on the loops in the “preferential treatment” level. But BELIEVE ME, I have tried this many, many ways.

I just erased my entire code and started from scratch for the third or fourth time, so i will update as I progress…but as it stands, this is a snippet of what i am using, and my hero stands there doing nothing:

while True:
    enemies = hero.findEnemies()
    healthThresh = hero.health < hero.maxHealth / 2
    if healthThresh:
        heal(self)
    enemyIndex = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.type == "thrower": # I want to prioritize enemies by type
            while enemy.health > 0:  # i.e. throwers first, then  the clone, then everyone else
                lightning(enemy) # these functions are defined previously and work fine, don't worry about these
                hero.attack(enemy)
        enemyIndex += 1

I have basically just been trying to find an efficient method/algorithm for prioritizing enemies, but I keep hitting a wall. As I said, i will update with more code as I write something a little more elegant, but a shove in the right direction would not be unwelcome.

Thanks!

Assuming your functions are fine (I have no idea how you’re healing yourself), your code is in working order. Still, there’s a few things that could be going wrong in the example code you gave:

  1. The nearest thrower is outside the range of your vision

  2. If you’re running this code on Clash of Clones, make sure to change “thrower” to “archer”

  3. There is a bug on certain levels where casting chain lightning will cause the level to crash, but this doesn’t sound like the problem you’re having.

Otherwise, what kind of error are you getting?

Welp, “archer” not “thrower”…that’ll do it. A few additional questions for you, here’s the full code, sorry it’s long…problem area is at the end.

def lightning(target):                                     
    if hero.canCast("lightning-bolt", target):
        hero.cast("lightning-bolt", target)
def shrink(target):
    if hero.canCast("shrink", target):
        hero.cast("shrink", target)
def zap(target):
    if hero.canElectrocute(target):
        hero.electrocute(target)
def buff(self):
    if hero.canCast("grow", self):
        hero.cast("grow", self)
def healme(self):
    if hero.canCast("regen", self):
        hero.cast("regen", self)
fallback1 = {'x':45, 'y':66}
while True:
    enemies = hero.findEnemies()
    healthThresh = hero.health < hero.maxHealth / 2
#    if healthThresh:
#        healme(self)
#        buff(self)
    i = 0
    while i < len(enemies):
        if enemies[i].type == "archer":
            while enemies[i].health > 0:
                hero.attack(enemies[i])
        i += 1
    i = 0
    while i < len(enemies):
        if enemies[i].type == "librarian":
            while hero.now() <= 10:        '
                lightning(enemies[i])
                hero.attack(enemies[i])
        i +=  1
    while hero.now() <= 15:
        hero.move(fallback1)
        healme(self)
        nearest = hero.findNearest(hero.findEnemies())
        zap(nearest)
        slow(nearest)
        buff(self)
    i = 0
    while i < len(enemies):
        if enemies[i].type != "librarian" and enemies[i].type != "sand-yak": # and hero.distanceTo(enemies[i]) < 50:
            while enemies.health > 0:
                hero.attack(enemies[i])
        i += 1

So once I get to the last loop, after I fall back and cast my heal and AoE spells, my hero should be going into a loop to start attacking again (ignoring the clone and sand-yaks of course). Instead, she stands there and dies. I’m guessing this has something to do with ending the previous with-loop…I know the code is a bit rigid because it ill be RNG when submitted.

Also, in regards to:

# and hero.distanceTo(enemies[i]) < 50: # (4th line up from the bottom)

Am I on the right track with this? I’ve been running into issues trying to ignore enemies out of a certain range.

Anyways, any thoughts would be appreciated, time to get some sleep. =)

Try changing “enemies.health” to “enemies[i].health”. I think that will fix it.

EDIT: It certainly wasn’t pretty…but I got it.