Skip to content

Commit

Permalink
api: use WorldView coord base
Browse files Browse the repository at this point in the history
WV and Scene's baseX/Y/templateChunks are different between the start
of LOADING and the scene being actually swapped in, which happens later
  • Loading branch information
abextm authored and Adam- committed May 24, 2024
1 parent e571865 commit 321993b
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 35 deletions.
5 changes: 1 addition & 4 deletions runelite-api/src/main/java/net/runelite/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -2075,10 +2075,7 @@ public interface Client extends OAuthApi, GameEngine
* @see InstanceTemplates
*/
@Deprecated
default int[][][] getInstanceTemplateChunks()
{
return getTopLevelWorldView().getScene().getInstanceTemplateChunks();
}
int[][][] getInstanceTemplateChunks();

/**
* Returns a 2D array containing XTEA encryption keys used to decrypt
Expand Down
41 changes: 41 additions & 0 deletions runelite-api/src/main/java/net/runelite/api/WorldView.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ public interface WorldView
*/
byte[][][] getTileSettings();

/**
* Get the size of the world view, x-axis
* @return
*/
int getSizeX();

/**
* Get the size of the world view, y-axis
* @return
*/
int getSizeY();

/**
* Returns the x-axis base coordinate.
* <p>
Expand Down Expand Up @@ -161,4 +173,33 @@ Projectile createProjectile(int id, int plane, int startX, int startY, int start
*/
@Nullable
Tile getSelectedSceneTile();

/**
* Check if this scene is an instance
* @see #getInstanceTemplateChunks()
* @return
*/
boolean isInstance();

/**
* Contains a 3D array of template chunks for instanced areas.
* <p>
* The array returned is of format [z][x][y], where z is the
* plane, x and y the x-axis and y-axis coordinates of a tile
* divided by the size of a chunk.
* <p>
* The bits of the int value held by the coordinates are -1 if there is no data,
* structured in the following format:
* <pre>{@code
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |rot| y chunk coord | x chunk coord |pln| |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* }</pre>
* @return the array of instance template chunks
* @see Constants#CHUNK_SIZE
* @see InstanceTemplates
*/
int[][][] getInstanceTemplateChunks();
}
35 changes: 32 additions & 3 deletions runelite-api/src/main/java/net/runelite/api/coords/LocalPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,21 @@ public static LocalPoint fromWorld(Client client, WorldPoint world)
*
* @return coordinate if the tile is in the world view, otherwise null
*/
@Nullable
public static LocalPoint fromWorld(WorldView wv, WorldPoint world)
{
if (wv.getPlane() != world.getPlane())
{
return null;
}
return fromWorld(wv.getScene(), world.getX(), world.getY());
return fromWorld(wv, world.getX(), world.getY());
}

@Deprecated
@Nullable
public static LocalPoint fromWorld(Client client, int x, int y)
{
return fromWorld(client.getTopLevelWorldView().getScene(), x, y);
return fromWorld(client.getTopLevelWorldView(), x, y);
}

/**
Expand All @@ -97,9 +99,18 @@ public static LocalPoint fromWorld(Client client, int x, int y)
* @param y y-axis coordinate of the tile
* @return coordinate if the tile is in the current scene, otherwise null
*/
@Nullable
public static LocalPoint fromWorld(WorldView wv, int x, int y)
{
return fromWorld(wv.getScene(), x, y);
if (!WorldPoint.isInScene(wv, x, y))
{
return null;
}

int baseX = wv.getBaseX();
int baseY = wv.getBaseY();

return fromScene(x - baseX, y - baseY, wv);
}

/**
Expand All @@ -110,6 +121,7 @@ public static LocalPoint fromWorld(WorldView wv, int x, int y)
* @param y y-axis coordinate of the tile
* @return coordinate if the tile is in the current scene, otherwise null
*/
@Nullable
public static LocalPoint fromWorld(Scene scene, int x, int y)
{
if (!WorldPoint.isInScene(scene, x, y))
Expand Down Expand Up @@ -183,6 +195,23 @@ public static LocalPoint fromScene(int x, int y, Scene scene)
);
}

/**
* Gets the coordinate at the center of the passed tile.
*
* @param x x-axis coordinate of the tile in Scene coords
* @param y y-axis coordinate of the tile in Scene coords
* @param wv wv containing the tile
* @return true coordinate of the tile
*/
public static LocalPoint fromScene(int x, int y, WorldView wv)
{
return new LocalPoint(
(x << Perspective.LOCAL_COORD_BITS) + (1 << Perspective.LOCAL_COORD_BITS - 1),
(y << Perspective.LOCAL_COORD_BITS) + (1 << Perspective.LOCAL_COORD_BITS - 1),
wv.getId()
);
}

/**
* Gets the x-axis coordinate in scene space (tiles).
*
Expand Down
66 changes: 49 additions & 17 deletions runelite-api/src/main/java/net/runelite/api/coords/WorldPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,30 +132,38 @@ public static boolean isInScene(Scene scene, int x, int y)
@Deprecated
public static boolean isInScene(Client client, int x, int y)
{
return isInScene(client.getTopLevelWorldView().getScene(), x, y);
return isInScene(client.getTopLevelWorldView(), x, y);
}

/**
* Checks whether this tile is located in the current scene.
* Checks whether a tile is located in the current scene.
*
* @param client the client
* @return true if this tile is in the scene, false otherwise
* @param wv the client
* @param x the tiles x coordinate
* @param y the tiles y coordinate
* @return true if the tile is in the scene, false otherwise
*/
@Deprecated
public boolean isInScene(Client client)
public static boolean isInScene(WorldView wv, int x, int y)
{
return isInScene(client.getTopLevelWorldView());
int baseX = wv.getBaseX();
int baseY = wv.getBaseY();

int maxX = baseX + wv.getSizeX();
int maxY = baseY + wv.getSizeY();

return x >= baseX && x < maxX && y >= baseY && y < maxY;
}

/**
* Checks whether this tile is located in the scene.
* Checks whether this tile is located in the current scene.
*
* @param worldView the scene to check
* @param client the client
* @return true if this tile is in the scene, false otherwise
*/
public boolean isInScene(WorldView worldView)
@Deprecated
public boolean isInScene(Client client)
{
return worldView.getPlane() == plane && isInScene(worldView.getScene(), x, y);
return client.getPlane() == plane && isInScene(client, x, y);
}

/**
Expand All @@ -182,7 +190,11 @@ public static WorldPoint fromLocal(Client client, LocalPoint local)
*/
public static WorldPoint fromLocal(WorldView wv, int x, int y, int plane)
{
return fromLocal(wv.getScene(), x, y, plane);
return new WorldPoint(
(x >> Perspective.LOCAL_COORD_BITS) + wv.getBaseX(),
(y >> Perspective.LOCAL_COORD_BITS) + wv.getBaseY(),
plane
);
}

/**
Expand Down Expand Up @@ -245,9 +257,9 @@ public static WorldPoint fromLocalInstance(Client client, LocalPoint localPoint,
{
var wv = client.getWorldView(localPoint.getWorldView());

if (wv.getScene().isInstance())
if (wv.isInstance())
{
return fromLocalInstance(wv.getScene().getInstanceTemplateChunks(), localPoint, plane);
return fromLocalInstance(wv.getInstanceTemplateChunks(), localPoint, plane);
}
else
{
Expand Down Expand Up @@ -309,7 +321,23 @@ private static WorldPoint fromLocalInstance(int[][][] instanceTemplateChunks, Lo
@Deprecated
public static Collection<WorldPoint> toLocalInstance(Client client, WorldPoint worldPoint)
{
return toLocalInstance(client.getTopLevelWorldView().getScene(), worldPoint);
return toLocalInstance(client.getTopLevelWorldView(), 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.
*/
public static Collection<WorldPoint> toLocalInstance(WorldView wv, WorldPoint worldPoint)
{
if (wv.isInstance())
{
return toLocalInstance(wv.getInstanceTemplateChunks(), wv.getBaseX(), wv.getBaseY(), worldPoint);
}
else
{
return Collections.singleton(worldPoint);
}
}

/**
Expand Down Expand Up @@ -432,15 +460,19 @@ public int distanceTo2D(WorldPoint other)
@Deprecated
public static WorldPoint fromScene(Client client, int x, int y, int plane)
{
return fromScene(client.getTopLevelWorldView().getScene(), x, y, plane);
return fromScene(client.getTopLevelWorldView(), x, y, plane);
}

/**
* Converts the passed scene coordinates to a world space
*/
public static WorldPoint fromScene(WorldView wv, int x, int y, int plane)
{
return fromScene(wv.getScene(), x, y , plane);
return new WorldPoint(
x + wv.getBaseX(),
y + wv.getBaseY(),
plane
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ public void testLocationHintArrowCleared()

// Move to SW of DRAYNOR_WHEAT_FIELD (hint arrow should be visible here)
when(localPlayer.getWorldLocation()).thenReturn(new WorldPoint(3105, 3265, 0));
Scene scene = mock(Scene.class);
when(scene.getBaseX()).thenReturn(3056);
when(scene.getBaseY()).thenReturn(3216);
WorldView wv = mock(WorldView.class);
when(wv.getScene()).thenReturn(scene);
when(wv.getBaseX()).thenReturn(3056);
when(wv.getBaseY()).thenReturn(3216);
when(wv.getSizeX()).thenReturn(104);
when(wv.getSizeY()).thenReturn(104);
when(client.getTopLevelWorldView()).thenReturn(wv);
plugin.onGameTick(new GameTick());
verify(client, times(++clueSetupHintArrowClears)).clearHintArrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import net.runelite.api.IterableHashTable;
import net.runelite.api.MessageNode;
import net.runelite.api.Player;
import net.runelite.api.Scene;
import net.runelite.api.Skill;
import net.runelite.api.WorldView;
import net.runelite.api.coords.LocalPoint;
Expand Down Expand Up @@ -149,7 +148,6 @@ public class LootTrackerPluginTest
@Bind
private ClientToolbar clientToolbar;

private Scene scene;
private WorldView wv;

@Before
Expand All @@ -161,9 +159,7 @@ public void setUp()
when(player.getWorldLocation()).thenReturn(new WorldPoint(0, 0, 0));
when(client.getLocalPlayer()).thenReturn(player);

scene = mock(Scene.class);
wv = mock(WorldView.class);
when(wv.getScene()).thenReturn(scene);
when(client.getTopLevelWorldView()).thenReturn(wv);

when(client.getWorldView(anyInt())).thenReturn(wv);
Expand Down Expand Up @@ -306,8 +302,8 @@ public void testToBRaidsLootValue()
when(itemManager.getItemComposition(ItemID.MAHOGANY_SEED)).thenReturn(compSeed);
when(compSeed.getHaPrice()).thenReturn(2_102);

when(scene.getBaseX()).thenReturn(3232);
when(scene.getBaseY()).thenReturn(4320);
when(wv.getBaseX()).thenReturn(3232);
when(wv.getBaseY()).thenReturn(4320);
LocalPoint localPoint = new LocalPoint(0, 0);
when(client.getLocalPlayer().getLocalLocation()).thenReturn(localPoint);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ public void before()
compostTracker.pendingCompostActions.clear();

WorldView wv = mock(WorldView.class);
when(wv.getSizeX()).thenReturn(104);
when(wv.getSizeY()).thenReturn(104);
when(client.getTopLevelWorldView()).thenReturn(wv);
when(wv.getScene()).thenReturn(scene);

when(client.getLocalPlayer()).thenReturn(player);
when(player.getWorldLocation()).thenReturn(worldPoint);
Expand Down

0 comments on commit 321993b

Please sign in to comment.