aorecomp

Anarchy Online classic in the browser

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


server/item_pool_sources.go (5279B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"fmt"
      6 	"maps"
      7 	"math"
      8 	"slices"
      9 	"strings"
     10 )
     11 
     12 func addTableLootSources(sources map[uint32][]*itemSource, entrances map[uint32][]itemSourceLocation) {
     13 	// Coalesce shared pools across encounters. Per-item copies of every spawn
     14 	// would make the catalog index much larger than the underlying world data.
     15 	populations := make(map[gamedata.LootPoolRoll][]string)
     16 	for _, key := range slices.Sorted(maps.Keys(gamedata.LootTables)) {
     17 		table := gamedata.LootTables[key]
     18 		for _, roll := range table.Pools {
     19 			populations[roll] = append(populations[roll], table.Groups...)
     20 		}
     21 		if len(table.Drops) == 0 {
     22 			continue
     23 		}
     24 
     25 		var locations []itemSourceLocation
     26 		levels := [2]uint16{65535, 0}
     27 		for _, id := range table.Groups {
     28 			group := gamedata.NPCGroups[id]
     29 			levels[0], levels[1] = min(levels[0], group.Levels[0]), max(levels[1], group.Levels[1])
     30 			for _, position := range group.Positions {
     31 				locations = append(locations, itemSourceLocations(itemSourceLocation{group.Name, npcSourcePlayfield(id), position}, entrances)...)
     32 			}
     33 		}
     34 		for _, drop := range table.Drops {
     35 			sources[itemFamily(drop.Template)] = append(sources[itemFamily(drop.Template)], &itemSource{
     36 				kind: "Loot", name: table.Name, quality: drop.Quality, chance: drop.Chance,
     37 				evidence: drop.Evidence, inference: drop.Inference, level: levels, locations: locations,
     38 			})
     39 		}
     40 	}
     41 	rolls := slices.Collect(maps.Keys(populations))
     42 	slices.SortFunc(rolls, func(a, b gamedata.LootPoolRoll) int {
     43 		if order := strings.Compare(a.Pool, b.Pool); order != 0 {
     44 			return order
     45 		}
     46 		return int(a.Chance) - int(b.Chance)
     47 	})
     48 	for _, roll := range rolls {
     49 		pool := gamedata.LootPools[roll.Pool]
     50 		levels := make(map[uint16]bool)
     51 		for _, id := range populations[roll] {
     52 			group := gamedata.NPCGroups[id]
     53 			for level := group.Levels[0]; level <= group.Levels[1]; level++ {
     54 				levels[level] = true
     55 			}
     56 		}
     57 		// Reuse the eligible templates that gameplay already indexes by NPC level.
     58 		type eligibility struct {
     59 			level      [2]uint16
     60 			candidates [2]int
     61 		}
     62 		ranges := make(map[uint32]eligibility, len(pool.Items))
     63 		for _, level := range slices.Sorted(maps.Keys(levels)) {
     64 			candidates := pool.Candidates[level]
     65 			for _, template := range candidates {
     66 				r, found := ranges[template]
     67 				if !found {
     68 					r.level[0], r.candidates[0] = level, len(candidates)
     69 				}
     70 				r.level[1] = level
     71 				r.candidates[0] = min(r.candidates[0], len(candidates))
     72 				r.candidates[1] = max(r.candidates[1], len(candidates))
     73 				ranges[template] = r
     74 			}
     75 		}
     76 		name := fmt.Sprintf("%s pool (%d%% roll)", strings.ReplaceAll(roll.Pool, "_", " "), roll.Chance)
     77 		type offer struct {
     78 			eligibility
     79 			quality [2]uint32
     80 		}
     81 		offers := make(map[offer]*itemSource)
     82 		for _, template := range pool.Items {
     83 			r, found := ranges[template]
     84 			if !found {
     85 				continue
     86 			}
     87 			// Both QL bounds are nondecreasing with NPC level.
     88 			low, _ := pool.Quality(template, r.level[0])
     89 			_, high := pool.Quality(template, r.level[1])
     90 			key := offer{r, [2]uint32{low, high}}
     91 			source := offers[key]
     92 			if source == nil {
     93 				source = &itemSource{kind: "Loot", name: name, groups: populations[roll],
     94 					pool: roll.Pool, chance: roll.Chance, evidence: pool.Evidence, inference: pool.Inference,
     95 					level: r.level, quality: key.quality, candidates: r.candidates}
     96 				offers[key] = source
     97 			}
     98 			sources[itemFamily(template)] = append(sources[itemFamily(template)], source)
     99 		}
    100 	}
    101 }
    102 
    103 // Count all source locations, but materialize links and map markers only for
    104 // the requested page. Some single pools cover tens of thousands of spawns.
    105 func (index *itemCatalogIndex) sourcePage(template uint32, page int) ([]*itemSource, int) {
    106 	skip := min(max(1, page)-1, math.MaxInt/adminPageSize) * adminPageSize
    107 	remaining, total := adminPageSize, 0
    108 	window := func(n int) (int, int) {
    109 		total += n
    110 		first := min(skip, n)
    111 		skip -= first
    112 		take := min(remaining, n-first)
    113 		remaining -= take
    114 		return first, first + take
    115 	}
    116 
    117 	sources := slices.Clone(index.families[itemFamily(template)])
    118 	for i, shared := range sources {
    119 		source := *shared
    120 		sources[i] = &source
    121 		first, end := window(len(shared.locations))
    122 		source.locations = shared.locations[first:end:end]
    123 		for _, id := range source.groups {
    124 			group := gamedata.NPCGroups[id]
    125 			if group.Levels[0] > source.level[1] || group.Levels[1] < source.level[0] {
    126 				continue
    127 			}
    128 			playfield := npcSourcePlayfield(id)
    129 			doors := index.entrances[playfield]
    130 			first, end := window(len(group.Positions) * max(1, len(doors)))
    131 			if first == end {
    132 				continue
    133 			}
    134 			name := fmt.Sprintf("%s (levels %d-%d)", group.Name, max(group.Levels[0], source.level[0]), min(group.Levels[1], source.level[1]))
    135 			if len(doors) > 0 {
    136 				name += " - " + gamedata.Playfields[playfield].Name + " entrance"
    137 			}
    138 			for j := first; j < end; j++ {
    139 				if len(doors) == 0 {
    140 					source.locations = append(source.locations, itemSourceLocation{name, playfield, group.Positions[j]})
    141 				} else {
    142 					door := doors[j%len(doors)]
    143 					door.name = name
    144 					source.locations = append(source.locations, door)
    145 				}
    146 			}
    147 		}
    148 	}
    149 	if last := max(1, (total+adminPageSize-1)/adminPageSize); page > last {
    150 		return index.sourcePage(template, last)
    151 	}
    152 	return sources, total
    153 }