Raiders of Long Dark Javascript help

// 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.
var arryn = hero.findByType("raider")[0];
var peasant = hero.findByType("peasant")[0];
function chooseHeroStrategy(enemy) {
    // 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.
    if (hero.distanceTo(peasant) > 5) {
        return "advance";
    }
    if (enemy) {
        return "fight";
    } else {
        return "advance";
    }
}
function heroFight() {
    // Stop the ogres from rushing past you to get to the peasant!
    // Hint: try to slow them down if you can
    while (enemy.health > 0) {
        hero.attack(enemy);
    }
}
function heroAdvance() {
    // Stay behind the peasant
    hero.moveXY(peasant.pos.x - 5, peasant.pos.y);
}
function choosePeasantStrategy() {
    // Return "follow", "build-above", or "build-below"
    // Hint: use isPathClear() to determine where the hallways are
    if (hero.isPathClear({
            x: hero.pos.x,
            y: hero.pos.y + 10
        }, {
            x: hero.pos.x,
            y: hero.pos.y
        })) {
        return "build-above";
    } else if (hero.isPathClear({
            x: hero.pos.x,
            y: hero.pos.y
        }, {
            x: hero.pos.x,
            y: hero.pos.y - 10
        })) {
        return "build-below";
    } else {
        return "follow";
    }
}
function peasantAdvance() {
    // Keep the peasant behind Arryn and her soldiers.
    hero.command(peasant, "move", {
        x: arryn.pos.x - 5,
        y: arryn.pos.y
    });
}
function peasantBuild(x, y) {
    // Command the peasant to build a palisade.
    hero.command(peasant, "buildXY", "palisade", x, y);
}
while (true) {
    var enemy = hero.findNearestEnemy();
    var heroStrategy = chooseHeroStrategy(enemy);
    if (heroStrategy == "fight") {
        heroFight();
    } else if (heroStrategy == "advance") {
        heroAdvance();
    }
    var peasantStrategy = choosePeasantStrategy();
    if (peasantStrategy == "build-above") {
        peasantBuild(peasant.pos.x, peasant.pos.y + 5);
    } else if (peasantStrategy == "build-below") {
        peasantBuild(peasant.pos.x, peasant.pos.y - 5);
    } else if (peasantStrategy == "follow") {
        peasantAdvance();
    }
}

I believe I have done what I am supposed to do, yet the level fails!
Screenshot 2021-05-19 1.00.04 PM
link:

Thanks,
@sci12

It might be in your fight area. You might need to use different abilities and attack.

1 Like

I added a combination of more abilities and it worked!

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.