Skip to content

Commit

Permalink
trying to improve play on my vps
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecoffman committed Feb 27, 2018
1 parent f4ecb3d commit 5345bf4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 19 deletions.
11 changes: 7 additions & 4 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ func (g *Game) NewBox(id BoxID) *Box {
return g.Boxes[id]
}

var boxSequence uint64

func (b *Box) Location() pkt.BoxLocation {
return pkt.BoxLocation{
ID: b.ID,
X: float32(b.Body.Position().X),
Y: float32(b.Body.Position().Y),
Angle: float32(b.Body.Angle()),
ID: b.ID,
Sequence: boxSequence,
X: float32(b.Body.Position().X),
Y: float32(b.Body.Position().Y),
Angle: float32(b.Body.Angle()),
}
}
15 changes: 15 additions & 0 deletions client/handlersc.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,21 @@ func join(packet tanklets.Packet, game *tanklets.Game, network *Client) {
game.Tanks[tank.ID] = tank
}

var lastLocationSeq uint64

func location(packet tanklets.Packet, game *tanklets.Game, network *Client) {
l := pkt.Location{}
if _, err := l.Serialize(packet.Bytes); err != nil {
log.Println(err)
return
}

if l.Sequence < lastLocationSeq {
fmt.Println("out of order packet")
return
}
lastLocationSeq = l.Sequence

player := game.Tanks[tanklets.PlayerID(l.ID)]
if player == nil {
log.Println("Client", Me, "-- Tank with ID", l.ID, "not found")
Expand Down Expand Up @@ -112,13 +120,20 @@ func location(packet tanklets.Packet, game *tanklets.Game, network *Client) {
player.Turret.Body.SetAngle(float64(l.Turret))
}

var lastBoxSeq uint64

func boxlocation(packet tanklets.Packet, game *tanklets.Game, network *Client) {
l := pkt.BoxLocation{}
if _, err := l.Serialize(packet.Bytes); err != nil {
log.Println(err)
return
}

if l.Sequence < lastBoxSeq {
return
}
lastBoxSeq = l.Sequence

box := game.Boxes[tanklets.BoxID(l.ID)]
if box == nil {
box = game.NewBox(tanklets.BoxID(l.ID))
Expand Down
8 changes: 5 additions & 3 deletions pkt/boxlocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (

// message sent to clients: update location information
type BoxLocation struct {
ID BoxID
X, Y float32
Angle float32
ID BoxID
Sequence uint64
X, Y float32
Angle float32
}

func (l BoxLocation) MarshalBinary() ([]byte, error) {
Expand All @@ -25,6 +26,7 @@ func (l *BoxLocation) Serialize(b []byte) ([]byte, error) {
var m uint8 = PacketBoxLocation
stream.Uint8(&m)
stream.Uint16((*uint16)(&l.ID))
stream.Uint64(&l.Sequence)
stream.Float32(&l.X)
stream.Float32(&l.Y)
stream.Float32(&l.Angle)
Expand Down
11 changes: 7 additions & 4 deletions pkt/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (

// message sent to clients: update location information
type Location struct {
ID PlayerID
X, Y float32
Vx, Vy float32
Angle, AngularVelocity float32
ID PlayerID
Sequence uint64
X, Y float32
Vx, Vy float32
Angle float32
AngularVelocity float32

Turret float32
}
Expand All @@ -28,6 +30,7 @@ func (l *Location) Serialize(b []byte) ([]byte, error) {
var m uint8 = PacketLocation
stream.Uint8(&m)
stream.Uint16((*uint16)(&l.ID))
stream.Uint64(&l.Sequence)
stream.Float32(&l.X)
stream.Float32(&l.Y)
stream.Float32(&l.Vx)
Expand Down
12 changes: 8 additions & 4 deletions server/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func Loop(network *Server) {
pingTick := time.Tick(1*time.Second)
pingTick := time.Tick(1 * time.Second)
go func() {
for range pingTick {
ping := pkt.Ping{T: time.Now()}
Expand Down Expand Up @@ -47,7 +47,7 @@ func Loop(network *Server) {

fmt.Println("Let's do this")
// This seems hacky but it works
time.Sleep(3*time.Second)
time.Sleep(3 * time.Second)
game.State = tanklets.StatePlaying
Players.SendAll(game.Network, pkt.State{State: tanklets.StatePlaying})

Expand All @@ -57,16 +57,19 @@ func Loop(network *Server) {
var BoxLocations = map[tanklets.BoxID]pkt.BoxLocation{}

const (
serverUpdates = time.Second / 21.0
playerUpdates = time.Second / 21.0
boxUpdates = time.Second / 5
physicsTicks = 180.0
physicsTickrate = 1.0 / physicsTicks
)

func Play(game *Game) {
physicsTick := time.NewTicker(time.Second / physicsTicks)
updateTick := time.NewTicker(serverUpdates)
updateTick := time.NewTicker(playerUpdates)
boxTick := time.NewTicker(boxUpdates)
defer physicsTick.Stop()
defer updateTick.Stop()
defer boxTick.Stop()

var accumulator float64
var dt time.Duration
Expand Down Expand Up @@ -106,6 +109,7 @@ func Play(game *Game) {
for _, tank := range game.Tanks {
Players.SendAll(game.Network, tank.Location())
}
case <-boxTick.C:
for _, box := range game.Boxes {
loc := box.Location()
if loc != BoxLocations[box.ID] {
Expand Down
12 changes: 8 additions & 4 deletions tank.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ type Tank struct {

width, height float64
Color mgl32.Vec3
Name string
Score int
Name string
Score int

LastShot time.Time

Expand All @@ -58,7 +58,7 @@ type Turret struct {
func (g *Game) NewTank(id PlayerID, color mgl32.Vec3) *Tank {
tank := &Tank{
ID: id,
Name: fmt.Sprintf("Player %v", id),
Name: fmt.Sprintf("Player %v", id),
Color: color,
}
tank.Body = g.Space.AddBody(cp.NewBody(1, cp.MomentForBox(1, TankWidth, TankHeight)))
Expand Down Expand Up @@ -95,7 +95,7 @@ func (tank *Tank) Update(dt float64) {
func (tank *Tank) FixedUpdate(dt float64) {
if tank.Destroyed {
// slowly stop, looks cool
tank.ControlBody.SetAngularVelocity(tank.ControlBody.AngularVelocity()*.99)
tank.ControlBody.SetAngularVelocity(tank.ControlBody.AngularVelocity() * .99)
tank.ControlBody.SetVelocityVector(tank.ControlBody.Velocity().Mult(.99))
return
}
Expand All @@ -118,10 +118,14 @@ func (tank *Tank) FixedUpdate(dt float64) {
tank.LastMove = tank.NextMove
}

var locationSequence uint64

// gather important data to transmit
func (tank *Tank) Location() *pkt.Location {
locationSequence++
return &pkt.Location{
ID: tank.ID,
Sequence: locationSequence,
X: float32(tank.Body.Position().X),
Y: float32(tank.Body.Position().Y),
Angle: float32(tank.Body.Angle()),
Expand Down

0 comments on commit 5345bf4

Please sign in to comment.