Review
B000O5DY0E

Living with The Sims’ AI: 21 Tricks to Adopt for Your Game

Alex J. Champandard on October 15, 2007

In terms of artificial intelligence, simulation games are often a step ahead of the mainstream. The actors interact with the players for much longer periods of time, and must find original ways keep their attention.

As the archetypal sim-game, The Sims revolutionized the industry by creating an interactive doll house, becoming the best selling PC game of all time in the process. The Sims is among Top 10 Most Influential AI Games for these reasons.

This technical review from AiGameDev.com looks into the technology behind this prolific franchise: things to adopt as well as what to avoid.

Smart Objects

Objects play a very important part in the interactive doll house. Not only do they provide goals for the materialist gameplay, but they also create a rich environment for the AI actors to interact with. Implementing a system to handle large numbers of games is not an easy task.

Kitchen and Dining Room

Screenshot 1: A suburban household from The Sims 2.

1) Make Your Objects Self-Contained

In The Sims 2, all the data relating to objects is stored together. According to Jake Simpson [1], Lead Simulation Engineer, this includes:

“Localized string lists, executable scripts, common definition meta data, animation lists, routing information, advertising information, interaction definitions, model information.”

A one-stop resource for any information relating to objects is very useful to have both during development and at runtime. It makes the whole game much more modular, which explains how easy it is for EA to build expansion packs!

2) Use Micro-Threads for Objects

Because there are so many objects collaborating together to form the gameplay, it’s important to make sure they all get a share of the computation time if necessary. In The Sims, this is done using cooperative scheduling.

Each object has its own micro-thread for execution which gets updated each frame if necessary. The scripting language supports features to help these threads cooperate with the game engine, for example a yield instruction to wait for the path-finder, animation system, or even a timer.

3) Automate Handling of Objects

Because there are so many objects and they are usable modularly, the game engine must handle them automatically without any special instructions. When a new object is inserted into the game:

  1. The initialization function of the object is executed.

  2. Each frame, the object’s main update function is called.

This differs from the majority of games where objects either contain no logic, or the functionality is implemented centrally in the engine. The advantage of this approach is that extending the game requires fewer changes to the engine (if any).

4) Use Objects Uniformly Everywhere

With an object model as powerful as the one in The Sims, it makes sense to use it extensively throughout the game. And indeed, objects are used practically everywhere. They fall into two categories:

  • Physical — Cars, chairs, electronics, gadgets, toys, toilets, etc.

  • Abstract — Objects to handle visitors arriving on the lot, change the weather, track time of day, etc.

The only requirement for an “object” is that it conforms to the common API, so its code is executed every frame.

5) Add Advertising Data to Objects

Each object has a public interface that broadcasts its functionality to actors in the game. This is called advertising data in The Sims, and contains a list of possible actions and what motives they satisfy.

For example:

  • Using the toilet satisfies the bladder and increases comfort.

  • Cleaning the toilet improves the atmosphere in the room.

This information is used by the decision-making code (discussed below) to pick the next object an actor uses autonomously.

6) Store Assets Separately

Despite objects being self-contained, certain assets are stored separately when necessary. Specifically, this prevents duplication of animation data, textures, or models. The references to these assets are still contained in the object itself, but they are saved in a common location.

7) Use an Excel Sheet to Store Properties Centrally

This is another exception to the objects being self-contained, but it makes things more convenient. When tweaking the game, whether in the final stages before shipping or during prototyping, it’s convenient to have a complete overview of the game in one place.

In The Sims, a large Excel sheet is used to store the properties of the objects. When the game is built, the information is copied over to each object individually, but the values can be adjusted centrally without having to search for each object.

Development Process

With a franchise as prolific as The Sims, there are certainly lessons to be drawn from the development methodologies. If anything, this may allow you to develop more smart objects for your game, and increase the realism of your actors.

Chaotic Living Room

Screenshot 2: The average living room in The Sims 2.

8) Formalize the Development Process

When you have a modular object system, it becomes possible to treat new features one object at a time. During the development of The Sims 2 and its expansion pack, there was a very clear development process [1]:

  1. Design meeting about the object to decide what motives it satisfies, if it needs repairing, if there are other similar objects in a group, how it fits with the theme.

  2. Technical meeting to define the scope of the object, what’s necessary to implement it (in terms of art assets and scripts), and also if additional engine support is required.

  3. Scheduling can take place to plan the workload required to get the asset into the game. This is typically a collaboration between script engineers and artists.

This process makes it much more predictable to build smart objects, so the environments are more likely to be interesting and varied.

9) Invest in Home Grown Tools

The custom tool behind the smart objects in The Sims is known as Edith. The developers decided it was worth the trade-off to invest in their own technology.

  • Advantages — It’s easier to make changes to suit the gameplay and workflow.

  • Disadvantages — It takes more time, skills are not transferable.

These days, most developers prefer reusing an existing language, but for very specific problems it still makes sense to roll your own.

10) Focus on Powerful Development Tools

Regardless of whether they are made in-house or not, it’s important to have a good set of tools for working with the scripts. Edith provides the developers with:

  1. Live debugging.

  2. Code and data breakpoints.

  3. Edit and continue.

  4. Profiling functionality.

Having a home grown toolset makes it easier to implement these features, which decreases turn around times. It’d be much harder to build these features on top of an existing language.

11) Make the Editor In-Game

The Edith editor is completely in-game. This has the advantage that scripts can be updated in real-time and the changes are visible immediately. This not only helps improve turn-around times, but the behaviors of the objects are also better tweaked.

The disadvantage of this approach is that it relies on a working build. If the build breaks, the editor is no longer functional, and progress on the game stops. Extra precautions must be taken to assure this doesn’t happen.

12) Create Audio After the Animations

In most games, the sound effects are annotated onto the animations. There are markers in the animation data that indicate when a sound should be played.

The Sims apparently uses one sound per animation. Since the timing is important, the audio can only be created after the animation.

Custom Scripting Language

In The Sims 2, the developers decided to implement a custom scripting language, specifically tuned to the kind of game they were making. Compared to using Lua, this has some advantages…

House Sims 1

Screenshot 3: A house in the original Sims game.

13) Use an Interpreted Language

The script data is loaded up and executed by an interpreter, as opposed to natively compiled code that is run on the processor directly. This is an interesting compromise:

  • Speed: It’s much slower to parse at runtime but it avoids the need for compilation during the build.

  • Extensibility: It’s easier to extend an interpreter than to rework a compiler.

  • Interactivity: The interpreter is required to implement edit and continue.

In The Sims, the developers decided to implement only an interpreter (a closer fit to their requirements), rather than maintain a separate compiler as well.

14) Use a Binary File Format for Scripts

The advantage of having a custom interpreted language is that it’s much harder to reverse-engineer. The Sims tries to take this even further by storing the scripts in a binary format, similarly to byte-code rather than providing the whole source code for the scripts to the end user.

15) Optimize the Scripting Language API

The custom language developed by EA/Maxis was specifically tailored for realtime performance. Specifically:

  1. There are no heavy trigonometry functions.

  2. All computations are done relatively to the object in space.

  3. The scripts rely on the engine for the heavy calculations.

  4. There are time limits on the computation of each object per frame.

These restrictions make it easier to guarantee good performance with the cooperative scheduling despite potential problems with isolated scripts.

16) Integrate the Scripts into the Engine Tightly

In many games, the scripting engine serves as a high-level logic that controls actors in the game. However, for The Sims, the scripting functionality is very tightly integrated into the engine itself.

For example, script functions in the objects can be called by the core decision-making system. This allows the actors to take into account the properties of each object according to their own properties (e.g. a toy is only fun if you’re not very old).

17) Implement Error Recovery Mechanisms

When a script crashes or a problem is detected by the interpreter, the object is reset to a known state (e.g. the initial state). This means the game can continue without affecting the player’s experience.

The scripts for the actors are handled similarly. When a problem occurs, the actor is reloaded from the last save, and repositioned in the world.

18) Inherit Scripts Hierarchically

The Sims uses a form of inheritance to help reuse code. This is done according to the file system: the closer towards the root directory the more general the code (e.g. global scripts), and specific objects are in the leaves.

This helps create expansion packs, as different directories may override the base file system. Any game with a Quake-like PAK archiving system supports file system extensions, but this needs to be integrated with the scripting engine.

Character AI

The characters in The Sims game are becoming increasingly sophisticated over the evolution of the franchise, but there are only few details about the inner workings of the AI.

Sims 2 Poster

Image 4: A group of original characters from The Sims 2.

19) Use Response Curves to Model Emotions

A form of fuzzy logic is used to model the emotions of the actors. Each of them have basic feelings measured in the range [-100,100], specifically:

  • Physical: Hunger, comfort, hygiene, and bladder.

  • Emotional: Energy, fun, social, and room.

Each of these are mapped to a mood/happiness value using a response curve. Some functions are linear mappings, e.g. hygiene and energy to happiness, but others are polynomial functions. For example, the more hungry actors get the less they care about anything else!

20) Try Greedy Decision Making

Given these basic feelings, the decision-making part of the AI is responsible for finding objects that fulfill their current needs. This is done based on multiple factors:

  • The actions possible based on the objects that are available.

  • The reward that each action provides to the current actor.

  • The distance of the object to the current location.

  • The level of need felt by the actor at the time.

This can be implemented using a voting system, whereby the most valuable object according to all feelings is used next by the autonomous behaviors.

21) Make Canned Plans

The behavior of the actors in The Sims is very much a by-product of the decision making and the smart objects. As such, they don’t really do much intelligent reasoning. In fact all their plans are “canned,” meaning they are specified manually using scripts.

For example, the process of preparing lunch may require multiple steps, but each of them is specified via a script attached to the fridge. In a sense, there is very little intelligence in the actors themselves, hence the “smart object” terminology!

References

[1] Scripting and Sims2: Coding the Psychology of Little People
Jake Simpson
Game Developer's Conference 2005
Download (PPT, 72Kb)
[2] Under the Hood of the Sims
Kenneth D. Forbus
CS 395 Game Design, 2002

Discussion 6 Comments

Ian Morrison on October 16th, 2007

Interestingly enough, I'm doing a whole lot of that in my current AI. The difference is that it's a 2D platformer/shooter, not a sim game! Please forgive my terminology, since I've never figured out what precisely, my designs would be called. A bunch of different action types are defined for the AIs to use. Each AI is capable of performing certain actions, and contains it's own routines for handling them (for instance, a flying robot would execute a moveTo command much differently from a security guard that must jump from platform to platform). Actions are tagged as relating to either the AI's aiming or it's movement, for semblence of concurrency. Individual objects in the world provide "summaries" to AIs with information on their position, velocity, health, etc. They also include a list of actions that can be executed on them. Each action contains it own functions for evaluating its value, and the highest rated action is always chosen. The catch, though, is that each action generates multiple values for different goals, like dealing damage to the enemy, avoiding damage, and getting in a good tactical position. Basically, these values serve as hand crafted approximations of how an action will satisfy an objective. These values can then be scaled independently based on the AI's personality, resulting in varied behaviour. It also provides a hook for more "traditional" AI, should the individual actor require it. Something as simple as an FSM (or less!) layered on top of this system, changing goal priorities, can result in much more intelligent behaviour. For example, simply scaling the minimizeRisk goal as the AI takes damage results in the AI retreating from battle to recover, as cover further away from the target is now looking much more valuable. Weirdly enough, I was inspired by "AI Game Development" and the presentation on FEAR's AI... I've never even played the Sims! I liked the goal oriented behaviour I saw in FEAR's AI, but I didn't want to go down the STRIPS planner route. Sorry if I went a little off topic!

gware on October 16th, 2007

This type of AI was the subject of my last project at school. We implemented some of the point mentionned (specifically 1,3, 4, 5, 11, 19, 20 and 21), in sim game. We were very pleased by the result since the main agent was behaving in a natural fashion, the game was quite extensible, and we were surprised by some unexpected behaviors. When writing this system our main issues where : 1/ when to update each object (what we were calling an Entity) Most of the time you don't want to update every entity in your world. Example : you may prefer updating objects located near your main agent, but still want (sometimes) "foreign" objects to send signals. 2/ How to build entities hierarchy You may want, at some point, to create layers and hierarchies for your entities. Doing this can be great for design purposes, but may create issues with scheduling since timing won't be per-object anymore. 3/ Callback overuse We made an architecture relying mostly on callback (when an action or other events where triggered we were notifying listeners, i.e. entities were able to register for certain events). This quickly became a bit of an issue : when using callback you are "breaking" scheduling of your update (since a plug in can be triggered anytime, depending on the events). Our solution was do delay "events update" at the begining (or end) of the frame when possible. Plus, at some point, it may create performance issues because compilers cannot optimise callbacks. 4/ Debugging features missing We had debugging and profiling functionnalities built in-game for tuning some variable, but we were still missing lot of functionnalities : the more important being "edit and continue" and a good "object" editor. I believe that a good pipeline makes about 50% of your game (or at least it will help you make 100% of your assets ;). pay attention to the pipe ! :)

Sergio on October 16th, 2007

The smart objects approach is fantastic for a game like The Sims. It provides incredible modularity and takes full advantage of the fact that the game is mostly based on optimizing a few variables the character has exposed. I just have one word of warning. Beware of advertising a system like this to people who don't have a good understanding of what's involved in building a complex AI system. If you say you're going to add a smart object, they will assume that characters will "magically" learn to use it directly, and use it fully and properly. This might be fine for adding a new type of prop for the Sims, but I doubt you can encode all the considerations a character has to make to use cover in the cover point itself. You still need the character to understand when to use cover, as part of its decision-making process. Think of it as adding a prop that the Sims have no need for: they won't listen to its advertisements. On the other hand, smart objects are great for mostly cosmetic systems. All those background characters can be much more interesting if they just walk up to a prop and play some animation. The character doesn't have to understand what's going on if it's just there for colour, although even in this case you still might want to encode some "common sense" rules.

diegix on October 18th, 2007

The system in the Sims is a really good idea that works perfectly for that environment. However, one can easily abuse of an "automatic" system like this. In past experiences with those notification systems I have found that it is very easy to loose track of the interactions that are happening in the system and the combinational explosion can really affect the performance of the system. In a game like the Sims you have a clear distinction between advertisers (objects) and consumers (people), so you don't have any interaction between two advertisers or two consumers. Since the number of consumers in the Sims is not very high and they are just receiving information locally, that shouldn't be a problem. It's really easy to follow the tendency to generalize this system and map the interactions between every entity in the system in the same way, which seems like a good idea, but in my opinion a very dangerous one if you are not very careful and know what you are doing. Since the users of that system are going to be scripters and artist you have to be very careful to make them understand the restrictions and the budget. In summary a very powerful idea that should be handle with care

Ian Morrison on October 18th, 2007

Amen to that. One of the issues I'm currently struggling with is setting up a consistent scale and/or units of measurement for the advertising values. It's not a big issue right now, but if I intend to scale my implementation far past what I already have I'd like to avoid having a monstrous mess of dependant values, and just work with an objective scale.

Ian Morrison on October 26th, 2007

Are you able to share what you did to get around the spikes? I've encountered the same speed issues in my own AI using similar techniques.

If you'd like to add a comment or question on this page, simply log-in to the site. You can create an account from the sign-up page if necessary... It takes less than a minute!