Why is that code realy slow?
function lowestHealthPaladin() {
var lowestHealth = Infinity;
var lowestFriend = null;
var friends = hero.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;
}
function commandPaladin(paladin) {
var enemy = paladin.findNearestEnemy();
var strongestEnemy = findStrongestEnemy();
var warlock = hero.findByType("warlock")[0];
// Полечи паладина с низким количеством здоровья используя lowestHealthPaladin()
if (lowestHealthPaladin()){
if (paladin.canCast("heal")) {
hero.command(paladin, "cast", "heal", lowestHealthPaladin());
}
}
//if (warlock){
// hero.command(paladin, "attack", warlock);
// } else
if (strongestEnemy && strongestEnemy.health>15) {
hero.command(paladin, "attack", strongestEnemy);
} else if (enemy) hero.command(paladin, "attack", enemy);
}
function findStrongestEnemy(){
var enemies = hero.findEnemies();
var maxHealth = -Infinity;
var strongestEnemy = null;
for (var i = 0; i< enemies.length; i++){
var enemy = enemies[i];
if (maxHealth<enemy.health) {
maxHealth = enemy.health;
strongestEnemy = enemy;
}
}
return strongestEnemy;
}
function findBestItem(items){
var bestItem = null;
var bestItemValue = -Infinity;
for (var i = 0; i<items.length; i++){
var item = items[i];
if (excludedItems.length === 0 || excludedItems.indexOf(item)>-1){continue;}
if (item.value/peasant.distanceTo(item)>bestItemValue){
bestItem = item;
bestItemValue = item.value/peasant.distanceTo(item);
}
}
return bestItem;
}
function commandPeasant(peasant){
var items = hero.findItems();
var bestItem = findBestItem(items);
var item = peasant.findNearestItem();
if (bestItem){
excludedItems.push(bestItem);
hero.command(peasant, "move", bestItem.pos);
} else
if (item){
excludedItems.push(item);
hero.command(peasant, "move", item.pos);
}
}
function commandGriffin(griffin){
var warlock = hero.findByType("warlock")[0];
var enemy = griffin.findNearestEnemy();
var strongestEnemy = findStrongestEnemy();
//
if (strongestEnemy && strongestEnemy.health>15) {
hero.command(griffin, "attack", strongestEnemy);
} else if (warlock){
hero.command(griffin, "attack", warlock);
}
else if (enemy) hero.command(griffin, "attack", enemy);
}
function commandFriends() {
// Командуй своими союзниками.
var friends = hero.findFriends();
for(var i=0; i < friends.length; i++) {
var friend = friends[i];
if(friend.type == "peasant") {
commandPeasant(friend);
} else if(friend.type == "griffin-rider") {
commandGriffin(friend);
} else if(friend.type == "paladin") {
commandPaladin(friend);
}
}
}
var excludedItems = [];
var peasants = hero.findByType("peasant");
while(true) {
excludedItems.length === 0;
if (hero.gold >= hero.costOf("griffin-rider")) {
hero.summon("griffin-rider");
}
commandFriends();
}