aorecomp

Anarchy Online classic in the browser

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


server/gamedata/loot.go (3252B)


      1 package gamedata
      2 
      3 import "fmt"
      4 
      5 // Ordinary pools use nearby QLs, intersected with the original item's bounds.
      6 // Named drops keep their explicit quality independently of NPC level.
      7 func (pool *LootPool) Quality(template uint32, level uint16) (uint32, uint32) {
      8 	if level < pool.MinimumLevel {
      9 		return 1, 0
     10 	}
     11 	if pool.FixedQuality {
     12 		if *ItemTemplates[template].Quality > uint32(level)+max(1, uint32(level)/10) {
     13 			return 1, 0
     14 		}
     15 		return *ItemTemplates[template].Quality, *ItemTemplates[template].Quality
     16 	}
     17 	low, high := *ItemTemplates[template].Quality, *ItemTemplates[template].Quality
     18 	if pair, exists := ItemFamilies[template]; exists {
     19 		low, high = *ItemTemplates[pair[0]].Quality, *ItemTemplates[pair[1]].Quality
     20 	}
     21 	variation := max(1, uint32(level)/10)
     22 	return max(low, max(1, uint32(level))-variation), min(high, uint32(level)+variation)
     23 }
     24 
     25 func loadLoot() {
     26 	loadYAML("loot_pools.yaml", &LootPools)
     27 	loadYAML("loot_tables.yaml", &LootTables)
     28 	for name, pool := range LootPools {
     29 		if len(pool.Items) == 0 || pool.Evidence == "" && pool.Inference == "" {
     30 			panic(fmt.Errorf("loot pool %s needs items and provenance", name))
     31 		}
     32 		seen := make(map[uint32]bool)
     33 		for _, template := range pool.Items {
     34 			item := ItemTemplates[template]
     35 			if item == nil || item.Quality == nil || *item.Quality == 0 || seen[template] {
     36 				panic(fmt.Errorf("loot pool %s has invalid or duplicate item %d", name, template))
     37 			}
     38 			seen[template] = true
     39 		}
     40 		pool.Candidates = make(map[uint16][]uint32)
     41 	}
     42 	for name, table := range LootTables {
     43 		if table.Name == "" || len(table.Groups) == 0 || len(table.Pools)+len(table.Drops) == 0 {
     44 			panic(fmt.Errorf("loot table %s needs a name, populations and rewards", name))
     45 		}
     46 		for _, drop := range table.Drops {
     47 			if drop.Chance == 0 || drop.Chance > 100 || drop.Quality[0] > drop.Quality[1] ||
     48 				!ItemQualitySupported(drop.Template, drop.Quality[0]) || !ItemQualitySupported(drop.Template, drop.Quality[1]) || drop.Evidence == "" && drop.Inference == "" {
     49 				panic(fmt.Errorf("loot table %s has invalid drop %d", name, drop.Template))
     50 			}
     51 		}
     52 		for _, roll := range table.Pools {
     53 			if LootPools[roll.Pool] == nil || roll.Chance == 0 || roll.Chance > 100 {
     54 				panic(fmt.Errorf("loot table %s has invalid pool %s", name, roll.Pool))
     55 			}
     56 		}
     57 		for _, id := range table.Groups {
     58 			group := NPCGroups[id]
     59 			if group == nil || group.ShopTemplate != 0 || group.StaticMesh != 0 || NPCLootTables[id] != nil ||
     60 				MonsterTemplates[group.MonsterData] == nil || len(MonsterTemplates[group.MonsterData].DeathAnimations) == 0 {
     61 				panic(fmt.Errorf("loot table %s has invalid or duplicate population %s", name, id))
     62 			}
     63 			NPCLootTables[id] = table
     64 			// Resolve candidates once per pool/level, not on every death or catalog page.
     65 			for _, roll := range table.Pools {
     66 				pool := LootPools[roll.Pool]
     67 				for level := group.Levels[0]; level <= group.Levels[1]; level++ {
     68 					if _, exists := pool.Candidates[level]; exists {
     69 						continue
     70 					}
     71 					pool.Candidates[level] = nil
     72 					for _, template := range pool.Items {
     73 						low, high := pool.Quality(template, level)
     74 						if low <= high {
     75 							pool.Candidates[level] = append(pool.Candidates[level], template)
     76 						}
     77 					}
     78 				}
     79 			}
     80 		}
     81 	}
     82 }