Sarven Treasure, JS, Running to the same Teleport all the time


#1

Hello there,

I’m stuck at Sarven Treasure, like the title tells you.
Here the Code:

function findOptimalCoin(coins){
    var optimalCoin = null;
    var coinIndex = 0;
    var mostValueCoin = 0;
    
    while (coinIndex < coins.length) {
        var coin = coins[coinIndex];
        var value = coin.value;
        var distance = hero.distanceTo(coin);
        if (value / distance > mostValueCoin) {
            mostValueCoin = value / distance;
            optimalCoin = coin;
        }
        coinIndex++;
    }
    return optimalCoin;
}

while(true){
    var coins = hero.findItems();
    var coin = null;
    coin = findOptimalCoin(coins);
    var enemies = hero.findEnemies();
    var enemy = hero.findNearest(enemies);
    
    if(enemy){
        var ogerDistance = hero.distanceTo(enemy);
        if(ogerDistance > 10){
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
        else if(ogerDistance <= 10){
            if (hero.pos.x <= 40 && hero.pos.y <= 35 ){
                hero.moveXY(5, 20);
            }
            else if (hero.pos.x <= 40 && hero.pos.y >= 35){
                hero.moveXY(5, 50);
            }
            else if (hero.pos.x >= 40 && hero.pos.y <= 35){
                hero.moveXY(76, 20);
            }
            else if (hero.pos.x >= 40 && hero.pos.y >= 35){
                hero.moveXY(76, 51);
            }
        }
    }
    else if(coin){
            hero.moveXY(coin.pos.x, coin.pos.y);
            }
}

Im looking for the best Coin. When there is an Enemy more than 10 away take it.
If it’s closer than 10 move to the closest Teleport.
When there is no enemy grab the best Coin.

My Problem is, that when an Enemy is close to me, Hero runs into the correct Teleport. But then instead of grabbing more Coins he goes to the same Teleport all over again.

Thx in advance :wink:

P.S.: Sorry for the format but it’s not working like supposed.


#2

Thx for formatting :wink:


#3

I believe the problem is that you are using hero.moveXY. You should be using hero.move for this. The issue with hero.moveXY is that once it executes, no other code will execute until the hero reaches that point. And because you never reach the point you issued your hero because it teleports you, he will try to keep on walking towards that point. hero.move issues the hero to move towards a point for a “single step”, so once you complete ure step, the code continues to run, and it will check for updated ogre distance, and most likely work.

TLDR: use hero.move() instead of hero.moveXY()

PS: If you have not yet reached mountain level, you may not be completely sure on how to use hero.move with specific x and y parameters. For python I know its like this
hero.move({‘x’: someNumber, ‘y’: someOtherNumber})
However, I am unsure with the syntax for Javascript


#4

Thank you alot!
That was the problem. I tricked a little bit around. The crosses have a 2m Radius so you can reach the point by changing the values to this.
But I went back on killing the enemies, its easier :smiley:
When I get the other Boots I will rewrite it.