# Your goal is to protect the peasant and move to the right.
# Arryn Stonewall will defend the front, and command the soldiers.
# You need to cover the rear and command the peasant.
arryn = hero.findByType("raider")[0]
peasant = hero.findByType("peasant")[0]
def chooseHeroStrategy():
# Return either "fight" or "advance".
# Try to stay 5m behind the peasant when not fighting.
# Don't get more than 15m away from the peasant.
enemy = hero.findNearestEnemy()
if enemy:
return "fight"
if hero.distanceTo(peasant)>5:
return "advance"
pass
def heroFight():
# Stop the ogres from rushing past you to get to the peasant!
# Hint: try to slow them down if you can
enemy = hero.findNearestEnemy()
if enemy:
if hero.isReady("bash"):
hero.bash(enemy)
pass
def heroAdvance():
# Stay behind the peasant
hero.moveXY(peasanr.pos.x-5, peasant.pos.y)
pass
def choosePeasantStrategy():
# Return "follow", "build-above", or "build-below"
# Hint: use isPathClear() to determine where the hallways are
if peasant.distanceTo(arryn)>5:
return "follow"
if peasant.isPathIsClear(peasant.pos.x, peasant.pos.y + 5):
return "build-above"
if peasant.isPathIsClear(peasant.pos.x, peasant.pos.y - 5):
return "build-below"
pass
def peasantAdvance():
# Keep the peasant behind Arryn and her soldiers.
hero.command(peasant, "move", {'x':arryn.pos.x-5,'y':arryn.pos.y)
pass
def peasantBuild(x,y):
# Command the peasant to build a palisade.
hero.command(peasant, "palisade", {'x':x,'y':y})
pass
while True:
heroStrategy = chooseHeroStrategy()
if heroStrategy == "fight":
heroFight()
elif heroStrategy == "advance":
heroAdvance()
peasantStrategy = choosePeasantStrategy()
if peasantStrategy == "build-above":
peasantBuild(peasant.pos.x, peasant.pos.y + 5)
elif peasantStrategy == "build-below":
peasantBuild(peasant.pos.x, peasant.pos.y - 5)
elif peasantStrategy == "follow":
peasantAdvance()
Also, make sure you redefine the variable âArrynâ inside a function. Sometimes, a function cannot see a variable outside of it during the calling procedure so make sure you redefine arryn = hero.findByType(âraiderâ)[0]