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