I am summoning soldiers and archers, but there are just too many ogres! Here is my code:
function summonTroops() {
hero.summon("soldier");
hero.summon("archer");
}
while (true) {
let item = hero.findNearestItem();
let friends = hero.findFriends();
let enemy = hero.findNearestEnemy();
if (hero.gold >= 40) {
summonTroops();
}
if (enemy) {
for (let friend of friends) {
if (friend && friend.type == "soldier" || friend && friend.type == "archer") {
hero.command(friend, "attack", enemy);
} else {
if (enemy.type !== "ogre") {
hero.command(friend, "move", {x: 30,y: 36});
}
}
}
if (item) {
for (let friend of friends) {
if (item && friend && friend.type == "peasant") {
hero.command(friend, "move", item.pos);
}
}
}
}
}
You should move the code that commands the peasant out of the if (enemy)
statement otherwise the peasant will stop collecting coins when there are no enemies, as well as deleting
else {
if (enemy.type !== "ogre") {
hero.command(friend, "move", {x: 30,y: 36});
}
}
otherwise the peasant will run to {x: 30,y: 36}
when an ogre is even on screen, and changing findNearestItem() and findNearestEnemy() so that it is each friend’s nearest item or enemy.
I changed the findNearestItem();
and the findNearestEnemy();
, but it still doesn’t work.
I also put in the:
if (enemy.type !== "ogre") {
hero.command(friend, move, {x: 30, y: 36});
}
I meant changing hero.findNearestEnemy() to friend.findNearestEnemy() and hero.findNearestItem() to friend.findNearestItem() so the soldiers and archers would attack their nearest enemy and the peasant would collect its nearest coin and not the heros nearest coin.
wdym?
Oh, sorry. I will change that.
If I change the findNearestItem();
and findNearestEnemy();
, then an error comes up and says,
try hero.findFriends();
and hero.findNearestEnemy();
.
You will need to move the defining of the variables into the for loops that command the friends (as well as move the if statements that check if there is an item or enemy.)