diff --git a/runelite-api/pom.xml b/runelite-api/pom.xml index 82ded15af1..d18ec334d0 100644 --- a/runelite-api/pom.xml +++ b/runelite-api/pom.xml @@ -47,6 +47,11 @@ lombok provided + + com.google.code.findbugs + jsr305 + 1.3.9 + junit diff --git a/runelite-api/src/main/java/net/runelite/api/Perspective.java b/runelite-api/src/main/java/net/runelite/api/Perspective.java index 8a0e44c1e0..36f02d5c74 100644 --- a/runelite-api/src/main/java/net/runelite/api/Perspective.java +++ b/runelite-api/src/main/java/net/runelite/api/Perspective.java @@ -64,6 +64,7 @@ public class Perspective * @param point world location * @return */ + @Deprecated public static boolean isWorldInScene(Client client, Point point) { int x = point.getX(); @@ -276,6 +277,7 @@ public class Perspective return 0; } + @Deprecated public static Point worldToLocal(Client client, Point point) { if (!isWorldInScene(client, point)) @@ -292,6 +294,7 @@ public class Perspective return new Point(x, y); } + @Deprecated public static Point localToWorld(Client client, Point point) { int x = (point.getX() >>> LOCAL_COORD_BITS) + client.getBaseX(); @@ -299,6 +302,7 @@ public class Perspective return new Point(x, y); } + @Deprecated public static Point regionToWorld(Client client, Point point) { int baseX = client.getBaseX(); @@ -308,6 +312,7 @@ public class Perspective return new Point(x, y); } + @Deprecated public static Point regionToLocal(Client client, Point point) { int x = point.getX() << LOCAL_COORD_BITS; diff --git a/runelite-api/src/main/java/net/runelite/api/coords/LocalPoint.java b/runelite-api/src/main/java/net/runelite/api/coords/LocalPoint.java new file mode 100644 index 0000000000..147dda469d --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/coords/LocalPoint.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2018 Abex + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.coords; + +import javax.annotation.Nullable; +import lombok.Value; +import net.runelite.api.Client; +import net.runelite.api.Perspective; + +/** + * A LocolPoint is a Two-Dimensional point in the local coordinate space. Because the local coordinate space moves, + * it is not safe to keep a LocalPoint after a loading zone. The unit is 1/128th of a Tile + */ +@Value +public class LocalPoint +{ + private final int x, y; + + /** + * Returns a LocalPoint of the center of the passed tile + * + * @return LocalPoint if in scene, otherwise null + */ + @Nullable + public static LocalPoint fromWorld(Client client, WorldPoint world) + { + if (client.getPlane() != world.getPlane()) + { + return null; + } + return fromWorld(client, world.getX(), world.getY()); + } + + /** + * Returns a LocalPoint of the center of the passed tile + * + * @return LocalPoint if in scene, otherwise null + */ + public static LocalPoint fromWorld(Client client, int x, int y) + { + if (!WorldPoint.isInScene(client, x, y)) + { + return null; + } + + int baseX = client.getBaseX(); + int baseY = client.getBaseY(); + + return fromRegion(x - baseX, y - baseY); + } + + /** + * Find the distance from this point to another point + * + * @param other + * @return + */ + public int distanceTo(LocalPoint other) + { + return (int) Math.hypot(getX() - other.getX(), getY() - other.getY()); + } + + /** + * Returns a LocalPoint of the center of the passed tile + */ + public static LocalPoint fromRegion(int x, int y) + { + return new LocalPoint( + (x << Perspective.LOCAL_COORD_BITS) + (1 << Perspective.LOCAL_COORD_BITS - 1) - 1, + (y << Perspective.LOCAL_COORD_BITS) + (1 << Perspective.LOCAL_COORD_BITS - 1) - 1 + ); + } + + /** + * Returns the X coordinate in Region space (tiles) + */ + public int getRegionX() + { + return x >>> Perspective.LOCAL_COORD_BITS; + } + + + /** + * Returns the Y coordinate in Region space (tiles) + */ + public int getRegionY() + { + return y >>> Perspective.LOCAL_COORD_BITS; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/coords/WorldPoint.java b/runelite-api/src/main/java/net/runelite/api/coords/WorldPoint.java index 75a4430819..7859d75f35 100644 --- a/runelite-api/src/main/java/net/runelite/api/coords/WorldPoint.java +++ b/runelite-api/src/main/java/net/runelite/api/coords/WorldPoint.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2018 Abex + * Copyright (c) 2017, Adam * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,8 +26,13 @@ package net.runelite.api.coords; import lombok.Value; +import net.runelite.api.Client; +import net.runelite.api.Perspective; import net.runelite.api.Point; +/** + * WorldPoint is a Three-Dimensional point representing the location of a Tile + */ @Value public class WorldPoint { @@ -34,21 +40,99 @@ public class WorldPoint * The X coordinate of the Point. * Units are in tiles */ - private final int X; + private final int x; /** * The Y coordinate of the Point. * Units are in tiles */ - private final int Y; + private final int y; /** * The plane coordinate of the Point. */ private final int plane; + public static boolean isInScene(Client client, int x, int y) + { + int baseX = client.getBaseX(); + int baseY = client.getBaseY(); + + int maxX = baseX + Perspective.SCENE_SIZE; + int maxY = baseY + Perspective.SCENE_SIZE; + + return x >= baseX && x < maxX && y >= baseY && y < maxY; + } + + public boolean isInScene(Client client) + { + return client.getPlane() == plane && isInScene(client, x, y); + } + + /** + * Returns a WorldPoint containing the passed LocalPoint + */ + public static WorldPoint fromLocal(Client client, LocalPoint local) + { + return fromLocal(client, local.getX(), local.getY(), client.getPlane()); + } + + /** + * Returns a WorldPoint containing the passed local coordinates + */ + public static WorldPoint fromLocal(Client client, int x, int y, int plane) + { + return new WorldPoint( + (x >>> Perspective.LOCAL_COORD_BITS) + client.getBaseX(), + (y >>> Perspective.LOCAL_COORD_BITS) + client.getBaseY(), + plane + ); + } + + /** + * Find the distance from this point to another point. Returns Integer.MAX_VALUE if other is on + * a different plane. + * + * @param other + * @return + */ + public int distanceTo(WorldPoint other) + { + if (other.plane != plane) + { + return Integer.MAX_VALUE; + } + + return (int) Math.hypot(getX() - other.getX(), getY() - other.getY()); + } + + + /** + * Find the distance from this point to another point. + * + * @param other + * @return + */ + public int distanceTo2D(WorldPoint other) + { + return (int) Math.hypot(getX() - other.getX(), getY() - other.getY()); + } + + /** + * Returns a WorldPoint from the passed region coords + */ + public static WorldPoint fromRegion(Client client, int x, int y, int plane) + { + return new WorldPoint( + x + client.getBaseX(), + y + client.getBaseY(), + plane + ); + } + + @Deprecated public Point toPoint() { - return new Point(X, Y); + return new Point(x, y); } }