Jump to content

Generating an area next to an RS2Object with variable size


House

Recommended Posts

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
Link to comment
Share on other sites

  • 5 weeks later...

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
Link to comment
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.

  • Recently Browsing   0 members

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