Jump to content

Tutorial: How to walk a path in one line of code


Alek

Recommended Posts

    private Position[] path = {
            new Position(3254, 3421, 0), new Position(3256, 3428, 0), new Position(3264, 3428, 0),
            new Position(3273, 3428, 0), new Position(3277, 3426, 0), new Position(3281, 3422, 0)
    };


getWalking().walkPath(path);

Explanation:

The position array listed above will be defined at the top of your script (with the other variables) and getWalking().walkPath(path) will be defined wherever you want to call it (such as in a method, in your onLoop, etc.). 

 

Getting positions:

Click on the settings gear icon in the client and scroll over "Entity debugs" and then toggle "Entity hover debug". Hover your mouse over a tile and it will show the position of the tile. The third number in the position constructor is the "Z" coordinate, meaning the current plane which you are on.

 

Order:

The position array has a list of positions with the first position being where your character will start walking and the last position being where your character will end walking. Add positions in a manner where once your character reaches one position, it can find the next position on the minimap. 

 

Additional:

If your character starts in the middle of the path, it will not walk to the beginning, it will resume until it reaches the final position. If for some reason your character gets off the path, the walkPath() method will attempt to get back onto your path.

 

  • Like 4
  • Sad 1
Link to comment
Share on other sites

 

Additional:

If your character starts in the middle of the path, it will not walk to the beginning, it will resume until it reaches the final position. If for some reason your character gets off the path, the walkPath() method will attempt to get back onto your path.

 

Based on what you said, does that mean we don't have to implement checks to determine where to start? Ex. The path contains 5 positions and my char is between 3 and 4, it will just start walking to 4?

Link to comment
Share on other sites

Based on what you said, does that mean we don't have to implement checks to determine where to start? Ex. The path contains 5 positions and my char is between 3 and 4, it will just start walking to 4?

that exactly what hes talking about.

 

Hes mostly likely using this method ive see on OSB

    public Position nextTile(int skipDist, Position...path) {
        int dist = -1, closest = -1;
        
        for (int i = path.length - 1; i >= 0; i--) {
            Position tile = path[i];
            int d = s.map.distance(tile);
            if (d < dist || dist == -1) {
                dist = d;
                closest = i;
            }
        }
        
        int feasibleTileIndex = -1;
        
        for (int i = closest; i < path.length; i++) {
            if (s.map.distance(path[i]) <= skipDist) {
                feasibleTileIndex = i;
            } else {
                break;
            }
        }

		return (feasibleTileIndex == -1) ? null : path[feasibleTileIndex];
    }
Link to comment
Share on other sites

It iterates to the positions closest to you, then continues to walk the path.

 

@BotRS123 - It does everything except handle obstacles.

@josedpay - I never use other people's code, defeats the purpose of learning :)

I feel you, well if anybody is just curious of how it could be done they have a method above (if you really read the code, you'll understand it and be like wow that was easy). If not they could create their own algo like you did. Thanks for the walking method btw.
Link to comment
Share on other sites

  • 1 month later...
  • 3 months later...
    private Position[] reverse(Position[] p) {
     Position[] r = new Position[p.length];
     
        for (int i=p.length;i>0;i--)
            r[i] = p[i];
        
     return r;
    }

 

For an array A where:

 

Position A[0] = x

Position A[1]  = y

Position A[2] = z

 

And an empty array B with size 3.

 

Array B after your method:

 

Position B[2] = A[2]

Position B[1] = A[1]

Position B[0] = A[0]

 

->

 

B[0] = A[0] = x

B[1] = A[1] = y

B[2] = B[2] = z

 

A = B 

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...