package brawl
import "strconv"
// Smart plans: a side's moves written as plain text, run tick by tick
// against the live match. Rooms use them so an agent can say "go get that
// coin" or "chase and hit when close" instead of guessing keystrokes.
//
// Steps are separated by ";" or new lines (at most MaxPlan characters, plain
// ASCII). Words are case-blind. Ticks are 1 to 300 (10 ticks = 1 s).
//
// run
move that way
// wait stand still
// attack | block | special | dodge
// press the button for one tick, still moving the
// way you were going
// goto , [ticks] walk to the middle of square (x,y), 0-14, sliding
// round walls; ends on arrival (next to it, for a
// table) or after ticks (60)
// collect head for the nearest gold, picking a new coin when
// that one is gone
// chase run at the rival
// flee run from the rival
// if near :
// one tick: press the button only if the rival is
// that close (centre to centre, like 1.4 or 3);
// otherwise just keep moving
// auto let the Normal house bot play for you
// guard on | guard off from here on, block on its own whenever the
// rival is within 1.5 squares and can swing (and you
// have gold and your block is ready), unless the
// step is already pressing a button. Takes no time.
//
// Directions: N, NE, E, SE, S, SW, W, NW (y grows toward S, so A's corner is
// NW and B's is SE), or a number 0-15 counting clockwise from E in steps of
// 22.5 degrees. Melee reaches 1.45 squares, the Hunter's beam 5.
//
// Everything is whole-number, so the chain and the browser run a plan the
// same. A tick costs a few comparisons; collect looks for gold ring by ring
// only when its coin is gone, and at most every 5 ticks while there is none.
const (
MaxPlan = 500
PlanTicks = 300 // longest step
GotoTicks = 60 // goto's default limit
// GuardReach is how close the rival must be for guard to block: a little
// past melee (1.45), so the block is up before the swing lands.
GuardReach = 1500
)
const (
stepRun = iota
stepWait
stepButton
stepGoto
stepCollect
stepChase
stepFlee
stepIfNear
stepAuto
stepGuard
)
type planStep struct {
kind int
n int // ticks
dir int // run
act int // button, if near
x, y int // goto
near int // if near, in board units
}
// Plan is a parsed plain-text plan for one side, run tick by tick.
type Plan struct {
steps []planStep
i int // current step
on bool // the current step has started
left int // its ticks left
dir int // the last move, kept by buttons
target int // collect: the coin it heads for, -1 for none
look int // collect: no looking again before this tick
stuck int
lastX int
lastY int
turn int // which way it slides round walls
hold int // ticks left of a slide
slide int
bot *Bot // auto
guard bool // guard on
}
var planDirs = map[string]int{"E": 0, "SE": 2, "S": 4, "SW": 6, "W": 8, "NW": 10, "N": 12, "NE": 14}
var planButtons = map[string]int{"attack": Attack, "block": Block, "special": Special, "dodge": Dodge}
// ParsePlan reads a plan; on a bad step it returns nil and a short error naming the step.
func ParsePlan(text string) (*Plan, string) {
if len(text) > MaxPlan {
return nil, "plan: at most " + strconv.Itoa(MaxPlan) + " characters"
}
for i := 0; i < len(text); i++ {
if text[i] >= 128 {
return nil, "plan: plain ASCII only"
}
}
p := &Plan{dir: Still, target: -1}
start, num := 0, 0
for i := 0; i <= len(text); i++ {
if i < len(text) && text[i] != ';' && text[i] != '\n' {
continue
}
step := text[start:i]
start = i + 1
f := planFields(step)
if len(f) == 0 {
continue
}
num++
st, err := parseStep(f)
if err != "" {
return nil, "step " + strconv.Itoa(num) + " \"" + planTrim(step) + "\": " + err
}
p.steps = append(p.steps, st)
}
return p, ""
}
func planSpace(c byte) bool {
return c == ' ' || c == '\t' || c == '\r' || c == ',' || c == '(' || c == ')'
}
// planFields splits a step into words on spaces, commas and brackets; a
// colon is a word of its own.
func planFields(s string) []string {
var f []string
w := -1
for i := 0; i <= len(s); i++ {
if i < len(s) && !planSpace(s[i]) && s[i] != ':' {
if w < 0 {
w = i
}
continue
}
if w >= 0 {
f = append(f, s[w:i])
w = -1
}
if i < len(s) && s[i] == ':' {
f = append(f, ":")
}
}
return f
}
func planTrim(s string) string {
a, b := 0, len(s)
for a < b && (s[a] == ' ' || s[a] == '\t' || s[a] == '\r') {
a++
}
for b > a && (s[b-1] == ' ' || s[b-1] == '\t' || s[b-1] == '\r') {
b--
}
return s[a:b]
}
// planNum reads 1 to 3 digits; -1 if it isn't one.
func planNum(s string) int {
if len(s) == 0 || len(s) > 3 {
return -1
}
v := 0
for i := 0; i < len(s); i++ {
if s[i] < '0' || s[i] > '9' {
return -1
}
v = v*10 + int(s[i]-'0')
}
return v
}
func planLower(s string) string {
b := []byte(s)
for i := range b {
if b[i] >= 'A' && b[i] <= 'Z' {
b[i] += 'a' - 'A'
}
}
return string(b)
}
func planUpper(s string) string {
b := []byte(s)
for i := range b {
if b[i] >= 'a' && b[i] <= 'z' {
b[i] -= 'a' - 'A'
}
}
return string(b)
}
func planTicks(s string) (int, string) {
n := planNum(s)
if n < 1 || n > PlanTicks {
return 0, "ticks must be 1 to " + strconv.Itoa(PlanTicks)
}
return n, ""
}
// planNear reads a distance in squares with at most one decimal, like 1.4.
func planNear(s string) int {
dot := -1
for i := 0; i < len(s); i++ {
if s[i] == '.' {
dot = i
}
}
whole, tenth := s, ""
if dot >= 0 {
whole, tenth = s[:dot], s[dot+1:]
if len(tenth) != 1 && len(tenth) != 2 {
return -1
}
}
w := planNum(whole)
if w < 0 || len(whole) > 2 {
return -1
}
d := w * U
if tenth != "" {
v := planNum(tenth)
if v < 0 {
return -1
}
if len(tenth) == 1 {
v *= 10
}
d += v * 10
}
if d < 100 || d > Size*U {
return -1
}
return d
}
func parseStep(f []string) (planStep, string) {
word := planLower(f[0])
var st planStep
var err string
if act, ok := planButtons[word]; ok {
if len(f) != 1 {
return st, "a button takes no number"
}
return planStep{kind: stepButton, n: 1, act: act}, ""
}
switch word {
case "run":
if len(f) != 3 {
return st, "use run "
}
d, ok := planDirs[planUpper(f[1])]
if !ok {
d = planNum(f[1])
if d < 0 || d > 15 {
return st, "direction is N, NE, E, SE, S, SW, W, NW or 0-15"
}
}
st = planStep{kind: stepRun, dir: d}
st.n, err = planTicks(f[2])
case "wait", "collect", "chase", "flee", "auto":
if len(f) != 2 {
return st, "use " + word + " "
}
st.kind = map[string]int{"wait": stepWait, "collect": stepCollect, "chase": stepChase, "flee": stepFlee, "auto": stepAuto}[word]
st.n, err = planTicks(f[1])
case "goto":
if len(f) != 3 && len(f) != 4 {
return st, "use goto , [ticks]"
}
x, y := planNum(f[1]), planNum(f[2])
if x < 0 || x >= Size || y < 0 || y >= Size {
return st, "squares are 0 to 14"
}
st = planStep{kind: stepGoto, x: x, y: y, n: GotoTicks}
if len(f) == 4 {
st.n, err = planTicks(f[3])
}
case "guard":
if len(f) != 2 || (planLower(f[1]) != "on" && planLower(f[1]) != "off") {
return st, "use guard on or guard off"
}
st = planStep{kind: stepGuard}
if planLower(f[1]) == "on" {
st.n = 1
}
case "if":
// if near [:]
if len(f) == 5 && f[3] == ":" {
f = []string{f[0], f[1], f[2], f[4]}
}
if len(f) != 4 || planLower(f[1]) != "near" {
return st, "use if near : "
}
d := planNear(f[2])
if d < 0 {
return st, "distance is 0.1 to 15 squares"
}
act, ok := planButtons[planLower(f[3])]
if !ok {
return st, "button is attack, block, special or dodge"
}
st = planStep{kind: stepIfNear, n: 1, near: d, act: act}
default:
return st, "unknown word \"" + f[0] + "\""
}
return st, err
}
// Done reports whether the plan has run out.
func (p *Plan) Done() bool { return p.i >= len(p.steps) }
// Next returns this tick's move and action for side s, reacting to the live game.
// Once the plan runs out it returns Still, None. Call it once per tick.
func (p *Plan) Next(g *Game, s int) (move, act int) {
me, o := &g.F[s], &g.F[1-s]
move, act = Still, None
for p.i < len(p.steps) {
st := &p.steps[p.i]
if !p.on {
p.on, p.left, p.target, p.look, p.hold, p.stuck = true, st.n, -1, 0, 0, 0
}
if st.kind == stepGuard {
p.guard = st.n == 1
p.i++
p.on = false
continue
}
if st.kind == stepGoto && p.arrived(g, me, st) {
p.i++
p.on = false
continue
}
switch st.kind {
case stepRun:
p.dir = st.dir
case stepWait:
p.dir = Still
case stepButton:
act = st.act
case stepIfNear:
if dx, dy := o.X-me.X, o.Y-me.Y; dx*dx+dy*dy <= st.near*st.near {
act = st.act
}
case stepGoto:
p.steer(st.x*U+U/2-me.X, st.y*U+U/2-me.Y)
case stepChase:
p.steer(o.X-me.X, o.Y-me.Y)
case stepFlee:
p.steer(me.X-o.X, me.Y-o.Y)
case stepCollect:
if (p.target < 0 || g.Coin[p.target] == 0) && g.T >= p.look {
p.target = g.ringCoin(me, Size-1)
if p.target < 0 {
p.look = g.T + 5
}
}
if p.target >= 0 && g.Coin[p.target] != 0 {
p.steer(p.target%Size*U+U/2-me.X, p.target/Size*U+U/2-me.Y)
} else {
p.dir = Still
}
case stepAuto:
if p.bot == nil {
p.bot = NewBot(Normal, g.Seed*31+uint32(s)+3)
}
p.dir, act = p.bot.Act(g, s)
}
move = p.dir
p.left--
if p.left <= 0 {
p.i++
p.on = false
}
break
}
if p.guard && act == None && me.Gold > 0 && g.T >= o.AtkCD && g.T >= me.BlockCD {
if dx, dy := o.X-me.X, o.Y-me.Y; dx*dx+dy*dy <= GuardReach*GuardReach {
act = Block
}
}
if move != Still && abs(me.X-p.lastX)+abs(me.Y-p.lastY) < 60 {
p.stuck++
} else {
p.stuck = 0
}
p.lastX, p.lastY = me.X, me.Y
return move, act
}
// arrived reports whether a goto is over: the fighter is on the square, or
// next to it if the square is a wall.
func (p *Plan) arrived(g *Game, me *Fighter, st *planStep) bool {
x, y := me.X/U, me.Y/U
if x == st.x && y == st.y {
return true
}
return g.Floor[st.y*Size+st.x] == Wall && abs(x-st.x) <= 1 && abs(y-st.y) <= 1
}
// steer heads along (dx, dy), sliding round a wall like the house bot: once
// it has been stuck for 3 ticks it turns a quarter for 4 ticks, and if that
// way is blocked too it tries the other side.
func (p *Plan) steer(dx, dy int) {
if p.hold > 0 {
if p.stuck <= 2 {
p.hold--
p.dir = p.slide
return
}
p.turn = 1 - p.turn
p.hold = 0
}
d := toward(dx, dy)
if p.stuck > 2 && d != Still {
if p.turn == 0 {
d = (d + 4) % 16
} else {
d = (d + 12) % 16
}
p.slide, p.hold, p.stuck = d, 3, 0
}
p.dir = d
}