With this code, you can tell an NPC to move from point A to point B, and it'll calculate the best path using the Manhattan method. If you want to learn how this system works, I recommend reading this.
The only parts you might need to customize can be found in the onCreated() function. The system can be used both serverside and clientside.
Class: aStar
PHP Code:
//#CLIENTSIDE
function onCreated()
{
// The following settings are adjusted for useage with a "normal" showCharacter() and can be changed to your likings.
enum {
WIDTH_IN_TILES = 2.5,
HEIGHT_IN_TILES = 2,
MOVEMENT_SPEED = 0.1,
X_OFFSET = 0,
Y_OFFSET = 1,
NORMAL_GSCORE = 10,
DIAGONAL_GSCORE = 14
};
showCharacter();
}
function moveTo(temp.destinationX, temp.destinationY)
{
if (onwall2(temp.destinationX + X_OFFSET, temp.destinationY + Y_OFFSET, WIDTH_IN_TILES, HEIGHT_IN_TILES))
return this.chat = "Error: blocking!";
// On a wall? Ignore.
if (temp.nodeOnWall)
continue;
// Closed already? Ignore.
if (temp.identifier in this.closedNodes.getDynamicVarNames())
continue;
// On the open list already?
if (temp.identifier in this.openNodes.getDynamicVarNames()) {
// Is this path better?
if (temp.gScore >= this.openNodes.(@temp.identifier).gScore)
continue;
}
if (this.movementCounter == 1)
moveTo(45, 29);
else if (this.movementCounter == 2)
moveTo(50, 53);
else if (this.movementCounter == 3)
moveTo(25, 47);
else if (this.movementCounter == 4)
moveTo(8, 19);
}
Here's a video showing an example of usage. Note that in a lot of cases, it's better to split up the movement sequence in several shorter routes than one big, especially when your target position is close to the start from a birds eye point of view but still so far away.
The movement looks a bit choppy in the video, but it's not in-game.