[SOLVED]Javascript: Aggressive Mimicry Help

This is my code.

// Protect the village from the ogres. 
// Watch for ogres, peasants and ogres disguised as peasants.
// This function checks if the text starts with the word.
function startsWith(text, word) {
    // If the word is longer then the text:
    if (word.length > text.length) {
        return false;
    }
    // Loop through the indexes of word and text.
    for (var index = 0; index < word.length; index++) {
        // If characters with the same index are different:
        if (word[index] != text[index]) {
            // Then the word doesn't coincide with the text.
            return false;
        }
    }
    // We checked all letters and they are the same.
    return true;
}
var ogreNameStart = "Zog";
var archers = hero.findByType("archer");
while (true) {
    var suspectFriend = hero.findNearest(hero.findFriends());
    var suspectName = suspectFriend.id;
    // Use the function "startsWith" to check
    // if suspectName starts with "Zog":
    if (startsWith(suspectName, "Zog")) {
        hero.attack(suspectFriend);
    }
    // Then attack suspectFriend:
    var enemy = hero.findNearestEnemy();
    // if there is an enemy, then attack it:
    if (enemy) {
        hero.attack(enemy);
    }    // Else return to the red X mark:
    else {
        hero.moveXY(27, 27);
    }
    if (hero.gold >= hero.costOf("archer")) {
        hero.summon("archer");
    }
    for (var i = 0; i < archers.length; i++) {
        var archer = archers[i];
        if (archers) {
            hero.command(archers, "attack", enemy);
        }
    }
}

The archers don’t do anything! (no errors)
Thanks
@sci12

2 Likes

You know that deleting all archer code you will pass the level, do you?
I tried to pass the level with archers, correcting your obvious errors ( i suppose you made them simply by omission, not lack of knowledge )

21	var archers = hero.findByType("archer"); //  not outside the main loop
22	while (true) {
        var archers = hero.findByType("archer"); // put find archers here 
42	    for (var i = 0; i < archers.length; i++) {
43	        var archer = archers[i];
            var foe = archer.findNearestEnemy() // archer finds the foe 
44	        if (foe) {
45	            hero.command(archer, "attack", foe);
46	        }
47	    }

but this doesn’t work either.
I can make it somehow to work if the archers stay behind the fence but the question remains why the hero is passive when the archers are summoned at point(27, 27) and Zogs emerge.
@Deadpool198 Danny, if you have time can you try the level summoning archers at point(27, 27)? They must act along with the hero killing the Zogs and the obvious enemies.

1 Like

I removed the archer code but it still fails

// Protect the village from the ogres. 
// Watch for ogres, peasants and ogres disguised as peasants.
// This function checks if the text starts with the word.
function startsWith(text, word) {
    // If the word is longer then the text:
    if (word.length > text.length) {
        return false;
    }
    // Loop through the indexes of word and text.
    for (var index = 0; index < word.length; index++) {
        // If characters with the same index are different:
        if (word[index] != text[index]) {
            // Then the word doesn't coincide with the text.
            return false;
        }
    }
    // We checked all letters and they are the same.
    return true;
}
var ogreNameStart = "Zog";
while (true) {
    var suspectFriend = hero.findNearest(hero.findFriends());
    var suspectName = suspectFriend.id;
    // Use the function "startsWith" to check
    // if suspectName starts with "Zog":
    if (startsWith(suspectName, "Zog")) {
        hero.attack(suspectFriend);
    }
    // Then attack suspectFriend:
    var enemy = hero.findNearestEnemy();
    // if there is an enemy, then attack it:
    if (enemy) {
        hero.attack(enemy);
    }    // Else return to the red X mark:
    else {
        hero.moveXY(27, 27);
    }
}

What is the issue?

1 Like

I think the code is OK. All is working smoothly but the ogres kill you?

1 Like

I used a Basic Sword and I won.

1 Like

What’s your equipment, sci12?

3 Likes

The reason for the error is the what you are defining suspectFriend as.
Take a look at that line:

var suspectFriend = hero.findNearest(hero.findFriends());

Archers are friends no? (I think they would be sad if they weren’t…)
They are also much closer to your hero than "Zog" should ever get. Meaning he goes undetected.
Instead maybe narrow down the search to just peasants with: hero.findNearest(hero.findByType("peasant"))
This still has problems because there is a delay while the good peasants are still your nearest peasants. When “Zog” becomes your nearest peasant, it’s too late to save your archers, who, if even just one of them dies, fail the whole level.
So it will be necessary to maybe loop through the peasants and call the startsWith() function on all of them, returning “Zog” at the end, so you can attack him the moment he appears.
Or maybe make a for loop which singles peasants down to those on the right side of the map (x>19).
I hope this helps, and solves the mystery @sci12 @xython

6 Likes

Using Danny’s hint, the archers do their thing.
It’s better to change the health of funny dressed ogres from 1 to 6. This solves the level without calling the function:

    suspectFriend = hero.findNearest(hero.findByType("peasant"))
    if suspectFriend.health == 1:
        hero.attack(suspectFriend)

No, my hero survives, one of the innocent peasants die!

1 Like

Screenshot 2021-02-08 at 9.19.09 AM
Here is my equipment.
@PeterPalov

When I do this, one of the archers die and I fail.
@Deadpool198
Thanks
Quick question: Does my code follow what the level wants me to do?

A trashy sword is all you need to pass the level. Archers are too squishy in the vicinity of ogres and if you want to kill the them without the help of the hero it will be a bit complicated. Maybe you can do it with soldiers. If you follow the Danny’s instructions or completely remove the archers code you will solve the level. If you want I can see your code with your equipment PM me including the session ID.
How to do it: Run the level, then open the Web developer Tools with Ctrl+Shift+I (Firefox) , then Console. Somewhere you will see


copy the sessionIDs xxxxxxxxxxxxxxx number and send it to me.![mim0|690x457]

Funny way to solve the level. You don’t need any armour or weapon. ( I still cannot upload the gif to the discourse)

2 Likes

I like the way you think but this doesn’t teach the concept of the level.

1 Like

your current code in the while loop is:

22	while (true) {
23	    var suspectFriend = hero.findNearest(hero.findByType("peasant"));
24	    var suspectName = suspectFriend.id;
27	    if (startsWith(suspectName, "Zog")) {
28	        hero.attack(suspectFriend);
29	    }
31	    var enemy = hero.findNearestEnemy();
33	    if (enemy) {
34	        hero.attack(enemy);
35	        if (hero.isReady("cleave")) {
                    // I really dislike stacking of ifs
36	            hero.cleave(enemy);            
37	        }
38	        if (hero.canCast("chain-lightning",enemy)) { 
                    // I really dislike stacking of ifs
39	            hero.cast("chain-lightning", enemy);
40	        }
41	    }    // Else return to the red X mark:
42	    else {
43	        hero.moveXY(27, 27);
               // you can also use move and usually it's quicker
               // avoid using moveXY() if you can
               // hero.move({x:27, y: 27});                      
44	    }
45	}

The result is:


because you stop attacking the nearest enemy chasing the fake peasant.
Then you suddenly have two adversaries.
The simplest solution is to beat the nearest enemy until defeated.
All attack code can be as simple as:

    var enemy = hero.findNearestEnemy();
    if (enemy) {
        while (enemy.health > 0)
            hero.attack(enemy);

    }    // Else return to the red X mark:
    else {
        // hero.moveXY(27, 27);
        // it works with moveXY() but move() is better
        hero.move({x:27,y: 27});        
    }

Running the code with corrections / no cleave, no lightning, with move() /:


Please delete entirely your previous post with your session id. Publishing it here you give access to your level for everyone forever.

1 Like

If this isn’t a Multiplayer Arena, session will give you code.

1 Like

What do you do to find the code?

First look into your code:

    var suspectName = suspectFriend.id;
    if (suspectName) { // you put  enemy instead of suspectName
        if (startsWith(suspectName, "Zog")) { 
            hero.attack(suspectFriend);
        }
    }

You can also chain lightning the enemy:

    var enemy = hero.findNearestEnemy();
    if (enemy) {
        while (enemy.health > 0) {
            if (hero.isReady('chain-lightning'))
               hero.cast('chain-lightning', enemy);
            else 
               hero.attack(enemy);
        }
    }
if (suspectFriend) {
        if (startsWith(suspectName, "Zog")) {
            hero.attack(suspectFriend);
        }
    }
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        while (enemy.health > 0) {
            if (hero.canCast("chain-lightning", enemy)) {
                hero.cast("chain-lightning", enemy);
            } else {
                hero.attack(enemy);
            }
        }
    } else {
        hero.move({
            x: 27,
            y: 27
        });
    }

Here is my code the level and the level still fails @xython
edit: I reloaded the page and submitted it and it said success!

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.