Get nearest NPC and stick to it & other questions



  • Hi there

    This is my first script and I cannot figure out how to stick with the NPC that I'm already attacking. He's always trying to cast the spell on another NPC. Could anyone push me in the right direction?

    Could I show mouse movement paths on debugging? So I could see that the mouse isn't spasming to see if it's human like?

    Thanks a lot guys!

    My script:

    import org.rspeer.runetek.adapter.scene.Npc;
    import org.rspeer.runetek.adapter.scene.Player;
    import org.rspeer.runetek.api.commons.math.Random;
    import org.rspeer.runetek.api.component.tab.Magic;
    import org.rspeer.runetek.api.component.tab.Spell;
    import org.rspeer.runetek.api.scene.Npcs;
    import org.rspeer.runetek.api.scene.Players;
    import org.rspeer.runetek.api.component.tab.Tab;
    import org.rspeer.runetek.api.component.tab.Tabs;
    import org.rspeer.script.Script;
    import org.rspeer.script.ScriptMeta;
    
    @ScriptMeta(developer = "Kets", desc = "Curser on rat", name = "Curser")
    public class Curser extends Script {
    
        String action = "Idle";
    
        @Override
        public int loop() {
            Player me = Players.getLocal();
            Npc rat = Npcs.getNearest(x -> x.getName().equals("Rat") && (x.getTargetIndex() == -1));
                    if (rat != null) {
                        if (Tabs.open(Tab.MAGIC)){
                            if (!me.isMoving()) {
                                if (Magic.cast(Spell.Modern.CURSE)){
                                    action = "Attacking";
                                    rat.interact("Cast");
                                }
                            }
                        }
                    }
            return Random.nextInt(1100, 3200);
        }
    }
    


  • So in your loop you are always searching for a new rat which isn't targeting anyone, you need to check if you're in combat before looking for a new rat.

            Players.getLocal().isAnimating(); //not always you stand still in combat
            Players.getLocal().getTargetIndex() != -1 //are you targeting anyone, won't trigger if someone is targeting you and auto retilate is off
            Npc rat = Npcs.getNearest(npc -> npc.getTargetIndex() != -1 && npc.getTarget().getId() == Players.getLocal().getId()) //npc targeting you if not rat != null
            Players.getLocal().isHealthBarVisible(); //maybe
    

    You could wait while "In combat" or if not in combat search for a new rat and execute round your main loop more often, all depends on the script you're making.

    Hit me up if you have any more questions


  • Script Writer

    @Kets in your Npc predicate, check if the npc's target is the local player. Remember to null check.



  •     @Override
        public int loop()
        {
            Player me = Players.getLocal();
    	// first check for a rat targeting us, if there is any
            Npc rat = Npcs.getNearest(x -> x.getName().equals("Rat") && x.getTarget() == me);
    	// otherwise find a rat that is not targeting anything, and target it
    	if (rat == null)
    	    rat = Npcs.getNearest(x -> x.getName().equals("Rat") && x.getTarget() == null);
    	if (rat != null) {
    		if (Tabs.open(Tab.MAGIC)) {
    			if (!me.isMoving()) {
    				if (Magic.cast(Spell.Modern.CURSE)) {
                                                action = "Attacking";
    					    rat.interact("Cast");
    				}
    			}
    		}
    	}
            return Random.nextInt(1100, 3200);
        }
    


  • Thanks guys! Now it's working well. Other question, could I just hover the mob and not click it jet for a split second, like a delay on the interact.



  • @Kets

    Add

    Time.sleep(Random.low(200, 300));
    

    before

    rat.interact("Cast");
    

 

86
Online

11.2k
Users

1.1k
Topics

14.6k
Posts