package rooms import ( "strconv" "strings" rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v3" "gno.land/p/nt/ufmt/v0" ) // Debriefs and styles: free reads that replay a room's recorded inputs and // study one side's play, so an agent can learn from a match, and spar // against a rival's style before a rematch. // direction table, per mille, as the brawl rules use (16 = still) var dX = [17]int{1000, 924, 707, 383, 0, -383, -707, -924, -1000, -924, -707, -383, 0, 383, 707, 924, 0} var dY = [17]int{0, 383, 707, 924, 1000, 924, 707, 383, 0, -383, -707, -924, -1000, -924, -707, -383, 0} // heading reports whether move m points within about 45 degrees of (tx, ty). func heading(m, tx, ty int) bool { if m == rules.Still || (tx == 0 && ty == 0) { return false } dot := dX[m]*tx + dY[m]*ty if dot <= 0 { return false } // cos 45 = 0.707: dot^2 >= 0.5 * |d|^2 * |t|^2, with |d| = 1000 return dot/1000*dot/1000*2 >= tx*tx+ty*ty } func iabs(v int) int { if v < 0 { return -v } return v } // replayer walks a room's recorded inputs tick by tick. type replayer struct { g *rules.Game a, b string } func newReplayer(r *Room) *replayer { if r.Phase == Waiting { panic("room #" + strconv.Itoa(r.ID) + " hasn't started") } 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} } // stats counts one side's habits over a stretch of ticks. type stats struct { ticks, still, stuck, corner, turns int nearChance, chased int // rival close with gold: did you go for them? jackSeen, jackGo int atkChance, atkHit, blkChance, blkHit int spcChance, spcHit, hurt, dodged int aheadNear, away int robbed, robs int } // step plays one tick and counts side s's habits. func (p *replayer) step(s int, st *stats, prev *int, gold *[2]int, dodgeWin *int) { g := p.g t := g.T m0, a0, _ := rules.Decode(p.a[t]) m1, a1, _ := rules.Decode(p.b[t]) mv, act := m0, a0 if s == 1 { mv, act = m1, a1 } me, o := g.F[s], g.F[1-s] ex, ey := o.X-me.X, o.Y-me.Y od := ex*ex + ey*ey st.ticks++ if mv == rules.Still { st.still++ } if *prev >= 0 && mv != *prev { st.turns++ } *prev = mv if (me.X < 1500 || me.X > rules.SizeU-1500) && (me.Y < 1500 || me.Y > rules.SizeU-1500) { st.corner++ } if mv != rules.Still { if o.Gold > 0 && od < 3000*3000 { st.nearChance++ if heading(mv, ex, ey) { st.chased++ } } if g.Coin[rules.Mid*rules.Size+rules.Mid] == 3 { st.jackSeen++ if heading(mv, rules.Mid*rules.U+rules.U/2-me.X, rules.Mid*rules.U+rules.U/2-me.Y) { st.jackGo++ } } if me.Gold > o.Gold && od < 3000*3000 { st.aheadNear++ if heading(mv, -ex, -ey) { st.away++ } } } near := od <= rules.Melee*rules.Melee if near && o.Gold > 0 && t >= me.AtkCD { st.atkChance++ if act == rules.Attack { st.atkHit++ } } if near { st.blkChance++ if act == rules.Block { st.blkHit++ } } if t >= me.CD { st.spcChance++ if act == rules.Special { st.spcHit++ } } x0, y0 := me.X, me.Y g.Step(m0, a0, m1, a1) if mv != rules.Still && iabs(g.F[s].X-x0)+iabs(g.F[s].Y-y0) < 60 { st.stuck++ } mine, theirs := g.F[s].Gold, g.F[1-s].Gold if mine < gold[s] && theirs > gold[1-s] { st.robbed++ } if mine > gold[s] && theirs < gold[1-s] { st.robs++ } if mine < gold[s] { st.hurt++ *dodgeWin = 6 } else if *dodgeWin > 0 { if act == rules.Dodge { st.dodged++ *dodgeWin = 0 } else { *dodgeWin-- } } gold[s], gold[1-s] = mine, theirs } func secs(ticks int) int { return (ticks + rules.TPS/2) / rules.TPS } // Debrief replays a room and explains, round by round, how side (0 = A, // 1 = B) did: gold won and lost, time stuck on walls, in corners or // standing still, robberies, and one line of advice per problem. func Debrief(id, side int) string { r := mustRoom(id) if side != 0 && side != 1 { panic("side: 0 for A, 1 for B") } p := newReplayer(r) played := len(p.a) thoughts := strings.Split(r.P[side].Thoughts, "\n") var b strings.Builder b.WriteString(ufmt.Sprintf(`{"room":%d,"side":"%s","name":%s,"rounds":[`, r.ID, string(rune('A'+side)), strconv.Quote(r.P[side].Name))) prev, dodgeWin := -1, 0 var gold [2]int total := stats{} for round := 0; round*r.RoundTicks < played; round++ { st := stats{} start := gold for i := 0; i < r.RoundTicks && p.g.T < played; i++ { p.step(side, &st, &prev, &gold, &dodgeWin) } gain, rivalGain := gold[side]-start[side], gold[1-side]-start[1-side] advice := []string{} if round < len(thoughts) && thoughts[round] == "" { advice = append(advice, "your fallback bot played this round") } if secs(st.stuck) >= 2 { advice = append(advice, ufmt.Sprintf("walked into walls for %d s: use goto or preview the plan", secs(st.stuck))) } if secs(st.corner) >= 4 { advice = append(advice, ufmt.Sprintf("spent %d s in a corner, far from the gold", secs(st.corner))) } if st.still*2 > st.ticks { advice = append(advice, ufmt.Sprintf("stood still for %d s", secs(st.still))) } if rivalGain >= 2*gain+3 { advice = append(advice, ufmt.Sprintf("out-collected %d to %d: aim for coins you reach first", rivalGain, gain)) } if st.robbed >= 2 { advice = append(advice, ufmt.Sprintf("robbed %d times: keep your distance or block", st.robbed)) } if round > 0 { b.WriteString(",") } 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}`, round+1, gold[0], gold[1], gain, rivalGain, secs(st.stuck), secs(st.corner), secs(st.still), st.robbed, st.robs, jsonList(advice))) total.stuck += st.stuck total.corner += st.corner total.robbed += st.robbed total.robs += st.robs } b.WriteString(ufmt.Sprintf(`],"final":[%d,%d],"stuckSeconds":%d,"cornerSeconds":%d,"robbed":%d,"robs":%d}`, gold[0], gold[1], secs(total.stuck), secs(total.corner), total.robbed, total.robs)) return b.String() } func digit(hit, n, def int) int { if n == 0 { return def } v := (hit*9 + n/2) / n if v > 9 { v = 9 } return v } func clampInt(v, lo, hi int) int { if v < lo { return lo } if v > hi { return hi } return v } // LearnStyle replays a room and turns side's play into a bot profile (the // eleven traits of the brawl package's profile bots), so anyone can spar // against that player's style with Spar. Returns {"profile": ..., "traits": ...}. func LearnStyle(id, side int) string { r := mustRoom(id) if side != 0 && side != 1 { panic("side: 0 for A, 1 for B") } p := newReplayer(r) st := stats{} prev, dodgeWin := -1, 0 var gold [2]int for p.g.T < len(p.a) { p.step(side, &st, &prev, &gold, &dodgeWin) } think := 6 if st.turns > 0 { think = clampInt((st.ticks+st.turns/2)/st.turns, 3, 9) } idle := 0 if st.ticks > 0 { idle = clampInt((st.still*20+st.ticks/2)/st.ticks, 0, 9) } keep := 0 if st.aheadNear > 0 { keep = clampInt((st.away*5+st.aheadNear/2)/st.aheadNear, 0, 5) } special := digit(st.spcHit*10, st.spcChance, 5) // specials are pressed about once per ten chances prof := &rules.Profile{ Class: r.P[side].Class, Think: think, Idle: idle, Chase: digit(st.chased, st.nearChance, 4), Jackpot: digit(st.jackGo, st.jackSeen, 5), Reach: 6, Attack: digit(st.atkHit, st.atkChance, 4), Block: digit(st.blkHit, st.blkChance, 2), Special: special, Dodge: digit(st.dodged, st.hurt, 3), Keep: keep, } s := prof.String() rules.MustProfile(s) // must be a valid profile 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}}`, 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) }