supported. Adding a new situation should not re-
quire a modification to code of any NPC.
• Synchronization of joint behaviors among agents
(a pub brawl, a game of cards, ...) must be sup-
ported.
• The NPCs retain their own AI which coexists with
the SAs. Smooth transition of control between
NPC AI and SA must be ensured.
3.2 Behavior Adoption and Drop
To ensure that gameplay-critical behavior remains un-
interrupted, we have decided that active NPC cooper-
ation is required to receive a behavior from a SA. We
have thus added a new BT node that requests a behav-
ior from SA, if possible (further referred to as request
node). This leaves the NPC AI in full control over
transition to SA-controlled behavior and the SA be-
havior may be forcefully terminated by activating a
different BT branch.
When the request node is updated and it does not
hold a behavior already, it asks for one. Both the node
and the SA take part in deciding, which behavior is
applicable. Consider an example: an NPC asks for a
behavior in a pub. Out of all of its behavior templates,
the SA filters those with satisfied constraints on the
requesting NPC (e.g., a template that involves invit-
ing everybody for drink is available only to NPCs that
are marked as rich). The request node then chooses
among those templates based on its parameters (e.g.,
it may prefer “get-drunk” template to “eat” or forbid
“play-cards” template). The same behavior may be
implemented in several SAs — the NPC may require
a “relax” behavior, which would have different imple-
mentations on a beach or in a pub.
If an applicable template is found, it is instantiated
in a data structure called behavior tag which is passed
to the request node. The behavior tag contains meta-
data about the behavior and an instance of a BT that
achieves the behavior. The BT is pasted as the only
child of the request node and the tree continues exe-
cution by evaluating the behavior subtree. If needed,
new message inboxes are added to the NPC. If no ap-
plicable behavior is found, the request node fails.
When the NPC leaves the SA that has provided
the tag, or the associated BT succeeds/fails there are
three possibilities: the behavior may be kept by the
request node as if nothing happened, it may be kept
but marked as overridable or it may be dropped. The
actual outcome is determined by the tag, by default
the behavior is marked as overridable when it suc-
ceeds and dropped upon leave/failure. If the behavior
is marked as overridable, the request node asks for a
behavior upon its update but if no applicable behavior
is found it continues executing the current one instead
of failing. This allows some behaviors to persist their
state between succesive executions or upon leaving
the SA.
3.3 Smart Area’s Brain
The basic decision making of the SA is passive: for
each behavior template, the SA maintains informa-
tion whether new instances of the template may be
requested and the maximum number of instances that
may be adopted at the same time. This information is
used upon request processing.
Some SAs have an active brain — a behavior tree
that gets updated regularly and may either modify
the passive decision making based on external con-
ditions (e.g., disable “drinking” behavior in a pub if
no innkeeper is present) or it may perform some coor-
dination among behavior tag holders inside the area
(e.g., instruct a pair of customers to play cards to-
gether). The coordination is done by message passing
between the SA and the tag holders. Since the NPCs
are nowcontrolled by BTs providedby the SA, the SA
can make strong assumptions about NPCs responses
to its messages.
In many scenarios, the SA needs to perform some
action whenever an NPC adopts/drops a behavior
(e.g., assign a free seat to a customer in a pub) or
when an NPC enters/leaves the area (e.g., innkeeper
says goodbye to the leaving guest). To streamline the
development in such scenarios and to make the BTs
of the SA brain and the behaviors more readable for
developers, we have introduced event handlers to the
SA brain. An event handler is simply a BT that is ex-
ecuted until completion for each instance of an event.
So far, our system uses four events: OnAdopt — an
NPC adopts a behavior, OnDrop — an NPC drops a
behavior, OnEnter — an NPC enters the area, OnExit
— an NPC leaves the area. The latter two fire whether
the NPC has requested a behavior or not.
The event handler trees are queued and executed
one at a time and may not be interrupted by execu-
tion of the main tree. This reduces the parallelism
of the main tree and the event trees to a bare mini-
mum and thus facilitates easier debugging. It is up
to the scripters to make sure the handler trees execute
quickly.
3.4 Smart Area Hierarchy
One of our primary use cases of SAs was the day-
to-day behavior of NPCs. Now, once an NPC enters
a pub for example, everything works well. But how
does the NPC know, where a pub is? As mentioned in
SmartAreas-AModularApproachtoSimulationofDailyLifeinanOpenWorldVideoGame
705