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.

bot.gno

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