rudimentary brakes

This commit is contained in:
Luke Meyers 2020-02-05 21:14:27 -08:00
parent ca3b673b41
commit bce4758dfd
2 changed files with 38 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package game
import ( import (
"log" "log"
"math/rand"
) )
type State struct { type State struct {
@ -70,13 +71,7 @@ func UpdateState(sOld State) State {
s := sOld s := sOld
for i, t := range s.Teams { for i, t := range s.Teams {
switch chooseCommand(t, sOld) { doCommand(chooseCommand(t, sOld), t.Baton.Holder)
case speedUp:
accelerate(t.Baton.Holder)
case left:
t.Baton.Holder.Lane++
}
moveBot(t.Baton.Holder, sOld) moveBot(t.Baton.Holder, sOld)
maybePassBaton(&s.Teams[i]) maybePassBaton(&s.Teams[i])
} }
@ -90,11 +85,45 @@ func UpdateState(sOld State) State {
return s return s
} }
func doCommand(cmd command, b *Bot) {
da := 1
da += rand.Intn(3) - 1
switch cmd {
case speedUp:
b.a += da
accelerate(b)
case slowDown:
b.a -= da
accelerate(b)
case left:
b.Lane++
}
}
func chooseCommand(t Team, s State) command { func chooseCommand(t Team, s State) command {
h := t.Baton.Holder h := t.Baton.Holder
if collide(h.Pos+1, h.Lane, s) { if collide(h.Pos+1, h.Lane, s) {
return left return left
} }
var nextBot *Bot
for i, b := range t.Bots {
if b.id == h.id+1 {
nextBot = &t.Bots[i]
break
}
}
if nextBot != nil {
if h.Lane != nextBot.Lane {
if abs(nextBot.Pos-h.Pos) < h.v {
log.Println("WHOOOOOOA")
return slowDown
}
}
}
return speedUp return speedUp
} }
@ -102,6 +131,7 @@ type command int
const ( const (
speedUp command = iota speedUp command = iota
slowDown
left left
) )
@ -140,7 +170,7 @@ const (
numBots = 5 numBots = 5
NumTeams = 2 NumTeams = 2
maxA = 3 maxA = 3
maxV = 2 maxV = 10
) )
func abs(n int) int { func abs(n int) int {

View File

@ -1,12 +1,6 @@
package game package game
import "math/rand"
func accelerate(b *Bot) { func accelerate(b *Bot) {
if b.a == 0 {
b.a = 1
}
b.a += rand.Intn(3) - 1
if b.a < -maxA { if b.a < -maxA {
b.a = -maxA b.a = -maxA
} }