aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/interest.go (3721B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"time"
      7 )
      8 
      9 func (server *server) npcActive(npc *character, now time.Time) bool {
     10 	// Keep nearby NPCs active for fifteen minutes after the last player leaves.
     11 	for id, client := range server.clients {
     12 		if !client.ready || client.loading {
     13 			continue
     14 		}
     15 		player := server.characters[id]
     16 		if player.playfield != npc.playfield {
     17 			continue
     18 		}
     19 		x, z := player.position[0]-npc.position[0], player.position[2]-npc.position[2]
     20 		if x*x+z*z <= 200*200 {
     21 			npc.npcActiveUntil = now.Add(15 * time.Minute)
     22 			if server.simulatedNPCs == nil {
     23 				server.simulatedNPCs = make(map[uint32]*character)
     24 			}
     25 			server.simulatedNPCs[npc.id] = npc
     26 			return true
     27 		}
     28 	}
     29 	return now.Before(npc.npcActiveUntil)
     30 }
     31 
     32 func (server *server) updateNPCInterest(client *worldClient, now time.Time) [][]byte {
     33 	player := server.characters[client.id]
     34 	position := predictedPosition(player, now)
     35 	var updates [][]byte
     36 
     37 	// Add NPCs within 180 metres and retain them to 200 to avoid boundary churn.
     38 	for id := range client.npcs {
     39 		other := server.characters[id]
     40 		if other != nil && other.playfield == player.playfield {
     41 			x, z := other.position[0]-position[0], other.position[2]-position[2]
     42 			if x*x+z*z <= 200*200 {
     43 				continue
     44 			}
     45 		}
     46 		updates = append(updates, n3Packet(0x36510078, 50000, id, nil))
     47 		if other != nil && other.corpse != nil {
     48 			updates = append(updates, n3Packet(0x36510078, 51050, id, nil))
     49 		}
     50 		delete(client.npcs, id)
     51 		if client.lootCorpse == id {
     52 			client.lootCorpse = 0
     53 		}
     54 	}
     55 
     56 	// Send current state before motion when an NPC enters this observer's vicinity.
     57 	spawned := make(map[uint32]bool)
     58 	for id, other := range server.nearbyNPCs(player.playfield, position) {
     59 		if client.npcs[id] {
     60 			continue
     61 		}
     62 		x, z := other.position[0]-position[0], other.position[2]-position[2]
     63 		if x*x+z*z > 180*180 {
     64 			continue
     65 		}
     66 		client.npcs[id] = true
     67 		spawned[id] = true
     68 		if other.corpse != nil {
     69 			updates = append(updates, corpsePacket(other))
     70 		} else {
     71 			updates = append(updates, characterPacket(other))
     72 			updates = append(updates, equippedItemPackets(other)...)
     73 			// kkr.dll sub_36082AD0 does not copy HasKnuBotData for dressed humans.
     74 			// Default action sub_3602C350 reads stat 768 to open native dialogue.
     75 			_, arrivalRepresentative := arrivalHallSide(other.npcDefinitionID)
     76 			if gamedata.NPCScripts[other.npcDefinitionID].Talk != nil || arrivalRepresentative {
     77 				body := binary.BigEndian.AppendUint32(nil, 768)
     78 				body = binary.BigEndian.AppendUint32(body, 1)
     79 				updates = append(updates, n3Packet(0x2b333d6e, 50000, other.id, body))
     80 			}
     81 			if other.pet != nil {
     82 				owner := server.characters[other.pet.owner]
     83 				updates = append(updates, petIdentityPacket(0x194e4f76, other.pet.owner, other.id))
     84 				if owner != nil {
     85 					updates = append(updates, petCounterPacket(owner))
     86 				}
     87 			}
     88 			if other.combatProfile != nil {
     89 				updates = append(updates, npcCombatPacket(other))
     90 			}
     91 			updates = append(updates, activeNanoPackets(other, now)...)
     92 			if other.health == 0 {
     93 				updates = append(updates, deathPacket(other))
     94 			}
     95 		}
     96 		if other.pet != nil && other.followTarget != 0 && !other.pet.paused && client.seesPlayer(other.followTarget) {
     97 			updates = append(updates, petFollowPacket(other, other.followTarget))
     98 		} else if other.motion != nil && other.motion.moving {
     99 			updates = append(updates, npcRoutePacket(other, other.position))
    100 		}
    101 	}
    102 	if client.ready && len(spawned) != 0 {
    103 		// Rebuild fighting targets only after every new participant has spawned.
    104 		updates = append(updates, server.combatInterest(client, spawned)...)
    105 		updates = append(updates, server.playerFollowReplay(client, spawned)...)
    106 	}
    107 	return updates
    108 }