How to pick up gold?


#1

Hi.
I am struggling in the multiplayer treasure grove mainly because my hero goes after bronze coins rather than going after the gold coins. Is there any way for my hero to give preference to the gold coins?

while True:
    item = hero.findNearestItem()
    if item:
        hero.moveXY(item.pos.x, item.pos.y)
    flag = hero.findFlag("green")
    if flag:
        hero.pickUpFlag(flag)

#2

Do

while True:
    item = hero.findNearestItem()
    if item:
        if item.type=='gold-coin':
            hero.moveXY(item.pos.x, item.pos.y)

Try that :wink:


#3

You can make your hero go for gold coins, or you can make your hero go for the best coins.
One way to make your hero go for the best coins is find all the coins, compare each coins value divided by your distance to them, and then move the the bestcoin.

while True:
    items = hero.findItems()
    bestItem = None
    bestValue = 0
    itemsIndex = 0
    # Loop over the items array.
    # Find the item with the highest valueOverDistance()
    while itemsIndex < len(items):
        item = items[itemsIndex]
        if item.value / hero.distanceTo(item) > bestValue:
            bestItem = item
            bestValue = item.value / hero.distanceTo(item)
        itemsIndex += 1
    if bestItem:
        hero.moveXY(bestItem.pos.x, bestItem.pos.y)
            

This does depend however on which programmaticon you have, but I find that this tactic works the best. You will come across this technique in the campaign, so if you don’t get it now, you will soon.
Hope this helps.


#4

I would just like to point out that this code wouldn’t work, as if the nearest coin isn’t a gold coin, then the hero wouldn’t do anything because they have no instructions for that eventuality.


#5

That is the code for only gold coins dude.


#6

Yeah this code doesn’t seem to work.
my hero just collects random coins even if there are gold coins.

while True:
    item = hero.findNearestItem()
    enemy = hero.findNearestEnemy()
    if enemy:
        ready = hero.isReady("cleave")
        if ready:
            hero.cleave(enemy)
        else:
            hero.attack(enemy)
    if item:
        if item.type=='gold-coin':
            hero.moveXY(item.pos.x, item.pos.y)
        else:
            hero.moveXY(item.pos.x, item.pos.y)

This is what I tried.


#7

I just gave you the standard code for Only Gold Coins