docs/Architecture

System Architecture

High-level overview of GTPS3 server components and data flow

Component Diagram

┌─────────────────────────────────────────────────────────────────┐
│                        GTPS3 SERVER                              │
├─────────────────────────────────────────────────────────────────┤
│  ┌──────────────┐  ┌──────────────┐  ┌────────────────────────┐ │
│  │  ENet Host   │◄─│  HTTP        │  │  Main Thread           │ │
│  │  (UDP 53181) │  │  Resolver    │  │  ┌──────────────────┐  │ │
│  │  ┌────────┐  │  │  (TCP 80)    │  │  │ Game Loop        │  │ │
│  │  │ Peer   │  │  └──────────────┘  │  │  ├─ World Updates │  │ │
│  │  │ Manager│  │                   │  │  ├─ Player Logic  │  │ │
│  │  └────────┘  │                   │  │  ├─ Events        │  │ │
│  └──────────────┘                   │  │  ├─ Save Loop     │  │ │
│         ▲                           │  │  └────────────────┘  │ │
│         │                           │  └────────────────────────┘ │
│  ┌──────────────┐                   │                               │
│  │  Packet      │                   │  ┌────────────────────────┐  │
│  │  Processor   │                   │  │ Data Stores            │  │
│  │  ┌────────┐  │                   │  │  ├─ worlds/            │  │
│  │  │ Text   │  │                   │  │  ├─ players/           │  │
│  │  │ (Type 2,3)    │                   │  │  ├─ guilds/            │  │
│  │  │ Binary │  │                   │  │  ├─ db/                │  │
│  │  │ (Type 4)    │                   │  │  └─ items.dat         │  │
│  │  └────────┘  │                   │  └────────────────────────┘  │
│  └──────────────┘                   │                               │
└─────────────────────────────────────────────────────────────────┘

Core Modules

ENet Network Layer

  • UDP-based reliable/unreliable channels
  • CRC32 checksum + Range Coder compression
  • New Packet Protocol (14-byte header + payload)
  • 1024 max peers, 2 channels

Game Loop (Main Thread)

  • Single-threaded event loop
  • ENet service with 3ms timeout
  • World tick processing (~1450ms interval)
  • Player state updates
  • Scheduled events & saves

Packet Processor

  • Text packets (type 2, 3): dialog, chat, commands
  • Binary packets (type 4): movement, actions
  • gamepacket_t builder system
  • PlayerMoving struct serialization

HTTP Resolver

  • TCP server on port 80
  • Returns server_data.php format
  • Requires Admin privileges
  • Thread detached from main loop

Data Flow

  1. Client Connect → ENet CONNECT event → allocate Player struct → send welcome
  2. Login → Type 2/3 packet with tankIDName/tankIDPass → player_login() → validate → send OSM
  3. World Entry → join_request → load world JSON → send world data → spawn player
  4. Gameplay → Type 4 binary packets → unpack PlayerMoving → process action → broadcast
  5. Save → Periodic (5 min) + on disconnect → serialize to JSON

Threading Model

ThreadPurposeSync
MainENet service, game logic, world updates, player processingSingle-threaded
ResolverHTTP server on port 80Detached, read-only access
Auto-savePeriodic world/player savesf_saving_ flag

Key Global State

// Network
ENetHost* server;                    // ENet server instance
int server_port = 53181;             // UDP port

// Game State
vector<World> worlds;                // All loaded worlds
vector<ItemDB> items;                // Item database from items.dat
vector<Guild> guilds;                // Guild system

// Security
struct Server_Security {
    int login_count = 0;
    long long login_time = 0;
    vector<string> ridbans;
    vector<pair<string, long long>> banned_ip_temporary;
    bool restart_server_status = false;
    // ...
} Server_Security;

// Events
struct Hide_N_Seek { ... };
struct Crypto_Update { ... };
struct World_Stuff { ... };