ive seen so many people not being able to solve this. let me explain my madness before I show you my code. im currently trying to make sure the soldier retreats before he dies a second time. ( have an undying ring ). my code has an infinate loop even though it is all spaced out, to make sure it isnt slow.
decider = 1
# Hushbaum has been ambushed by ogres!
# She is busy healing her soldiers, you should command them to fight!
# The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!
# Soldiers spread out in a circle and defend.
def commandSoldier(soldier, soldierIndex, numSoldiers):
if decider and decider == 1:
angle = Math.PI * 2 * soldierIndex / numSoldiers
defendPos = {"x": 41, "y": 40}
defendPos.x += 10 * Math.cos(angle)
defendPos.y += 10 * Math.sin(angle)
hero.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 = hero.findEnemies()
# Figure out which enemy has the most health, and set bestTarget to be that enemy.
for enemy in enemies:
if enemy.health > mostHealth:
mostHealth = enemy.health
bestTarget = enemy
# Only focus archers' fire if there is a big ogre.
if bestTarget and bestTarget.health > 15:
archerTarget = bestTarget
else:
return None
def decide():
if soldier.pos.x == 43 or soldier.pos.y == 38:
decider = 1
def getSafety():
for soldier in hero.findByType("soldier"):
if soldier and soldier.health <= soldier.maxHealth / 4 + 16:
while soldier.pos.x != 43 or soldier.pos.y != 38:
decider = 0
hero.command(soldier, "move", {"x" : 43, "y" : 38})
decide()
# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
def commandArcher(archer):
nearest = archer.findNearestEnemy()
if archerTarget:
hero.command(archer, "attack", archerTarget)
elif nearest:
hero.command(archer, "attack", nearest)
archerTarget = None
def castUndyingCharm():
set = hero.findFriends()
for good in set:
if good.health < good.maxHealth / 5:
utoh = good
if utoh and hero.canCast("undying-charm") == True:
hero.cast("undying-charm", utoh)
while True:
getSafety()
castUndyingCharm()
# If archerTarget is defeated 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()
archers = hero.findByType("archer")
friends = hero.findFriends()
soldiers = hero.findByType("soldier")
# Create a variable containing your archers.
for i in range(len(soldiers)):
soldier = soldiers[i]
commandSoldier(soldier, i, len(soldiers));
# use commandArcher() to command your archers
for archer in archers:
commandArcher(archer)