2004scape Server Documentation

repository·main·Indexed 17 days ago

https://github.com/2004scape/server

A server implementation for a 2004-era game environment featuring a built-in web client. The project supports both content and engine development workflows, requiring NodeJS 22 and Java 17. Documentation covers server setup, developer commands, a custom software rendering engine, binary data parsing via the f class, texture management with the I0 class, and a WebAssembly-compiled bzip2 implementation for buffer compression.

Tokens
13K
Snippets
47
Records
69
Agent score
64%

What's inside 2004scape

  1. Get started with the 2004scape server

    main

    To set up and run the server, follow these steps:

    1. Download: Download and extract this repository to a local directory.
    2. Install Dependencies: Ensure you have NodeJS 22 and Java 17 (or later LTS versions) installed.
    3. Run Quickstart: Open the downloaded folder and run the quickstart script. Follow the on-screen prompts (you may disregard severity warnings).
    4. Wait for Startup: Do not attempt to play until the script indicates that the world has started.

    Note: The server includes its own web client, so no separate client download is required.

  2. Developer workflows for Content and Engine

    main

    Depending on your role, use the following commands to start the development environment:

    Content Developers

    Run npm start. The server will watch for changes to scripts and configs, then automatically repack everything.

    Engine Developers

    Run npm run dev. This performs the same watching/repacking as npm start, but also performs a complete server restart whenever engine code changes.

    # For content development
    npm start
    
    # For engine development
    npm run dev
  3. Install dependencies for 2004scape

    main

    The server requires the following runtime environments:

    • NodeJS 22
    • Java 17 (later LTS versions are also acceptable)

    If you are developing using VS Code, it is recommended to install the 2004scape.runescriptlanguage extension from the marketplace.

  4. Handle mouse clicks on tiles

    main

    The client tracks mouse interactions to determine which tile a user has clicked. This is handled during the drawTileUnderlay process.

    When V.takingInput is true, the system checks if the mouse coordinates (V.mouseX, V.mouseY) fall within the projected triangle of a tile's underlay using this.pointInsideTriangle. If a match is found, V.clickTileX and V.clickTileZ are updated with the clicked tile's coordinates.

  5. How the level tile system works

    main

    The level system is organized into a multi-level grid structure. Each tile at a specific level can contain multiple types of data:

    1. Layers: The world is divided into multiple levels (e.g., ground level, bridge level, etc.).
    2. Tile Composition: A single tile (Z0) acts as a container for:
      • underlay: A r0 object defining base colors/textures.
      • overlay: A y object defining top-layer shapes and textures.
      • wall: A L_ object defining structural walls.
      • locs: A collection of G_ objects representing specific entities or interactive objects.
      • groundDecoration: A b_ object for ground-level items.
      • objStack: An N_ object for stacked items.

    This hierarchical approach allows for complex environments where objects can be placed on top of each other or within different vertical layers of the same coordinate.

  6. Graceful shutdown of the server

    main
    The server handles SIGINT and SIGTERM signals to attempt a safe exit. Upon receiving these signals, the server calls World.rebootTimer(0) to manage the shutdown process. Note that in development mode using tsx watch, graceful shutdown may not be fully supported due to limitations in the tsx tool.
  7. Handle social and chat input via keyboard

    main

    The handleInputKey method manages keyboard input for various UI states, including social input, chatback input, and standard chat.

    Chat Commands and Formatting

    When typing in the chat interface (chatInterfaceId !== -1), the client supports several special commands and color prefixes:

    • FPS Commands:
      • ::fpson: Toggles FPS display on.
      • ::fpsoff: Toggles FPS display off.
      • ::fps <number>: Sets a targeted framerate.
    • Color Prefixes: Adding these to the start of a message changes the chat color:
      • yellow:, red:, green:, cyan:, purple:, white:
    • Visual Styles:
      • flash1:, flash2:, flash3:
      • glow1:, glow2:, glow3:
      • wave:, scroll:

    Social Actions

    When showSocialInput is active, the client processes social actions based on socialAction:

    • socialAction === 1: Add friend.
    • socialAction === 2: Remove friend.
    • socialAction === 3: Send a private message.
    • socialAction === 4: Add to ignore list.
    • socialAction === 5: Remove from ignore list.
  8. Handle NPC and Player models with sequenced animations

    main

    The client uses specialized classes to manage the rendering and animation of NPCs and Players.

    NPC Rendering (I_ class)

    Extends s0 (a base entity class). It manages npcType and handles the drawing of sequenced models. It supports spotanim (spot animations) which can be applied as transformations to the base model.

    Player Rendering (K0 class)

    Manages player appearances, including gender, head icons, and a collection of 12 appearance slots (clothing/equipment).

    • Appearance Hashing: Uses an appearanceHashcode to cache models in K0.modelCache to avoid redundant computations.
    • Recoloring: Applies specific color palettes for torso and design IDs during model construction.
    • Sequences: Supports primary and secondary animation sequences (e.g., walking, turning, running).
  9. Understand the 2004scape Client Architecture

    main

    The public/client/client.js file serves as the web client entrypoint. It contains a highly optimized software rendering engine and data processing utilities designed to run in the browser.

    Key architectural components include:

    • Software Renderer (T, w): A custom 2D/3D rendering engine that manipulates a raw Int32Array of pixels. It supports features like Gouraud shading, textured triangles, and alpha blending.
    • Data Buffer Management (f): A specialized class for reading and writing structured binary data (similar to a DataView but with optimized methods for specific game protocols like Isaac random numbers and custom integer types).
    • Image/Texture Handling (I0): Logic for decoding and managing game assets (archives and indices) and converting them into usable pixel buffers with color palettes.
    • Memory Management: The engine uses custom linked-list-based pools (m0, f.cacheMin, etc.) to minimize garbage collection during high-frequency rendering and data parsing.
  10. Handle unused variables with underscore prefix

    main

    To avoid ESLint errors for unused variables, parameters, or caught errors, prefix them with an underscore (_). This is specifically configured for:

    • Variables: varsIgnorePattern: '^_'
    • Arguments/Parameters: argsIgnorePattern: '^_' (useful for router methods like (req, res) => ... where req might be unused)
    • Caught Errors: caughtErrorsIgnorePattern: '^_'
    // Example of ignoring an unused parameter
    const routerMethod = (_req: Request, res: Response) => {
        res.send('Hello');
    };
    
    // Example of ignoring an unused variable
    const _unusedVar = 10;
  11. Start the 2004scape server

    main

    The server application entrypoint initializes the game world, starts multiple networking layers (TCP and WebSocket), and launches web management interfaces.

    When running the application, it performs the following lifecycle steps:

    1. Compiler Update: If Environment.BUILD_STARTUP_UPDATE is true, it updates the RuneScript compiler.
    2. Asset Packing: If the required pack files (data/pack/client/config or data/pack/server/script.dat) are missing, it automatically runs packClient() and packServer() to generate the cache.
    3. Worker Initialization: If Environment.EASY_STARTUP is true, it spawns workers for login.ts, friend.ts, and logger.ts.
    4. World Startup: Initializes the core World engine.
    5. Networking: Starts a TcpServer for game traffic and a WSServer for web-based connections.
    6. Web Interfaces: Launches the standard web server and the management web interface.
    7. Metrics: Registers Prometheus metrics using the Environment.NODE_ID.