server/player.go

312 lines
8.0 KiB
Go
Raw Normal View History

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"
"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()
log.Printf("player %s: sender close", p.Robot.Id)
2013-08-19 20:43:26 -07:00
}
func (p *player) recv() {
for {
// 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 {
// 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
}
2013-11-07 21:34:54 -08:00
if msg.Repair != nil && *msg.Repair == true {
2013-11-06 22:50:59 -08:00
p.Robot.TargetSpeed = 0
p.Robot.FireAt = nil
p.Robot.MoveTo = nil
if p.Robot.RepairCounter <= 0 {
p.Robot.RepairCounter = 3.0
}
2013-11-07 21:34:54 -08:00
} 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 {
2013-11-06 22:50:59 -08:00
p.Robot.RepairCounter = 0
2013-11-07 21:34:54 -08:00
p.Robot.ActiveScan = false
2013-11-06 22:50:59 -08:00
// 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
}
2013-11-06 23:26:29 -08:00
if msg.Probe != nil {
p.Robot.Probe = msg.Probe
2013-11-07 08:41:39 -08:00
p.Robot.ProbeResult = nil
2013-11-06 23:26:29 -08:00
} else {
p.Robot.Probe = nil
}
2013-08-19 20:43:26 -07:00
}
2013-11-06 22:50:59 -08:00
2013-11-07 21:34:54 -08:00
if msg.Message != nil {
p.Robot.Message = *msg.Message
}
2013-08-19 20:43:26 -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) {
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
}
// 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 {
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 {
return collision, pos, nil
}
}
2013-10-21 07:49:56 -07:00
return collision, intersection_point, nil
}
func (p *player) Tick(g *game) {
2013-10-25 22:30:15 -07:00
p.Robot.Collision = false
2013-11-06 23:06:51 -08:00
p.Robot.Hit = false
2013-10-25 22:51:41 -07:00
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
2013-11-07 21:34:54 -08:00
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()
}
2013-10-18 20:40:09 -07:00
2013-11-06 22:50:59 -08:00
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()
}
2013-10-18 20:40:09 -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
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)
2013-10-28 21:52:06 -07:00
collision, intersection_point, hit_player := p.checkCollisions(g, move_vector)
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
}
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
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 {
p.Robot.Position = p.Robot.Position.Add(move_vector)
if new_heading.Mag() > 0 {
p.Robot.Heading = new_heading
} else {
log.Printf("Zero Heading %v", new_heading)
}
2013-10-18 20:40:09 -07:00
}
}
2013-11-07 20:50:17 -08:00
// We only self repair when we're stopped
2013-11-06 22:50:59 -08:00
if math.Abs(float64(p.Robot.Speed)) < v.Epsilon && p.Robot.RepairCounter > 0 {
p.Robot.RepairCounter -= delta
if p.Robot.RepairCounter < 0 {
2013-11-07 21:00:10 -08:00
p.Robot.Health += g.repair_hp
p.Robot.RepairCounter = g.repair_rate
2013-11-06 22:50:59 -08:00
}
}
2013-11-07 21:34:54 -08:00
// 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
}
}
2013-11-06 23:26:29 -08:00
2013-11-07 08:41:39 -08:00
if p.Robot.Probe != nil && p.Robot.ProbeResult == nil {
2013-11-06 23:26:29 -08:00
probe_vector := p.Robot.Probe.Sub(p.Robot.Position)
coll, pos, _ := p.checkCollisions(g, probe_vector)
if coll {
2013-11-07 08:41:39 -08:00
p.Robot.ProbeResult = &pos
2013-11-06 23:26:29 -08:00
}
}
2013-11-07 21:34:54 -08:00
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-11-07 21:34:54 -08:00
if dist < float32(p.Robot.Stats.ScannerRadius+int(p.Robot.ScanCounter)) {
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 {
2013-11-07 20:50:17 -08:00
if proj.Owner == p {
2013-10-25 22:51:41 -07:00
continue
}
dist := v.Distance(proj.Position, p.Robot.Position)
2013-11-07 21:34:54 -08:00
if dist < float32(p.Robot.Stats.ScannerRadius+int(p.Robot.ScanCounter)) {
2013-10-25 22:51:41 -07:00
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
}
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
2013-08-19 20:43:26 -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-11-06 21:07:31 -08:00
Damage: p.Robot.Stats.WeaponDamage,
2013-08-19 20:43:26 -07:00
Radius: p.Robot.Stats.WeaponRadius,
2013-11-06 21:07:31 -08:00
Speed: p.Robot.Stats.WeaponSpeed,
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{
2013-11-07 22:05:38 -08:00
X: rand.Float32() * float32(g.width),
Y: rand.Float32() * float32(g.height),
2013-10-25 22:30:15 -07:00
}
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
}
}