Antipodes (Javascript): Can't get them to move to the correct clone [SOLVED]

Hi kind reader
I need/want some help on the level Antipodes in Cloudrip Mountain.
Here is the link to the level: Antipodes
I can’t get my archers to move to the correct clone. What they do is they move to the clone that is right in front of them, and then they all die, and my hero joins them soon after. The hero that I’m using is Pender Spellbane.
Here is my code:

Ma line of code
// The warlock used the "clone" spell and created evil antipodes of our archers.
// But even that evil spell has weaknesses.
// If your archer touches his antipode, then it will disappear.
// If an archer touches the wrong clone or attacks one of them, then the clones start to fight.
// We can find antipodes by their names - they are each other's reverse.
// This function checks two units whether they are antipodes or not.
function areAntipodes(unit1, unit2) {
    var id1 = unit1.id;
    var id2 = unit2.id;
    var idLength = id1.length;
    if (idLength != id2.length) {
        return false;
    }
    for (var i = 0; i < idLength.length; i++) {
        var id1Character = id1[i];
        var id2CharacterIndex = idLength - i;
        var id2Character = id2[i];
        if (id1Character != id2Character) {
            return false;
        }
    }
    return true;
}
var friends = hero.findFriends();
var enemies = hero.findEnemies();
// Find antipodes for each of your archers.
// Iterate all friends.
for (var i = 0; i < friends.length; i++) {
    var friend = friends[i];
    // For each of friends iterate all enemies.
    for (var j = 0; j < enemies.length; j++) {
        var enemy = enemies[j];
        // Check if the pair of the current friend and the enemy are antipodes.
        var IDsAreAntipodes = areAntipodes(friend, enemy);
        if (IDsAreAntipodes) {
            // If they are antipodes, command the friend move to the enemy.
            hero.command(friend, "move", enemy.pos);
            break;
        }
    }
}
// When all clones disappears, attack the warlock.
while (true) {
    var warlock = hero.findNearest(hero.findByType("warlock"));
    enemy = hero.findNearestEnemy();
    for (i = 0; i < friends.length; i++) {
        friend = friends[i];
        if (warlock && friend.health > 0) {
            hero.command(friend, "attack", warlock);
        } else if (enemy && friend.health > 0) {
            hero.command(friend, "attack", enemy);
        }
    }
}

This other line of code also fails:

Dem other line o' code
// The warlock used the "clone" spell and created evil antipodes of our archers.
// But even that evil spell has weaknesses.
// If your archer touches his antipode, then it will disappear.
// If an archer touches the wrong clone or attacks one of them, then the clones start to fight.
// We can find antipodes by their names - they are each other's reverse.
// This function checks two units whether they are antipodes or not.
function areAntipodes(unit1, unit2) {
    var reversed1 = unit1.id.split("").reverse().join("");
    return reversed1 === unit2.id;
}
var friends = hero.findFriends();
var enemies = hero.findEnemies();
// Find antipodes for each of your archers.
// Iterate all friends.
for (var i = 0; i < friends.length; i++) {
    var friend = friends[i];
    // For each of friends iterate all enemies.
    for (var j = 0; j < enemies.length; j++) {
        var enemy = enemies[j];
        // Check if the pair of the current friend and the enemy are antipodes.
        var IDsAreAntipodes = areAntipodes(friend, enemy);
        if (IDsAreAntipodes) {
            // If they are antipodes, command the friend move to the enemy.
            hero.command(friend, "move", enemy.pos);
            break;
        }
    }
}
// When all clones disappears, attack the warlock.
while (true) {
    var warlock = hero.findNearest(hero.findByType("warlock"));
    enemy = hero.findNearestEnemy();
    for (i = 0; i < friends.length; i++) {
        friend = friends[i];
        if (warlock && friend.health > 0) {
            hero.command(friend, "attack", warlock);
        } else if (enemy && friend.health > 0) {
            hero.command(friend, "attack", enemy);
        }
    }
}

Sorry for the hellish, eye-destroying, ruins-your-eyesight-for-life red text.
Please forgive me. —Ethan

@3nterpris3 to fix those red lines, add javascript, immediately after the first set of 3 dots, like this:

//type or paste code here
hero.say("now, your comments are shown as comments");
var friends = hero.findFriends(); //and code looks like code

image

Going with the second block of code (dump the first one), you want your hero to attack the warlock, not your friends. However, you need to do so only once all of the enemy archers are down. So, test for enemy archers by type and when there are no more, then test for the warlock and attack him.

Oh…you can ignore any skeletons.

1 Like

Thanks :smile:! I changed it and it worked!