I wrote code for Sarven Treasure. It worked OK, but I decided I want my character to check for enemy proximity WHILE he is walking towards a coin, rather than before/after. Therefore I switched self.moveXY(coin.pos.x, coin.pos.y) to self.move({“x”: coin.pos.x, “y”:coin.pos.y}). Now, it works, he will be walking towards a coin and then suddenly walk away if the ogre gets to close! Perfect! Except instead of walking away, he takes a step back, a step forward, repeat, and oscillates between them. I don’t understand why, if the ogre is getting closer it should keep telling him to run away. Can anyone tell me why it is not working?
# I die in 1 hit 0.o
loop:
minDistance = 20
coinIndex = 0
coins = self.findItems()
ogres = self.findEnemies()
ogre = self.findNearest(ogres)
bestCoin = None
bestRating = 0
#Cycle through my array of coins and choose the best one based on coin value and it's dstance from me. Give it the name "bestCoin" and a rating of "bestRating"
while coinIndex < len(coins):
currentCoin = coins[coinIndex]
coinIndex += 1
coinDistance = self.distanceTo(currentCoin)
currentRating = 100*currentCoin.value / coinDistance
if currentRating > bestRating:
bestRating = currentRating
bestCoin = currentCoin
# If no ogres, get coins.
if not ogre and bestCoin:
self.moveXY(bestCoin.pos.x, bestCoin.pos.y)
# If ogre, decide what to do:
elif ogre:
ogreDistance = self.distanceTo(ogre)
# Compare the ogre distance with the rating of the coin. If it's worth it, grab it. Raise the number to be more risky. At 1.2 I grab about 120 coins and at 1.3 about 130 coins. Any higher and the ogres kill me.
if ogreDistance / bestRating > 1.3 :
self.move({"x": bestCoin.pos.x, "y": bestCoin.pos.y})
# Otherwise, run to a teleporter.
else:
if self.pos.x > 43 and self.pos.y > 38:
self.moveXY(75, 50)
elif self.pos.x > 43 and self.pos.y <= 38:
self.moveXY(75, 20)
elif self.pos.x <= 43 and self.pos.y > 38:
self.moveXY(5, 50)
elif self.pos.x <= 43 and self.pos.y <= 38:
self.moveXY(5, 20)
bestCoin = None
pass
# To debug, I yell out.
else:
self.say("no coin!")