Skip to content

Commit

Permalink
perspective: fix getCanvasTileAreaPoly when getting tile next to a br…
Browse files Browse the repository at this point in the history
…idge

Pick the plane of the given tile and use it for all height calculations
  • Loading branch information
Adam- committed Jul 27, 2018
1 parent 0b8857d commit cca977f
Showing 1 changed file with 60 additions and 16 deletions.
76 changes: 60 additions & 16 deletions runelite-api/src/main/java/net/runelite/api/Perspective.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,33 @@ public static int getTileHeight(@Nonnull Client client, int localX, int localY,
return 0;
}

/**
* Get the height of a location, in local coordinates. Interpolates the height from the adjacent tiles.
* Does not account for bridges.
* @param client
* @param localX
* @param localY
* @param plane
* @return
*/
private static int getHeight(@Nonnull Client client, int localX, int localY, int plane)
{
int sceneX = localX >> LOCAL_COORD_BITS;
int sceneY = localY >> LOCAL_COORD_BITS;
if (sceneX >= 0 && sceneY >= 0 && sceneX < SCENE_SIZE && sceneY < SCENE_SIZE)
{
int[][][] tileHeights = client.getTileHeights();

int x = localX & (LOCAL_TILE_SIZE - 1);
int y = localY & (LOCAL_TILE_SIZE - 1);
int var8 = x * tileHeights[plane][sceneX + 1][sceneY] + (LOCAL_TILE_SIZE - x) * tileHeights[plane][sceneX][sceneY] >> LOCAL_COORD_BITS;
int var9 = tileHeights[plane][sceneX][sceneY + 1] * (LOCAL_TILE_SIZE - x) + x * tileHeights[plane][sceneX + 1][sceneY + 1] >> LOCAL_COORD_BITS;
return (LOCAL_TILE_SIZE - y) * var8 + y * var9 >> LOCAL_COORD_BITS;
}

return 0;
}

/**
* Calculates a tile polygon from offset worldToScreen() points.
*
Expand All @@ -284,23 +311,40 @@ public static Polygon getCanvasTilePoly(@Nonnull Client client, @Nonnull LocalPo
*/
public static Polygon getCanvasTileAreaPoly(@Nonnull Client client, @Nonnull LocalPoint localLocation, int size)
{
int plane = client.getPlane();
final int plane = client.getPlane();

final int swX = localLocation.getX() - (size * LOCAL_TILE_SIZE / 2);
final int swY = localLocation.getY() - (size * LOCAL_TILE_SIZE / 2);

final int neX = localLocation.getX() + (size * LOCAL_TILE_SIZE / 2);
final int neY = localLocation.getY() + (size * LOCAL_TILE_SIZE/2);

final int seX = swX;
final int seY = neY;

final int nwX = neX;
final int nwY = swY;

final byte[][][] tileSettings = client.getTileSettings();

final int sceneX = localLocation.getSceneX();
final int sceneY = localLocation.getSceneY();

int tilePlane = plane;
if (plane < Constants.MAX_Z - 1 && (tileSettings[1][sceneX][sceneY] & TILE_FLAG_BRIDGE) == TILE_FLAG_BRIDGE)
{
tilePlane = plane + 1;
}

final int swHeight = getHeight(client, swX, swY, tilePlane);
final int nwHeight = getHeight(client, nwX, nwY, tilePlane);
final int neHeight = getHeight(client, neX, neY, tilePlane);
final int seHeight = getHeight(client, seX, seY, tilePlane);

// Shift over one half tile as localLocation is the center point of the tile, and then shift the area size
Point southWestCorner = new Point(localLocation.getX() - (size * LOCAL_TILE_SIZE / 2),
localLocation.getY() - (size * LOCAL_TILE_SIZE / 2));
// expand by size
Point northEastCorner = new Point(southWestCorner.getX() + size * LOCAL_TILE_SIZE - 1,
southWestCorner.getY() + size * LOCAL_TILE_SIZE - 1);
// Take the x of top left and the y of bottom right to create bottom left
Point bottomRight = new Point(southWestCorner.getX(), northEastCorner.getY());
// Similarly for top right
Point topLeft = new Point(northEastCorner.getX(), southWestCorner.getY());

Point p1 = worldToCanvas(client, southWestCorner.getX(), southWestCorner.getY(), plane);
Point p2 = worldToCanvas(client, topLeft.getX(), topLeft.getY(), plane);
Point p3 = worldToCanvas(client, northEastCorner.getX(), northEastCorner.getY(), plane);
Point p4 = worldToCanvas(client, bottomRight.getX(), bottomRight.getY(), plane);
Point p1 = localToCanvas(client, swX, swY, swHeight);
Point p2 = localToCanvas(client, nwX, nwY, nwHeight);
Point p3 = localToCanvas(client, neX, neY, neHeight);
Point p4 = localToCanvas(client, seX, seY, seHeight);

if (p1 == null || p2 == null || p3 == null || p4 == null)
{
Expand Down

0 comments on commit cca977f

Please sign in to comment.