Help with more effecient healing please! [Solved]

points = hero.getControlPoints()
point = hero.findNearest(points)

hero.say("Good luck!!!!")
needsHeal = []
warriors = []

def commandAlchemist():
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == 'alchemist':
            # Run away from the nearest enemy if too close.
            if enemy:
                d = Vector(friend.pos, enemy.pos)
                d = Vector.subtract(friend.pos, enemy.pos)
                d = Vector.normalize(d)
                d = Vector.multiply(d, 10)
            if enemy:
                if friend.distanceTo(enemy) < 17:
                    hero.command(friend, "move", d)            
            archer = friend.findNearest(hero.findByType("archer", hero.findEnemies()))
            targets = friend.findFriends()
            enemy = friend.findNearestEnemy()
            points = hero.getControlPoints()
            # Heal the wounded
            t = friend.findNearest(needsHeal)
            if t and friend.canCast("heal") and friend.health > 80:
                hero.command(friend, "cast", 'heal', t)
            for w in targets:
                if w.type == 'knight' or w.type == 'goliath':
                    warriors.append(w)
            if len(warriors) > 0:
                warrior = friend.findNearest(warriors)
            if archer and friend.canCast("poison-cloud") and friend.health > 80:
                hero.command(friend, "cast", 'poison-cloud', archer) 
            if enemy and not archer and friend.canCast("poison-cloud"):
                hero.command(friend, "cast", 'poison-cloud', enemy) 
            if enemy:
                if friend.distanceTo(enemy) < 10 and enemy and friend.health > 80:
                    hero.command(friend, "attack", enemy)
            slow = friend.findNearest(hero.findByType("goliath"))
            if friend.health < 80 and point:
                hero.command(friend, "say", 'Alchemist ' +  friend.id + ' leaving battle due to critical health')
                hero.command(friend, "moveXY", point.pos.x, point.pos.y)
            if enemy and friend.health < 80 and friend.canCast("heal") and friend.distanceTo(enemy) > 40:
                hero.command(friend, "cast", 'heal', friend)
                hero.command(friend, "say", 'Yay! I am healed! :D')
            if warrior and friend.canCast("regen", warrior) and friend.health > 80 :
                hero.command(friend, "cast", 'regen', warrior)
            if warrior and friend.canCast("haste", warrior) and friend.health > 80:
                hero.command(friend, "cast", 'haste', warrior)
            if warrior and friend.canCast("grow", warrior) and friend.health > 80:
                hero.command(friend, "cast", 'grow', warrior)
            if friend.canCast("flame-armor") and warrior and friend.health > 80:
                hero.command(friend, "cast", 'flame-armor', warrior)
            if slow and friend.canCast("slow", enemy) and friend.distanceTo(slow) < 40 and friend.health > 80:
                hero.command(friend, "cast", 'slow', slow) 
def commandKnight():
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == 'knight':
            target = friend.findNearestEnemy()                
            if target:
                hero.command(friend, "attack", target)
            targets = hero.findEnemies()
            if friend.health < 300:
                warriors.pop(friend)
                hero.command(friend, "say", 'Knight ' + friend.id + ' leaving battle due to critical health')
                hero.command(friend, "move", point.pos)
            victims = []
            for target in targets:
                if friend.distanceTo(target) < 15:
                    victims.append(target)
            anchor = friend.findNearest(victims)
            if len(victims) >= 2 and friend.isReady("cleave") and anchor and friend.health > 1/2 * friend.maxHealth:
                hero.command(friend, "cleave", anchor)
            else:
                target = friend.findNearest(targets)                
                if target and friend.health > 1/2 * friend.maxHealth:
                    hero.command(friend, "attack", target)
def summonTroops():
    type = summonTypes[len(hero.built) % len(summonTypes)]
    if hero.gold > hero.costOf(type):
        hero.summon(type)
def dodgeMissiles():
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == 'shaman-cut' or friend.type == 'archer':
            shot = friend.findNearest(hero.findEnemyMissiles())
            if shot and friend.distanceTo(shot) < 10:
                d = Vector.subtract(friend.pos, shot.pos)
                d = Vector.normalize(d)
                d = Vector.multiply(d, 20)
                moveToPos = Vector.add(friend.pos, d)
                hero.command(friend, "move", moveToPos) 



def commandArcher():
    kite()
    dodgeMissiles()
def kite():
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == 'archer' or friend.type == 'shaman-cut':
            if friend.health < friend.maxHealth / 2:
                hero.command(friend, "say", friend.id + ' leaving battle due to critical health')
                hero.command(friend, "move", point.pos)
                needsHeal.append(friend)
            
            enemies = hero.findEnemies()
            enemy = friend.findNearest(enemies)
            if enemy and friend.health > friend.maxHealth / 2:
                d = Vector(friend.pos, enemy.pos)
                d = Vector.subtract(friend.pos, enemy.pos)
                d = Vector.normalize(d)
                d = Vector.multiply(d, 10)
            if enemy and friend.health > 20:
                if friend.distanceTo(enemy) < 7 and friend.health > friend.maxHealth / 2:
                    hero.command(friend, "move", d)
                elif enemy and friend.health > friend.maxHealth / 2:
                    hero.command(friend, "attack", enemy)

Hi, this is my code in burning ices, to heal my friends.
Intention:
Append injured friends to an array, and get alchemists to heal them when they are ready
Outcome:
Friends get injured, and are appended and healed.
They don’t get removed from the array, so the alchemists keep healing the alreayd full health unit.
None of my friends are dying except for some scouts and throwers and soldiers.
BUT I AM A PERFECTIONIST REEEEEEEEEEEE
PLEASE HELP!!!

The simplest method given your current code is to not store a global list of injured units and calculate it for each alchemist when deciding who to heal.

# Untested sample code

friends = hero.findFriends()
for unit in hero.findByType("alchemist", friends):
  if unit.canCast("heal"):
    injured = unit.findNearest([f for f in friends if f.health < f.maxHealth - 100])
    if injured: hero.command(unit, "cast", "heal", injured)

Side note: warriors.pop(friend) is incorrect, .pop() takes an index, not an element.

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.