factor out renderBot

This commit is contained in:
Luke Meyers 2020-02-07 23:06:15 -08:00
parent e31c585e3a
commit 1f8607a902
3 changed files with 47 additions and 37 deletions

View File

@ -1,7 +1,5 @@
package game
import "log"
func chooseCommand(s State, teamID int) command {
t := s.Teams[teamID]
h := ActiveBot(t)
@ -32,16 +30,18 @@ func chooseCommand(s State, teamID int) command {
}
func smartChooseCommand(s State, teamID int) command {
return smartChooseHelper(s, teamID, 2)
}
func smartChooseHelper(s State, teamID int, depth int) command {
bestCmd, bestN := speedUp, 0
log.Printf("team %d base score: %d", teamID, score(s, teamID))
for _, cmd := range validCommands {
if !legalMove(s, teamID, cmd) {
continue
}
ss := doCommand(cmd, s, teamID)
n := score(ss, teamID)
log.Printf("team %d score %s: %d", teamID, cmd, n)
n := score(ss, teamID, depth)
if n > bestN {
bestCmd, bestN = cmd, n
}
@ -50,11 +50,17 @@ func smartChooseCommand(s State, teamID int) command {
return bestCmd
}
func score(s State, teamID int) int {
func score(s State, teamID int, depth int) int {
if depth == 0 {
t := s.Teams[teamID]
b := ActiveBot(t)
if b == nil {
return 0
}
return b.Position.Pos
}
cmd := smartChooseHelper(s, teamID, depth-1)
ss := doCommand(cmd, s, teamID)
return score(ss, teamID, depth-1)
}

View File

@ -85,11 +85,9 @@ func updateTeam(s State, t Team) State {
func destroyBot(s State, b Bot) State {
// insert obstacle where bot was
s.Obstacles = append(s.Obstacles, Obstacle{Position: b.Position})
log.Printf("spawn obstacle at %v", b.Position)
// spawn bot back at starting position
b.Position = b.StartPos
log.Printf("respawn bot %d at %v", b.ID, b.Position)
return updateBot(s, b)
}
@ -97,7 +95,11 @@ func destroyBot(s State, b Bot) State {
func removeObstacle(s State, pos Position) State {
for i, o := range s.Obstacles {
if o.Position == pos {
s.Obstacles = append(s.Obstacles[:i], s.Obstacles[i+1:]...)
var os []Obstacle
os = append(os, s.Obstacles[:i]...)
os = append(os, s.Obstacles[i+1:]...)
s.Obstacles = os
//s.Obstacles = append([]Obstacle{}, append(s.Obstacles[:i], s.Obstacles[i+1:]...)...)
break
}
}
@ -257,8 +259,8 @@ var (
)
const (
Steps = 40
numBots = 4
NumTeams = 2
Steps = 50
numBots = 5
NumTeams = 6
NumLanes = NumTeams
)

View File

@ -33,17 +33,31 @@ func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window) RenderStat
}
func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, colors map[*game.Team]pixel.RGBA) {
bounds := w.Bounds()
im := imdraw.New(nil)
for i, t := range sNew.Teams {
for j, bot := range t.Bots {
c := colors[&sNew.Teams[i]]
for j, bot := range t.Bots {
oldBot := sOld.Teams[i].Bots[j]
renderBot(oldBot, bot, sOld, sNew, w, c, tween)
}
oldHolder, newHolder := game.ActiveBot(sOld.Teams[i]), game.ActiveBot(sNew.Teams[i])
oldPos := lanePos(oldHolder.Position.Pos, oldHolder.Position.Lane, botWidth, w.Bounds())
newPos := lanePos(newHolder.Position.Pos, newHolder.Position.Lane, botWidth, w.Bounds())
pos := pixel.Vec{
X: oldPos.X + tween*(newPos.X-oldPos.X),
Y: oldPos.Y + tween*(newPos.Y-oldPos.Y),
}
renderBaton(pos, w)
}
}
func renderBot(oldBot, bot game.Bot, sOld, sNew game.State, w *pixelgl.Window, c pixel.RGBA, tween float64) {
im := imdraw.New(nil)
im.Color = c
oldBot := sOld.Teams[i].Bots[j]
oldPos := lanePos(oldBot.Position.Pos, oldBot.Position.Lane, botWidth, bounds)
newPos := lanePos(bot.Position.Pos, bot.Position.Lane, botWidth, bounds)
oldPos := lanePos(oldBot.Position.Pos, oldBot.Position.Lane, botWidth, w.Bounds())
newPos := lanePos(bot.Position.Pos, bot.Position.Lane, botWidth, w.Bounds())
pos := pixel.Vec{
X: oldPos.X + tween*(newPos.X-oldPos.X),
@ -54,18 +68,6 @@ func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, colors
im.Clear()
im.Circle(botWidth, 0)
im.Draw(w)
}
oldHolder, newHolder := game.ActiveBot(sOld.Teams[i]), game.ActiveBot(sNew.Teams[i])
oldPos := lanePos(oldHolder.Position.Pos, oldHolder.Position.Lane, botWidth, bounds)
newPos := lanePos(newHolder.Position.Pos, newHolder.Position.Lane, botWidth, bounds)
pos := pixel.Vec{
X: oldPos.X + tween*(newPos.X-oldPos.X),
Y: oldPos.Y + tween*(newPos.Y-oldPos.Y),
}
renderBaton(pos, w)
}
}
func renderBaton(pos pixel.Vec, w *pixelgl.Window) {