[quote="Wiilli, post:1, topic:6072"]
enemyIndex = 0
while len(enemies) > enemyIndex:
enemy = enemies[enemyIndex]
if enemy:
enemyIndex += 1
if enemy.health > mostHealth:
mostHealth = enemy.health
bestTarget = enemy
[/quote]
The problem is somewhere in here. When I paste this section into my code people die. You are using python, just replace it with a for loop and make your life happy.
for enemy in enemies:
if enemy.health > mostHealth:
bestTarget = enemy
mostHealth = enemy.health
I also have a code as well, however the error that pops out is “Too much recursion” and I am not sure why…
Here is my code:
// Hushbaum has been ambushed by ogres!
// She is busy healing her soldiers, you should command them to fight!
// The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!
// Soldiers spread out in a circle and defend.
this.commandSoldier = function(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);
this.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.
this.findStrongestTarget = function() {
var mostHealth = 0;
var bestTarget = null;
var enemies = this.findEnemies();
// Figure out which enemy has the most health, and set bestTarget to be that enemy.
var archerTarget = this.findStrongestTarget();
// 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.
this.commandArcher = function(archer) {
var nearest = archer.findNearestEnemy();
if(archerTarget) {
this.command(archer, "attack", archerTarget);
} else if(nearest) {
this.command(archer, "attack", nearest);
}
};
var archerTarget = null;
loop {
// If archerTarget is dead 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 = this.findStrongestTarget();
}
var friends = this.findFriends();
var soldiers = this.findByType("soldier");
for(var i=0; i < soldiers.length; i++) {
var soldier = soldiers[i];
this.commandSoldier(soldier, i, soldiers.length);
}
// use commandArcher() to command your archers
this.commandArcher(archer);
}
The var nearest = archer.findNearestEnemy(); part has a “tmp72[tmp73] is not a function” error. Whereas if I change it to var nearest = archer.findNearest(archer.findEnemies));, it says “tmp75[tmp76] is not a function”…
Where am I going wrong?!
At this part, now it says “Expected ‘}’ to match ‘{’ from line 56 and instead saw’;’.” // use commandArcher() to command your archers var archer = this.findNearest(friends); for (var i = 0, i < this.findByType("archer").length, i++) { this.commandArcher(archer); } }