Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Yet Another Crux of the Desert

First character I used moved too slowly so that I couldn’t get down to the bottom left in time. Second character I used moved too quickly it seemed, for he would set up the 4th fire-trap when the second ogre was coming. This would mean the ogre would not take a straight path and just barely (but accidentally) walk around the fire-trap. So, I decided to slow down my character by adding a say command, still to no avail. Long story short, now he tries to set up a second fire-trap where one is already set up, and blows himself up.

// Figure out which direction the ogres are coming from.

while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        // Left: enemy.pos.x is less than hero.pos.x
        var isLeft = hero.pos.x  > enemy.pos.x;
        // Above: enemy.pos.y is greater than hero.pos.y
        var isAbove = hero.pos.y < enemy.pos.y;
        // Right: enemy.pos.x is greater than hero.pos.x
        var isRight = hero.pos.x > enemy.pos.x;
        // Below: enemy.pos.y is less than hero.pos.y
        var isBelow = hero.pos.y < enemy.pos.y;
        
        // If enemy isAbove and isLeft:
        // buildXY() a "fire-trap" at the X mark.
        if(isLeft && isAbove) {
            hero.buildXY("fire-trap", 20, 51);
           
            hero.moveXY(40, 34);
        }
        
        // If enemy isAbove and isRight:
        // buildXY() a "fire-trap" at the X mark.
        if(isAbove && isRight) {
            hero.buildXY("fire-trap", 60, 51);
            hero.say("message");
            hero.moveXY(40, 34);
        }
        
        // If enemy isBelow and isLeft:
        // buildXY() a "fire-trap" at the X mark.
        if(isBelow && isLeft) {
            hero.buildXY("fire-trap", 20, 17);
            hero.moveXY(40, 34);
        }
        
        // If enemy isBelow and isRight:
        // buildXY() a "fire-trap" at the X mark.
        if(isBelow && isLeft) {
            hero.buildXY("fire-trap", 60, 17);
            hero.moveXY(40, 34);
    } else {
        hero.moveXY(40, 34);
    }
}
}

Quick glance. Last group, check left or right again.

1 Like

Very strange. I fixed that issue, and now, my hero sits there and does nothing…normally I don’t have such unfortunate results from improving my code!

Another quick glance:

        // Right: enemy.pos.x is greater than hero.pos.x
        var isRight = hero.pos.x > enemy.pos.x; //?

        // Below: enemy.pos.y is less than hero.pos.y
        var isBelow = hero.pos.y < enemy.pos.y; // ? 
        
        // If enemy isBelow and isRight:
        if(isBelow && isLeft) {
2 Likes

Ok, I see that, and fixed it, thank you…and now what I have happening is everything running perfectly, for several iterations. Then, my guy decides to randomly (double checked the X,Y) overshoot moving to the center. Which is extra odd, since he ran that code before just fine… And so he cannot see the ogre coming, which creates all kinds of problems.

(I might need to make a short video.) Anyways, here is the full updated code:

// Figure out which direction the ogres are coming from.

while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        // Left: enemy.pos.x is less than hero.pos.x
        var isLeft = hero.pos.x > enemy.pos.x;
        // Above: enemy.pos.y is greater than hero.pos.y
        var isAbove = hero.pos.y < enemy.pos.y;
        // Right: enemy.pos.x is greater than hero.pos.x
        var isRight = hero.pos.x < enemy.pos.x;
        // Below: enemy.pos.y is less than hero.pos.y
        var isBelow = hero.pos.y > enemy.pos.y;
        
        // If enemy isAbove and isLeft:
        // buildXY() a "fire-trap" at the X mark.
        if(isLeft && isAbove) {
            hero.buildXY("fire-trap", 20, 51);
           
            hero.moveXY(40, 34);
        }
        
        // If enemy isAbove and isRight:
        // buildXY() a "fire-trap" at the X mark.
        if(isAbove && isRight) {
            hero.buildXY("fire-trap", 60, 51);
            hero.moveXY(40, 34);
        }
        
        // If enemy isBelow and isLeft:
        // buildXY() a "fire-trap" at the X mark.
        if(isBelow && isLeft) {
            hero.buildXY("fire-trap", 20, 17);
            hero.moveXY(40, 34);
        }
        
        // If enemy isBelow and isRight:
        // buildXY() a "fire-trap" at the X mark.
        if(isBelow && isRight) {
            hero.buildXY("fire-trap", 60, 17);
            hero.moveXY(40, 34);
    } else {
        hero.moveXY(40, 34);
    }
}
}

Thanks again.

“overshoot moving to the center”
You won’t overshoot the target if you use move instead of moveXY. Replace all occurrences of

hero.moveXY(40, 34)
// with
while (hero.distanceTo({x:40, y:34}) > 0 )hero.move({x:40, y:34})
// or the same thing expressed with vectors
while (hero.distanceTo(Vector(40, 34)) > 0 )hero.move(Vector(40, 34));
1 Like