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.

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

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

Check if an element is INSIDE the excluded items array

Put this in an if statement

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

Too much indentation.

Elijah the Prophet

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

That’s only because of the quote.
Danny

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

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)

Ok :slight_smile: thank you for your help!!

Thank you so much, it worked!!

Congrats on completing the level :partying_face:

Thank you!! :partying_face: :partying_face: