2013-08-19 20:43:26 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
v "bitbucket.org/hackerbots/vector"
|
2013-08-19 21:42:43 -07:00
|
|
|
"code.google.com/p/go.net/websocket"
|
2013-08-19 20:43:26 -07:00
|
|
|
"log"
|
2013-09-27 01:30:07 -07:00
|
|
|
"math"
|
2013-08-19 20:43:26 -07:00
|
|
|
"math/rand"
|
|
|
|
)
|
|
|
|
|
|
|
|
type player struct {
|
|
|
|
ws *websocket.Conn
|
2013-09-27 22:27:05 -07:00
|
|
|
Robot Robot
|
|
|
|
send chan *Boardstate
|
|
|
|
Instruction Instruction
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) sender() {
|
|
|
|
for things := range p.send {
|
|
|
|
err := websocket.JSON.Send(p.ws, *things)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.ws.Close()
|
2013-09-04 23:23:05 -07:00
|
|
|
log.Printf("player %s: sender close", p.Robot.Id)
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) recv() {
|
|
|
|
for {
|
2013-09-08 09:32:24 -07:00
|
|
|
// XXX: need to mark myself as having received something, also binding
|
|
|
|
// such action to a particular game turn ID
|
2013-09-27 22:27:05 -07:00
|
|
|
var msg Instruction
|
2013-08-19 20:43:26 -07:00
|
|
|
err := websocket.JSON.Receive(p.ws, &msg)
|
|
|
|
if err != nil {
|
2013-09-08 09:32:24 -07:00
|
|
|
// TODO: perhaps we could be a bit more precise in the handling of
|
|
|
|
// this 'error' by selecting on some kill signal channel and this
|
|
|
|
// json read?
|
|
|
|
log.Print("problem receiving JSON from player: ", err)
|
2013-08-19 20:43:26 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if msg.MoveTo != nil {
|
2013-09-04 00:05:38 -07:00
|
|
|
p.Robot.MoveTo = msg.MoveTo
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
if msg.FireAt != nil {
|
2013-09-04 00:05:38 -07:00
|
|
|
p.Robot.FireAt = msg.FireAt
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
2013-09-27 01:30:07 -07:00
|
|
|
|
|
|
|
p.Robot.TargetSpeed = p.Robot.Stats.Speed
|
|
|
|
|
2013-08-19 20:43:26 -07:00
|
|
|
if msg.Stats.Speed > 0 {
|
|
|
|
p.Robot.Stats = msg.Stats
|
|
|
|
p.Robot.Health = p.Robot.Stats.Hp
|
2013-09-27 01:30:07 -07:00
|
|
|
p.Robot.Speed = 0
|
|
|
|
p.Robot.TargetSpeed = p.Robot.Stats.Speed
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
}
|
2013-09-04 23:23:05 -07:00
|
|
|
log.Printf("player %s: recv close", p.Robot.Id)
|
2013-08-19 20:43:26 -07:00
|
|
|
p.ws.Close()
|
|
|
|
}
|
|
|
|
|
2013-09-28 12:59:04 -07:00
|
|
|
func (p *player) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point2d) {
|
2013-09-27 01:30:07 -07:00
|
|
|
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)
|
|
|
|
if collision {
|
|
|
|
return collision, pos
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check Other Bots
|
|
|
|
|
|
|
|
// Check Terrain
|
|
|
|
// TBD
|
|
|
|
|
|
|
|
return collision, intersection_point
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) nudge(g *game) {
|
|
|
|
|
|
|
|
// Adjust Speed
|
|
|
|
if p.Robot.Speed < p.Robot.TargetSpeed {
|
|
|
|
p.Robot.Speed += (p.Robot.Stats.Acceleration * delta)
|
|
|
|
} else if (p.Robot.Speed - p.Robot.TargetSpeed) > v.Epsilon {
|
|
|
|
p.Robot.Speed -= (p.Robot.Stats.Acceleration * delta)
|
|
|
|
} else {
|
|
|
|
p.Robot.Speed = p.Robot.TargetSpeed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust Heading
|
|
|
|
current_heading := p.Robot.Heading
|
|
|
|
if current_heading.Mag() == 0 {
|
|
|
|
// We may have been stopped before this and had no heading
|
|
|
|
current_heading = p.Robot.MoveTo.Sub(p.Robot.Position).Normalize()
|
|
|
|
}
|
2013-10-18 20:40:09 -07:00
|
|
|
|
|
|
|
// Where do we WANT to be heading?
|
2013-09-27 01:30:07 -07:00
|
|
|
new_heading := p.Robot.MoveTo.Sub(p.Robot.Position).Normalize()
|
2013-10-18 20:40:09 -07:00
|
|
|
|
2013-09-27 01:30:07 -07:00
|
|
|
// 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
|
|
|
|
}
|
2013-10-18 20:40:09 -07:00
|
|
|
|
|
|
|
// Max turn radius in this case is 100 degrees per second
|
2013-09-27 01:30:07 -07:00
|
|
|
if math.Abs(angle) > (100 * delta) {
|
|
|
|
// New heading should be a little less, take current heading and
|
|
|
|
// rotate by the max turn radius per frame.
|
|
|
|
rot := (100 * delta) * v.Deg2rad
|
|
|
|
|
|
|
|
new_heading = current_heading.Rotate(rot * dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
move_vector := new_heading.Scale(p.Robot.Speed * delta)
|
2013-09-28 12:59:04 -07:00
|
|
|
collision, _ := p.checkCollisions(g, move_vector)
|
2013-09-27 01:30:07 -07:00
|
|
|
if collision {
|
|
|
|
// p.Robot.Position = intersection_point
|
|
|
|
p.Robot.Speed = 0
|
|
|
|
p.Robot.MoveTo = &p.Robot.Position
|
|
|
|
p.Robot.Health -= 5
|
|
|
|
p.Robot.Heading = v.Vector2d{X: 0, Y: 0}
|
|
|
|
} else {
|
|
|
|
p.Robot.Position = p.Robot.Position.Add(move_vector)
|
2013-10-18 20:40:09 -07:00
|
|
|
if new_heading.Mag() > 0 {
|
|
|
|
p.Robot.Heading = new_heading
|
|
|
|
} else {
|
|
|
|
log.Printf("Zero Heading %v", new_heading)
|
|
|
|
}
|
2013-09-27 01:30:07 -07:00
|
|
|
}
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
|
2013-09-04 00:07:47 -07:00
|
|
|
func (p *player) scan(players map[*player]bool) {
|
2013-09-07 19:13:12 -07:00
|
|
|
p.Robot.Scanners = p.Robot.Scanners[:0]
|
2013-09-04 00:07:47 -07:00
|
|
|
for player, _ := range players {
|
2013-08-19 20:43:26 -07:00
|
|
|
if player.Robot.Id == p.Robot.Id || player.Robot.Health <= 0 {
|
|
|
|
continue
|
|
|
|
}
|
2013-09-03 23:35:42 -07:00
|
|
|
dist := v.Distance(player.Robot.Position, p.Robot.Position)
|
2013-08-19 20:43:26 -07:00
|
|
|
if dist < float64(p.Robot.Stats.ScannerRadius) {
|
2013-09-27 22:27:05 -07:00
|
|
|
s := Scanner{
|
2013-08-19 20:43:26 -07:00
|
|
|
Position: v.Point2d{
|
|
|
|
X: player.Robot.Position.X,
|
|
|
|
Y: player.Robot.Position.Y,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p.Robot.Scanners = append(p.Robot.Scanners, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 22:27:05 -07:00
|
|
|
func (p *player) fire(projectiles map[*Projectile]bool) *Projectile {
|
2013-09-04 00:07:47 -07:00
|
|
|
// XXX: is this to prevent us from having multiple projectiles from the
|
|
|
|
// same bot?
|
|
|
|
for proj := range projectiles {
|
2013-08-19 20:43:26 -07:00
|
|
|
if proj.Id == p.Robot.Id {
|
2013-09-03 23:26:40 -07:00
|
|
|
return nil
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 22:27:05 -07:00
|
|
|
return &Projectile{
|
2013-08-19 20:43:26 -07:00
|
|
|
Id: p.Robot.Id,
|
|
|
|
Position: p.Robot.Position,
|
2013-09-04 00:05:38 -07:00
|
|
|
MoveTo: *p.Robot.FireAt,
|
2013-08-19 20:43:26 -07:00
|
|
|
Damage: 10,
|
|
|
|
Radius: p.Robot.Stats.WeaponRadius,
|
|
|
|
Speed: float64(p.Robot.Stats.Speed * 2),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) reset() {
|
|
|
|
start_pos := v.Point2d{
|
2013-08-19 23:25:08 -07:00
|
|
|
X: rand.Float64() * *width,
|
|
|
|
Y: rand.Float64() * *height,
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
2013-09-04 00:05:38 -07:00
|
|
|
p.Robot.MoveTo = &start_pos
|
2013-08-19 20:43:26 -07:00
|
|
|
p.Robot.Position = start_pos
|
|
|
|
p.Robot.Health = p.Robot.Stats.Hp
|
|
|
|
}
|