aorecomp

Anarchy Online classic in the browser

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


server/npc_index.go (3249B)


      1 package main
      2 
      3 import (
      4 	"iter"
      5 	"math"
      6 	"time"
      7 )
      8 
      9 type npcCell struct {
     10 	playfield uint32
     11 	x, z      int
     12 }
     13 
     14 func (server *server) nearbyNPCs(playfield uint32, position [3]float32) iter.Seq2[uint32, *character] {
     15 	if server.npcCells == nil {
     16 		server.npcCells = make(map[npcCell]map[uint32]*character)
     17 		server.npcLocations = make(map[uint32]npcCell)
     18 		for _, actor := range server.characters {
     19 			if actor.npc {
     20 				server.indexNPC(actor)
     21 			}
     22 		}
     23 	}
     24 	// A 200m cell and its eight neighbours cover both interest radii. Callers
     25 	// still check exact distance, including the corners of these square cells.
     26 	x, z := int(math.Floor(float64(position[0])/200)), int(math.Floor(float64(position[2])/200))
     27 	return func(yield func(uint32, *character) bool) {
     28 		for dx := -1; dx <= 1; dx++ {
     29 			for dz := -1; dz <= 1; dz++ {
     30 				for id, npc := range server.npcCells[npcCell{playfield, x + dx, z + dz}] {
     31 					if !yield(id, npc) {
     32 						return
     33 					}
     34 				}
     35 			}
     36 		}
     37 	}
     38 }
     39 
     40 func (server *server) indexNPC(npc *character) {
     41 	if server.npcCells == nil {
     42 		return
     43 	}
     44 	cell := npcCell{npc.playfield, int(math.Floor(float64(npc.position[0]) / 200)), int(math.Floor(float64(npc.position[2]) / 200))}
     45 	if previous, exists := server.npcLocations[npc.id]; exists {
     46 		if previous == cell && server.npcCells[cell][npc.id] == npc {
     47 			return
     48 		}
     49 		delete(server.npcCells[previous], npc.id)
     50 		if len(server.npcCells[previous]) == 0 {
     51 			delete(server.npcCells, previous)
     52 		}
     53 	}
     54 	if server.npcCells[cell] == nil {
     55 		server.npcCells[cell] = make(map[uint32]*character)
     56 	}
     57 	server.npcCells[cell][npc.id] = npc
     58 	server.npcLocations[npc.id] = cell
     59 }
     60 
     61 func (server *server) addNPC(npc *character) {
     62 	server.characters[npc.id] = npc
     63 	server.indexNPC(npc)
     64 	if server.world != nil {
     65 		server.world.setOwner(npc.id, server.zone)
     66 	}
     67 }
     68 
     69 func (server *server) removeNPC(id uint32) {
     70 	if npc := server.characters[id]; npc != nil && npc.motion != nil && npc.motion.navigation != nil {
     71 		close(npc.motion.navigation.cancel)
     72 		npc.motion.navigation = nil
     73 	}
     74 	if cell, exists := server.npcLocations[id]; exists {
     75 		delete(server.npcCells[cell], id)
     76 		if len(server.npcCells[cell]) == 0 {
     77 			delete(server.npcCells, cell)
     78 		}
     79 		delete(server.npcLocations, id)
     80 	}
     81 	delete(server.characters, id)
     82 	delete(server.simulatedNPCs, id)
     83 	if server.world != nil {
     84 		server.world.setOwner(id, nil)
     85 	}
     86 }
     87 
     88 func (server *server) simulationNPCs(now time.Time) map[uint32]*character {
     89 	if server.simulatedNPCs == nil {
     90 		server.simulatedNPCs = make(map[uint32]*character)
     91 	}
     92 	// Previously observed NPCs retain the fifteen-minute grace period without
     93 	// rescanning distant cells or unoccupied zones.
     94 	for id, client := range server.clients {
     95 		if !client.ready || client.loading {
     96 			continue
     97 		}
     98 		player := server.characters[id]
     99 		for _, npc := range server.nearbyNPCs(player.playfield, player.position) {
    100 			x, z := player.position[0]-npc.position[0], player.position[2]-npc.position[2]
    101 			if x*x+z*z <= 200*200 {
    102 				npc.npcActiveUntil = now.Add(15 * time.Minute)
    103 				server.simulatedNPCs[npc.id] = npc
    104 			}
    105 		}
    106 	}
    107 	for id, npc := range server.simulatedNPCs {
    108 		if server.characters[id] != npc || !now.Before(npc.npcActiveUntil) {
    109 			delete(server.simulatedNPCs, id)
    110 		}
    111 	}
    112 	return server.simulatedNPCs
    113 }