Jump to content

Filter to Check if NPC in Array of Areas


Recommended Posts

Posted

 

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 :)

Posted (edited)

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
  • Like 1
Posted (edited)
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
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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