Dev Diary #07 ? Code Update
May 14, 2014
MikeD here, thought its time to give you an update on how the code is coming along.
I will leave it to the art teams to provide flashy videos, this is going to be a text heavy blog, but you might find some interesting info in here, even if you?re not technically minded..
Chris and myself have quite a few major systems up and running. Here is a summary of a few of them.
Guns, Lots of Guns
We contracted an old colleague of ours, Chris Vera, to build and texture all our in-game weapons for us from a selection of a million designs which were produced by concept artist Jeremy Love. Chris did a fantastic job, so we now have lots of weapon meshes in game, and they are also built to high enough quality to use in load out screens.
The code side has a few interesting things implemented, like a first-pass at a flame thrower and rocket launcher. We have different classes of weapon implemented, such as beam and ballistics, including charge phase or warm up, cool downs, reload times, fire rates, damage rates to various shields/armour/flesh? All going through a pool system to avoid feeding the garbage collector. Particle systems are spawned, maintained and cleaned up, although our effects artist hasn?t got to the fun bit of actually making the weapon effects, so it?s mostly programmer art at the moment. This results in the comedic effect of one of the biggest guns in the game, which needs charging for several seconds, firing out a tiny little bullet with barely a pop.
Agents aim their weapons by blending between about 9 animations that form a grid aiming the gun down any vector in a +/- 90 degree arc in both horizontal and vertical directions. Multiply this by the various weapon types (single handed, dual handed, heavy,..) and you get a lot of anims. I?ve also been playing around with FinalIK from the Unity Asset Store. It?s showing promise, but needs me to spend a bit more time setting it up. IK (inverse kinematics) effectively allows us to say where we want the weapon to point, and the IK positions the arms accordingly (coincidentally, I once spent a week or two coding up just this sort of system when I was at THQ).
The weapon-hit mechanics are raycast-based, so cover works physically correctly with noise added to aiming if they are fired repeatedly, and are a recoil type of weapon, or indeed the target is moving and far away. Lots of numbers to tweak!
State System
This is the nuts and bolts of the gameplay. It?s not really AI, it?s lots of smaller building blocks of AI. We have states that implement various abilities, like shoot, reload, enter cover, exit cover, shoot from cover, use car, flee, patrol, investigate, arrest, queue up, guard and so on. Not quite hundreds, but a lot. These states can sometimes operate in parallel, or get queued up, or form a tree of states with parent states monitoring child states. States can be interrupted and resumed, and handle or ignore events.
Event System
Nearly everything that happens in the game fires off events to nearby people. These events sometimes travel at the speed of light, sometimes the speed of sound. They then travel through the nervous system of the receiver. I literally coded up the time taken for the nervous system of a human to react to a gun shot (I hate games were AIs react instantly).
Everyone reacts to events if they feel so inclined, nothing is really hard coded. There?s a few base parameters for everyone including bravery, intelligence, skill, that sort of thing. This goes through some fuzzy logic for each of the possible events they may witness, which factors in lots of extra info as necessary, such as getting a bravery boost from having allies nearby, or multiplying in your diligence stat if your boss is watching. Lots of numbers to tweak!
I especially like this stat that everyone has
public float m_Grit;
//stick-to-it-ness, gets the job done, not easily bored/distracted, resolute of purpose
Which I got from the preacher comics after reading the issue that talked about good ol? Texas grit.
The result is that we already get surprised by some of the events that happen, like a civilian accidentally wandering into a secure area causing himself to be arrested, but because I haven?t written any code to handle being arrested (the player is supposed to stand still and not exit a projected circle on the floor while being scanned by security), the civilian happily ignored the demands of security to stay still, which resulted in him being shot, which caused nearby civilians to pull out pistols and start firing back, which resulted in the security mob with their superior fire power leaving a trail of dead civilians across the map.
Another fun event was when the agents bravery got corrupted and combined with a bug, which resulted in them running away from each other when one of them fired a gun. One of your agents fleeing to cower in a corner because the Soldier fired his mini-gun is a feature I?d almost like to keep. Maybe the enemies can have a fear inducement gun.
StateCoverCombat
This is our work-horse system for strategic combat. AIs evaluate cover possibilities, and try to work as a team, providing covering fire. Although a lot of the cover in the game is destroy-able, so what appears as a great cover spot one moment can suddenly become a killing zone shortly after. Choosing which cover is best is made suitably complex by factoring in multiple possible enemies in multiple locations, each with different fields of view over the ground that needs to be covered. Lots of numbers to tweak!
Abilities
These are effectively special abilities outside the normal gun-play, so things like stealth mode, or throwing a grenade, or something not yet to be revealed. Healing boosts, or damage multipliers, special abilities your agents may have due to their class or upgrades. But the AIs often have these abilities too. One issue at the moment is the enemies are a bit grenade happy, so if you group your agents together in a combat situation, the enemies will happily take you all down with one well aimed grenade. Lots of numbers to tweak!
Drones
One weekend, I went a bit crazy with the drone physics, and pretty much implemented quad-copter physics, because, well, they look a bit like quad copters. They are a bit twitchy still, but its nice to see them moving around, clearly running with a simulated movement rather than just on rails. They have parameters for braking and acceleration, lift, gyroscopic stabilization. Lots of numbers to tweak!
Traffic System with junctions and usable cars.
The cars correctly move through the city, and have a decent navigation system that allows them to get to their desired location. The queuing at junctions needs a bit more work, it?s currently a first-come-first-served approach, but that needs to change to be more like traffic lights, so a few cars can flow through at a time. The cars are pretty efficient. We can really crowd the streets with cars, but then the city does suffer from grid lock
too much like reality. Agents can use cars to get round although there is no enter /exit car animations yet (everyone blame Mitch), and running to a car door is fraught with navigation bugs, as the agents don?t yet know they cant just run through a car.
Audio
After a few false starts, I finally settled on SECTR_Audio from the Unity Asset Store, a well put-together system by the ex-technical director of Double Fine. He?s super friendly, and helped me get going with audio. Unity?s audio system wasn?t giving us the results we wanted, but SECTR_Audio adds a lot of the features I?m used to as a AAA dev. All of our audio is place-holder at the moment, we don?t get the mighty Russell Shaw full time for a few more weeks yet. And from putting in place-holder audio, I can now really appreciate how hard it is to mix the audio for a game and get it all nicely balanced.
Making a city in Unity
Unity doesn?t like city-sized maps. To be honest, no editor does, so we have lots of work-around solutions. Chris made some tools for loading and unloading smaller city segments so Unity doesn?t have to have the whole map in memory at once. I?ve been testing the run-time, and I?m pretty confident we can have the whole city loaded at run-time, we just need to jump through a few hoops to make it happen. Unity isn?t happy with tens of thousands of objects placed in the world, the frustum culling alone takes 20 ms, so I?ve ended up with my own system switching on and off objects on the fly, just so Unity doesn?t have to worry about so many objects at once.
We have tools for generating cover locations (about 10000 per square km), that dynamically update as scenery is destroyed at run-time. We have painting tools for defining secure areas. Traffic editors. Power supply editors for defining where all the cabling goes in the city. Data and power cables are important, as you will discover.
Wardrobe System
This system bolts together our civilians from a variety of pieces. Dean, our character artist, can define probabilities of various pieces being used to generate a unique looking civilian of a certain classification or faction. The agents themselves use this system too at the moment, so every run of the game, the agents face/hair styles look different. It?s quite fun to get an agent with a skull face-tattoo and a big punkish mohawk. This will factor into the cloning system when agents are regenerated from civilian stock.
Navigation
I spent a year or so as a navigation programmer (writing code to build nav meshes and do path smoothing), so this is pretty dear to my heart. I played round with a few systems on the Unity Asset Store, but kept coming back to the built-in Unity one (Pro only feature). It?s actually pretty good, it?s almost awesome, but it just has a few niggles with dynamic cutting causing occasional bugs in the navmesh (please hurry up and fix this, Unity). I have spent a lot of time building on top of this with our own nav systems, and also integrating the navigation with Mecanim (Unity?s state-based animation system demonstrated in Mitch?s animation video) so that I feed steering information to Mitch?s super-complex anim state tree, which can blend in all kinds of turn anims depending on the situation, while also providing some overridden course correction if we start straying too far. We also have stop anims built in, so people will come to a nice smooth halt without feet sliding or a sudden anim pop.
Frag System.
Every game with guns needs a frag system. Meat and metal can now fly apart in a pleasingly realistic way, matching the pose at the time of death.
Optimization
When trying to simulate a whole city, optimisation is a constant battle, so thank Unity for their built-in profiler. We have various levels of lowering the simulation cost of NPCs as you move around the map. Mainly simplified movement/collision/Animation systems. A kind of LOD for AI.
Rendering
We recently changed over to Candella to do our reflective surfaces, freeing up our visual effects guys to concentrate on all the other fancy shader things we need.
To ease our material/lighting setup, we moved over to a physical based lighting model using Alloy, which works very nicely with Candella.
The artists are really happy with the engines lighting abilities in the game now, especially combined with Unity?s deferred render engine, which allows a gazillion on-screen lights.
Did I mention we have lots of numbers to tweak?
-MikeD.