xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / internal / sim / traits.go 143 lines4.0 kB Raw
1package sim
2
3import (
4	"slices"
5	"strings"
6
7	"tinyadventure/internal/model"
8)
9
10type trait struct {
11	suffix string
12	slots  []model.Slot
13	cost   model.Stats
14}
15
16var weaponTrinket = []model.Slot{model.SlotWeapon, model.SlotTrinket}
17var armorTrinket = []model.Slot{model.SlotArmor, model.SlotHelm, model.SlotTrinket}
18
19var itemTraits = map[string]trait{
20	"lifesteal": {"of the Leech", weaponTrinket, model.Stats{Defense: 2}},
21	"first":     {"of the Hawk", weaponTrinket, model.Stats{MaxHP: 6}},
22	"sweep":     {"of Sweeping", []model.Slot{model.SlotWeapon}, model.Stats{Crit: 5}},
23	"pierce":    {"of Piercing", []model.Slot{model.SlotWeapon}, model.Stats{Crit: 4}},
24	"edge":      {"", weaponTrinket, model.Stats{Magic: 2}},
25	"thorns":    {"of Thorns", armorTrinket, model.Stats{Speed: 2}},
26	"guard":     {"", armorTrinket, model.Stats{Attack: 2}},
27}
28
29var traitOrder = []string{"lifesteal", "first", "sweep", "pierce", "edge", "thorns", "guard"}
30
31var edgeSuffix = map[string]string{Poison: "of Venom", Fire: "of Embers", Frost: "of Rime", Lightning: "of Storms", Holy: "of Dawn", Shadow: "of Dusk"}
32
33var guardSuffix = map[string]string{Poison: "of Venomguard", Fire: "of Fireguard", Frost: "of Frostguard", Lightning: "of Stormguard", Holy: "of Dawnguard", Shadow: "of Shadowguard"}
34
35const guardPrefix = "guard-"
36
37var traitChance = map[string]int{"common": 0, "uncommon": 45, "rare": 75, "epic": 100, "legendary": 100}
38
39func Imbue(roll func(n int) int, it *model.Item) {
40	if roll(100) >= traitChance[it.Rarity] {
41		return
42	}
43	var fits []string
44	for _, name := range traitOrder {
45		if slices.Contains(itemTraits[name].slots, it.Slot) {
46			fits = append(fits, name)
47		}
48	}
49	name := fits[roll(len(fits))]
50	t := itemTraits[name]
51	suffix := t.suffix
52	it.Trait = name
53	switch name {
54	case "edge":
55		it.Trait = Elements[roll(len(Elements))]
56		suffix = edgeSuffix[it.Trait]
57	case "guard":
58		el := Elements[roll(len(Elements))]
59		it.Trait, suffix = guardPrefix+el, guardSuffix[el]
60	}
61	it.Name += " " + suffix
62	b := &it.Bonus
63	b.MaxHP -= t.cost.MaxHP
64	b.Attack -= t.cost.Attack
65	b.Defense -= t.cost.Defense
66	b.Magic -= t.cost.Magic
67	b.Speed -= t.cost.Speed
68	b.Crit -= t.cost.Crit
69	it.Value = model.Price(*it)
70}
71
72func TraitsOf(u model.Unit) []string {
73	out := append([]string(nil), u.Traits...)
74	for _, slot := range Slots {
75		if it := u.Gear[slot]; it != nil && it.Trait != "" && !slices.Contains(out, it.Trait) {
76			out = append(out, it.Trait)
77		}
78	}
79	return out
80}
81
82func edgeOf(traits []string) string {
83	for _, t := range traits {
84		if slices.Contains(Elements, t) {
85			return t
86		}
87	}
88	return ""
89}
90
91func guardsOf(traits, resist []string) []string {
92	out := slices.Clone(resist)
93	for _, t := range traits {
94		if el, ok := strings.CutPrefix(t, guardPrefix); ok {
95			out = append(out, el)
96		}
97	}
98	return out
99}
100
101func Guards(u model.Unit, typ string) bool {
102	return slices.Contains(guardsOf(TraitsOf(u), u.Resist), typ)
103}
104
105func typesOf(u model.Unit) []string {
106	edge := edgeOf(TraitsOf(u))
107	var out []string
108	for _, a := range slices.Concat(AllAbilities(u), Abilities(StrikeID)) {
109		if a.Kind != KindDamage && a.Kind != KindAoE {
110			continue
111		}
112		typ := a.Type
113		if typ == Physical && edge != "" {
114			typ = edge
115		}
116		if typ != Physical && !slices.Contains(out, typ) {
117			out = append(out, typ)
118		}
119	}
120	return out
121}
122
123func EdgeOf(units, foes []model.Unit) model.Edge {
124	e := model.Edge{Hits: []string{}, Blunted: []string{}}
125	armored := slices.ContainsFunc(foes, func(f model.Unit) bool { return slices.Contains(f.Traits, "armored") })
126	has := func(pick func(model.Unit) []string, el string) bool {
127		return slices.ContainsFunc(foes, func(f model.Unit) bool { return slices.Contains(pick(f), el) })
128	}
129	for _, u := range units {
130		for _, el := range typesOf(u) {
131			switch {
132			case has(func(f model.Unit) []string { return f.Weak }, el):
133				e.Hits = append(e.Hits, el+" ("+u.Name+")")
134			case has(func(f model.Unit) []string { return f.Resist }, el):
135				e.Blunted = append(e.Blunted, el+" ("+u.Name+")")
136			}
137		}
138		if armored && slices.Contains(TraitsOf(u), "pierce") {
139			e.Hits = append(e.Hits, "pierce ("+u.Name+")")
140		}
141	}
142	return e
143}