|
|
@ -14,30 +14,6 @@ type BotHealth struct { |
|
|
|
Health int `json:"health"` |
|
|
|
} |
|
|
|
|
|
|
|
type Boardstate struct { |
|
|
|
MyRobots []Robot `json:"my_robots"` |
|
|
|
OtherRobots []OtherRobot `json:"robots"` |
|
|
|
Projectiles []Projectile `json:"projectiles"` |
|
|
|
Splosions []Splosion `json:"splosions"` |
|
|
|
Obstacles []Obstacle `json:"obj"` |
|
|
|
Reset bool `json:"reset"` |
|
|
|
Type string `json:"type"` |
|
|
|
Turn int `json:"turn"` |
|
|
|
AllBots []BotHealth `json:"all_bots"` |
|
|
|
Messages []string `json:"messages"` |
|
|
|
} |
|
|
|
|
|
|
|
func NewBoardstate() *Boardstate { |
|
|
|
return &Boardstate{ |
|
|
|
MyRobots: []Robot{}, |
|
|
|
OtherRobots: []OtherRobot{}, |
|
|
|
Projectiles: []Projectile{}, |
|
|
|
Splosions: []Splosion{}, |
|
|
|
AllBots: []BotHealth{}, |
|
|
|
Type: "boardstate", |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
type Scanner struct { |
|
|
|
Id string `json:"id"` |
|
|
|
Type string `json:"type"` |
|
|
@ -65,22 +41,23 @@ func (ml *MapLock) add(g *game) { |
|
|
|
} |
|
|
|
|
|
|
|
type game struct { |
|
|
|
id string |
|
|
|
players map[*player]bool |
|
|
|
projectiles map[*Projectile]bool |
|
|
|
splosions map[*Splosion]bool |
|
|
|
obstacles []Obstacle |
|
|
|
register chan *player |
|
|
|
unregister chan *player |
|
|
|
turn int |
|
|
|
width, height float32 |
|
|
|
spectators map[*Spectator]bool |
|
|
|
sregister chan *Spectator |
|
|
|
sunregister chan *Spectator |
|
|
|
kill chan bool |
|
|
|
repair_hp int |
|
|
|
repair_rate float32 |
|
|
|
tick_duration int |
|
|
|
id string |
|
|
|
players map[*player]bool |
|
|
|
projectiles map[*Projectile]bool |
|
|
|
splosions map[*Splosion]bool |
|
|
|
obstacles []Obstacle |
|
|
|
register chan *player |
|
|
|
unregister chan *player |
|
|
|
turn int |
|
|
|
players_remaining int |
|
|
|
width, height float32 |
|
|
|
spectators map[*Spectator]bool |
|
|
|
sregister chan *Spectator |
|
|
|
sunregister chan *Spectator |
|
|
|
kill chan bool |
|
|
|
repair_hp int |
|
|
|
repair_rate float32 |
|
|
|
tick_duration int |
|
|
|
} |
|
|
|
|
|
|
|
func NewGame(id string, width, height float32, tick int) *game { |
|
|
@ -106,10 +83,8 @@ func NewGame(id string, width, height float32, tick int) *game { |
|
|
|
return g |
|
|
|
} |
|
|
|
|
|
|
|
func (g *game) tick(payload *Boardstate) int { |
|
|
|
robots_remaining := 0 |
|
|
|
players_remaining := 0 |
|
|
|
|
|
|
|
func (g *game) tick(payload *Boardstate) { |
|
|
|
g.players_remaining = 0 |
|
|
|
payload.Obstacles = g.obstacles |
|
|
|
|
|
|
|
// Update Players
|
|
|
@ -139,8 +114,7 @@ func (g *game) tick(payload *Boardstate) int { |
|
|
|
} |
|
|
|
|
|
|
|
if living_robots > 0 { |
|
|
|
players_remaining++ |
|
|
|
robots_remaining += living_robots |
|
|
|
g.players_remaining++ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -162,8 +136,6 @@ func (g *game) tick(payload *Boardstate) int { |
|
|
|
} |
|
|
|
payload.Splosions = append(payload.Splosions, *s) |
|
|
|
} |
|
|
|
|
|
|
|
return players_remaining |
|
|
|
} |
|
|
|
|
|
|
|
func (g *game) sendUpdate(payload *Boardstate) { |
|
|
@ -180,7 +152,6 @@ func (g *game) sendUpdate(payload *Boardstate) { |
|
|
|
player_payload.Obstacles = payload.Obstacles |
|
|
|
player_payload.AllBots = payload.AllBots |
|
|
|
player_payload.Turn = payload.Turn |
|
|
|
player_payload.Reset = payload.Reset |
|
|
|
for _, r := range p.Robots { |
|
|
|
player_payload.MyRobots = append(player_payload.MyRobots, *r) |
|
|
|
player_payload.OtherRobots = append( |
|
|
@ -289,26 +260,12 @@ func (g *game) run() { |
|
|
|
} |
|
|
|
|
|
|
|
// UPDATE GAME STATE
|
|
|
|
players_remaining := g.tick(payload) |
|
|
|
|
|
|
|
// Determine end game?
|
|
|
|
if players_remaining <= 1 && len(g.players) > 1 { |
|
|
|
g.obstacles = GenerateObstacles(5, g.width, g.height) |
|
|
|
log.Printf("game %s: game over", g.id) |
|
|
|
|
|
|
|
for p := range g.players { |
|
|
|
for _, r := range p.Robots { |
|
|
|
if r.Health > 0 { |
|
|
|
log.Printf("Robot %v Survived", r.Id) |
|
|
|
} |
|
|
|
r.reset(g) |
|
|
|
} |
|
|
|
} |
|
|
|
payload.Reset = true |
|
|
|
} else { |
|
|
|
payload.Reset = false |
|
|
|
if end, data := g.gameOver(); end { |
|
|
|
g.sendGameOver(data) |
|
|
|
} |
|
|
|
|
|
|
|
g.tick(payload) |
|
|
|
|
|
|
|
t1 = time.Now() |
|
|
|
if *verbose { |
|
|
|
log.Printf("Turn Processes %v\n", t1.Sub(t0)) |
|
|
@ -324,3 +281,34 @@ func (g *game) run() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (g *game) gameOver() (bool, *GameOver) { |
|
|
|
over := false |
|
|
|
stats := NewGameOver() |
|
|
|
|
|
|
|
if g.players_remaining <= 1 && len(g.players) > 1 { |
|
|
|
g.obstacles = GenerateObstacles(conf.Obstacles, g.width, g.height) |
|
|
|
log.Printf("game %s: game over", g.id) |
|
|
|
|
|
|
|
for p := range g.players { |
|
|
|
for _, r := range p.Robots { |
|
|
|
if r.Health > 0 { |
|
|
|
log.Printf("Robot %v Survived", r.Id) |
|
|
|
} |
|
|
|
r.reset(g) |
|
|
|
} |
|
|
|
} |
|
|
|
over = true |
|
|
|
} |
|
|
|
return over, stats |
|
|
|
} |
|
|
|
|
|
|
|
func (g *game) sendGameOver(eg *GameOver) { |
|
|
|
log.Printf("sending out game over message: %+v", eg) |
|
|
|
for p := range g.players { |
|
|
|
p.send <- eg |
|
|
|
} |
|
|
|
for s := range g.spectators { |
|
|
|
s.send <- eg |
|
|
|
} |
|
|
|
} |