Friday, January 28, 2005

Creating virtual organisms

I'm well aware
that the task of creating realistic simulations of living organisms in software is, well, probably near-impossible.

To give just a minor sampling of the sheer variety of variables to consider, cruise on over to this site.

An organism is really a complex engine that requires constant reorienting in relation to its environment and internal state. A body will attempt to maintain homeostasis. This does not mean that a body will ever be balanced, but rather that its condition will fluctuate constantly as it attempts to reach a balance.

Not only are an organism's internal states, physical and emotional, in constant flux, but so are its reactions to environmental conditions.

Most games will use complex chains of IF-THEN procedures to dictate behavior. For even more detailed behaviors, some games use lookup tables, wherein different reactions fall into different slots in the hierarchy. The problem with such tables is that they tend to be processor-intensive and are therefore the first things to be cut down when a game goes into the optimization step.

Perhaps the most difficult thing about simulating living creatures is the sheer variety of their physical expression. Even members of the same species can operate with different sets of instructions.

When we discuss, for example, a way for a creature to navigate, there are many factors to consider: Is the creature fleeing, fighting, or foraging? What is its position in a herd/pack, and what relation does it maintain to other creatures of similar type? When does herd/pack cohesion break down?

The A* pathfinding algorithm is extremely popular, but why might it be a poor choice for organic movement? A* is used to find the shortest route between a start and end destination. Often animal behaviors require different goals. A deer might flee in a zig-zag pattern, confusing a predator by creating a 'cone' of scent to follow while at the same time increasing its distance and attempting to flee in a line perpendicular to the predator's movement direction. There may be no actual endpoint, but rather a desired distance from a predator and proximity to the herd/pack.

And what of foraging? This requires a search pattern. Let us consider a small herd of ten animals. Each animal is going to attempt to maximize its food intake while minimizing its activity and maintaining proper distance to the herd. Animals must be given both desired maximums and acceptable minimums. But how to set these? When should an animal's instincts take precedence over its loyalty to the herd? So we need instructions for both individual organisms and the herd as a superorganism.

But then we have to take into account internal herd behaviors. Herds maintain a structure, and there are different ways that creatures maintain these structures. Systems of subordinates, bellwethers to determine movement, protective maneuvers. Some animals may have strong family bonds, so a system is needed to decide which orders are relevant when.

One interesting take on building behavior is presented here. Rather than attempt to stack behaviors in a hierarchy, they are treated as discrete units. There is a very precise way of determining priority, and once a meme gains top priority there is a method for it to exercise and relinquish control. I like this system because it is incredibly flexible. New behaviors can be added easily. A picture of an organism can be built up slowly, instead of attempting to quantify everything in one go. This system could even be extended to allow certain memes to transmit to other creatures. Particularly successful memes could alter the behavior of whole species.

Creating convincing organisms is not merely for scientific simulations. Irrational Games is trying to build a functional ecosystem into their upcoming BioShock. Depending on the implementation, the game might provide some truly surprising interactions. Gameplay in such a system requires the player to observe behavior and utilize it to achieve objectives.

This seems no different from most games, which require players to find patterns in enemies. But patterns are limited. Let's assume a stealth game. Your objective is, obviously, to stay undetected. In most games of this type, if your character is seen the guards will go into an active mode. You must find a place where the game marks you as hidden long enough for the guards to go back to search mode. In a more realistic game, there would be differences between guards actively searching due to suspicion (you make a noise) and observation (they see you).

Suspicious guards would stay on alert long enough to investigate. If nothing is forthcoming, they return to post. Once you are observed, there will be different tiers of effects. An active search is conducted. If you are not discovered, then maybe guard levels will be increased, or their search patterns will be more thorough, or secondary defenses are activated. Sections can be go on lockdown. Rather than limiting your character's progress, a backup defense system can force the player into an alternate playstyle. A game like this would need lots of playtesting to balance, though.

No comments: