Added worldToMiniMap() and hooked mapOffset (#20)

Added worldToMiniMap() and hooked mapOffset
This commit is contained in:
Kronos
2017-04-15 10:16:30 +10:00
committed by Adam
parent db7737906f
commit c412ab1764
5 changed files with 169 additions and 40 deletions

View File

@@ -24,10 +24,8 @@
*/
package net.runelite.api;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.Rectangle2D;
import net.runelite.rs.api.CombatInfo1;
import net.runelite.rs.api.CombatInfo2;
import net.runelite.rs.api.CombatInfoList;
@@ -139,43 +137,16 @@ public abstract class Actor extends Renderable
public Polygon getCanvasTilePoly()
{
int plane = client.getPlane();
int halfTile = Perspective.LOCAL_TILE_SIZE / 2;
Point p1 = Perspective.worldToCanvas(client, getX() - halfTile, getY() - halfTile, plane);
Point p2 = Perspective.worldToCanvas(client, getX() - halfTile, getY() + halfTile, plane);
Point p3 = Perspective.worldToCanvas(client, getX() + halfTile, getY() + halfTile, plane);
Point p4 = Perspective.worldToCanvas(client, getX() + halfTile, getY() - halfTile, plane);
if (p1 == null || p2 == null || p3 == null || p4 == null)
{
return null;
}
Polygon poly = new Polygon();
poly.addPoint(p1.getX(), p1.getY());
poly.addPoint(p2.getX(), p2.getY());
poly.addPoint(p3.getX(), p3.getY());
poly.addPoint(p4.getX(), p4.getY());
return poly;
return Perspective.getCanvasTilePoly(client, getLocalLocation());
}
public Point getCanvasTextLocation(Graphics2D graphics, String text, int zOffset)
{
int plane = client.getPlane();
return Perspective.getCanvasTextLocation(client, graphics, getLocalLocation(), text, zOffset);
}
Point p = Perspective.worldToCanvas(client, getLocalLocation().getX(), getLocalLocation().getY(), plane, zOffset);
if (p == null)
{
return null;
}
FontMetrics fm = graphics.getFontMetrics();
Rectangle2D bounds = fm.getStringBounds(text, graphics);
int xOffset = p.getX() - (int) (bounds.getWidth() / 2);
return new Point(xOffset, p.getY());
public Point getMinimapLocation()
{
return Perspective.worldToMiniMap(client, getX(), getY());
}
}

View File

@@ -204,6 +204,26 @@ public class Client
return client.getWidgetPositionsY();
}
public int getMapScale()
{
return client.getMapScale();
}
public int getMapAngle()
{
return client.getMapAngle();
}
public int getMapOffset()
{
return client.getMapOffset();
}
public boolean isResized()
{
return client.isResized();
}
public int getRevision()
{
return client.getRevision();

View File

@@ -24,6 +24,11 @@
*/
package net.runelite.api;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.Rectangle2D;
public class Perspective
{
private static final double UNIT = Math.PI / 1024d; // How much of the circle each unit of SINE/COSINE is
@@ -107,6 +112,62 @@ public class Perspective
}
/**
* Translates two-dimensional ground coordinates within the 3D world to
* their corresponding coordinates on the Minimap.
*
* @param client
* @param x ground coordinate on the x axis
* @param y ground coordinate on the y axis
* @return a {@link Point} on screen corresponding to the position in
* 3D-space
*/
public static Point worldToMiniMap(Client client, int x, int y)
{
return worldToMiniMap(client, x, y, 6400);
}
/**
* Translates two-dimensional ground coordinates within the 3D world to
* their corresponding coordinates on the Minimap.
*
* @param client
* @param x ground coordinate on the x axis
* @param y ground coordinate on the y axis
* @param distance max distance from local player to minimap point
* @return a {@link Point} on screen corresponding to the position in
* 3D-space
*/
public static Point worldToMiniMap(Client client, int x, int y, int distance)
{
int angle = client.getMapScale() + client.getMapAngle() & 0x7FF;
Point localLocation = client.getLocalPlayer().getLocalLocation();
x = x / 32 - localLocation.getX() / 32;
y = y / 32 - localLocation.getY() / 32;
int dist = x * x + y * y;
if (dist < distance)
{
int sin = SINE[angle];
int cos = COSINE[angle];
sin = sin * 256 / (client.getMapOffset() + 256);
cos = cos * 256 / (client.getMapOffset() + 256);
int xx = y * sin + cos * x >> 16;
int yy = sin * x - y * cos >> 16;
int miniMapX = client.getClientWidth() - (!client.isResized() ? 208 : 167);
x = (miniMapX + 167 / 2) + xx;
y = (167 / 2 - 1) + yy;
return new Point(x, y);
}
return new Point(-1, -1);
}
/**
* Calculates the above ground height of a tile point.
*
@@ -159,4 +220,65 @@ public class Perspective
return new Point(x, y);
}
/**
* Calculates a tile polygon from offset worldToScreen() points.
*
* @param client
* @param localLocation local location of the tile
* @return a {@link Polygon} on screen corresponding to the given
* localLocation.
*/
public static Polygon getCanvasTilePoly(Client client, Point localLocation)
{
int plane = client.getPlane();
int halfTile = Perspective.LOCAL_TILE_SIZE / 2;
Point p1 = Perspective.worldToCanvas(client, localLocation.getX() - halfTile, localLocation.getY() - halfTile, plane);
Point p2 = Perspective.worldToCanvas(client, localLocation.getX() - halfTile, localLocation.getY() + halfTile, plane);
Point p3 = Perspective.worldToCanvas(client, localLocation.getX() + halfTile, localLocation.getY() + halfTile, plane);
Point p4 = Perspective.worldToCanvas(client, localLocation.getX() + halfTile, localLocation.getY() - halfTile, plane);
if (p1 == null || p2 == null || p3 == null || p4 == null)
{
return null;
}
Polygon poly = new Polygon();
poly.addPoint(p1.getX(), p1.getY());
poly.addPoint(p2.getX(), p2.getY());
poly.addPoint(p3.getX(), p3.getY());
poly.addPoint(p4.getX(), p4.getY());
return poly;
}
/**
* Calculates text position and centers depending on string length.
*
* @param client
* @param graphics
* @param localLocation local location of the tile
* @param text string for width measurement
* @param zOffset offset from ground plane
* @return a {@link Point} on screen corresponding to the given
* localLocation.
*/
public static Point getCanvasTextLocation(Client client, Graphics2D graphics, Point localLocation, String text, int zOffset)
{
int plane = client.getPlane();
Point p = Perspective.worldToCanvas(client, localLocation.getX(), localLocation.getY(), plane, zOffset);
if (p == null)
{
return null;
}
FontMetrics fm = graphics.getFontMetrics();
Rectangle2D bounds = fm.getStringBounds(text, graphics);
int xOffset = p.getX() - (int) (bounds.getWidth() / 2);
return new Point(xOffset, p.getY());
}
}

View File

@@ -24,6 +24,9 @@
*/
package net.runelite.api;
import java.awt.Graphics2D;
import java.awt.Polygon;
public abstract class TileObject
{
protected final Client client;
@@ -61,4 +64,19 @@ public abstract class TileObject
Point locaLocation = getLocalLocation();
return Perspective.worldToCanvas(client, locaLocation.getX(), locaLocation.getY(), 0);
}
public Polygon getCanvasTilePoly()
{
return Perspective.getCanvasTilePoly(client, getLocalLocation());
}
public Point getCanvasTextLocation(Graphics2D graphics, String text, int zOffset)
{
return Perspective.getCanvasTextLocation(client, graphics, getLocalLocation(), text, zOffset);
}
public Point getMinimapLocation()
{
return Perspective.worldToMiniMap(client, getLocalX(), getLocalY());
}
}