Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Buy OSRS Gold

Sell OSRS Gold
Sign in to follow this  
orange451

AreaBounds - Get area information containing walkable/nonwalkable tiles

Recommended Posts

Posted (edited)

Here's a small utility I've created when working on a rooftop agility script. It scans a position for all nearby walkable tiles and non-walkable tiles.

Kxa5dun.png

 

Source:

package scripts.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.tribot.api.interfaces.Positionable;
import org.tribot.api2007.PathFinding;
import org.tribot.api2007.Projection;
import org.tribot.api2007.types.RSTile;

public class AreaBounds {
	/** List of walkable tiles */
	private RSTile[] walkableTiles;
	
	/** List of nonwalkable tiles */
	private RSTile[] nonWalkableTiles;
	
	/** Root tile used to create AreaBounds */
	private RSTile root;
	
	/** Internal tile lookup map */
	private Map<RSTile, Boolean> tileLookup;
	
	/** Minimum tile */
	private RSTile minTile;
	
	/** Maximum tile */
	private RSTile maxTile;
	
	/** Max distance area will scan for tiles from root */
	private final static int MAX_DISTANCE = 24;
	
	public AreaBounds(Positionable position) {
		RSTile start = position.getPosition();

		root = start;
		tileLookup = new HashMap<>();
		
		// Compute all tiles
		List<RSTile> walkable = new ArrayList<>();
		List<RSTile> nonWalkable = new ArrayList<>();
		fillTiles(start, walkable, nonWalkable);

		// Find min/max files
		int minX,minY;
		int maxX,maxY;
		minX = minY = Integer.MAX_VALUE;
		maxX = maxY = Integer.MIN_VALUE;
		for (RSTile tile : tileLookup.keySet()) {
			if ( tile.getX() < minX )
				minX = tile.getX();
			if ( tile.getY() < minY )
				minY = tile.getY();
			if ( tile.getX() > maxX )
				maxX = tile.getX();
			if ( tile.getY() > maxY )
				maxY = tile.getY();
		}
		minTile = new RSTile(minX, minY, root.getPlane());
		maxTile = new RSTile(maxX, maxY, root.getPlane());
		
		// Update public tile info
		this.walkableTiles = walkable.toArray(new RSTile[walkable.size()]);
		this.nonWalkableTiles = nonWalkable.toArray(new RSTile[nonWalkable.size()]);
	}
	
	private void fillTiles(RSTile tile, List<RSTile> walkable, List<RSTile> nonWalkable) {
		if ( tile == null )
			return;
		
		if ( tile.distanceTo(root) > MAX_DISTANCE )
			return;
		
		Boolean isWalkable = tileLookup.get(tile);
		if ( isWalkable == null )
			isWalkable = PathFinding.isTileWalkable(tile);
		
		if ( !tileLookup.containsKey(tile) )
			if ( isWalkable )
				walkable.add(tile);
			else
				nonWalkable.add(tile);
		else
			return;
		
		tileLookup.put(tile, isWalkable);
		
		if ( isWalkable ) {
			fillTiles(offset(tile, -1, 0), walkable, nonWalkable);
			fillTiles(offset(tile, 1, 0), walkable, nonWalkable);
			fillTiles(offset(tile, 0, 1), walkable, nonWalkable);
			fillTiles(offset(tile, 0, -1), walkable, nonWalkable);
		}
	}
	
	private RSTile offset(RSTile tile, int x, int y) {
		return new RSTile(tile.getX() + x, tile.getY() + y, tile.getPlane(), tile.getType());
	}

	/**
	 * Returns a list of walkable tiles in this area.
	 * @return
	 */
	public RSTile[] getWalkableTiles() {
		return walkableTiles;
	}
	
	/**
	 * Returns a list of non-walkable tiles in this area.
	 * @return
	 */
	public RSTile[] getNonWalkableTiles() {
		return nonWalkableTiles;
	}
	
	/**
	 * Returns whether a tile is inside this area bounds.
	 * @param positionable
	 * @return
	 */
	public boolean contains(Positionable positionable) {
		RSTile position = positionable.getPosition();
		return tileLookup.containsKey(position);
	}
	
	/**
	 * Returns whether a tile is touching the perimeter of the bounds.
	 * @param positionable
	 * @return
	 */
	public boolean isTouching(Positionable positionable) {
		RSTile nonwalk = chestAdjacentNonWalkableTile(positionable.getPosition());
		if ( nonwalk == null )
			nonwalk = checkAdjacentWalkableTile(positionable.getPosition());
			
		return nonwalk != null;
	}
	
	/**
	 * Returns the overall amount of tiles within this area.
	 * @return
	 */
	public int size() {
		return tileLookup.size();
	}
	
	public void debugPaint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		
		for (RSTile tile : getWalkableTiles()) {
			if (tile.isOnScreen()) {
				g2d.setColor(new Color(0.1f, 0.8f, 0.1f, 0.2f));
				g2d.fill(Projection.getTileBoundsPoly(tile.getPosition(), 0));
				g2d.setColor(new Color(0.1f, 0.8f, 0.1f, 0.66f));
				g2d.draw(Projection.getTileBoundsPoly(tile.getPosition(), 0));
			}
		}
		
		for (RSTile tile : getNonWalkableTiles()) {
			if (tile.isOnScreen()) {
				g2d.setColor(new Color(0.8f, 0.1f, 0.1f, 0.2f));
				g2d.fill(Projection.getTileBoundsPoly(tile.getPosition(), 0));
				g2d.setColor(new Color(0.8f, 0.1f, 0.1f, 0.66f));
				g2d.draw(Projection.getTileBoundsPoly(tile.getPosition(), 0));
			}
		}
	}

	/**
	 * Returns the tile that was used to build this AreaBounds originally.
	 * @return
	 */
	public RSTile getRoot() {
		return this.root;
	}

	/**
	 * Return the nearest walkable tile to a given RSTile within the area.
	 * @param position
	 * @return
	 */
	public RSTile checkAdjacentWalkableTile(RSTile position) {
		if ( position == null )
			return null;
			
		if ( tileLookup.get(position) != null && tileLookup.get(position).booleanValue() )
			return position;
		
		RSTile t1 = offset(position, -1, 0);
		if ( tileLookup.get(t1) != null && tileLookup.get(t1).booleanValue() )
			return t1;
		
		RSTile t2 = offset(position, 1, 0);
		if ( tileLookup.get(t2) != null && tileLookup.get(t2).booleanValue() )
			return t2;
		
		RSTile t3 = offset(position, 0, -1);
		if ( tileLookup.get(t3) != null && tileLookup.get(t3).booleanValue() )
			return t3;
		
		RSTile t4 = offset(position, 0, 1);
		if ( tileLookup.get(t4) != null && tileLookup.get(t4).booleanValue() )
			return t4;
		
		return null;
	}

	/**
	 * Returns the nearest nonwalkable RSTile within the area.
	 * @param position
	 * @return
	 */
	public RSTile chestAdjacentNonWalkableTile(RSTile position) {
		if ( position == null )
			return null;
			
		if ( tileLookup.get(position) != null && !tileLookup.get(position).booleanValue() )
			return position;
		
		RSTile t1 = offset(position, -1, 0);
		if ( tileLookup.get(t1) != null && !tileLookup.get(t1).booleanValue() )
			return t1;
		
		RSTile t2 = offset(position, 1, 0);
		if ( tileLookup.get(t2) != null && !tileLookup.get(t2).booleanValue() )
			return t2;
		
		RSTile t3 = offset(position, 0, -1);
		if ( tileLookup.get(t3) != null && !tileLookup.get(t3).booleanValue() )
			return t3;
		
		RSTile t4 = offset(position, 0, 1);
		if ( tileLookup.get(t4) != null && !tileLookup.get(t4).booleanValue() )
			return t4;
		
		return null;
	}
	
	@Override
	public boolean equals(Object object) {
		if ( object == null )
			return false;
		
		if (!(object instanceof AreaBounds))
			return false;
		
		AreaBounds bounds = (AreaBounds)object;
		
		return this.minTile.equals(bounds.minTile)
				&& this.maxTile.equals(bounds.maxTile)
				&& this.size() == bounds.size();
	}
}

 

Edited by orange451

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Our picks

    • Over the past few months, I’ve been working diligently on a new project - TRiBot X. Everything has been written from the ground up, with all of the best practices of software engineering. Every aspect of TRiBot has been re-imagined to support three main goals: flexibility, useability, and reliability.
        • Like
      • 30 replies
    • Come give us feedback on the next version of TRiBot!
        • Thanks
        • Like
      • 74 replies
    • TRiBot is looking to improve a lot of its customer relationship management, customer on boarding process, customer experience, design elements, community engagement and pretty much everything else you can imagine when it comes to marketing.

      Our goal: To ensure that the marketing done TRULY reflects the experience and does not shine an inaccurate light on what TRiBot is lacking in.

      So I ask, what do you love about TRiBot and what do you hate about TRiBot? What does O S Bot, Rune M8, PowR Bot and Dre amBot do better? (yes I purposely didn't spell it right 😂).

      Love, 

      RileyZ
        • Like
      • 23 replies
    • Over the last three weeks, I've been working on upgrading our server infrastructure. It's finally ready and is now live!

      Why?

      Increased reliability - less server errors


      Increased availability - less downtime


      Increased security - keeping us and you secure


      Increased capacity - ability to serve you better


      Increased speed - less waiting for things to load


      Faster development - server and service updates will come faster


      What are the changes?

      Move from a single AWS EC2 instance to AWS ECS (Elastic Container Service)


      Distributed computing


      Load balancing


      Git management of server files and filesystem


      Redis caching


      How?

      AWS ECS (with 10 EC2 instances)


      AWS ElastiCache (Redis)


      AWS Load Balancing


      AWS EFS (Elastic file system)


      Please bare with us as I continue to tune the server for maximum performance. Slow loading speeds may occur temporarily. I thank everyone for their patience.

      Please post on this thread if you experience any issues other than slow loading times.
        • Like
      • 51 replies
    • This release will:

      Fix prayers and world hopper API (Thanks @JoeDezzy1 and @erickho123)


      Improve banking API (Thanks @Encoded)


      Adds methods for returning and using Java Lists, rather than arrays


      Slightly randomizes some hardcoded behaviour


      Removes sleeps from waitConditions; the efficiency saving potential is negligible in these use-cases, therefore cleaner code is preferable


      Other back-end improvements





      Note: If you are using LG, please restart both the RS client and TRiBot.
        • Sad
        • Haha
        • Thanks
        • Like
      • 90 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...