Skip to content

Building with Behaviors and AI

Rocky Bevins edited this page Nov 28, 2016 · 15 revisions

AI scripts are loaded into memory when the server boots. Game entities reference these scripts via entries in their behaviors[] property. This allows for AI scripts to be used across entities while providing a consistent implementation path.

Along with the generic behaviors game entities can have functions directly attached to them -- within their respective area files.

All behaviors are in /ai.

Lets get to an example.

##Using core Behaviors## We will define a new mob. For now let it walk around an area and attack any encountered players.

In this case we will be using both the wander and the aggie behaviors.

// id property is mandatory. Otherwise all objects in a rooms. 
// monsters[] array are extended with the entity.json template
{
    name: 'Bugbear',
    id: '10',
    behaviors: [{module: 'wander'}, {module: 'aggie'}]
}

Now the mob will wander around, contained to its area, and attack whatever it encounters. Attacks are triggered in one of two ways: when something enters the room the mob is in (which triggers onVisit() in aggie.js) and on the onAlive() tick -- which is going to check the room for any players.

We can change settings by toggling one of the behaviors properties. For example if we wanted our Bugbear to only attack creatures who walk in on it, omitting people who teleport/recall/etc to the room we would make the mob object look like the following:

{
    name: 'Bugbear',
    id: '10',
    attackOnAlive: false,
    behaviors: [{module: 'wander'}, {module: 'aggie'}]
}

Switching the attackOnAlive property (which was put onto the entity when loaded) to false will omit the onAlive aggie check. Meaning the mob will now only attack players that walk into the room -- e.g a portal or something would prevent the attack.

Events of any type can always be avoided by toggling prevent[eventName] = true. In this case mob.preventOnAlive = true. We don't want to do that here because it would prevent our wander behavior from firing.

Some other options provided by ai/aggie.js:

attackOnVisit: true, // fire onVisit
attackOnAlive: true, // fire onAlive
mobAggressive: false, // can attack mobs
onlyAttackLarger: false, // will only attack things of larger size
onlyAttackSmaller: false, // will only attack things of smaller size
revenge: false // only aggie against whatever attacked the mob last
Clone this wiki locally