aorecomp

Anarchy Online classic in the browser

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


server/item_sources.go (11042B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"cmp"
      6 	"fmt"
      7 	"maps"
      8 	"slices"
      9 	"strconv"
     10 	"strings"
     11 )
     12 
     13 type itemSource struct {
     14 	kind        string
     15 	name        string
     16 	shop        uint32
     17 	ingredients [2]uint32
     18 	quality     [2]uint32
     19 	chance      uint32
     20 	pool        string
     21 	candidates  [2]int
     22 	evidence    string
     23 	inference   string
     24 	level       [2]uint16
     25 	groups      []string
     26 	locations   []itemSourceLocation
     27 }
     28 
     29 type itemSourceLocation struct {
     30 	name      string
     31 	playfield uint32
     32 	position  [3]float32
     33 }
     34 
     35 func itemFamily(template uint32) uint32 {
     36 	if pair, exists := gamedata.ItemFamilies[template]; exists {
     37 		return pair[0]
     38 	}
     39 	return template
     40 }
     41 
     42 func npcSourcePlayfield(id string) uint32 {
     43 	zone, _, _ := strings.Cut(id, "/")
     44 	value, _ := strconv.ParseUint(zone, 10, 32)
     45 	return uint32(value)
     46 }
     47 
     48 type itemCatalogIndex struct {
     49 	families   map[uint32][]*itemSource
     50 	entrances  map[uint32][]itemSourceLocation
     51 	items      []*gamedata.Item
     52 	categories []uint32
     53 }
     54 
     55 // The embedded catalog, loot rules and zone links are immutable while the server runs.
     56 func (server *server) itemCatalog() *itemCatalogIndex {
     57 	server.catalogOnce.Do(server.buildItemCatalog)
     58 	return &server.catalog
     59 }
     60 
     61 func (server *server) buildItemCatalog() {
     62 	sources := make(map[uint32][]*itemSource)
     63 	sellers := make(map[uint32][]itemSourceLocation)
     64 	// Reverse authored doors so shared and nested interiors expose every entrance.
     65 	incoming := make(map[uint32][]itemSourceLocation)
     66 	for _, playfield := range slices.Sorted(maps.Keys(server.zoneLinks["target"])) {
     67 		for _, door := range server.zoneLinks["target"][playfield].Doors {
     68 			destination := door.Destination
     69 			// 14.9.1 resource 1000026/505, statel 0xc00101f9 declares Camelot (120).
     70 			// Its entrance is known even though the gameplay arrival is unresolved.
     71 			if playfield == 505 && door.ID == 0xc00101f9 {
     72 				destination = 120
     73 			}
     74 			if destination != 0 && !door.Return {
     75 				incoming[destination] = append(incoming[destination], itemSourceLocation{"", playfield, door.Position})
     76 			}
     77 		}
     78 	}
     79 	entrances := make(map[uint32][]itemSourceLocation)
     80 	for interior := range incoming {
     81 		if !gamedata.Playfields[interior].Outdoor {
     82 			entrances[interior] = outdoorEntrances(interior, incoming)
     83 		}
     84 	}
     85 	addTableLootSources(sources, entrances)
     86 	for _, id := range slices.Sorted(maps.Keys(gamedata.ShopMachines)) {
     87 		machine := gamedata.ShopMachines[id]
     88 		if itemStat(gamedata.ItemTemplates[machine.Template], 30)&8 == 0 {
     89 			continue
     90 		}
     91 		sellers[machine.Template] = append(sellers[machine.Template], itemSourceLocations(itemSourceLocation{
     92 			fmt.Sprintf("%s (machine %d)", gamedata.ItemTemplates[machine.Template].Name, id), machine.Playfield, machine.Position}, entrances)...)
     93 	}
     94 	for _, id := range slices.Sorted(maps.Keys(gamedata.NPCGroups)) {
     95 		group := gamedata.NPCGroups[id]
     96 		if group.ShopTemplate != 0 {
     97 			for _, position := range group.Positions {
     98 				sellers[group.ShopTemplate] = append(sellers[group.ShopTemplate], itemSourceLocations(itemSourceLocation{group.Name, npcSourcePlayfield(id), position}, entrances)...)
     99 			}
    100 		}
    101 		// Use only profiles admitted for this group at levels it can actually spawn at.
    102 		type dropKey struct {
    103 			family, chance      uint32
    104 			evidence, inference string
    105 		}
    106 		drops := make(map[dropKey]*itemSource)
    107 		for _, level := range slices.Sorted(maps.Keys(gamedata.NPCCombatProfiles[group.MonsterData])) {
    108 			profile := gamedata.NPCCombatProfiles[group.MonsterData][level]
    109 			if level < group.Levels[0] || level > group.Levels[1] || !group.SupportsCombatProfile(group.MonsterData, profile) {
    110 				continue
    111 			}
    112 			for _, drop := range profile.Loot {
    113 				if drop.Chance == 0 {
    114 					continue
    115 				}
    116 				family := itemFamily(drop.Template)
    117 				// Keep distinct probabilities separate rather than inventing an aggregate drop rate.
    118 				key := dropKey{family, drop.Chance, drop.Evidence, drop.Inference}
    119 				source, exists := drops[key]
    120 				if !exists {
    121 					source = &itemSource{kind: "Loot", name: group.Name + " (" + id + ")", quality: drop.Quality, chance: drop.Chance, evidence: drop.Evidence, inference: drop.Inference, level: [2]uint16{level, level}}
    122 					for _, position := range group.Positions {
    123 						source.locations = append(source.locations, itemSourceLocations(itemSourceLocation{group.Name, npcSourcePlayfield(id), position}, entrances)...)
    124 					}
    125 				} else {
    126 					source.quality[0] = min(source.quality[0], drop.Quality[0])
    127 					source.quality[1] = max(source.quality[1], drop.Quality[1])
    128 					source.level[1] = level
    129 				}
    130 				drops[key] = source
    131 			}
    132 		}
    133 		keys := slices.Collect(maps.Keys(drops))
    134 		slices.SortFunc(keys, func(a, b dropKey) int {
    135 			return cmp.Or(cmp.Compare(a.family, b.family), cmp.Compare(a.chance, b.chance), cmp.Compare(a.evidence, b.evidence), cmp.Compare(a.inference, b.inference))
    136 		})
    137 		for _, key := range keys {
    138 			sources[key.family] = append(sources[key.family], drops[key])
    139 		}
    140 		if group.StaticMesh != 0 && len(group.Positions) > 0 {
    141 			template := gamedata.NPCNames.Templates[group.TemplateCode].ItemTemplate
    142 			source := &itemSource{kind: "World pickup", name: group.Name, quality: [2]uint32{uint32(group.Levels[0]), min(uint32(group.Levels[1]), uint32(group.Levels[0])+uint32(len(group.Positions))-1)}}
    143 			for _, position := range group.Positions {
    144 				source.locations = append(source.locations, itemSourceLocations(itemSourceLocation{group.Name, npcSourcePlayfield(id), position}, entrances)...)
    145 			}
    146 			sources[itemFamily(template)] = append(sources[itemFamily(template)], source)
    147 		}
    148 	}
    149 	for _, template := range slices.Sorted(maps.Keys(sellers)) {
    150 		// A vendor offers many templates at the same QL and locations.
    151 		offers := make(map[[2]uint32]*itemSource)
    152 		for _, entry := range gamedata.ShopStock[template] {
    153 			low, high := shopQualityRange(entry)
    154 			quality := [2]uint32{low, high}
    155 			source := offers[quality]
    156 			if source == nil {
    157 				source = &itemSource{kind: "Shop", name: gamedata.ItemTemplates[template].Name, shop: template, quality: quality, locations: sellers[template]}
    158 				offers[quality] = source
    159 			}
    160 			family := itemFamily(entry.Template)
    161 			sources[family] = append(sources[family], source)
    162 		}
    163 	}
    164 	// Recipes, mission rewards/objectives and issued equipment are also acquisition paths.
    165 	for _, tool := range slices.Sorted(maps.Keys(gamedata.ItemCombinations)) {
    166 		for _, material := range slices.Sorted(maps.Keys(gamedata.ItemCombinations[tool])) {
    167 			recipe := gamedata.ItemCombinations[tool][material]
    168 			result := gamedata.ItemTemplates[recipe.Result]
    169 			sources[itemFamily(recipe.Result)] = append(sources[itemFamily(recipe.Result)], &itemSource{kind: "Crafted", name: "Combine", ingredients: [2]uint32{tool, material}, quality: [2]uint32{*result.Quality, *result.Quality}})
    170 		}
    171 	}
    172 	for _, template := range slices.Sorted(maps.Keys(gamedata.MissionTemplates)) {
    173 		mission := gamedata.MissionTemplates[template]
    174 		for _, item := range append(slices.Clone(mission.RewardItems), mission.ObjectiveItem) {
    175 			if item == 0 {
    176 				continue
    177 			}
    178 			quality := mission.RewardQuality
    179 			kind := "Mission reward"
    180 			if item == mission.ObjectiveItem {
    181 				kind = "Mission objective"
    182 				quality = *gamedata.ItemTemplates[item].Quality
    183 			}
    184 			sources[itemFamily(item)] = append(sources[itemFamily(item)], &itemSource{kind: kind, name: mission.Description, quality: [2]uint32{quality, quality}, locations: []itemSourceLocation{{"Mission booth", gamedata.MissionBooths[template].Playfield, gamedata.MissionBooths[template].Position}}})
    185 		}
    186 	}
    187 	issued := make(map[uint32]bool)
    188 	for profession := uint16(1); profession <= 12; profession++ {
    189 		for side := uint16(0); side <= 2; side++ {
    190 			for _, template := range startingItemTemplates(profession, side) {
    191 				if issued[template] {
    192 					continue
    193 				}
    194 				issued[template] = true
    195 				item := gamedata.ItemTemplates[template]
    196 				sources[itemFamily(template)] = append(sources[itemFamily(template)], &itemSource{kind: "Starter equipment", name: "Issued during character creation", quality: [2]uint32{*item.Quality, *item.Quality}})
    197 			}
    198 		}
    199 	}
    200 	server.catalog.families = sources
    201 	server.catalog.entrances = entrances
    202 	server.catalog.buildRows()
    203 }
    204 
    205 func itemSourceLocations(location itemSourceLocation, entrances map[uint32][]itemSourceLocation) []itemSourceLocation {
    206 	doors := entrances[location.playfield]
    207 	if len(doors) == 0 {
    208 		return []itemSourceLocation{location}
    209 	}
    210 	locations := slices.Clone(doors)
    211 	name := location.name + " - " + gamedata.Playfields[location.playfield].Name + " entrance"
    212 	for i := range locations {
    213 		locations[i].name = name
    214 	}
    215 	return locations
    216 }
    217 
    218 func outdoorEntrances(interior uint32, incoming map[uint32][]itemSourceLocation) []itemSourceLocation {
    219 	// Walk outward through nested interiors, stopping at outdoor doors.
    220 	var locations []itemSourceLocation
    221 	pending := []uint32{interior}
    222 	visited := make(map[uint32]bool)
    223 	for len(pending) != 0 {
    224 		playfield := pending[0]
    225 		pending = pending[1:]
    226 		if visited[playfield] {
    227 			continue
    228 		}
    229 		visited[playfield] = true
    230 		for _, entrance := range incoming[playfield] {
    231 			if gamedata.Playfields[entrance.playfield].Outdoor {
    232 				locations = append(locations, entrance)
    233 			} else {
    234 				pending = append(pending, entrance.playfield)
    235 			}
    236 		}
    237 	}
    238 	return locations
    239 }
    240 
    241 func itemSourceSummary(sources []*itemSource) string {
    242 	shops := make(map[uint32]int)
    243 	loot := make(map[string]bool)
    244 	other := make(map[string]bool)
    245 	for _, source := range sources {
    246 		if source.kind == "Shop" {
    247 			shops[source.shop] = len(source.locations)
    248 		} else if source.kind == "Loot" {
    249 			loot[source.name] = true
    250 		} else {
    251 			other[source.kind] = true
    252 		}
    253 	}
    254 	var parts []string
    255 	count := 0
    256 	for _, n := range shops {
    257 		count += n
    258 	}
    259 	if count != 0 {
    260 		parts = append(parts, fmt.Sprintf("Shops: %d", count))
    261 	}
    262 	if len(loot) != 0 {
    263 		parts = append(parts, fmt.Sprintf("Loot: %d", len(loot)))
    264 	}
    265 	parts = append(parts, slices.Sorted(maps.Keys(other))...)
    266 	if len(parts) == 0 {
    267 		return "No known acquisition source"
    268 	}
    269 	return strings.Join(parts, "; ")
    270 }
    271 
    272 func itemLootSummary(sources []*itemSource) (string, string) {
    273 	chances := make(map[string]bool)
    274 	basis := make(map[string]bool)
    275 	for _, source := range sources {
    276 		if source.kind != "Loot" {
    277 			continue
    278 		}
    279 		chances[itemLootChance(source)] = true
    280 		if source.inference != "" {
    281 			basis["Inferred"] = true
    282 		} else if source.evidence != "" {
    283 			basis["Evidence recorded"] = true
    284 		} else {
    285 			basis["Unspecified"] = true
    286 		}
    287 	}
    288 	if len(chances) == 0 {
    289 		return "No configured drop", ""
    290 	}
    291 	var rates []string
    292 	for _, chance := range slices.Sorted(maps.Keys(chances)) {
    293 		rates = append(rates, chance)
    294 	}
    295 	return strings.Join(rates, ", "), strings.Join(slices.Sorted(maps.Keys(basis)), ", ")
    296 }
    297 
    298 func itemLootChance(source *itemSource) string {
    299 	if source.pool == "" {
    300 		return fmt.Sprintf("%d%%", source.chance)
    301 	}
    302 	low := float64(source.chance) / float64(source.candidates[1])
    303 	high := float64(source.chance) / float64(source.candidates[0])
    304 	if low == high {
    305 		return fmt.Sprintf("%.4g%%", low)
    306 	}
    307 	return fmt.Sprintf("%.4g-%.4g%%", low, high)
    308 }