Jump to content

Generating an area next to an RS2Object with variable size


Recommended Posts

Posted (edited)

What does this code do? It generates an area with specified dimensions spread outward.

Why is it useful? can be used to generate locations to walk to near an object.

 

Code:

public enum Direction {
	NORTH, EAST, SOUTH, WEST;
}
	public Area generateAreaRelativeToObject(RS2Object origin_object, Direction direction, int width_outwards, int height_outwards) {

		ArrayList<Position> area_tiles = new ArrayList<Position>();
		Position origin_object_position = origin_object.getPosition();

		int x_min = 0;
		int x_max = 0;

		int y_min = 0;
		int y_max = 0;

		switch (direction) {

		case NORTH:
			x_min = origin_object_position.getX() - width_outwards;
			x_max = origin_object_position.getX() + origin_object.getSizeX() - 1 + width_outwards;
			y_min = origin_object_position.getY() + origin_object.getSizeY();
			y_max = y_min + height_outwards - 1;
			break;

		case EAST:
			x_min = origin_object_position.getX() + origin_object.getSizeX();
			x_max = x_min + width_outwards - 1;
			y_min = origin_object_position.getY() - height_outwards - 1;
			y_max = origin_object_position.getY() + origin_object.getSizeY() - 1 + height_outwards;
			break;

		case SOUTH:
			x_min = origin_object_position.getX() - width_outwards;
			x_max = origin_object_position.getX() + origin_object.getSizeX() - 1 + width_outwards;
			y_min = origin_object_position.getY() - height_outwards;
			y_max = origin_object_position.getY() - 1;
			break;

		case WEST:
			x_min = origin_object_position.getX() - width_outwards;
			x_max = origin_object_position.getX() - 1;
			y_min = origin_object_position.getY() - height_outwards;
			y_max = origin_object_position.getY() + origin_object.getSizeY() - 1 + height_outwards;
			break;

		}

		for (int x = x_min; x <= x_max; x++) {
			for (int y = y_min; y <= y_max; y++) {
				area_tiles.add(new Position(x, y, origin_object_position.getZ()));
			}
		}

		Position top_left_position = null;
		Position bottom_right_position = null;

		for (Position p : area_tiles) {

			if (top_left_position == null) {
				top_left_position = p;
			} else {
				if (p.getX() < top_left_position.getX())
					top_left_position = p;
				if (p.getY() > top_left_position.getY())
					top_left_position = p;

				if (bottom_right_position == null) {
					bottom_right_position = p;
				}

				if (p.getX() > bottom_right_position.getX())
					bottom_right_position = p;
				if (p.getY() < bottom_right_position.getY())
					bottom_right_position = p;
			}
		}

		return new Area(top_left_position, bottom_right_position);
	}

 

Examples:

area = generateAreaRelativeToObject(altar, Direction.SOUTH, 0, 1); 

d703028cce7f604e11b59bcaf2282a40.png

area = generateAreaRelativeToObject(altar, Direction.EAST, 1, 0);

7adbc2d5b3d9206e251263be19ea086f.png

area = generateAreaRelativeToObject(altar, Direction.SOUTH, 2, 2);

e19c2aaa68c592b4c0768764e51dc917.png

area = generateAreaRelativeToObject(cart, Direction.EAST, 1, 0); 

[Works with any size object]

32f54eae6f857073825fcf945ebd20e8.png

 

 

Edited by House
  • Like 8
  • 5 weeks later...
Posted

What does this code do? It generates an area with specified dimensions spread outward.

Why is it useful? can be used to generate locations to walk to near an object.

 

Code:

public enum Direction {
	NORTH, EAST, SOUTH, WEST;
}
	public Area generateAreaRelativeToObject(RS2Object origin_object, Direction direction, int width_outwards, int height_outwards) {

		ArrayList<Position> area_tiles = new ArrayList<Position>();
		Position origin_object_position = origin_object.getPosition();

		int x_min = 0;
		int x_max = 0;

		int y_min = 0;
		int y_max = 0;

		switch (direction) {

		case NORTH:
			x_min = origin_object_position.getX() - width_outwards;
			x_max = origin_object_position.getX() + origin_object.getSizeX() - 1 + width_outwards;
			y_min = origin_object_position.getY() + origin_object.getSizeY();
			y_max = y_min + height_outwards - 1;
			break;

		case EAST:
			x_min = origin_object_position.getX() + origin_object.getSizeX();
			x_max = x_min + width_outwards - 1;
			y_min = origin_object_position.getY() - height_outwards - 1;
			y_max = origin_object_position.getY() + origin_object.getSizeY() - 1 + height_outwards;
			break;

		case SOUTH:
			x_min = origin_object_position.getX() - width_outwards;
			x_max = origin_object_position.getX() + origin_object.getSizeX() - 1 + width_outwards;
			y_min = origin_object_position.getY() - height_outwards;
			y_max = origin_object_position.getY() - 1;
			break;

		case WEST:
			x_min = origin_object_position.getX() - width_outwards;
			x_max = origin_object_position.getX() - 1;
			y_min = origin_object_position.getY() - height_outwards;
			y_max = origin_object_position.getY() + origin_object.getSizeY() - 1 + height_outwards;
			break;

		}

		for (int x = x_min; x <= x_max; x++) {
			for (int y = y_min; y <= y_max; y++) {
				area_tiles.add(new Position(x, y, origin_object_position.getZ()));
			}
		}

		Position top_left_position = null;
		Position bottom_right_position = null;

		for (Position p : area_tiles) {

			if (top_left_position == null) {
				top_left_position = p;
			} else {
				if (p.getX() < top_left_position.getX())
					top_left_position = p;
				if (p.getY() > top_left_position.getY())
					top_left_position = p;

				if (bottom_right_position == null) {
					bottom_right_position = p;
				}

				if (p.getX() > bottom_right_position.getX())
					bottom_right_position = p;
				if (p.getY() < bottom_right_position.getY())
					bottom_right_position = p;
			}
		}

		return new Area(top_left_position, bottom_right_position);
	}

 

Examples:

area = generateAreaRelativeToObject(altar, Direction.SOUTH, 0, 1); 

d703028cce7f604e11b59bcaf2282a40.png

area = generateAreaRelativeToObject(altar, Direction.EAST, 1, 0);

7adbc2d5b3d9206e251263be19ea086f.png

area = generateAreaRelativeToObject(altar, Direction.SOUTH, 2, 2);

e19c2aaa68c592b4c0768764e51dc917.png

area = generateAreaRelativeToObject(cart, Direction.EAST, 1, 0); 

[Works with any size object]

32f54eae6f857073825fcf945ebd20e8.png

 

 

 

Can you tell me how to implement it without having to have the code in the class itself?

 

Posted

Can you tell me how to implement it without having to have the code in the class itself?

 

If you can't figure that out, your going to need to learn more about Java before you start working on scripts. Not meaning that in a harsh way whatsoever but things like that are very basic and you have to walk before you can run.

Posted

If you can't figure that out, your going to need to learn more about Java before you start working on scripts. Not meaning that in a harsh way whatsoever but things like that are very basic and you have to walk before you can run.

 

Ya weird thing is ive made several scripts already. Willing to tutor me alittle, i'll pay ya! :)

How do I call this class into another class though. its a Enum?

Posted

Ya weird thing is ive made several scripts already. Willing to tutor me alittle, i'll pay ya! smile.png

How do I call this class into another class though. its a Enum?

 

Read through this,

https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

It explains what an Enum is and how to use it in basic and more advanced ways.

Currently in the middle of studying for exams which are coming up in a week or two so couldn't tutor you atm, wouldn't need any payment if I did either. Mr Pro Pop has a thread regarding him tutoring people for free so check him out.

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...