server/game.go

305 lines
6.9 KiB
Go
Raw Normal View History

2013-08-19 20:43:26 -07:00
package main
import (
// "encoding/json"
"errors"
2013-08-19 20:43:26 -07:00
"log"
"sort"
"sync"
2013-08-19 20:43:26 -07:00
"time"
)
const maxPlayer = 128
type BotHealth struct {
RobotId string `json:"robot_id"`
Health int `json:"health"`
}
2013-09-27 22:27:05 -07:00
type Boardstate struct {
MyRobots []Robot `json:"my_robots"`
OtherRobots []OtherRobot `json:"robots"`
2013-09-27 22:27:05 -07:00
Projectiles []Projectile `json:"projectiles"`
Splosions []Splosion `json:"splosions"`
2013-10-25 22:30:15 -07:00
Obstacles []Obstacle `json:"obj"`
2013-09-27 22:27:05 -07:00
Reset bool `json:"reset"`
Type string `json:"type"`
Turn int `json:"turn"`
AllBots []BotHealth `json:"all_bots"`
2013-09-27 22:27:05 -07:00
}
func NewBoardstate() *Boardstate {
return &Boardstate{
MyRobots: []Robot{},
OtherRobots: []OtherRobot{},
2013-09-27 22:27:05 -07:00
Projectiles: []Projectile{},
Splosions: []Splosion{},
AllBots: []BotHealth{},
2013-09-27 22:27:05 -07:00
Type: "boardstate",
}
}
type Scanner struct {
2013-11-06 20:09:45 -08:00
Id string `json:"id"`
Type string `json:"type"`
2013-09-27 22:27:05 -07:00
}
type MapLock struct {
m map[string]*game
sync.RWMutex
}
// get is a function that returns a game if found, and creates one if
// not found and force is true. In order to get a hash (rather than use
// the string you pass) send "" for id.
func (ml *MapLock) get(id string, force bool) (*game, error) {
ml.Lock()
g, ok := games.m[id]
ml.Unlock()
if ok {
return g, nil
}
if !force {
return nil, errors.New("game not found")
}
if id == "" {
id = idg.Hash()
}
_g := NewGame(id, float32(*width), float32(*height))
go _g.run()
ml.Lock()
ml.m[id] = _g
ml.Unlock()
return _g, nil
}
2013-08-19 20:43:26 -07:00
type game struct {
2013-09-01 23:00:09 -07:00
id string
2013-09-01 22:04:34 -07:00
players map[*player]bool
2013-09-27 22:27:05 -07:00
projectiles map[*Projectile]bool
splosions map[*Splosion]bool
2013-10-25 22:30:15 -07:00
obstacles []Obstacle
2013-09-01 22:04:34 -07:00
register chan *player
unregister chan *player
turn int
width, height float32
2013-09-01 22:04:34 -07:00
spectators map[*Spectator]bool
sregister chan *Spectator
sunregister chan *Spectator
kill chan bool
2013-08-19 20:43:26 -07:00
}
func NewGame(id string, width, height float32) *game {
2013-09-04 00:06:39 -07:00
g := &game{
2013-09-01 23:00:09 -07:00
id: id,
2013-08-25 23:10:02 -07:00
register: make(chan *player),
unregister: make(chan *player, maxPlayer),
2013-09-27 22:27:05 -07:00
projectiles: make(map[*Projectile]bool),
splosions: make(map[*Splosion]bool),
2013-10-25 22:30:15 -07:00
obstacles: GenerateObstacles(5, width, height),
2013-08-25 23:10:02 -07:00
players: make(map[*player]bool),
turn: 0,
2013-09-01 22:04:34 -07:00
width: width,
height: height,
2013-08-25 23:10:02 -07:00
spectators: make(map[*Spectator]bool),
sregister: make(chan *Spectator),
sunregister: make(chan *Spectator),
kill: make(chan bool, maxPlayer),
2013-08-25 23:10:02 -07:00
}
2013-09-04 00:06:39 -07:00
return g
}
func (g *game) tick(payload *Boardstate) int {
robots_remaining := 0
2013-10-25 22:30:15 -07:00
payload.Obstacles = g.obstacles
// Update Players
for p := range g.players {
if p.Robot.Health > 0 {
robots_remaining++
p.Tick(g)
}
payload.OtherRobots = append(
payload.OtherRobots,
p.Robot.GetTruncatedDetails())
payload.AllBots = append(
payload.AllBots,
BotHealth{RobotId: p.Robot.Id, Health: p.Robot.Health})
}
// Update Projectiles
for pr := range g.projectiles {
pr.Tick(g)
}
// We do this here, because the tick calls can alter g.projectiles
for pr := range g.projectiles {
payload.Projectiles = append(payload.Projectiles, *pr)
}
// Update Splosions
for s := range g.splosions {
s.Tick()
if !s.Alive() {
delete(g.splosions, s)
}
payload.Splosions = append(payload.Splosions, *s)
}
return robots_remaining
}
func (g *game) send_update(payload *Boardstate) {
// Ensure that the robots are always sent in a consistent order
sort.Sort(RobotSorter{Robots: payload.OtherRobots})
2013-11-06 21:12:19 -08:00
sort.Sort(AllRobotSorter{Robots: payload.AllBots})
for p := range g.players {
// Copy the payload but only add the robots in scanner range
player_payload := NewBoardstate()
player_payload.Splosions = payload.Splosions
2013-10-25 22:30:15 -07:00
player_payload.Obstacles = payload.Obstacles
player_payload.AllBots = payload.AllBots
player_payload.Turn = payload.Turn
player_payload.Reset = payload.Reset
player_payload.MyRobots = append(player_payload.MyRobots, p.Robot)
player_payload.OtherRobots = append(
player_payload.OtherRobots,
p.Robot.GetTruncatedDetails())
player_payload.Projectiles = []Projectile{}
player_payload.Obstacles = []Obstacle{}
if p.Robot.Health > 0 {
// Filter robots by scanner
for player := range g.players {
for _, scan_entry := range p.Robot.Scanners {
if player.Robot.Id == scan_entry.Id {
player_payload.OtherRobots = append(
player_payload.OtherRobots,
player.Robot.GetTruncatedDetails())
}
}
}
// Filter projectiles
for proj := range g.projectiles {
if proj.Owner == p {
player_payload.Projectiles = append(
player_payload.Projectiles,
*proj)
}
for _, scan_entry := range p.Robot.Scanners {
if proj.Id == scan_entry.Id {
player_payload.Projectiles = append(
player_payload.Projectiles,
*proj)
}
}
}
// Filter objects
for _, ob := range g.obstacles {
if ob.distance_from_point(p.Robot.Position) < float32(p.Robot.Stats.ScannerRadius) {
player_payload.Obstacles = append(
player_payload.Obstacles,
ob)
}
}
} else {
player_payload.OtherRobots = payload.OtherRobots
player_payload.Projectiles = payload.Projectiles
player_payload.Obstacles = payload.Obstacles
}
// x, _ := json.Marshal(player_payload)
// log.Printf("%v", string(x))
p.send <- player_payload
}
for s := range g.spectators {
s.send <- payload
}
}
2013-09-04 00:06:39 -07:00
func (g *game) run() {
2013-09-05 23:31:13 -07:00
var t0, t1 time.Time
2013-10-01 21:37:53 -07:00
ticker := time.NewTicker(time.Duration(*tick) * time.Millisecond)
2013-08-19 20:43:26 -07:00
for {
select {
2013-08-28 22:16:55 -07:00
case <-g.kill:
log.Printf("game %s: received kill signal, dying gracefully", g.id)
games.Lock()
for player := range g.players {
close(player.send)
}
delete(games.m, g.id)
games.Unlock()
2013-08-28 22:16:55 -07:00
return
2013-08-19 20:43:26 -07:00
case p := <-g.register:
g.players[p] = true
case p := <-g.unregister:
delete(g.players, p)
close(p.send)
2013-08-19 22:23:35 -07:00
case s := <-g.sregister:
g.spectators[s] = true
case s := <-g.sunregister:
delete(g.spectators, s)
close(s.send)
2013-10-01 21:37:53 -07:00
case <-ticker.C:
t0 = time.Now()
payload := NewBoardstate()
2013-08-19 20:43:26 -07:00
g.turn++
2013-09-07 19:13:12 -07:00
payload.Turn = g.turn
2013-08-19 20:43:26 -07:00
if *verbose {
log.Printf("\033[2JTurn: %v", g.turn)
log.Printf("Players: %v", len(g.players))
log.Printf("Projectiles: %v", len(g.projectiles))
log.Printf("Explosions: %v", len(g.splosions))
}
2013-09-03 23:26:40 -07:00
// UPDATE GAME STATE
robots_remaining := g.tick(payload)
2013-08-19 20:43:26 -07:00
// Determine end game?
2013-08-19 20:43:26 -07:00
if robots_remaining <= 1 && len(g.players) > 1 {
for p := range g.players {
if p.Robot.Health > 0 {
log.Printf("Robot %v Wins", p.Robot.Id)
2013-09-28 13:19:44 -07:00
log.Printf("game %s: game over", g.id)
2013-08-19 20:43:26 -07:00
}
2013-10-25 22:30:15 -07:00
p.reset(g)
2013-10-28 23:35:00 -07:00
g.obstacles = GenerateObstacles(5, g.width, g.height)
2013-08-19 20:43:26 -07:00
}
payload.Reset = true
} else {
payload.Reset = false
}
2013-09-05 23:31:13 -07:00
t1 = time.Now()
2013-08-19 20:43:26 -07:00
if *verbose {
log.Printf("Turn Processes %v\n", t1.Sub(t0))
}
// SEND THE UPDATE TO EACH PLAYER
g.send_update(payload)
2013-08-19 20:43:26 -07:00
t1 = time.Now()
if *verbose {
log.Printf("Sent Payload %v\n", t1.Sub(t0))
}
}
}
}