Misty Island Mine

hi quick question in javascript misty island mine at the hero.gold >= hero.costOf(‘decoy’) why it say
i only can summon soldier and archer how do i fix this?

// If the peasant is the target of the enemy
            // AND the hero has enough gold for a decoy
            if (enemy.target == peasant && hero.gold >= hero.costOf("decoy")) {
                // Command a peasant to build a "decoy":
                hero.command(peasant, "buildXY", peasant.pos.x, peasant.pos.y);
                // Add a continue so the peasant doesn't collect coins when building.
                continue;

but i want to summon decoy

hero.gold >= hero.costOf('decoy')

error at this part 20 char

comment say

that mean i need to use hero.costOf right?

you never mentioned the decoy in here, you just gave in the command of building and the position of where to build, not what to build

4 Likes

I’m pretty sure you need to equip the stone hammer to build a decoy. I don’t think there is any other way of summoning a decoy. Since the decoy comes from the stone hammer. The boss star doesn’t work on it. Altho im pretty sure the cost is 60? Here’s a snippet:

if hero.gold >= **:
    hero.buildXY("apples", hero.pos.x, hero.pos.y)
FYI

Remember. It comes from the stone hammer. hero.summon will not work

1 Like

so i don’t need to

20 chars

still dosent work 2002022

// Collect gold efficiently by commanding peasants wisely!
// Peasants should collect coins and build decoys.

// The function should return the best item per target
// Use an array of ids to ensure no two peasants target the same item.
function findBestItem(friend, excludedItems) {
    var items = friend.findItems();
    var bestItem = null;
    var bestItemValue = 0;
    for(var i = 0; i < items.length; i++) {
        var item = items[i];
        // indexOf searches and array for a certain element:
        var idx = excludedItems.indexOf(item);
        // If the array doesn't contain it, it returns -1
        // In that case, skip over that item as another peasant is targeting it.
        if(idx != -1) { continue; }
        // Finish the function!
        // Remember bestItemValue should be the highest item.value / distanceTo
        bestItemValue = bestItemValue > item.value / hero.distanceTo(item);
        bestItem = item;
    }
    return bestItem;
}

// This function checks if you have enough gold for a decoy.
function enoughGoldForDecoy() {
    return hero.gold >= 25;
}

while(true) {
    var peasants = hero.findByType("peasant");
    // Create a new array every loop.
    var claimedItems = [];
    for(var i = 0; i < peasants.length; i++) {
        var peasant = peasants[i];
        var enemy = peasant.findNearestEnemy();
        if(enemy) {
            // If the peasant is the target of the enemy
            // AND the hero has enough gold for a decoy
            if (enemy.target == peasant) {
                // Command a peasant to build a "decoy":
                hero.command(peasant, "buildXY", "decoy", peasant.pos.x, peasant.pos.y);
                // Add a continue so the peasant doesn't collect coins when building.
                continue;
            }
        }
        var item = findBestItem(peasant, claimedItems);
        if(item) {
            // After an item is claimed, stick it in the claimedItems array.
            claimedItems.push(item);
            // Command the peasant to collect the coin:
            hero.command(peasant, "move", item.pos);
        }
    }
}

@Aya @WaWa_Yang ? 20 char

1 Like

how to enumerate in javascript if in python they say for i, enumerate "stmhing"

I think there are several problems

now im not sure about js, but just to avoid problems, separate those
so make an if-statement checking if bestItemValue < item.value / hero.distanceTo(item) then adjust bestItemValue and bestItem variables

Again im not sure about js, but try making it an if-statement
if the gold is greater than 25, return true for example

you didnt make use of that comment, use the function above to do what is required in the comments

I suggest you put this in the last parameter: {"x":item.pos.x, "y":item.pos.y}

3 Likes