Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Restless Dead - Almost There

// This level is supposed to be VERY hard! You may need a great strategy and or gear to complete it!
// Find and defeat the yeti then gather its essence for the ritual.
// You might want to gather the coins the yeti leaves behind, you'll need them to summon an army
// Stand at the summoning stone (red x) to begin summoning
// Now you just have to survive the undead hoard
function start() {
    while (true) {
        var enemy = hero.findNearestEnemy();
        var coin = hero.findNearestItem();
        if (enemy) {
            hero.attack(enemy);
        } else {
            if (coin) {
                hero.move(coin.pos);
                if (hero.gold >= 270) {
                    break;
                }
            }
        }
    }
}
function summon() {
    while (true) {
        if (hero.gold > hero.costOf("soldier")) {
            hero.summon("soldier");
        }
        if (hero.gold < hero.costOf("soldier")) {
            break;
        }
    }
}
function command() {
    while (true) {
        var soldiers = hero.findByType("soldier");
        for (var i = 0; i < soldiers.length; i++) {
            var soldier = soldiers[i];
            var enemy = soldier.findNearestEnemy();
            if (enemy) {
                hero.command(soldier, "attack", enemy);
            }
        }
    }
}
hero.moveXY(54, 14);
start();
hero.moveXY(35, 37);
summon();
hero.moveXY(19, 40);
hero.moveXY(25, 35);
command();
hero.moveXY(36, 32);

I hope my code pasted properly. I’m having issues with my last function “command().” Nothing else works after the command function so my hero can’t move, or join in the battle with my soldiers. My other functions have a break statement in the while loop. Is the lack of break statement the issue wit my “command()” function? I’m not sure where I would put it or under what if condition. Any insights would be appreciated. Thank you

The problem, as you said, is that you don’t have a way to end the loop. Instead of doing that, why don’t you move before the command() function, then, change the command function to include code for the hero to attack the enemy.
Danny

1 Like
function start() {
    while (true) {
        var enemy = hero.findNearestEnemy();
        var coin = hero.findNearestItem();
        if (enemy) {
            hero.attack(enemy);
        } else {
            if (coin) {
                hero.move(coin.pos);
                if (hero.gold >= 270) {
                    break;
                }
            }
        }
    }
}
function summon() {
    while (true) {
        if (hero.gold > hero.costOf("soldier")) {
            hero.summon("soldier");
        }
        if (hero.gold < hero.costOf("soldier")) {
            break;
        }
    }
}
function command() {
    while (true) {
        var soldiers = hero.findByType("soldier");
        for (var i = 0; i < soldiers.length; i++) {
            var soldier = soldiers[i];
            var enemies = hero.findNearestEnemy();
            var enemy = soldier.findNearestEnemy();
            if (enemy) {
                hero.command(soldier, "attack", enemy);
                hero.attack(enemy);
            }
        }
    }
}
hero.moveXY(54, 14);
start();
hero.moveXY(35, 37);
summon();
hero.moveXY(19, 40);
hero.moveXY(42, 35);
command();

Thanks @Deadpool198! I threw a hero.attack(enemy) in the command function. It’s really weird because my hero will attack, but the soldiers will not. When my hero dies, all the soldiers begin attacking. That’s why I tried to make another function for my hero attacking, but as you mentioned I’m not sure how to break the loop of the summon soldiers function. Thanks in advance for any assistance.

function command() {
    while (true) {
        var soldiers = hero.findByType("soldier");
        for (var i = 0; i < soldiers.length; i++) {
            var soldier = soldiers[i];
            var enemy = soldier.findNearestEnemy();
            if (enemy) {
                hero.command(soldier, "attack", enemy);
            }
        }
    }
    while(true) {
        var enemies = hero.findNearestEnemy();
        if (enemies) {
            hero.attack(enemy);
        }
    }
}

I also tried throwing in another while loop in the function and using the different variable “enemies” for the hero. It seems I can only get either the soldiers to attack or the hero…but not both.

The second bit of code won’t work because the second while true loop will never run.
The first code doesn’t work because you put the hero.attack() inside the for loop. Instead why don’t you put it in a separate if statement:

# define enemy
# if enemy
#     hero attack
# do for loop

Danny

1 Like