jschomay/elm-narrative-engine/4.0.0

NOTE: the latest version is 6.0.3

Narrative.WorldModel

See how the world model is defined in the full working example.

Types

type alias ID =
String

A unique identifier for each entity.

type alias WorldModel a =
Dict ID (NarrativeComponent a)

Your story/game will have a store, or "world model", of all of the entities that live in your world, and the various properties that describe them. The narrative engine expects your world model to have this shape.

type alias NarrativeComponent a =
{ a | tags : Tags, stats : Stats, links : Links }

Entities are just IDs coupled with various fields of information. The engine requires that your entities have tags, stats, and links fields.

It uses these fields to track "salience-based" and "quality/stats-based" narratives (described very well in Emily Shot's blog post), which can provide a more flexible and robust story.

Because this is an extensible record, you can have other properties on your entities as well (like "name" and "description" or "sprite" for example), which works well with the "Entity Component System" design pattern.

Note that you are not restricted to the traditional "items/characters/locations" world model. You can define your entities with what ever properties you want, to fit any story world.

type alias Tags =
Set String

"Tags" on an entity.

Examples: "item", "edible", "poisonous", "SmithFamily", etc.

type alias Stats =
Dict String Int

"Stats" on an entity.

Examples: "health", "honor", "plotProgression", "bars of gold", etc.

Creating entities

emptyTags : Tags

A empty starting state for the "tags" property

emptyStats : Stats

A empty starting state for the "stats" property

tag :
String
-> ( ID, NarrativeComponent a )
-> ( ID, NarrativeComponent a )

A helper function to add a tag to an entity when setting up your world model.

String
-> Int
-> ( ID, NarrativeComponent a )
-> ( ID, NarrativeComponent a )

A helper function to add a stat to an entity when setting up your world model. A stat is a key and a numeric value on any scale you like.

Updating entities

= Update ID (List ChangeEntity)
| UpdateAll (List Query) (List ChangeEntity)

Declarative statements of how an entity should change, designed to be used with rules.

Note that you can use $ as the ID to reference the entity ID that triggered the rule (useful for generic rules).

= AddTag String
| RemoveTag String
| SetStat String Int
| IncStat String Int
| DecStat String Int
| SetLink String ID

Declarative statements for changing a property on an entity.

Note that you can use $ as the ID in SetLink to reference the entity ID that triggered the rule (useful for generic rules).

applyChanges : List ChangeWorld -> ID -> WorldModel a -> WorldModel a

Update the world model based on a list of changes. Also takes the id of the entity that triggered the rule to allow changes to use trigger matching (with $).

Querying the world model

Queries are run against the world model to assert a condition or select particular entities. This is useful to render a list of characters in a given location for example. The engine uses these when checking rules.

= Match ID (List Query)
| MatchAny (List Query)

Semantic means for matching entities. Specifies an optional entity ID and a list of queries to match against.

Note that you can use $ as the ID to reference the entity ID that triggered the rule (useful for generic rules).

type Query
= HasTag String
| HasStat String Order Int
| HasLink String EntityMatcher
| Not Query

Semantic queries for checking properties of an entity.

query : List Query -> WorldModel a -> List ( ID, NarrativeComponent a )

A way to retrieve information from the store.

assert : ID -> List Query -> WorldModel a -> Bool

Asserts if the current state of the world model matches the given queries.

assertMatch : EntityMatcher -> WorldModel a -> ID -> Bool

A utility to test an EntityMatcher matcher against a specific entity ID.

getStat : ID -> String -> WorldModel a -> Maybe Int

Get a specific stat from a specific entity.