Primal Instinct | Companion AI in Far Cry Primal
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.
AI and Games is a crowdfunded series hosted on patreon.
Let us return to the world of Far Cry to look at one of the core mechanics of 2016’s Far Cry Primal: animal taming. In Primal players can lure and tame a variety of predators to later use as weapons in combat. It’s a fun new system to add to the Far Cry formula, but it’s introduction was far from straightforward. So let’s take a look at how the companion AI works in Primal and the steps taken to ensure it operates in and around the systemic AI Far Cry is known for. As detailed in an earlier entry in the series, Far Cry is built atop a systemic gameplay framework: where numerous systems and mechanics interact with one another and enables emergent gameplay to arise. There are numerous AI systems involved in making this happen: the friendly NPCs, the enemies soldiers, cars and all the wildlife running around in these massive open worlds. So speaking of wildlife, where do the companions come into play?
The Origins of Companions
So, while Far Cry is built primarily at Ubisoft Montreal, the companion AI in Primal was developed by a small team at Ubisoft Toronto. It wasn’t their first foray into the franchise, having been brought in to assist development on Far Cry 4 after the launch of their inaugural title Splinter Cell: Blacklist in 2013. Toronto were responsible for building the ‘Shangri-La’ sequence in Far Cry 4: a series of five linear missions within the game. In these levels, the player befriends a tiger that they can use as a weapon and clear out enemies. As Far Cry 4 wrapped development in 2014 and Primal moved into pre-production, Toronto pitched to Montreal that they would expand this one-off feature and turn it into one of the core mechanics of the new game. However, this brought with it a stack of new problems that were never encountered during the development of the Shangri-La sequence.
As already described, Far Cry carries numerous systems (several of which in the AI itself) as part of the systemic gameplay experience. However, Shangri-La is a series of linear missions with very specific hand-built and crafted sequences. As a result, almost all of the systemic AI is disabled and the behaviours of the tiger were heavily customised to allow it fit within the narrative playing out. However, the companions are Primal are expected to operate throughout a game where all the systemic shenanigans are in full force. As such, many of the existing systems that they had built for the Tiger didn’t transpose directly across to Primal and in fact an entirely new system was put into place.
The Companion System
To build the companion system, Ubisoft took a step back to consider two key aspects:
- What is the fantasy they’re trying to sell? In this case the idea of the player being the beast master that can literally throw bears at their problems.
- What is the role of the companion?
To do this they considered how the animals would support the player and took inspiration from the likes of Dog Meat from Fallout 4 and Elizabeth from BioShock Infinite, a topic I’ve already covered on AI and Games. Animals act as both companions on the hunt across the plains of Oros Valley but also as a collection of diverse weapons players can throw into combat scenarios and turn the tide of combat. During initial development there were over 50 animals prototyped as companions, pretty much any animal from previous Far Cry games were toyed with as a potential weapon. However in the final product this was reduced down to 17 animals that would be more pragmatic for the purposes of the games design.
How Does the Companion AI Work?
So how do they work? All of the AI in Far Cry is built using behaviour trees: a technique that has cropped up in my pieces on Halo 2, Alien: Isolation and Spec Ops: The Line to name a few. These trees are built using design tools that encode them entirely in XML. The team in Toronto were wary of coming on board to start messing around with the existing code base, not just because it could cause conflicts for the teams in Montreal, Shanghai and Kiev who also worked on the game, but also because of the sheer number and scale of these systems. This would require them to edit dozens of existing behaviour trees already established in the Far Cry code base. But then that presents a new problem, the new companion behaviours would often contradict the systemic AI behaviours that already exist. So how do they address this?
The solution is a system referred to as dynamic behaviour tree injection: the game at run-time injects a new micro behaviour into the system based on the command the player has given to the companion. This solution required little effort to introduce into the existing Far Cry AI architecture, plus new companions could be prototyped in a couple of days given any animal could be extended to support the injection system. Perhaps most critically, it meant that the Toronto team could focus on refining the behaviours without worrying whether the Shanghai office would wake up the following morning only to discover the entire behaviour tree system was broken.
So how does this balance out with the existing system? After all, the companion behaviour is directive from the player, while the AI already operating in each animal is an emergent and responsive system based on the world around it. It needs to do what you tell it to do, so as not to annoy First of all each injected behaviour tree dictates what priority it has over existing ones. Hence, they would ignore things they may typically react to. But conversely, it also permitted for certain instinctual responses, such as the fear of death, to interrupt or overrule the injected behaviour. Hence while you might tell your tiger to run towards a chosen location, it won’t run through fire and burn itself alive to satisfy your request, but it will return to operating in systemic AI mode once it has reach the destination.
Secondly, systemic behaviours can be disabled entirely if it doesn’t suit what the companion behaviour requires. This is useful in some instances given that your beast master actions aren’t really in the best interests of the animal, such as sending smaller animals to fight larger ones in combat. This retains a sense of empowerment in the mechanics – and the fantasy the game tries to sell – by smartly and cleanly overriding all of the existing AI systems. Interestingly, maintaining the fantasy is the reason one of the most notable mechanics with the companions – the ability to pet them – is kept in the game. Petting a tamed animal has absolutely zero impact on the AI behaviour or its relationship with the player. Ubisoft just knew people would get a kick out of it.
Smoke and Mirrors
There were two specific issues that the companion AI team had to contend with in Primal: how companions stay within proximity of the player and how do they follow you when you’re on the move. This actually caused a number of problems given the team wanted to ensure that your companion should always be with you at any point in time, but sometimes the navigation meshes in the game world don’t provide a valid path or you’ve done something ridiculous like just off a high cliff. So how do they compensate for that?
So keeping the companion in proximity is done the easiest way possible: it teleports, but only if it’s not within the players field of view for more than a couple of frames and it’s too far away to reach you quickly. Should the animal be too far away, the game begins to sample locations behind the player that are valid for the companion to spawn onto. That way you don’t have a bear spawn on a tiny rock, or a sabre-tooth tiger spawning in a river, given they can’t swim. While in the retail build this happens very infrequently, you can catch it in the minimap in the bottom corner in very specific circumstances.
Secondly, when companions are following you, they try to stay in your field of view, but not directly in front of you. Thus ensuring that they’re not blocking your view of the path ahead, but also you don’t have to worry about staring at a bears arse for 10 hours of gameplay. Given each animal has its own custom animations set, each has to handle how they move on each frame and adapt the animations to suit based on whether you’re idle, walking, running or even in stealth mode. All animals run at approximately 130% of the players running speed when in field of view, but they move at 250% of your speed when you can’t see them. Thus ensuring they’re never too far behind you if you run off without them.
However, this creates a very specific problem that the Toronto team nicknamed ‘slingshotting’, where if you get far enough away from your companion, it will come screaming up behind you and if you stop the animal will keep moving for a time until it slows down. This created a very real problem during testing where if the player stops suddenly near a cliff edge, the animal couldn't’t slow down in time and would throw themselves to their doom. The first solution to this was to make them re-spawn and teleport in after they fell from view – effectively resurrecting a dead companion so as not to punish the player for the animals mistake. But tiger base jumping broke the immersion. To address this, animals frequently conduct raycast checks around them to calculate distances to nearby edges and can apply speed reduction multipliers to the movement code, which solves around 90% of these instances in the final game.
While it proved a minor detour from the traditional Far Cry formula, Primal’s companion AI was a fun extension that provides some cool and engaging gameplay. It subsequently returned in much reduced capacity in 2018’s Far Cry 5, with a handful of animals available as companions. Perhaps we’ll see more of these systems – and Far Cry itself – on AI and Games in the future.
- "Animal House: Creating Systemic Animal Companions in Far Cry Primal" Chris Seddon (Ubisoft Toronto), nucl.ai 2016, Vienna, Austria.