server/npc_threat.go (1630B)
1 package main 2 3 import "time" 4 5 func (server *server) combatParticipantAvailable(actor *character, now time.Time) bool { 6 if actor == nil || actor.health == 0 { 7 return false 8 } 9 if actor.pet != nil { 10 owner := server.characters[actor.pet.owner] 11 return !actor.pet.expired(now) && owner != nil && owner.playfield == actor.playfield && server.combatParticipantAvailable(owner, now) 12 } 13 if actor.npc { 14 return true 15 } 16 client := server.clients[actor.id] 17 return client != nil && client.ready && !client.loading 18 } 19 20 func (server *server) selectNPCTarget(npc *character, now time.Time, updates map[*worldClient][][]byte) { 21 var target *character 22 if npc.health != 0 { 23 for id, score := range npc.threat { 24 player := server.characters[id] 25 if !server.combatParticipantAvailable(player, now) || player.playfield != npc.playfield { 26 continue 27 } 28 client := server.clients[id] 29 if player.pet != nil { 30 client = server.clients[player.pet.owner] 31 } 32 if client == nil || !client.npcs[npc.id] { 33 continue 34 } 35 // Keep the current opponent on ties; otherwise use stable identity order. 36 if target == nil || score > npc.threat[target.id] || score == npc.threat[target.id] && 37 (id == npc.fighting || target.id != npc.fighting && id < target.id) { 38 target = player 39 } 40 } 41 } 42 if target != nil && npc.fighting == target.id { 43 return 44 } 45 46 // Native StopFight consumes queued hits against the old opponent first. 47 nextAttack := npc.nextAttack 48 server.stopAttack(npc, updates) 49 if target != nil { 50 server.startAttack(npc, target, now, updates) 51 if !nextAttack.IsZero() { 52 npc.nextAttack = nextAttack 53 } 54 } 55 }