# Source code of the Counter-Strike 1.6 browser client at swedencode.com/cs/play/

The game engine and the game client running at https://swedencode.com/cs/play/ are built from the source code in
this archive. This archive is the Corresponding Source for those binaries, under the GNU GPL.

| File served by the page | Built from | Licence |
|---|---|---|
| `engine/xash.wasm`, `engine/generated/xash.js` | `xash3d-fwgs/` (engine), Emscripten runtime | GPL-3.0 (engine), MIT (Emscripten runtime) |
| `engine/libref_webgl2.wasm` | `xash3d-fwgs/ref/gl` (renderer `ref_gles3compat`) | GPL-3.0 |
| `engine/libref_soft.wasm` | `xash3d-fwgs/ref/soft` (software renderer, for PCs without WebGL 2) | GPL-3.0 |
| `engine/filesystem_stdio.wasm` | `xash3d-fwgs/filesystem` | GPL-3.0 |
| `cs16/client_emscripten_wasm32.wasm` | `cs16-client/cl_dll` | GPL-2.0 + Half-Life SDK licence |
| `cs16/menu_emscripten_wasm32.wasm` | `cs16-client/3rdparty/mainui_cpp` | GPL-2.0 + Half-Life SDK licence |
| `engine/xash3d.js`, `engine/net/`, `engine/utils/` | JavaScript wrapper from the `xash3d-fwgs` npm package, served as source | MIT |

The Half-Life SDK licence allows free distribution only; the browser client is free to play.
Valve's game data (maps, models, sounds, textures) is not part of this archive and is not covered by these licences.

## What is in here

- `xash3d-fwgs/`: FWGS Xash3D at the upstream commit in `VERSIONS.txt`, with all submodules used by the build,
  and `patches/xash3d-fwgs.patch` applied.
- `cs16-client/`: FWGS cs16-client at the upstream commit in `VERSIONS.txt`, with `patches/cs16-client.patch` applied.
  The server library (ReGameDLL_CS), the bots (YaPB) and the add-on asset folder are left out: the browser client
  does not ship them.
- `patches/`: our changes as diffs against upstream.
- `build.sh`: the build. `em_glue_post.py`: the post-processing step for the Emscripten JavaScript glue.

## Our changes (patches/xash3d-fwgs.patch)

- The browser build is shown as **SwedenCode Engine** (console corner, menu corner, `version` line), built from Xash3D FWGS;
  `XASH_ENGINE_NAME` itself is unchanged for paths and protocol strings.
- `gl_web_maxtex` (ref/gl/gl_image.c): caps texture size on low-memory devices; the page sets 256 on phones. Lightmap
  atlas pages and fonts are never shrunk.
- `Module.onDisconnect` (cl_main.c, library_xash.js): after a real disconnect (not a map change) the page shows its own
  screen instead of the engine menu.
- cs16-client menu (patches/cs16-client.patch): text buttons in DejaVu Sans Bold (`gfx/fonts/DejaVuSans-Bold.ttf` in
  cs16/extras.pk3, Bitstream Vera / DejaVu licence) instead of the WON picture buttons; SwedenCode client name in the corner.
- `ref/soft/r_glblit.c`: the software renderer's colour-tint table rounds instead of truncating (the CS HUD
  orange was drawn red). The engine is configured with `--enable-soft`, so it also accepts `-ref soft`.
- Emscripten platform: library names `<name>_emscripten_wasm32.wasm`, engine as the Emscripten MAIN_MODULE with the
  renderer, filesystem and game libraries as side modules, growable memory (no fixed heap), browser main loop.
- `engine/platform/emscripten/`: the game's UDP socket goes to the page's network object (`Module.net`), which
  carries the datagrams over a WebSocket relay; HTTP downloads use Emscripten's sockets.
- `identification.c`: a browser exposes no machine id, so the player's id comes from a per-browser key the page
  gives (`Module.playerKey`).
- `cl_main.c`: in a game, the engine lets the page show its own menu (`Module.onGameMenu`) instead of the engine's.
- `s_sdl2.c`: the Web Audio buffer size can be raised by the page (`Module.audioBufferMultiplier`).
- `cmd.c`: a server's refused privileged commands (it may stuff `bind` on join) are reported in the developer console
  only, not on the player's screen.
- `ref/gl/gl2_shim/gl2_shim.c`: a WebGL vertex stream (one vertex array object, one streaming vertex and index
  buffer, one upload per draw) and a cache that drops GL state calls which would change nothing.
  `-noglstream` restores the generic path.
  Upstream's WebGL stub for `glPolygonOffset` is no longer installed (WebGL has it): without the offset, decals - player
  sprays, blood, bullet holes - z-fought with the surface and showed as stripes.
- `em_glue_post.py` (glue post-processing) adds `backgroundTicks(on)`: a hidden browser tab gets no animation frames,
  so the page runs the main loop on timers while the tab is hidden and the server does not drop the player.

## Our changes (patches/cs16-client.patch)

- Emscripten platform entry for the library names; no `--no-undefined` for side modules.
- `3rdparty/mainui_cpp`: the menu keeps its own `gpGlobals` name (`g_pMenuGlobals`) in the web build, because all modules
  share one symbol table there and the game client has a variable of the same name.
- `udll_int.cpp`: the page may show its own team and class screens (`Module.onTeamMenu`, `Module.onTeamMenuHide`)
  and buy menus (`Module.onBuyMenu`, which also gets the player's money).
- `controls/BaseClientWindow.cpp`: in the web build, Esc closes the window.
- `cl_dll/events/event_createsmoke.cpp`: in the web build, smoke grenades make a denser cloud in two layers.
- `cl_dll/cs_wpn/cs_weapons.cpp`, `entity.cpp`, `cdll_int.cpp`: in the web build, a predicted weapon deploy uses the
  view model the server last used for that weapon (servers with weapon skin plugins), so the draw does not jump from
  the stock model to the server's.
- `cl_dll/hud/scoreboard.cpp`: in the web build, the page may draw the scoreboard: while it is up the client sends the
  player list as JSON (`Module.onScoreboard`).
- `cl_dll/hud.h`: in the web build, a server's own HUD icons missing from `hud.txt` are reported in the developer console only.
- `cl_dll/menu.cpp`: in the web build, the page may draw the server's text menus (AMX Mod X menus, map votes):
  `Module.onHudMenu(text, keys, seconds)`; keys still choose through the client.
- `cl_dll/hud/MOTD.cpp`: in the web build, the page may show the server's MOTD windows (the join message, /top15,
  /rankstats, /vip), HTML included: `Module.onMotd(text)`. The HUD itself can only draw plain text.

## Build

Requirements: Emscripten SDK 7.0.0 (`emsdk install 7.0.0 && emsdk activate 7.0.0`), Python 3, CMake (not 4.2.0-4.2.5
or 4.3.0-4.3.2), Ninja, Git. Linux, macOS or Windows (Git Bash).

    ./build.sh            # output in out/

To rebuild from upstream instead of the trees in this archive: clone both repositories at the commits in
`VERSIONS.txt` with `--recursive`, check out the listed submodule commits, apply the two patches
(`git apply patches/xash3d-fwgs.patch` inside `xash3d-fwgs`, `git apply patches/cs16-client.patch` inside
`cs16-client`), then run `build.sh`.

Questions: https://swedencode.com/discord
