My peasants are killed before they can build even one decoy.
function findBestItem(friend, excludedItems) {
var items = friend.findItems();
var bestItem = null;
var bestItemValue = 0;
for (let item of items) {
var idx = excludedItems.indexOf(item);
if (idx != -1) {
continue;
}
item.score = item.value / hero.distanceTo(item);
if (item.score > bestItemValue) {
bestItemValue = item.score;
bestItem = item;
}
}
return bestItem;
}
function enoughGoldForDecoy() {
return hero.gold >= 25;
}
while (true) {
var peasants = hero.findByType("peasant");
var claimedItems = [];
for (let peasant of peasants) {
var enemy = peasant.findNearestEnemy();
if (enemy && enemy.target == peasant && enoughGoldForDecoy()) {
hero.command(peasant, "buildXY", "decoy", peasant.pos.x - 2, peasant.pos.y);
continue;
}
var item = findBestItem(peasant, claimedItems);
if (item) {
claimedItems.push(item);
hero.command(peasant, "move", item.pos);
}
}
}