server/player.go

111 lines
2.6 KiB
Go
Raw Normal View History

2013-08-19 20:43:26 -07:00
package main
import (
2013-09-03 23:26:40 -07:00
"bitbucket.org/hackerbots/bot"
2013-08-19 20:43:26 -07:00
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/rand"
)
type player struct {
ws *websocket.Conn
2013-09-03 23:26:40 -07:00
Robot bot.Robot
send chan *bot.Boardstate
Instruction bot.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
var msg bot.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
}
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
}
if msg.Stats.Speed > 0 {
p.Robot.Stats = msg.Stats
p.Robot.Health = p.Robot.Stats.Hp
}
}
log.Printf("player %s: recv close", p.Robot.Id)
2013-08-19 20:43:26 -07:00
p.ws.Close()
}
func (p *player) nudge() {
// XXX: we must take into account going past our p.Robot.MoveTo ...
2013-09-04 00:07:22 -07:00
newPos := v.Move(p.Robot.Position, *p.Robot.MoveTo, p.Robot.Stats.Speed, delta)
2013-08-19 20:43:26 -07:00
p.Robot.Position.X = newPos.X
p.Robot.Position.Y = newPos.Y
}
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-03 23:26:40 -07:00
s := bot.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-04 00:07:47 -07:00
func (p *player) fire(projectiles map[*bot.Projectile]bool) *bot.Projectile {
// 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-03 23:26:40 -07:00
return &bot.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
}