Hi. I would really appreciate some help with my code. I can’t get my paladins to attack nor shield. I don’t get any coding errors, but I’m pretty sure I’ve misplaced something.
Here is my code:
// Your goal is to protect Reynaldo
// Find the paladin with the lowest health.
this.lowestHealthPaladin = function() {
var lowestHealth = 99999;
var lowestFriend = null;
var friends = this.findFriends();
for(var f=0; f < friends.length; f++) {
var friend = friends[f];
if(friend.type != "paladin") { continue; }
if(friend.health < lowestHealth && friend.health < friend.maxHealth) {
lowestHealth = friend.health;
lowestFriend = friend;
}
}
return lowestFriend;
};
this.commandPaladin = function(paladin) {
// Heal the paladin with the lowest health using lowestHealthPaladin()
// You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
// Paladins can also shield: command(paladin, "shield")
var lowestPaladin = this.lowestHealthPaladin();
var enemy = paladin.findNearestEnemy();
if (lowestPaladin && paladin.canCast("heal")) {
this.command(paladin, "cast", "heal", lowPal);
}
else if (paladin.health < paladin.maxHealth /3) {
this.command(paladin, "shield");
}
else if (enemy) {
this.command(paladin, "attack", enemy);
}
};
this.commandPeasant = function(friend) {
friends = this.findByType("peasant");
for (var i=0; i < friends.length; i++) {
friend = friends[i];
coins = this.findItems();
nearestCoin = friend.findNearest(coins);
if (i === 0 && nearestCoin.pos.y <= 40){
this.command(friend, "move", nearestCoin.pos);
}
else if (i === 1 && nearestCoin.pos.y > 40){
this.command(friend, "move", nearestCoin.pos);
}
}
};
this.commandGriffin = function(friend) {
var enemy = friend.findNearestEnemy();
if (enemy) {
this.command(friend, "attack", enemy);
}
};
this.commandFriends = function() {
// Command your friends.
var friends = this.findFriends();
for(var i=0; i < friends.length; i++) {
var friend = friends[i];
if (friend.type == "peasant") {
this.commandPeasant(friend);
} else if (friend.type == "griffin-rider") {
this.commandGriffin(friend);
} else if (friend.type == "paladin") {
this.commandPaladin(friend);
}
}
};
loop {
this.commandFriends();
// Summon griffin riders!
if (this.gold > this.costOf("griffin-rider")){
this.summon("griffin-rider");
}
// Move Paladins forward to avoid the enemy missile blowback into mine field
var friends = this.findFriends();
for (var i = 0; i < friends.length; i++) {
var friend = friends[i];
if (friend.type == "paladin"){
var friendPOSx = 80;
var friendPOSy = friend.pos.y;
var friendPOS = {'x':friendPOSx,'y':friendPOSy};
this.command(friend, "move", friendPOS );
}
}
}