The Trial - Can't stop my infinite loop [Python]

I have been at it for hours, it sort of works, but lags and sometimes crashes with infinite loop error.

# This level is intended to be for advanced players. The solution should be pretty complex with a lot of moving parts. It might also be a bit of a gear check unless you use "creative" methods.
# You need to make your way to the first trial (Oasis of Marr) killing enemies along the way. When you reach it, pick all the mushrooms to trigger the trial to begin. If you survive the onslaught, make your way to the next Oasis for the second trial, then the Temple. When all trials are complete you will have to face the final boss. Good luck!
# HINT: Glasses with a high visual range help tremendously on this level so buy the best you can get.
# HINT: the unit 'type' for the oasis guardians is 'oasis-guardian'
loop:
    closestEnemy = None
    minDistance = 150
    enemyIndex = 0
    enemies = self.findEnemies()
    mushrooms = self.findItems()
    mushroom = self.findNearest(mushrooms)
    while enemyIndex < len(enemies):
        currentEnemy = enemies[enemyIndex]
        enemyIndex += 1
        currentDistance = self.distanceTo(currentEnemy)
        if currentDistance < minDistance and self.isPathClear(self.pos, currentEnemy):
            minDistance = currentDistance
            closestEnemy = currentEnemy
        else:
            pass
    if closestEnemy:
        while closestEnemy.health > 0:
            if closestEnemy.health > 150:
                if self.isReady("power-up"):
                    self.powerUp()
                elif self.isReady("bash"):
                    self.bash(closestEnemy)
                else:
                    self.attack(closestEnemy)
            else:
                self.attack(closestEnemy)

    elif mushroom:
        self.move(mushroom.pos)
    elif self.pos.y < 40:
        self.move({"x": 10, "y":10})
    elif self.pos.y > 100:
        if self.pos.x > 90:
            self.move({"x": 75, "y":125})
        if self.pos.x < 40:
            self.moveXY(100, 125) 
        if self.pos.x > 50 and self.pos.x <= 90:
            newEnemy = self.findNearest(self.findEnemies())
            if not newEnemy:
                self.move({"x": 55, "y":115})
                self.moveXY(27, 121)
                self.moveXY(56, 122)
                self.moveXY(45, 96)
                self.move({"x": 45, "y":70})
                self.moveXY(80, 70)
            else:
                pass

Can you comment in and out parts of your code until you find the part that is infinitely looping?

OK, copied and pasted your code.

(my take on what the code is doing) After finishing #1, you clean up the enemies and the mushrooms for #2, then you visit two and four over and over dragging the enemies with you to fight them in the #four area. (# three isn’t on the circuit and I’ve already cleaned the enemies (but will never stop to pick the 'shrooms))
(what glasses are you using, maybe I can see too far? or is it not far enough…)

I don’t get any “inf-loop” or any javascript console errors (after it starts, and only the usual before “running”), my hero just stops after killing the first load of followers into area4.

I’m at 61,74. I can’t see any enemies, no mushrooms, so it seems like ==> the while shouldn’t happen, and the if/elif should take me to . . . . nothing . . . there is no case for y == 74 you have “elif y < 40” and “elif y > 100” but no “else:”

so I get stuck in the phantom else: of the if closestEnemy:

The last moveXY of the “look for enemies” walk leaves me at “self.moveXY(80, 70)” which is in no-y-mans land.

I just downgraded to the wooden glases (I don’t own the ones in between wooden and enchanted) and made the needed changes to use those glasses and I still end up in area 4 with the no-else problem. (I kill more stuff but not enough, I got boss2 this time but there is a little group of shaman-scout-2throwers right below #2 that never move)

(I also don’t own the see through walls glasses so I can’t test what happens when you can see everything.)

1 Like

Thanks for the reply! My code definitely has an infinite loop. I’m going to rewrite it now and try to solve some problems. Also, I have the see through wall glasses, if that matters.

See through walls matters. It means you have more things to process. Also you never have the “dead spots” I do.

Dead spots: When I end up in those areas not covered by your if statements (x,y of 61,74) I get stuck but you can see enemies/mushrooms so don’t get stuck.

More to process: every time through the loop (iow: every “move”) your code processes every enemy & mushroom on the map. (I know nick is looking into why this can cause such problems <enter needling mode> but you know he probably has a super cool souped up machine so never gets the problem (see his awesome video of Sarven Treasure for proof (not only was he running with all that on the screen but he was recording it as well))<end needling mode>.) :wink:

(iow: time based “slow-code”/inf-loop is machine dependant. If I get 100 flops and he gets 1000 flops, well… I am running a 64bit 3.6Ghz (4 core 8gb) machine and I get bog/lag-ging during execution.) (My favorite is when it whines about the slow code and still finishes. Again with the Sarven Treasure example, I believe that it would be impossible for my machine to loop through the ogres, coins, AND the missles every “move” without SERIOUS bog/lag (I already get it just doing ogre&coins, and I use moveXY)) (Hmm, I don’t think it should really matter but I can try looping through ogres and coins (and missiles) together (one loop) instead of during them separately (2(3) loops).)

I would love it if I could set enemies to only be enemies within a certain distance, so that I didn’t have to cycle through every enemy on the map every loop.

hmm, a distance parameter on the Infinite distance glasses… :smiley:

Yes!!! I would love to trade my infinite glasses in for limited glasses! I wish I knew a code to search for all _____ WITHIN a certain distance.

That is easy /at least in Python):

enemies = [e for e in self.findEnemies() if self.distanceTo(e) < 20]

Obviously you should replace 20 by the desired value.

That is the “pythonic” way.

My question is does it create better javascript code than doing it “by hand”? (Since it is my understanding that everything “passes through” javascript on its way to being run.)

enemies = []
es = self.findEnemies()
index = 0
while index < len(es):
    e = es[index]
    index += 1
    if self.distanceTo(e) < 20:
        enemies.append(e)

(I believe that should be right but, I just threw it out with any tests, so…)

it says tmp61[tmp62]is not a function


# This level is intended to be for advanced players. The solution should be pretty complex with a lot of moving parts. It might also be a bit of a gear check unless you use "creative" methods.
# You need to make your way to the first trial (Oasis of Marr) killing enemies along the way. When you reach it, pick all the mushrooms to trigger the trial to begin. If you survive the onslaught, make your way to the next Oasis for the second trial, then the Temple. When all trials are complete you will have to face the final boss. Good luck!
# HINT: Glasses with a high visual range help tremendously on this level so buy the best you can get.
# HINT: the unit 'type' for the oasis guardians is 'oasis-guardian'
loop:
    closestEnemy = None
    minDistance = 150
    enemyIndex = 0
    enemies = self.findEnemies()
    mushrooms = self.findItems()
    mushroom = self.findNearest(mushrooms)
    while enemyIndex < len(enemies):
        currentEnemy = enemies[enemyIndex]
        enemyIndex += 1
        currentDistance = self.distanceTo(currentEnemy)
        if currentDistance < minDistance and self.isPathClear(self.pos, currentEnemy):
            minDistance = currentDistance
            closestEnemy = currentEnemy
        else:
            pass
    if closestEnemy:
        while closestEnemy.health > 0:
            if closestEnemy.health > 150:
                if self.isReady("power-up"):
                    self.powerUp()
                elif self.isReady("bash"):
                    self.bash(closestEnemy)
                else:
                    self.attack(closestEnemy)
            else:
                self.attack(closestEnemy)

    elif mushroom:
        self.move(mushroom.pos)
    elif self.pos.y < 40:
        self.move({"x": 10, "y":10})
    elif self.pos.y > 100:
        if self.pos.x > 90:
            self.move({"x": 75, "y":125})
        if self.pos.x < 40:
            self.moveXY(100, 125) 
        if self.pos.x > 50 and self.pos.x <= 90:
            newEnemy = self.findNearest(self.findEnemies())
            if not newEnemy:
                self.move({"x": 55, "y":115})
                self.moveXY(27, 121)
                self.moveXY(56, 122)
                self.moveXY(45, 96)
                self.move({"x": 45, "y":70})
                self.moveXY(80, 70)
            else:
                pass

Where? It usually means that you do not have an Item that does that function.

sorry ive been so late to reply i had the wrong glasses so i bought them it now works but my hero wont collect the mushrooms and when he’s done attacking some enemies he doesnt attack the rest or the oracle all he does is moves to the xy cordniates

Your indentation is wrong. Try indenting the move line once more.

Just so you know, the whole while-loop could be replaced with “closestEnemy = self.findNearest(enemies)”.

Starting with [quote=“lundor5, post:11, topic:2984”]
elif self.pos.y < 40:
[/quote], you are adding elif-statements after a loop. This does not make sense. Try correcting your indentation.

Best wishes!