Level: Peasant Protection


#41
loop:
    enemy = self.findNearestEnemy()
    distance = self.distanceTo(enemy.pos)
    if distance < 10:
        self.attack(enemy)
      self.moveXY(40,37)

#42

Thanks so much. The same code is now working. The only difference is that now I am using some updated gear and I have gained a few levels playing other levels.

Thank you!!

Clare


#43

No Problem if you need anymore help ill see what I can do.


#44

Hello all, I’ve got the same problem as the others. I estimate my success rate to about 10%. The character almost always successfully kills the first wave of enemies coming from the left, but does not move quickly enough towards the second wave coming from the right which kill the peasant. Increasing the attack distance to 20 or 30 does help sometimes, but not always.

I play with javascript, and my code is essentially the same as @clare12345’s, only a bit more compact, e.g.

var E = this.findNearest(this.findEnemies());
if (this.distanceTo(E) < 10) {
    this.attack(E);
} else {
    this.move({x: 40, y: 37});
}

Further, my figher character is level 12 with speed 8, and the level fails with either the longsword (cleave) equipped or the faster short sword. Using cleave does help, but this randomly depends on the number of enemies that are currently in cleaving range. Complicating the tests with checking the length of the enemy list etc does not help at all.

One bug I noticed is that the character seems to get stuck in non-attacking, non-noticing move mode when it is already on the XY-coordinate that the move command directs it to.


#45

Follow-up: buying and equipping the “softened leather boots” instead of the “compound boots” works for me: the character does not dawdle nor bumps into the peasant but niceley stays within range:

var E = null;
loop {
    E = this.findNearest( this.findEnemies() );
    if (this.distanceTo(E) < 10) {
        if (this.isReady("cleave"))
            this.cleave(E);
        else
            this.attack(E);
    } else {
    // instead of the failing? this.move({x: 40, y: 37});
        this.moveXY(40, 37);
    }
}

#46

Yeah, sorry–I need to do a big refactoring to get the move boots to work without dropping a frame every small movement. For now, just stick to moveXY boots.


#47

can anyone post how to beat pheasent protection because im stuck


#48

Hello Joshua,

it is usually not the case that we (as community) hand out code directly, but you present your code and we tell you how to improve.
Despite from that, this post contains multiple hints and solutions. Did you actually read it?

Greetings,
J_F_B_M


#49

yes but i’m at school and i got free time so im usually on at this time. I bought alot of stuff so maybe it would help but im stumped

Merged Double-Post

alright i tried can’t figure it out


#51

for real i cant beat it


#52

Second Time:

What did you try, and what do you think why it won’t work? Post your code, maybe it is just a problem with indentations (happens more often than one might think).


#53

I’m stuck as well. My code is:

loop {
    var enemy = this.findNearestEnemy();
    var distance = this.distanceTo(enemy);
    if (distance < 10) {
        this.attack(enemy);
    } else {
        this.moveXY(40, 37);
    }
}

Any thoughts on what’s wrong with the code?


#54

That looks right, but what seems to be going wrong with the level? Is there an error? Is the peasant dying, and if so, what is your hero doing that doesn’t protect the peasant?


#56

Could be that you’re trying to get the distance to enemy, but enemy is potentially null where you check it.

loop {
    var enemy = this.findNearestEnemy()   //can return null
    var distance = this.distanceTo(enemy) //checks against null
    ...
}

You should check if (enemy) {...}. To actually avoid some issues, you should check the distance in chain with the enemy.

loop {
    var enemy = this.findNearestEnemy();
   if (enemy && this.distanceTo(enemy) < 10) {
        this.attack(enemy);
    } else {
        this.moveXY(40, 37);
    }
}

This code firsts checks if there is an enemy, and if this is the case, checks the distance to it. If there is no enemy, this.distanceTo(enemy) is never checked.


Eductation Part, it is not necessary to read this

This is a nice feature of modern programming languages which allows you to chain checks which otherwise would fail. Most often this is used to first check a precondition that absolutly must be valid before the actual condition is checked.

//avoid dividing through 0
if (a != 0 && b/a = 0.5) {...}

//avoid targetting non-existent enemys
if (enemy && this.distanceTo(enemy) < 10) {...}

In the early time of programming, both checks would have caused an error. Todays compilers are pretty advanced compared to that.


#57

Hero doesn’t get back to X (40,37) on time to defend peasant from the first wave of attacks coming from the left. When I tried reducing distance, she notices the first attackers too late, when I tried adding to it, she wanders out too far.


#58

Thanks for trying to solve my problem! Unfortunately the peasant still dies at the first wave of attacks coming from the left (hero doesn’t get there on time)


#59

Ok, my guess was wrong…

Which hero do you use? Anya/Tharin? How much bonus-speed do you get from shoes?


#60

I’m using Anya, my speed is shown to be capped at 8 no matter what shoes I use (I have the ones that use move() instead of moveXY() as well but it doesn’t work out with those either)


#61

I just checked on your account and it looks like it’s working now; did anything change? Or are you still somehow seeing the loss?


#62

Hi Nick, yes the problem was on my part – I was using the simple sword to attack instead of the cleaver. It didn’t kill the ogres in time that’s why the hero was late to arrive to the left side. I was using the sword in the previous mission and didn’t think to change it for this level. Thanks for the help!