Question about self.move [Python]


#1

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!")

#2

First thing I see is that you still have a moveXY in your “no Ogres get coin” (this makes the assumption that the ogre doesn’t spawn “between” you).

And since you only have “three” move/moveXY statements and “two” of those are moveXY.
As soon as he actually decides to “bolt” as it were he should bolt (moveXY) all the way to the telepoter.
Therefore he can’t be “bolting”, I would think that the only thing left to cause oscillating would be a change to best coin.
(As ogre & bestCoin is the only thing that gets him a “move”.)

(You can “move(bestCoin.pos)” it is only if you don’t have an object that you have to create one with “{“x”:#,“y”:#}”. I think you can even do “move(bestCoin)” but not as sure about that one.)


#3

Thanks! I think I understand, as I walk away from the ogre + coin, the coin’s value goes up faster than the ogres threat, so I walk back towards the coin. I can change it! But I don’t see where my infinite loop is that keep crashing.


#4

It says, “Code never finished, infinite loop” in a red error, or it says “Hard execution limit of 1000000 exceeded” in a red error, or the tab just crashes?


#5

It’s very laggy and sometimes errors with “Code never finished, infinite loop” an sometimes doesn’t error. It actually successfully beats the mission for some seeds, but lags a lot.


#6

Right, so the code is just really slow as there gets to be a lot of coins. I need to optimize things so that code which iterates over every coin, every frame, isn’t so slow.