Brainfree Posted May 16, 2013 Share Posted May 16, 2013 (edited) This piece of software will tell you all Entity by a selected set of types, at a given distance, that you are facing. This has uses to tell what Entity's you can interact with (without changing positions, or direction), or are about to interact with. You could also pair it with animations to tell if a player is interacting with it, or yourself (ex. prioritizing ores by filtering 'currently being mined' ore, same with trees). By mainly this was made to tell what tree your were facing, and decide if it's a evil tree, or not (Wiz). Real use example: Example of use is within the code. import org.osbot.script.Script; import org.osbot.script.rs2.Client; import org.osbot.script.rs2.map.Position; import org.osbot.script.rs2.model.Entity; import org.osbot.script.rs2.model.Player; import java.util.ArrayDeque; import java.util.EnumSet; import java.util.List; /** * @author Brainfree */ public class FacingEntities extends Script { public enum DirectionalOrientation { NORTH_WEST(-1, 1, 135), NORTH(0, 1, 90), NORTH_EAST(1, 1, 45), EAST(1, 0, 0), SOUTH_EAST(1, -1, 315), SOUTH(0, -1, 270), SOUTH_WEST(-1, -1, 225), WEST(-1, 0, 180); public final int x_shift, y_shift, angle; DirectionalOrientation(int x_shift, int y_shift, int angle) { this.x_shift = x_shift; this.y_shift = y_shift; this.angle = angle; } } public Position getFacingPosition(Player player, int distance) { int angle_shift = (player.getRotation() / 0x100) * 45; angle_shift -= 270; if (angle_shift > 0) angle_shift += 270; angle_shift = Math.abs(angle_shift); for (DirectionalOrientation orientation : DirectionalOrientation.values()) { if (orientation.angle == angle_shift) { return new Position( player.getX() + (distance * orientation.x_shift), player.getY() + (distance * orientation.y_shift), player.getZ() ); } } return null; } public enum EntityType { OBJECT { @Override public List<? extends Entity> getType(Client client) { return client.getCurrentRegion().getObjects(); } }, NPC { @Override public List<? extends Entity> getType(Client client) { return client.getLocalNPCs(); } }, PLAYER { @Override public List<? extends Entity> getType(Client client) { return client.getLocalPlayers(); } }, GROUND_ITEM { @Override public List<? extends Entity> getType(Client client) { return client.getCurrentRegion().getItems(); } }; public abstract List<? extends Entity> getType(Client client); } public Entity[] getFacingEntities(Client client, Player player, int distanceInTiles, EnumSet<EntityType> checkFor) { if (checkFor == null || checkFor.size() == 0) return new Entity[0]; final Position facing = getFacingPosition(player, distanceInTiles); if (facing == null) return new Entity[0]; ArrayDeque<Entity> pool = new ArrayDeque<Entity>(); for (EntityType type : checkFor) { for (Entity node : type.getType(client)) { if (node != null && node.getX() == facing.getX() && node.getY() == facing.getY() && node.getZ() == facing.getZ()) { pool.add(node); } } } return pool.toArray(new Entity[pool.size()]); } public int onLoop() { Entity[] entitiesImFacingByMySelectedDistance = getFacingEntities( client, myPlayer(), 1, EnumSet.of(EntityType.NPC)); for(Entity cur : entitiesImFacingByMySelectedDistance) { if(cur.getName() != null && cur.getName() == "Tree") { System.out.println("I'm facing a tree, but possibly not interacting with it."); } } return 200; } } This part will tell ALL entities at a given distance away from player, so if the entities was less than, or equal to the distance you desired, and your facing it, it'll be added into the return. import org.osbot.script.Script; import org.osbot.script.ScriptManifest; import org.osbot.script.rs2.Client; import org.osbot.script.rs2.map.Position; import org.osbot.script.rs2.model.Entity; import org.osbot.script.rs2.model.NPC; import org.osbot.script.rs2.model.Player; import java.awt.*; import java.util.ArrayDeque; import java.util.EnumSet; import java.util.List; /** * @author Brainfree */ @ScriptManifest(author = "Brainfree", version = 1.0, info = "Tells all entities you're facing, within a given distance.", name = "FutureViewer") public class AllFacingEntities extends Script { public enum DirectionalOrientation { NORTH_WEST(-1, 1, 135), NORTH(0, 1, 90), NORTH_EAST(1, 1, 45), EAST(1, 0, 0), SOUTH_EAST(1, -1, 315), SOUTH(0, -1, 270), SOUTH_WEST(-1, -1, 225), WEST(-1, 0, 180); public final int x_shift, y_shift, angle; DirectionalOrientation(int x_shift, int y_shift, int angle) { this.x_shift = x_shift; this.y_shift = y_shift; this.angle = angle; } } enum EntityType { OBJECT { @Override public List<? extends Entity> getType(Client client) { return client.getCurrentRegion().getObjects(); } }, NPC { @Override public List<? extends Entity> getType(Client client) { return client.getLocalNPCs(); } }, PLAYER { @Override public List<? extends Entity> getType(Client client) { return client.getLocalPlayers(); } }, GROUND_ITEM { @Override public List<? extends Entity> getType(Client client) { return client.getCurrentRegion().getItems(); } }; public abstract List<? extends Entity> getType(Client client); } public Position getFacingPosition(Player player) { int angle_shift = (player.getRotation() / 0x100) * 45; angle_shift -= 270; if (angle_shift > 0) angle_shift += 270; angle_shift = Math.abs(angle_shift) + 180; if(angle_shift >= 360) angle_shift-= 360; for (DirectionalOrientation orientation : DirectionalOrientation.values()) { if (orientation.angle == angle_shift) { return new Position( player.getX() + orientation.x_shift, player.getY() + orientation.y_shift, player.getZ() ); } } return null; } public static boolean lineContainsPoint(double Ax, double Ay, double Bx, double By, double Px, double Py) { double cross_product = (Py - Ay) * (Bx - Ax) - (Px - Ax) * (By - Ay); double dotproduct = (Px - Ax) * (Bx - Ax) + (Py - Ay) * (By - Ay); return Math.abs((cross_product)) == 0 && dotproduct < 0; } public Entity[] getFacingEntities(Client client, Player player, int distanceInTiles, EnumSet<EntityType> checkFor) { if (checkFor == null || checkFor.size() == 0) return new Entity[0]; final Position facing = getFacingPosition(player); final Position me = client.getMyPlayer().getPosition(); if (facing == null) return new Entity[0]; ArrayDeque<Entity> pool = new ArrayDeque<Entity>(); for (EntityType type : checkFor) { for (Entity node : type.getType(client)) { if (node != null && lineContainsPoint( me.getX(), me.getY(), facing.getX(), facing.getY(), node.getX(), node.getY()) && Math.hypot(node.getX() - me.getX(), node.getY() - me.getY() ) <= distanceInTiles) { pool.add(node); } } } return pool.toArray(new Entity[pool.size()]); } public int onLoop() { Entity[] AllEntitiesImFacingWithinADistanceOf3 = getFacingEntities(client, myPlayer(), 3, EnumSet.of( EntityType.OBJECT, EntityType.NPC)); for(Entity whatImFacing : AllEntitiesImFacingWithinADistanceOf3) { if(whatImFacing instanceof NPC && whatImFacing.getName() == "Banker tutor") { System.out.println("I'm facing a banker tutor :}, she could be 3 tiles away, or even less >_>"); } } return 500; } public void onPaint(Graphics graphics) { Entity[] AllEntitiesImFacingWithinADistanceOf3 = getFacingEntities(client, myPlayer(), 3, EnumSet.of( EntityType.OBJECT, EntityType.NPC)); for(Entity whatImFacing : AllEntitiesImFacingWithinADistanceOf3) { ((Graphics2D) graphics).draw(whatImFacing.getMouseDestination().getArea()); } } } Hope this helps. /Updated, forgot to implement the distance into it. /Updated, as requested by wiz, added code for all facing entities, within a given distance. Edited May 19, 2013 by Brainfree 2 Link to comment Share on other sites More sharing options...
Wizard Posted May 16, 2013 Share Posted May 16, 2013 Thank you, the exact thing i needed Link to comment Share on other sites More sharing options...
iGotLube Posted May 16, 2013 Share Posted May 16, 2013 Thanks for this mate! Link to comment Share on other sites More sharing options...
Brainfree Posted May 19, 2013 Author Share Posted May 19, 2013 Updated to show all facing entities within a given distance. Link to comment Share on other sites More sharing options...