Hi guys, I have ran into an infinite loop in my code
// Fight your way into the Inner Sanctum of the ogre chieftain, and defeat her.
var stage = 1;
var friends = hero.findFriends();
for (var i = 0; i < friends.length; ++i) {
hero.command(friends[i], "move", Vector(0, 37));
}
function summonArcher() {
if (hero.gold > hero.costOf("archer")) {
hero.summon("archer");
}
}
function specAttack(target) {
if (hero.distanceTo(target) < 100) {
while (target.health >= 0) {
if (hero.isReady("slam")) {
hero.slam(target);
} else if (hero.isReady("bash")) {
hero.bash(target);
} else if (hero.canCast("chain-lightning")) {
hero.cast("chain-lightning", target);
} else if (hero.canCast("counter-blast")) {
hero.cast("counter-blast", target);
} else {
hero.attack(enemy);
}
}
}
}
function lowestHealthPaladin() {
var lowestHealth = 99999;
var lowestFriend = null;
var friends = hero.findFriends();
for (var i = 0; i < friends.length; ++i) {
// if (friend.type != "paladin") {
// continue; }
if (friends[i].health < lowestHealth && friends[i].health < friends[i].maxHealth) {
lowestHealth = friends[i].health;
lowestFriend = friends[i];
}
}
return lowestFriend;
}
function commandPaladin(paladin, stage) {
var target = paladin.findNearestEnemy();
if (target) {
if (paladin.canCast("heal")) {
if (hero.health <= 1000) {
target = hero;
if (target) {
hero.command(paladin, "cast", "heal", target);
}
} else {
target = lowestHealthPaladin();
if (target) {
hero.command(paladin, "cast", "heal", target);
}
}
} if (paladin.health < 100) {
hero.command(paladin, "shield");
} if (stage == 3) {
var pos = {"x": hero.pos.x, "y": hero.pos.y};
hero.command(paladin, "move", pos);
} if (stage == 4) {
if (warlock) {
target = warlock;
} if (target) {
hero.command(paladin, "attack", target);
} else {
enemy = paladin.findNearestEnemy();
if (enemy) {
hero.command(paladin, "attack", enemy);
}
}
} if (stage == 5) {
target = hero.findNearestEnemy();
if (target) {
hero.command(paladin, "attack", target);
}
}
}
}
function commandSoldier(soldier, stage) {
var target = soldier.findNearestEnemy();
if (target) {
if (stage == 3) {
var pos = {"x": hero.pos.x, "y": hero.pos.y};
hero.command(soldier, "move", pos);
} if (stage == 4) {
var warlock = archer.findNearest(hero.findByType("warlock"));
if (warlock) {
target = warlock;
} if (target) {
hero.command(soldier, "attack", target);
} else {
var enemy = soldier.findNearestEnemy();
if (enemy) {
hero.command(soldier, "attack", enemy);
}
}
} if (stage == 5) {
target = hero.findNearestEnemy();
if (target) {
hero.command(soldier, "attack", target);
}
}
}
}
function commandArcher(archer, stage) {
var target = archer.findNearestEnemy();
if (target) {
if (stage == 1) {
hero.command(archer, "move", Vector(0, 37));
} if (stage == 3) {
var pos = {"x": hero.pos.x, "y": hero.pos.y};
hero.command(archer, "move", pos);
} if (stage == 4) {
var warlock = archer.findNearest(hero.findByType("warlock"));
if (warlock) {
target = warlock;
} if (target) {
hero.command(archer, "attack", target);
} else {
var enemy = archer.findNearestEnemy();
if (enemy) {
hero.command(archer, "attack", enemy);
}
}
} if (stage == 5) {
target = hero.findNearestEnemy();
if (target) {
hero.command(archer, "attack", target);
}
}
}
}
function commandFriends(stage) {
var friends = hero.findFriends();
for (var i = 0; i < friends.length; ++i) {
if (friends[i].type == "paladin") {
commandPaladin(friends[i], stage);
} if (friends[i].type == "soldier") {
commandSoldier(friends[i], stage);
} if (friends[i].type == "archer") {
commandArcher(friends[i], stage);
}
}
}
while(true) {
summonArcher();
var catapults = hero.findByType("catapult");
if (stage == 1) {
var enemy = hero.findNearestEnemy();
if (enemy && enemy.type != "door") {
specAttack(enemy);
commandFriends();
} else if (catapults.length) {
specAttack(hero.findNearest(catapults));
}
} else if (!catapults.length){
stage++;
break;
}
}
when I change this line:
} else {
hero.attack(enemy);
}
to this:
} else {
hero.attack(target);
}
My hero attacks the top catapult but then when she goes for the bottom one, it just stops. This doesn’t happen when the argument for attack is enemy
and not target
. However, when it is enemy, it hits the gate once, slams the catapult, hits the gate again and finishes the catapult which results in getting hit twice by the catapult. Could someone please help? Thanks