Help in Reaping Fire [JavaScript]

// The goal is to survive for 30 seconds, and keep the mines intact for at least 30 seconds.

function chooseStrategy() {
    var enemies = hero.findEnemies();
    // If you can summon a griffin-rider, return "griffin-rider"
    if (hero.gold >= hero.costOf("griffin-rider")) {
        hero.summon("griffin-rider");
        return "griffin-rider";
    }
    // If there is a fangrider on your side of the mines, return "fight-back"
    var enemy = hero.findByType("fangrider", hero.findEnemies()); 
    if (enemy.type == "fangrider" && enemy.pos.x <= 36) {
        return "fight-back";
    }
    // Otherwise, return "collect-coins"
    else {
        return "collect-coins";
    }
}


function commandAttack() {
    var enemies = hero.findByType("ogre", hero.findEnemies());
    var friends = hero.findByType("griffin-rider", hero.findFriends());
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        for (var j = 0; j < enemies.length; j++) {
            var enemy = enemies[j];
            // Command your griffin riders to attack ogres.
            if (enemy) {
                if (friend !== null) {
                 hero.command(friend, "attack", enemy);
            }
        }
}
}
}
function pickUpCoin() {
    // Collect coins
    var item = hero.findNearestItem();
    if (item) {
        hero.moveXY(item.pos.x, item.pos.y);
    }
}

function heroAttack() {
    // Your hero should attack fang riders that cross the minefield.
    var enemy = hero.findNearest(hero.findByType("fangrider"));
    if (enemy){
        if (hero.distanceTo(target) < 20){
            moveTo(enemy.pos);
        }
        else if (hero.isReady("bash")){
            hero.bash(enemy);
        }
        else if (hero.isReady("power-up")){
            hero.powerUp();
            hero.attack(enemy);
        }
        else if (hero.isReady("cleave")){
            hero.cleave(enemy);
    }
        else {
            hero.attack(enemy);
}
}
}
while(true) {
    var strategy = chooseStrategy();
    // Call a function, depending on what the current strategy is.
    if (strategy == "fight-back") {
        heroAttack();
    }
    else if (strategy == "griffin-rider") {
        commandAttack();
    }
    else if (strategy == "collect-coins") {
        pickUpCoin();
    }
}   

I don’t know what is happening, it wont work.

function commandAttack() {
    var enemies = hero.findByType("ogre", hero.findEnemies());
    var friends = hero.findByType("griffin-rider", hero.findFriends());
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        for (var j = 0; j < enemies.length; j++) {
            var enemy = enemies[j];
            // Command your griffin riders to attack ogres.
            if (enemy) {
                if (friend !== null) {
                 hero.command(friend, "attack", enemy);
            }
        }
}
}
}

Your griffin-riders are only targeting enemies with type “ogre”, you just need to find the griffin-rider’s nearest enemy from hero.findEnemies(), no need to cycle through them.

hero.moveXY(item.pos.x, item.pos.y);

I would also recommend changing from using moveXY to move, it helps you command your allies better.

if (hero.gold >= hero.costOf("griffin-rider")) {
        hero.summon("griffin-rider");
        return "griffin-rider";
}

Always running the command in the while true loop will also help your allies react faster instead of only when a new griffin-rider is summoned.

1 Like

Even though I don’t do javascript, but I see that the issue seems to be in your chooseStrategy and commandAttack functions.

  1. In the chooseStrategy function, you should only summon a griffin-rider if you have enough gold for it, and you shouldn’t actually summon it. Just return “griffin-rider” when you can afford it, and put the summoning part into commandAttack

  2. In the commandAttack function, you should iterate through the friends and just simply command the griffin riders to attack the nearest enemy ogre.

  3. In the heroAttack function, you don’t need to check distance nor do any spell check, just attack the nearest enemy when there is any, because the function is only called when the fangrider gets past the mines.

  4. @JustALuke is right, you should use move() so the hero can constantly command the allies.

  5. This is optional, you can replace the else if with if so the commands can run separately and don’t fight over each other.

i reworked some stuff, but this is the thing i thought to do but still it wont work

function chooseStrategy() {
    if (hero.gold >= hero.costOf("griffin-rider")) {
        hero.summon("griffin-rider");
        return "griffin-rider";
    }
    var fangriders = hero.findByType("fangrider", hero.findEnemies());
    if (fangriders.length > 0) {
        for (var k = 0; k < fangriders.length; k++) {
            var enemy = fangriders[k];
            if (enemy.pos.x <= 36) {
                return "fight-back";
            }
        }
    }    
    else {
        return "collect-coins";
    }
}
function commandAttack() {
    var enemies = hero.findEnemies();
    var friends = hero.findByType("griffin-rider", hero.findFriends());
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        for (var j = 0; j < enemies.length; j++) {
            var enemy = enemies[j];
            if (enemy && (enemy.type == "scout" || enemy.type == "munchkin")) {
                if (friend !== null) {
                    hero.command(friend, "attack", enemy);
                }
            }
        }
    }
}
function pickUpCoin() {
    var items = hero.findItems();
    var nearestCoin = hero.findNearest(items);
    if (nearestCoin) {
        hero.move(nearestCoin.pos);
    }
}
function heroAttack() {
    var enemy = hero.findNearest(hero.findByType("fangrider"));
    if (enemy) {
        if (hero.distanceTo(enemy) < 20) {
            hero.move(enemy.pos);
        } else if (hero.isReady("bash")) {
            hero.bash(enemy);
        } else if (hero.isReady("power-up")) {
            hero.powerUp();
            hero.attack(enemy);
        } else if (hero.isReady("cleave")) {
            hero.cleave(enemy);
        } else {
            hero.attack(enemy);
        }
    }
}
while (true) {
    var strategy = chooseStrategy();
    if (strategy == "fight-back") {
        heroAttack();
    } 
    else if (strategy == "griffin-rider") {
        commandAttack();
    } 
    else if (strategy == "collect-coins") {
        pickUpCoin();
    }
}

i also tried to find what type of enemies they are, because my troops wont attack them. so i found out, that they are undefined. how can i finish this level?