aorecomp

Anarchy Online classic in the browser

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


server/roaming.go (5435B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"log"
      6 	"math"
      7 	"math/rand/v2"
      8 	"time"
      9 )
     10 
     11 type npcMotion struct {
     12 	roaming     bool
     13 	home        [3]float32
     14 	destination [3]float32
     15 	nextMove    time.Time
     16 	moving      bool
     17 	returning   bool
     18 	route       [][3]float32
     19 	routeRetry  time.Time
     20 	navigation  *navigationJob
     21 }
     22 
     23 func (server *server) advanceActions(now time.Time) {
     24 	server.refreshPlayerInterest(now)
     25 	server.advanceMissions(now)
     26 	server.advanceCorpseExpiry(now)
     27 	server.advanceNPCRespawns(now)
     28 	server.advanceNPCs(now)
     29 	server.advanceEquipment(now)
     30 	server.advanceItemEffects(now)
     31 	server.advanceNanos(now)
     32 	server.advanceCombat(now)
     33 	server.advanceRegeneration(now)
     34 }
     35 
     36 func (server *server) advanceNPCs(now time.Time) {
     37 	// Keep motion packets ordered with zone initialization.
     38 	seconds := math.Min(now.Sub(server.lastNPCUpdate).Seconds(), 0.25)
     39 	server.lastNPCUpdate = now
     40 
     41 	clients := make(map[*worldClient][][]byte)
     42 	for _, client := range server.clients {
     43 		if client.ready && !client.loading {
     44 			clients[client] = nil
     45 		}
     46 	}
     47 
     48 	// Dormant NPCs keep their state until a nearby player returns.
     49 	for _, character := range server.simulationNPCs(now) {
     50 		if character.health == 0 || character.motion == nil || character.pet != nil {
     51 			continue
     52 		}
     53 		if server.geometry.physical(character.playfield) {
     54 			character.geometry = server.geometry
     55 		}
     56 		if character.movementRestricted() {
     57 			server.stopNPCMotion(character, clients)
     58 			continue
     59 		}
     60 		if server.advanceNPCPursuit(character, now, seconds, clients) {
     61 			continue
     62 		}
     63 		if character.fighting != 0 || !character.motion.roaming {
     64 			continue
     65 		}
     66 		if character.motion.nextMove.IsZero() {
     67 			character.motion.nextMove = now.Add(time.Duration(1+character.id%32) * 250 * time.Millisecond)
     68 		}
     69 		if now.Before(character.motion.nextMove) {
     70 			continue
     71 		}
     72 
     73 		// Pick a nearby destination relative to home, so repeated walks never drift.
     74 		started := !character.motion.moving
     75 		if !character.motion.moving {
     76 			angle := rand.Float64() * 2 * math.Pi
     77 			distance := 4 * math.Sqrt(rand.Float64())
     78 			character.motion.destination = [3]float32{
     79 				character.motion.home[0] + float32(math.Sin(angle)*distance),
     80 				character.motion.home[1],
     81 				character.motion.home[2] + float32(math.Cos(angle)*distance),
     82 			}
     83 			// Buried waypoints make the native follower stop short on rising ground.
     84 			if character.geometry.physical(character.playfield) {
     85 				point, normal := character.motion.destination, [3]float32{}
     86 				character.geometry.floor(character.playfield, server.terrain[character.playfield], point, &point, &normal)
     87 				character.motion.destination[1] = point[1] + .01
     88 				// Idle walks need a clear approach; try another destination after the pause.
     89 				if _, clear := probeNPCWalk(*character, character.motion.destination); !clear {
     90 					character.motion.nextMove = now.Add(time.Duration(3+rand.IntN(6)) * time.Second)
     91 					continue
     92 				}
     93 			}
     94 			character.motion.moving = true
     95 		}
     96 		if started {
     97 			x := float64(character.motion.destination[0] - character.position[0])
     98 			z := float64(character.motion.destination[2] - character.position[2])
     99 			angle := math.Atan2(x, z)
    100 			character.rotation = [4]float32{0, float32(math.Sin(angle / 2)), 0, float32(math.Cos(angle / 2))}
    101 			character.movement.action(character, 22)
    102 			server.queueCombat(clients, character, character, npcRoutePacket(character, character.position))
    103 			continue
    104 		}
    105 
    106 		// Use the same acceleration and waypoint arrival as the native vehicle.
    107 		previous := character.position
    108 		arrived, blocked := character.advanceNPCMotion(character.motion.destination, seconds)
    109 		position := character.position
    110 
    111 		// Preserve source offsets when physical scene geometry is unavailable.
    112 		if ground := server.terrain[character.playfield]; ground != nil && !character.geometry.physical(character.playfield) {
    113 			home, available := ground.elevation(character.motion.home[0], character.motion.home[2])
    114 			if available {
    115 				height, inside := ground.elevation(position[0], position[2])
    116 				if inside {
    117 					position[1] = character.motion.home[1] + height - home
    118 				} else {
    119 					position = previous
    120 					arrived = true
    121 				}
    122 			}
    123 		}
    124 		character.position = position
    125 		server.indexNPC(character)
    126 		if arrived || blocked {
    127 			server.stopNPCMotion(character, clients)
    128 			character.motion.nextMove = now.Add(time.Duration(3+rand.IntN(6)) * time.Second)
    129 		}
    130 	}
    131 
    132 	// Only known NPCs receive motion; new arrivals get a spawn and current motion.
    133 	for client := range clients {
    134 		if !client.loading {
    135 			clients[client] = append(clients[client], server.updateNPCInterest(client, now)...)
    136 		}
    137 	}
    138 
    139 	// Nearby observers receive the same entity position, orientation and movement control.
    140 	for client, packets := range clients {
    141 		for _, packet := range packets {
    142 			if err := client.send(packet); err != nil {
    143 				log.Printf("NPC roaming: %v", err)
    144 				client.disconnect(client.conn)
    145 				break
    146 			}
    147 		}
    148 	}
    149 }
    150 
    151 func npcMovementPacket(character *character, action uint32) []byte {
    152 	// Original CharDCMove carries action, XYZ, quaternion and two trailing floats.
    153 	body := binary.BigEndian.AppendUint32(nil, action)
    154 	for _, value := range character.position {
    155 		body = binary.BigEndian.AppendUint32(body, math.Float32bits(value))
    156 	}
    157 	for _, value := range character.rotation {
    158 		body = binary.BigEndian.AppendUint32(body, math.Float32bits(value))
    159 	}
    160 	body = append(body, make([]byte, 8)...)
    161 	return n3Packet(0x54111123, 50000, character.id, body)
    162 }