Instead of writing my scripts in the NWN2 Electron Toolset, I chose to create them in the NWN1 Aurora Toolset first. Scripting and testing in NWN1 is much faster than in NWN2. I figured that porting my NWN1 scripts to NWN2 later would be a simple matter.
After two days, I got an initial set of scripts working. To test the scripts, I pitted an army of 38 dwarves against a total of 41 enemies -- 28 orcs, 11 half-orcs, and 2 hill giants. All of the foot soldiers on both sides were level 3, and most of their commanders were level 5. The exceptions were the dwarf king, who was level 6, and the hill giants, who were level 12. Despite the slight superiority of the orcish alliance in terms of numbers and total character levels, the dwarves generally won.
I couldn't help but smile when I saw the dwarven archers march in formation to get within arrow range of the enemy and loose their volleys together. Having squads fight as a unit is what differentiates the default AI from the one that I wanted to develop.
Incidentally, the scenario described here will not appear in "Faithless." The sole purpose of this scenario was to test my battlefield AI routine.
After spending an additional day ironing out a few kinks, I was ready to port the module to NWN2. I had to create the area and NPCs from scratch. I only spent a couple of hours creating the battlefield since I didn't want to obsess over an area that I wasn't going to use in my module. For my NWN2 demo, I pitted 26 dwarves against 45 orcs. I was too lazy to add half-orc NPCs to the module, and since the Electron Toolset does not have any hill giants, I couldn't add those. As for my scripts, I exported them to an ERF file, which I then imported to the NWN2 module. The scripts compiled without a hitch. When I ran the module, however, my game kept freezing as the armies were about to clash.
Fortunately, I had created a trace debugging technique for NWN1 three years ago. This debugging technique also works in NWN2, although setting up the game to output error logs is more convoluted than in NWN1. With this technique, I found that the problem was with something completely unexpected. The NPCs were issuing too many shouts that other NPCs had to respond to. This was causing the engine to overload and break down from sheer stress. It took me two days to get my scripts to run without freezing the game.
Features of the Battlefield AI
The battlefield AI that I created makes soldiers fall in formation when they are spawned. Even if the soldiers are placed haphazardly in their area, they will immediately organize themselves around their unit leader during the game. Hence, the only soldiers that have to be positioned properly are the unit leaders.
The picture below is from the toolset. It shows how the dwarves are initially arranged in their area. In particular, the dwarven archers on the right and the royal guard at the rear are scattered. The dwarven men-at-arms on the left are spaced widely apart.
Below is an in-game view of the dwarves shortly after the module starts. Notice that all soldiers are now neatly lined up in close formation.
Soldiers on the move will also march in formation, albeit somewhat sporadically. Below are a couple of images of orcs on the march. These images were taken within seconds of each other.
Soldiers will generally fight as a unit. Unless their commanders order them to attack, they will stand their ground. The moment steel cleaves flesh or crushes bone, however, fights will quickly degenerate into chaos. But then, such is the nature of war.
The first time that a military unit suffers 50% losses or more, all its soldiers must make a morale check, which is basically a Will save versus fear at DC 20. Any soldier that loses at least 50% of its hit points must also make a morale check. Soldiers that fail will flee the battlefield.
Below are a couple of pictures of orcs exercising the better part of valor. A tell-tale sign that they are running away is that they are no longer carrying their weapons, having dropped them along the way.
There is still quite a bit of work to be done with this AI routine. For example, I have not implemented any fancy maneuvers such as flanking. In addition, I haven't coded any routine for unit commanders to try to rally their troops. Also, the in-game frame rate when there are too many soldiers in the area is bad. I'll have to figure out what the maximum number of NPCs should be to maintain a minimum frame rate of, say, 15 per second.
All these features will have to come later. For now, I wanted to post an update in this blog regarding my progress. It has been a while since I've written anything here.
Making a War Movie
Screenshots of NPC soldiers in the battlefield are nice, but nothing captures the drama and excitement of war better than a video. I ran my module several times, shooting scenes from different positions until I had enough footage to make a three-minute movie.
While filming, I found out that my frame rate was terribly slow. The actual rate varied from time to time, but I was getting as low as 7 frames per second. Sometimes, my video speed dipped even lower than that. This is unacceptable. I will have to find a way to raise the video speed. The most obvious way is to decrease the number of NPC combatants, although my scripts can probably stand a little more streamlining.
To create a movie out of my footage, I used Microsoft Windows Movie Maker, which comes packaged with Windows XP. Within a few minutes, I started wishing I had bought a reliable video editing software. Windows Movie Maker kept crashing on me. Each time I made a move, I had to save my file for fear of losing all my hard work. This travail was worse than playing the computer game version of Temple of Elemental Evil. War is hell, but so is making a video with Windows Movie Maker. If I had been coerced into using it, that action would have constituted a crime against humanity. Since no one forced me to use the software, I can only excuse myself by pleading temporary insanity.
After a day of madness, I finally completed the video to my satisfaction. It isn't Apocalypse Now, but at least it shows what my battlefield AI is capable of doing. A somewhat hi-res version of the video may be downloaded from the Neverwinter Vault at this page. A very low-res version may be viewed below.