xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / internal / sim / choice.go 143 lines3.6 kB Raw
1package sim
2
3import (
4	"slices"
5
6	"tinyadventure/internal/model"
7	"tinyadventure/internal/rng"
8)
9
10func (b *battle) pickTarget(actor *combatant, foes []*combatant) *combatant {
11	if seen := slices.DeleteFunc(slices.Clone(foes), (*combatant).hidden); len(seen) > 0 {
12		foes = seen
13	}
14	if t := actor.tauntedBy(foes); t != nil {
15		return t
16	}
17	switch actor.role {
18	case RoleTank:
19		if slices.Contains(foes, actor.hurtBy) {
20			return actor.hurtBy
21		}
22	case RoleStriker:
23		best := foes[0]
24		for _, f := range foes[1:] {
25			if f.hp < best.hp {
26				best = f
27			}
28		}
29		return best
30	}
31	return rng.Pick(b.r, foes)
32}
33
34func (b *battle) choose(actor *combatant, extra bool) model.Ability {
35	allies, foes := b.side(actor.side), b.side(1-actor.side)
36	ready := map[string]model.Ability{}
37	for _, a := range actor.abils {
38		if actor.cd[a.ID] == 0 && !(extra && a.ID == "haste") {
39			ready[a.ID] = a
40		}
41	}
42	a := func(id string) float64 { return ready[id].Power }
43	use := func(id string, when bool) (model.Ability, bool) {
44		a, ok := ready[id]
45		return a, ok && when
46	}
47	low := 0
48	hurt := false
49	for _, al := range allies {
50		if al.hp*10 < al.stats.MaxHP*7 {
51			low++
52		}
53		if al != actor && al.hurt() {
54			hurt = true
55		}
56	}
57	for _, try := range []struct {
58		id   string
59		when bool
60	}{
61		{"prayer", low >= 2},
62		{"heal", slices.ContainsFunc(allies, (*combatant).hurt)},
63		{"taunt", hurt && !actor.hurt()},
64		{"dodge", actor.hurt()},
65		{"sneak", actor.hurt()},
66		{"ward", !actor.under("ward") && b.threatens(foes, allies)},
67		{"rage", b.payback(actor, foes, a("rage")*b.best(actor, foes, 1))},
68		{"rally", b.payback(actor, foes, a("rally")*b.team(allies, foes, 1))},
69		{"mark", b.payback(actor, foes, a("mark")*b.team(allies, foes, 1)/float64(len(foes)))},
70		{"haste", len(allies) > 1 && b.payback(actor, foes, b.best(b.hasteTarget(actor), foes, 0))},
71	} {
72		if a, ok := use(try.id, try.when); ok {
73			return a
74		}
75	}
76	best, bestScore := mustAbility(StrikeID), -1.0
77	for _, a := range slices.Concat(actor.abils, []model.Ability{best}) {
78		if (a.Kind != KindDamage && a.Kind != KindAoE) || actor.cd[a.ID] > 0 {
79			continue
80		}
81		if s := b.expected(actor, a, foes); s > bestScore {
82			best, bestScore = a, s
83		}
84	}
85	return best
86}
87
88func (b *battle) payback(actor *combatant, foes []*combatant, gain float64) bool {
89	return gain > b.best(actor, foes, 0)
90}
91
92func (b *battle) best(c *combatant, foes []*combatant, slack int) float64 {
93	top := 0.0
94	for _, a := range slices.Concat(c.abils, []model.Ability{mustAbility(StrikeID)}) {
95		if (a.Kind == KindDamage || a.Kind == KindAoE) && c.cd[a.ID] <= slack {
96			top = max(top, b.expected(c, a, foes))
97		}
98	}
99	return top
100}
101
102func (b *battle) team(allies, foes []*combatant, slack int) float64 {
103	n := 0.0
104	for _, al := range allies {
105		n += b.best(al, foes, slack)
106	}
107	return n
108}
109
110func (b *battle) expected(actor *combatant, a model.Ability, foes []*combatant) float64 {
111	typ := actor.typeOf(a)
112	total, top := 0.0, 0.0
113	for _, f := range foes {
114		v := float64(max(actor.statFor(a), 1)) * a.Power
115		if slices.Contains(f.unit.Weak, typ) {
116			v *= weakMult
117		}
118		if f.guarded(typ) && !actor.has("pierce") {
119			v = v * 10 / float64(10+f.defense())
120		}
121		total += v
122		top = max(top, v)
123	}
124	if a.Kind == KindAoE {
125		return total
126	}
127	return top
128}
129
130func (b *battle) threatens(foes, allies []*combatant) bool {
131	for _, f := range foes {
132		for _, a := range f.abils {
133			typ := f.typeOf(a)
134			if (a.Kind == KindDamage || a.Kind == KindAoE) && slices.ContainsFunc(allies, func(al *combatant) bool { return !al.guarded(typ) }) {
135				return true
136			}
137		}
138		if typ := f.typeOf(mustAbility(StrikeID)); slices.ContainsFunc(allies, func(al *combatant) bool { return !al.guarded(typ) }) {
139			return true
140		}
141	}
142	return false
143}