Raiders of the long dark - codes work but not perfect

hi lovely helpers:

my codes ran smoothly in this level, but it seems that my peasant couldn’t build the ‘palisade’, would you take a look at my codes? thanks

# 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'
    else:
        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.canCast("slow", enemy):
            hero.cast("slow", enemy)
        elif hero.isReady("reset-cooldown"):
            hero.resetCooldown("slow")
        else:
            hero.attack(enemy)
    pass

def heroAdvance():
    # Stay behind the peasant
    hero.moveXY(peasant.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"
    elif peasant.isPathClear({'x':peasant.pos.x, 'y':peasant.pos.y}, {'x':peasant.pos.x, 'y':peasant.pos.y + 5}):
        return 'build-above'
    elif peasant.isPathClear({'x':peasant.pos.x, 'y': peasant.pos.y}, {'x':peasant.pos.x, 'y': peasant.pos.y - 5}):
        return 'build-below'
#    else:
#        return 'follow'
    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.
    if hero.gold >= hero.costOf("palisade"):
        hero.command(peasant, "buildXY", 'palisade', x, 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()
    

Hi,
Ok, this code has a few problems:
Let’s start with the peasant code:
1.

if peasant.distanceTo(arryn)>5:
    return "follow"

This will always be true if you’re constantly following Arryn. Maybe make the distance a little bit longer.
2.

elif peasant.isPathClear({'x':peasant.pos.x, 'y':peasant.pos.y}, {'x':peasant.pos.x, 'y':peasant.pos.y + 5}):
    return 'build-above'
elif peasant.isPathClear({'x':peasant.pos.x, 'y': peasant.pos.y}, {'x':peasant.pos.x, 'y': peasant.pos.y - 5}):
    return 'build-below'

These lines are alright, except for a bit of a unfortunate error-- peasant.isPathClear isn’t a function, but hero.isPathClear is. You have to use hero.isPathClear even when referring to two objects completely unconnected to the hero.
3.

if hero.gold >= hero.costOf("palisade"):
   hero.command(peasant, "buildXY", 'palisade', x, y)
   pass

Hero.costOf doesn’t ‘govern’ palisades and other peasant buildables. You could compare your gold with an actual number, or just not check the price-- the level is designed for you to have enough gold to build the right amount of palisades so really there’s no worry)

Apart from that, you’ll need to change your hero code to make sure it only attacks close enemies and I would suggest a more effective method of stopping the ogres, because when I tried your code (after having fixed it) the peasant kept dying :frowning:.
I hope this helps
Danny

1 Like

hey mate, thanks for such a clear and detailed explanation, I fixed all the issues that your pointed out and now I can pass this level with the peasant building the palisades, much appreciated!

1 Like