Level Sand Snakes

The path is not safe is we use move, because the hero turn sooner than expected. MoveXY is ok.

1 Like

I would assume most people doing the campaign wouldn’t have bought the compound boots until the Mountain Campaign, so I’m pretty sure most players would use moveXY().

Although while loops are taught in the desert campaign, so this works:

while (best && this.distanceTo(best)) this.move(best.pos);

(Though maybe a slightly greater distance, like distance < 1 might work better when it comes to time.)

1 Like

Just because most players would be using moveXY() does not mean that the level should only work for moveXY(). In general, I don’t think a level should fail just because a player has a higher level item.

1 Like

Yeah, I know, but move() behaves quite interestingly.

According to the description:

The move method sets the hero’s targetPos to the given (x, y) coordinates and also sets the hero’s action to move for one “step”.

Basically at a turn, once the hero picks up a coin (I believe the hero has a collect range of 2 meters), depending on what method the hero uses, the hero will continue to it’s target position or find a new one.

If the hero is using move(), the loop immediate continues to its next iteration, and the hero instantly changes their target position, which here will make them collide with the fire traps.

moveXY() moves all the way to the target position before the loop can continue, so their next target position will be the next coin in the path.

Which is why if one wants to use move(), they’ll have to use a while loop to check if their distance to their target position is near zero before continuing the loop. (See my post above for an example.)

I don’t see how this is limiting the player.

1 Like

Since the higher level boots also have moveXY on them, hopefully players will know to just switch the method (which is what I had to do). Might be good if in the description something was added about needing to use moveXY or at least about being careful not to cut corners.

2 Likes

moveXY didn’t work for me. My hero sits still for a moment, then turns right into the nearest fire trap. This happens without fail. Here’s my code:

# This field is covered in firetraps.  Thankfully we've sent a scout ahead to find a path.  He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.

# This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.

loop:
    coins = self.findItems()
    coinIndex = 0
    nearest = None
    nearestDistance = 9999
    
    # Loop through all the coins to find the nearest one.
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        distance = self.distanceTo(coin)
        
        # If this coin's distance is less than the nearestDistance
            # Set nearest to coin
            # Set nearestDistance to distance
        if distance < nearestDistance:
            nearest = coin
            nearestDistance = distance
        if nearest:
            self.moveXY(nearest.pos.x, nearest.pos.y)

Can you show me where I went wrong?

1 Like

So, do you collect any coins at all? Or from the very beginning you head for a mine? I don’t see anything wrong with your code as it stands. Looks to be exactly the same as my working code. What hero? Can you add a screenshot?

1 Like

The code editor says that I am moving to `nearest.pos.x, nearest.pos.y’, but there’s really no coin where my hero is standing right now.

EDIT: Just saw this, but my hero, when she gets blown up, she is heading for one of the coins that is definitely not the nearest.

1 Like

It looks like your moveXY is inside the while-loop. Going to nearest when the loop isn’t finished yet might get you in some trouble I believe.

1 Like

Nice catch Bibliotek, totally missed that, ChronistGilver, you are trying to get the the current nearest before checking all the coins. Wait to move till you’ve looked at all coins.

1 Like

i coded a shortcut, going to the moon and back :

this.summon('soldier');
var flyingMan = this.findFriends()[0];
loop {
    if (flyingMan.health <= 0) break;
    this.command(flyingMan,'move',this.findItems()[0].pos);
}
this.say('paf, yer flying to the moon xD');
this.moveXY(74,53);
1 Like

Perhaps the boss stars should be restricted for this level for the above reason.
Edit: Maybe use the ignoresItemGold property to prevent the summoning of troops?

Also, it might be better for the devs to create threads for each new adventurer level as an official place to post feedback and issues during testing.

1 Like

ok same problem here so my question is how do you wait for the while loop to finish before you move

1 Like

just put the move after the loop like so:

i = 0;
while(i < 10){
    stuff
}

moveXY(10,99);
1 Like

I have this same problem. My adventurer always runs towards the same coin, which is most definitely not the closest by any means. This is the same coin as ChronisGilver’s. I have made sure that the moveXY is not within the while loop. What do I do?

loop:
    coins = self.findItems()
    coinIndex = 0
    nearest = None
    nearestDistance = 9999
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        distance = self.distanceTo(coin)
        if distance < nearestDistance:
            nearest = coins[coinIndex]
            nearestDistance = distance
    if nearest:        
        self.moveXY(nearest.pos.x,nearest.pos.y)

What on earth? Why isn’t my code working? I mean in the forums! I have enclosed it within 's, which are backticks. Well, anyway, it seems to be indented right in CodeCombat. The moveXY is out of the while loop and all. What's wrong with it? [edit - the backticks are 3 (```) and go on their own lines]

1 Like

You already got “coin” from “coins” why would you get a different one…

coin = coins[coinIndex]
coinIndex +=1

nearest = coins[coinIndex]

Use the one you got the distance to not a different one . . . nearest = coin

2 Likes

Hello, I have a strange bug in this level.
Here’s my code:

loop:
    coins = self.findItems()
    coinIndex = 0
    nearest = None
    nearestDistance = 9999
    
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        distance = self.distanceTo(coin)
        if distance < nearestDistance:
            nearest = coin
            nearestDistance = distance

    if nearest:
        if self.isPathClear(self.pos, nearest.pos):
            self.move(nearest.pos)

The problem is the hero (Anya or Tharin, I cheked both of them) doesn’t move exactly on nearest.position , but has some sort of inertia. So sometimes the nearest coin is not the safest one.
The only solution I’ve found is to add this code in the end:

        if self.pos != nearest.pos:
            self.moveXY(nearest.pos.x, nearest.pos.y)

So the hero needs twice go to nearest coin position. It works for me, but it doesn’t seem other players use it to solve this level.

1 Like

Hello, Linguini_Incident, and welcome. Please read the FAQ before you post again. This will teach you to “post your code with radiant, harmonious formatting”, that is, making sure your levels of indentation show up. I’ve done it for you this time, but please do so yourself in the future.

The comments in the default code specifically say

# If there's a nearest coin,...you'll need moveXY so you don't cut corners and hit a trap.

Using moveXY by itself should get you along fine.

1 Like

Hello, ChronistGilver, and thank you for help with formatting.
Of course, I’ve tried moveXY for the first, but the result was the same. After that I’ve bought a new shoes to try move and jumpTo but nothing changed.
But I think, I’ve found the problem - I used self.say(self.pos) after moving. When I don’t use it moveXY works fine.

1 Like

Yes, that would be your problem. self.say() keeps your momentum, even though you’ve stopped moving, and so you “slide” for a bit. Come to think of it, why wasn’t that in the code you posted earlier? Did you think it wasn’t important?

1 Like