server/player.go

315 lines
8.2 KiB
Go

package main
import (
v "bitbucket.org/hackerbots/vector"
"code.google.com/p/go.net/websocket"
"log"
"math"
"math/rand"
)
type player struct {
ws *websocket.Conn
Robot Robot
send chan *Boardstate
Instruction Instruction
}
func (p *player) sender() {
for things := range p.send {
err := websocket.JSON.Send(p.ws, *things)
if err != nil {
break
}
}
p.ws.Close()
log.Printf("player %s: sender close", p.Robot.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)
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
// json read?
log.Print("problem receiving JSON from player: ", err)
break
}
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
}
} 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
if msg.MoveTo != nil {
p.Robot.MoveTo = msg.MoveTo
}
if msg.FireAt != nil {
p.Robot.FireAt = msg.FireAt
}
if msg.TargetSpeed != nil {
p.Robot.TargetSpeed = float32(*msg.TargetSpeed)
} else {
p.Robot.TargetSpeed = p.Robot.Stats.Speed
}
if msg.Probe != nil {
p.Robot.Probe = msg.Probe
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)
p.ws.Close()
}
func (p *player) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point2d, *player) {
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, nil
}
// Check Other Bots
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
}
}
// 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
}
}
return collision, intersection_point, nil
}
func (p *player) Tick(g *game) {
p.Robot.Collision = false
p.Robot.Hit = false
p.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
}
} else if float32(math.Abs(float64(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 && 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)
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)
} else {
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)
}
}
}
// 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) {
p.Robot.Scanners = p.Robot.Scanners[:0]
for player, _ := range g.players {
if player.Robot.Id == p.Robot.Id || player.Robot.Health <= 0 {
continue
}
dist := v.Distance(player.Robot.Position, p.Robot.Position)
if dist < float32(p.Robot.Stats.ScannerRadius+int(p.Robot.ScanCounter)) {
s := Scanner{
Id: player.Robot.Id,
Type: "robot",
}
p.Robot.Scanners = append(p.Robot.Scanners, s)
}
}
for proj, _ := range g.projectiles {
if proj.Owner == p {
continue
}
dist := v.Distance(proj.Position, p.Robot.Position)
if dist < float32(p.Robot.Stats.ScannerRadius+int(p.Robot.ScanCounter)) {
s := Scanner{
Id: proj.Id,
Type: "projectile",
}
p.Robot.Scanners = append(p.Robot.Scanners, s)
}
}
}
func (p *player) 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) {
return nil
}
p.Robot.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,
}
}
func (p *player) 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
// 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
}
}
}