Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

snapshot.gno

4.00 Kb · 130 lines
  1package brawl
  2
  3import (
  4	"strconv"
  5	"strings"
  6)
  7
  8// Snapshot packs everything that changes during a match into a short string,
  9// so a realm can store a live match cheaply and carry on later with Restore.
 10// The floor isn't stored: it comes back from the seed.
 11func (g *Game) Snapshot() string {
 12	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)}
 13	for s := 0; s < 2; s++ {
 14		f := &g.F[s]
 15		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,
 16			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, f.Combo, f.ComboT)
 17	}
 18	// coins and power-ups as square*4 + value pairs
 19	for c := 0; c < Cells; c++ {
 20		if g.Coin[c] != 0 {
 21			n = append(n, c*4+int(g.Coin[c]))
 22		}
 23	}
 24	n = append(n, -1)
 25	for c := 0; c < Cells; c++ {
 26		if g.Power[c] != 0 {
 27			n = append(n, c*4+int(g.Power[c]))
 28		}
 29	}
 30	return joinInts(n)
 31}
 32
 33// Restore rebuilds a match from a Snapshot.
 34func Restore(snap string) *Game {
 35	n := splitInts(snap)
 36	g := New(uint32(n[0]), n[11], n[11+27])
 37	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]
 38	g.jackpot, g.rushOn, g.rng = n[8] == 1, n[9] == 1, uint32(n[10])
 39	i := 11
 40	for s := 0; s < 2; s++ {
 41		f := &g.F[s]
 42		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]
 43		f.Shield, f.Double = n[i+6] == 1, n[i+7] == 1
 44		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]
 45		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]
 46		f.PadT, f.SwingT, f.DodgeCD, f.Face, f.Cell, f.Combo, f.ComboT = n[i+20], n[i+21], n[i+22], n[i+23], n[i+24], n[i+25], n[i+26]
 47		i += 27
 48	}
 49	g.Coin = [Cells]byte{}
 50	g.Power = [Cells]byte{}
 51	for ; n[i] != -1; i++ {
 52		g.Coin[n[i]/4] = byte(n[i] % 4)
 53	}
 54	for i++; i < len(n); i++ {
 55		g.Power[n[i]/4] = byte(n[i] % 4)
 56	}
 57	return g
 58}
 59
 60// Snapshot packs a bot's memory the same way.
 61func (b *Bot) Snapshot() string {
 62	return joinInts([]int{b.Level, int(b.rng), b.wait, b.dir, b.target, b.rescan, b.stuck, b.lastX, b.lastY, b.turn})
 63}
 64
 65// RestoreBot rebuilds a bot from its Snapshot.
 66func RestoreBot(snap string) *Bot {
 67	n := splitInts(snap)
 68	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]}
 69}
 70
 71func b2i(v bool) int {
 72	if v {
 73		return 1
 74	}
 75	return 0
 76}
 77
 78func joinInts(n []int) string {
 79	parts := make([]string, len(n))
 80	for i, v := range n {
 81		parts[i] = strconv.Itoa(v)
 82	}
 83	return strings.Join(parts, ",")
 84}
 85
 86func splitInts(s string) []int {
 87	parts := strings.Split(s, ",")
 88	n := make([]int, len(parts))
 89	for i, p := range parts {
 90		v, err := strconv.Atoi(p)
 91		if err != nil {
 92			panic("brawl: bad snapshot")
 93		}
 94		n[i] = v
 95	}
 96	return n
 97}
 98
 99// An Actor is a bot a realm can keep between rounds: a house Bot or a
100// ProfileBot.
101type Actor interface {
102	Act(g *Game, s int) (int, int)
103	Snapshot() string
104}
105
106// Snapshot packs a profile bot's memory: its profile, then its counters.
107func (b *ProfileBot) Snapshot() string {
108	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})
109}
110
111// RestoreProfileBot rebuilds a profile bot from its Snapshot.
112func RestoreProfileBot(snap string) *ProfileBot {
113	if len(snap) < ProfileLen+1 || snap[ProfileLen] != ',' {
114		panic("brawl: bad snapshot")
115	}
116	p := MustProfile(snap[:ProfileLen])
117	n := splitInts(snap[ProfileLen+1:])
118	if len(n) != 10 {
119		panic("brawl: bad snapshot")
120	}
121	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]}
122}
123
124// RestoreActor rebuilds either kind of bot from its Snapshot.
125func RestoreActor(snap string) Actor {
126	if len(snap) > 0 && snap[0] == 'c' {
127		return RestoreProfileBot(snap)
128	}
129	return RestoreBot(snap)
130}