Merge branch 'multibot'

Conflicts:
	game.go
This commit is contained in:
Fraser Graham 2013-11-08 22:31:33 -08:00
commit 34f5d6876d
6 changed files with 302 additions and 232 deletions

View File

@ -83,12 +83,15 @@ func listGames(w http.ResponseWriter, req *http.Request) {
ids := make([]gl, 0) ids := make([]gl, 0)
for id, g := range games.m { for id, g := range games.m {
players := make([]pout, 0) players := make([]pout, 0)
// TODO - players instead of robots?
for p, _ := range g.players { for p, _ := range g.players {
for _, r := range p.Robots {
players = append(players, pout{ players = append(players, pout{
Name: p.Robot.Name, Name: r.Name,
Id: p.Robot.Id, Id: r.Id,
}) })
} }
}
ids = append(ids, gl{ ids = append(ids, gl{
Id: id, Id: id,
Players: players, Players: players,

80
game.go
View File

@ -106,30 +106,40 @@ func NewGame(id string, width, height float32) *game {
func (g *game) tick(payload *Boardstate) int { func (g *game) tick(payload *Boardstate) int {
robots_remaining := 0 robots_remaining := 0
players_remaining := 0
payload.Obstacles = g.obstacles payload.Obstacles = g.obstacles
// Update Players // Update Players
for p := range g.players { for p := range g.players {
if p.Robot.Health > 0 { living_robots := 0
robots_remaining++
p.Tick(g) for _, r := range p.Robots {
if r.Health > 0 {
living_robots++
r.Tick(g)
} }
if len(p.Robot.Message) > 0 { if len(r.Message) > 0 {
if len(p.Robot.Message) > 100 { if len(r.Message) > 100 {
p.Robot.Message = p.Robot.Message[0:99] r.Message = r.Message[0:99]
} }
payload.Messages = append(payload.Messages, p.Robot.Message) payload.Messages = append(payload.Messages, r.Message)
} }
payload.OtherRobots = append( payload.OtherRobots = append(
payload.OtherRobots, payload.OtherRobots,
p.Robot.GetTruncatedDetails()) r.GetTruncatedDetails())
payload.AllBots = append( payload.AllBots = append(
payload.AllBots, payload.AllBots,
BotHealth{RobotId: p.Robot.Id, Health: p.Robot.Health}) BotHealth{RobotId: r.Id, Health: r.Health})
}
if living_robots > 0 {
players_remaining++
robots_remaining += living_robots
}
} }
// Update Projectiles // Update Projectiles
@ -151,7 +161,7 @@ func (g *game) tick(payload *Boardstate) int {
payload.Splosions = append(payload.Splosions, *s) payload.Splosions = append(payload.Splosions, *s)
} }
return robots_remaining return players_remaining
} }
func (g *game) send_update(payload *Boardstate) { func (g *game) send_update(payload *Boardstate) {
@ -160,6 +170,7 @@ func (g *game) send_update(payload *Boardstate) {
sort.Sort(AllRobotSorter{Robots: payload.AllBots}) sort.Sort(AllRobotSorter{Robots: payload.AllBots})
for p := range g.players { for p := range g.players {
// Copy the payload but only add the robots in scanner range // Copy the payload but only add the robots in scanner range
player_payload := NewBoardstate() player_payload := NewBoardstate()
player_payload.Messages = payload.Messages player_payload.Messages = payload.Messages
@ -168,22 +179,29 @@ func (g *game) send_update(payload *Boardstate) {
player_payload.AllBots = payload.AllBots player_payload.AllBots = payload.AllBots
player_payload.Turn = payload.Turn player_payload.Turn = payload.Turn
player_payload.Reset = payload.Reset player_payload.Reset = payload.Reset
player_payload.MyRobots = append(player_payload.MyRobots, p.Robot) for _, r := range p.Robots {
player_payload.MyRobots = append(player_payload.MyRobots, *r)
player_payload.OtherRobots = append( player_payload.OtherRobots = append(
player_payload.OtherRobots, player_payload.OtherRobots,
p.Robot.GetTruncatedDetails()) r.GetTruncatedDetails())
}
player_payload.Projectiles = []Projectile{} player_payload.Projectiles = []Projectile{}
player_payload.Obstacles = []Obstacle{} player_payload.Obstacles = []Obstacle{}
living_robots := 0
if p.Robot.Health > 0 { for _, r := range p.Robots {
if r.Health > 0 {
living_robots++
// Filter robots by scanner // Filter robots by scanner
for player := range g.players { for player := range g.players {
for _, scan_entry := range p.Robot.Scanners { for _, scan_entry := range r.Scanners {
if player.Robot.Id == scan_entry.Id { for _, r := range player.Robots {
if r.Id == scan_entry.Id {
player_payload.OtherRobots = append( player_payload.OtherRobots = append(
player_payload.OtherRobots, player_payload.OtherRobots,
player.Robot.GetTruncatedDetails()) r.GetTruncatedDetails())
}
} }
} }
} }
@ -191,13 +209,13 @@ func (g *game) send_update(payload *Boardstate) {
// Filter projectiles // Filter projectiles
for proj := range g.projectiles { for proj := range g.projectiles {
if proj.Owner == p { if proj.Owner == r {
player_payload.Projectiles = append( player_payload.Projectiles = append(
player_payload.Projectiles, player_payload.Projectiles,
*proj) *proj)
} }
for _, scan_entry := range p.Robot.Scanners { for _, scan_entry := range r.Scanners {
if proj.Id == scan_entry.Id { if proj.Id == scan_entry.Id {
player_payload.Projectiles = append( player_payload.Projectiles = append(
player_payload.Projectiles, player_payload.Projectiles,
@ -208,13 +226,16 @@ func (g *game) send_update(payload *Boardstate) {
// Filter objects // Filter objects
for _, ob := range g.obstacles { for _, ob := range g.obstacles {
if ob.distance_from_point(p.Robot.Position) < float32(p.Robot.Stats.ScannerRadius)+p.Robot.ScanCounter { if ob.distance_from_point(r.Position) < float32(r.Stats.ScannerRadius)+r.ScanCounter {
player_payload.Obstacles = append( player_payload.Obstacles = append(
player_payload.Obstacles, player_payload.Obstacles,
ob) ob)
} }
} }
} else { }
}
if living_robots == 0 {
player_payload.OtherRobots = payload.OtherRobots player_payload.OtherRobots = payload.OtherRobots
player_payload.Projectiles = payload.Projectiles player_payload.Projectiles = payload.Projectiles
player_payload.Obstacles = payload.Obstacles player_payload.Obstacles = payload.Obstacles
@ -266,17 +287,20 @@ func (g *game) run() {
} }
// UPDATE GAME STATE // UPDATE GAME STATE
robots_remaining := g.tick(payload) players_remaining := g.tick(payload)
// Determine end game? // Determine end game?
if robots_remaining <= 1 && len(g.players) > 1 { if players_remaining <= 1 && len(g.players) > 1 {
for p := range g.players {
if p.Robot.Health > 0 {
log.Printf("Robot %v Wins", p.Robot.Id)
log.Printf("game %s: game over", g.id)
}
g.obstacles = GenerateObstacles(5, g.width, g.height) g.obstacles = GenerateObstacles(5, g.width, g.height)
p.reset(g) 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 payload.Reset = true
} else { } else {

238
player.go
View File

@ -10,7 +10,7 @@ import (
type player struct { type player struct {
ws *websocket.Conn ws *websocket.Conn
Robot Robot Robots []*Robot
send chan *Boardstate send chan *Boardstate
Instruction Instruction Instruction Instruction
} }
@ -23,15 +23,17 @@ func (p *player) sender() {
} }
} }
p.ws.Close() p.ws.Close()
log.Printf("player %s: sender close", p.Robot.Id) // TODO
log.Printf("player %s: sender close", p.Robots[0].Id)
} }
func (p *player) recv() { func (p *player) recv() {
for { for {
// XXX: need to mark myself as having received something, also binding // XXX: need to mark myself as having received something, also binding
// such action to a particular game turn ID // such action to a particular game turn ID
var msg Instruction var msgs map[string]Instruction
err := websocket.JSON.Receive(p.ws, &msg) err := websocket.JSON.Receive(p.ws, &msgs)
if err != nil { if err != nil {
// TODO: perhaps we could be a bit more precise in the handling of // TODO: perhaps we could be a bit more precise in the handling of
// this 'error' by selecting on some kill signal channel and this // this 'error' by selecting on some kill signal channel and this
@ -40,79 +42,92 @@ func (p *player) recv() {
break break
} }
for _, r := range p.Robots {
msg, ok := msgs[r.Id]
if !ok {
continue
}
// log.Printf("%v", msg.FireAt)
if msg.Repair != nil && *msg.Repair == true { if msg.Repair != nil && *msg.Repair == true {
p.Robot.TargetSpeed = 0 r.TargetSpeed = 0
p.Robot.FireAt = nil r.FireAt = nil
p.Robot.MoveTo = nil r.MoveTo = nil
if p.Robot.RepairCounter <= 0 { if r.RepairCounter <= 0 {
p.Robot.RepairCounter = 3.0 r.RepairCounter = 3.0
} }
} else if msg.Scan != nil && *msg.Scan == true { } else if msg.Scan != nil && *msg.Scan == true {
p.Robot.TargetSpeed = 0 r.TargetSpeed = 0
p.Robot.FireAt = nil r.FireAt = nil
p.Robot.MoveTo = nil r.MoveTo = nil
p.Robot.ActiveScan = true r.ActiveScan = true
} else { } else {
p.Robot.RepairCounter = 0 r.RepairCounter = 0
p.Robot.ActiveScan = false r.ActiveScan = false
// Reapiring halts all other activity // Reapiring halts all other activity
if msg.MoveTo != nil { if msg.MoveTo != nil {
p.Robot.MoveTo = msg.MoveTo r.MoveTo = msg.MoveTo
} }
if msg.FireAt != nil { if msg.FireAt != nil {
p.Robot.FireAt = msg.FireAt r.FireAt = msg.FireAt
} }
if msg.TargetSpeed != nil { if msg.TargetSpeed != nil {
p.Robot.TargetSpeed = float32(*msg.TargetSpeed) r.TargetSpeed = float32(*msg.TargetSpeed)
} else { } else {
p.Robot.TargetSpeed = p.Robot.Stats.Speed r.TargetSpeed = r.Stats.Speed
} }
if msg.Probe != nil { if msg.Probe != nil {
p.Robot.Probe = msg.Probe r.Probe = msg.Probe
p.Robot.ProbeResult = nil r.ProbeResult = nil
} else { } else {
p.Robot.Probe = nil r.Probe = nil
} }
} }
if msg.Message != nil { if msg.Message != nil {
p.Robot.Message = *msg.Message r.Message = *msg.Message
}
}
} }
} log.Printf("player %s: recv close", p.Robots[0].Id)
log.Printf("player %s: recv close", p.Robot.Id)
p.ws.Close() p.ws.Close()
} }
func (p *player) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point2d, *player) { func (r *Robot) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point2d, *Robot) {
collision := false collision := false
intersection_point := v.Point2d{X: 0, Y: 0} intersection_point := v.Point2d{X: 0, Y: 0}
// Check Walls // Check Walls
r_walls := v.Rect2d{A: v.Point2d{X: 0, Y: 0}, B: v.Point2d{X: g.width, Y: g.height}} r_walls := v.Rect2d{A: v.Point2d{X: 0, Y: 0}, B: v.Point2d{X: g.width, Y: g.height}}
collision, _, pos := v.RectIntersection(r_walls, p.Robot.Position, move_vector) collision, _, pos := v.RectIntersection(r_walls, r.Position, move_vector)
if collision { if collision {
return collision, pos, nil return collision, pos, nil
} }
// Check Other Bots // Check Other Bots
for player := range g.players { for player := range g.players {
if player.Robot.Id == p.Robot.Id { for _, bot := range player.Robots {
if bot.Id == r.Id {
continue continue
} }
player_rect := v.RectFromPoint(player.Robot.Position, 3) player_rect := v.RectFromPoint(bot.Position, 3)
collision, _, pos := v.RectIntersection(player_rect, p.Robot.Position, move_vector) collision, _, pos := v.RectIntersection(player_rect, r.Position, move_vector)
if collision { if collision {
return collision, pos, player return collision, pos, bot
}
} }
} }
// Check Obstacles // Check Obstacles
for _, obj := range g.obstacles { for _, obj := range g.obstacles {
collision, _, pos := v.RectIntersection(obj.Bounds, p.Robot.Position, move_vector) collision, _, pos := v.RectIntersection(obj.Bounds, r.Position, move_vector)
if collision { if collision {
return collision, pos, nil return collision, pos, nil
} }
@ -121,35 +136,35 @@ func (p *player) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point
return collision, intersection_point, nil return collision, intersection_point, nil
} }
func (p *player) Tick(g *game) { func (r *Robot) Tick(g *game) {
p.Robot.Collision = false r.Collision = false
p.Robot.Hit = false r.Hit = false
p.scan(g) r.scan(g)
// Adjust Speed // Adjust Speed
if p.Robot.Speed < p.Robot.TargetSpeed { if r.Speed < r.TargetSpeed {
p.Robot.Speed += (p.Robot.Stats.Acceleration * delta) r.Speed += (r.Stats.Acceleration * delta)
if p.Robot.Speed > p.Robot.TargetSpeed { if r.Speed > r.TargetSpeed {
p.Robot.Speed = p.Robot.TargetSpeed r.Speed = r.TargetSpeed
} }
} else if float32(math.Abs(float64(p.Robot.Speed-p.Robot.TargetSpeed))) > v.Epsilon { } else if float32(math.Abs(float64(r.Speed-r.TargetSpeed))) > v.Epsilon {
p.Robot.Speed -= (p.Robot.Stats.Acceleration * delta) r.Speed -= (r.Stats.Acceleration * delta)
} else { } else {
p.Robot.Speed = p.Robot.TargetSpeed r.Speed = r.TargetSpeed
} }
// Adjust Heading // Adjust Heading
current_heading := p.Robot.Heading current_heading := r.Heading
if current_heading.Mag() == 0 && p.Robot.MoveTo != nil { if current_heading.Mag() == 0 && r.MoveTo != nil {
// We may have been stopped before this and had no heading // We may have been stopped before this and had no heading
current_heading = p.Robot.MoveTo.Sub(p.Robot.Position).Normalize() current_heading = r.MoveTo.Sub(r.Position).Normalize()
} }
new_heading := current_heading new_heading := current_heading
if p.Robot.MoveTo != nil { if r.MoveTo != nil {
// Where do we WANT to be heading? // Where do we WANT to be heading?
new_heading = p.Robot.MoveTo.Sub(p.Robot.Position).Normalize() new_heading = r.MoveTo.Sub(r.Position).Normalize()
} }
if new_heading.Mag() > 0 { if new_heading.Mag() > 0 {
@ -162,35 +177,35 @@ func (p *player) Tick(g *game) {
} }
// Max turn radius in this case is in degrees per second // Max turn radius in this case is in degrees per second
if float32(math.Abs(float64(angle))) > (float32(p.Robot.Stats.TurnSpeed) * delta) { if float32(math.Abs(float64(angle))) > (float32(r.Stats.TurnSpeed) * delta) {
// New heading should be a little less, take current heading and // New heading should be a little less, take current heading and
// rotate by the max turn radius per frame. // rotate by the max turn radius per frame.
rot := (float32(p.Robot.Stats.TurnSpeed) * delta) * v.Deg2rad rot := (float32(r.Stats.TurnSpeed) * delta) * v.Deg2rad
new_heading = current_heading.Rotate(rot * float32(dir)) new_heading = current_heading.Rotate(rot * float32(dir))
} }
move_vector := new_heading.Scale(p.Robot.Speed * delta) move_vector := new_heading.Scale(r.Speed * delta)
collision, intersection_point, hit_player := p.checkCollisions(g, move_vector) collision, intersection_point, hit_robot := r.checkCollisions(g, move_vector)
if collision { if collision {
p.Robot.Collision = true r.Collision = true
if hit_player != nil { if hit_robot != nil {
hit_player.Robot.Health -= int(p.Robot.Speed / 10.0) hit_robot.Health -= int(r.Speed / 10.0)
hit_player.Robot.Speed = (hit_player.Robot.Speed * 0.1) hit_robot.Speed = (hit_robot.Speed * 0.1)
hit_player.Robot.Heading = p.Robot.Heading hit_robot.Heading = r.Heading
} }
move_by := intersection_point.Sub(p.Robot.Position) move_by := intersection_point.Sub(r.Position)
move_dist := move_by.Scale(float32(math.Floor(float64(move_by.Mag()-3.0))) / move_by.Mag()) move_dist := move_by.Scale(float32(math.Floor(float64(move_by.Mag()-3.0))) / move_by.Mag())
p.Robot.Position = p.Robot.Position.Add(move_dist) r.Position = r.Position.Add(move_dist)
p.Robot.Health -= int(p.Robot.Speed / 10.0) r.Health -= int(r.Speed / 10.0)
p.Robot.MoveTo = &p.Robot.Position r.MoveTo = &r.Position
p.Robot.Speed = (p.Robot.Speed * 0.1) r.Speed = (r.Speed * 0.1)
p.Robot.Heading = p.Robot.Heading.Scale(-1.0) r.Heading = r.Heading.Scale(-1.0)
} else { } else {
p.Robot.Position = p.Robot.Position.Add(move_vector) r.Position = r.Position.Add(move_vector)
if new_heading.Mag() > 0 { if new_heading.Mag() > 0 {
p.Robot.Heading = new_heading r.Heading = new_heading
} else { } else {
log.Printf("Zero Heading %v", new_heading) log.Printf("Zero Heading %v", new_heading)
} }
@ -198,108 +213,111 @@ func (p *player) Tick(g *game) {
} }
// We only self repair when we're stopped // We only self repair when we're stopped
if math.Abs(float64(p.Robot.Speed)) < v.Epsilon && p.Robot.RepairCounter > 0 { if math.Abs(float64(r.Speed)) < v.Epsilon && r.RepairCounter > 0 {
p.Robot.RepairCounter -= delta r.RepairCounter -= delta
if p.Robot.RepairCounter < 0 { if r.RepairCounter < 0 {
p.Robot.Health += g.repair_hp r.Health += g.repair_hp
p.Robot.RepairCounter = g.repair_rate r.RepairCounter = g.repair_rate
} }
} }
// We only self repair when we're stopped // We only self repair when we're stopped
if math.Abs(float64(p.Robot.Speed)) < v.Epsilon && p.Robot.ActiveScan { if math.Abs(float64(r.Speed)) < v.Epsilon && r.ActiveScan {
p.Robot.ScanCounter += delta * float32(p.Robot.Stats.ScannerRadius) * 0.1 r.ScanCounter += delta * float32(r.Stats.ScannerRadius) * 0.1
} else if p.Robot.ScanCounter > 0 { } else if r.ScanCounter > 0 {
p.Robot.ScanCounter -= delta * float32(p.Robot.Stats.ScannerRadius) * 0.05 r.ScanCounter -= delta * float32(r.Stats.ScannerRadius) * 0.05
if p.Robot.ScanCounter <= 0 { if r.ScanCounter <= 0 {
p.Robot.ScanCounter = 0 r.ScanCounter = 0
} }
} }
if p.Robot.FireAt != nil { if r.FireAt != nil {
proj := p.fire(g.projectiles, g.turn) proj := r.fire(g.projectiles, g.turn)
if proj != nil { if proj != nil {
g.projectiles[proj] = true g.projectiles[proj] = true
} }
} }
if p.Robot.Probe != nil && p.Robot.ProbeResult == nil { if r.Probe != nil && r.ProbeResult == nil {
probe_vector := p.Robot.Probe.Sub(p.Robot.Position) probe_vector := r.Probe.Sub(r.Position)
coll, pos, _ := p.checkCollisions(g, probe_vector) coll, pos, _ := r.checkCollisions(g, probe_vector)
if coll { if coll {
p.Robot.ProbeResult = &pos r.ProbeResult = &pos
}
} }
} }
} func (r *Robot) scan(g *game) {
r.Scanners = r.Scanners[:0]
func (p *player) scan(g *game) {
p.Robot.Scanners = p.Robot.Scanners[:0]
for player, _ := range g.players { for player, _ := range g.players {
if player.Robot.Id == p.Robot.Id || player.Robot.Health <= 0 { for _, bot := range player.Robots {
if bot.Id == r.Id || bot.Health <= 0 {
continue continue
} }
dist := v.Distance(player.Robot.Position, p.Robot.Position) dist := v.Distance(bot.Position, r.Position)
if dist < float32(p.Robot.Stats.ScannerRadius+int(p.Robot.ScanCounter)) { if dist < float32(r.Stats.ScannerRadius+int(r.ScanCounter)) {
s := Scanner{ s := Scanner{
Id: player.Robot.Id, Id: bot.Id,
Type: "robot", Type: "robot",
} }
p.Robot.Scanners = append(p.Robot.Scanners, s) r.Scanners = append(r.Scanners, s)
}
} }
} }
for proj, _ := range g.projectiles { for proj, _ := range g.projectiles {
if proj.Owner == p { if proj.Owner == r {
continue continue
} }
dist := v.Distance(proj.Position, p.Robot.Position) dist := v.Distance(proj.Position, r.Position)
if dist < float32(p.Robot.Stats.ScannerRadius+int(p.Robot.ScanCounter)) { if dist < float32(r.Stats.ScannerRadius+int(r.ScanCounter)) {
s := Scanner{ s := Scanner{
Id: proj.Id, Id: proj.Id,
Type: "projectile", Type: "projectile",
} }
p.Robot.Scanners = append(p.Robot.Scanners, s) r.Scanners = append(r.Scanners, s)
} }
} }
} }
func (p *player) fire(projectiles map[*Projectile]bool, turn int) *Projectile { func (r *Robot) fire(projectiles map[*Projectile]bool, turn int) *Projectile {
// Throttle the fire rate // Throttle the fire rate
time_since_fired := (float32(turn) * (delta * 1000)) - (float32(p.Robot.LastFired) * (delta * 1000)) time_since_fired := (float32(turn) * (delta * 1000)) - (float32(r.LastFired) * (delta * 1000))
if time_since_fired < float32(p.Robot.Stats.FireRate) { if time_since_fired < float32(r.Stats.FireRate) {
return nil return nil
} }
p.Robot.LastFired = turn r.LastFired = turn
return &Projectile{ return &Projectile{
Id: idg.Hash(), Id: idg.Hash(),
Position: p.Robot.Position, Position: r.Position,
MoveTo: *p.Robot.FireAt, MoveTo: *r.FireAt,
Damage: p.Robot.Stats.WeaponDamage, Damage: r.Stats.WeaponDamage,
Radius: p.Robot.Stats.WeaponRadius, Radius: r.Stats.WeaponRadius,
Speed: p.Robot.Stats.WeaponSpeed, Speed: r.Stats.WeaponSpeed,
Owner: p, Owner: r,
} }
} }
func (p *player) reset(g *game) { func (r *Robot) reset(g *game) {
for { for {
start_pos := v.Point2d{ start_pos := v.Point2d{
X: rand.Float32() * float32(g.width), X: rand.Float32() * float32(g.width),
Y: rand.Float32() * float32(g.height), Y: rand.Float32() * float32(g.height),
} }
p.Robot.MoveTo = &start_pos r.MoveTo = &start_pos
p.Robot.Position = start_pos r.Position = start_pos
p.Robot.Health = p.Robot.Stats.Hp r.Health = r.Stats.Hp
log.Printf("Reset %v", r)
// Check Obstacles // Check Obstacles
retry := false retry := false
for _, obj := range g.obstacles { for _, obj := range g.obstacles {
_, inside, _ := v.RectIntersection(obj.Bounds, p.Robot.Position, v.Vector2d{0, 0}) _, inside, _ := v.RectIntersection(obj.Bounds, r.Position, v.Vector2d{0, 0})
if inside { if inside {
retry = true retry = true
} }

View File

@ -12,7 +12,7 @@ type Projectile struct {
Radius int `json:"-"` Radius int `json:"-"`
Speed float32 `json:"-"` Speed float32 `json:"-"`
Damage int `json:"-"` Damage int `json:"-"`
Owner *player `json:"-"` Owner *Robot `json:"-"`
} }
func (p *Projectile) Tick(g *game) { func (p *Projectile) Tick(g *game) {
@ -23,21 +23,23 @@ func (p *Projectile) Tick(g *game) {
hit_player := false hit_player := false
for player := range g.players { for player := range g.players {
if player == p.Owner { for _, r := range player.Robots {
if r == p.Owner {
continue continue
} }
player_rect := v.RectFromPoint(player.Robot.Position, 3) player_rect := v.RectFromPoint(r.Position, 3)
collision, _, _ := v.RectIntersection(player_rect, p.Position, v_scaled) collision, _, _ := v.RectIntersection(player_rect, p.Position, v_scaled)
if collision { if collision {
hit_player = true hit_player = true
if player.Robot.Health > 0 { if r.Health > 0 {
// Direct hit causes more damage // Direct hit causes more damage
log.Printf("Direct Hit %v, Dmg:%v", player.Robot.Id, p.Damage) log.Printf("Direct Hit %v, Dmg:%v", r.Id, p.Damage)
player.Robot.Health -= p.Damage r.Health -= p.Damage
player.Robot.Hit = true r.Hit = true
}
} }
} }
} }
@ -73,11 +75,13 @@ func (p *Projectile) Tick(g *game) {
g.splosions[splo] = true g.splosions[splo] = true
for player := range g.players { for player := range g.players {
dist := v.Distance(player.Robot.Position, p.Position) for _, r := range player.Robots {
dist := v.Distance(r.Position, p.Position)
if dist < float32(p.Radius) { if dist < float32(p.Radius) {
if player.Robot.Health > 0 { if r.Health > 0 {
player.Robot.Health -= p.Damage r.Health -= p.Damage
player.Robot.Hit = true r.Hit = true
}
} }
} }
} }

View File

@ -41,7 +41,28 @@ func (c *ClientID) Valid() (bool, string) {
type ClientConfig struct { type ClientConfig struct {
ID string `json:"id"` ID string `json:"id"`
Stats StatsRequest `json:"stats"` Stats map[string]StatsRequest `json:"stats"`
}
func (config ClientConfig) Valid() bool {
total := 0
for _, s := range config.Stats {
total += (s.Speed +
s.Hp +
s.WeaponRadius +
s.ScannerRadius +
s.Acceleration +
s.TurnSpeed +
s.FireRate +
s.WeaponDamage +
s.WeaponSpeed)
}
// allowing for 50 pts in every category
if total > 1000 {
return false
}
return true
} }
type BoardSize struct { type BoardSize struct {
@ -152,38 +173,50 @@ func addPlayer(ws *websocket.Conn) {
log.Printf("%s Waiting for client to send conf ...", player_id) log.Printf("%s Waiting for client to send conf ...", player_id)
err = websocket.JSON.Receive(ws, &conf) err = websocket.JSON.Receive(ws, &conf)
log.Printf("conf received: %+v", conf) log.Printf("conf received: %+v", conf)
if err != nil { if err != nil {
log.Printf("%s %s config parse error", gid.Id, player_id) log.Printf("%s %s config parse error", gid.Id, player_id)
websocket.JSON.Send(ws, NewFailure("config parse error")) websocket.JSON.Send(ws, NewFailure("config parse error"))
return return
} }
// TODO: verify conf's type // TODO: verify conf's type
if conf.Stats.Valid() { if conf.Valid() {
log.Printf("Config is Valid, continuing")
_ = websocket.JSON.Send(ws, NewHandshake(player_id, true)) _ = websocket.JSON.Send(ws, NewHandshake(player_id, true))
break break
} else { } else {
log.Printf("Config is INVALID, abort")
_ = websocket.JSON.Send(ws, NewHandshake(player_id, false)) _ = websocket.JSON.Send(ws, NewHandshake(player_id, false))
} }
} }
p := &player{ p := &player{
Robot: Robot{ Robots: []*Robot{},
Stats: DeriveStats(conf.Stats),
Id: player_id,
Name: clientid.Name,
Health: conf.Stats.Hp,
Scanners: make([]Scanner, 0)},
send: make(chan *Boardstate), send: make(chan *Boardstate),
ws: ws, ws: ws,
} }
p.reset(game)
for _, stats := range conf.Stats {
r := Robot{
Stats: DeriveStats(stats),
Id: idg.Hash(),
Name: clientid.Name,
Health: 10,
Scanners: make([]Scanner, 0)}
r.Health = r.Stats.Hp
log.Printf("Adding Robot: %v", r)
r.reset(game)
p.Robots = append(p.Robots, &r)
}
game.register <- p game.register <- p
defer func() { defer func() {
game.unregister <- p game.unregister <- p
}() }()
go p.sender() go p.sender()
p.recv() p.recv()
log.Printf("game %s: player %v has been disconnected from this game\n", gid.Id, p.Robot.Id) log.Printf("game %s: player %v has been disconnected from this game\n", gid.Id, p.Robots[0].Id)
case "spectator": case "spectator":
s := &Spectator{ s := &Spectator{
send: make(chan *Boardstate), send: make(chan *Boardstate),

View File

@ -149,17 +149,6 @@ func DeriveStats(request StatsRequest) Stats {
return s return s
} }
func (s StatsRequest) Valid() bool {
total := (s.Speed + s.Hp + s.WeaponRadius +
s.ScannerRadius + s.Acceleration + s.TurnSpeed + s.FireRate)
// allowing for 50 pts in every category
if total > 500 {
return false
}
return true
}
type Instruction struct { type Instruction struct {
Message *string `json:"message,omitempty"` Message *string `json:"message,omitempty"`
MoveTo *v.Point2d `json:"move_to,omitempty"` MoveTo *v.Point2d `json:"move_to,omitempty"`
@ -168,5 +157,4 @@ type Instruction struct {
TargetSpeed *float32 `json:"target_speed,omitempty"` TargetSpeed *float32 `json:"target_speed,omitempty"`
Repair *bool `json:"repair,omitempty"` Repair *bool `json:"repair,omitempty"`
Scan *bool `json:"scan,omitempty"` Scan *bool `json:"scan,omitempty"`
Stats Stats `json:"stats"`
} }