Sarven Treasure: what's wrong with this code?


#1

Here’s a simplified version of some code that doesn’t work well for me. This is on level 1, but I presume would have the same basic problem on other levels. Essentially what happens is that after I teleport, the hero thinks the nearest item is from pre-teleport location rather than post-teleport location.

But I have the item finding at the top of the loop and the move to the teleport location at the bottom of the loop. So shouldn’t the hero recalculate the nearest item AFTER teleporting? Or is something wrong with my logic? (probably!)

loop:

    item  = self.findNearest(self.findItems())
    if item:
        self.moveXY(item.pos.x,item.pos.y)

    if self.pos.x > 40:
        if self.pos.y > 40: 
            x = 76                
            y = 51
        else:               
            x = 76
            y = 19
    else:
        if self.pos.y > 40: 
            x =  5 
            y = 49
        else:
            x =  5
            y = 19

    self.moveXY(x,y)

#2

I started to have this problem last night.

The hero’s location is not being updated for some reason.


#3

Yeah, I mentioned in the “headhunter” thread that this seems to have started happening sometime yesterday. Code that I believe was substantially similar to this was working before that.

If no one sees a problem with this code then I can at least stop trying to fix something that is not wrong! But I’m also open to the fact that my code could be incorrect.


#4

No it is not your code. @nick

You can strip your code down to just two lines: going through a porter and then find nearest coin and it finds a coin by where you went in.


#5

I wonder if what is happening is that the sequence goes like this:

  1. go to teleport location
  2. go to top of loop
  3. find nearest item
  4. teleport happens

In other words, maybe there is a delay such that you don’t teleport instantly when you move to the teleport location? Without knowing the back end I can only guess what is happening. But there definitely seems to be some “irregularity” associated with the teleport location. Seems like sometimes the teleport is fast and other times the hero sits on the teleport for some amount of time before teleporting? Hard to tell what is happening when things are moving though…


#6
if item:
    self.moveXY(item.pos.x,item.pos.y)

if self.pos.x > 40:
    if self.pos.y > 40: 
        x = 76                
        y = 51

these two if’s do problems? both on same line, the lower should be an elif or?


#7

The code as written does what it is trying to do

if X > 40 then use a porter on right side of screen
…if y is high then top one else bottom one

if they were combined he would have to check if x> 40 more than once

although you are right that he can simplifiy it. Since, the top/bottom and right/left are really separate decisions:

if x > 40:
    x = 76
else:
    x = 5

if y > 40:
    y = 49
else:
    y = 5

#8

Should be fixed now; I changed it so that the teleport moves you and then lets you pick the next action.


#9

Originally I had the code symmetric (hence more concise) but it seems like the teleport locations are almost symmetric but not exactly (one Y is 49 and one is 51), so I coded to be as close to the middle of the “X” as possible, not knowing if it mattered or not.


#10

Thanks, Nick, big improvement.


#11

what is wrong with this code in this level?

coin = self.findNearestItem()
# Pick up the coin 
self.moveXY(coin.pos.x, coin.pos.y)

the error is
coin (red) undefined is not a function

I have tried to change into another name penny. gold, nikel, round, tttttttt,…
no success
the same code several levels before worked without a problem

but in this level if I use the code from the level where only gold coins are mandatory it seems to work - but is not my target now - that code use array and while to loop through items

I need to stress that I have the Mahogany Glasses


#12

If there are no coins (or none within range of your glasses) then coin will not exist and you need “if coin:” to avoid trying to move towards a non-existent coin. Computers are SO literal. :wink: Same issue with enemies.

On levels where there are lots of coins or enemies you can often get away with not using that code though even then it’s good practice to check for existence of coin/enemy before trying to do something related to them.


#13

aparently is not the case

I have reduced the code only to this

loop:
    coin = self.findNearestItem()
    if coin:
        # Pick up the coin
        self.moveXY(coin.pos.x, coin.pos.y)
    self.moveXY(x+5, y+5)

and the error is there at first indent into the

        coin = self.findNearestItem()

#14

check your boots and or glasses.

You either dont have access to the findNearestItem method or to moveXY


#15

I need to stress that I have the Mahogany Glasses

which has
self.distanceTo(target)
self.findEnemies()
self.findFriends()
self.findItems()
self.findNearest(units)


#16

You just answered your own question then.

You do not have findNearestItem()

the correct syntax is

self.findNearest(self.findItems())

#17

thanks that’s it

coin = self.findNearest(self.findItems())