package brawl import ( "strconv" "strings" ) // Snapshot packs everything that changes during a match into a short string, // so a realm can store a live match cheaply and carry on later with Restore. // The floor isn't stored: it comes back from the seed. func (g *Game) Snapshot() string { n := []int{int(g.Seed), g.End, g.T, g.NCoins, g.NPower, g.nextCoin, g.nextPower, g.powerKind, b2i(g.jackpot), b2i(g.rushOn), int(g.rng)} for s := 0; s < 2; s++ { f := &g.F[s] n = append(n, f.Class, f.X, f.Y, f.VX, f.VY, f.Gold, b2i(f.Shield), b2i(f.Double), f.CD, f.AtkCD, f.BlockCD, f.BlockT, f.FeintT, f.InvT, f.DashT, f.DashX, f.DashY, f.StunT, f.TrapT, f.BoostT, f.PadT, f.SwingT, f.DodgeCD, f.Face, f.Cell) } // coins and power-ups as square*4 + value pairs for c := 0; c < Cells; c++ { if g.Coin[c] != 0 { n = append(n, c*4+int(g.Coin[c])) } } n = append(n, -1) for c := 0; c < Cells; c++ { if g.Power[c] != 0 { n = append(n, c*4+int(g.Power[c])) } } return joinInts(n) } // Restore rebuilds a match from a Snapshot. func Restore(snap string) *Game { n := splitInts(snap) g := New(uint32(n[0]), n[11], n[11+25]) g.End, g.T, g.NCoins, g.NPower, g.nextCoin, g.nextPower, g.powerKind = n[1], n[2], n[3], n[4], n[5], n[6], n[7] g.jackpot, g.rushOn, g.rng = n[8] == 1, n[9] == 1, uint32(n[10]) i := 11 for s := 0; s < 2; s++ { f := &g.F[s] f.Class, f.X, f.Y, f.VX, f.VY, f.Gold = n[i], n[i+1], n[i+2], n[i+3], n[i+4], n[i+5] f.Shield, f.Double = n[i+6] == 1, n[i+7] == 1 f.CD, f.AtkCD, f.BlockCD, f.BlockT, f.FeintT, f.InvT = n[i+8], n[i+9], n[i+10], n[i+11], n[i+12], n[i+13] f.DashT, f.DashX, f.DashY, f.StunT, f.TrapT, f.BoostT = n[i+14], n[i+15], n[i+16], n[i+17], n[i+18], n[i+19] f.PadT, f.SwingT, f.DodgeCD, f.Face, f.Cell = n[i+20], n[i+21], n[i+22], n[i+23], n[i+24] i += 25 } g.Coin = [Cells]byte{} g.Power = [Cells]byte{} for ; n[i] != -1; i++ { g.Coin[n[i]/4] = byte(n[i] % 4) } for i++; i < len(n); i++ { g.Power[n[i]/4] = byte(n[i] % 4) } return g } // Snapshot packs a bot's memory the same way. func (b *Bot) Snapshot() string { return joinInts([]int{b.Level, int(b.rng), b.wait, b.dir, b.target, b.rescan, b.stuck, b.lastX, b.lastY, b.turn}) } // RestoreBot rebuilds a bot from its Snapshot. func RestoreBot(snap string) *Bot { n := splitInts(snap) return &Bot{Level: n[0], rng: uint32(n[1]), wait: n[2], dir: n[3], target: n[4], rescan: n[5], stuck: n[6], lastX: n[7], lastY: n[8], turn: n[9]} } func b2i(v bool) int { if v { return 1 } return 0 } func joinInts(n []int) string { parts := make([]string, len(n)) for i, v := range n { parts[i] = strconv.Itoa(v) } return strings.Join(parts, ",") } func splitInts(s string) []int { parts := strings.Split(s, ",") n := make([]int, len(parts)) for i, p := range parts { v, err := strconv.Atoi(p) if err != nil { panic("brawl: bad snapshot") } n[i] = v } return n } // An Actor is a bot a realm can keep between rounds: a house Bot or a // ProfileBot. type Actor interface { Act(g *Game, s int) (int, int) Snapshot() string } // Snapshot packs a profile bot's memory: its profile, then its counters. func (b *ProfileBot) Snapshot() string { return b.P.String() + "," + joinInts([]int{int(b.rng), b.wait, b.dir, b.target, b.rescan, b.stuck, b.lastX, b.lastY, b.turn, b.gold}) } // RestoreProfileBot rebuilds a profile bot from its Snapshot. func RestoreProfileBot(snap string) *ProfileBot { if len(snap) < ProfileLen+1 || snap[ProfileLen] != ',' { panic("brawl: bad snapshot") } p := MustProfile(snap[:ProfileLen]) n := splitInts(snap[ProfileLen+1:]) if len(n) != 10 { panic("brawl: bad snapshot") } return &ProfileBot{P: *p, rng: uint32(n[0]), wait: n[1], dir: n[2], target: n[3], rescan: n[4], stuck: n[5], lastX: n[6], lastY: n[7], turn: n[8], gold: n[9]} } // RestoreActor rebuilds either kind of bot from its Snapshot. func RestoreActor(snap string) Actor { if len(snap) > 0 && snap[0] == 'c' { return RestoreProfileBot(snap) } return RestoreBot(snap) }