Jump to content

Filter to Check if NPC in Array of Areas


Juggles

Recommended Posts

 

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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