Need help with Kelvintaph crusader

I have a problem with my code: whenever I run it, it ignores the function that commands my friends but not the one that commands my hero.

// You can find friends through walls, but not enemies(unless you have twighlight glasses! Only 500 gems in the item shop).
// Watch out for smooth, frictionless ice patches!
function friendAttack() {
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        var ogres = friend.findNearest(hero.findByType("ogre"));
        var witches = friend.findNearest(hero.findByType("witch"));
        var enemy = friend.findNearestEnemy();
        var ogre = hero.findByType("ogre");
        if (enemy) {
            if (friend.type == "paladin") {
                if (friend.health <= 450 && friend.canCast("heal")){
                    hero.command(friend, "cast", "heal", friend);
                } else if (witches){
                    hero.command(friend, "attack", witches);
                    }else{
                    hero.command(friend, "attack", enemy);
                }
            } else if (friend.type == "archer") {
                if (witches) {
                    hero.command(friend, "attack", friend.findNearest(hero.findByType("witch")));
                } else {
                    hero.command(friend, "attack", enemy);
                }
            } else if (ogres) {
                hero.command(friend, "attack", ogres);
            } else {
                hero.command(friend, "attack", enemy);
            }
        } else {
            hero.command(friend, "move", {
                x: 50,
                y: 57
            });
            hero.command(friend, "move", {
                x: 50,
                y: 39
            });
            hero.command(friend, "move", {
                x: 78,
                y: 40
            });
        }
    }
}
function AttackOrMove() {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        if (hero.canCast("chain-lightning")) {
            hero.cast("chain-lightning", enemy);
        } else if (hero.isReady("bash")) {
            hero.bash(enemy);
            hero.shield();
        } else {
            hero.attack(enemy);
        }
    } else {
        hero.moveXY(78, 14);
    }
}
hero.moveXY(70, 15);
while (true) {
    AttackOrMove();
    friendAttack();
}

1 Like

The hint starts with: “// You can find friends through walls, but not enemies.

hero.findByType("ogre") # will not work

Only your friends can find enemies. See Array.filter() and Array.find() in array section.

function isOgre(enemy) {
  return enemy.type == 'ogre';
}
function isWitch(enemy) {
  return enemy.type == 'witch';
}
var friend = hero.findFriends()[0];
var enemies = friend.findEnemies();
var ogres = enemies.filter(isOgre);
var ogre = friend.findNearest(ogres);
var witch = enemies.find(isWitch);  
console.log(friend.id);
console.log(ogre.id);
console.log(witch.id);

The output in the console will be:

Gorgin 
Oni
Yzzrith

If you want to command your friends individually see the post How do I name my units?

This also will not work:

hero.command(friend, "move", { x: 50, y: 57 });
hero.command(friend, "move", {x: 50, y: 39});
hero.command(friend, "move", {x: 78, y: 40});

see:

actually the move commands were meant to happen if there were no enemies, i didn’t want them to get trapped by the bear trap.

I’m wrong, wrong, wrong!

var friend = hero.findFriends()[0];
var witch = friend.findNearest(hero.findByType("witch"));
console.log(witch.id);

has Yzzrith as output! - Will check again when free - but all other things i wrote are ( hopefully ) OK :slight_smile: