When I played the level it said that hero.findEnemies() doesn’t work. It says it on that comment. But maybe it does, I don’t know now. I’ll have to check…
My solution was time-depending. As I mentioned it included two basic functions. First (and easiest) for the hero: where to go at the certain period of time (using hero.move(moveToPos)). Hero should stay alive while catapults kill brawlers and themselves (I added chain-lightning, but it’s optional). The second defines actions for allies. Again their tactics depended on the time and type as primary conditions and then other things like ally.health or shield.ability. Also I used enemies names (as far as they are hardcoded) to indicate targets.
Thanks to you I managed to complete all the goals (when I passed the level for the first time not all of my allies stayed alive). Now I figured out missed piece of puzzle)
You code is a bit in another manner. I can try your current code but it seems to me that if your allies don’t wanna obey it’s good idea to go back to previous levels with practicing of units management.
For the start I would recommend to move this part
in
if friend.type == 'paladin':
CommandPaladin(friend)
block.
And then try to set up other things.
They move not to the end but to 50,39. It’s because of line:
hero.command(friend, "move", {'x': 50, 'y': 39})
And this is because of mistakes in the whole commandTroops() function
Plus I suppose function findWorstEnemy() is also a little irrelevant(
F.e. look what happens if edit function like this:
def commandTroops():
for index, friend in enumerate(hero.findFriends()):
enemies = hero.findEnemies()
witch = hero.findNearest(hero.findByType('witch'))
if len(enemies) > 0 and witch:
if friend.type == 'paladin':
CommandPaladin(friend)
if friend.canCast("heal"):
hero.command(friend, "cast", "heal", friend)
hero.command(friend, "move", {'x': 31, 'y': 40})
hero.command(friend, "move", {'x': 7, 'y': 40})
else:
CommandSoldier(friend)
worst = findWorstEnemy()
if (worst and friend.pos.x - worst.pos.x < 10):
hero.command(friend, "move", {'x': 50, 'y': 58})
hero.command(friend, "move", {'x': 6, 'y': 58})
else:
hero.command(friend, "move", {'x': 49, 'y': 58})
hero.command(friend, "move", {'x': 50, 'y': 39})
hero.command(friend, "move", {'x': 78, 'y': 40})
Where do all the allies go?
what do you mean by where do the allies go @Alexbrand?
I mean that with your last version of code
And after editing a little they go not to the (50,39) point, but to the left upper corner of the level. Why? Because there is hero.command(friend, "move", {'x': 6, 'y': 58})
line in your code.
I’m sorry but I think that looks like you don’t understand basic principles of how to make your troops move and attack or shield depending on different circumstances and conditions. Frankly speaking I’m not a good programmer at all but with a bit of work it’s possible to write simple code by oneself without copying solutions.
why can’t i load the level now?
like i load it and it stops half way through
It loads and works on my machine. Maybe you have some errors in code? Try to comment it out maybe?
is their a way to reset the level before you enter the level? if not then i dont think i can do this level anymore…
You can use flags to command your troops where to go after they destroyed their enemies
again, use cast chain lightning to kill on ogre for your troops first. it will make them fight much easier.
@Code_Master how would i tell my troops to follow a flag? i don’t think i have done that before.
def moveTroops(flag, troops): # flag is the flag and troops are your troops
for troop in troops:
hero.command(troop, 'move', flag.pos)
You would also need to remove that flag after your troops reach the flag, or I think they will keep trying to go to the flag.
alright so my paladin kills the witch then does nothing and my troops just go for somewhere even though their is no flag… also my paladin dies before i can get a flag down. code:
# You can find friends through walls, but not enemies.
# Watch out for smooth, frictionless ice patches!
def moveTo(position, fast=True):
if (hero.isReady("jump") and fast):
hero.jumpTo(position)
else:
hero.move(position)
def commandTroops():
for index, friend in enumerate(hero.findFriends()):
enemies = hero.findEnemies()
witch = hero.findNearest(hero.findByType('witch'))
if len(enemies) > 0 and witch:
if friend.type == 'paladin':
CommandPaladin(friend)
else:
CommandSoldier(friend)
hero.command(friend, "move", {'x': 31, 'y': 40})
worst = findWorstEnemy()
if (worst and friend.pos.x - worst.pos.x < 10):
def moveTroops(flag, troops):
for troop in troops:
hero.command(troop, 'move', flag.pos)
def CommandPaladin(paladin):
if (paladin.canCast("heal")):
target = lowestHealthFriend()
if target:
hero.command(paladin, "cast", "heal", target)
elif (paladin.health < 100):
hero.command(paladin, "shield")
else:
target = findWorstEnemy()
if (target):
hero.command(paladin, "attack", target)
def CommandSoldier(soldier):
target = findWorstEnemy()
if (target):
hero.command(soldier, "attack", target)
def findWorstEnemy():
witch = hero.findNearest(hero.findByType('witch'))
ogre = hero.findNearest(hero.findByType('ogre'))
skeleton = hero.findNearest(hero.findByType('skeleton'))
if witch:
return witch
elif ogre:
return ogre
elif skeleton:
return skeleton
else:
return None
def lowestHealthFriend():
lowestHealth = 99999
lowestFriend = None
friends = hero.findFriends()
for friend in friends:
if friend.health < lowestHealth and friend.health < friend.maxHealth:
lowestHealth = friend.health
lowestFriend = friend
return lowestFriend
def attack(target):
if target:
if (hero.distanceTo(target) > 10):
moveTo(target.pos)
elif (hero.isReady("bash")):
hero.bash(target)
else:
hero.attack(target)
while True:
commandTroops()
brawler = hero.findNearest(hero.findByType('brawler'))
catapult = hero.findNearest(hero.findByType('catapult'))
if brawler and hero.distanceTo(brawler) > 15:
moveTo(brawler.pos, False)
elif brawler:
runaway = Vector.subtract(hero.pos, brawler.pos)
runaway = Vector.normalize(runaway)
runaway = Vector.multiply(runaway, 15)
direction = Vector.add(runaway, hero.pos)
moveTo(direction, False)
elif catapult:
attack(catapult)
else:
hero.move({'x': 78, 'y': 15})