package brawl import ( "strconv" "testing" ) // gen makes a match of inputs the same way the browser's parity check does. func gen(seed uint32) string { b := make([]byte, Ticks) s := seed dir, hold := 0, 0 for t := 0; t < Ticks; t++ { s = s*1103515245 + 12345 r := int(s >> 8) if hold == 0 { dir = r % 17 hold = 5 + r%12 } hold-- act := 0 if r%7 == 0 { act = 1 + (r>>4)%4 } b[t] = Encode(dir, act) } return string(b) } // fingerprint plays a match and folds both fighters' state into a number // every tick, so any difference between two copies of the rules shows up. func fingerprint(seed uint32, ca, cb int, inA, inB string, mirror bool) string { g := New(seed, ca, cb) var h uint32 for t := 0; t < Ticks; t++ { m0, a0 := in(inA, t) m1, a1 := in(inB, t) if mirror && m1 != Still { m1 = (m1 + 8) % 16 } g.Step(m0, a0, m1, a1) for s := 0; s < 2; s++ { f := g.F[s] h = h*31 + uint32(f.X+7*f.Y+13*f.Gold) } h = h*31 + uint32(g.NCoins+5*g.NPower) } return strconv.Itoa(g.F[0].Gold) + "-" + strconv.Itoa(g.F[1].Gold) + ":" + strconv.FormatUint(uint64(h), 10) } func TestParity(t *testing.T) { out := "" for i := 0; i < 30; i++ { seed := uint32(i * 7) out += fingerprint(seed, i%5, (i*3+1)%5, gen(uint32(2*i+1)), gen(uint32(2*i+2)), i%2 == 1) + " " } t.Logf("PARITY %s", out) } func botFingerprint(seed uint32, ca, level int, inA string) string { g := New(seed, ca, BotClass[level][seed%3]) b := NewBot(level, seed) var h uint32 for t := 0; t < Ticks; t++ { m0, a0 := in(inA, t) m1, a1 := b.Act(g, 1) g.Step(m0, a0, m1, a1) for s := 0; s < 2; s++ { f := g.F[s] h = h*31 + uint32(f.X+7*f.Y+13*f.Gold) } } return strconv.Itoa(g.F[0].Gold) + "-" + strconv.Itoa(g.F[1].Gold) + ":" + strconv.FormatUint(uint64(h), 10) } func TestBotParity(t *testing.T) { out := "" for i := 0; i < 18; i++ { out += botFingerprint(uint32(i*5+1), i%5, i%3, gen(uint32(100+i))) + " " } t.Logf("BOTPARITY %s", out) } // Move recipes: the browser must accept and refuse the same recipes, cost // them the same, and play matches with them the same. var recipeChecks = []string{ "Blink Thief/84/D82S11", "Haymaker/150/S21K91T41", "Sniper/54/S15K25", "Magnet/40/P24", "Turtle/147/H00I90", "Bully/90/D41T53K94", "Grab Dash/33/P11D62", "Flee/36/D83", "A/32/S11", "Rush-in/50/D71I30", "O'Hare/150/S22P13T21", "Z/200/P35", "Pick/20/P11", "A/20/S11", "A/31/S11", "Sniper/53/S15K25", "", "x", "A/20/", "A/020/S11", "A/19/H00", "A/201/H00", " A/20/H00", "A /20/H00", "A/20/H01", "A/20/S11S11", "A/20/Q11", "A/20/S1", "A/99/S11H00I90K11", "A|B/20/H00", "A/30/S12H00", "Thief Lord/200/S35", "ABCDEFGHIJKLMNOPQ/20/H00", "A/20/D80", "A/20/D90", "A/20/S01", "A/20/S16", "A/20/I91", "A/20/T05", "A/20/K95", "A/2x/H00", "A/20/H00/", "A/20/h00", } func recipeLine() string { out := "" for _, s := range recipeChecks { r, err := ParseRecipe(s) if err != nil { out += "x " continue } out += r.String() + "=" + strconv.Itoa(r.Cost()) + " " } return out } func validRecipes() []*Recipe { rs := []*Recipe{} for _, s := range recipeChecks { if r, err := ParseRecipe(s); err == nil { rs = append(rs, r) } } return rs } func moveFingerprint(seed uint32, ca, cb int, inA, inB string, mirror bool, ra, rb *Recipe) string { g := New(seed, ca, cb) g.Equip(0, ra) g.Equip(1, rb) var h uint32 for t := 0; t < Ticks; t++ { m0, a0 := in(inA, t) m1, a1 := in(inB, t) if mirror && m1 != Still { m1 = (m1 + 8) % 16 } g.Step(m0, a0, m1, a1) for s := 0; s < 2; s++ { f := g.F[s] h = h*31 + uint32(f.X+7*f.Y+13*f.Gold+17*f.StunT+19*f.InvT) } h = h*31 + uint32(g.NCoins+5*g.NPower) } return strconv.Itoa(g.F[0].Gold) + "-" + strconv.Itoa(g.F[1].Gold) + ":" + strconv.FormatUint(uint64(h), 10) } func TestParityMoves(t *testing.T) { t.Logf("RECIPES %s", recipeLine()) rs := validRecipes() n := len(rs) out := "" for i := 0; i < 22; i++ { ra, rb := rs[i%n], rs[(i*3+1)%n] if i%4 == 0 { rb = nil } out += moveFingerprint(uint32(i*11+3), i%5, (i*2+3)%5, gen(uint32(300+2*i)), gen(uint32(301+2*i)), i%2 == 1, ra, rb) + " " } t.Logf("MOVES %s", out) bo := "" for i := 0; i < 11; i++ { inA := gen(uint32(400 + i)) a, b := VersusMove(uint32(i*13+2), i%5, i%3, inA, rs[i%n].String()) bo += strconv.Itoa(a) + "-" + strconv.Itoa(b) + " " } t.Logf("MOVEBOT %s", bo) } // Bot profiles: the browser must accept and refuse the same profiles and // play the same matches against them. var profileChecks = []string{ "c1t3i1f6j8r6a7b2s5d4k0", "c0t3i0f9j9r8a9b9s9d9k5", "c4t9i9f0j0r1a0b0s0d0k0", "c2t4i2f1j5r4a3b6s9d8k3", "c3t3i0f7j2r8a9b1s7d2k1", "c0t5i4f3j9r3a5b5s5d5k2", "c1t3i0f0j0r8a0b0s0d0k0", "", "c5t3i1f6j8r6a7b2s5d4k0", "c1t1i1f6j8r6a7b2s5d4k0", "c1t2i1f6j8r6a7b2s5d4k0", "c1t3i1f6j8r9a7b2s5d4k0", "c1t3i1f6j8r0a7b2s5d4k0", "c1t3i1f6j8r6a7b2s5d4k6", "c1t3i1f6j8r6a7b2s5d4k", "c1t3i1f6j8r6a7b2s5d4k00", "t3c1i1f6j8r6a7b2s5d4k0", "C1t3i1f6j8r6a7b2s5d4k0", "c1t3i1f6j8r6a7b2s5d4kx", "c1t3i1f6j8r6a7b2s5d4k/", "c1t3 i1f6j8r6a7b2s5d4k0", } func profileFingerprint(seed uint32, ca int, p *Profile, inA string) string { g := New(seed, ca, p.Class) b := NewProfileBot(p, seed) var h uint32 for t := 0; t < Ticks; t++ { m0, a0 := in(inA, t) m1, a1 := b.Act(g, 1) g.Step(m0, a0, m1, a1) for s := 0; s < 2; s++ { f := g.F[s] h = h*31 + uint32(f.X+7*f.Y+13*f.Gold+17*f.StunT+19*f.InvT) } } return strconv.Itoa(g.F[0].Gold) + "-" + strconv.Itoa(g.F[1].Gold) + ":" + strconv.FormatUint(uint64(h), 10) } func TestParityProfiles(t *testing.T) { out := "" ps := []*Profile{} for _, s := range profileChecks { p, err := ParseProfile(s) if err != nil { out += "x " continue } ps = append(ps, p) out += p.String() + " " } n := len(ps) for i := 0; i < 14; i++ { out += profileFingerprint(uint32(i*17+4), i%5, ps[i%n], gen(uint32(500+i))) + " " } a, b := VersusProfile(9, 2, ps[0].String(), gen(600)) out += strconv.Itoa(a) + "-" + strconv.Itoa(b) t.Logf("PROFILEBOT %s", out) }