Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Filter to Check if NPC in Array of Areas

Featured Replies

 

Good morning everyone. Just had a quick question if someone would like to help me out. I am trying to filter my NPC to only attack ones that are in my array of areas. I know how to do it if it was just a single area by doing

area.contains(f)

 but with an Array it is different. Here is my code I have

    Area[] CONTAINS_NPC_AREA = { CHICKENS,COWS,BARBARIANS,WARRIORS };

                    NPC npc = getNpcs().closest(f -> f != null && f.getName().equals(name) && map.canReach(f) && f.isAttackable() && f.getHealthPercent() > 0 && f.hasAction("Attack"));

I know how to check if myPlayer is in one of the Array of areas with this code. I tried to apply a similar method to my NPC filter, but could not get it working. 

 public boolean myPlayerInsideAreas(Area[] areas) {
        for(Area a: areas) {
            if(a.contains(myPlayer())) {
                return true;
            }
        }
        return false;
    }

Any help would be greatly appreciated :)

public boolean npcInsideAreas(Area[] areas,NPC n) {
        for(Area a: areas) {
            if(a.contains(n)) {
                return true;
            }
        }
        return false;
    }

Maybe this?

Why not just make an enum for the areas and map the available NPCs to them? You don't need to have a runtime calculation to determine what NPCs are in your area when you have prior knowledge.

    public enum NpcArea {
        CHICKENS("Chicken", new Area(x1, y1, x2, y2)),
        BARBARIANS("Barbarian", new Area(x1, y1, x2, y2));

        private final String npcName;
        private final Area area;

        NpcArea(String name, Area area) {
            this.npcName = name;
            this.area = area;
        }

        public static NpcArea fromMyLocation(MethodProvider api) {
            return Arrays.stream(NpcArea.values()).filter(f -> f.area.contains(api.myPosition())).findFirst().orElse(null);
        }
    }

And then later on...

        NpcArea myArea = NpcArea.fromMyLocation(this); // Store this for later use
	if (myArea == null) {
        	// Walk to area
        } else {
        	NPC npcToAttack = npcs.closest(myArea.npcName);
    	}

 

Edited by NoxMerc

1 hour ago, progamerz said:

public boolean npcInsideAreas(Area[] areas,NPC n) {
        for(Area a: areas) {
            if(a.contains(n)) {
                return true;
            }
        }
        return false;
    }

Maybe this?

Better yet

	public boolean npcInsideAreas(Area[] areas, NPC n) {
		return Arrays.asList(areas).stream().anyMatch(a -> a.contains(n));

	}

Should do the trick.

 

@Juggles What you have is right, you just need to substitute Player for the NPC. They are both entities, so I don't see why it wouldn't work.
Alternatively you can check contains(npc.getPosition())

Edited by Tom

public static boolean isInArea(Entity e, Area... areas) {
    return Stream.of(areas).anyMatch(area -> area.contains(e));
}

Edited by liverare

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.