Write a function to be used within a Runelite plugin to trace out a shortest path (in terms of world coordinates to click on that will be accessible/clickable on the screen) from the current world coordinate to any other given world coordinate (if possible with just walking).
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a function for a RuneLite plugin that finds the shortest walkable path between two world coordinates:
java
import net.runelite.api.Client;
import net.runelite.api.CollisionData;
import net.runelite.api.Player;
import net.runelite.api.coords.LocalPoint;
import net.runelite.api.coords.WorldPoint;
import javax.inject.Inject;
import java.util.*;
public class PathFinder {
@Inject
private Client client;
private static final int SCENE_SIZE = 104;
private static final int MAX_INTERACT_DISTANCE = 20;
public List<WorldPoint> findPath(WorldPoint start, WorldPoint destination) {
if (start == null || destination == null) {
return Collections.emptyList();
}
// Check if destination is on same plane
if (start.getPlane() != destination.getPlane()) {
return Collections.emptyList();
}
// Use A* pathfinding algorithm
PriorityQueue<Node> openSet = new PriorityQueue<>(Comparator.comparingInt(n -> n.f));
Map<WorldPoint, Node> allNodes = new HashMap<>();
Set<WorldPoint> closedSet = new HashSet<>();
Node startNode = new Node(start, null, 0, heuristic(start, destination));
openSet.add(startNode);
allNodes.put(start, startNode);
while (!openSet.isEmpty()) {
Node current = openSet.poll();
if (current.point.equals(destination)) {
return reconstructPath(current);
}
closedSet.add(current.point);
// Check all adjacent tiles
for (Direction dir : Direction.values()) {
WorldPoint neighbor = current.point.dx(dir.x).dy(dir.y);
if (closedSet.contains(neighbor)) {
continue;
}
if (!isWalkable(current.point, neighbor)) {
continue;
}
int tentativeG = current.g + 1;
Node neighborNode = allNodes.get(neighbor);
if (neighborNode == null) {
neighborNode = new Node(neighbor, current, tentativeG,
tentativeG + heuristic(neighbor, destination));
allNodes.put(neighbor, neighborNode);
openSet.add(neighborNode);
} else if (tentativeG < neighborNode.g) {
neighborNode.parent = current;
neighborNode.g = tentativeG;
neighborNode.f = tentativeG + heuristic(neighbor, destination);
// Re-add to update priority
openSet.remove(neighborNode);
openSet.add(neighborNode);
}
}
}
return Collections.emptyList(); // No path found
}
public List<WorldPoint> findClickablePath(WorldPoint destination) {
Player localPlayer = client.getLocalPlayer();
if (localPlayer == null) {
return Collections.emptyList();
}
WorldPoint start = localPlayer.getWorldLocation();
List<WorldPoint> fullPath = findPath(start, destination);
if (fullPath.isEmpty()) {
return fullPath;
}
// Filter path to only include points that would be clickable on screen
List<WorldPoint> clickablePath = new ArrayList<>();
WorldPoint currentPos = start;
for (int i = 0; i < fullPath.size(); i++) {
WorldPoint point = fullPath.get(i);
// Check if this point would be visible and clickable from current position
if (isClickableFromPosition(currentPos, point) || i == fullPath.size() - 1) {
clickablePath.add(point);
currentPos = point;
}
}
return clickablePath;
}
private boolean isWalkable(WorldPoint from, WorldPoint to) {
LocalPoint fromLocal = LocalPoint.fromWorld(client, from);
LocalPoint toLocal = LocalPoint.fromWorld(client, to);
if (fromLocal == null || toLocal == null) {
return false; // Outside loaded region
}
CollisionData[] collisionData = client.getCollisionMaps();
if (collisionData == null) {
return false;
}
int plane = from.getPlane();
if (plane >= collisionData.length || collisionData[plane] == null) {
return false;
}
int fromX = fromLocal.getSceneX();
int fromY = fromLocal.getSceneY();
int toX = toLocal.getSceneX();
int toY = toLocal.getSceneY();
if (fromX < 0 || fromX >= SCENE_SIZE || fromY < 0 || fromY >= SCENE_SIZE ||
toX < 0 || toX >= SCENE_SIZE || toY < 0 || toY >= SCENE_SIZE) {
return false;
}
int dx = toX - fromX;
int dy = toY - fromY;
// Check collision flags based on direction
int[][] flags = collisionData[plane].getFlags();
if (dx == 0 && dy == 1) { // North
return (flags[fromX][fromY] & CollisionDataFlag.BLOCK_MOVEMENT_NORTH) == 0;
} else if (dx == 1 && dy == 0) { // East
return (flags[fromX][fromY] & CollisionDataFlag.BLOCK_MOVEMENT_EAST) == 0;
} else if (dx == 0 && dy == -1) { // South
return (flags[fromX][fromY] & CollisionDataFlag.BLOCK_MOVEMENT_SOUTH) == 0;
} else if (dx == -1 && dy == 0) { // West
return (flags[fromX][fromY] & CollisionDataFlag.BLOCK_MOVEMENT_WEST) == 0;
} else if (dx == 1 && dy == 1) { // Northeast
return (flags[fromX][fromY] & CollisionDataFlag.BLOCK_MOVEMENT_NORTH_EAST) == 0 &&
(flags[fromX + 1][fromY] & CollisionDataFlag.BLOCK_MOVEMENT_NORTH) == 0 &&
(flags[fromX][fromY + 1] & CollisionDataFlag.BLOCK_MOVEMENT_EAST) == 0;
} else if (dx == 1 && dy == -1) { // Southeast
return (flags[fromX][fromY] & CollisionDataFlag.BLOCK_MOVEMENT_SOUTH_EAST) == 0 &&
(flags[fromX + 1][fromY] & CollisionDataFlag.BLOCK_MOVEMENT_SOUTH) == 0 &&
(flags[fromX][fromY - 1] & CollisionDataFlag.BLOCK_MOVEMENT_EAST) == 0;
} else if (dx == -1 && dy == -1) { // Southwest
return (flags[fromX][fromY] & CollisionDataFlag.BLOCK_MOVEMENT_SOUTH_WEST) == 0 &&
(flags[fromX - 1][fromY] & CollisionDataFlag.BLOCK_MOVEMENT_SOUTH) == 0 &&
(flags[fromX][fromY - 1] & CollisionDataFlag.BLOCK_MOVEMENT_WEST) == 0;
} else if (dx == -1 && dy == 1) { // Northwest
return (flags[fromX][fromY] & CollisionDataFlag.BLOCK_MOVEMENT_NORTH_WEST) == 0 &&
(flags[fromX - 1][fromY] & CollisionDataFlag.BLOCK_MOVEMENT_NORTH) == 0 &&
(flags[fromX][fromY + 1] & CollisionDataFlag.BLOCK_MOVEMENT_WEST) == 0;
}
return false;
}
private boolean isClickableFromPosition(WorldPoint from, WorldPoint to) {
int distance = from.distanceTo(to);
// Check if within reasonable clicking distance
if (distance > MAX_INTERACT_DISTANCE) {
return false;
}
// Check if the tile would be visible on screen
LocalPoint toLocal = LocalPoint.fromWorld(client, to);
if (toLocal == null) {
return false;
}
// Additional visibility checks could be added here
// For now, we assume tiles within interact distance are clickable
return true;
}
private int heuristic(WorldPoint a, WorldPoint b) {
// Manhattan distance heuristic
return Math.abs(a.getX() - b.getX()) + Math.abs(a.getY() - b.getY());
}
private List<WorldPoint> reconstructPath(Node node) {
List<WorldPoint> path = new ArrayList<>();
while (node != null) {
path.add(0, node.point);
node = node.parent;
}
return path;
}
private static class Node {
WorldPoint point;
Node parent;
int g; // Cost from start
int f; // Total cost (g + heuristic)
Node(WorldPoint point, Node parent, int g, int f) {
this.point = point;
this.parent = parent;
this.g = g;
this.f = f;
}
}
private enum Direction {
NORTH(0, 1),
NORTHEAST(1, 1),
EAST(1, 0),
SOUTHEAST(1, -1),
SOUTH(0, -1),
SOUTHWEST(-1, -1),
WEST(-1, 0),
NORTHWEST(-1, 1);
final int x, y;
Direction(int x, int y) {
this.x = x;
this.y = y;
}
}
// Collision flags interface
private interface CollisionDataFlag {
int BLOCK_MOVEMENT_NORTH = 0x2;
int BLOCK_MOVEMENT_EAST = 0x8;
int BLOCK_MOVEMENT_SOUTH = 0x20;
int BLOCK_MOVEMENT_WEST = 0x80;
int BLOCK_MOVEMENT_NORTH_EAST = 0x4;
int BLOCK_MOVEMENT_SOUTH_EAST = 0x10;
int BLOCK_MOVEMENT_NORTH_WEST = 0x1;
int BLOCK_MOVEMENT_SOUTH_WEST = 0x40;
}
}
This implementation provides two main methods:
findPath(WorldPoint start, WorldPoint destination) - Finds the complete shortest walkable path between two points using A* pathfinding.
findClickablePath(WorldPoint destination) - Finds a path from the player's current location to the destination, but filters it to only include waypoints that would be clickable on screen. This reduces the path to key points that the player needs to click.
The pathfinding algorithm checks collision data to ensure the path only goes through walkable tiles. It handles all 8 directions of movement and properly checks collision flags for diagonal movement.
To use this in your plugin, inject the Client instance and call findClickablePath() with your destination. The returned list contains the world coordinates you should click on in order to reach the destination efficiently.