Class TargetLock

java.lang.Object
privbot.mechanics.TargetLock

public final class TargetLock extends Object
Chooses which NPC to fight and sticks to it, so a bot does not flip between two targets every tick.

What it proves: the NPC returned matched the TargetSpec (steps 1, 3 and 4) or was the one already locked, and was alive. A lock is the pair (index, id): the server reuses indices, so an index that now holds a different id is a different NPC and the lock is dropped, never "kept".

Unreadable looks like: choose returns null when nothing qualifies, and the lock is cleared. A locked NPC that is missing from the list (or whose index now holds another id) is no longer kept: the choice falls through to steps 3 and 4, and the lock moves to whatever they pick, or is cleared when they pick nothing. A locked NPC that no longer matches the spec is dropped the same way.

Thread rule: mutable, single-owner. Call choose once per game tick from the thread that builds the NpcView list.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    choose(List<NpcView> npcs, int currentTargetIndex, net.runelite.api.coords.WorldPoint player, TargetSpec spec)
    Picks the target, in this order: the current target, if it matches and is alive and not busy; the locked NPC, if present with the same index and id, still matching the spec, alive and not busy; the first matching NPC that targets us and is alive; the nearest matching NPC that is alive and not busy, ties to the lower index. A locked NPC seen dead counts a kill if it had targeted us or been our current target.
    int
    Locked NPCs seen dead after they had targeted us or been our current target.
    int
    The locked NPC's index, or -1 for none.
    void
    Forgets the lock and the kill count.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TargetLock

      public TargetLock()
  • Method Details

    • choose

      public NpcView choose(List<NpcView> npcs, int currentTargetIndex, net.runelite.api.coords.WorldPoint player, TargetSpec spec)
      Picks the target, in this order:
      1. the current target, if it matches and is alive and not busy;
      2. the locked NPC, if present with the same index and id, still matching the spec, alive and not busy;
      3. the first matching NPC that targets us and is alive;
      4. the nearest matching NPC that is alive and not busy, ties to the lower index.
      A locked NPC seen dead counts a kill if it had targeted us or been our current target.
      Parameters:
      currentTargetIndex - the index of the NPC we are interacting with, or -1
      spec - which NPCs qualify; null means TargetSpec.any()
    • lockedIndex

      public int lockedIndex()
      The locked NPC's index, or -1 for none.
    • kills

      public int kills()
      Locked NPCs seen dead after they had targeted us or been our current target.
    • reset

      public void reset()
      Forgets the lock and the kill count.