server/gamedata/npc_combat.go (3040B)
1 package gamedata 2 3 import ( 4 "fmt" 5 "maps" 6 "slices" 7 ) 8 9 // Expand only the bounded ranges between authored level anchors, never extrapolate. 10 func interpolateNPCProfiles(profiles map[uint32]map[uint16]*NPCCombatProfile) error { 11 for monster, levels := range profiles { 12 anchors := slices.Sorted(maps.Keys(levels)) 13 for index := 1; index < len(anchors); index++ { 14 low, high := anchors[index-1], anchors[index] 15 if high-low == 1 { 16 continue 17 } 18 first, second := levels[low], levels[high] 19 if !slices.Equal(first.TemplateCodes, second.TemplateCodes) || !slices.Equal(first.Groups, second.Groups) || first.Weapon != second.Weapon || 20 first.Initiative != second.Initiative || first.AggDef != second.AggDef || 21 first.RespawnSeconds != second.RespawnSeconds || first.CorpseSeconds != second.CorpseSeconds || 22 first.Credits != second.Credits || len(first.Loot) != len(second.Loot) { 23 return fmt.Errorf("NPC combat anchors %d/%d..%d change noninterpolated settings", monster, low, high) 24 } 25 for index, drop := range first.Loot { 26 if drop.Template != second.Loot[index].Template || drop.Chance != second.Loot[index].Chance || drop.Evidence != second.Loot[index].Evidence || drop.Inference != second.Loot[index].Inference { 27 return fmt.Errorf("NPC combat anchors %d/%d..%d change loot identity, chance or evidence", monster, low, high) 28 } 29 } 30 31 // Each resolved level owns its maps and loot; catalog anchors remain unchanged. 32 for level := low + 1; level < high; level++ { 33 interpolate := func(first, second int64) int64 { 34 return first + (second-first)*int64(level-low)/int64(high-low) 35 } 36 profile := *first 37 profile.TemplateCodes = slices.Clone(first.TemplateCodes) 38 profile.Groups = slices.Clone(first.Groups) 39 profile.Health = uint32(interpolate(int64(first.Health), int64(second.Health))) 40 profile.Experience = uint32(interpolate(int64(first.Experience), int64(second.Experience))) 41 profile.Skills, profile.Modifiers = maps.Clone(first.Skills), maps.Clone(first.Modifiers) 42 for _, pair := range []struct{ first, second, result map[uint32]int32 }{ 43 {first.Skills, second.Skills, profile.Skills}, {first.Modifiers, second.Modifiers, profile.Modifiers}, 44 } { 45 if len(pair.first) != len(pair.second) { 46 return fmt.Errorf("NPC combat anchors %d/%d..%d change stat membership", monster, low, high) 47 } 48 for stat, value := range pair.first { 49 upper, exists := pair.second[stat] 50 if !exists { 51 return fmt.Errorf("NPC combat anchors %d/%d..%d omit stat %d", monster, low, high, stat) 52 } 53 pair.result[stat] = int32(interpolate(int64(value), int64(upper))) 54 } 55 } 56 profile.Loot = slices.Clone(first.Loot) 57 for index := range profile.Loot { 58 for endpoint := range profile.Loot[index].Quality { 59 profile.Loot[index].Quality[endpoint] = uint32(interpolate(int64(first.Loot[index].Quality[endpoint]), int64(second.Loot[index].Quality[endpoint]))) 60 } 61 } 62 levels[level] = &profile 63 } 64 } 65 } 66 return nil 67 }