Help with Sowing Fire (Javascript)

Hi, when I’m using the code below, for some reasons after my heroes built the first few traps and about 9 ogres came, the hero would stop planting fire traps and the griffin riders are like “Meh, not my problem”. Can anyone help?
The code:

// Goal: build three rows of nine fire-traps.
// Returns "retreat", "attack", "start-next-trap-column", or "build-next-trap-in-column"
function chooseStrategy() {
    var enemies = hero.findEnemies();
    // If there are overwhelming ogre forces, return the "retreat" strategy.
    if (enemies.length > 20) {
        return "retreat";
    }
    // If there are some ogres, return the "attack" strategy.
    if (enemies.length < 20 && enemies.length !== 0) {
        return "attack";
    }
    // Use x % 9 === 0 to see if x is divisible by 9.
    // Use this.built.length to see how many traps you have built.
    // If you have finished a column of 9 traps, return "start-next-trap-column"
    if (hero.built.length % 9 === 0) {
        return "start-next-trap-column";
    }    // Otherwise, return "build-next-trap-in-column"
    else {
        return "build-next-trap-in-column";
    }
}
var trapsInColumn = 9;
var startX = 40;
var columnX = startX;
// Build the next trap in a column in the correct place.
function buildNextTrapInColumn(columnX, numTraps) {
    // Change newY to use % to wrap around and only build trapsInColumn (9) traps per column
    var newY = 7 * (numTraps % trapsInColumn) + 10;
    if (hero.pos.y < newY) {
        hero.move({
            x: columnX - 5,
            y: newY
        });
    } else {
        buildTrap(columnX, newY);
    }
}
// Start a new column of traps.
function startNextTrapColumn(columnX, numTraps) {
    var newX = startX - (Math.floor(numTraps / trapsInColumn) * 6);
    if (hero.pos.y > 10) {
        hero.move({
            "x": newX - 5,
            "y": 10
        });
        return columnX;
    } else {
        buildTrap(newX, 10);
        return newX;
    }
}
function buildTrap(x, y) {
    hero.buildXY("fire-trap", x, y);
}
function commandAttack() {
    // Have your griffin riders fend off the attackers.
    var friends = hero.findFriends;
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        var enemy = hero.findNearestEnemy();
        if (enemy) {
            hero.command(friend, "attack", enemy);
        }
    }
}
function commandRetreat() {
    hero.say("Retreat!");
    // You and your griffin riders retreat to safety behind the traps.
    hero.move({
        x: 4,
        y: 42
    });
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        hero.command(friend, "move", {x: 4, y: 42});
    }
}
while (true) {
    var strategy = chooseStrategy();
    if (strategy == "attack") {
        commandAttack();
    } else if (strategy == "build-next-trap-in-column") {
        buildNextTrapInColumn(columnX, hero.built.length);
    } else if (strategy == "start-next-trap-column") {
        columnX = startNextTrapColumn(columnX, hero.built.length);
    } else if (strategy == "retreat") {
        commandRetreat();
    }
}

Thanks

Well, JS is not my forte, but here goes…

function chooseStrategy()
//what happens if there are exactly 20 ogres?

function BuildNextTrapInColumn()
//syntax error in the move() statement?
//if not, then there is an error in the move() statement in function startNextTrapColumn

function commandRetreat()
//move() syntax…also, I’d recommend using moveXY instead, as move() is a one step method used in a loop and moveXY is move until you get there (fire and forget) type of method.
//hero.command() move syntax

1 Like

@dedreous


@3nterpris3
You have somewhere

var friends = hero.findFriends; // without ()
hero.say("Retreat!"); // it's useless

for debug use console.log(something) or for immediate feedback pet.say(something) / if you have pets/

2 Likes

heh…I totally missed the findFriends; …good eye xython :wink:

1 Like

Thanks. I added the parentheses and it worked