House Posted September 22, 2016 Share Posted September 22, 2016 (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); area = generateAreaRelativeToObject(altar, Direction.EAST, 1, 0); area = generateAreaRelativeToObject(altar, Direction.SOUTH, 2, 2); area = generateAreaRelativeToObject(cart, Direction.EAST, 1, 0); [Works with any size object] Edited September 22, 2016 by House 8 Quote Link to comment Share on other sites More sharing options...
Mr Pro Pop Posted September 26, 2016 Share Posted September 26, 2016 Nice one bro, Hope to see more from you soon! Quote Link to comment Share on other sites More sharing options...
TheGreatests Posted October 25, 2016 Share Posted October 25, 2016 How do you implement this into a constants class? or without having to add the code to a node. Quote Link to comment Share on other sites More sharing options...
TheGreatests Posted October 26, 2016 Share Posted October 26, 2016 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); area = generateAreaRelativeToObject(altar, Direction.EAST, 1, 0); area = generateAreaRelativeToObject(altar, Direction.SOUTH, 2, 2); area = generateAreaRelativeToObject(cart, Direction.EAST, 1, 0); [Works with any size object] Can you tell me how to implement it without having to have the code in the class itself? Quote Link to comment Share on other sites More sharing options...
venetox Posted October 26, 2016 Share Posted October 26, 2016 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. Quote Link to comment Share on other sites More sharing options...
TheGreatests Posted October 26, 2016 Share Posted October 26, 2016 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? Quote Link to comment Share on other sites More sharing options...
venetox Posted October 26, 2016 Share Posted October 26, 2016 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? 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. 1 Quote Link to comment Share on other sites More sharing options...