Butters Posted November 28, 2016 Share Posted November 28, 2016 I'm having trouble finding what position on RS map did my mouse click. I've created my own MouseListener class and it catches the mouse click event perfectly. But how can I convert my mouse coordinates during the click to, lets say, a Position on RS map? Quote Link to comment Share on other sites More sharing options...
Rudie Posted November 28, 2016 Share Posted November 28, 2016 Can you explain why you need this? Quote Link to comment Share on other sites More sharing options...
Butters Posted November 28, 2016 Author Share Posted November 28, 2016 Can you explain why you need this? I'm making a mining script and I'm storing the current rock that is mined, so that I could later check if it still contains ore (to stop animating and go to next rock). The problem is when I enable input and click on a random rock myself the script stops working after the rock has been mined. I want to determine on what rock (or on what location) I've clicked and do something accordingly. Quote Link to comment Share on other sites More sharing options...
House Posted November 28, 2016 Share Posted November 28, 2016 I'm making a mining script and I'm storing the current rock that is mined, so that I could later check if it still contains ore (to stop animating and go to next rock). The problem is when I enable input and click on a random rock myself the script stops working after the rock has been mined. I want to determine on what rock (or on what location) I've clicked and do something accordingly. When you interact with the rock you have an instance of the object. You can store the position and see if the rock is still a valid mineable rock? Quote Link to comment Share on other sites More sharing options...
Butters Posted November 28, 2016 Author Share Posted November 28, 2016 When you interact with the rock you have an instance of the object. You can store the position and see if the rock is still a valid mineable rock? Nah, that doesn't work or I don't know how to check that. The object that I've interacted with somehow is still visible and existing. The new no-ore rock is a new object with a different ID. So what I do is I store the objects' that I've interacted with position and then scan for rocks that contain no ore. If the rocks' with no ore position matches the the rocks' position that I'm interacting then I now that the rock was mined. Quote Link to comment Share on other sites More sharing options...
House Posted November 28, 2016 Share Posted November 28, 2016 Nah, that doesn't work or I don't know how to check that. The object that I've interacted with somehow is still visible and existing. The new no-ore rock is a new object with a different ID. So what I do is I store the objects' that I've interacted with position and then scan for rocks that contain no ore. If the rocks' with no ore position matches the the rocks' position that I'm interacting then I now that the rock was mined. You need to regrab the object on the position you store. You do not store the RS2Object. you can get an updated version of the current object on a tile using Objects.get(x,y).get(0); 2 Quote Link to comment Share on other sites More sharing options...
Butters Posted November 28, 2016 Author Share Posted November 28, 2016 You need to regrab the object on the position you store. You do not store the RS2Object. you can get an updated version of the current object on a tile using Objects.get(x,y).get(0); Thanks. Will make the code much cleaner. But what does the get(0) do? And back to the original question. The mouse class has a method getPosition(), but I'm pretty sure it returns the coordinates of your window. Any way to get a runescape map position from those mouse coordinates? Quote Link to comment Share on other sites More sharing options...
House Posted November 28, 2016 Share Posted November 28, 2016 (edited) Thanks. Will make the code much cleaner. But what does the get(0) do? Objects.get() returns an array of objects at the position, .get(0) returns the first one and usually there is only one objects per tile. And back to the original question. The mouse class has a method getPosition(), but I'm pretty sure it returns the coordinates of your window. Any way to get a runescape map position from those mouse coordinates? You could take an area around the player in a eg 10x10 grid and check if the mouse click or press event's .getPoint() is within the bounds of a tile. (Do not check every tile loaded, 10x10 is already a significant load) Idk if this is the best way to approach it but i know it works Useful snippets: ArrayList<Position> nearby_positions = new ArrayList<Position>(); private void updateNearbyPositions(int scan_radius) { Position my_position = myPosition(); int start_x = my_position.getX() - scan_radius; int start_y = my_position.getY() - scan_radius; int end_x = my_position.getX() + scan_radius; int end_y = my_position.getY() + scan_radius; for (int xx = start_x; xx <= end_x; xx++) { for (int yy = start_y; yy <= end_y; yy++) { Position pp = new Position(xx, yy, my_position.getZ()); nearby_positions.add(pp); } } } @[member=Override] public void mouseClicked(MouseEvent e) { for (Position p : nearby_positions) { if (p.isVisible(getBot()) && p.getPolygon(getBot()).contains(e.getPoint())) { if (mining_rock_positions.containsKey(p)) { mining_rock_positions.remove(p); e.consume(); } else if (!mining_rock_positions.containsKey(p)) { RS2Object rock = getObjects().get(p.getX(), p.getY()).get(0); if (rock != null) { mining_rock_positions.put(p, rock.getId()); e.consume(); } } } } } Apologies for the structure, its a pretty old code of mine Edited November 28, 2016 by House Quote Link to comment Share on other sites More sharing options...
Butters Posted November 28, 2016 Author Share Posted November 28, 2016 Objects.get() returns an array of objects at the position, .get(0) returns the first one and usually there is only one objects per tile. You could take an area around the player in a eg 10x10 grid and check if the mouse click or press event's .getPoint() is within the bounds of a tile. (Do not check every tile loaded, 10x10 is already a significant load) Idk if this is the best way to approach it but i know it works Useful snippets: ArrayList<Position> nearby_positions = new ArrayList<Position>(); private void updateNearbyPositions(int scan_radius) { Position my_position = myPosition(); int start_x = my_position.getX() - scan_radius; int start_y = my_position.getY() - scan_radius; int end_x = my_position.getX() + scan_radius; int end_y = my_position.getY() + scan_radius; for (int xx = start_x; xx <= end_x; xx++) { for (int yy = start_y; yy <= end_y; yy++) { Position pp = new Position(xx, yy, my_position.getZ()); nearby_positions.add(pp); } } } @[member='Override'] public void mouseClicked(MouseEvent e) { for (Position p : nearby_positions) { if (p.isVisible(getBot()) && p.getPolygon(getBot()).contains(e.getPoint())) { if (mining_rock_positions.containsKey(p)) { mining_rock_positions.remove(p); e.consume(); } else if (!mining_rock_positions.containsKey(p)) { RS2Object rock = getObjects().get(p.getX(), p.getY()).get(0); if (rock != null) { mining_rock_positions.put(p, rock.getId()); e.consume(); } } } } } Apologies for the structure, its a pretty old code of mine Thanks! Extremely useful. Just to clarify on this bit: if (mining_rock_positions.containsKey(p)) { mining_rock_positions.remove(p); e.consume(); } else if (!mining_rock_positions.containsKey(p)) { RS2Object rock = getObjects().get(p.getX(), p.getY()).get(0); if (rock != null) { mining_rock_positions.put(p, rock.getId()); e.consume(); } } You populate a HashMap of rock positions in the area where you are and then add/remove the rocks with ores in the list? Though this snippet is only for updating the rock map that the user manually clicked on, cause I believe mouseListener does not catch client "mouse clicks". Am I right? Quote Link to comment Share on other sites More sharing options...
House Posted November 28, 2016 Share Posted November 28, 2016 You populate a HashMap of rock positions in the area where you are and then add/remove the rocks with ores in the list? Though this snippet is only for updating the rock map that the user manually clicked on, cause I believe mouseListener does not catch client "mouse clicks". Am I right? Im not exactly sure what you are asking sorry lol. It stores the rocks that are clicked in a hashmap with the key being the rocks position and the value being the rocks id at the selection time (the rock is selected when it has ore in it) And if i understand what you are saying then why are you trying to listen to mouse clicks by the client mouse method. All you need is the rs2object it is interacting with (clicking) to know the position the script "clicked on". Quote Link to comment Share on other sites More sharing options...