server/npc_placement.go (2656B)
1 package main 2 3 import "ao/server/gamedata" 4 5 func (server *server) placeFallbackNPCs(spawns []*character) { 6 // Collect admitted combatants without changing the catalog's source placements. 7 byGroup := make(map[string][]*character) 8 for _, spawn := range spawns { 9 if spawn.combatProfile != nil && gamedata.NPCGroups[spawn.npcDefinitionID].PlacementStatus == "district_center_fallback" { 10 byGroup[spawn.npcDefinitionID] = append(byGroup[spawn.npcDefinitionID], spawn) 11 } 12 } 13 14 // Only generated combat placements use dry ground; authored points stay intact. 15 for _, playfield := range gamedata.NPCs.Playfields { 16 layout, exists := server.geometry.playfields[playfield.ID] 17 ground := server.terrain[playfield.ID] 18 if !exists || ground == nil || playfield.Source == nil || len(playfield.Source.ZoneToDistrictMap) != layout.cellsX*layout.cellsZ { 19 continue 20 } 21 for _, district := range playfield.Districts { 22 if district.SourceIndex == nil { 23 continue 24 } 25 var generated []*character 26 for _, group := range district.Groups { 27 generated = append(generated, byGroup[group.ID]...) 28 } 29 if len(generated) == 0 { 30 continue 31 } 32 33 // Sample original terrain tiles only inside this district's source cells. 34 var candidates [][3]float32 35 for cell, source := range playfield.Source.ZoneToDistrictMap { 36 if source != uint16(*district.SourceIndex) { 37 continue 38 } 39 x, z := float32(cell%layout.cellsX)*layout.cellWidth, float32(cell/layout.cellsX)*layout.cellDepth 40 for sampleZ := z + ground.spacing/2; sampleZ < z+layout.cellDepth; sampleZ += ground.spacing { 41 for sampleX := x + ground.spacing/2; sampleX < x+layout.cellWidth; sampleX += ground.spacing { 42 height, inside := ground.elevation(sampleX, sampleZ) 43 if !inside { 44 continue 45 } 46 point, normal := [3]float32{sampleX, height, sampleZ}, [3]float32{} 47 water := server.geometry.floor(playfield.ID, ground, point, &point, &normal) 48 if water.height <= point[1] { 49 candidates = append(candidates, point) 50 } 51 } 52 } 53 } 54 55 // Use the closest unused dry tile; identity and raw source records are unchanged. 56 for _, spawn := range generated { 57 if len(candidates) == 0 { 58 break 59 } 60 closest := 0 61 for candidate := 1; candidate < len(candidates); candidate++ { 62 if surfaceDistanceSquared(spawn.position, candidates[candidate]) < surfaceDistanceSquared(spawn.position, candidates[closest]) { 63 closest = candidate 64 } 65 } 66 spawn.position = candidates[closest] 67 spawn.motion.home = spawn.position 68 candidates = append(candidates[:closest], candidates[closest+1:]...) 69 } 70 } 71 } 72 }