bot.gno
6.30 Kb · 247 lines
1package brawl
2
3// House bots: the opponents you fight in the browser, at three levels. They
4// react to the match every tick, so they are fair rivals, and they are
5// deterministic, so the chain can replay a match against them exactly.
6
7// Bot levels.
8const (
9 Easy = iota
10 Normal
11 Hard
12)
13
14type botLevel struct {
15 think int // ticks between decisions
16 idle int // per mille chance to stand still for a decision
17 fight int // per mille chance to go after the rival and use buttons
18 reach int // how far it looks for gold first, in squares
19 guard int // per mille chance, each tick, to block a rival in reach who can swing
20}
21
22var botLevels = [3]botLevel{
23 {think: 4, idle: 30, fight: 600, reach: 8, guard: 0},
24 {think: 3, idle: 80, fight: 900, reach: 8, guard: 150},
25 {think: 2, idle: 0, fight: 200, reach: 8, guard: 250},
26}
27
28// BotClass is the class each level plays on each arena.
29var BotClass = [3][3]int{
30 {Coward, Trickster, Reader},
31 {Hunter, Brawler, Reader},
32 {Brawler, Hunter, Trickster},
33}
34
35// A Bot plays one side of a match.
36type Bot struct {
37 Level int
38 rng uint32
39 wait int
40 dir int
41 target int // square it is heading for, -1 for none
42 rescan int // decisions left before it looks for a better coin
43 stuck int
44 lastX int
45 lastY int
46 turn int
47}
48
49func NewBot(level int, seed uint32) *Bot {
50 return &Bot{Level: level, rng: seed*2654435761 + 1, dir: Still, target: -1}
51}
52
53func (b *Bot) rand(n int) int {
54 b.rng = b.rng*1103515245 + 12345
55 return int(b.rng>>8) % n
56}
57
58// toward picks one of the 16 directions for (dx, dy) with a few comparisons
59// (tan 11.25, 33.75, 56.25 and 78.75 degrees); Still if both are 0.
60func toward(dx, dy int) int {
61 if dx == 0 && dy == 0 {
62 return Still
63 }
64 ax, ay := abs(dx), abs(dy)
65 j := 4
66 if ay*1000 < ax*199 {
67 j = 0
68 } else if ay*1000 < ax*668 {
69 j = 1
70 } else if ay*1000 < ax*1497 {
71 j = 2
72 } else if ay*1000 < ax*5027 {
73 j = 3
74 }
75 if dx >= 0 && dy >= 0 {
76 return j
77 }
78 if dx < 0 && dy >= 0 {
79 return 8 - j
80 }
81 if dx < 0 {
82 return 8 + j
83 }
84 return (16 - j) % 16
85}
86
87// ringCoin looks for gold ring by ring outward from the fighter's square,
88// up to r squares away, and stops one ring after the first find. -1 if none.
89func (g *Game) ringCoin(me *Fighter, r int) int {
90 cx, cy := me.X/U, me.Y/U
91 best, bd, found := -1, 0, -1
92 for k := 0; k <= r; k++ {
93 if found >= 0 && k > found+1 {
94 break
95 }
96 for y := cy - k; y <= cy+k; y++ {
97 if y < 0 || y >= Size {
98 continue
99 }
100 step := 2 * k
101 if y == cy-k || y == cy+k || k == 0 {
102 step = 1
103 }
104 for x := cx - k; x <= cx+k; x += step {
105 if x < 0 || x >= Size || g.Coin[y*Size+x] == 0 {
106 continue
107 }
108 dx, dy := x*U+U/2-me.X, y*U+U/2-me.Y
109 if d := dx*dx + dy*dy; best < 0 || d < bd {
110 best, bd = y*Size+x, d
111 if found < 0 {
112 found = k
113 }
114 }
115 }
116 }
117 }
118 return best
119}
120
121// looseCoin finds gold on the fighter's square or one of the 8 round it (the
122// first in ringX order), or -1.
123func (g *Game) looseCoin(me *Fighter) int {
124 cx, cy := me.X/U, me.Y/U
125 if c := cy*Size + cx; g.Coin[c] != 0 {
126 return c
127 }
128 for k := 0; k < 8; k++ {
129 x, y := cx+ringX[k], cy+ringY[k]
130 if x >= 0 && y >= 0 && x < Size && y < Size && g.Coin[y*Size+x] != 0 {
131 return y*Size + x
132 }
133 }
134 return -1
135}
136
137// Act decides the bot's move and button for this tick, as side s.
138func (b *Bot) Act(g *Game, s int) (int, int) {
139 lv := botLevels[b.Level]
140 me, o := &g.F[s], &g.F[1-s]
141 think := b.wait == 0
142 if !think {
143 b.wait--
144 } else {
145 b.wait = lv.think - 1
146 if b.rand(1000) < lv.idle {
147 b.dir = Still
148 } else {
149 tx, ty := o.X, o.Y
150 ex, ey := o.X-me.X, o.Y-me.Y
151 od := ex*ex + ey*ey
152 loose := g.looseCoin(me) // gold right next to it (knocked loose, say) comes first
153 chase := loose < 0 && o.Gold > 0 && od < 2500*2500 && (o.Gold >= me.Gold || od < 1600*1600) && b.rand(1000) < lv.fight
154 if !chase {
155 if loose >= 0 {
156 b.target = loose
157 } else if g.Coin[Mid*Size+Mid] == 3 {
158 b.target = Mid*Size + Mid // the jackpot is worth the trip
159 } else if b.target < 0 || g.Coin[b.target] == 0 || b.rescan <= 0 {
160 b.target = g.ringCoin(me, lv.reach)
161 b.rescan = 10
162 }
163 b.rescan--
164 if b.target >= 0 {
165 tx, ty = b.target%Size*U+U/2, b.target/Size*U+U/2
166 }
167 }
168 b.dir = toward(tx-me.X, ty-me.Y)
169 if b.stuck > 2 { // walking into a wall: slide round it for a moment
170 if b.turn == 0 {
171 b.dir = (b.dir + 4) % 16
172 } else {
173 b.dir = (b.dir + 12) % 16
174 }
175 b.turn = 1 - b.turn
176 b.stuck = 0
177 b.wait = 3
178 }
179 }
180 }
181 if b.dir != Still && abs(me.X-b.lastX)+abs(me.Y-b.lastY) < 60 {
182 b.stuck++
183 } else {
184 b.stuck = 0
185 }
186 b.lastX, b.lastY = me.X, me.Y
187 act := None
188 if lv.fight > 0 {
189 t := g.T
190 ex, ey := me.X-o.X, me.Y-o.Y
191 dd := ex*ex + ey*ey
192 if think && t >= me.CD && b.pays(g, me, o, dd) {
193 act = Special
194 } else if dd <= Melee*Melee && o.Gold > 0 && t >= me.AtkCD {
195 act = Attack
196 } else if lv.guard > 0 && dd <= Melee*Melee && t >= o.AtkCD && t >= me.BlockCD && me.Gold > 0 && b.rand(1000) < lv.guard {
197 act = Block
198 }
199 }
200 return b.dir, act
201}
202
203func (b *Bot) pays(g *Game, me, o *Fighter, dd int) bool {
204 return specialPays(g, me, o, dd, b.dir, b.target)
205}
206
207// specialPays says whether the class special is worth firing now: dir is where the
208// bot is heading and target the coin square it wants (-1 for none).
209func specialPays(g *Game, me, o *Fighter, dd, dir, target int) bool {
210 switch me.Class {
211 case Hunter:
212 return dd <= Long*Long && o.Gold > 0 && g.clear(me, o)
213 case Brawler, Trickster:
214 return dd <= Melee*Melee && o.Gold > 0
215 case Coward:
216 return dd < 2000*2000 && me.Gold > 0 && dir != Still
217 case Reader:
218 return target >= 0 && g.Coin[target] != 0 &&
219 abs(target%Size*U+U/2-me.X) <= Magnet*U && abs(target/Size*U+U/2-me.Y) <= Magnet*U
220 }
221 return false
222}
223
224// Versus replays a run (side A's inputs) against a house bot on side B.
225func Versus(seed uint32, classA, level int, inA string) (int, int) {
226 if level < Easy || level > Hard {
227 panic("brawl: no such bot level")
228 }
229 if len(inA) > Ticks {
230 panic("brawl: inputs longer than a match")
231 }
232 g := New(seed, classA, BotClass[level][seed%3])
233 b := NewBot(level, seed)
234 for t := 0; t < Ticks; t++ {
235 m0, a0 := Still, None
236 if t < len(inA) {
237 c := inA[t]
238 if c >= 128 || decMove[c] < 0 {
239 panic("brawl: bad input character")
240 }
241 m0, a0 = decMove[c], decAct[c]
242 }
243 m1, a1 := b.Act(g, 1)
244 g.Step(m0, a0, m1, a1)
245 }
246 return g.F[0].Gold, g.F[1].Gold
247}