Hunting Party (JavaScript) Help

I’m having some trouble with my code and I’m hoping someone here can help. In the level “Hunting Party”, I’m trying to get all the soldiers to move together en masse to try and kill the enemies without letting one of my guys die (figuring safety in numbers), but no one is doing anything. Where am I going wrong? Thanks

var points = [];
points[0] = {'x': 60, 'y': 70};
points[1] = {'x': 60, 'y': 25};
points[2] = {'x': 80, 'y': 50};

var friends = this.findNearest(this.findFriends());
for (var friendIndex = 0; friendIndex < friends.length; f++) {
var who = friends[friendIndex];
    for (var where = 0; where < points.length; where++) {
        var here = points[where];
        this.command(who, 'move', here);
        var enemy = who.findNearest(who.findEnemies());
        if (enemy) {
            this.command(who, 'attack', enemy);
        }
    }
}

I saw three things to start:

  1. You don’t want the findNearest on your first findFriends call.
  2. You want a loop around your for-loop so that you tell each friend what to do each frame.
  3. You’re incrementing f but using friendIndex.

That might get you a little further. Hope this helps.

thanks! It definitely helped. Now they move and attack. But they move straight to the last point I want them to move to. I’ve friend a few different things, mainly rearranging the code, but they just keep doing the same thing, which means I’m probably misreading the way the code is supposed to work. here’s one of the variations I have

var points = [];
points[0] = {'x': 60, 'y': 70};
points[1] = {'x': 60, 'y': 25};
points[2] = {'x': 80, 'y': 50};

loop {
    var friends = this.findFriends();
    for (var friendIndex = 0; friendIndex < friends.length; friendIndex++) {
        var friend = friends[friendIndex];
        for (var where = 0; where < points.length; where++) {
            var here = points[where];
            this.command(friend, 'move', here);
            var enemy = friend.findNearestEnemy();
            if(enemy) {
                this.command(friend, "attack", enemy);
            }
        }
    }
}

I’m trying to get them to move to the first point, if there’s an enemy there attack, then move onto the next point. Instead they just move straight to points[2], attack the enemy there, and then just stay there. Where is my thinking wrong?

You have:

Short version:

You are sending all commands at “once”, giving them no time to fulfill any of the commands…
(There is no time to walk to point 0, before you tell them to walk to point 1, etc.)

Long version:

loop
    for friend
        for point
            move friend to point

Which is to say:

loop every time you can take an action
    friend 0
        move to point 0
        move to point 1
        move to point 2
    friend *etc*
        move to point 0
        move to point 1
        move to point 2

What they end up doing every cycle through the “loop” is the last thing you told them to do…

    all friends
        move to point 2

Do you really mean send all troops to point[0], then all troops to point[1]?? or do you mean send top group to top point middle group to middle point, etc…

If you mean “ALL to point[0]”, then you need to wait until they are all there and have killed all enemies they can see before you send them to point[1] …

If you mean top group to top point middle to middle etc, then you need to check the y position of each soldier and send them towards the closest point match (or decide on a matching range for each point and check if the soldier is in it such as say: friend y > middle point y + a then go to top, friend y < middle point y - a then go to bottom or some other (probably better) way to tell each group to keep their y and advance to the east (increase x).

loop {
var friends = this.findFriends();
for(var j=0; j < friends.length; j++) {
    var friend = friends[j];
    var archer = (friend.type == "archer");
    var soldier = (friend.type == "soldier");
    var enemy = friend.findNearestEnemy();
    if(enemy) {
        this.command(friend, "attack", enemy);
    } else {
        this.command(friend, "move", this.pos);
        if (enemy) {
            this.command(friend, "attack", enemy);
        }
    }
}

}
I would love to know how to select the archers by name and send them up, the soldiers to the middle and I’m going down using the spells. Here I haven’t done any of that except for making all soldiers follow me, kill the bottom ogres and 0 losses in my army.
How can I command just my archers?
And then again, how can I command just my soldiers? Thanks a lot

You can use this.findByType(type). This command will return a list of every unit of type type.

I don’t have a findByType method. What do I need to buy? Thanks

You can get this.findByType with the Hardened Steel Glasses or up, but you may find it less expensive to just to do a type check like this:

for (var i = 0, i < friends.length,i++) {
    var friend = friends[i]
    if (friend.type == "soldier") {
        // Command your soldiers here
    } else if (friend.type == "archer" ) {
        // Command your archers here
    }
}

Hey, thanks a lot, it worked pretty well.
I have two issues:
I want to split the soldiers in two, those whose distance to enemy is <40 and those >40, but it tells me continually distance is null (I took it out from my code…)
The second and most important, I can’t get my player to attack:

loop {
var friends = this.findFriends();
for(var j=0; j < friends.length; j++) {
    var friend = friends[j];
    var enemy = friend.findNearestEnemy();
    if(enemy){
        this.command(friend, "attack", enemy);
    }
    else {
        if (friend.type == "soldier") {
            this.command(friend, "move", {x:87, y:49});
        }
        else if (friend.type == "archer" ) {
            this.command(friend, "move", {x:61, y:66});
            this.move({x:63, y:25});
        }
    }
}

}

I can’t seem to get how the level should be completed either, is there an example code I can borrow?

Basically, command all your friends to move forward a specific amount if there are no enemies that can be seen. Else, attack.

I need help on this level. My code looks like this:

var friends = this.findFriends();
var j = 0;
loop {
for(var i=0; i < friends.length; i++) {
friend = friends[i];
var enemy = friend.findNearestEnemy();
if (enemy) {
this.command(friend, “attack”, enemy);
} else {
var step = 1;
if (friend.type === “archer”) {step = (j%5>1)?1:0;}
this.command(friend, “move”, {x: friend.pos.x+step, y: friend.pos.y});
}
}
j++;
if (j > 500) {break;}
}

Would anybody mind telling me what I am doing wrong?