Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.rsbot.script.wrappers;
- import org.rsbot.bot.Bot;
- import java.awt.*;
- import java.util.ArrayList;
- /**
- * Represents a shape made of Tiles.
- *
- * @author SpeedWing, Emeleo, Parnassian
- */
- public class Area {
- private final Polygon area;
- private final int plane;
- private final Tile[] tiles;
- /**
- * @param tiles An Array containing of <b>Tiles</b> forming a polygon shape.
- * @param plane The plane of the <b>Area</b>.
- */
- public Area(final Tile[] tiles, final int plane) {
- this.tiles = tiles;
- area = tileArrayToPolygon(this.tiles);
- this.plane = plane;
- }
- /**
- * @param tiles An Array containing of <b>Tiles</b> forming a polygon shape.
- */
- public Area(final Tile[] tiles) {
- this(tiles, 0);
- }
- /**
- * @param sw The <i>South West</i> <b>Tile</b> of the <b>Area</b>
- * @param ne The <i>North East</i> <b>Tile</b> of the <b>Area</b>
- * @param plane The plane of the <b>Area</b>.
- */
- public Area(final Tile sw, final Tile ne, final int plane) {
- this(new Tile[]{sw, new Tile(ne.getX() + 1, sw.getY()),
- new Tile(ne.getX() + 1, ne.getY() + 1),
- new Tile(sw.getX(), ne.getY() + 1)}, plane);
- }
- /**
- * @param sw The <i>South West</i> <b>Tile</b> of the <b>Area</b>
- * @param ne The <i>North East</i> <b>Tile</b> of the <b>Area</b>
- */
- public Area(final Tile sw, final Tile ne) {
- this(sw, ne, 0);
- }
- /**
- * @param swX The X axle of the <i>South West</i> <b>Tile</b> of the
- * <b>Area</b>
- * @param swY The Y axle of the <i>South West</i> <b>Tile</b> of the
- * <b>Area</b>
- * @param neX The X axle of the <i>North East</i> <b>Tile</b> of the
- * <b>Area</b>
- * @param neY The Y axle of the <i>North East</i> <b>Tile</b> of the
- * <b>Area</b>
- */
- public Area(final int swX, final int swY, final int neX, final int neY) {
- this(new Tile(swX, swY), new Tile(neX, neY), 0);
- }
- /**
- * @param x The x location of the <b>Tile</b> that will be checked.
- * @param y The y location of the <b>Tile</b> that will be checked.
- * @return True if the <b>Area</b> contains the given <b>Tile</b>.
- */
- public boolean contains(final int x, final int y) {
- return this.contains(new Tile(x, y));
- }
- /**
- * @param plane The plane to check.
- * @param tiles The <b>Tile(s)</b> that will be checked.
- * @return True if the <b>Area</b> contains the given <b>Tile(s)</b>.
- */
- public boolean contains(final int plane, final Tile... tiles) {
- return this.plane == plane && this.contains(tiles);
- }
- /**
- * @param tiles The <b>Tile(s)</b> that will be checked.
- * @return True if the <b>Area</b> contains the given <b>Tile(s)</b>.
- */
- public boolean contains(final Tile... tiles) {
- final Tile[] areaTiles = getTileArray();
- for (final Tile check : tiles) {
- for (final Tile space : areaTiles) {
- if (check.equals(space)) {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * @return The bounding box of the <b>Area</b>.
- */
- public Rectangle getBounds() {
- return new Rectangle(area.getBounds().x + 1,
- area.getBounds().y + 1, getWidth(), getHeight());
- }
- /**
- * @return The central <b>Tile</b> of the <b>Area</b>.
- */
- public Tile getCentralTile() {
- if (area.npoints < 1) {
- return null;
- }
- int totalX = 0, totalY = 0;
- for (int i = 0; i < area.npoints; i++) {
- totalX += area.xpoints[i];
- totalY += area.ypoints[i];
- }
- return new Tile(Math.round(totalX / area.npoints),
- Math.round(totalY / area.npoints));
- }
- /**
- * @param base The base tile to measure the closest tile off of.
- * @return The nearest <b>Tile</b> in the <b>Area</b>
- * to the given <b>Tile</b>.
- */
- public Tile getNearestTile(final Tile base) {
- final Tile[] tiles = getTileArray();
- Tile cur = null;
- double dist = -1;
- for (final Tile tile : tiles) {
- final double distTmp = distanceBetween(tile, base);
- if (cur == null) {
- dist = distTmp;
- cur = tile;
- } else if (distTmp < dist) {
- cur = tile;
- dist = distTmp;
- }
- }
- return cur;
- }
- /**
- * @return The plane of the <b>Area</b>.
- */
- public int getPlane() {
- return plane;
- }
- /**
- * @return The <b>Tiles</b> the <b>Area</b> contains.
- */
- public Tile[] getTileArray() {
- final ArrayList<Tile> list = new ArrayList<Tile>();
- for (int x = getX(); x <= getX() + getWidth(); x++) {
- for (int y = getY(); y <= getY() + getHeight(); y++) {
- if (area.contains(x, y)) {
- list.add(new Tile(x, y, plane));
- }
- }
- }
- final Tile[] tiles = new Tile[list.size()];
- for (int i = 0; i < list.size(); i++) {
- tiles[i] = list.get(i);
- }
- return tiles;
- }
- /**
- * @return The <b>Tiles</b> the <b>Area</b> contains.
- */
- public Tile[][] getTiles() {
- final Tile[][] tiles = new Tile[getWidth()][getHeight()];
- for (int i = 0; i < getWidth(); ++i) {
- for (int j = 0; j < getHeight(); ++j) {
- if (area.contains(getX() + i, getY() + j)) {
- tiles[i][j] = new Tile(getX() + i, getY() + j);
- }
- }
- }
- return tiles;
- }
- /**
- * @return The distance between the the <b>Tile</b> that's most
- * <i>East</i> and the <b>Tile</b> that's most <i>West</i>.
- */
- public int getWidth() {
- return area.getBounds().width;
- }
- /**
- * @return The distance between the the <b>Tile</b> that's most
- * <i>South</i> and the <b>Tile</b> that's most <i>North</i>.
- */
- public int getHeight() {
- return area.getBounds().height;
- }
- /**
- * @return The X axle of the <b>Tile</b> that's most <i>West</i>.
- */
- public int getX() {
- return area.getBounds().x;
- }
- /**
- * @return The Y axle of the <b>Tile</b> that's most <i>South</i>.
- */
- public int getY() {
- return area.getBounds().y;
- }
- /**
- * @return An array of <b>Tile<b>'s that make the area.
- */
- public Tile[] getAreaTiles() {
- return this.tiles;
- }
- /**
- * Converts an shape made of <b>Tile</b> to a polygon.
- *
- * @param tiles The <b>Tile</b> of the Polygon.
- * @return The Polygon of the <b>Tile</b>.
- */
- private Polygon tileArrayToPolygon(final Tile[] tiles) {
- final Polygon poly = new Polygon();
- for (final Tile t : tiles) {
- poly.addPoint(t.getX(), t.getY());
- }
- return poly;
- }
- /**
- * @param curr first <b>Tile</b>
- * @param dest second <b>Tile</b>
- * @return the distance between the first and the second Tile
- */
- private double distanceBetween(final Tile curr, final Tile dest) {
- return Math.sqrt((curr.getX() - dest.getX())
- * (curr.getX() - dest.getX()) + (curr.getY() - dest.getY())
- * (curr.getY() - dest.getY()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement