1package sim 2 3import ( 4 "fmt" 5 "math/rand/v2" 6 7 "tinyadventure/internal/model" 8 "tinyadventure/internal/rng" 9) 10 11var Rarities = []string{"common", "uncommon", "rare", "epic", "legendary"} 12 13var rarityMult = map[string]float64{"common": 1.0, "uncommon": 1.25, "rare": 1.5, "epic": 1.8, "legendary": 2.2} 14 15var rarityWeights = [5]int{60, 25, 10, 4, 1} 16 17var rarityPrefix = map[string]string{"common": "Plain", "uncommon": "Fine", "rare": "Runed", "epic": "Moonlit", "legendary": "Ancient"} 18 19type base struct { 20 name string 21 slot model.Slot 22 s model.Stats 23} 24 25var bases = []base{ 26 {"Sword", model.SlotWeapon, model.Stats{Attack: 3, Crit: 2}}, 27 {"Axe", model.SlotWeapon, model.Stats{Attack: 4, Crit: 3, Defense: -1}}, 28 {"Mace", model.SlotWeapon, model.Stats{Attack: 3, Defense: 1}}, 29 {"Dagger", model.SlotWeapon, model.Stats{Attack: 2, Speed: 2, Crit: 4}}, 30 {"Staff", model.SlotWeapon, model.Stats{Magic: 4, Attack: 1}}, 31 {"Plate", model.SlotArmor, model.Stats{Defense: 5, MaxHP: 8, Speed: -2}}, 32 {"Mail", model.SlotArmor, model.Stats{Defense: 3, MaxHP: 6}}, 33 {"Jerkin", model.SlotArmor, model.Stats{Defense: 2, MaxHP: 4, Speed: 2}}, 34 {"Robe", model.SlotArmor, model.Stats{Defense: 1, MaxHP: 4, Magic: 3}}, 35 {"Helm", model.SlotHelm, model.Stats{Defense: 2, MaxHP: 5}}, 36 {"Cap", model.SlotHelm, model.Stats{Defense: 1, MaxHP: 3, Speed: 1}}, 37 {"Hood", model.SlotHelm, model.Stats{MaxHP: 3, Crit: 3}}, 38 {"Circlet", model.SlotHelm, model.Stats{MaxHP: 2, Magic: 3}}, 39 {"Amulet", model.SlotTrinket, model.Stats{Magic: 2, MaxHP: 5}}, 40 {"Ring", model.SlotTrinket, model.Stats{Attack: 1, Crit: 4}}, 41 {"Charm", model.SlotTrinket, model.Stats{Defense: 1, Speed: 2}}, 42 {"Idol", model.SlotTrinket, model.Stats{Magic: 4, Speed: -1}}, 43} 44 45var Slots = []model.Slot{model.SlotWeapon, model.SlotArmor, model.SlotHelm, model.SlotTrinket} 46 47var slotAbilities = map[model.Slot][]string{ 48 model.SlotWeapon: {"heavy-strike", "envenom", "firebolt", "ice-lance", "lightning-bolt", "smite", "shadow-bolt"}, 49 model.SlotArmor: {"taunt", "dodge", "rage"}, 50 model.SlotHelm: {"heal", "ward", "mark"}, 51 model.SlotTrinket: {"cleave", "poison-cloud", "fireball", "frost-nova", "chain-lightning", "radiance", "shadow-wave", "rally", "haste", "prayer", "sneak"}, 52} 53 54func tierScale(tier int) float64 { return 1 + 0.3*float64(max(tier, 1)-1) } 55 56func scaleStat(v int, f float64) int { 57 if v <= 0 { 58 return v 59 } 60 return max(1, int(float64(v)*f+0.5)) 61} 62 63func rollRarity(r *rand.Rand, tier int) string { 64 w := rarityWeights 65 shift := max(tier, 1) - 1 66 for i := 1; i < 5; i++ { 67 w[i] += shift * i * 2 68 } 69 w[0] = max(10, w[0]-shift*8) 70 total := 0 71 for _, v := range w { 72 total += v 73 } 74 n := r.IntN(total) 75 for i, v := range w { 76 if n < v { 77 return Rarities[i] 78 } 79 n -= v 80 } 81 return Rarities[0] 82} 83 84func BuildItem(r *rand.Rand, slot model.Slot, rarity string, tier int) model.Item { 85 var pool []base 86 for _, b := range bases { 87 if b.slot == slot { 88 pool = append(pool, b) 89 } 90 } 91 b := rng.Pick(r, pool) 92 f := tierScale(tier) * rarityMult[rarity] 93 it := model.Item{ 94 ID: fmt.Sprintf("it-%08x", r.Uint32()), 95 Name: rarityPrefix[rarity] + " " + b.name, 96 Slot: slot, 97 Rarity: rarity, 98 Bonus: model.Stats{ 99 MaxHP: scaleStat(b.s.MaxHP, f), 100 Attack: scaleStat(b.s.Attack, f), 101 Defense: scaleStat(b.s.Defense, f), 102 Magic: scaleStat(b.s.Magic, f), 103 Speed: scaleStat(b.s.Speed, f/2), 104 Crit: scaleStat(b.s.Crit, f), 105 }, 106 } 107 if rarityMult[rarity] >= rarityMult["rare"] { 108 it.Abilities = []string{rng.Pick(r, slotAbilities[slot])} 109 } 110 it.Value = model.Price(it) 111 Imbue(r.IntN, &it) 112 return it 113} 114 115func RollItem(r *rand.Rand, tier int) model.Item { 116 return BuildItem(r, rng.Pick(r, Slots), rollRarity(r, tier), tier) 117} 118 119func Effective(u model.Unit) model.Stats { 120 s := u.Stats 121 for _, it := range u.Gear { 122 if it == nil { 123 continue 124 } 125 s.MaxHP += it.Bonus.MaxHP 126 s.Attack += it.Bonus.Attack 127 s.Defense += it.Bonus.Defense 128 s.Magic += it.Bonus.Magic 129 s.Speed += it.Bonus.Speed 130 s.Crit += it.Bonus.Crit 131 } 132 s.HP = min(u.Stats.HP, s.MaxHP) 133 return s 134} 135 136func AllAbilities(u model.Unit) []model.Ability { 137 out := append([]model.Ability(nil), u.Abilities...) 138 seen := map[string]bool{} 139 for _, a := range out { 140 seen[a.ID] = true 141 } 142 for _, slot := range Slots { 143 it := u.Gear[slot] 144 if it == nil { 145 continue 146 } 147 for _, id := range it.Abilities { 148 if a, ok := Lookup(id); ok && !seen[id] { 149 seen[id] = true 150 out = append(out, a) 151 } 152 } 153 } 154 return out 155}