api: add method to find instanced tiles in the scene

Use in groundmarkers plugin
This commit is contained in:
Adam
2019-02-10 12:35:18 -05:00
parent 55395b6708
commit 31e1e2563b
2 changed files with 62 additions and 81 deletions

View File

@@ -25,6 +25,10 @@
*/
package net.runelite.api.coords;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import lombok.Value;
import net.runelite.api.Client;
import static net.runelite.api.Constants.CHUNK_SIZE;
@@ -150,7 +154,8 @@ public class WorldPoint
}
/**
* Gets the coordinate of the tile that contains the passed local point.
* Gets the coordinate of the tile that contains the passed local point,
* accounting for instances.
*
* @param client the client
* @param localPoint the local coordinate
@@ -190,6 +195,46 @@ public class WorldPoint
}
}
/**
* Get occurrences of a tile on the scene, accounting for instances. There may be
* more than one if the same template chunk occurs more than once on the scene.
* @param client
* @param worldPoint
* @return
*/
public static Collection<WorldPoint> toLocalInstance(Client client, WorldPoint worldPoint)
{
if (!client.isInInstancedRegion())
{
return Collections.singleton(worldPoint);
}
// find instance chunks using the template point. there might be more than one.
List<WorldPoint> worldPoints = new ArrayList<>();
final int z = client.getPlane();
int[][][] instanceTemplateChunks = client.getInstanceTemplateChunks();
for (int x = 0; x < instanceTemplateChunks[z].length; ++x)
{
for (int y = 0; y < instanceTemplateChunks[z][x].length; ++y)
{
int chunkData = instanceTemplateChunks[z][x][y];
int rotation = chunkData >> 1 & 0x3;
int templateChunkY = (chunkData >> 3 & 0x7FF) * CHUNK_SIZE;
int templateChunkX = (chunkData >> 14 & 0x3FF) * CHUNK_SIZE;
if (worldPoint.getX() >= templateChunkX && worldPoint.getX() < templateChunkX + CHUNK_SIZE
&& worldPoint.getY() >= templateChunkY && worldPoint.getY() < templateChunkY + CHUNK_SIZE)
{
WorldPoint p = new WorldPoint(client.getBaseX() + x * CHUNK_SIZE + (worldPoint.getX() & (CHUNK_SIZE - 1)),
client.getBaseY() + y * CHUNK_SIZE + (worldPoint.getY() & (CHUNK_SIZE - 1)),
worldPoint.getPlane());
p = rotate(p, rotation);
worldPoints.add(p);
}
}
}
return worldPoints;
}
/**
* Rotate the coordinates in the chunk according to chunk rotation
*