Quest for Gaming

Compiling Stronghold 2 AI fix from source

Author avatar image

Posted by: Daerandin
Lord Paladin

A while ago I created a tool that lets you add AI opponents in multiplayer games in Stronghold 2. The tool can be downloaded for windows on this very website: Stronghold 2 fix

The files available for download are precompiled binaries for Windows. My tool also works on Linux, but you need to compile it yourself. For Arch Linux I have created an AUR package which can be found here: sh2_mp_ai_enabler

I am not going to provide details on compiling this on Linux as it is quite simple. You just need to ensure you have build tools installed, and then just go into the 'src' directory for my tool and type 'make'. However for Windows it might be a little more difficult. I know some people have used Visual Studio to compile it, but I am not familiar with it and I also prefer open-source tools. In particular I prefer Unix-style tools, so I will provide a guide for how to compile this tool on Widows, the "Unix" way.

Installing MSYS2

MSYS2 is open source software, and it provides many typical Linux tools to aid in building software on Windows. It can compile C code as if you were on Linux, but the result is a native Windows executable.

Note that this entire guide assumes you are on 64-bit windows. This is the case unless you are using very old hardware.

The MSYS2 installer can be downloaded from here: msys2.org (opens in a new tab)

There are instructions on the website for MSYS2 for what you should do when installing, but to keep everything in one spot, I will provide full instructions here.

On the MSYS2 website, you click to download the installer.

Article image

Once it has completed downloading, you can find the installer in your "Downloads" folder. Double click to run it. It is highly likely that Windows will ask if you really want to run it, click "Run".

Article image

The installer will open up, and you just need to click "Next".

Article image

It will next ask you which directory to install MSYS2 into. You can specify a custom directory, or just let it install to the default location. Click on "Next".

Article image

Next it will ask if you want to change the Start Menu Shortcuts. I strongly suggest no changing anything here and just click on "Next".

Article image

Now just wait for the installation to finish.

Article image

Once the installation has completed, ensure that "Ryn MSYS2" is selected before you click on "Finish"

Article image

Now you will see the MSYS2 prompt. If you are unfamiliar with using a command line, don't worry. I'm going to provide very detailed instructions on the following steps.

Article image

The first thing we want to do is ensure all the tools in MSYS2 is fully updated. To update, you type in the command:
pacman -Syu

Then hit Enter.

Article image

It will now tell you what is going to be updated. Note that it is highly likely that you are going to see different packages being updated, so your screen might look a little different. However, the process remains the same. You will be asked to proceed with installation. Type in 'y' and hit Enter to proceed.

Article image

After downloading and installing updates for the MSYS2 environment, it is going to inform you that it needs to shut down the Terminal window during the update. Again you just need to confirm by hitting 'y' and Enter.

Article image

The window should now close. You just need to start up MSYS2 again. Either find it on your start menu, or just search for msys2.

The program you want to launch is called "MSYS2 MSYS"

Article image

Once again you will see the MSYS2 prompt. You now need to complete the update, so once again type in: pacman -Syu

Then hit Enter

Article image

The packages that needs updating will be shown, and you will be asked to confirm. Again press 'y' and Enter to continue.

Article image

Once the update has finished, you need to install the build tools. MSYS2 only provides the basic working environment, and you need to manually install the required additional tools.

To install the build tools required, type in the command:
pacman -S --needed base-devel mingw-w64-x86_64-toolchain

Article image

You will now be asked which components you want. Just hit Enter to install all.

Article image

All packages to be installed will be listed, and you just need to hit 'y' and Enter to confirm and install.

Article image

When the installation is complete, you can simply close the MSYS2 window.

Article image

Now you need to get the source code for the Stronghold 2 Multiplayer AI Enabler. In the example here we will use the 0.3 release, which is the newest at the time this is written. But if there is a newer release later, you can compile it in the exact same way.

Get the 0.3 version source code here: sh2_mp_ai_enabler-0.3 (opens in a new tab)

You want to download the source code in tar.gz format.

Article image

It will now be in your Downloads folder. Windows, by default, do not know how to handle this specific format, but for MSYS2 (and Linux/BSD/Unix) it is a normal format.

Article image

Now to proceed, we need to open MSYS2, but not the same program we used to update. In order to compile for Windows, we want to use "MSYS2 MinGW x64" Either find it in your start menu, or just search for it.

Article image

This will open a prompt very similar to what was used before. First of all, you will want to navigate to where you have the source code downloaded. Assuming you put it in the normal Downloads folder, then you need to type:
cd /c/Users/[YOUR_USERNAME]/Downloads/

Be sure to replace [YOUR_USERNAME] with your actual username. If you are unsure, you can always open a the windows file explorer, and navigate to C:\Users\ and there you should see a folder with your username. That's the one you want to put in here.

Once you have navigated to the correct directory, you can verify this by using the command:
ls

This will show the contents of the current directory. You should see the source code file listed among the contents.

Article image

Now we are going to unpack the archive that contains the source code. The tar.gz format is just a tar archive with gzip compression. The command to unpack the archive is:
tar xzf sh2_mp_ai_enabler-0.3.tar.gz

Article image

Now that it has been unpacked, you can verify with another use of the command:
ls

Now, in addition to the sh2_mp_ai_enabler-0.3.tar.gz file, you should also see a directory simply named: sh2_mp_ai_enabler-0.3

Use the 'cd' command to move into that new directory, and also into the 'src' subdirectory:
cd sh2_mp_ai_enabler-0.3/src

To further verify that you are in the right spot, you can use the command:
ls

You should see the different source files, along with the Makefile. You can check the picture below for comparison.

Article image

To actually perform the compilation we only need to run a single command:
make windows CFLAGS="-s"

Note that you could also compile with only 'make windows', but adding the CFLAGS="-s" at the end will create a smaller exe file as it strips away symbol tables.

Now you can check that you have a new exe file here with the command:
ls

Article image

Now you can close the MSYS2 window, and just open the normal windows file explorer. Go to your Downloads folder, and then into sh2_mp_ai_enabler-0.3 and src folders. Here you can find the executable.

Article image

You can move this file wherever you want, and even rename it if you so desire.

The source code is no longer needed, so you can delete all of that if you don't want to keep it around. MSYS2 can also be uninstalled at this point if you don't want to keep it around, but it's a great tool for compiling C code in Windows.

Categories: Programming, Free Software, Strategy, Real Time Strategy

You need to Log in to comment.
If you don't have an account, then feel free to Create an account