Help! Misty Island Mine (Solved)

This is my code.

def findBestItem(friend, excludedItems):
    items = friend.findItems()
    bestItem = None
    bestItemValue = 0
    for item in items:
        
        if item not in excludedItems:
            if bestItemValue < item.value / friend.distanceTo(item):
                bestItemValue = item.value / friend.distanceTo(item)
                bestItem = item
                
    return bestItem


while True:
    peasants = hero.findByType("peasant")
    
    claimedItems = []
    for peasant in peasants:
        enemy = peasant.findNearestEnemy()
        if enemy:
            
            if hero.gold > hero.costOf('decoy'):
                
                hero.command(peasant, 'buildXY', 'decoy', peasant.pos.x, peasant.pos.y)
                
                continue
            pass
        item = findBestItem(peasant, claimedItems)
        if item:
            
            claimedItems.append(item)
            
            hero.command(peasant, 'move', item.pos)`

But it tells me that my code is really slow or never finished.
Please Help!

Read the hints. They say to command the peasants to build a decoy when an enemy is targeting the peasant and to build the decoy at the peasant’s x position - 2.

1 Like

I tried what you said to do but all of my peasants die.

def findBestItem(friend, excludedItems):
    items = friend.findItems()
    bestItem = None
    bestItemValue = 0
    for item in items:
        
        if item not in excludedItems:
            if bestItemValue < item.value / friend.distanceTo(item):
                bestItemValue = item.value / friend.distanceTo(item)
                bestItem = item
                
    return bestItem


while True:
    peasants = hero.findByType("peasant")
    
    claimedItems = []
    for peasant in peasants:
        enemy = peasant.findNearestEnemy()
        if enemy:
            
            if hero.gold > hero.costOf('decoy'):
                hero.command(peasant, 'buildXY', 'decoy', peasant.pos.x - 2, peasant.pos.y)
                
                continue
            pass
        item = findBestItem(peasant, claimedItems)
        if item:
            
            claimedItems.append(item)
            
            hero.command(peasant, 'move', item.pos)
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("throw"):
            hero.throw(enemy)

This is my newest code, but the peasants still die!! Please HELP!

You forgot about this. (the function is: unit.target. This returns the unit’s current attack target).
Danny

2 Likes
# Use in to check if an element is IN the excludedItems array.

Check if an element is INSIDE the excluded items array

1 Like

Put this in an if statement

1 Like

What do you mean, I don’t understand.

I’m not sure exactly what @FalconX11_312 means, your code worked for me when you put in enemy.target.
Danny

1 Like

Too much indentation.

Elijah the Prophet

1 Like

Sorry. I meant put it outside of the if statement if item not in excludedItems.

1 Like

That’s only because of the quote.
Danny

1 Like

I’m afraid I still don’t understand what you mean :worried:

Have you followed the advice I gave you? If so it should work… Please post your current code.
Danny

1 Like
def findBestItem(friend, excludedItems):
    items = friend.findItems()
    bestItem = None
    bestItemValue = 0
    for item in items:
        
        if item not in excludedItems:
            if bestItemValue < item.value / friend.distanceTo(item):
                bestItemValue = item.value / friend.distanceTo(item)
                bestItem = item
                
    return bestItem


while True:
    peasants = hero.findByType("peasant")
    
    claimedItems = []
    for peasant in peasants:
        enemy = peasant.findNearestEnemy()
        if enemy:
            
            if hero.gold > hero.costOf('decoy'):
                
                hero.command(peasant, 'buildXY', 'decoy', peasant.pos.x, peasant.pos.y)
                
                continue
            pass
        item = findBestItem(peasant, claimedItems)
        if item:
            
            claimedItems.append(item)
            
            hero.command(peasant, 'move', item.pos)

The peasants build decoys but the orges go for the peasants instead of the decoys.

Again, only build a decoy if the enemy is targeting the peasant.

if enemy.target == peasant:

Also,

should be

hero.command(peasant, 'buildXY', 'decoy', peasant.pos.x-2, peasant.pos.y)
4 Likes

Ok :slight_smile: thank you for your help!!

2 Likes

Thank you so much, it worked!!

2 Likes

Congrats on completing the level :partying_face:

3 Likes

Thank you!! :partying_face: :partying_face:

2 Likes