profile.gno
6.03 Kb · 231 lines
1package brawl
2
3import (
4 "errors"
5 "strconv"
6)
7
8// Bot profiles: a bot written as a handful of whole-number traits instead of
9// code, so a player's own bot fits in one short string on chain. The page
10// learns the traits from how you play; NewProfileBot plays with them.
11//
12// The string is eleven letter-digit pairs in this order, for example
13// "c1t3i1f6j8r6a7b2s5d4k0":
14//
15// c class it plays, 0..4 (Hunter, Brawler, Coward, Trickster, Reader)
16// t ticks between plans, 3..9 (lower re-plans more often)
17// i chance to stand still when it plans, 5% per step, 0..9
18// f chance to chase a rival who carries gold, 11% per step, 0..9
19// j chance to go for the jackpot when it is out, 11% per step, 0..9
20// r how far it looks for gold, in squares, 1..8
21// a chance to swing at a rival in reach, each tick, 11% per step, 0..9
22// b chance to block a rival in reach, each tick, 11% per step, 0..9
23// s chance to fire its special when it pays, 11% per step, 0..9
24// d chance to dodge right after losing gold, 11% per step, 0..9
25// k runs from a rival closer than this many squares while ahead, 0..5 (0 never)
26//
27// The house levels (Easy, Normal, Hard) are left as they are.
28
29// ProfileLen is the length of an encoded profile.
30const ProfileLen = 22
31
32const profileKeys = "ctifjrabsdk"
33
34var profileLo = [11]int{0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0}
35var profileHi = [11]int{4, 9, 9, 9, 9, 8, 9, 9, 9, 9, 5}
36
37type Profile struct {
38 Class int
39 Think int
40 Idle int
41 Chase int
42 Jackpot int
43 Reach int
44 Attack int
45 Block int
46 Special int
47 Dodge int
48 Keep int
49}
50
51func (p *Profile) field(i int) *int {
52 switch i {
53 case 0:
54 return &p.Class
55 case 1:
56 return &p.Think
57 case 2:
58 return &p.Idle
59 case 3:
60 return &p.Chase
61 case 4:
62 return &p.Jackpot
63 case 5:
64 return &p.Reach
65 case 6:
66 return &p.Attack
67 case 7:
68 return &p.Block
69 case 8:
70 return &p.Special
71 case 9:
72 return &p.Dodge
73 }
74 return &p.Keep
75}
76
77// ParseProfile reads an encoded profile, refusing anything out of range.
78func ParseProfile(s string) (*Profile, error) {
79 if len(s) != ProfileLen {
80 return nil, errors.New("profile: 11 letter-digit pairs, like c1t3i1f6j8r6a7b2s5d4k0")
81 }
82 p := &Profile{}
83 for i := 0; i < 11; i++ {
84 if s[2*i] != profileKeys[i] {
85 return nil, errors.New("profile: expected " + profileKeys[i:i+1] + " at " + strconv.Itoa(2*i+1))
86 }
87 v := int(s[2*i+1]) - '0'
88 if v < profileLo[i] || v > profileHi[i] {
89 return nil, errors.New("profile: " + profileKeys[i:i+1] + " is " + strconv.Itoa(profileLo[i]) + " to " + strconv.Itoa(profileHi[i]))
90 }
91 *p.field(i) = v
92 }
93 return p, nil
94}
95
96// MustProfile is ParseProfile that panics on a bad profile.
97func MustProfile(s string) *Profile {
98 p, err := ParseProfile(s)
99 if err != nil {
100 panic(err.Error())
101 }
102 return p
103}
104
105// String writes the profile back in its encoded form.
106func (p *Profile) String() string {
107 b := make([]byte, ProfileLen)
108 for i := 0; i < 11; i++ {
109 b[2*i], b[2*i+1] = profileKeys[i], byte('0'+*p.field(i))
110 }
111 return string(b)
112}
113
114// A ProfileBot plays one side of a match with a profile's traits. It moves
115// like the house bot (plan, head for gold or the rival, slide round walls)
116// and rolls its own dice for every choice, so a replay is exact.
117type ProfileBot struct {
118 P Profile
119 rng uint32
120 wait int
121 dir int
122 target int
123 rescan int
124 stuck int
125 lastX int
126 lastY int
127 turn int
128 gold int
129}
130
131func NewProfileBot(p *Profile, seed uint32) *ProfileBot {
132 return &ProfileBot{P: *p, rng: seed*2654435761 + 7, dir: Still, target: -1}
133}
134
135func (b *ProfileBot) rand(n int) int {
136 b.rng = b.rng*1103515245 + 12345
137 return int(b.rng>>8) % n
138}
139
140// Act decides the bot's move and button for this tick, as side s.
141func (b *ProfileBot) Act(g *Game, s int) (int, int) {
142 p := &b.P
143 me, o := &g.F[s], &g.F[1-s]
144 ex, ey := o.X-me.X, o.Y-me.Y
145 od := ex*ex + ey*ey
146 think := b.wait == 0
147 if !think {
148 b.wait--
149 } else {
150 b.wait = p.Think - 1
151 if b.rand(1000) < p.Idle*50 {
152 b.dir = Still
153 } else {
154 kr := p.Keep * U
155 if me.Gold > o.Gold && od < kr*kr {
156 b.dir = toward(-ex, -ey) // ahead: keep the rival away
157 } else {
158 tx, ty := o.X, o.Y
159 if !(o.Gold > 0 && od < 3000*3000 && b.rand(1000) < p.Chase*111) {
160 if g.Coin[Mid*Size+Mid] == 3 && b.rand(1000) < p.Jackpot*111 {
161 b.target = Mid*Size + Mid
162 } else if b.rescan <= 0 || b.target >= 0 && (g.Coin[b.target] == 0 || b.target == Mid*Size+Mid) {
163 // look again; after an empty look, wait 3 plans (it is the
164 // costly part of a replay when the floor is bare)
165 b.target = g.ringCoin(me, p.Reach)
166 b.rescan = 10
167 if b.target < 0 {
168 b.rescan = 3
169 }
170 }
171 b.rescan--
172 if b.target >= 0 {
173 tx, ty = b.target%Size*U+U/2, b.target/Size*U+U/2
174 }
175 }
176 b.dir = toward(tx-me.X, ty-me.Y)
177 }
178 if b.stuck > 2 {
179 if b.turn == 0 {
180 b.dir = (b.dir + 4) % 16
181 } else {
182 b.dir = (b.dir + 12) % 16
183 }
184 b.turn = 1 - b.turn
185 b.stuck = 0
186 b.wait = 3
187 }
188 }
189 }
190 if b.dir != Still && abs(me.X-b.lastX)+abs(me.Y-b.lastY) < 60 {
191 b.stuck++
192 } else {
193 b.stuck = 0
194 }
195 b.lastX, b.lastY = me.X, me.Y
196 lost := me.Gold < b.gold
197 b.gold = me.Gold
198 t := g.T
199 near := od <= Melee*Melee
200 act := None
201 if lost && t >= me.DodgeCD && b.rand(1000) < p.Dodge*111 {
202 act = Dodge
203 } else if think && t >= me.CD && specialPays(g, me, o, od, b.dir, b.target) && b.rand(1000) < p.Special*111 {
204 act = Special
205 } else if near && o.Gold > 0 && t >= me.AtkCD && b.rand(1000) < p.Attack*111 {
206 act = Attack
207 } else if near && t >= me.BlockCD && b.rand(1000) < p.Block*111 {
208 act = Block
209 }
210 return b.dir, act
211}
212
213// VersusProfile replays a run (side A's inputs) against a profile bot on
214// side B, which plays the profile's class.
215func VersusProfile(seed uint32, classA int, profile, inA string) (int, int) {
216 if classA < Hunter || classA > Reader {
217 panic("brawl: no such class")
218 }
219 if len(inA) > Ticks {
220 panic("brawl: inputs longer than a match")
221 }
222 p := MustProfile(profile)
223 g := New(seed, classA, p.Class)
224 b := NewProfileBot(p, seed)
225 for t := 0; t < Ticks; t++ {
226 m0, a0 := in(inA, t)
227 m1, a1 := b.Act(g, 1)
228 g.Step(m0, a0, m1, a1)
229 }
230 return g.F[0].Gold, g.F[1].Gold
231}