Merge branch 'multibot'
Conflicts: game.go
This commit is contained in:
commit
34f5d6876d
@ -83,12 +83,15 @@ func listGames(w http.ResponseWriter, req *http.Request) {
|
||||
ids := make([]gl, 0)
|
||||
for id, g := range games.m {
|
||||
players := make([]pout, 0)
|
||||
// TODO - players instead of robots?
|
||||
for p, _ := range g.players {
|
||||
for _, r := range p.Robots {
|
||||
players = append(players, pout{
|
||||
Name: p.Robot.Name,
|
||||
Id: p.Robot.Id,
|
||||
Name: r.Name,
|
||||
Id: r.Id,
|
||||
})
|
||||
}
|
||||
}
|
||||
ids = append(ids, gl{
|
||||
Id: id,
|
||||
Players: players,
|
||||
|
80
game.go
80
game.go
@ -106,30 +106,40 @@ func NewGame(id string, width, height float32) *game {
|
||||
|
||||
func (g *game) tick(payload *Boardstate) int {
|
||||
robots_remaining := 0
|
||||
players_remaining := 0
|
||||
|
||||
payload.Obstacles = g.obstacles
|
||||
|
||||
// Update Players
|
||||
for p := range g.players {
|
||||
if p.Robot.Health > 0 {
|
||||
robots_remaining++
|
||||
p.Tick(g)
|
||||
living_robots := 0
|
||||
|
||||
for _, r := range p.Robots {
|
||||
if r.Health > 0 {
|
||||
living_robots++
|
||||
r.Tick(g)
|
||||
}
|
||||
|
||||
if len(p.Robot.Message) > 0 {
|
||||
if len(p.Robot.Message) > 100 {
|
||||
p.Robot.Message = p.Robot.Message[0:99]
|
||||
if len(r.Message) > 0 {
|
||||
if len(r.Message) > 100 {
|
||||
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,
|
||||
p.Robot.GetTruncatedDetails())
|
||||
r.GetTruncatedDetails())
|
||||
|
||||
payload.AllBots = append(
|
||||
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
|
||||
@ -151,7 +161,7 @@ func (g *game) tick(payload *Boardstate) int {
|
||||
payload.Splosions = append(payload.Splosions, *s)
|
||||
}
|
||||
|
||||
return robots_remaining
|
||||
return players_remaining
|
||||
}
|
||||
|
||||
func (g *game) send_update(payload *Boardstate) {
|
||||
@ -160,6 +170,7 @@ func (g *game) send_update(payload *Boardstate) {
|
||||
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.Messages = payload.Messages
|
||||
@ -168,22 +179,29 @@ func (g *game) send_update(payload *Boardstate) {
|
||||
player_payload.AllBots = payload.AllBots
|
||||
player_payload.Turn = payload.Turn
|
||||
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,
|
||||
p.Robot.GetTruncatedDetails())
|
||||
r.GetTruncatedDetails())
|
||||
}
|
||||
|
||||
player_payload.Projectiles = []Projectile{}
|
||||
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
|
||||
for player := range g.players {
|
||||
for _, scan_entry := range p.Robot.Scanners {
|
||||
if player.Robot.Id == scan_entry.Id {
|
||||
for _, scan_entry := range r.Scanners {
|
||||
for _, r := range player.Robots {
|
||||
if r.Id == scan_entry.Id {
|
||||
player_payload.OtherRobots = append(
|
||||
player_payload.OtherRobots,
|
||||
player.Robot.GetTruncatedDetails())
|
||||
r.GetTruncatedDetails())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -191,13 +209,13 @@ func (g *game) send_update(payload *Boardstate) {
|
||||
// Filter 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 {
|
||||
for _, scan_entry := range r.Scanners {
|
||||
if proj.Id == scan_entry.Id {
|
||||
player_payload.Projectiles = append(
|
||||
player_payload.Projectiles,
|
||||
@ -208,13 +226,16 @@ func (g *game) send_update(payload *Boardstate) {
|
||||
|
||||
// Filter objects
|
||||
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,
|
||||
ob)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
if living_robots == 0 {
|
||||
player_payload.OtherRobots = payload.OtherRobots
|
||||
player_payload.Projectiles = payload.Projectiles
|
||||
player_payload.Obstacles = payload.Obstacles
|
||||
@ -266,17 +287,20 @@ func (g *game) run() {
|
||||
}
|
||||
|
||||
// UPDATE GAME STATE
|
||||
robots_remaining := g.tick(payload)
|
||||
players_remaining := g.tick(payload)
|
||||
|
||||
// Determine end game?
|
||||
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)
|
||||
log.Printf("game %s: game over", g.id)
|
||||
}
|
||||
if players_remaining <= 1 && len(g.players) > 1 {
|
||||
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
|
||||
} else {
|
||||
|
236
player.go
236
player.go
@ -10,7 +10,7 @@ import (
|
||||
|
||||
type player struct {
|
||||
ws *websocket.Conn
|
||||
Robot Robot
|
||||
Robots []*Robot
|
||||
send chan *Boardstate
|
||||
Instruction Instruction
|
||||
}
|
||||
@ -23,15 +23,17 @@ func (p *player) sender() {
|
||||
}
|
||||
}
|
||||
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() {
|
||||
for {
|
||||
// XXX: need to mark myself as having received something, also binding
|
||||
// such action to a particular game turn ID
|
||||
var msg Instruction
|
||||
err := websocket.JSON.Receive(p.ws, &msg)
|
||||
var msgs map[string]Instruction
|
||||
err := websocket.JSON.Receive(p.ws, &msgs)
|
||||
|
||||
if err != nil {
|
||||
// TODO: perhaps we could be a bit more precise in the handling of
|
||||
// this 'error' by selecting on some kill signal channel and this
|
||||
@ -40,79 +42,92 @@ func (p *player) recv() {
|
||||
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 {
|
||||
p.Robot.TargetSpeed = 0
|
||||
p.Robot.FireAt = nil
|
||||
p.Robot.MoveTo = nil
|
||||
if p.Robot.RepairCounter <= 0 {
|
||||
p.Robot.RepairCounter = 3.0
|
||||
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 {
|
||||
p.Robot.TargetSpeed = 0
|
||||
p.Robot.FireAt = nil
|
||||
p.Robot.MoveTo = nil
|
||||
p.Robot.ActiveScan = true
|
||||
r.TargetSpeed = 0
|
||||
r.FireAt = nil
|
||||
r.MoveTo = nil
|
||||
r.ActiveScan = true
|
||||
} else {
|
||||
p.Robot.RepairCounter = 0
|
||||
p.Robot.ActiveScan = false
|
||||
r.RepairCounter = 0
|
||||
r.ActiveScan = false
|
||||
|
||||
// Reapiring halts all other activity
|
||||
if msg.MoveTo != nil {
|
||||
p.Robot.MoveTo = msg.MoveTo
|
||||
r.MoveTo = msg.MoveTo
|
||||
}
|
||||
if msg.FireAt != nil {
|
||||
p.Robot.FireAt = msg.FireAt
|
||||
r.FireAt = msg.FireAt
|
||||
}
|
||||
|
||||
if msg.TargetSpeed != nil {
|
||||
p.Robot.TargetSpeed = float32(*msg.TargetSpeed)
|
||||
r.TargetSpeed = float32(*msg.TargetSpeed)
|
||||
} else {
|
||||
p.Robot.TargetSpeed = p.Robot.Stats.Speed
|
||||
r.TargetSpeed = r.Stats.Speed
|
||||
}
|
||||
|
||||
if msg.Probe != nil {
|
||||
p.Robot.Probe = msg.Probe
|
||||
p.Robot.ProbeResult = nil
|
||||
r.Probe = msg.Probe
|
||||
r.ProbeResult = nil
|
||||
} else {
|
||||
p.Robot.Probe = nil
|
||||
r.Probe = nil
|
||||
}
|
||||
}
|
||||
|
||||
if msg.Message != nil {
|
||||
p.Robot.Message = *msg.Message
|
||||
r.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()
|
||||
}
|
||||
|
||||
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
|
||||
intersection_point := v.Point2d{X: 0, Y: 0}
|
||||
|
||||
// Check Walls
|
||||
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 {
|
||||
return collision, pos, nil
|
||||
}
|
||||
|
||||
// Check Other Bots
|
||||
for player := range g.players {
|
||||
if player.Robot.Id == p.Robot.Id {
|
||||
for _, bot := range player.Robots {
|
||||
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)
|
||||
collision, _, pos := v.RectIntersection(player_rect, r.Position, move_vector)
|
||||
if collision {
|
||||
return collision, pos, player
|
||||
return collision, pos, bot
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check 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 {
|
||||
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
|
||||
}
|
||||
|
||||
func (p *player) Tick(g *game) {
|
||||
p.Robot.Collision = false
|
||||
p.Robot.Hit = false
|
||||
p.scan(g)
|
||||
func (r *Robot) Tick(g *game) {
|
||||
r.Collision = false
|
||||
r.Hit = false
|
||||
r.scan(g)
|
||||
|
||||
// Adjust Speed
|
||||
if p.Robot.Speed < p.Robot.TargetSpeed {
|
||||
p.Robot.Speed += (p.Robot.Stats.Acceleration * delta)
|
||||
if p.Robot.Speed > p.Robot.TargetSpeed {
|
||||
p.Robot.Speed = p.Robot.TargetSpeed
|
||||
if r.Speed < r.TargetSpeed {
|
||||
r.Speed += (r.Stats.Acceleration * delta)
|
||||
if r.Speed > r.TargetSpeed {
|
||||
r.Speed = r.TargetSpeed
|
||||
}
|
||||
|
||||
} else if float32(math.Abs(float64(p.Robot.Speed-p.Robot.TargetSpeed))) > v.Epsilon {
|
||||
p.Robot.Speed -= (p.Robot.Stats.Acceleration * delta)
|
||||
} else if float32(math.Abs(float64(r.Speed-r.TargetSpeed))) > v.Epsilon {
|
||||
r.Speed -= (r.Stats.Acceleration * delta)
|
||||
} else {
|
||||
p.Robot.Speed = p.Robot.TargetSpeed
|
||||
r.Speed = r.TargetSpeed
|
||||
}
|
||||
|
||||
// Adjust Heading
|
||||
current_heading := p.Robot.Heading
|
||||
if current_heading.Mag() == 0 && p.Robot.MoveTo != nil {
|
||||
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 = p.Robot.MoveTo.Sub(p.Robot.Position).Normalize()
|
||||
current_heading = r.MoveTo.Sub(r.Position).Normalize()
|
||||
}
|
||||
|
||||
new_heading := current_heading
|
||||
if p.Robot.MoveTo != nil {
|
||||
if r.MoveTo != nil {
|
||||
// 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 {
|
||||
@ -162,35 +177,35 @@ func (p *player) Tick(g *game) {
|
||||
}
|
||||
|
||||
// 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
|
||||
// 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))
|
||||
}
|
||||
|
||||
move_vector := new_heading.Scale(p.Robot.Speed * delta)
|
||||
collision, intersection_point, hit_player := p.checkCollisions(g, move_vector)
|
||||
move_vector := new_heading.Scale(r.Speed * delta)
|
||||
collision, intersection_point, hit_robot := r.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
|
||||
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(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())
|
||||
|
||||
p.Robot.Position = p.Robot.Position.Add(move_dist)
|
||||
p.Robot.Health -= int(p.Robot.Speed / 10.0)
|
||||
p.Robot.MoveTo = &p.Robot.Position
|
||||
p.Robot.Speed = (p.Robot.Speed * 0.1)
|
||||
p.Robot.Heading = p.Robot.Heading.Scale(-1.0)
|
||||
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 {
|
||||
p.Robot.Position = p.Robot.Position.Add(move_vector)
|
||||
r.Position = r.Position.Add(move_vector)
|
||||
if new_heading.Mag() > 0 {
|
||||
p.Robot.Heading = new_heading
|
||||
r.Heading = new_heading
|
||||
} else {
|
||||
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
|
||||
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
|
||||
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(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 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 p.Robot.FireAt != nil {
|
||||
proj := p.fire(g.projectiles, g.turn)
|
||||
if r.FireAt != nil {
|
||||
proj := r.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 r.Probe != nil && r.ProbeResult == nil {
|
||||
probe_vector := r.Probe.Sub(r.Position)
|
||||
coll, pos, _ := r.checkCollisions(g, probe_vector)
|
||||
if coll {
|
||||
p.Robot.ProbeResult = &pos
|
||||
r.ProbeResult = &pos
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (p *player) scan(g *game) {
|
||||
p.Robot.Scanners = p.Robot.Scanners[:0]
|
||||
func (r *Robot) scan(g *game) {
|
||||
r.Scanners = r.Scanners[:0]
|
||||
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
|
||||
}
|
||||
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)
|
||||
if dist < float32(r.Stats.ScannerRadius+int(r.ScanCounter)) {
|
||||
s := Scanner{
|
||||
Id: player.Robot.Id,
|
||||
Id: bot.Id,
|
||||
Type: "robot",
|
||||
}
|
||||
p.Robot.Scanners = append(p.Robot.Scanners, s)
|
||||
r.Scanners = append(r.Scanners, s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for proj, _ := range g.projectiles {
|
||||
if proj.Owner == p {
|
||||
if proj.Owner == r {
|
||||
continue
|
||||
}
|
||||
|
||||
dist := v.Distance(proj.Position, p.Robot.Position)
|
||||
if dist < float32(p.Robot.Stats.ScannerRadius+int(p.Robot.ScanCounter)) {
|
||||
dist := v.Distance(proj.Position, r.Position)
|
||||
if dist < float32(r.Stats.ScannerRadius+int(r.ScanCounter)) {
|
||||
s := Scanner{
|
||||
Id: proj.Id,
|
||||
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
|
||||
time_since_fired := (float32(turn) * (delta * 1000)) - (float32(p.Robot.LastFired) * (delta * 1000))
|
||||
if time_since_fired < float32(p.Robot.Stats.FireRate) {
|
||||
time_since_fired := (float32(turn) * (delta * 1000)) - (float32(r.LastFired) * (delta * 1000))
|
||||
if time_since_fired < float32(r.Stats.FireRate) {
|
||||
return nil
|
||||
}
|
||||
|
||||
p.Robot.LastFired = turn
|
||||
r.LastFired = turn
|
||||
|
||||
return &Projectile{
|
||||
Id: idg.Hash(),
|
||||
Position: p.Robot.Position,
|
||||
MoveTo: *p.Robot.FireAt,
|
||||
Damage: p.Robot.Stats.WeaponDamage,
|
||||
Radius: p.Robot.Stats.WeaponRadius,
|
||||
Speed: p.Robot.Stats.WeaponSpeed,
|
||||
Owner: p,
|
||||
Position: r.Position,
|
||||
MoveTo: *r.FireAt,
|
||||
Damage: r.Stats.WeaponDamage,
|
||||
Radius: r.Stats.WeaponRadius,
|
||||
Speed: r.Stats.WeaponSpeed,
|
||||
Owner: r,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *player) reset(g *game) {
|
||||
func (r *Robot) reset(g *game) {
|
||||
for {
|
||||
start_pos := v.Point2d{
|
||||
X: rand.Float32() * float32(g.width),
|
||||
Y: rand.Float32() * float32(g.height),
|
||||
}
|
||||
p.Robot.MoveTo = &start_pos
|
||||
p.Robot.Position = start_pos
|
||||
p.Robot.Health = p.Robot.Stats.Hp
|
||||
r.MoveTo = &start_pos
|
||||
r.Position = start_pos
|
||||
r.Health = r.Stats.Hp
|
||||
|
||||
log.Printf("Reset %v", r)
|
||||
|
||||
// Check Obstacles
|
||||
retry := false
|
||||
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 {
|
||||
retry = true
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ type Projectile struct {
|
||||
Radius int `json:"-"`
|
||||
Speed float32 `json:"-"`
|
||||
Damage int `json:"-"`
|
||||
Owner *player `json:"-"`
|
||||
Owner *Robot `json:"-"`
|
||||
}
|
||||
|
||||
func (p *Projectile) Tick(g *game) {
|
||||
@ -23,21 +23,23 @@ func (p *Projectile) Tick(g *game) {
|
||||
|
||||
hit_player := false
|
||||
for player := range g.players {
|
||||
if player == p.Owner {
|
||||
for _, r := range player.Robots {
|
||||
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)
|
||||
if collision {
|
||||
hit_player = true
|
||||
|
||||
if player.Robot.Health > 0 {
|
||||
if r.Health > 0 {
|
||||
// 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
|
||||
player.Robot.Hit = true
|
||||
r.Health -= p.Damage
|
||||
r.Hit = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -73,11 +75,13 @@ func (p *Projectile) Tick(g *game) {
|
||||
g.splosions[splo] = true
|
||||
|
||||
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 player.Robot.Health > 0 {
|
||||
player.Robot.Health -= p.Damage
|
||||
player.Robot.Hit = true
|
||||
if r.Health > 0 {
|
||||
r.Health -= p.Damage
|
||||
r.Hit = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
53
protocol.go
53
protocol.go
@ -41,7 +41,28 @@ func (c *ClientID) Valid() (bool, string) {
|
||||
|
||||
type ClientConfig struct {
|
||||
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 {
|
||||
@ -152,38 +173,50 @@ func addPlayer(ws *websocket.Conn) {
|
||||
log.Printf("%s Waiting for client to send conf ...", player_id)
|
||||
err = websocket.JSON.Receive(ws, &conf)
|
||||
log.Printf("conf received: %+v", conf)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("%s %s config parse error", gid.Id, player_id)
|
||||
websocket.JSON.Send(ws, NewFailure("config parse error"))
|
||||
return
|
||||
}
|
||||
|
||||
// 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))
|
||||
break
|
||||
} else {
|
||||
log.Printf("Config is INVALID, abort")
|
||||
_ = websocket.JSON.Send(ws, NewHandshake(player_id, false))
|
||||
}
|
||||
}
|
||||
|
||||
p := &player{
|
||||
Robot: Robot{
|
||||
Stats: DeriveStats(conf.Stats),
|
||||
Id: player_id,
|
||||
Name: clientid.Name,
|
||||
Health: conf.Stats.Hp,
|
||||
Scanners: make([]Scanner, 0)},
|
||||
Robots: []*Robot{},
|
||||
send: make(chan *Boardstate),
|
||||
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
|
||||
defer func() {
|
||||
game.unregister <- p
|
||||
}()
|
||||
go p.sender()
|
||||
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":
|
||||
s := &Spectator{
|
||||
send: make(chan *Boardstate),
|
||||
|
12
robot.go
12
robot.go
@ -149,17 +149,6 @@ func DeriveStats(request StatsRequest) Stats {
|
||||
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 {
|
||||
Message *string `json:"message,omitempty"`
|
||||
MoveTo *v.Point2d `json:"move_to,omitempty"`
|
||||
@ -168,5 +157,4 @@ type Instruction struct {
|
||||
TargetSpeed *float32 `json:"target_speed,omitempty"`
|
||||
Repair *bool `json:"repair,omitempty"`
|
||||
Scan *bool `json:"scan,omitempty"`
|
||||
Stats Stats `json:"stats"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user