This is a problem because the hero will sit there if the enemy is a sand yak and do nothing as he is repeatedly stabbed by a brawler who was not the closest enemy to him when he ran the command.
Trying to re-run the command won’t work either, because archers are farther away[than the sand yak].
I have also tried many ways of using Hero.findEnemies() but cannot figure out how to exclude attacking sank-yaks then. Even with 3.5k health, the sand-yaks kill me within the two minute time I have.
If i do what you are suggesting, the hero just sits there and gets stabbed repeatedly when the enemy the code identified is a sand yak and a brawler approaches after that code is run
and if I re-run hero.findNearestEnemy, it will kill the ogres except for if there are archers or shaman farther away from the than a sank-yak is. If they are able to attack me from behind a sand-yak, then my hero will never attack them as it will always identify the sank-yak as the “nearest” enemy.
This will always be a fundamental problem with hero.findNearestEnemy: if the nearest enemy isn’t what you want, that function can’t help you. Unless you build it yourself, there’s no easy way to request a type of not "sand-yak".
hero.findEnemies is the tool to use. It returns a list of every enemy you can see, leaving it to you do manually filter the list.
The most intuitive way to do so is to iterate through all enemies, check if they’re a yak, and if not consider them a target.
# Python
non_yaks = []
for enemy in hero.findEnemies():
if enemy.type != "sand-yak":
non_yaks.append(enemy)
enemy = hero.findNearest(non_yaks)
[Python] If you’re comfortable with such loops, I suggest researching how to use list comprehensions, a shorthand for the same logic. Using the filter function is another more complex way to filter a list.
[JS] Javascript provides the .filter method on arrays which are great for this application.
non_yaks = []
for enemy in hero.findEnemies():
if enemy.type != "sand-yak":
non_yaks.append(enemy)
while True:
enemy = hero.findNearest(non_yaks)
if enemy:
hero.attack(enemy)
pass
You are defining non_yaks once, at the beginning of the simulation, such that the list does not update to include new enemies as the game moves on. You’ll want to find potential enemies inside your loop.
while True:
non_yaks = []
for enemy in hero.findEnemies():
if enemy.type != "sand-yak":
non_yaks.append(enemy)
enemy = hero.findNearest(non_yaks)
if enemy:
hero.attack(enemy)
pass