Jump to content

Storing NPC that player is currently fighting from a distance?


ospaul

Recommended Posts

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

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

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

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

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