Restless dead javascript

// 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 attack(enemy) {
    if (hero.isReady("bash")) {
        hero.bash(enemy);
    }
    if (hero.canCast("chain-lightning", enemy)) {
        hero.cast("chain-lightning", enemy);
    }
    else {
        hero.attack(enemy);
    }
}

function collect() {
    var item = hero.findNearestItem();
    if (item) {
        moveTo(item.pos);
    }
}

function summon() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

function command() {
    var enemy = hero.findNearestEnemy();
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i ++) {
        if (enemy) {
            hero.command(friend, "defend", hero);
        }
    }
}

function moveTo(position) {
    if (hero.isReady("jump")) {
        hero.jumpTo(position);
    }
    else {
        hero.move(position);
    }
}

moveTo({'x': 56, 'y': 12});

while(true) {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        attack(enemy);
    }
    collect();
}

moveTo({'x': 19, 'y': 39});
while (enemy.health > 0) {
    var enemy = hero.findNearestEnemy();
    attack(enemy);
}
collect();
moveTo({'x': 19, 'y': 40});
while(true) {
    summon();
    command();
}

the hero is trying to reach the coin, he can’t reach and causing a timeout, please how to fix it?

try adjusting this to hero.moveXY(position.x, position.y)

Also, which hero are you using?

im using hattori 20 character

that not working here new code

// 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 attack(enemy) {
    if (hero.isReady("bash")) {
        hero.bash(enemy);
    }
    if (hero.canCast("chain-lightning", enemy)) {
        hero.cast("chain-lightning", enemy);
    }
    else {
        hero.attack(enemy);
    }
}

function collect() {
    var item = hero.findNearestItem();
    if (item) {
        moveTo(item.pos);
    }
}

function summon() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

function command() {
    var enemy = hero.findNearestEnemy();
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i ++) {
        var friend = friends[i];
        if (enemy) {
            hero.command(friend, "defend", hero);
        }
    }
}

function moveTo(position) {
    if (hero.isReady("jump")) {
        hero.jumpTo(position);
    }
    else {
        hero.moveTo(position);
    }
}

var enemies = hero.findEnemies();
for (var i = 0; i < enemies.length; i++) {
    var enemy = enemies[i];
    while (enemy.health > 0) {
        attack(enemy);
    }
}

collect();
hero.moveXY(19, 40);
while(true) {
    summon();
    command();
}


hero just ignore the yeti and get killed.

you didn’t put this in the while true loop. this would only run once in the beginning and that would be it. you should also put the collect function in the loop for it to constantly search for the gold

but i already put this as loop as long the enemy health > 0
hero will keep attacking.

yes but the for loop runs once, once the level starts, it checks for enemies, which returns 0 indexes, so no enemy, so it doesn’t attack, and then the for loop finishes

it can’t cause infinite loops isn’t it?

why would it? wdym?(20chars)

cuz it has 2 loop 20 char

that’s fine (20 chars)

ok that works too i already try but hero stop at the yeti place

and not collecting coins

Code please? (20chars)

// 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 attack(enemy) {
    if (hero.isReady("bash")) {
        hero.bash(enemy);
    }
    if (hero.canCast("chain-lightning", enemy)) {
        hero.cast("chain-lightning", enemy);
    }
    else {
        hero.attack(enemy);
    }
}

function collect() {
    var item = hero.findNearestItem();
    if (item) {
        moveTo(item.pos);
    }
}

function summon() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

function command() {
    var enemy = hero.findNearestEnemy();
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i ++) {
        var friend = friends[i];
        if (enemy) {
            hero.command(friend, "defend", hero);
        }
    }
}

function moveTo(position) {
    if (hero.isReady("jump")) {
        hero.jumpTo(position);
    }
    else {
        hero.moveTo(position);
    }
}

hero.moveXY(57, 23);
while(true) {
    var enemies = hero.findEnemies();
    for (var i = 0; i < enemies.length; i++) {
        var enemy = enemies[i];
        while (enemy.health > 0) {
            attack(enemy);
        }
    }
    collect();
}

hero.moveXY(19, 40);
while(true) {
    summon();
    command();
}


when i tried running the code, it arised the error of this line

make it as i told you, hero.moveXY(position.pos.x, position.pos.y)
also, put everything inside the function in an if statement to check if the item exists

wait bruh i forgot to change that

work with no error but still hero still stuck at the place

// 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 attack(enemy) {
    if (hero.isReady("bash")) {
        hero.bash(enemy);
    }
    if (hero.canCast("chain-lightning", enemy)) {
        hero.cast("chain-lightning", enemy);
    }
    else {
        hero.attack(enemy);
    }
}

function collect() {
    var item = hero.findNearestItem();
    if (item) {
        moveTo(item.pos);
    }
}

function summon() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

function command() {
    var enemy = hero.findNearestEnemy();
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i ++) {
        var friend = friends[i];
        if (enemy) {
            hero.command(friend, "defend", hero);
        }
    }
}

function moveTo(position) {
    if (hero.isReady("jump")) {
        hero.jumpTo(position);
    }
    else {
        hero.moveXY(position.x, position.y);
    }
}

hero.moveXY(57, 23);
while(true) {
    var enemies = hero.findEnemies();
    for (var i = 0; i < enemies.length; i++) {
        var enemy = enemies[i];
        while (enemy.health > 0) {
            attack(enemy);
        }
    }
    collect();
}

hero.moveXY(19, 40);
while(true) {
    summon();
    command();
}


I recommend you use flags for this

Also, no need to make 2 while true loops, just put them in one