Skip to main content

Overview

The Game State API provides the core data structures for managing the GRPG server state, including players, NPCs, objects, and world data.

Game Struct

The Game struct is the central state container for the server.
type Game struct {
    Players      map[*Player]struct{}
    Connections  map[net.Conn]*Player
    MaxX         uint32
    MaxY         uint32
    Database     *sql.DB
    TrackedObjs  map[util.Vector2I]*GameObj
    TrackedNpcs  map[util.Vector2I]*GameNpc
    Mu           sync.RWMutex
    CollisionMap map[util.Vector2I]struct{}
    Objs         map[util.Vector2I]struct{}
    TimedScripts map[uint32][]func()
    NpcMoves     map[util.Vector2I][][]NpcMove
    CurrentTick  uint32
}

Fields

Players
map[*Player]struct{}
Set of all currently connected players
Connections
map[net.Conn]*Player
Maps network connections to player instances
MaxX
uint32
Maximum X coordinate of the game world
MaxY
uint32
Maximum Y coordinate of the game world
Database
*sql.DB
Database connection for persistent storage
TrackedObjs
map[util.Vector2I]*GameObj
Map of position to game objects (trees, rocks, bushes, etc.)
TrackedNpcs
map[util.Vector2I]*GameNpc
Map of position to NPCs in the world
Mu
sync.RWMutex
Read-write mutex for thread-safe game state access
CollisionMap
map[util.Vector2I]struct{}
Positions that block player movement
Objs
map[util.Vector2I]struct{}
Set of all object positions (currently overlaps with CollisionMap)
TimedScripts
map[uint32][]func()
Scripts scheduled to run at specific game ticks
NpcMoves
map[util.Vector2I][][]NpcMove
Queued NPC movement paths organized by chunk
CurrentTick
uint32
Current game tick counter

Player Struct

Represents a player character in the game world.
type Player struct {
    Pos           util.Vector2I
    ChunkPos      util.Vector2I
    Facing        Direction
    Inventory     Inventory
    Name          string
    DialogueQueue DialogueQueue
    Skills        map[Skill]*SkillInfo
    Conn          net.Conn
}

Fields

Pos
util.Vector2I
Player’s current world position (tile coordinates)
ChunkPos
util.Vector2I
Current chunk the player is in (Pos / 16)
Facing
Direction
Direction the player is facing (UP, RIGHT, DOWN, LEFT)
Inventory
Inventory
Player’s inventory (24 slots)
Name
string
Player’s username
DialogueQueue
DialogueQueue
Queue of dialogue messages for NPC conversations
Skills
map[Skill]*SkillInfo
Player’s skill levels and experience points
Conn
net.Conn
Network connection to the player’s client

Methods

LoadFromDB

func (p *Player) LoadFromDB(db *sql.DB) error
Loads player data from the database. If the player doesn’t exist, returns nil without error. Returns: Error if database operation fails

SaveToDB

func (p *Player) SaveToDB(db *sql.DB) error
Saves player data to the database. Creates a new record if the player doesn’t exist, otherwise updates the existing record. Returns: Error if database operation fails

GetFacingCoord

func (p *Player) GetFacingCoord() util.Vector2I
Returns the coordinate of the tile the player is currently facing. Returns: Vector2I position of the faced tile

AddXp

func (p *Player) AddXp(skill Skill, xpAmount uint32)
Adds experience points to a skill and automatically levels up if thresholds are reached.
skill
Skill
The skill to add XP to (e.g., Foraging)
xpAmount
uint32
Amount of XP to add (capped at MAX_XP)

Supporting Types

GameObj

Represents an interactive object in the world.
type GameObj struct {
    ObjData  grpgobj.Obj
    ChunkPos util.Vector2I
    State    byte
}
ObjData
grpgobj.Obj
Static object data (ID, sprite, etc.)
ChunkPos
util.Vector2I
Chunk containing this object
State
byte
Dynamic state value (e.g., 0 = berry bush empty, 1 = has berries)

GameNpc

Represents an NPC in the world.
type GameNpc struct {
    Pos         util.Vector2I
    NpcData     *grpgnpc.Npc
    ChunkPos    util.Vector2I
    WanderRange uint8
}
Pos
util.Vector2I
Current NPC position
NpcData
*grpgnpc.Npc
Static NPC data (ID, sprite, name, etc.)
ChunkPos
util.Vector2I
Current chunk position
WanderRange
uint8
How many tiles the NPC can wander from spawn point

Inventory

Player inventory with 24 slots.
type Inventory struct {
    Items [24]InventoryItem
}

type InventoryItem struct {
    ItemId uint16
    Count  uint16
    Dirty  bool
}

AddItem

func (i *Inventory) AddItem(itemId uint16)
Adds an item to the inventory. Stacks with existing items or finds the first empty slot.

SkillInfo

Tracks a player’s level and XP for a specific skill.
type SkillInfo struct {
    Level uint8
    XP    uint32
}
Available Skills:
  • Foraging - Gathering berries and other resources

DialogueQueue

Manages NPC conversation state.
type DialogueQueue struct {
    Index       uint16
    MaxIndex    uint16
    ActiveNpcId uint16
    Dialogues   []Dialogue
}

type Dialogue struct {
    Type    DialogueType  // PLAYER or NPC
    Content string
}

Clear

func (dq *DialogueQueue) Clear()
Resets the dialogue queue, clearing all messages and state.

Direction Enum

type Direction byte

const (
    UP    Direction = 0
    RIGHT Direction = 1
    DOWN  Direction = 2
    LEFT  Direction = 3
)

Usage Example

// Access player in packet handler
func handleSomething(game *shared.Game, player *shared.Player) {
    // Get the tile the player is facing
    facingPos := player.GetFacingCoord()
    
    // Check if there's an object there
    if obj, exists := game.TrackedObjs[facingPos]; exists {
        // Interact with the object
        obj.State = 1
    }
    
    // Add XP to player
    player.AddXp(shared.Foraging, 10)
    
    // Save player data
    player.SaveToDB(game.Database)
}

Build docs developers (and LLMs) love