factor out renderBot
This commit is contained in:
parent
e31c585e3a
commit
1f8607a902
18
game/ai.go
18
game/ai.go
|
@ -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,7 +50,8 @@ 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 {
|
||||
|
@ -58,3 +59,8 @@ func score(s State, teamID int) int {
|
|||
}
|
||||
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 {
|
||||
// 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
|
||||
)
|
||||
|
|
40
gfx/gfx.go
40
gfx/gfx.go
|
@ -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),
|
||||
|
@ -56,18 +70,6 @@ func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, colors
|
|||
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) {
|
||||
im := imdraw.New(nil)
|
||||
im.Color = pixel.RGB(0, 0, 0)
|
||||
|
|
Loading…
Reference in New Issue