aorecomp

Anarchy Online classic in the browser

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


server/npc_navigation.go (5616B)


      1 package main
      2 
      3 import (
      4 	"container/heap"
      5 	"math"
      6 )
      7 
      8 // Candidate routes and live movement use identical body, slope and wall handling.
      9 func (npc *character) walkSurface(position [3]float32, seconds float64) {
     10 	previous := npc.position
     11 	frames := max(1, int(math.Ceil(seconds*60)))
     12 	frameSeconds := float32(seconds) / float32(frames)
     13 	for range frames {
     14 		before := npc.position
     15 		displacement := [3]float32{(position[0] - previous[0]) / float32(frames), 0, (position[2] - previous[2]) / float32(frames)}
     16 		if npc.movement.normal[1] > .001 {
     17 			displacement[1] = -(npc.movement.normal[0]*displacement[0] + npc.movement.normal[2]*displacement[2]) / npc.movement.normal[1]
     18 		}
     19 		for axis := range npc.position {
     20 			npc.position[axis] += displacement[axis]
     21 		}
     22 		if npc.movement.falling {
     23 			npc.movement.verticalVelocity = max(float32(-50), npc.movement.verticalVelocity-20*frameSeconds)
     24 			npc.position[1] += npc.movement.verticalVelocity * frameSeconds
     25 		}
     26 		npc.alignSurface(before, false)
     27 	}
     28 }
     29 
     30 func probeNPCWalk(npc character, destination [3]float32) (character, bool) {
     31 	// Keep routing shortcuts visible above the floor as well as physically walkable.
     32 	origin, end := npc.position, destination
     33 	origin[1] += .11
     34 	end[1] += .11
     35 	if !npc.geometry.lineOfSight(npc.playfield, origin, end) {
     36 		origin[1] = npc.position[1] + 1.2
     37 		end[1] = destination[1] + 1.2
     38 		if !npc.geometry.lineOfSight(npc.playfield, origin, end) {
     39 			return npc, false
     40 		}
     41 	}
     42 	distance := math.Hypot(float64(destination[0]-npc.position[0]), float64(destination[2]-npc.position[2]))
     43 	npc.walkSurface(destination, distance/float64(npc.runningMovementSpeed(false)))
     44 	// A descent may reach the horizontal goal before gravity reaches its floor.
     45 	for settle := 0; npc.movement.falling && settle < 8; settle++ {
     46 		npc.walkSurface(npc.position, .25)
     47 	}
     48 	x, z := npc.position[0]-destination[0], npc.position[2]-destination[2]
     49 	return npc, x*x+z*z < .0025
     50 }
     51 
     52 type navigationNode struct {
     53 	actor    character
     54 	parent   *navigationNode
     55 	x, z     int
     56 	cost     float64
     57 	priority float64
     58 }
     59 
     60 type navigationQueue []*navigationNode
     61 
     62 func (queue navigationQueue) Len() int           { return len(queue) }
     63 func (queue navigationQueue) Less(i, j int) bool { return queue[i].priority < queue[j].priority }
     64 func (queue navigationQueue) Swap(i, j int)      { queue[i], queue[j] = queue[j], queue[i] }
     65 func (queue *navigationQueue) Push(value any)    { *queue = append(*queue, value.(*navigationNode)) }
     66 func (queue *navigationQueue) Pop() any {
     67 	last := len(*queue) - 1
     68 	node := (*queue)[last]
     69 	*queue = (*queue)[:last]
     70 	return node
     71 }
     72 
     73 func planNPCRoute(npc *character, destination [3]float32, cancelled, stopped <-chan struct{}) [][3]float32 {
     74 	select {
     75 	case <-cancelled:
     76 		return nil
     77 	case <-stopped:
     78 		return nil
     79 	default:
     80 	}
     81 	// This bounded half-metre search is reconstruction policy, not a recovered
     82 	// original server algorithm. Probe copies never alter the live actor or DB.
     83 	if scene := npc.geometry.dungeon(npc.playfield); scene != nil {
     84 		if scene.roomAt(destination) == nil {
     85 			return nil
     86 		}
     87 	} else if _, inside := npc.geometry.terrain[npc.playfield].elevation(destination[0], destination[2]); !inside {
     88 		return nil
     89 	}
     90 	if arrived, ok := probeNPCWalk(*npc, destination); ok && surfaceDistanceSquared(arrived.position, destination) < .01 {
     91 		return [][3]float32{destination}
     92 	}
     93 	start := &navigationNode{actor: *npc}
     94 	queue := navigationQueue{start}
     95 	costs := map[[3]int]float64{{0, int(math.Round(float64(npc.position[1]) * 2)), 0}: 0}
     96 	for visited := 0; queue.Len() != 0 && visited < 4096; visited++ {
     97 		select {
     98 		case <-cancelled:
     99 			return nil
    100 		case <-stopped:
    101 			return nil
    102 		default:
    103 		}
    104 		node := heap.Pop(&queue).(*navigationNode)
    105 		if surfaceDistanceSquared(node.actor.position, destination) < 1 {
    106 			if arrived, ok := probeNPCWalk(node.actor, destination); ok && surfaceDistanceSquared(arrived.position, destination) < .01 {
    107 				points := [][3]float32{destination}
    108 				for at := node; at.parent != nil; at = at.parent {
    109 					points = append(points, at.actor.position)
    110 				}
    111 				for i, j := 0, len(points)-1; i < j; i, j = i+1, j-1 {
    112 					points[i], points[j] = points[j], points[i]
    113 				}
    114 				// Keep only shortcuts that the same physical actor can walk.
    115 				var route [][3]float32
    116 				actor := *npc
    117 				for len(points) != 0 {
    118 					for i := len(points) - 1; i >= 0; i-- {
    119 						select {
    120 						case <-cancelled:
    121 							return nil
    122 						case <-stopped:
    123 							return nil
    124 						default:
    125 						}
    126 						arrived, ok := probeNPCWalk(actor, points[i])
    127 						if ok && surfaceDistanceSquared(arrived.position, points[i]) < .01 {
    128 							route = append(route, points[i])
    129 							actor, points = arrived, points[i+1:]
    130 							break
    131 						}
    132 						if i == 0 {
    133 							return nil
    134 						}
    135 					}
    136 				}
    137 				return route
    138 			}
    139 		}
    140 		for _, offset := range [][2]int{{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}} {
    141 			x, z := node.x+offset[0], node.z+offset[1]
    142 			point := [3]float32{npc.position[0] + float32(x)*.5, node.actor.position[1], npc.position[2] + float32(z)*.5}
    143 			actor, ok := probeNPCWalk(node.actor, point)
    144 			if !ok {
    145 				continue
    146 			}
    147 			key := [3]int{x, int(math.Round(float64(actor.position[1]) * 2)), z}
    148 			cost := node.cost + math.Sqrt(float64(surfaceDistanceSquared(node.actor.position, actor.position)))
    149 			if previous, exists := costs[key]; exists && previous <= cost {
    150 				continue
    151 			}
    152 			costs[key] = cost
    153 			heap.Push(&queue, &navigationNode{actor: actor, parent: node, x: x, z: z, cost: cost,
    154 				priority: cost + math.Sqrt(float64(surfaceDistanceSquared(actor.position, destination)))})
    155 		}
    156 	}
    157 	return nil
    158 }