Jump to content

Checking if certain Position or Area is clear


aekramer

Recommended Posts

Hey all, I am trying to figure something out and I really need to clear my head on this one probably lol.

 

How would I go around checking if a certain tile or area does NOT contain ANY NPC?

 

I've tried creating a Position list for the area, then looping through all these positions (I only use small area's, like 3x3), then grabbing a List of all local NPCs and checking for every Tile if none of those NPCs are on it.

    private boolean isAreaClear(Area area) {
        List<NPC> npcs = getNpcs();
        if (area == null) {
            return true;
        }
        List<Position> areaPos = area.getPositions(0);
        
        for (NPC npc : npcs) {
            for (Position pos : areaPos) {
                if (npc.getPosition().equals(pos)) {
                    return false;
                }
            }
        }
        return true;
    }
    
    private List<NPC> getNpcs() {
        List<NPC> list = new LinkedList<>();
        for (NPC npc : sC.npcs.getAll()) {
            if (npc.getDefinition() != null) {
                search:
                {
                    if(npc.getDefinition().getName() != null){
                    	String name = npc.getDefinition().getName();
                        if (name.equalsIgnoreCase("bandit")) {
                            list.add(npc);
                            break search;
                        }
                    }
                }
            }
        }
        return list;
    } 
Link to comment
Share on other sites

I don't remember exact OSBot API, but something like this should work (this is one of several ways to do it):

//TODO add whatever null checks are needed...
public boolean NpcInArea(Area area) {
  for (NPC npc : npcs.getAll()) {
    if (area.contains(npc))
      return true;
  }

  return false;
}
Edited by Eliot
  • Like 2
Link to comment
Share on other sites

 

I don't remember exact OSBot API, but something like this should work (this is one of several ways to do it):

//TODO add whatever null checks are needed...
public boolean NpcInArea(Area area) {
  for (NPC npc : npcs.getAll()) {
    if (area.contains(npc))
      return true;
  }

  return false;
}

 

Thank you, apparantly I was overcomplicating things again..

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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