destroy and respawn bots on collision
This commit is contained in:
parent
230310a330
commit
cfa4d64492
|
@ -2,7 +2,7 @@ package game
|
||||||
|
|
||||||
func chooseCommand(s State, teamID int) command {
|
func chooseCommand(s State, teamID int) command {
|
||||||
t := s.Teams[teamID]
|
t := s.Teams[teamID]
|
||||||
h := t.BatonHolder()
|
h := ActiveBot(t)
|
||||||
if collide(h.Position.Pos+1, h.Position.Lane, s) != nil {
|
if collide(h.Position.Pos+1, h.Position.Lane, s) != nil {
|
||||||
if h.Position.Lane <= t.Lane && h.Position.Lane < NumLanes-1 {
|
if h.Position.Lane <= t.Lane && h.Position.Lane < NumLanes-1 {
|
||||||
return left
|
return left
|
||||||
|
|
|
@ -31,6 +31,6 @@ func doCommand(cmd command, s State, teamID int) State {
|
||||||
b.Position.Lane--
|
b.Position.Lane--
|
||||||
}
|
}
|
||||||
|
|
||||||
s = updateBot(s, teamID, *b)
|
s = updateBot(s, *b)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
48
game/game.go
48
game/game.go
|
@ -6,7 +6,7 @@ func UpdateState(s State, sOld State) State {
|
||||||
for i := range s.Teams {
|
for i := range s.Teams {
|
||||||
s = doCommand(chooseCommand(s, i), s, i)
|
s = doCommand(chooseCommand(s, i), s, i)
|
||||||
if b := ActiveBot(s.Teams[i]); b != nil {
|
if b := ActiveBot(s.Teams[i]); b != nil {
|
||||||
s = moveBot(s, i, *b)
|
s = moveBot(s, *b)
|
||||||
}
|
}
|
||||||
s = maybePassBaton(s, i)
|
s = maybePassBaton(s, i)
|
||||||
}
|
}
|
||||||
|
@ -35,12 +35,12 @@ func maybePassBaton(s State, teamID int) State {
|
||||||
if abs(b.Position.Pos-h.Position.Pos) <= passDistance {
|
if abs(b.Position.Pos-h.Position.Pos) <= passDistance {
|
||||||
h.v = 0
|
h.v = 0
|
||||||
h.a = 0
|
h.a = 0
|
||||||
s = updateBot(s, teamID, *h)
|
s = updateBot(s, *h)
|
||||||
newH := t.Bots[i]
|
newH := t.Bots[i]
|
||||||
newH.a = baseAccel
|
newH.a = baseAccel
|
||||||
t.Baton.HolderID = newH.ID
|
t.Baton.HolderID = newH.ID
|
||||||
s = updateTeam(s, t)
|
s = updateTeam(s, t)
|
||||||
return updateBot(s, teamID, newH)
|
return updateBot(s, newH)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,8 +56,8 @@ func ActiveBot(t Team) *Bot {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateBot(s State, teamID int, b Bot) State {
|
func updateBot(s State, b Bot) State {
|
||||||
t := s.Teams[teamID]
|
t := s.Teams[b.TeamID]
|
||||||
for i, bb := range t.Bots {
|
for i, bb := range t.Bots {
|
||||||
if bb.ID == b.ID {
|
if bb.ID == b.ID {
|
||||||
bots := append([]Bot{}, t.Bots[:i]...)
|
bots := append([]Bot{}, t.Bots[:i]...)
|
||||||
|
@ -81,6 +81,18 @@ func updateTeam(s State, t Team) State {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
func won(b Bot, s State) bool {
|
func won(b Bot, s State) bool {
|
||||||
return b.Position.Pos >= Steps
|
return b.Position.Pos >= Steps
|
||||||
}
|
}
|
||||||
|
@ -115,18 +127,11 @@ type Team struct {
|
||||||
Lane int
|
Lane int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Team) BatonHolder() *Bot {
|
|
||||||
for _, b := range t.Bots {
|
|
||||||
if b.ID == t.Baton.HolderID {
|
|
||||||
return &b
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Bot struct {
|
type Bot struct {
|
||||||
ID int
|
ID int
|
||||||
|
TeamID int
|
||||||
Position Position
|
Position Position
|
||||||
|
StartPos Position
|
||||||
v int
|
v int
|
||||||
a int
|
a int
|
||||||
}
|
}
|
||||||
|
@ -136,6 +141,11 @@ type Position struct {
|
||||||
Pos int
|
Pos int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Battery struct {
|
||||||
|
Capacity int
|
||||||
|
Charge int
|
||||||
|
}
|
||||||
|
|
||||||
type Baton struct {
|
type Baton struct {
|
||||||
HolderID int
|
HolderID int
|
||||||
}
|
}
|
||||||
|
@ -150,12 +160,14 @@ func NewState() State {
|
||||||
var bots []Bot
|
var bots []Bot
|
||||||
for j := 0; j < numBots; j++ {
|
for j := 0; j < numBots; j++ {
|
||||||
b := Bot{
|
b := Bot{
|
||||||
ID: i*NumTeams + j,
|
ID: i*NumTeams + j,
|
||||||
Position: Position{
|
TeamID: i,
|
||||||
|
StartPos: Position{
|
||||||
Lane: i,
|
Lane: i,
|
||||||
Pos: j * (Steps / numBots),
|
Pos: j * (Steps / numBots),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
b.Position = b.StartPos
|
||||||
bots = append(bots, b)
|
bots = append(bots, b)
|
||||||
}
|
}
|
||||||
teams = append(teams, Team{
|
teams = append(teams, Team{
|
||||||
|
@ -199,7 +211,7 @@ func NewState() State {
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Steps = 40
|
Steps = 40
|
||||||
numBots = 5
|
numBots = 4
|
||||||
NumTeams = 4
|
NumTeams = 4
|
||||||
NumLanes = 4
|
NumLanes = NumTeams
|
||||||
)
|
)
|
||||||
|
|
|
@ -21,17 +21,17 @@ func accelerate(b Bot) Bot {
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func moveBot(s State, teamID int, b Bot) State {
|
func moveBot(s State, b Bot) State {
|
||||||
for i := 0; i < b.v; i++ {
|
for i := 0; i < b.v; i++ {
|
||||||
if o := collide(b.Position.Pos+1, b.Position.Lane, s); o != nil {
|
if o := collide(b.Position.Pos+1, b.Position.Lane, s); o != nil {
|
||||||
log.Printf("bot %d crashed into %#v!", b.ID, o)
|
log.Printf("bot %d crashed into %#v!", b.ID, o)
|
||||||
break
|
return destroyBot(s, b)
|
||||||
} else {
|
} else {
|
||||||
b.Position.Pos++
|
b.Position.Pos++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
s = updateBot(s, teamID, b)
|
s = updateBot(s, b)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue