Turok 4 Multiplayer, Part 1
To start this off, I suppose I should explain what Turok 4 Multiplayer is. Put simply, it's a modification for the original Turok 4: Evolution game, which was released on PC with no multiplayer support. It introduces online multiplayer by means of reverse engineering, programming, and injecting custom code into the game engine.
The idea of this series of posts is to go over the various hurdles and challenges I went through (and am still going through) while creating this modification, in the hope that it will help others learn this stuff, or even create a similar project of their own for their own game. The caveat here is that Turok 4: Evolution on PC did have some local multiplayer code available within the binary. That was a huge step towards having existing multiplayer content usable, and it drastically reduced the amount of work needed.
If we look at other games such as Grand Theft Auto III or Grand Theft Auto: San Andreas, while there was no multiplayer support initially, there was a wide-open world and tons of NPCs to control or replace. That task (replacing NPCs with players, or dynamically spawning them) should have been relatively straightforward once some initial reversing was done… I'll get into that later in the series, though.
The idea when I obtained Turok 4 was the same. I had no idea I was walking into a game where multiplayer modes would exist, but with no way to control the other players or disable the split-screen modes, among other things, including various bugs and crashes to overcome.
So the question I had to ask myself, before even having the game, was where to start. I'll get into that next, but first I want to note that this series assumes the reader has some prior reverse engineering and game hacking knowledge, as well as some understanding of tools such as Cheat Engine/MHS, IDA Pro + Hex-Rays Decompiler/OllyDbg, and ReClass.
The first part of this article won't be super technical, as it's an introduction to the project and the initial steps taken. If you're looking for more technical information on how classes were discovered, the player spawning functionality, or the networking put in place, please tune in later; this part of the series probably isn't for you.
If you're just into the source code and not interested in reading about how it came to be, this is for you:
A video of the current progress of this project can be seen here:
As promised, though, here's the series, part by part, on how the Turok 4: Multiplayer modification came about, what the code contains, how it works, and so on.
Research
I decided the first steps into this would be to research the game engine used and the file formats in place, to see if any files could provide clues about how the game worked, and whether any source code was available for the engine out there.
Game engine
Researching the game engine, I discovered it was used by a few other games: Vexx, 100 Bullets (unreleased) and at least one other. The idea here was that if I got stuck, maybe I could open the files or binaries of one of these games, find additional debug strings or information there, and carry it back to the engine I was working with. Having multiple copies of an engine, even if it's used for entirely different games, is never a bad idea.
Though in my case here, it honestly didn't help much.
File formats
There was no information readily available online about the file formats used by the game engine. Even today I personally haven't documented my own research much. There's a map editor project out there now, but everything is still very limited, and I think anyone should expect to experience this with any game that doesn't have major popularity at the moment, or some pre-existing project or modification such as this.
Looking at the game engine's files is honestly what intrigued me most about the game, though. There's a set of "Actors", and each actor has two files: an actor instance file and an actor data file, if you will. While this didn't help me much with how things worked, browsing through them I discovered there was still a Multiplayer.atr (actor data file) and Multiplayer.ati (actor instance file) within the game's content.
To me this immediately implied there was some multiplayer mode available. After further research I discovered the game was ported to PC from the original Xbox, and on the console it had 4-player split-screen modes… So this was kind of a cool concept: if I could get the game into this mode, I'd have most of the multiplayer problem solved (maps, weapons, scoreboard (or so I thought), players, etc.).
Trial and error: the guessing/testing game
From here my focus shifted a bit away from file formats and more towards playing the guessing/testing game. Honestly, a lot of my personal reverse engineering has been just this, rather than fully reversing the engine out of the starting gate. I had nothing to lose by trying some random stuff that may or may not work, and it would save me some time if it did…
So the first step I took was simply replacing existing .atr/.ati files with the multiplayer ones. The ones I chose were Cheats.atr and Cheats.ati, because there was already a "Cheats" option on the game's main menu.
Getting lucky: split-screen multiplayer
Starting the game and clicking my "Cheats" button, I was in luck! The game opened a multiplayer menu of some sort that appeared to be the same as the original Xbox one: I was prompted with 4 separate boxes, each one asking the player to press a button to choose their avatar.

Now, the idea was that I could easily create a 'ghetto' online multiplayer here if I simply synced the position of the other players (2, 3, 4) to the first player on the other systems. The problem was that the game on PC was never designed to accept more than the local player's input, so there was no way to actually select a character or even "sign in" the other players. What I noticed initially was that the players were signing in, but then backing themselves (and the first player) out for some reason.
So, searching around the files again, I ran into MultiplayerJoin.atr/MultiplayerJoin.ati. At this point I needed to shift my focus back to the file formats and try to understand them, because my understanding was that these files were responsible for letting players join and displaying the appropriate menus so they could select the player they wanted, and so on.
My assumptions were correct: these were the menus used for choosing the individual players. Browsing through the file, I could see various references to each player (the files weren't encoded in any way and had text strings in them, which made things much easier).
Screenshot: player references in MultiplayerJoin (archived)
After finding that players were being referenced, and comparing the individual players, I got an idea of which "block" of data contained each player's data. From there I simply browsed through strings until one caught my eye for some reason:
Screenshot: the string forcing players out of sign-in (archived)
I discovered that this was forcing the players "out" of the sign-in, and after a bit of experimenting I managed to get just the first and second players signed in on that screen, with the second player automatically selecting the default character. My goal of a ghetto multiplayer was one step closer: I was finally in the game, with 2 players spawned that I could (potentially) control.
A video of the progress at this point can be seen here: youtu.be/WbrRCV9FBbI
Incoming bugs
While these bugs don't necessarily line up with the actual timeline of events, they're bugs I faced after getting into the multiplayer screens, and I wanted to cover them before going into further detail about the next steps, as they were pretty important to making any of this worthwhile. The only bug I'll really cover in depth is the invisible players.
Beyond that, I faced some other bugs, such as specific multiplayer-only weapons causing crashes. Rather than reverse engineering the subroutine that was crashing, I simply nopped (replaced the instruction with the assembly for "No Operation", aka nop) the reference to invalid memory and prayed it would work. I got lucky and ran into no additional issues related to that nop. A lot of this has been major trial and error, and major persistence.
Invisible players

When porting the game to PC, they decided the rendering loop would clear the depth buffer before rendering the player's weapon. The side effect was that the first player would be rendered behind everything, due to having no depth buffer (Z-buffer) data. An example of this can be seen in this video.
The main reason I'm covering this is to go over how it was actually fixed, as I'm sure other people will run into graphics issues at some point; maybe the game is rendering something they don't want it to, or failing to render something, similar to what I was experiencing. Your best friend here is PIX, the DirectX debugging/tracing tool. Using it, I was able to determine exactly what was causing the issue, which is how I discovered that the depth buffer was being cleared at the point the player's weapon was rendered.
Using PIX, you can see in the following image that the player exists in the upper black/white box, which contains depth data, with the "Depth" channel selected:

And in the next image you'll see that the player disappears just after IDirect3DDevice9::Clear() is called.

To fix this problem I used a D3D9 proxy DLL, which exports all of the original's functions, replaces it, and calls the original, letting you intercept and change any DirectX 9 functions the game uses. I have to thank Azorbix for this, as the d3d9 kit I was using was originally created by him. Since Game Deception is gone, the only reference I can find to it is here.
With this, I simply looked at the Clear function and the arguments being used, and stopped one of the calls from happening so the depth buffer was only cleared once.
Skipping forward a bit and getting back to the file formats, I discovered history files. I was later able to reverse engineer how they load and actually tell the engine, after the level had started loading, that only the first player had joined, preventing the second player's screen from rendering entirely. I'll cover that when I get more in depth, but for now here's the video of the fixes applied.
So at this point I was in the game with two players, and I had a major bug fixed. Now I had to figure out how to control the second player, which I'll cover in part 2, using IDA Pro to identify player data and dive into how to control the second player.

Comments