factor out renderBot
This commit is contained in:
parent
e31c585e3a
commit
1f8607a902
28
game/ai.go
28
game/ai.go
|
@ -1,7 +1,5 @@
|
||||||
package game
|
package game
|
||||||
|
|
||||||
import "log"
|
|
||||||
|
|
||||||
func chooseCommand(s State, teamID int) command {
|
func chooseCommand(s State, teamID int) command {
|
||||||
t := s.Teams[teamID]
|
t := s.Teams[teamID]
|
||||||
h := ActiveBot(t)
|
h := ActiveBot(t)
|
||||||
|
@ -32,16 +30,18 @@ func chooseCommand(s State, teamID int) command {
|
||||||
}
|
}
|
||||||
|
|
||||||
func smartChooseCommand(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
|
bestCmd, bestN := speedUp, 0
|
||||||
|
|
||||||
log.Printf("team %d base score: %d", teamID, score(s, teamID))
|
|
||||||
for _, cmd := range validCommands {
|
for _, cmd := range validCommands {
|
||||||
if !legalMove(s, teamID, cmd) {
|
if !legalMove(s, teamID, cmd) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ss := doCommand(cmd, s, teamID)
|
ss := doCommand(cmd, s, teamID)
|
||||||
n := score(ss, teamID)
|
n := score(ss, teamID, depth)
|
||||||
log.Printf("team %d score %s: %d", teamID, cmd, n)
|
|
||||||
if n > bestN {
|
if n > bestN {
|
||||||
bestCmd, bestN = cmd, n
|
bestCmd, bestN = cmd, n
|
||||||
}
|
}
|
||||||
|
@ -50,11 +50,17 @@ func smartChooseCommand(s State, teamID int) command {
|
||||||
return bestCmd
|
return bestCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func score(s State, teamID int) int {
|
func score(s State, teamID int, depth int) int {
|
||||||
t := s.Teams[teamID]
|
if depth == 0 {
|
||||||
b := ActiveBot(t)
|
t := s.Teams[teamID]
|
||||||
if b == nil {
|
b := ActiveBot(t)
|
||||||
return 0
|
if b == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return b.Position.Pos
|
||||||
}
|
}
|
||||||
return b.Position.Pos
|
|
||||||
|
cmd := smartChooseHelper(s, teamID, depth-1)
|
||||||
|
ss := doCommand(cmd, s, teamID)
|
||||||
|
return score(ss, teamID, depth-1)
|
||||||
}
|
}
|
||||||
|
|
14
game/game.go
14
game/game.go
|
@ -85,11 +85,9 @@ func updateTeam(s State, t Team) State {
|
||||||
func destroyBot(s State, b Bot) State {
|
func destroyBot(s State, b Bot) State {
|
||||||
// insert obstacle where bot was
|
// insert obstacle where bot was
|
||||||
s.Obstacles = append(s.Obstacles, Obstacle{Position: b.Position})
|
s.Obstacles = append(s.Obstacles, Obstacle{Position: b.Position})
|
||||||
log.Printf("spawn obstacle at %v", b.Position)
|
|
||||||
|
|
||||||
// spawn bot back at starting position
|
// spawn bot back at starting position
|
||||||
b.Position = b.StartPos
|
b.Position = b.StartPos
|
||||||
log.Printf("respawn bot %d at %v", b.ID, b.Position)
|
|
||||||
|
|
||||||
return updateBot(s, b)
|
return updateBot(s, b)
|
||||||
}
|
}
|
||||||
|
@ -97,7 +95,11 @@ func destroyBot(s State, b Bot) State {
|
||||||
func removeObstacle(s State, pos Position) State {
|
func removeObstacle(s State, pos Position) State {
|
||||||
for i, o := range s.Obstacles {
|
for i, o := range s.Obstacles {
|
||||||
if o.Position == pos {
|
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
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -257,8 +259,8 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Steps = 40
|
Steps = 50
|
||||||
numBots = 4
|
numBots = 5
|
||||||
NumTeams = 2
|
NumTeams = 6
|
||||||
NumLanes = NumTeams
|
NumLanes = NumTeams
|
||||||
)
|
)
|
||||||
|
|
42
gfx/gfx.go
42
gfx/gfx.go
|
@ -33,32 +33,16 @@ 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) {
|
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 i, t := range sNew.Teams {
|
||||||
|
c := colors[&sNew.Teams[i]]
|
||||||
for j, bot := range t.Bots {
|
for j, bot := range t.Bots {
|
||||||
c := colors[&sNew.Teams[i]]
|
|
||||||
im.Color = c
|
|
||||||
|
|
||||||
oldBot := sOld.Teams[i].Bots[j]
|
oldBot := sOld.Teams[i].Bots[j]
|
||||||
oldPos := lanePos(oldBot.Position.Pos, oldBot.Position.Lane, botWidth, bounds)
|
renderBot(oldBot, bot, sOld, sNew, w, c, tween)
|
||||||
newPos := lanePos(bot.Position.Pos, bot.Position.Lane, botWidth, bounds)
|
|
||||||
|
|
||||||
pos := pixel.Vec{
|
|
||||||
X: oldPos.X + tween*(newPos.X-oldPos.X),
|
|
||||||
Y: oldPos.Y + tween*(newPos.Y-oldPos.Y),
|
|
||||||
}
|
|
||||||
|
|
||||||
im.Push(pos)
|
|
||||||
im.Clear()
|
|
||||||
im.Circle(botWidth, 0)
|
|
||||||
im.Draw(w)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
oldHolder, newHolder := game.ActiveBot(sOld.Teams[i]), game.ActiveBot(sNew.Teams[i])
|
oldHolder, newHolder := game.ActiveBot(sOld.Teams[i]), game.ActiveBot(sNew.Teams[i])
|
||||||
oldPos := lanePos(oldHolder.Position.Pos, oldHolder.Position.Lane, botWidth, bounds)
|
oldPos := lanePos(oldHolder.Position.Pos, oldHolder.Position.Lane, botWidth, w.Bounds())
|
||||||
newPos := lanePos(newHolder.Position.Pos, newHolder.Position.Lane, botWidth, bounds)
|
newPos := lanePos(newHolder.Position.Pos, newHolder.Position.Lane, botWidth, w.Bounds())
|
||||||
|
|
||||||
pos := pixel.Vec{
|
pos := pixel.Vec{
|
||||||
X: oldPos.X + tween*(newPos.X-oldPos.X),
|
X: oldPos.X + tween*(newPos.X-oldPos.X),
|
||||||
|
@ -68,6 +52,24 @@ func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, colors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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),
|
||||||
|
Y: oldPos.Y + tween*(newPos.Y-oldPos.Y),
|
||||||
|
}
|
||||||
|
|
||||||
|
im.Push(pos)
|
||||||
|
im.Clear()
|
||||||
|
im.Circle(botWidth, 0)
|
||||||
|
im.Draw(w)
|
||||||
|
}
|
||||||
|
|
||||||
func renderBaton(pos pixel.Vec, w *pixelgl.Window) {
|
func renderBaton(pos pixel.Vec, w *pixelgl.Window) {
|
||||||
im := imdraw.New(nil)
|
im := imdraw.New(nil)
|
||||||
im.Color = pixel.RGB(0, 0, 0)
|
im.Color = pixel.RGB(0, 0, 0)
|
||||||
|
|
Loading…
Reference in New Issue