Jump to content

Best way to generate a random path to a position?


7331337

Recommended Posts

Right now I think I'm doing it in a really hacky way and I'd hope there would be better ways than doing it.

 

Right now I'm basically doing;

int rand1 = random(0, 10) - 5;
int rand2 = random(0, 30) - 15;
Position randomPath = new Position((baseObject.getX() + rand1), (baseObject.getY() + rand2), 0);

if (map.canReach(randomPath)){
    localWalker.map.walk(randomPath);
}

This is for getting into view of the object itself so it can be interacted with later. Would there be better ways than doing this to get to a object not yet visible but I have the known coord's for?

Link to comment
Share on other sites

1) you are not generating a path, you are generating a position, a single position.

2) you are making the x and y positions have a variance, if you happen to generate a random position that is not walkable, then the if statement will not execute.

3) if the position is too far to walk to then it will do nothing. 

 

LocalWalker will not take you cross regions without a path unless the region is cached, then it might (depends on client implementation).

If you are trying to walk a path, you need to give it a path. This will walk from one single point (the player) to the sudo random position that you are generating within the same region.

 

If that is what you want to do then this will kind of work maybe sometimes. tongue.png

 

Edited by Mysteryy
  • Like 1
Link to comment
Share on other sites

1) you are not generating a path, you are generating a position, a single position.

2) you are making the x and y positions have a variance, if you happen to generate a random position that is not walkable, then the if statement will not execute.

3) if the position is too far to walk to then it will do nothing. 

 

LocalWalker will not take you cross regions without a path unless the region is cached, then it might (depends on client implementation).

If you are trying to walk a path, you need to give it a path. This will walk from one single point (the player) to the sudo random position that you are generating within the same region.

 

If that is what you want to do then this will kind of work maybe sometimes. tongue.png

 

Was a quick example not actually what I use >.> the random position is run in a while loop until it finds a correct one and the area this used is in its own sub-area of the map like minigames.

 

I'd like to however be able to fix your 3rd point out of it not working if its too far away which is what I want to resolve by generating a path to that position.

Link to comment
Share on other sites

Was a quick example not actually what I use >.> the random position is run in a while loop until it finds a correct one and the area this used is in its own sub-area of the map like minigames.

 

I'd like to however be able to fix your 3rd point out of it not working if its too far away which is what I want to resolve by generating a path to that position.

 

 

What you are asking goes far beyond a simple explanation. I would suggest doing what apaec said, you should start with defining some paths and pick between them. 

 

On a side note, if you have a while loop generating the random position until it gets one that is walkable, you may get stuck in an infinite loop where there is never a walkable tile. Generally speaking while loops arent a great thing to use in scripts. I try to avoid them most of the time, and when I do use them I always add a watchdog timer. 

  • Like 1
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...