Jump to content

Storing NPC that player is currently fighting from a distance?


Recommended Posts

Posted (edited)

Hi there,

currently wrapping up a script, in which my player is in ranged combat with an NPC from a distance, so I cant store which exact NPC is closest, as normally this npc is medium-long range. As of now my code looks something along the lines of

private list<npc> npcName;

npcName = script.getNpcs().filter(isNPCDangerous());

private static boolean isNPCDangerous(NPC npc) {
        if( script.myPlayer().isInteracting(npc) && npc.getName().equals("NPC name") && !npc.isUnderAttack())
            return false;
        if (npc.getName().equals("NPC name") && script.myPlayer().isInteracting(npc) || npc.getName().equals("NPC name") && !script.myPlayer().isInteracting(npc) && !npc.isUnderAttack())
            return true;
        else
            return false;
    }

Currently NPCs that are not under attack are included as dangerous when I am in combat with a different NPC, as well as NPCs that are under attack by another player but are in a long animation, or are not being attacked back

Edited by ospaul
Posted (edited)

I believe the problem is your second if statement. The mix of && and || is confusing and I don't think it works the way you are thinking it does. Try looking into ways to change that.

For example, maybe something like this where it groups the statements:

if ((npc.getName().equals("NPC name") && script.myPlayer().isInteracting(npc)) || (npc.getName().equals("NPC name") && !script.myPlayer().isInteracting(npc) && !npc.isUnderAttack()))

 

EDIT: Your second if statement is actually really confusing. I just realized it contradicts itself so it'll always return true.

Edited by Heist
Posted

Just an fyi (not sure if this is what you're referring to in previous comment) but your if conditions are redundant.

This:

private static boolean isNPCDangerous(NPC npc) {
        if( script.myPlayer().isInteracting(npc) && npc.getName().equals("NPC name") && !npc.isUnderAttack())
            return false;
        if (npc.getName().equals("NPC name") && script.myPlayer().isInteracting(npc) || npc.getName().equals("NPC name") && !script.myPlayer().isInteracting(npc) && !npc.isUnderAttack())
            return true;
        else
            return false;
    }

should just be ->

private static boolean isNPCDangerous(NPC npc) {
        return (npc.getName().equals("NPC name") && script.myPlayer().isInteracting(npc) || npc.getName().equals("NPC name") && !script.myPlayer().isInteracting(npc) && !npc.isUnderAttack())
    }
Posted
28 minutes ago, Mom said:

Just an fyi (not sure if this is what you're referring to in previous comment) but your if conditions are redundant.

This:


private static boolean isNPCDangerous(NPC npc) {
        if( script.myPlayer().isInteracting(npc) && npc.getName().equals("NPC name") && !npc.isUnderAttack())
            return false;
        if (npc.getName().equals("NPC name") && script.myPlayer().isInteracting(npc) || npc.getName().equals("NPC name") && !script.myPlayer().isInteracting(npc) && !npc.isUnderAttack())
            return true;
        else
            return false;
    }

should just be ->


private static boolean isNPCDangerous(NPC npc) {
        return (npc.getName().equals("NPC name") && script.myPlayer().isInteracting(npc) || npc.getName().equals("NPC name") && !script.myPlayer().isInteracting(npc) && !npc.isUnderAttack())
    }

Yeah that’s close to what I came up with,  it was a long day haha

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...