First pass of multibot support

This commit is contained in:
Fraser Graham 2013-11-08 21:25:42 -08:00
parent 49ff6af1f3
commit e39a97089e
6 changed files with 350 additions and 278 deletions

View File

@ -83,11 +83,14 @@ 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 {
players = append(players, pout{ for _, r := range p.Robots {
Name: p.Robot.Name, players = append(players, pout{
Id: p.Robot.Id, Name: r.Name,
}) Id: r.Id,
})
}
} }
ids = append(ids, gl{ ids = append(ids, gl{
Id: id, Id: id,

134
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)
}
if len(p.Robot.Message) > 0 { for _, r := range p.Robots {
if len(p.Robot.Message) > 100 { if r.Health > 0 {
p.Robot.Message = p.Robot.Message[0:99] living_robots++
p.Tick(g)
} }
payload.Messages = append(payload.Messages, p.Robot.Message)
if len(r.Message) > 0 {
if len(r.Message) > 100 {
r.Message = r.Message[0:99]
}
payload.Messages = append(payload.Messages, r.Message)
}
payload.OtherRobots = append(
payload.OtherRobots,
r.GetTruncatedDetails())
payload.AllBots = append(
payload.AllBots,
BotHealth{RobotId: r.Id, Health: r.Health})
} }
payload.OtherRobots = append( if living_robots > 0 {
payload.OtherRobots, players_remaining++
p.Robot.GetTruncatedDetails()) robots_remaining += living_robots
}
payload.AllBots = append(
payload.AllBots,
BotHealth{RobotId: p.Robot.Id, Health: p.Robot.Health})
} }
// 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,53 +179,63 @@ 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.OtherRobots = append( player_payload.MyRobots = append(player_payload.MyRobots, r)
player_payload.OtherRobots, player_payload.OtherRobots = append(
p.Robot.GetTruncatedDetails()) player_payload.OtherRobots,
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 {
// Filter robots by scanner if r.Health > 0 {
for player := range g.players { living_robots++
for _, scan_entry := range p.Robot.Scanners { // Filter robots by scanner
if player.Robot.Id == scan_entry.Id { for player := range g.players {
player_payload.OtherRobots = append( for _, scan_entry := range r.Scanners {
player_payload.OtherRobots, for _, r := range player.Robots {
player.Robot.GetTruncatedDetails()) if r.Id == scan_entry.Id {
player_payload.OtherRobots = append(
player_payload.OtherRobots,
r.GetTruncatedDetails())
}
}
} }
} }
}
// 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,
*proj)
}
for _, scan_entry := range p.Robot.Scanners {
if proj.Id == scan_entry.Id {
player_payload.Projectiles = append( player_payload.Projectiles = append(
player_payload.Projectiles, player_payload.Projectiles,
*proj) *proj)
} }
}
}
// Filter objects for _, scan_entry := range r.Scanners {
for _, ob := range g.obstacles { if proj.Id == scan_entry.Id {
if ob.distance_from_point(p.Robot.Position) < float32(p.Robot.Stats.ScannerRadius)+p.Robot.ScanCounter { player_payload.Projectiles = append(
player_payload.Obstacles = append( player_payload.Projectiles,
player_payload.Obstacles, *proj)
ob) }
}
}
// Filter objects
for _, ob := range g.obstacles {
if ob.distance_from_point(r.Position) < float32(r.Stats.ScannerRadius)+r.ScanCounter {
player_payload.Obstacles = append(
player_payload.Obstacles,
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 {
g.obstacles = GenerateObstacles(5, g.width, g.height)
log.Printf("game %s: game over", g.id)
for p := range g.players { for p := range g.players {
if p.Robot.Health > 0 { for _, r := range p.Robots {
log.Printf("Robot %v Wins", p.Robot.Id) if r.Health > 0 {
log.Printf("game %s: game over", g.id) log.Printf("Robot %v Survived", r.Id)
}
r.reset(g)
} }
p.reset(g)
g.obstacles = GenerateObstacles(5, g.width, g.height)
} }
payload.Reset = true payload.Reset = true
} else { } else {

373
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,16 @@ 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 PlayerInstructions
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 +41,89 @@ func (p *player) recv() {
break break
} }
if msg.Repair != nil && *msg.Repair == true { for _, r := range p.Robots {
p.Robot.TargetSpeed = 0
p.Robot.FireAt = nil
p.Robot.MoveTo = nil
if p.Robot.RepairCounter <= 0 {
p.Robot.RepairCounter = 3.0
}
} else if msg.Scan != nil && *msg.Scan == true {
p.Robot.TargetSpeed = 0
p.Robot.FireAt = nil
p.Robot.MoveTo = nil
p.Robot.ActiveScan = true
} else {
p.Robot.RepairCounter = 0
p.Robot.ActiveScan = false
// Reapiring halts all other activity msg, ok := msgs.RobotInstructions[r.Id]
if msg.MoveTo != nil { if !ok {
p.Robot.MoveTo = msg.MoveTo continue
}
if msg.FireAt != nil {
p.Robot.FireAt = msg.FireAt
} }
if msg.TargetSpeed != nil { if msg.Repair != nil && *msg.Repair == true {
p.Robot.TargetSpeed = float32(*msg.TargetSpeed) r.TargetSpeed = 0
r.FireAt = nil
r.MoveTo = nil
if r.RepairCounter <= 0 {
r.RepairCounter = 3.0
}
} else if msg.Scan != nil && *msg.Scan == true {
r.TargetSpeed = 0
r.FireAt = nil
r.MoveTo = nil
r.ActiveScan = true
} else { } else {
p.Robot.TargetSpeed = p.Robot.Stats.Speed r.RepairCounter = 0
r.ActiveScan = false
// Reapiring halts all other activity
if msg.MoveTo != nil {
r.MoveTo = msg.MoveTo
}
if msg.FireAt != nil {
r.FireAt = msg.FireAt
}
if msg.TargetSpeed != nil {
r.TargetSpeed = float32(*msg.TargetSpeed)
} else {
r.TargetSpeed = r.Stats.Speed
}
if msg.Probe != nil {
r.Probe = msg.Probe
r.ProbeResult = nil
} else {
r.Probe = nil
}
} }
if msg.Probe != nil { if msg.Message != nil {
p.Robot.Probe = msg.Probe r.Message = *msg.Message
p.Robot.ProbeResult = nil
} else {
p.Robot.Probe = nil
} }
} }
if msg.Message != nil {
p.Robot.Message = *msg.Message
}
} }
log.Printf("player %s: recv close", p.Robot.Id)
log.Printf("player %s: recv close", p.Robots[0].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 {
continue if bot.Id == r.Id {
} continue
player_rect := v.RectFromPoint(player.Robot.Position, 3) }
collision, _, pos := v.RectIntersection(player_rect, p.Robot.Position, move_vector) player_rect := v.RectFromPoint(bot.Position, 3)
if collision { collision, _, pos := v.RectIntersection(player_rect, r.Position, move_vector)
return collision, pos, player if collision {
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
} }
@ -122,184 +133,188 @@ func (p *player) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point
} }
func (p *player) Tick(g *game) { func (p *player) Tick(g *game) {
p.Robot.Collision = false for _, r := range p.Robots {
p.Robot.Hit = false
p.scan(g)
// Adjust Speed r.Collision = false
if p.Robot.Speed < p.Robot.TargetSpeed { r.Hit = false
p.Robot.Speed += (p.Robot.Stats.Acceleration * delta) r.scan(g)
if p.Robot.Speed > p.Robot.TargetSpeed {
p.Robot.Speed = p.Robot.TargetSpeed
}
} else if float32(math.Abs(float64(p.Robot.Speed-p.Robot.TargetSpeed))) > v.Epsilon { // Adjust Speed
p.Robot.Speed -= (p.Robot.Stats.Acceleration * delta) if r.Speed < r.TargetSpeed {
} else { r.Speed += (r.Stats.Acceleration * delta)
p.Robot.Speed = p.Robot.TargetSpeed if r.Speed > r.TargetSpeed {
} r.Speed = r.TargetSpeed
// Adjust Heading
current_heading := p.Robot.Heading
if current_heading.Mag() == 0 && p.Robot.MoveTo != nil {
// We may have been stopped before this and had no heading
current_heading = p.Robot.MoveTo.Sub(p.Robot.Position).Normalize()
}
new_heading := current_heading
if p.Robot.MoveTo != nil {
// Where do we WANT to be heading?
new_heading = p.Robot.MoveTo.Sub(p.Robot.Position).Normalize()
}
if new_heading.Mag() > 0 {
// Is our direction change too much? Hard coding to 5 degrees/s for now
angle := v.Angle(current_heading, new_heading) * v.Rad2deg
dir := 1.0
if angle < 0 {
dir = -1.0
}
// Max turn radius in this case is in degrees per second
if float32(math.Abs(float64(angle))) > (float32(p.Robot.Stats.TurnSpeed) * delta) {
// New heading should be a little less, take current heading and
// rotate by the max turn radius per frame.
rot := (float32(p.Robot.Stats.TurnSpeed) * delta) * v.Deg2rad
new_heading = current_heading.Rotate(rot * float32(dir))
}
move_vector := new_heading.Scale(p.Robot.Speed * delta)
collision, intersection_point, hit_player := p.checkCollisions(g, move_vector)
if collision {
p.Robot.Collision = true
if hit_player != nil {
hit_player.Robot.Health -= int(p.Robot.Speed / 10.0)
hit_player.Robot.Speed = (hit_player.Robot.Speed * 0.1)
hit_player.Robot.Heading = p.Robot.Heading
} }
move_by := intersection_point.Sub(p.Robot.Position)
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) } else if float32(math.Abs(float64(r.Speed-r.TargetSpeed))) > v.Epsilon {
p.Robot.Health -= int(p.Robot.Speed / 10.0) r.Speed -= (r.Stats.Acceleration * delta)
p.Robot.MoveTo = &p.Robot.Position
p.Robot.Speed = (p.Robot.Speed * 0.1)
p.Robot.Heading = p.Robot.Heading.Scale(-1.0)
} else { } else {
p.Robot.Position = p.Robot.Position.Add(move_vector) r.Speed = r.TargetSpeed
if new_heading.Mag() > 0 { }
p.Robot.Heading = new_heading
// Adjust Heading
current_heading := r.Heading
if current_heading.Mag() == 0 && r.MoveTo != nil {
// We may have been stopped before this and had no heading
current_heading = r.MoveTo.Sub(r.Position).Normalize()
}
new_heading := current_heading
if r.MoveTo != nil {
// Where do we WANT to be heading?
new_heading = r.MoveTo.Sub(r.Position).Normalize()
}
if new_heading.Mag() > 0 {
// Is our direction change too much? Hard coding to 5 degrees/s for now
angle := v.Angle(current_heading, new_heading) * v.Rad2deg
dir := 1.0
if angle < 0 {
dir = -1.0
}
// Max turn radius in this case is in degrees per second
if float32(math.Abs(float64(angle))) > (float32(r.Stats.TurnSpeed) * delta) {
// New heading should be a little less, take current heading and
// rotate by the max turn radius per frame.
rot := (float32(r.Stats.TurnSpeed) * delta) * v.Deg2rad
new_heading = current_heading.Rotate(rot * float32(dir))
}
move_vector := new_heading.Scale(r.Speed * delta)
collision, intersection_point, hit_robot := r.checkCollisions(g, move_vector)
if collision {
r.Collision = true
if hit_robot != nil {
hit_robot.Health -= int(r.Speed / 10.0)
hit_robot.Speed = (hit_robot.Speed * 0.1)
hit_robot.Heading = r.Heading
}
move_by := intersection_point.Sub(r.Position)
move_dist := move_by.Scale(float32(math.Floor(float64(move_by.Mag()-3.0))) / move_by.Mag())
r.Position = r.Position.Add(move_dist)
r.Health -= int(r.Speed / 10.0)
r.MoveTo = &r.Position
r.Speed = (r.Speed * 0.1)
r.Heading = r.Heading.Scale(-1.0)
} else { } else {
log.Printf("Zero Heading %v", new_heading) r.Position = r.Position.Add(move_vector)
if new_heading.Mag() > 0 {
r.Heading = new_heading
} else {
log.Printf("Zero Heading %v", new_heading)
}
}
}
// We only self repair when we're stopped
if math.Abs(float64(r.Speed)) < v.Epsilon && r.RepairCounter > 0 {
r.RepairCounter -= delta
if r.RepairCounter < 0 {
r.Health += g.repair_hp
r.RepairCounter = g.repair_rate
}
}
// We only self repair when we're stopped
if math.Abs(float64(r.Speed)) < v.Epsilon && r.ActiveScan {
r.ScanCounter += delta * float32(r.Stats.ScannerRadius) * 0.1
} else if r.ScanCounter > 0 {
r.ScanCounter -= delta * float32(r.Stats.ScannerRadius) * 0.05
if r.ScanCounter <= 0 {
r.ScanCounter = 0
}
}
if r.FireAt != nil {
proj := r.fire(g.projectiles, g.turn)
if proj != nil {
g.projectiles[proj] = true
}
}
if r.Probe != nil && r.ProbeResult == nil {
probe_vector := r.Probe.Sub(r.Position)
coll, pos, _ := r.checkCollisions(g, probe_vector)
if coll {
r.ProbeResult = &pos
} }
} }
} }
// We only self repair when we're stopped
if math.Abs(float64(p.Robot.Speed)) < v.Epsilon && p.Robot.RepairCounter > 0 {
p.Robot.RepairCounter -= delta
if p.Robot.RepairCounter < 0 {
p.Robot.Health += g.repair_hp
p.Robot.RepairCounter = g.repair_rate
}
}
// We only self repair when we're stopped
if math.Abs(float64(p.Robot.Speed)) < v.Epsilon && p.Robot.ActiveScan {
p.Robot.ScanCounter += delta * float32(p.Robot.Stats.ScannerRadius) * 0.1
} else if p.Robot.ScanCounter > 0 {
p.Robot.ScanCounter -= delta * float32(p.Robot.Stats.ScannerRadius) * 0.05
if p.Robot.ScanCounter <= 0 {
p.Robot.ScanCounter = 0
}
}
if p.Robot.FireAt != nil {
proj := p.fire(g.projectiles, g.turn)
if proj != nil {
g.projectiles[proj] = true
}
}
if p.Robot.Probe != nil && p.Robot.ProbeResult == nil {
probe_vector := p.Robot.Probe.Sub(p.Robot.Position)
coll, pos, _ := p.checkCollisions(g, probe_vector)
if coll {
p.Robot.ProbeResult = &pos
}
}
} }
func (p *player) scan(g *game) { func (r *Robot) scan(g *game) {
p.Robot.Scanners = p.Robot.Scanners[:0] r.Scanners = r.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 {
continue if bot.Id == r.Id || bot.Health <= 0 {
} continue
dist := v.Distance(player.Robot.Position, p.Robot.Position) }
if dist < float32(p.Robot.Stats.ScannerRadius+int(p.Robot.ScanCounter)) { dist := v.Distance(bot.Position, r.Position)
s := Scanner{ if dist < float32(r.Stats.ScannerRadius+int(r.ScanCounter)) {
Id: player.Robot.Id, s := Scanner{
Type: "robot", Id: bot.Id,
Type: "robot",
}
r.Scanners = append(r.Scanners, s)
} }
p.Robot.Scanners = append(p.Robot.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
// 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 {
continue if &r == p.Owner {
} 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 {
if dist < float32(p.Radius) { dist := v.Distance(r.Position, p.Position)
if player.Robot.Health > 0 { if dist < float32(p.Radius) {
player.Robot.Health -= p.Damage if r.Health > 0 {
player.Robot.Hit = true r.Health -= p.Damage
r.Hit = true
}
} }
} }
} }

View File

@ -40,8 +40,29 @@ 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), send: make(chan *Boardstate),
ws: ws,
}
for _, stats := range conf.Stats {
r := Robot{
Stats: DeriveStats(stats),
Id: player_id, Id: player_id,
Name: clientid.Name, Name: clientid.Name,
Health: conf.Stats.Hp, Health: 10,
Scanners: make([]Scanner, 0)}, Scanners: make([]Scanner, 0)}
send: make(chan *Boardstate), r.Health = r.Stats.Hp
ws: ws, log.Printf("Adding Robot: %v", r)
p.Robots = append(p.Robots, r)
r.reset(game)
} }
p.reset(game)
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"`
@ -170,3 +159,7 @@ type Instruction struct {
Scan *bool `json:"scan,omitempty"` Scan *bool `json:"scan,omitempty"`
Stats Stats `json:"stats"` Stats Stats `json:"stats"`
} }
type PlayerInstructions struct {
RobotInstructions map[string]Instruction `json:"instructions"`
}