aorecomp

Anarchy Online classic in the browser

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


server/navigation_worker.go (3510B)


      1 package main
      2 
      3 import (
      4 	"slices"
      5 	"time"
      6 )
      7 
      8 type navigationJob struct {
      9 	actor       character
     10 	destination [3]float32
     11 	cancel      chan struct{}
     12 	done        chan struct{}
     13 	route       [][3]float32
     14 }
     15 
     16 // Each playfield owns its search queue. A blocked search in one zone cannot
     17 // consume another zone's worker or block its owner on a path search.
     18 type zoneNavigator struct {
     19 	jobs chan *navigationJob
     20 	stop chan struct{}
     21 	done chan struct{}
     22 }
     23 
     24 func (navigator *zoneNavigator) run() {
     25 	defer close(navigator.done)
     26 	for {
     27 		select {
     28 		case <-navigator.stop:
     29 			return
     30 		case job := <-navigator.jobs:
     31 			job.route = planNPCRoute(&job.actor, job.destination, job.cancel, navigator.stop)
     32 			close(job.done)
     33 		}
     34 	}
     35 }
     36 
     37 func (server *server) requestNPCRoute(npc *character, destination [3]float32, now time.Time) bool {
     38 	if server.navigationStopped {
     39 		return false
     40 	}
     41 	// Motion replacement (death, zoning, a new command) drops the old job. A
     42 	// changed start, target identity or collision body invalidates a result. A
     43 	// moving target's last observed position still provides a usable next route.
     44 	if job := npc.motion.navigation; job != nil {
     45 		if job.actor.position != npc.position || job.actor.playfield != npc.playfield ||
     46 			job.actor.fighting != npc.fighting || job.actor.followTarget != npc.followTarget ||
     47 			job.actor.body != npc.body {
     48 			close(job.cancel)
     49 			npc.motion.navigation = nil
     50 		} else {
     51 			select {
     52 			case <-job.done:
     53 				npc.motion.route = job.route
     54 				npc.motion.navigation = nil
     55 				npc.motion.routeRetry = time.Time{}
     56 				if len(job.route) == 0 {
     57 					npc.motion.routeRetry = now.Add(time.Second)
     58 				}
     59 				return true
     60 			default:
     61 				return false
     62 			}
     63 		}
     64 	}
     65 
     66 	if server.navigation == nil {
     67 		server.navigation = make(map[uint32]*zoneNavigator)
     68 	}
     69 	navigator := server.navigation[npc.playfield]
     70 	if navigator == nil {
     71 		navigator = &zoneNavigator{jobs: make(chan *navigationJob, 32), stop: make(chan struct{}), done: make(chan struct{})}
     72 		server.navigation[npc.playfield] = navigator
     73 		server.navigationWorkers.Go(navigator.run)
     74 	}
     75 
     76 	// Only the worker can mutate the copied actor. Equipment and effect slices
     77 	// are read by collision and speed calculations; scenes are pinned for the job.
     78 	job := &navigationJob{actor: *npc, destination: destination, cancel: make(chan struct{}), done: make(chan struct{})}
     79 	job.actor.inventory.Items = slices.Clone(npc.inventory.Items)
     80 	job.actor.activeNanos = slices.Clone(npc.activeNanos)
     81 	job.actor.geometry = npc.geometry.navigationGeometry(npc.playfield)
     82 	select {
     83 	case navigator.jobs <- job:
     84 		npc.motion.navigation = job
     85 	default:
     86 		// Retry next tick rather than blocking the simulation on a full queue.
     87 	}
     88 	return false
     89 }
     90 
     91 func (geometry *sceneGeometry) navigationGeometry(playfield uint32) *sceneGeometry {
     92 	// Mission retirement may remove the live registration while a search runs.
     93 	// The completed scene stays immutable and alive until this job releases it.
     94 	view := &sceneGeometry{database: geometry.database,
     95 		playfields: geometry.playfields, terrain: geometry.terrain, interiors: geometry.interiors,
     96 		surfaces: geometry.surfaces, waters: geometry.waters, models: geometry.models}
     97 	view.missions = map[uint32]*missionScene{playfield: geometry.mission(playfield)}
     98 	return view
     99 }
    100 
    101 func (server *server) stopNavigation() {
    102 	if !server.navigationStopped {
    103 		server.navigationStopped = true
    104 		for _, navigator := range server.navigation {
    105 			close(navigator.stop)
    106 		}
    107 		clear(server.navigation)
    108 	}
    109 	server.navigationWorkers.Wait()
    110 }