# 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.
def findBestItem(friend, excludedItems):
items = friend.findItems()
bestItem = None
bestItemValue = 0
for item in items:
# Use in to check if an element is in the excludedItems array.
# In that case, skip over that item as another peasant is targeting it.
if item in excludedItems:
continue
# Finish the function!
# Remember bestItemValue should be the highest item.value / distanceTo
value = item.value / hero.distanceTo(item)
if value > bestItemValue:
bestItem = item
bestItemValue = value
return bestItem
# This function checks if you have enough gold for a decoy.
def enoughGoldForDecoy():
return hero.gold >= 25
while True:
peasants = hero.findByType("peasant")
# Create a new array every loop.
claimedItems = []
for peasant in peasants:
enemy = peasant.findNearestEnemy()
if enemy and enemy.target == peasant and enoughGoldForDecoy() and peasant.distanceTo(enemy):
hero.command(peasant, "buildXY", "decoy",peasant.pos.x + 2, peasant.pos.y)
item = findBestItem(peasant, claimedItems)
if item:
# After an item is claimed, stick it in the claimedItems array.
claimedItems.append(item)
# Command the peasant to collect the coin:
hero.command(peasant, "move", item.pos)
It doesn’t give me the infinite loop error when i submit it, but the peasants aren’t summoning decoys because the distanceTo condition doesn’t have a value.
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.
def findBestItem(friend, excludedItems):
items = friend.findItems()
bestItem = None
bestItemValue = 0
for item in items:
# Use in to check if an element is in the excludedItems array.
# In that case, skip over that item as another peasant is targeting it.
if item in excludedItems:
continue
# Finish the function!
# Remember bestItemValue should be the highest item.value / distanceTo
value = item.value / hero.distanceTo(item)
if value > bestItemValue:
bestItem = item
bestItemValue = value
return bestItem
# This function checks if you have enough gold for a decoy.
def enoughGoldForDecoy():
return hero.gold >= 25
while True:
peasants = hero.findByType("peasant")
# Create a new array every loop.
claimedItems = []
for peasant in peasants:
enemy = peasant.findNearestEnemy()
if enemy and enemy.target == peasant and enoughGoldForDecoy() and peasant.distanceTo(enemy) < 10:
hero.command(peasant, "buildXY", "decoy",peasant.pos.x + 2, peasant.pos.y)
item = findBestItem(peasant, claimedItems)
if item:
# After an item is claimed, stick it in the claimedItems array.
claimedItems.append(item)
# Command the peasant to collect the coin:
hero.command(peasant, "move", item.pos)
# 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.
def findBestItem(friend, excludedItems):
items = friend.findItems()
bestItem = None
bestItemValue = 0
for item in items:
# Use in to check if an element is in the excludedItems array.
# In that case, skip over that item as another peasant is targeting it.
if item in excludedItems:
continue
# Finish the function!
# Remember bestItemValue should be the highest item.value / distanceTo
value = item.value / hero.distanceTo(item)
if value > bestItemValue:
bestItem = item
bestItemValue = value
return bestItem
# This function checks if you have enough gold for a decoy.
def enoughGoldForDecoy():
return hero.gold >= 25
while True:
peasants = hero.findByType("peasant")
# Create a new array every loop.
claimedItems = []
for peasant in peasants:
enemy = peasant.findNearestEnemy()
item = findBestItem(peasant, claimedItems)
if enemy and enemy.target == peasant and enoughGoldForDecoy() and peasant.distanceTo(enemy) < 10:
hero.command(peasant, "buildXY", "decoy",peasant.pos.x + 2, peasant.pos.y)
elif item:
# After an item is claimed, stick it in the claimedItems array.
claimedItems.append(item)
# Command the peasant to collect the coin:
hero.command(peasant, "move", item.pos)
it gives me the error "code never finished. it’s eaither realty slow or has an infinity loop
That’s weird. It works for me but is pretty laggy. Maybe change out the function enoughGoldForDecoy with hero.gold >= 25. It definitely made it lag less. Also in your findBestItem it should use the friend’s distance to the item not the hero’s.