The Code

I'd rather people not skip to this section directly, since the previous sections explain so much of what we're trying to do and how we're going to do it. A complete cut-and-paste job will simply not work well for mods with even moderate complexity. There are quite a few little niggly things that you're likely to miss if you don't read the previous two sections. So if you skipped directly to here, please do yourself a favor and go back.

Also, before you go on, make sure you have a project that builds both within your test environment (probably Visual C++ or Visual Studio) and for production (VMs). If you can't get it working, visit Code3Arena, where there are some great tutorials on it.

Those of you working on a new mod have it easy. Just drop these source files in the right places, overwriting the 1.29h source files. Follow the directions in the next section (“Importing”), redefine GAMEVERSION in g_local.h, and you're good to go.


I've included two source files with this distribution, “cg_unlagged.c” and “g_unlagged.c.”

cg_unlagged.c contains all of the code for predicting instant-hit weapon effects (rail trails, bullet strikes), and a couple of miscellaneous functions. g_unlagged.c contains the backward reconciliation logic and most of the skip correction logic.

To get one of them into a Visual C++ project, right-click the correct project in the FileView tab in the Workspace window, and choose “Add Files to Project.” A file dialog will appear; select the file and click “OK.” Now, when you build, the file should get compiled and linked into the final DLL. Obviously, you'll want to put g_unlagged.c in the “game” project and cg_unlagged.c in the “cgame” project.

Now search in the Unlagged source for “//unlagged - cg_unlagged.c” and “//unlagged - g_unlagged.c”. Copy the lines those comments sandwich into the right places. Now you can use the functions in those source files.

The batch files that build the VMs will need to know about these new source files as well. I can't be certain what your files will be named or look like, but I can take a guess. Edit game.bat and add g_unlagged.c to it, copying whatever code there is in it that compiles other source files. It doesn't matter what order the files are compiled. Then add g_unlagged to game.q3asm. Do the same for cg_unlagged.c in cgame.bat and cgame.q3asm.


First of all, back up all of your code. You might need it to roll back changes, or for reference when debugging.

There are about 100 pieces of code to merge to get everything working like in Unlagged 2.01. That's actually not too much, since a lot of them are one-liners at the beginnings of functions to define a variable. Many more are one-liners that just need to be copied in – like the code that calls the function to “time shift” other players. Still more are pieces of code that are commented out because they are no longer necessary.

Some of the features have either minor dependencies or no dependencies, making them quite simple to include. Here they are:

I've put them in order of difficulty. Of those listed, the player prediction optimization, as well as being the most difficult, is the most invasive, and should be handled with the most care. If you've never made a change to CG_PredictPlayerState() in cg_predict.c, though, you should be fine just overwriting that whole function with Unlagged's version. You'll have lucked out.

Be aware that true ping, lag simulation, and optimized prediction require some new cvars.

Since the rest have major dependencies, there really is a “best” order to do things in. Here it is:

  1. If you haven't done it already, merge the forward declarations for the functions in cg_unlagged.c (look for “//unlagged - cg_unlagged.c”)
  2. Merge forward declarations for the functions in g_unlagged.c (“//unlagged - g_unlagged.c”)
  3. New server cvars (“//unlagged - server options”)
  4. New client cvars (“//unlagged - client options”)
  5. All of the backward reconciliation code, even if you're not planning on doing the full lag compensation (“//unlagged - backward reconciliation”)
  6. All of the attack prediction code, only if you are planning on doing the full lag compensation (“//unlagged - attack prediction”)
  7. Lag-compensated grappling hook if your mod features the default grappling hook (“//unlagged – grapple”)
  8. Projectile nudge (“//unlagged - projectile nudge”)
  9. Early transitioning of missile entities (“//unlagged - early transitioning”)
  10. Skip correction (“//unlagged - smooth clients”)

The last five can be done in any order without problems, so I've put them in order of difficulty.

Next: Code Walk-throughs