Archers appear to not target the strongest enemy. Any help is appreciated.
var archerTarget = null;
// Soldiers spread out in a circle and defend.
function commandSoldier(soldier, soldierIndex, numSoldiers) {
var angle = Math.PI * 2 * soldierIndex / numSoldiers;
var defendPos = {x: 41, y: 40};
defendPos.x += 10 * Math.cos(angle);
defendPos.y += 10 * Math.sin(angle);
hero.command(soldier, "defend", defendPos);
}
// Find the strongest target (most health)
// This function returns something! When you call the function, you will get some value back.
function findStrongestTarget() {
var mostHealth = 0;
var bestTarget = null;
var enemies = hero.findEnemies();
// Figure out which enemy has the most health, and set bestTarget to be that enemy.
for(var i=0;i<enemies;i++){
var enemie = enemies[i];
if (enemie.health > mostHealth) {
mostHealth = enemie.health;
bestTarget = enemie;
}
}
// Only focus archers' fire if there is a big ogre.
if (bestTarget && bestTarget.health > 15) {
return bestTarget;
} else {
return null;
}
}
// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
function commandArcher(archer) {
var nearest = archer.findNearestEnemy();
if(archerTarget) {
hero.command(archer, "attack", archerTarget);
} else if(nearest) {
hero.command(archer, "attack", nearest);
}
}
while(true) {
// If archerTarget is defeated or doesn't exist, find a new one.
if(!archerTarget || archerTarget.health <= 0) {
// Set archerTarget to be the target that is returned by findStrongestTarget()
archerTarget = findStrongestTarget();
}
var friends = hero.findFriends();
var soldiers = hero.findByType("soldier");
// Create a variable containing your archers.
var archers = hero.findByType("archers");
for(var i=0; i < soldiers.length; i++) {
var soldier = soldiers[i];
commandSoldier(soldier, i, soldiers.length);
}
for(var i=0;i<archers.length;i++){
var archer = archers[i];
commandArcher(archer);
}
// use commandArcher() to command your archers
}
I haven’t tested this, but I’m pretty sure this is the only error. It’s a typo. It should be “archer”, singular, not “archers”, plural.
When you use hero.findByType(), you always have to use the .type of the unit, which is always singular.
Danny
Please could you post your new code.
This level is a tricky one. It sometimes acts quite strangely.
I’ll give your code a test and see if I can fix it.
Danny
var archerTarget = null;
// Soldiers spread out in a circle and defend.
function commandSoldier(soldier, soldierIndex, numSoldiers) {
var angle = Math.PI * 2 * soldierIndex / numSoldiers;
var defendPos = {x: 41, y: 40};
defendPos.x += 10 * Math.cos(angle);
defendPos.y += 10 * Math.sin(angle);
hero.command(soldier, "defend", defendPos);
}
// Find the strongest target (most health)
// This function returns something! When you call the function, you will get some value back.
function findStrongestTarget() {
var mostHealth = 0;
var bestTarget = null;
var enemies = hero.findEnemies();
// Figure out which enemy has the most health, and set bestTarget to be that enemy.
for(var i=0;i<enemies;i++){
var enemie = enemies[i];
if (enemie.health > mostHealth) {
mostHealth = enemie.health;
bestTarget = enemie;
}
}
// Only focus archers' fire if there is a big ogre.
if (bestTarget && bestTarget.health > 15) {
return bestTarget;
} else {
return null;
}
}
// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
function commandArcher(archer) {
var nearest = archer.findNearestEnemy();
if(archerTarget) {
hero.command(archer, "attack", archerTarget);
} else if(nearest) {
hero.command(archer, "attack", nearest);
}
}
while(true) {
// If archerTarget is defeated or doesn't exist, find a new one.
if(!archerTarget || archerTarget.health <= 0) {
// Set archerTarget to be the target that is returned by findStrongestTarget()
archerTarget = findStrongestTarget();
}
var friends = hero.findFriends();
var soldiers = hero.findByType("soldier");
// Create a variable containing your archers.
var archers = hero.findByType("archer");
for(var i=0; i < soldiers.length; i++) {
var soldier = soldiers[i];
commandSoldier(soldier, i, soldiers.length);
}
for(var i=0;i<archers.length;i++){
var archer = archers[i];
commandArcher(archer);
}
// use commandArcher() to command your archers
}
Yaay, this is very satisfying.
I found the error. There’s only one, and it’s on this line:
Remember that enemies is an array, not a number, and you can only compare numbers with < and >. How can you access the length of an array? (clue’s in the name).
If you change that, it will work .
Danny