My code has an error that it didn’t have before:
I don’t understand where I give an undefined value in the functions, I always check if the object exists… I know this error comes from internal code somewhere, but I don’t understand where it could be trying to read an undefined value
hero.isAbleToAttack = function (enemy) {
return hero.isPathClear(hero.pos, enemy.pos);
};
hero.findEnemiesInSight = function () {
let enemies = [];
for (let enemy of hero.findEnemies()) if (hero.isAbleToAttack(enemy)) enemies.push(enemy);
return enemies;
};
hero.findLowestFriend = function () {
let friends = hero.findFriends();
let lowestFriend = friends[0];
for (let friend of friends) if (friend.health < lowestFriend.health) lowestFriend = friend;
return lowestFriend;
};
hero.cast("gold-shield", hero.findByType("paladin")[0]);
while (true) {
let enemy;
enemy = hero.findNearest(hero.findByType("catapult"));
if (!(enemy)) enemy = hero.findNearest(hero.findEnemiesInSight());
if (enemy && hero.isPathClear(hero.pos, enemy.pos)) {
if (hero.distanceTo(enemy) <= 3) hero.attack(enemy);
else hero.move(enemy.pos);
}
if (hero.findEnemiesInSight().length === 0) hero.move({x: 78, y: 14});
for (let friend of hero.findFriends()) {
if (friend.findNearestEnemy() && friend.type == "soldier") {
hero.command(friend, "defend", hero.findByType("archer")[hero.findByType("soldier").indexOf(friend)]);
continue;
}
let friendEnemy;
if (friend.type == "paladin" && friend.canCast("heal")) {
let healTarget = hero.findLowestFriend();
if (healTarget.health < healTarget.maxHealth) {
hero.command(friend, "cast", "heal", healTarget);
continue;
}
}
if (friend.type == "paladin") friendEnemy = friend.findNearest(hero.findByType("witch"));
if (!(friendEnemy)) friendEnemy = friend.findNearest(hero.findByType("skeleton"));
if (!(friendEnemy)) friendEnemy = friend.findNearest(hero.findByType("ogre"));
if (friendEnemy) hero.command(friend, "attack", friendEnemy);
else {
if (friend.pos.x < 43) hero.command(friend, "move", {x: 43, y: 59});
else if (friend.pos.x < 50) hero.command(friend, "move", {x: 50, y: 58});
else if (friend.pos.y > 39 && friend.pos.x <= 55) hero.command(friend, "move", {x: 50, y: 39});
else if (friend.pos.x < 78) hero.command(friend, "move", {x: 78, y: 40});
}
}
}