# Stuck on "peasant protection" - farmer gets killed always

Hello ,

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) {
hero.cleave(enemy);
} else {
hero.attack(enemy);
}
} else {
hero.moveXY(40, 37);
}
}
``````

Any help would be apreciated.

it depends on the book you are using but you can use

``````x = peasent.pos.x
y = peasent.pos.y
``````

hopefully that helps

Thank. Looks good.

But sadly, i do not have access to this yet. Here are the methods i can use:

The boots i have give me a max speed of 8. This dash() seems very useless, as it is so short you do not notice any difference to the normal move.

To me this Level seems unsolvable with the given code possibilities and conditions.

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)) {
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);
}
}

here is my code, I had no problem

Hi VDude,

yes, that was i was starting with and what i think is expected here. But …

``````    your code lasts for 14 seconds - tried several times!
``````

This peasant is always walking away from the fights - this yellow-belly, scaredy cat.

See here:

Tharin is staying guard on X40 Y37 as suggested, while Viktor is getting slayered by the munchkin which is outside the 10m range.

Don`t use pet or write before while-loop pet.moveXY(35,19) and problem is solved …

dear juraj_pechac,
you are aboslutly right. My little pet caused all this problems!

So to anybody else out there:

DON’T USE THE PET IN THIS LEVEL!

Leaving the pet at home, this was a milkrun. On the other side, i have learned a lot while chewing on this nut.

Instruction don’t give a clue here and are misleading. According to this it is

``````hero.pet;
``````

with no Arguments. But you have statet, that it is

``````pet.moveXY(x,y);
``````

So instructions are wrong here.

Hey, thanks for the report.

Pets are a new addition, I didn’t expect the wolf pup to cause this much trouble.

I’ll work on fixing this and future levels throughout the week, thanks for understanding!