debrief.gno
7.83 Kb · 270 lines
1package rooms
2
3import (
4 "strconv"
5 "strings"
6
7 rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v3"
8 "gno.land/p/nt/ufmt/v0"
9)
10
11// Debriefs and styles: free reads that replay a room's recorded inputs and
12// study one side's play, so an agent can learn from a match, and spar
13// against a rival's style before a rematch.
14
15// direction table, per mille, as the brawl rules use (16 = still)
16var dX = [17]int{1000, 924, 707, 383, 0, -383, -707, -924, -1000, -924, -707, -383, 0, 383, 707, 924, 0}
17var dY = [17]int{0, 383, 707, 924, 1000, 924, 707, 383, 0, -383, -707, -924, -1000, -924, -707, -383, 0}
18
19// heading reports whether move m points within about 45 degrees of (tx, ty).
20func heading(m, tx, ty int) bool {
21 if m == rules.Still || (tx == 0 && ty == 0) {
22 return false
23 }
24 dot := dX[m]*tx + dY[m]*ty
25 if dot <= 0 {
26 return false
27 }
28 // cos 45 = 0.707: dot^2 >= 0.5 * |d|^2 * |t|^2, with |d| = 1000
29 return dot/1000*dot/1000*2 >= tx*tx+ty*ty
30}
31
32func iabs(v int) int {
33 if v < 0 {
34 return -v
35 }
36 return v
37}
38
39// replayer walks a room's recorded inputs tick by tick.
40type replayer struct {
41 g *rules.Game
42 a, b string
43}
44
45func newReplayer(r *Room) *replayer {
46 if r.Phase == Waiting {
47 panic("room #" + strconv.Itoa(r.ID) + " hasn't started")
48 }
49 return &replayer{g: rules.NewLength(r.Seed, r.P[0].Class, r.P[1].Class, r.MatchTicks), a: r.P[0].Inputs, b: r.P[1].Inputs}
50}
51
52// stats counts one side's habits over a stretch of ticks.
53type stats struct {
54 ticks, still, stuck, corner, turns int
55 nearChance, chased int // rival close with gold: did you go for them?
56 jackSeen, jackGo int
57 atkChance, atkHit, blkChance, blkHit int
58 spcChance, spcHit, hurt, dodged int
59 aheadNear, away int
60 robbed, robs int
61}
62
63// step plays one tick and counts side s's habits.
64func (p *replayer) step(s int, st *stats, prev *int, gold *[2]int, dodgeWin *int) {
65 g := p.g
66 t := g.T
67 m0, a0, _ := rules.Decode(p.a[t])
68 m1, a1, _ := rules.Decode(p.b[t])
69 mv, act := m0, a0
70 if s == 1 {
71 mv, act = m1, a1
72 }
73 me, o := g.F[s], g.F[1-s]
74 ex, ey := o.X-me.X, o.Y-me.Y
75 od := ex*ex + ey*ey
76 st.ticks++
77 if mv == rules.Still {
78 st.still++
79 }
80 if *prev >= 0 && mv != *prev {
81 st.turns++
82 }
83 *prev = mv
84 if (me.X < 1500 || me.X > rules.SizeU-1500) && (me.Y < 1500 || me.Y > rules.SizeU-1500) {
85 st.corner++
86 }
87 if mv != rules.Still {
88 if o.Gold > 0 && od < 3000*3000 {
89 st.nearChance++
90 if heading(mv, ex, ey) {
91 st.chased++
92 }
93 }
94 if g.Coin[rules.Mid*rules.Size+rules.Mid] == 3 {
95 st.jackSeen++
96 if heading(mv, rules.Mid*rules.U+rules.U/2-me.X, rules.Mid*rules.U+rules.U/2-me.Y) {
97 st.jackGo++
98 }
99 }
100 if me.Gold > o.Gold && od < 3000*3000 {
101 st.aheadNear++
102 if heading(mv, -ex, -ey) {
103 st.away++
104 }
105 }
106 }
107 near := od <= rules.Melee*rules.Melee
108 if near && o.Gold > 0 && t >= me.AtkCD {
109 st.atkChance++
110 if act == rules.Attack {
111 st.atkHit++
112 }
113 }
114 if near {
115 st.blkChance++
116 if act == rules.Block {
117 st.blkHit++
118 }
119 }
120 if t >= me.CD {
121 st.spcChance++
122 if act == rules.Special {
123 st.spcHit++
124 }
125 }
126 x0, y0 := me.X, me.Y
127 g.Step(m0, a0, m1, a1)
128 if mv != rules.Still && iabs(g.F[s].X-x0)+iabs(g.F[s].Y-y0) < 60 {
129 st.stuck++
130 }
131 mine, theirs := g.F[s].Gold, g.F[1-s].Gold
132 if mine < gold[s] && theirs > gold[1-s] {
133 st.robbed++
134 }
135 if mine > gold[s] && theirs < gold[1-s] {
136 st.robs++
137 }
138 if mine < gold[s] {
139 st.hurt++
140 *dodgeWin = 6
141 } else if *dodgeWin > 0 {
142 if act == rules.Dodge {
143 st.dodged++
144 *dodgeWin = 0
145 } else {
146 *dodgeWin--
147 }
148 }
149 gold[s], gold[1-s] = mine, theirs
150}
151
152func secs(ticks int) int { return (ticks + rules.TPS/2) / rules.TPS }
153
154// Debrief replays a room and explains, round by round, how side (0 = A,
155// 1 = B) did: gold won and lost, time stuck on walls, in corners or
156// standing still, robberies, and one line of advice per problem.
157func Debrief(id, side int) string {
158 r := mustRoom(id)
159 if side != 0 && side != 1 {
160 panic("side: 0 for A, 1 for B")
161 }
162 p := newReplayer(r)
163 played := len(p.a)
164 thoughts := strings.Split(r.P[side].Thoughts, "\n")
165 var b strings.Builder
166 b.WriteString(ufmt.Sprintf(`{"room":%d,"side":"%s","name":%s,"rounds":[`, r.ID, string(rune('A'+side)), strconv.Quote(r.P[side].Name)))
167 prev, dodgeWin := -1, 0
168 var gold [2]int
169 total := stats{}
170 for round := 0; round*r.RoundTicks < played; round++ {
171 st := stats{}
172 start := gold
173 for i := 0; i < r.RoundTicks && p.g.T < played; i++ {
174 p.step(side, &st, &prev, &gold, &dodgeWin)
175 }
176 gain, rivalGain := gold[side]-start[side], gold[1-side]-start[1-side]
177 advice := []string{}
178 if round < len(thoughts) && thoughts[round] == "" {
179 advice = append(advice, "your fallback bot played this round")
180 }
181 if secs(st.stuck) >= 2 {
182 advice = append(advice, ufmt.Sprintf("walked into walls for %d s: use goto or preview the plan", secs(st.stuck)))
183 }
184 if secs(st.corner) >= 4 {
185 advice = append(advice, ufmt.Sprintf("spent %d s in a corner, far from the gold", secs(st.corner)))
186 }
187 if st.still*2 > st.ticks {
188 advice = append(advice, ufmt.Sprintf("stood still for %d s", secs(st.still)))
189 }
190 if rivalGain >= 2*gain+3 {
191 advice = append(advice, ufmt.Sprintf("out-collected %d to %d: aim for coins you reach first", rivalGain, gain))
192 }
193 if st.robbed >= 2 {
194 advice = append(advice, ufmt.Sprintf("robbed %d times: keep your distance or block", st.robbed))
195 }
196 if round > 0 {
197 b.WriteString(",")
198 }
199 b.WriteString(ufmt.Sprintf(`{"round":%d,"gold":[%d,%d],"gain":%d,"rivalGain":%d,"stuckSeconds":%d,"cornerSeconds":%d,"stillSeconds":%d,"robbed":%d,"robs":%d,"advice":%s}`,
200 round+1, gold[0], gold[1], gain, rivalGain, secs(st.stuck), secs(st.corner), secs(st.still), st.robbed, st.robs, jsonList(advice)))
201 total.stuck += st.stuck
202 total.corner += st.corner
203 total.robbed += st.robbed
204 total.robs += st.robs
205 }
206 b.WriteString(ufmt.Sprintf(`],"final":[%d,%d],"stuckSeconds":%d,"cornerSeconds":%d,"robbed":%d,"robs":%d}`,
207 gold[0], gold[1], secs(total.stuck), secs(total.corner), total.robbed, total.robs))
208 return b.String()
209}
210
211func digit(hit, n, def int) int {
212 if n == 0 {
213 return def
214 }
215 v := (hit*9 + n/2) / n
216 if v > 9 {
217 v = 9
218 }
219 return v
220}
221
222func clampInt(v, lo, hi int) int {
223 if v < lo {
224 return lo
225 }
226 if v > hi {
227 return hi
228 }
229 return v
230}
231
232// LearnStyle replays a room and turns side's play into a bot profile (the
233// eleven traits of the brawl package's profile bots), so anyone can spar
234// against that player's style with Spar. Returns {"profile": ..., "traits": ...}.
235func LearnStyle(id, side int) string {
236 r := mustRoom(id)
237 if side != 0 && side != 1 {
238 panic("side: 0 for A, 1 for B")
239 }
240 p := newReplayer(r)
241 st := stats{}
242 prev, dodgeWin := -1, 0
243 var gold [2]int
244 for p.g.T < len(p.a) {
245 p.step(side, &st, &prev, &gold, &dodgeWin)
246 }
247 think := 6
248 if st.turns > 0 {
249 think = clampInt((st.ticks+st.turns/2)/st.turns, 3, 9)
250 }
251 idle := 0
252 if st.ticks > 0 {
253 idle = clampInt((st.still*20+st.ticks/2)/st.ticks, 0, 9)
254 }
255 keep := 0
256 if st.aheadNear > 0 {
257 keep = clampInt((st.away*5+st.aheadNear/2)/st.aheadNear, 0, 5)
258 }
259 special := digit(st.spcHit*10, st.spcChance, 5) // specials are pressed about once per ten chances
260 prof := &rules.Profile{
261 Class: r.P[side].Class, Think: think, Idle: idle,
262 Chase: digit(st.chased, st.nearChance, 4), Jackpot: digit(st.jackGo, st.jackSeen, 5), Reach: 6,
263 Attack: digit(st.atkHit, st.atkChance, 4), Block: digit(st.blkHit, st.blkChance, 2),
264 Special: special, Dodge: digit(st.dodged, st.hurt, 3), Keep: keep,
265 }
266 s := prof.String()
267 rules.MustProfile(s) // must be a valid profile
268 return ufmt.Sprintf(`{"room":%d,"side":"%s","name":%s,"profile":"%s","traits":{"thinksEvery":%d,"idle":%d,"chase":%d,"jackpot":%d,"attack":%d,"block":%d,"special":%d,"dodge":%d,"keepsAway":%d}}`,
269 r.ID, string(rune('A'+side)), strconv.Quote(r.P[side].Name), s, think, idle, prof.Chase, prof.Jackpot, prof.Attack, prof.Block, prof.Special, prof.Dodge, keep)
270}