Add conversion methods to WorldPoint and LocalPoint

This commit is contained in:
Max Weber
2018-03-08 02:47:43 -07:00
parent 6f9b855520
commit cf4d5d8333
4 changed files with 210 additions and 3 deletions

View File

@@ -47,6 +47,11 @@
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>1.3.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>

View File

@@ -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;

View File

@@ -0,0 +1,113 @@
/*
* Copyright (c) 2018 Abex
* Copyright (c) 2017, Adam <Adam@sigterm.info>
* 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;
}
}

View File

@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018 Abex
* Copyright (c) 2017, Adam <Adam@sigterm.info>
* 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);
}
}