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-10-21 07:49:56 -07:00
|
|
|
func (p *player) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point2d, *player) {
|
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 {
|
2013-10-21 07:49:56 -07:00
|
|
|
return collision, pos, nil
|
2013-09-27 01:30:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check Other Bots
|
2013-10-21 09:28:50 -07:00
|
|
|
for player := range g.players {
|
|
|
|
if player.Robot.Id == p.Robot.Id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
player_rect := v.RectFromPoint(player.Robot.Position, 3)
|
|
|
|
collision, _, pos := v.RectIntersection(player_rect, p.Robot.Position, move_vector)
|
|
|
|
if collision {
|
|
|
|
log.Printf("Player Collision %v hit %v, rect:%v", p.Robot.Position, move_vector, player_rect)
|
|
|
|
return collision, pos, player
|
|
|
|
}
|
|
|
|
}
|
2013-10-21 07:49:56 -07:00
|
|
|
|
2013-10-25 22:30:15 -07:00
|
|
|
// Check Obstacles
|
|
|
|
for _, obj := range g.obstacles {
|
|
|
|
collision, _, pos := v.RectIntersection(obj.Bounds, p.Robot.Position, move_vector)
|
|
|
|
if collision {
|
|
|
|
log.Printf("Object Collision %v hit %v, rect:%v", p.Robot.Position, move_vector, obj.Bounds)
|
|
|
|
return collision, pos, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-21 07:49:56 -07:00
|
|
|
return collision, intersection_point, nil
|
2013-09-27 01:30:07 -07:00
|
|
|
}
|
|
|
|
|
2013-10-20 21:15:23 -07:00
|
|
|
func (p *player) Tick(g *game) {
|
2013-10-25 22:30:15 -07:00
|
|
|
p.Robot.Collision = false
|
2013-10-25 22:51:41 -07:00
|
|
|
p.scan(g)
|
2013-09-27 01:30:07 -07:00
|
|
|
|
|
|
|
// 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-10-18 23:22:35 -07:00
|
|
|
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
|
2013-10-18 20:40:09 -07:00
|
|
|
|
2013-10-18 23:22:35 -07:00
|
|
|
dir := 1.0
|
|
|
|
if angle < 0 {
|
|
|
|
dir = -1.0
|
|
|
|
}
|
2013-09-27 01:30:07 -07:00
|
|
|
|
2013-10-18 23:22:35 -07:00
|
|
|
// Max turn radius in this case is in degrees per second
|
2013-10-19 00:00:45 -07:00
|
|
|
if float32(math.Abs(float64(angle))) > (float32(p.Robot.Stats.TurnSpeed) * delta) {
|
2013-10-18 23:22:35 -07:00
|
|
|
// New heading should be a little less, take current heading and
|
|
|
|
// rotate by the max turn radius per frame.
|
2013-10-19 00:00:45 -07:00
|
|
|
rot := (float32(p.Robot.Stats.TurnSpeed) * delta) * v.Deg2rad
|
2013-09-27 01:30:07 -07:00
|
|
|
|
2013-10-19 00:00:45 -07:00
|
|
|
new_heading = current_heading.Rotate(rot * float32(dir))
|
2013-10-18 23:22:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
move_vector := new_heading.Scale(p.Robot.Speed * delta)
|
2013-10-28 21:52:06 -07:00
|
|
|
collision, intersection_point, hit_player := p.checkCollisions(g, move_vector)
|
2013-10-18 23:22:35 -07:00
|
|
|
if collision {
|
2013-10-25 22:30:15 -07:00
|
|
|
p.Robot.Collision = true
|
2013-10-21 07:49:56 -07:00
|
|
|
if hit_player != nil {
|
|
|
|
hit_player.Robot.Health -= int(p.Robot.Speed / 10.0)
|
2013-10-25 22:30:15 -07:00
|
|
|
hit_player.Robot.Speed = (hit_player.Robot.Speed * 0.1)
|
2013-10-21 09:28:50 -07:00
|
|
|
hit_player.Robot.Heading = p.Robot.Heading
|
2013-10-21 07:49:56 -07:00
|
|
|
}
|
2013-11-05 23:21:41 -08:00
|
|
|
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())
|
2013-10-28 21:52:06 -07:00
|
|
|
|
2013-11-05 23:21:41 -08:00
|
|
|
p.Robot.Position = p.Robot.Position.Add(move_dist)
|
2013-10-21 09:28:50 -07:00
|
|
|
p.Robot.Health -= int(p.Robot.Speed / 10.0)
|
|
|
|
p.Robot.MoveTo = &p.Robot.Position
|
2013-10-25 22:30:15 -07:00
|
|
|
p.Robot.Speed = (p.Robot.Speed * 0.1)
|
2013-10-21 09:28:50 -07:00
|
|
|
p.Robot.Heading = p.Robot.Heading.Scale(-1.0)
|
2013-10-18 20:40:09 -07:00
|
|
|
} else {
|
2013-10-18 23:22:35 -07:00
|
|
|
p.Robot.Position = p.Robot.Position.Add(move_vector)
|
|
|
|
if new_heading.Mag() > 0 {
|
|
|
|
p.Robot.Heading = new_heading
|
|
|
|
// log.Printf("%v %v %v", new_heading, current_heading, angle)
|
|
|
|
} else {
|
|
|
|
log.Printf("Zero Heading %v", new_heading)
|
|
|
|
}
|
2013-10-18 20:40:09 -07:00
|
|
|
}
|
2013-09-27 01:30:07 -07:00
|
|
|
}
|
2013-10-18 23:22:35 -07:00
|
|
|
|
2013-10-20 21:15:23 -07:00
|
|
|
if p.Robot.FireAt != nil {
|
|
|
|
proj := p.fire(g.projectiles, g.turn)
|
|
|
|
if proj != nil {
|
|
|
|
g.projectiles[proj] = true
|
|
|
|
}
|
|
|
|
}
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
|
2013-10-25 22:51:41 -07:00
|
|
|
func (p *player) scan(g *game) {
|
2013-09-07 19:13:12 -07:00
|
|
|
p.Robot.Scanners = p.Robot.Scanners[:0]
|
2013-10-25 22:51:41 -07:00
|
|
|
for player, _ := range g.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-10-19 00:00:45 -07:00
|
|
|
if dist < float32(p.Robot.Stats.ScannerRadius) {
|
2013-09-27 22:27:05 -07:00
|
|
|
s := Scanner{
|
2013-11-06 20:09:45 -08:00
|
|
|
Id: player.Robot.Id,
|
2013-10-25 22:51:41 -07:00
|
|
|
Type: "robot",
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
p.Robot.Scanners = append(p.Robot.Scanners, s)
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 22:51:41 -07:00
|
|
|
|
|
|
|
for proj, _ := range g.projectiles {
|
|
|
|
if proj.Owner.Robot.Id == p.Robot.Id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dist := v.Distance(proj.Position, p.Robot.Position)
|
|
|
|
if dist < float32(p.Robot.Stats.ScannerRadius) {
|
|
|
|
s := Scanner{
|
2013-11-06 20:09:45 -08:00
|
|
|
Id: proj.Id,
|
2013-10-25 22:51:41 -07:00
|
|
|
Type: "projectile",
|
|
|
|
}
|
|
|
|
p.Robot.Scanners = append(p.Robot.Scanners, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
|
2013-10-18 23:22:35 -07:00
|
|
|
func (p *player) fire(projectiles map[*Projectile]bool, turn int) *Projectile {
|
|
|
|
// Throttle the fire rate
|
2013-10-19 00:00:45 -07:00
|
|
|
time_since_fired := (float32(turn) * (delta * 1000)) - (float32(p.Robot.LastFired) * (delta * 1000))
|
|
|
|
if time_since_fired < float32(p.Robot.Stats.FireRate) {
|
2013-10-18 23:22:35 -07:00
|
|
|
return nil
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
|
2013-10-18 23:22:35 -07:00
|
|
|
p.Robot.LastFired = turn
|
|
|
|
|
2013-09-27 22:27:05 -07:00
|
|
|
return &Projectile{
|
2013-11-06 20:32:56 -08:00
|
|
|
Id: idg.Hash(),
|
2013-08-19 20:43:26 -07:00
|
|
|
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,
|
2013-10-19 00:00:45 -07:00
|
|
|
Speed: float32(p.Robot.Stats.Speed * 3),
|
2013-10-25 22:51:41 -07:00
|
|
|
Owner: p,
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-25 22:30:15 -07:00
|
|
|
func (p *player) reset(g *game) {
|
|
|
|
for {
|
|
|
|
start_pos := v.Point2d{
|
|
|
|
X: rand.Float32() * float32(*width),
|
|
|
|
Y: rand.Float32() * float32(*height),
|
|
|
|
}
|
|
|
|
p.Robot.MoveTo = &start_pos
|
|
|
|
p.Robot.Position = start_pos
|
|
|
|
p.Robot.Health = p.Robot.Stats.Hp
|
|
|
|
|
|
|
|
// Check Obstacles
|
|
|
|
retry := false
|
|
|
|
for _, obj := range g.obstacles {
|
|
|
|
_, inside, _ := v.RectIntersection(obj.Bounds, p.Robot.Position, v.Vector2d{0, 0})
|
|
|
|
if inside {
|
|
|
|
retry = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !retry {
|
|
|
|
break
|
|
|
|
}
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
}
|