aorecomp

Anarchy Online classic in the browser

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


server/npcs.go (5045B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"fmt"
      6 	"math"
      7 )
      8 
      9 func (character *character) attackableNPC() bool {
     10 	// Human encounters require an explicit population binding; guards and vendors
     11 	// keep their appearances without acquiring combat from a shared body/template.
     12 	return character.npc && character.pet == nil && (character.appearance>>4&7 == 6 ||
     13 		character.combatProfile != nil && len(character.combatProfile.Groups) != 0 || gamedata.NPCLootTables[character.npcDefinitionID] != nil)
     14 }
     15 
     16 func npcCombatProfile(definition string, monster uint32, level uint16) *gamedata.NPCCombatProfile {
     17 	group := gamedata.NPCGroups[definition]
     18 	if group == nil {
     19 		return nil
     20 	}
     21 	profile := gamedata.NPCCombatProfiles[monster][level]
     22 	if !group.SupportsCombatProfile(monster, profile) {
     23 		return nil
     24 	}
     25 	return profile
     26 }
     27 
     28 func makeNPCSpawn(group *gamedata.NPCGroup, level uint16, position [3]float32) character {
     29 	spawn := character{name: group.Name, position: position, rotation: npcSpawnRotation(group),
     30 		appearance: group.Appearance, monsterData: group.MonsterData, level: level, npc: true,
     31 		combatProfile: npcCombatProfile(group.ID, group.MonsterData, level), npcDefinitionID: group.ID}
     32 	spawn.equipNPCOutfit()
     33 	spawn.health = spawn.maximumHealth()
     34 	// Idle placement is independent of admitted combat movement.
     35 	roaming := group.PlacementStatus != "fixed_point" && group.Appearance>>4&7 == 6
     36 	if roaming || spawn.combatProfile != nil {
     37 		spawn.motion = &npcMotion{roaming: roaming, home: position}
     38 	}
     39 	return spawn
     40 }
     41 
     42 func npcSpawnRotation(group *gamedata.NPCGroup) [4]float32 {
     43 	if group == nil || group.FixedSource == nil {
     44 		return [4]float32{0, 0, 0, 1}
     45 	}
     46 	// Fixed spawns retain a facing centre and cone width in degrees. Use the centre.
     47 	halfYaw := float64(group.FixedSource.Angles[0]) * math.Pi / 360
     48 	return [4]float32{0, float32(math.Sin(halfYaw)), 0, float32(math.Cos(halfYaw))}
     49 }
     50 
     51 func (npc *character) equipNPCOutfit() {
     52 	if len(gamedata.NPCOutfits[npc.npcDefinitionID]) == 0 {
     53 		return
     54 	}
     55 	// Definition equipment uses the shared wear/bonus consumers without allocating
     56 	// transferable item identities. Loot remains the separate authored drop table.
     57 	npc.inventory.Items = nil
     58 	for _, entry := range gamedata.NPCOutfits[npc.npcDefinitionID] {
     59 		item := gamedata.ItemTemplates[entry.Template]
     60 		npc.inventory.Items = append(npc.inventory.Items, ownedItem{
     61 			Type: item.HeaderWords[0], Template: entry.Template, Quality: *item.Quality, Slot: entry.Slot, Count: 1,
     62 		})
     63 	}
     64 	if npc.headMesh == 0 {
     65 		npc.headMesh = gamedata.MonsterTemplates[npc.monsterData].Stats[64]
     66 	}
     67 }
     68 
     69 // Catalog ordinals reserve identities even while a zone has no live actors.
     70 type npcPopulation struct {
     71 	definition *gamedata.NPCPlayfield
     72 	first, end uint32
     73 }
     74 
     75 func readPopulations() (map[uint32]npcPopulation, error) {
     76 	populations := make(map[uint32]npcPopulation)
     77 	identity := uint32(1000000)
     78 	for index := range gamedata.NPCs.Playfields {
     79 		playfield := &gamedata.NPCs.Playfields[index]
     80 		if _, exists := gamedata.Playfields[playfield.ID]; !exists {
     81 			return nil, fmt.Errorf("NPC data: unknown playfield %d", playfield.ID)
     82 		}
     83 		population := npcPopulation{definition: playfield, first: identity}
     84 		for _, district := range playfield.Districts {
     85 			for _, group := range district.Groups {
     86 				identity += uint32(len(group.Positions))
     87 				if group.StaticMesh != 0 {
     88 					continue
     89 				}
     90 				if gamedata.NPCs.AppearanceStatuses[group.AppearanceStatus].Confidence == "" || gamedata.NPCs.PlacementStatuses[group.PlacementStatus].Confidence == "" {
     91 					return nil, fmt.Errorf("NPC data: %d/%s/%s needs defined appearance and placement statuses", playfield.ID, district.Name, group.TemplateCode)
     92 				}
     93 				if group.Name == "" || group.MonsterData == 0 {
     94 					return nil, fmt.Errorf("NPC data: %d/%s/%s needs a name and MonsterData", playfield.ID, district.Name, group.TemplateCode)
     95 				}
     96 				if group.Levels[0] == 0 || group.Levels[0] > group.Levels[1] {
     97 					return nil, fmt.Errorf("NPC data: %d/%s/%s has invalid levels %v", playfield.ID, district.Name, group.TemplateCode, group.Levels)
     98 				}
     99 			}
    100 		}
    101 		population.end = identity
    102 		populations[playfield.ID] = population
    103 	}
    104 	return populations, nil
    105 }
    106 
    107 func (population npcPopulation) visit(visit func(uint32, *gamedata.NPCGroup, int, [3]float32)) {
    108 	identity := population.first
    109 	for _, district := range population.definition.Districts {
    110 		for index := range district.Groups {
    111 			group := &district.Groups[index]
    112 			for index, position := range group.Positions {
    113 				if group.StaticMesh == 0 {
    114 					visit(identity, group, index, position)
    115 				}
    116 				identity++
    117 			}
    118 		}
    119 	}
    120 }
    121 
    122 func (population npcPopulation) spawns() []*character {
    123 	var spawns []*character
    124 	population.visit(func(id uint32, group *gamedata.NPCGroup, index int, position [3]float32) {
    125 		level := group.Levels[0] + uint16(index%(int(group.Levels[1])-int(group.Levels[0])+1))
    126 		spawn := makeNPCSpawn(group, level, position)
    127 		spawn.id, spawn.playfield = id, population.definition.ID
    128 		spawns = append(spawns, &spawn)
    129 	})
    130 	return spawns
    131 }