BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

Your first hour: build, play a round, run a test, read one port

BF22142Heroes

This is one path, not a menu. At its end you have built the two programs, played a round on your own server, run a test and watched it pass, and read one function of the original game beside its Rust port and the test that holds the two together. Everything after that is on the other pages.

The first hour 1 build the server and the client about 30 min, mostly waiting 2 play a server folder, a round about 10 min 3 test one test green, then red about 5 min 4 read an address, a port, its test about 15 min Your install is only read. The server works in a copy of its own. A test must be able to fail.
The hour, in four steps. The times are a guess for a machine that has never built the tree; most of the first step is waiting for the compiler.
What on this page was checked, and how

The wiki's own worker does not run the compiler: it may only read the tree and run Python. So every command on this page carries one of two marks. Checked here means it was run, or its file was opened and the arithmetic redone, while this page was written. As the tree's docs give it means the command is copied from the named document of the tree, which is kept by the people who run it: if it fails for you, that document is the one to read and to correct.

Before the clock starts#

You needWhySource
your own install of Battlefield 2 (version 1.50), 2142 or Heroesbf2-rs is an engine; it ships no game data and only ever reads yoursREADME.md
Windows 10 or 11 with a current Vulkan or DirectX 12 driverwhat the play guide was tested onnotes/play-guide.md, section 1
the Rust toolchain stable-x86_64-pc-windows-gnuevery builddocs/release/BUILDING.md, "What a build needs"
Python 3the tools, the scenarios, this wikiused by every command under tools/
about an hour, most of it the first build

Two source fetches are needed once, for the server's script host and the client's menus. As the tree's docs give it (docs/release/BUILDING.md; the commands are in notes/play-guide.md, section 1):

text
powershell -File crates\bf2-python\fetch-cpython.ps1
powershell -File crates\bf2-ui\vendor\ruffle\setup.ps1

Both scripts are in the tree (checked here: the two files exist).

Step 1: build the two programs#

From the tree's top. As the tree's docs give it (README.md, "Build and run"; the same two lines are in docs/release/BUILDING.md and notes/play-guide.md):

text
cargo build --release -p bf2-server --bins
cargo build --release --manifest-path crates\bf2-client\app\Cargo.toml --target-dir target

That gives target\release\bf2_server.exe and target\release\bf2_client.exe. Build release: the docs say a debug build is too slow to play.

While it compiles, read what bf2-rs is and skim how the codebase is organised: the server and the client are thin programs over some fifty crates, and one pair of programs runs all three games. Which game runs follows from the folder you point them at.

Step 2: a server, a client, a round#

The server must not run in your install: it writes compiled scripts, settings and logs. It gets a folder of its own. As the tree's docs give it (notes/play-guide.md, sections 3 to 5):

text
powershell -File crates\bf2-server\difftest\levels\make-root.ps1 -Root <a folder under analysis\scratch>
copy <server>\mods\bf2\Settings\ServerSettings.con <server>\mysettings.con

Put one line into a new file <server>\mymaplist.con:

text
mapList.append strike_at_karkand gpm_cq 16

In mysettings.con set sv.numPlayersNeededToStart 1, so that the round starts with you alone (the original's default is 2). Then, in two windows:

text
target\release\bf2_server.exe --root <server> -- +config mysettings.con +mapList mymaplist.con
target\release\bf2_client.exe --home <profile folder> -- +restart 1 +joinServer 127.0.0.1 +port 16567 +playerName Tester

The server prints Port: 16567 Map: ... Players: 0/16 when it is up. A new profile folder gets a profile named "Tester". Keep +restart 1: the play guide explains that a join given on the command line is otherwise dropped while the intro movies play. On the spawn screen pick a kit and a spawn point, press DONE, and you are in.

What every setting means, bots, a second player on the network, and what is known not to work: start here, which is this step in full, and notes/play-guide.md.

What you just ran

The server ran the original game's own Python scripts and its own level files through a Rust engine; the client drew the original's own menus through a Flash player. One tick of the server shows what happened thirty times a second while you walked, and the life of a player what the server did when you joined, spawned and died.

Step 3: run one test#

Pick the smallest thing that can fail: the tests of the random number generator, which you will read in the next step. As the tree's docs give it (docs/release/CONTRIBUTING.md, section 2: "a crate: cargo test -p <crate>"), narrowed with cargo's own name filter:

text
cargo test -p bf2-gameplay --lib lehmer

It should run two tests, lehmer_matches_modular_multiplication and lehmer_first_steps_by_hand (checked here: both are in crates/bf2-gameplay/src/random.rs; the command itself was not run by the wiki's worker).

Then make it fail, on purpose. Open crates/bf2-gameplay/src/random.rs, change the multiplier 0x5e30 in next_state to 0x5e31, and run the test again. Both tests must go red. Put the digit back. That is a mutation, and it is not a party trick: every port in this tree has to show that its test can fail (how a port is made).

A test that needs a real server and a real client is a scenario. As the tree's docs give it (the head of tools/scenarios/scn.py):

text
python tools\scenarios\scn.py list
python tools\scenarios\scn.py run spawn_move

Scenarios explains what a scenario asserts and where its pictures go.

One command on this page was run by the wiki's worker, many times: the wiki itself, built from your checkout and served on your own machine.

text
python tools\devwiki\build.py --serve 26060
python tools\devwiki\test_devwiki.py

Step 4: read one port, end to end#

The port is the generator behind weapon deviation: 21 callers in the original's server, about forty lines of Rust. Read it in this order.

1. The claim. The original has a function at bf2_w32ded.exe!0x00580750. It takes a 32-bit state, multiplies it by 24112 and then by 26143, each time modulo 231 - 1, stores the result back and returns it. The notes say so in notes/subsystems/gameplay-weapons.md ("Random numbers"), and the wiki's page is random numbers.

2. The port. crates/bf2-gameplay/src/random.rs, the type Lehmer. Look at three things:

  • the comment above the type cites the address and the number of callers. A port without its citation is not counted (parity);
  • mul_step does not write (s * m) % 0x7fffffff. It splits the state into 16-bit halves and shifts, as the original's machine code does. The result is the same number; the port keeps the original's shape so that a reader can lay the two side by side;
  • next_k24 is the next thing every caller does with the state: ((state | 0x80) >> 7) + 1, a number from 1 to 224.

3. The test. At the bottom of the same file. lehmer_matches_modular_multiplication compares ten thousand steps with the plain modular product. lehmer_first_steps_by_hand starts from the seed of the deviation table, 0x20fee, and asserts two numbers worked out by hand in its comment.

4. Check the test's numbers yourself. You do not have to believe the comment. Checked here, with Python:

text
>>> M = 2**31 - 1
>>> 135150 * 24112 % M          # 0x20fee is 135150
1111253153
>>> 1111253153 * 26143 % M
332402263
>>> ((332402263 | 0x80) >> 7) + 1
2596894

Those are the test's two expected values, 332,402,263 and 2,596,894.

5. See it move. The page random numbers has the generator as a piece you can click, draw by draw.

6. Follow one caller. A shot's deviation reads a table of 1024 offsets, and that table is filled by this generator from the seed 0x20fee: deviation and the random table. That page ends with a table of what the port has and how it is tested, as most pages of this wiki do. The rows that are not finished, from every page, are gathered on what is still open: that list is where a second hour could start.

What you know now#

You didThe idea behind itRead on
pointed two programs at a folderthe engine is ours, the data is yoursthe asset pipeline
ran a server in a copythe original is only ever readstart here
broke a test on purposea proof must be able to failhow a port is made
read an address, a port and a testevery claim about the original cites its evidencereading the notes
saw a table of open rowsnothing is called done that was not comparedparity, what is still open

How this page is tested#

The wiki's test checks that every link on it leads to a page of the build. The commands marked "as the tree's docs give it" are not run by that test; the documents they are copied from are named beside each.

In the code#

WhatWhere
the build's table of programsdocs/release/BUILDING.md
how a change gets indocs/release/CONTRIBUTING.md
every folder and crate, one line eachdocs/release/LAYOUT.md
the play guidenotes/play-guide.md
the generator of step 4crates/bf2-gameplay/src/random.rs
the scenario runnertools/scenarios/scn.py
Source: docs/dev/guide/first-hour.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.