Is there a better way to make peasants target different coins?

There is a simple solution for this: make your peasants work in “zones”. I tried multiple approaches:

a) Simpler version: set a limit for your peasants and if the coin is behind this limit, skip it. Sample python code:

nextCoin = peasant.findNearestItem()
# if coin is above the middle of the field
if nextCoin.pos.y > middle:
    # ...pick up the coin
    self.command(peasant, "move", nextCoin.pos)
# ...otherwise move away from the middle
else:
    self.command(peasant, "move", {"x": peasant.pos.x, "y": peasant.pos.y+2})

b) More elegant version: create separate coinlists for the zones and pick your next coin from the appropriate list:

coins = self.findItems()
coins1 = [] # upper zone
coins2 = [] # lower zone

for coin in coins:
    if coin.pos.y > middle:
        coins1.append(coin)
    else:
        coins2.append(coin)

nextCoin = peasant.findNearest(coins1) # or (coins2)

Of course, you can make these much more elaborate, these are just basic ideas.

Finding the optimal next coin can also be improved: you can make a weighted list of coins (e.g. based on value and distance) or calculate the “center” of the coins and head towards that, etc. You can find such topics here on the forum:
http://discourse.codecombat.com/t/help-on-improving-code-looting-coins/3359
http://discourse.codecombat.com/t/vectors-facingtowards-and-looting-coins/3330

2 Likes