i can’t solve this level, as the peasant is always walking away to the left. In the end my hero can’t protect him as he is walking to the position 40,37 while the peasent is far away and gets killed by the ogres.
Instead of walking back to the fixed position, i think my hero should walk to the peasent. But how can i get his position?
Here is my Code:
while (true) {
var enemy = hero.findNearestEnemy();
var distance = hero.distanceTo(enemy);
if (distance < 10) {
if (hero.isReady("cleave")) {
hero.cleave(enemy);
} else {
hero.attack(enemy);
}
} else {
hero.moveXY(40, 37);
}
}
Ok, i found a solution. But it depends on random if it works or not. This peasent is moving away from the fights in a very unpredictible way. So if you have luck, he stays near your defence positon. If not, he is walking right into the hands of this munchkins. But finally after several tries it worked succesfully.
var defpos ={x:40,y:37};
while (true) {
var enemy = hero.findNearestEnemy();
var distenemy = hero.distanceTo(enemy);
var disthome = hero.distanceTo(defpos);
if ((enemy.pos.x >25) && (enemy.pos.x < 50)) {
if (hero.isReady("dash")) {
hero.dash(enemy);
}
hero.attack(enemy);
} else {
//determine, from which side next enemy is coming
if (enemy.pos.x < defpos.x) {
//he is coming from left
if (hero.pos.x > defpos.x) {
hero.move(defpos);
}
} else if (enemy.pos.x > defpos.x) {
//he is coming from right
if (hero.pos.x < defpos.x) {
hero.move(defpos);
}
}
}
}
So, overall, I think this level needs to be redefined.
while(true) {
var enemy = hero.findNearestEnemy();
var distance = hero.distanceTo(enemy);
if (distance < 10) {
// Attack if they get too close to the peasant.
hero.attack(enemy);
hero.attack(enemy);
}else{
// Else, stay close to the peasant! Use else.
hero.moveXY(40, 40);
}
}