Conditions
Every kind of condition, a test of the state written as data, and the functions that check one and explain why it fails.
A condition is a test of the state, written as plain data, that says when something applies: a line of prose, a choice, a lock or a character's schedule. Content writes one in a when, a blocked entry or a gate, and the content checker checks every id in it. State, commands and the view explains why conditions are data, and Find out why a choice doesn't show puts explain to work.
In the showcase: examples/showcase/src/story/, whose scenes gate their blocks and choices on conditions.
import { holds, inRange, explain, explainText, type Condition, type Explanation } from '@ifloom/engine'Condition
A test of the state that holds or doesn't, told apart by its one key, such as { stat: 'coins', gte: 3 }. Every form is in Condition.
A condition names what it reads by its noun (stat, entity, storyline), with a Range or an is beside it. A condition that names nothing, such as turn, takes its range as its value, as in { turn: { gte: 3 } }. A plugin's condition or the game's own is a call, whose one key is its name.
Effects use the same keys, so { kv: 'met' } asks about the key that { kv: 'met', to: true } sets. The field decides which one an object is, a condition in when and an effect in effects.
An object with two of the engine's keys, such as { kv: 'x', stat: 'coins' }, never holds, since the engine can't tell which was meant. One with no key it knows, such as {} or { at: ['cafe'] }, never holds either. The content checker reports both, so content joins two tests with all.
This condition holds for a regular at the café: at the café, with four coins, after meeting Ava, and owing her nothing.
import type { Condition } from '@ifloom/engine'
import { places } from '@ifloom/plugin-places'
export const regularAtTheCafe: Condition = {
all: [places.at('cafe'), { stat: 'coins', gte: 4 }, { seen: 'ava.hi' }, { not: { kv: 'owesAva' } }],
}These fields take a condition:
- A scene's blocks (
when), its choices (when,blockedandlabels) and avariantsblock'sgate. SeeDataScene. - An action's
whenandblocked. - A lock's, a trigger's and an input's
when. - The game's
finishedandsaveable. SeeGame. - A plugin's content, such as a location's
blockedin the places plugin and a character'sschedulein the cast, with'$'for that character.
Explanation
A condition walked part by part, as explain answers it, with whether each part holds and why. Every field is in Explanation.
holds says whether this part holds, and why is one line saying what it read and what it wanted. parts holds the parts of an all, any or not, each an Explanation of its own. explainText prints the tree.
Conditions
Each kind of condition the engine keeps, by the key that tells it apart.
all, any, not
all holds when every condition in its list holds, any when at least one does, and not when its one condition doesn't. { all: [] } is true and { any: [] } is false.
{ all: [{ kv: 'marketDay' }, { owned: 'clothing:scarf' }] }
{ any: [{ owned: 'clothing:scarf' }, { kv: 'hasRaincoat' }] }
{ not: { kv: 'phase', is: 'arrival' } }kv
Tests a key in the kv store. The test depends on the fields beside it:
istests strict equality, so1and'1'differ, and a list holds when the key equals any of its values. A key nobody has set counts as 0, sois: 0holds for it andis: falsedoesn't. A range besideisis ignored.- A range tests the key's number. A value that isn't a number (unset, text or
true) counts as 0. - Neither tests whether the key is truthy.
0,''andfalsearen't.
{ kv: 'marketOpen' } // set to anything truthy
{ kv: 'errand', is: 'bread' } // exactly this value
{ kv: 'phase', is: ['arrival', 'open'] } // one of these values
{ kv: 'coffeesBought', gte: 3 } // a number, 3 or moreWith of, it tests another kv store with the same three tests. of names an entity ('staff:ana', or '$' inside an each or a count's where), a storyline ('storyline:ava'), or the minigame being played ('minigame'). A 'minigame' test is false with no minigame running, and the checker reports a key no minigame declares.
{ kv: 'role', of: 'staff:ana', is: 'cook' } // an entity's
{ kv: 'visits', of: 'storyline:ava', gte: 3 } // a storyline's
{ kv: 'score', of: 'minigame', gte: 3 } // the running minigame'sAn entity that isn't there fails every test, lt and is included, so { kv: 'warned', of: 'staff:ghost', lt: 1 } is false. { not: { kv: 'warned', of: 'staff:ghost' } } is true for a missing entity.
phase
The stretch of the game the player is in is the kv key phase, read as any key is:
{ kv: 'phase', is: ['arrival', 'first-day'] }value
Tests a value by its full name, a plugin's ('inventory/count') or the game's ('game/breadPrice'), asked with its arguments. It uses the same three tests as kv, so content reads a plugin's data the way it reads a variable, while only the plugin's own effects change it:
{ value: 'messages/unread', with: { channel: 'texts' } } // a number other than 0, or text other than ''
{ value: 'game/weather', is: ['rain', 'fog'] } // one of these values
{ value: 'inventory/count', with: { entity: 'food:coffee' }, gte: 1 }Text counts as 0 in a range. A value nobody declares fails every test, is and lt included, and the checker reports its name.
setting
Tests a play setting, the value in state.settings, with the same three tests as kv. A ui setting isn't in the state, so the checker reports a condition that names one, an unknown setting, and an is the setting can't take.
{ setting: 'hints' } // a toggle that is on
{ setting: 'difficulty', is: 'hard' } // exactly this option
{ setting: 'patience', gte: 6 } // 6 or morechose
The number of times the player has chosen a choice, keyed by the scene's id, a # and the choice's id, against a range. A bare { chose } means at least once. The engine counts every choice in chosen, so a later scene can branch on an earlier decision without a kv key. The checker checks the key against the scene's choices.
{ chose: 'ava.hi#compliment' } // chosen at least once
{ chose: 'ava.hi#compliment', gte: 3 } // three times or more
{ chose: 'ava.hi#compliment', lt: 1 } // neverseen
The number of times the player has entered a scene, from seen, against a range. A bare { seen } means at least once. It gates on order, since it says the step before has happened.
{ seen: 'ava.river' } // entered at least once
{ seen: 'ava.hi', gte: 3 } // three times or more
{ seen: 'ava.river', lt: 1 } // neversince
Whole turns since a scene was first entered, or since a choice was first chosen when the id is scene#choice, against a range. With last: true, it counts from the latest time instead, which makes a cooldown. A bare { since } means at least one turn, so it's false in the same turn.
A scene not yet seen, or a choice not yet chosen, fails every range, lt included. So a cooldown is a blocked entry that holds while it's too soon. This bench can be visited again three turns after the last visit:
{
id: 'sit-by-the-river',
blocked: [{ when: { since: 'river.bench', last: true, lt: 3 }, reason: 'You sat there only recently.' }],
effects: [{ scene: 'river.bench' }],
}stat
Tests a stat against a range. It reads the stat with statValue, so a derived stat is worked out and a stat the save lacks reads as its start.
With of, it tests an entity's stat: an EntityRef, '$' inside an each or a count's where, 'you' for the protagonist or 'player'. An entity that isn't there fails every range, lt included, and so does '$' where nothing binds it.
{ stat: 'charm', gte: 6 }
{ stat: 'energy', lt: 20 } // flagging
{ stat: 'friendship', of: 'character:ava', gte: 20 }
{ stat: 'morale', of: '$', lt: 20 } // in an eachcount
The number of entities of a kind, against a range. A bare { count } means at least one, and a kind with none, or one the game doesn't declare, has 0. With where, it counts the entities where holds for, asking it once for each with '$' as that entity.
In place of a kind, count takes a Select of a kind, a tag, a trait and a where, and counts distinct entities. where: { owned: '$' } counts the sorts of thing someone has. How many of one thing they have is owned. A count binds its own '$', so inside an each's where, a count's '$' is the entity being counted.
{ count: 'staff', lt: 4 } // room for one more
{ count: 'staff', where: { stat: 'morale', of: '$', gte: 75 }, gte: 2 } // two happy people
{ count: { tag: 'fruit', where: { owned: '$' } }, gte: 3 } // three sorts of fruit on you
{ count: { of: 'food', where: { owned: '$', of: 'character:ava' } }, lt: 4 } // Ava has fewer than four sorts of foodentity
Holds while an entity is in the game, declared or created and not removed. Whether someone has it is owned.
The checker reports a kind the game doesn't declare, and not an unknown id, since the entity may be made in play. Inside an each or a count's where, '$' is bound here, so { entity: '$' } is always true there and false anywhere else.
{ entity: 'staff:ana' } // Ana works hereowned
How many of a thing someone has, against a range, added up across the game's receivers, the plugins that keep things. A bare { owned } means at least one. It's the one question "do they have it", whether the inventory, the wardrobe or another plugin keeps the thing.
of names whose, someone: 'player', 'you' or a unique entity. Each receiver reads its own default when it's left out, and the first-party ones count the protagonist being played and then the player's own things. via asks one receiver only.
{ owned: 'item:sandwich' } // you have a sandwich
{ owned: 'food:coffee', gte: 2 } // two coffees
{ owned: 'gift:scarf', of: 'character:ava' } // Ava has the scarf
{ owned: 'clothing:shirt', via: 'wardrobe' } // in the wardrobe, not carriedIn a game with no receiver it's always 0 and the checker reports it. A Twine-style game asks a kv key instead, such as { kv: 'hasSandwich' }.
level
The level a stat with levels has reached, against a range, as statLevel works it out. A bare { level } means level 1 or more. Hidden levels read the same way. With of, it reads an entity's level, as stat does.
A stat with no levels is always at level 0, so { level: 'charm' } never holds on it. The checker checks that the stat is declared on the right owner, and not that it has levels.
{ level: 'cooking', gte: 2 } // level 2 or more
{ level: 'skill', of: 'staff:ana', lt: 3 } // an entity's, not yet 3storyline
Holds while a storyline is open, meaning not closed or closed until a turn that has come, as storylineOpen says. open: false asks whether it's closed for now. A storyline the save lacks reads as open, and the checker reports one the game doesn't declare.
{ storyline: 'ava' } // open
{ storyline: 'ava', open: false } // closed for nowroll
The latest roll made under a key. Bare, it asks whether one has been made. passed asks whether the total reached the roll's against, and is false either way for a roll with no target. critical: true asks whether every die showed its top face or every die showed 1, and 'max' or 'min' asks for one of them.
The total is a kv key, so { kv: 'skim', gte: 10 } tests the number. A roll is kept until another roll under the same key replaces it, across turns and saves.
{ roll: 'skim', passed: true } // it reached its target
{ roll: 'climb', critical: 'max' } // every die on its top facetimer
Holds while one of the game's timers is running, or with a range, running with that many milliseconds left. It's false for a timer that isn't running, whatever the range. Time passes only on a tick, so a trigger on the time left fires after the tick that crosses it. The checker reports a timer the game doesn't declare.
{ timer: 'phone' } // running
{ timer: 'phone', lte: 3000 } // running, with 3 seconds or less leftscene
Holds while the scene on screen is one of a list. It's false when no scene is open.
{ scene: ['ava.hi'] }turn
Tests the number of turns passed, from 0, against a range given as its value. { turn: {} } is false in turn 0. The time of day, the weekday and the day are the calendar plugin's conditions, such as calendar.period('morning').
{ turn: { gte: 3 } } // the fourth turn onConditionCall
A condition of a plugin's or of the game's own, written as its full name with its arguments as the value, or true for none. Every form is in ConditionCall, and PluginCall describes the short form.
The game's own conditions are in its rules, called with game/ in front, and a plugin's with the plugin's id in front. A plugin's helpers write its calls, such as places.at('cafe') for { 'places/at': { location: ['cafe'] } } and cast.here('ava') for Ava being here. A name nobody declares is false rather than an error, so a broken mod hides content instead of crashing, and the checker reports it.
import type { GameRules } from '@ifloom/engine'
export const rules: GameRules = {
conditions: {
marketDay: (state) => state.turn % 7 === 5,
},
}Content then writes { 'game/marketDay': true }. Write your own condition, effect or macro covers the rest.
Functions
holds
Returns whether a condition holds in a state. Its signature is in holds.
undefined holds, so a when left out always applies. The state comes first and the game second, as in every function that reads the game in play.
import { holds } from '@ifloom/engine'
import { places } from '@ifloom/plugin-places'
holds(state, game, { kv: 'metAva' }) // true once you've met Ava
holds(state, game, places.at('cafe')) // a plugin's conditioninRange
Returns whether a number is in a Range, tested as every built-in condition tests one. Its signature is in inRange.
A plugin's condition that takes a range in its arguments calls it, so it reads as the built-in ones do. The engine works out the bounds at the top of a call's arguments before it asks the plugin. inRange works out a range deeper in, reading each Amount bound in the state and game given. A bound that isn't an amount reads as 0.
import { inRange, type Range } from '@ifloom/engine'
import { definePlugin } from '@ifloom/engine/plugin'
// { 'dishes/dirty': { gte: 3 } }, or { 'dishes/dirty': true } for any at all.
export const dishes = definePlugin<{ dirty: number }>({
id: 'dishes',
version: 1,
start: () => ({ dirty: 0 }),
conditions: {
dirty: (state, args, { slice, game }) => inRange(state, game, slice.dirty, args as Range),
},
})explain
Returns a condition walked part by part against a state, as an Explanation. Its signature is in explain.
Each part of an all, any or not says whether it holds, and each leaf says what it read and what it wanted, such as stat coins is 3; wants 5 or more. The top holds always agrees with holds. With no condition, it answers { holds: true, why: 'no condition' }. A call says only which function it called, such as custom condition game/marketDay, and not what that function read.
import { explain } from '@ifloom/engine'
import { places } from '@ifloom/plugin-places'
const why = explain(state, game, {
all: [{ kv: 'metAva' }, { any: [{ stat: 'coins', gte: 5 }, { level: 'charm', gte: 1 }] }, { not: places.at('cafe') }],
})
why.holds // false
why.parts?.[1].parts?.[0].why // 'stat coins is 3; wants 5 or more'explainText
Returns an Explanation as indented lines, with ✓ or ✗ on each, for a log, a test's failure message or a debug panel. Its signature is in explainText. depth indents the whole tree, two spaces per level, from 0.
import { explainText } from '@ifloom/engine'
console.log(explainText(why))
// ✗ all of 3: 1 fail
// ✓ kv metAva is true; wants set
// ✓ any of 2: 1 hold
// ✗ stat coins is 3; wants 5 or more
// ✓ stat charm is at level 1; wants 1 or more
// ✗ not: the part below holds
// ✓ custom condition places/atChecks
The content checker checks every condition content writes:
| Problem | Cause |
|---|---|
a condition asks one thing, but this one has 2 keys that each say what it asks: kv, stat | One object holds two of the engine's keys. |
a condition needs one key that says what it asks, such as kv or stat, or a call by name; this one has only "at" | The object holds no key the engine knows. |
unknown stat "gold", unknown storyline "nobody" | The condition names a stat, storyline, scene, timer or setting the game doesn't declare. |
no game condition named "marketDay"; the game's own code is called with "game/" in front, as "game/marketDay" | A call's name has no game/ or plugin id in front. |
no plugin of the game keeps things, so this does nothing: … | An owned in a game with no receiver. |
Caveats
- A test on something missing fails every range,
ltincluded: an entity that isn't there, a scene not seen insince, and a timer not running.notaround a bare test asks the opposite. explainandholdsneed the game, or every call fails andlevelreads level 0.
Last updated on