delta

package module
v0.0.0-...-f58a252 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2025 License: MIT Imports: 4 Imported by: 0

README

Delta - Game State Synchronization

Efficient delta compression for multiplayer games in Go. Only transmit what changed, not the entire state.

Installation

go get github.com/cbodonnell/delta
go install github.com/cbodonnell/delta/cmd/deltagen@latest

Quick Start

1. Define Your Game State
// delta:entity
type GameState struct {
    ID         int64   // Required
    PlayerX    float64
    PlayerY    float64
    Score      int32
    PlayerName string
    Inventory  []string
}
2. Generate Delta Code
deltagen -input .
3. Use Deltas
// Create states
state1 := &GameState{ID: 1, PlayerX: 10, Score: 100}
state2 := state1.Clone().(*GameState)
state2.PlayerX = 15  // Player moved
state2.Score = 120   // Score changed

// Create and serialize delta
delta := state1.Delta(state2)
var buf bytes.Buffer
delta.Serialize(&buf)

// Apply delta elsewhere
newDelta := &GameStateDelta{}
newDelta.Deserialize(&buf)
state1.ApplyDelta(newDelta)  // state1 now equals state2

Supported Types

  • Primitives: bool, int8-int64, uint8-uint64, float32, float64, and string
  • Collections: []T, map[K]V, and []byte, where K and V are supported primitive types

Network Usage

// Server: send only changes
delta := client.lastState.Delta(newState)
sendToClient(delta)
client.lastState = newState.Clone()

// Client: apply changes
delta := receiveDelta()
gameState.ApplyDelta(delta)

Requirements

  • Structs must have // delta:entity comment
  • Must include ID int64 field
  • Only exported fields are processed

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapsEqual

func MapsEqual[K, V comparable](a, b map[K]V) bool

Helper functions for map comparison

func SlicesEqual

func SlicesEqual[T comparable](a, b []T) bool

Helper functions for slice comparison

Types

type BinaryReader

type BinaryReader struct {
	// contains filtered or unexported fields
}

func NewBinaryReader

func NewBinaryReader(r io.Reader) *BinaryReader

func (*BinaryReader) ReadBool

func (br *BinaryReader) ReadBool() (bool, error)

func (*BinaryReader) ReadByte

func (br *BinaryReader) ReadByte() (byte, error)

func (*BinaryReader) ReadBytes

func (br *BinaryReader) ReadBytes() ([]byte, error)

func (*BinaryReader) ReadFloat32

func (br *BinaryReader) ReadFloat32() (float32, error)

func (*BinaryReader) ReadFloat64

func (br *BinaryReader) ReadFloat64() (float64, error)

func (*BinaryReader) ReadInt16

func (br *BinaryReader) ReadInt16() (int16, error)

func (*BinaryReader) ReadInt32

func (br *BinaryReader) ReadInt32() (int32, error)

func (*BinaryReader) ReadInt64

func (br *BinaryReader) ReadInt64() (int64, error)

func (*BinaryReader) ReadInt8

func (br *BinaryReader) ReadInt8() (int8, error)

func (*BinaryReader) ReadString

func (br *BinaryReader) ReadString() (string, error)

func (*BinaryReader) ReadUint16

func (br *BinaryReader) ReadUint16() (uint16, error)

func (*BinaryReader) ReadUint32

func (br *BinaryReader) ReadUint32() (uint32, error)

func (*BinaryReader) ReadUint64

func (br *BinaryReader) ReadUint64() (uint64, error)

func (*BinaryReader) ReadUint8

func (br *BinaryReader) ReadUint8() (uint8, error)

func (*BinaryReader) ReadVarUint32

func (br *BinaryReader) ReadVarUint32() (uint32, error)

type BinaryWriter

type BinaryWriter struct {
	// contains filtered or unexported fields
}

func NewBinaryWriter

func NewBinaryWriter(w io.Writer) *BinaryWriter

func (*BinaryWriter) WriteBool

func (bw *BinaryWriter) WriteBool(b bool) error

func (*BinaryWriter) WriteByte

func (bw *BinaryWriter) WriteByte(b byte) error

func (*BinaryWriter) WriteBytes

func (bw *BinaryWriter) WriteBytes(b []byte) error

func (*BinaryWriter) WriteFloat32

func (bw *BinaryWriter) WriteFloat32(v float32) error

func (*BinaryWriter) WriteFloat64

func (bw *BinaryWriter) WriteFloat64(v float64) error

func (*BinaryWriter) WriteInt16

func (bw *BinaryWriter) WriteInt16(v int16) error

func (*BinaryWriter) WriteInt32

func (bw *BinaryWriter) WriteInt32(v int32) error

func (*BinaryWriter) WriteInt64

func (bw *BinaryWriter) WriteInt64(v int64) error

func (*BinaryWriter) WriteInt8

func (bw *BinaryWriter) WriteInt8(v int8) error

func (*BinaryWriter) WriteString

func (bw *BinaryWriter) WriteString(s string) error

func (*BinaryWriter) WriteUint16

func (bw *BinaryWriter) WriteUint16(v uint16) error

func (*BinaryWriter) WriteUint32

func (bw *BinaryWriter) WriteUint32(v uint32) error

func (*BinaryWriter) WriteUint64

func (bw *BinaryWriter) WriteUint64(v uint64) error

func (*BinaryWriter) WriteUint8

func (bw *BinaryWriter) WriteUint8(v uint8) error

func (*BinaryWriter) WriteVarUint32

func (bw *BinaryWriter) WriteVarUint32(v uint32) error

Variable-length encoding for better compression

type Delta

type Delta interface {
	ApplyTo(e Entity)
	Serialize(w io.Writer) error
	Deserialize(r io.Reader) error
}

type Entity

type Entity interface {
	GetID() int64
	Clone() Entity
	Delta(other Entity) Delta
	ApplyDelta(d Delta)
}

Directories

Path Synopsis
cmd
deltagen command
Code generated by deltagen.
Code generated by deltagen.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL