Skip to content

Commit

Permalink
Added worldToMiniMap() and hooked mapOffset (runelite#20)
Browse files Browse the repository at this point in the history
Added worldToMiniMap() and hooked mapOffset
  • Loading branch information
KronosDesign authored and Adam- committed Apr 15, 2017
1 parent 0c8ea5d commit 8c6bc96
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 45 deletions.
41 changes: 6 additions & 35 deletions runelite-api/src/main/java/net/runelite/api/Actor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,43 +137,16 @@ public int getModelHeight()

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();

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 Perspective.getCanvasTextLocation(client, graphics, getLocalLocation(), text, zOffset);
}

return new Point(xOffset, p.getY());
public Point getMinimapLocation()
{
return Perspective.worldToMiniMap(client, getX(), getY());
}
}
20 changes: 20 additions & 0 deletions runelite-api/src/main/java/net/runelite/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,26 @@ public int[] getWidgetPositionsY()
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();
Expand Down
122 changes: 122 additions & 0 deletions runelite-api/src/main/java/net/runelite/api/Perspective.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -107,6 +112,62 @@ public static Point worldToCanvas(Client client, int x, int y, int plane, int zO

}

/**
* 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.
*
Expand Down Expand Up @@ -159,4 +220,65 @@ public static Point localToWorld(Client client, Point point)
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());
}

}
18 changes: 18 additions & 0 deletions runelite-api/src/main/java/net/runelite/api/TileObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/
package net.runelite.api;

import java.awt.Graphics2D;
import java.awt.Polygon;

public abstract class TileObject
{
protected final Client client;
Expand Down Expand Up @@ -61,4 +64,19 @@ public Point getCanvasLocation()
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());
}
}
8 changes: 3 additions & 5 deletions runescape-api/src/main/java/net/runelite/rs/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* (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.rs.api;

import java.awt.Canvas;
Expand Down Expand Up @@ -60,6 +59,9 @@ public interface Client extends GameEngine
@Import("mapAngle")
int getMapAngle();

@Import("mapOffset")
int getMapOffset();

@Import("tileHeights")
int[][][] getTileHeights();

Expand Down Expand Up @@ -156,13 +158,9 @@ public interface Client extends GameEngine
@Import("sendGameMessage")
void sendGameMessage(int var1, String var2, String var3);

//void hopToWorld(String var1, int var2, int var3);

@Import("objectDefinition")
ObjectComposition getObjectDefinition(int var1);

//void setScale(int var1);

@Import("scale")
int getScale();

Expand Down
11 changes: 6 additions & 5 deletions runescape-client/src/main/java/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ public final class Client extends GameEngine {
@ObfuscatedGetter(
intValue = -502197487
)
static int field374;
@Export("mapOffset")
static int mapOffset;
@ObfuscatedName("eq")
@ObfuscatedGetter(
intValue = -314507465
Expand Down Expand Up @@ -3130,7 +3131,7 @@ protected final void packetHandler() {
field548 = 0;
var12 = (int)(Math.random() * 8.0D);
if((var12 & 1) == 1) {
mapScale += field374;
mapScale += mapOffset;
}

if((var12 & 2) == 2) {
Expand All @@ -3139,11 +3140,11 @@ protected final void packetHandler() {
}

if(mapScale < -60) {
field374 = 2;
mapOffset = 2;
}

if(mapScale > 60) {
field374 = -2;
mapOffset = -2;
}

if(mapScaleDelta < -20) {
Expand Down Expand Up @@ -3746,7 +3747,7 @@ public static void method557(IndexDataBase var0) {
field371 = 1;
field372 = 0;
mapScale = 0;
field374 = 2;
mapOffset = 2;
mapScaleDelta = 0;
field376 = 1;
field548 = 0;
Expand Down

0 comments on commit 8c6bc96

Please sign in to comment.