Early development: IFLoom can change until 1.0. What's stable
IFLoom
First-party plugins

Calendar

The calendar plugin gives a game days, weekdays and periods of the day, a bed and a curfew, and sets how long a turn is.

@ifloom/plugin-calendar gives a game days, weekdays and periods of the day, a bed to go to and an optional curfew. It also sets how long a turn is: one period, one day or one week. The calendar needs no other plugin, and the places and cast plugins read its conditions for opening hours and schedules. Turns and time explains how the calendar's clock and the engine's turn keep step, Add a plugin installs it, and Set up days and times of day builds a day with it.

In the showcase: examples/showcase/src/game.ts, which lists calendar({ start: { day: 0, period: 'night' } }) first among its plugins.

import { calendar, type CalendarOptions, type CalendarState, type CalendarView, type PeriodDef } from '@ifloom/plugin-calendar'

calendar

The plugin's factory, which returns the plugin with the id calendar. Every field of its result is in calendar, and its helpers are listed under Functions.

With no options, a day has five periods, a week has seven days, a turn is a day, and a new game starts on day 0 in the morning. List the calendar first in the game's plugins. The engine runs plugins' onTurn hooks in the order plugins lists them, and the calendar's hook moves the clock, so plugins listed after it see the new day.

The calendar's onTurn hook runs on every turn, however the turn ended. It moves the clock to the start of the next unit of turn: the next period, the first period of the next day, or the first period of the next week counted from start.day. With turn: false the clock stays put. The game's rules.onTurn runs before every plugin's onTurn, so it sees the clock as the turn ended, and rules.onTurnDone sees the new day.

read and write on the plugin come from DefinedPlugin. write puts the clock somewhere without ending a turn, for tests and tools.

import { loadGame, startingState, type Content } from '@ifloom/engine'
import { calendar } from '@ifloom/plugin-calendar'

declare const town: Content

const clock = calendar()
const { game } = loadGame([town], { id: 'small-town', title: 'Small Town', plugins: [clock] })

clock.read(startingState(game), game) // { day: 0, period: 'morning' }

CalendarOptions

The calendar's settings, all optional. Every field is in CalendarOptions. The options are code, so a mod can't change them.

FieldMeaning
turnThe length of a turn, 'period', 'day' or 'week', and 'day' if left out. A week turn starts each time the clock enters a new block of week days counted from start.day. With false, passing time ends no turn and a turn ends without moving the clock.
sleepThe settings of the calendar/sleep command. from lists the periods the player can go to bed in, the last three if left out, and wake is the period the next day starts in, the first if left out. With false, the calendar has no sleep command.
curfewA condition. While it holds, the calendar refuses a command that would pass time over the end of the day (see calendar/pass). The curfew always binds if left out, and never with false.
weekendThe days of the week that are the weekend, counting from 0, and the last two days of the week if left out.

A curfew the player can lift reads a kv key:

import { calendar } from '@ifloom/plugin-calendar'

export const clock = calendar({ turn: 'week', curfew: { kv: 'nightOwl', is: false } })

PeriodDef

A period of the day, one entry of the periods option. Every field is in PeriodDef.

A bare string in periods is a period with only an id. The label is the default for the string calendar/period.<id>, so a game or a translation can still reword it (see Strings). Period ids are unique, and the calendar reports two that share one.

import { calendar } from '@ifloom/plugin-calendar'

export const clock = calendar({ periods: ['dawn', 'day', { id: 'dusk', label: 'after dark' }] })

Conditions

Each condition is a plugin call, written by the helper beside it. A calendar condition works anywhere a condition goes, such as a block's when, a blocked entry, a lock or a character's schedule.

calendar/period

Holds in one of the named periods.

{ 'calendar/period': { period: ['evening', 'night'] } } // calendar.period('evening', 'night')

calendar/weekday

Holds on one of the named days of the week, counting from 0. In a seven-day week, 0 is Monday.

{ 'calendar/weekday': { day: [0, 1, 2, 3, 4] } } // calendar.weekday(0, 1, 2, 3, 4)

calendar/weekend

Holds on one of the weekend days.

{ 'calendar/weekend': true } // calendar.weekend()

calendar/day

Holds while the day, counting from 0, is in the range. The player reads the day from 1, so calendar.day({ gte: 6 }) holds from the day the header calls Day 7.

{ 'calendar/day': { gte: 6 } } // calendar.day({ gte: 6 })

calendar/curfew

Holds while the curfew binds. It has no helper.

{ 'calendar/curfew': true }

Effects

calendar/pass

Passes that many periods, 1 if by is left out, and ends a turn at each boundary of turn it crosses. The calendar counts the boundaries, ends that many turns as one turn effect with by, then puts the clock exactly where it was sent. A count that isn't a number, or is negative, passes nothing, and a fraction is rounded down.

{ 'calendar/pass': { by: 2 } } // calendar.pass(2)

With the default turn: 'day' and five periods, a pass ends one turn for each new day it reaches:

  • From day 0 in the morning, calendar.pass(2) lands on day 0 in the evening and ends no turn.
  • From day 0 at night, calendar.pass(4) lands on day 1 in the evening and ends one turn.
  • From day 1 in the evening, calendar.pass(11) lands on day 3 at night and ends two turns.

A button whose effects pass time carries a note in its price.notes saying where the clock will be: until evening the same day, until tomorrow morning the next, and until Wednesday afternoon further on.

While the curfew binds, the calendar's guard refuses a command whose passes, from effectsOf, add up past the day's last period. The refusal is soft, That would run past dawn., so the button stays pressable and carries the line as its warning, and the engine refuses it all the same. At night, calendar.pass(1) is allowed and calendar.pass(3) isn't. The guard never refuses calendar/sleep.

A lock with effect set to 'calendar/pass' stops every command that passes time, bed included. It doesn't stop a { turn: true } effect, and effect: 'turn' doesn't stop calendar/pass, because the turns a pass ends are applied inside it.

import type { Lock } from '@ifloom/engine/plugin'

export const locks: Lock[] = [{ when: { kv: 'ill' }, deny: ['*'], effect: 'calendar/pass', reason: 'You feel too ill.' }]

calendar/set

Puts the clock on a day, a period or both, and ends no turn. A field left out stays as it is, and a period the calendar doesn't have leaves the period unchanged. It has no helper. The debug plugin's debug/time command sets the clock through it.

{ 'calendar/set': { day: 3, period: 'evening' } }

Commands

calendar/sleep

Goes to bed. It passes time to the wake period of the next day and ends turns as calendar/pass does. Its effectsOf is that pass, so the bed button carries the price until tomorrow morning, and a lock on calendar/pass stops it too.

{ 'calendar/sleep': true } // calendar.sleep()

The calendar refuses it, in this order:

  1. With There is no such thing to do. (noSuchAction) when the calendar was built with sleep: false. The content checker also reports content that names it.
  2. With Finish this first. (finishFirst) while a scene is open.
  3. With Too early. (calendar/tooEarly) in a period that isn't in sleep.from.

A lock names the command calendar/sleep, or calendar/*.

Queries

Other plugins ask these through their context's query, and content can't. The calendar declares all three in its QueryRegistry, so they're typed in a game that imports it. A plugin that doesn't import it gives the answer's type, as in Queries and partners.

KeyMeaning
calendar/nowThe clock as a copy of CalendarState, { day, period }.
calendar/periodsThe period ids, in order.
calendar/bedtimetrue in a period calendar/sleep can be used from, and always false with sleep: false.

A market-stall plugin shuts at night when the game has a calendar, and stays open when it hasn't:

import { definePlugin } from '@ifloom/engine/plugin'

export const stall = definePlugin<{ stock: number }>({
  id: 'stall',
  version: 1,
  start: () => ({ stock: 5 }),
  conditions: {
    open: (_state, _args, { query }) => {
      const now = query<{ day: number; period: string }>('calendar/now')
      return now ? now.period !== 'night' && now.period !== 'late' : true
    },
  },
})

Values

Prose prints these as tokens.

KeyMeaning
{{calendar/day}}The day as the player counts it, from 1.
{{calendar/weekday}}The weekday's name, such as Monday.
{{calendar/period}}The period's word, such as morning, or its label.

On a new game, Day {{calendar/day}}, a {{calendar/weekday}} {{calendar/period}}. prints Day 1, a Monday morning.

State

CalendarState

The clock a save keeps at state.plugins.calendar, the day from 0 and the period's id. Every field is in CalendarState.

The save keeps only the clock, and the calendar reads its periods and week from its options. A UI reads CalendarView instead.

View

CalendarView

The calendar as the player sees it, in view.plugins.calendar. Every field is in CalendarView.

day counts from 1, as the player does. label is the whole line, Day 3 · Wednesday morning, from the calendar/label string. curfew is true in the day's last period while the curfew binds and no scene is open, which is a UI's cue to offer bed.

The calendar's label provider gives the same line to view.when, ahead of the labels of plugins listed after it. With places listed second, view.when reads Day 1 · Monday morning · High Street.

{ day: 1, weekday: 'Monday', weekdayIndex: 0, period: 'morning', periodLabel: 'morning', label: 'Day 1 · Monday morning', curfew: false }

Render the clock renders it in Plain JS, React and Vue.

React UI

The React UI is optional: the @ifloom/plugin-calendar/react subpath renders the calendar for a game that uses @ifloom/react, and a game rendered another way never loads it. Show plugins in the React UI adds it to a game.

calendarUi

The calendar's PluginUi, listed in mountGame's plugins. It renders CalendarClock in place of the React UI's own Clock. Its type is in calendarUi.

CalendarClock

Renders view.when as the React UI's Clock does, in the side column and in the top bar on a phone. Its props are in CalendarClock.

It sets data-day, data-weekday, data-period, and data-curfew while the curfew binds, so a stylesheet can colour the night. A game replaces it as CalendarClock in components. Its part is clock.

[data-part="clock"][data-period="night"] { color: var(--primary); }

useCalendar

Returns the calendar's CalendarView from the running game, or null in a game without the plugin. It re-renders when the view changes. Its signature is in useCalendar.

import { useCalendar } from '@ifloom/plugin-calendar/react'

export function Weekday() {
  const calendar = useCalendar()
  return calendar ? <span>{calendar.weekday}</span> : null
}

Functions

The helpers are properties of calendar and write its calls, typed:

  • calendar.sleep() returns the calendar/sleep command as a CommandRef.
  • calendar.pass(n) returns the calendar/pass effect, one period if n is left out.
  • calendar.period(...ids), calendar.weekday(...days), calendar.weekend() and calendar.day(range) return the conditions.
import type { ActionDef } from '@ifloom/engine'
import { calendar } from '@ifloom/plugin-calendar'

export const jog: ActionDef = { id: 'jog', label: 'Go for a jog', when: calendar.period('morning', 'evening'), effects: [calendar.pass()] }

Checks

When the game loads, the calendar reports problems with its options, each on base with where as options <option>:

ProblemCause
options periods · a calendar needs at least one periodperiods is empty.
options periods · two periods share an idTwo periods have the same id.
options start · no period called "noon"start.period names a period the calendar doesn't have.
options sleep · no period called "pm"sleep.wake or an entry of sleep.from names a period the calendar doesn't have.
options weekend · no day 9 in a week of 7A weekend day is outside the week.
options curfew · unknown kind "pet"The curfew condition names something the game doesn't have.

Strings

A game, a pack or a translation rewords these by their full key in its strings. {day} counts from 1, and {weekday} and {period} are the player's words. calendar/sleep also uses the engine's finishFirst.

KeyMeaning
calendar/tooEarlyToo early., the refusal of bed outside sleep.from.
calendar/pastDawnThat would run past dawn., the curfew's refusal.
calendar/untiluntil {period}, the price note for a pass that ends the same day.
calendar/tomorrowuntil tomorrow {period}, the price note for a pass that ends the next day.
calendar/lateruntil {weekday} {period}, the price note for a longer pass.
calendar/labelDay {day} · {weekday} {period}, the view's label and the calendar's part of view.when.
calendar/period.<id>The period's word, its label or else its id.
calendar/weekday.<n>Monday to Sunday for 0 to 6 in a seven-day week, and Day <n + 1> of the week in a week of any other length.

Caveats

  • rules.onTurn sees the clock before the calendar moves it, so a rule for the day that has just begun belongs in rules.onTurnDone.
  • calendar/pass applied from an onTurn hook moves the clock but ends no turn, because a turn effect does nothing while a turn is ending.
  • The weekday counts from day 0, but a week turn counts from start.day. With start: { day: 3 } and turn: 'week', the game starts on a Thursday and each turn runs from Thursday to Wednesday.
  • When one pass ends several turns, only the first turn's rules.onTurn sees the clock where the pass began. Each later one sees the clock at the start of the unit the turn before moved it to.

Last updated on

On this page