aorecomp

Anarchy Online classic in the browser

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


server/npc_pursuit.go (5837B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"math"
      6 	"math/rand/v2"
      7 	"time"
      8 )
      9 
     10 func (server *server) advanceNPCPursuit(npc *character, now time.Time, seconds float64, updates map[*worldClient][][]byte) bool {
     11 	// Combat capability selects pursuit independently of idle placement.
     12 	if npc.combatProfile == nil || npc.fighting == 0 && !npc.motion.returning {
     13 		return false
     14 	}
     15 	defer server.indexNPC(npc)
     16 	physical := server.geometry.physical(npc.playfield)
     17 	destination := npc.motion.home
     18 	if npc.fighting != 0 {
     19 		target := server.characters[npc.fighting]
     20 		if !server.combatParticipantAvailable(target, now) || target.playfield != npc.playfield {
     21 			server.selectNPCTarget(npc, now, updates)
     22 			target = server.characters[npc.fighting]
     23 		}
     24 		if npc.fighting != 0 {
     25 			destination = target.position
     26 			if !target.npc {
     27 				destination = predictedPosition(target, now)
     28 			}
     29 			if !npc.motion.moving && npc.pursuitArrived(destination) {
     30 				server.stopNPCMotion(npc, updates)
     31 				return true
     32 			}
     33 		}
     34 	}
     35 
     36 	changed := !npc.motion.moving || npc.motion.destination != destination
     37 	reconcile := [3]float32{}
     38 	if physical {
     39 		replan := npc.motion.navigation != nil || len(npc.motion.route) == 0 && !now.Before(npc.motion.routeRetry)
     40 		if replan {
     41 			if !server.requestNPCRoute(npc, destination, now) {
     42 				// Keep the job, but stop observers if the search outlasts a tick.
     43 				server.pauseNPCMotion(npc, updates)
     44 				return true
     45 			}
     46 			changed = true
     47 			reconcile = npc.position
     48 		}
     49 		if len(npc.motion.route) == 0 {
     50 			npc.walkSurface(npc.position, seconds)
     51 			server.stopNPCMotion(npc, updates)
     52 			npc.motion.destination = destination
     53 			return true
     54 		}
     55 	}
     56 
     57 	// Publish the starting pose before either side advances the new route.
     58 	if !npc.running && npc.movement.action(npc, 23) {
     59 		changed = true
     60 	}
     61 	if !npc.motion.moving {
     62 		npc.motion.destination = destination
     63 		npc.motion.moving = true
     64 		server.queueCombat(updates, npc, npc, npcRoutePacket(npc, npc.position))
     65 		changed = false
     66 		if !physical {
     67 			return true
     68 		}
     69 	}
     70 	routeLength := len(npc.motion.route)
     71 	previous := npc.position
     72 	arrived, blocked := npc.advanceNPCMotion(destination, seconds)
     73 	if blocked {
     74 		server.stopNPCMotion(npc, updates)
     75 		npc.motion.routeRetry = now.Add(time.Second)
     76 		return true
     77 	}
     78 	changed = changed || len(npc.motion.route) != routeLength
     79 	if arrived && npc.movement.falling {
     80 		npc.walkSurface(npc.position, seconds)
     81 	}
     82 
     83 	// Preserve source offsets when physical scene geometry is unavailable.
     84 	if ground := server.terrain[npc.playfield]; ground != nil && !npc.geometry.physical(npc.playfield) {
     85 		home, available := ground.elevation(npc.motion.home[0], npc.motion.home[2])
     86 		if available {
     87 			height, inside := ground.elevation(npc.position[0], npc.position[2])
     88 			if !inside {
     89 				npc.position = previous
     90 				server.stopNPCMotion(npc, updates)
     91 				return true
     92 			}
     93 			npc.position[1] = npc.motion.home[1] + height - home
     94 		}
     95 	}
     96 
     97 	// A moving actor's stop uses the pose at this tick, after integrating its interval.
     98 	if npc.fighting != 0 && npc.pursuitArrived(destination) {
     99 		server.stopNPCMotion(npc, updates)
    100 		return true
    101 	}
    102 
    103 	// Complete the return before allowing another idle roaming destination.
    104 	if npc.fighting == 0 && !npc.movement.falling && arrived {
    105 		server.stopNPCMotion(npc, updates)
    106 		npc.motion.returning = false
    107 		npc.motion.nextMove = now.Add(time.Duration(3+rand.IntN(6)) * time.Second)
    108 		return true
    109 	}
    110 	npc.motion.destination = destination
    111 	npc.motion.moving = true
    112 	// Refresh from the completed frame's pose. Consuming a previous search must
    113 	// advance movement before another target update can request its replacement.
    114 	if physical && (len(npc.motion.route) == 0 || surfaceDistanceSquared(npc.motion.route[len(npc.motion.route)-1], destination) >= 1) {
    115 		server.requestNPCRoute(npc, destination, now)
    116 	}
    117 	if changed {
    118 		server.queueCombat(updates, npc, npc, npcRoutePacket(npc, reconcile))
    119 	}
    120 	return true
    121 }
    122 
    123 func (server *server) stopNPCMotion(npc *character, updates map[*worldClient][][]byte) {
    124 	if npc.motion.navigation != nil {
    125 		close(npc.motion.navigation.cancel)
    126 		npc.motion.navigation = nil
    127 	}
    128 	server.pauseNPCMotion(npc, updates)
    129 }
    130 
    131 func (server *server) pauseNPCMotion(npc *character, updates map[*worldClient][][]byte) {
    132 	npc.motion.route = nil
    133 	if !npc.motion.moving {
    134 		return
    135 	}
    136 	npc.motion.moving = false
    137 	npc.movement.velocity = [3]float32{}
    138 	npc.movement.speed = 0
    139 	npc.movement.action(npc, 19)
    140 	npc.movement.action(npc, 22)
    141 	// Clear the waypoint controller, then restore walking for idle roaming.
    142 	server.queueCombat(updates, npc, npc, npcRoutePacket(npc, npc.position), npcMovementPacket(npc, 22))
    143 }
    144 
    145 func npcRoutePacket(npc *character, reconcilePosition [3]float32) []byte {
    146 	// 14.9.1 kkr.dll FUN_1007e540 / FUN_1007e690, including the waypoint count.
    147 	targetType, targetID, control, count := uint32(0), uint32(0), byte(19), uint32(0)
    148 	if npc.motion.moving {
    149 		targetType, targetID, control, count = 50000, npc.id, 22, 2
    150 		if len(npc.motion.route) != 0 {
    151 			count = uint32(1 + min(29, len(npc.motion.route)))
    152 		}
    153 		if npc.running {
    154 			control = 23
    155 		}
    156 	}
    157 	var body []byte
    158 	for _, value := range []uint32{targetType, targetID, 0} {
    159 		body = binary.BigEndian.AppendUint32(body, value)
    160 	}
    161 	body = append(body, control)
    162 	for _, value := range reconcilePosition {
    163 		body = binary.BigEndian.AppendUint32(body, math.Float32bits(value))
    164 	}
    165 	body = binary.BigEndian.AppendUint32(body, count)
    166 	if npc.motion.moving {
    167 		points := [][3]float32{npc.position, npc.motion.destination}
    168 		if len(npc.motion.route) != 0 {
    169 			points = append(points[:1], npc.motion.route[:min(29, len(npc.motion.route))]...)
    170 		}
    171 		for _, position := range points {
    172 			for _, value := range position {
    173 				body = binary.BigEndian.AppendUint32(body, math.Float32bits(value))
    174 			}
    175 		}
    176 	}
    177 	return n3Packet(0x260f3671, 50000, npc.id, body)
    178 }