All Projects → astrochili → narrator

astrochili / narrator

Licence: MIT license
The Ink language parser and runtime implementation in Lua.

Programming Languages

lua
6591 projects

Projects that are alternatives of or similar to narrator

ink-soaked
A template for Twine-like hypertext using inkle's ink scripting language and ink.js
Stars: ✭ 23 (-70.51%)
Mutual labels:  interactive-fiction, ink
ink roguelike
A narrative mini-roguelike written completely in ink.
Stars: ✭ 13 (-83.33%)
Mutual labels:  narrative, ink
metaparser
INSTEAD module to create games with text input
Stars: ✭ 18 (-76.92%)
Mutual labels:  interactive-fiction
limnoria-plugins
Limnoria plugins I wrote or forked.
Stars: ✭ 35 (-55.13%)
Mutual labels:  interactive-fiction
reinstead
RE:INSTEAD - Lightweight INSTEAD metaparser-only player
Stars: ✭ 15 (-80.77%)
Mutual labels:  interactive-fiction
arthur
Arthur: The Quest for Excalibur, by Bob Bates of Infocom (1989)
Stars: ✭ 62 (-20.51%)
Mutual labels:  interactive-fiction
DreamForgerJS
Interactive Fiction JS library
Stars: ✭ 14 (-82.05%)
Mutual labels:  interactive-fiction
leathergoddesses
Leather Goddesses of Phobos, by Steve Meretzky (Infocom)
Stars: ✭ 63 (-19.23%)
Mutual labels:  interactive-fiction
terminal.land
A boundless world of interactive fiction
Stars: ✭ 37 (-52.56%)
Mutual labels:  interactive-fiction
wishbringer-gold
Wishbringer: The Magick Stone of Dreams (Solid-Gold Edition) by Brian Moriarty (Infocom)
Stars: ✭ 45 (-42.31%)
Mutual labels:  interactive-fiction
plunderedhearts
Plundered Hearts (Amy Briggs) (Infocom)
Stars: ✭ 57 (-26.92%)
Mutual labels:  interactive-fiction
kingslayer
A text-based adventure written in Rust
Stars: ✭ 28 (-64.1%)
Mutual labels:  interactive-fiction
vorple
JavaScript libraries for Vorple
Stars: ✭ 41 (-47.44%)
Mutual labels:  interactive-fiction
CYOAwesome
An easy-to-use html5 text adventure game engine. PLAY THE DEMO:
Stars: ✭ 24 (-69.23%)
Mutual labels:  interactive-fiction
borogove-ide
Borogove online IDE for IF languages
Stars: ✭ 15 (-80.77%)
Mutual labels:  interactive-fiction
seastalker
Seastalker, by Stu Galley and Jim Lawrence (Infocom)
Stars: ✭ 52 (-33.33%)
Mutual labels:  interactive-fiction
Yarn-Weaver
simple tool built in Unity C# to test Yarn files, good for game devs and narrative designers
Stars: ✭ 52 (-33.33%)
Mutual labels:  narrative
ink-tab
Tab component for Ink 🌈
Stars: ✭ 87 (+11.54%)
Mutual labels:  ink
checkpoint
Checkpoint (Unreleased Game) by Stu Galley (Infocom)
Stars: ✭ 52 (-33.33%)
Mutual labels:  interactive-fiction
frobtads
Linux and macOS development tools and text-mode interpreter for TADS adventure games.
Stars: ✭ 41 (-47.44%)
Mutual labels:  interactive-fiction

logo buymeacoffee tinkoff twitter telegram

Narrator

Overview

The Ink language parser and runtime implementation in Lua.

Ink is a powerful narrative scripting language. You can find more information about how to write Ink scripts here. There is also Inky editor with useful features to test and debug Ink scripts.

Narrator allows to convert raw Ink scripts to the book (a lua table) and play it as story.

  • A book is a passive model on the shelf like a game level.
  • A story is a runtime state of the book reading like a game process.

Quick example

local narrator = require('narrator.narrator')

-- Parse a book from the Ink file.
local book = narrator.parseFile('stories.game')

-- Init a story from the book
local story = narrator.initStory(book)

-- Begin the story
story:begin()

while story:canContinue() do
  
  -- Get current paragraphs to output
  local paragraphs = story:continue()

  for _, paragraph in ipairs(paragraphs) do
    local text = paragraph.text

    -- You can handle tags as you like, but we attach them to text here.
    if paragraph.tags then
      text = text .. ' #' .. table.concat(paragraph.tags, ' #')
    end

    -- Output text to the player
    print(text)
  end

  -- If there is no choice it seems like the game is over
  if not story:canChoose() then break end
  
  -- Get available choices and output them to the player
  local choices = story:getChoices()
  for i, choice in ipairs(choices) do
    print(i .. ') ' .. choice.text)
  end

  -- Read the choice from the player input
  local answer = tonumber(io.read())

  -- Send answer to the story to generate new paragraphs
  story:choose(answer)
end

Features

Supported

  • Comments: singleline, multiline, todo's
  • Tags: global tags, knot tags, stitch tags, paragraph tags
  • Paths and sections: inclusions, knots, stitches, labels
  • Choices: suppressing and mixing, labels, conditions, sticky and fallback choices, tags
  • Branching: diversions, glues, gathers, nesting
  • Tunnels
  • Alternatives: sequences, cycles, once-only, shuffles, empty steps, nesting
  • Multiline alternatives: all the same + shuffle options
  • Conditions: logical operations, string queries, if and else statements, nesting
  • Multiline conditions: all the same + elseif statements, switches, nesting
  • Variables: assignments, constants, global variables, temporary variables, visits, lists
  • Lists: logical operations, multivalued lists, multi-list lists, all the queries, work with numbers
  • Game queries: all the queries without TURNS() and TURNS_SINCE()
  • State: saving and loading
  • Integration: external functions, variables observing, jumping
  • Migration: the ability to implement the migration of player's saves after the book update
  • Internal functions

Unsupported

  • Threads
  • Divert targets as variable's type
  • Assigning string evaluations to variables

Roadmap

The separated roadmap contains the full list of planned tasks, ideas and known limitations.

Installation

Common case (Löve, pure Lua, etc.)

Download the latest release archive and require the narrator module.

local narrator = require('narrator.narrator')

Narrator requires lpeg as dependency to parse Ink content. You can install it with luarocks.

$ luarocks install lpeg

In fact, you don't need lpeg in the release, but you need it locally to parse Ink content and generate lua versions of books to play in your game. Use parsing in development only, prefer already parsed and stored books in production.

Defold

Add links to the zip-archives of the latest versions of narrator and defold-lpeg to your Defold project as dependencies.

https://github.com/astrochili/narrator/archive/master.zip
https://github.com/astrochili/defold-lpeg/archive/master.zip

Then you can require the narrator module.

local narrator = require('narrator.narrator')

Documentation

narrator.parseFile(path, params)

Parses the Ink file at path with all the inclusions and returns a book instance. Path notations 'stories/game.ink', 'stories/game' and 'stories.game' are valid.

You can save a parsed book to the lua file with the same path by passing { save = true } as params table. By default, the params table is { save = false }.

-- Parse a Ink file at path 'stories/game.ink'
local book = narrator.parseFile('stories.game')

-- Parse a Ink file at path 'stories/game.ink'
-- and save the book at path 'stories/game.lua'
local book = narrator.parseFile('stories.game', { save = true })

Reading and saving files required io so if you can't work with files by this way use narrator.parseBook().

narrator.parseBook(content, inclusions)

Parses the string with Ink content and returns a book instance. The inclusions param is optional and can be used to pass an array of strings with Ink content of inclusions.

local content = 'Content of a root Ink file'
local inclusions = {
  'Content of an included Ink file',
  'Content of another included Ink file'
}

-- Parse a string with Ink content
local book = narrator.parseBook(content)

-- Parse a string with Ink content and inclusions
local book = narrator.parseBook(content, inclusions)

Content parsing is useful when you should manage files by your engine environment and don't want to use io module. For example, in Defold, you may want to load ink files as custom resources with sys.load_resource().

narrator.initStory(book)

Inits a story instance from the book. This is aclual to use in production. For example, just load a book with require() and pass it to this function.

-- Require a parsed and saved before book
local book = require('stories.game')

-- Init a story instance
local story = narrator.initStory(book)

story:begin()

Begins the story. Generates the first chunk of paragraphs and choices.

story:canContinue()

Returns a boolean, does the story have paragraphs to output or not.

while story:canContinue() do
  -- Get paragraphs?
end

story:continue(steps)

Get the next paragraphs. You can specify the number of paragraphs that you want to pull by the steps param.

  • Pass nothing if you want to get all the currently available paragraphs. 0 also works.
  • Pass 1 if you want to get one next paragraph without wrapping to array.

A paragraph is a table like { text = 'Hello.', tags = { 'tag1', 'tag2' } }. Most of the paragraphs do not have tags so tags can be nil.

-- Get all the currently available paragraphs
local paragraphs = story:continue()

-- Get one next paragraph
local paragraph = story:continue(1)

story:canChoose()

Returns a boolean, does the story have choices to output or not. Also returns false if there are available paragraphs to continue.

if story:canChoose() do
  -- Get choices?
end

story:getChoices()

Returns an array of available choices. Returns an empty array if there are available paragraphs to continue.

A choice is a table like { text = 'Bye.', tags = { 'tag1', 'tag2' } }. Most of the choices do not have tags so tags can be nil.

Choice tags are not an official feature of Ink, but it's a Narrator feature. These tags also will appear in the answer paragraph as it works in Ink by default. But if you have a completely eaten choice like '[Answer] #tag' you will receive tags only in the choice.

  -- Get available choices and output them to the player
  local choices = story:getChoices()
  for i, choice in ipairs(choices) do
    print(i .. ') ' .. choice.text)
  end

story:choose(index)

Make a choice to continue the story. Pass the index of the choice that you was received with getChoices() before. Will do nothing if canContinue() returns false.

  -- Get the answer from the player in the terminal
  answer = tonumber(io.read())

  -- Send the answer to the story to generate new paragraphs
  story:choose(answer)

  -- Get the new paragraphs
  local newParagraphs = story:continue()

story:jumpTo(pathString)

Jumps to the path. The pathString param is a string like 'knot.stitch.label'.

  -- Jump to the maze stitch in the adventure knot
  story:jumpTo('adventure.maze')

  -- Get the maze paragraphs
  local mazeParagraphs = story:continue()

story:getVisits(pathString)

Returns the number of visits to the path. The pathString param is a string like 'knot.stitch.label'.

-- Get the number of visits to the maze's red room
local redRoomVisits = story:getVisits('adventure.maze.red_room')

-- Get the number of adventures visited.
local adventureVisits = story:getVisits('adventure')

story:getTags(pathString)

Returns tags for the path. The pathString param is a string like 'knot.stitch'. This function is useful when you want to get tags before continue the story and pull paragraphs. Read more about it here.

-- Get tags for the path 'adventure.maze'
local mazeTags = story:getTags('adventure.maze')

story:saveState()

Raturns a table with the story state that can be saved and restored later. Use it to save the game.

-- Get the story's state
local state = story:saveState()

-- Save the state to your local storage
manager.save(state)

story:loadState(state)

Restores a story's state from the saved before state. Use it to load the game.

-- Load the state from your local storage
local state = manager.load()

-- Restore the story's state
story:loadState(state)

story:observe(variable, observer)

Assigns an observer function to the variable's changes.

local function xDidChange(x)
  print('The x did change! Now it\'s ' .. x)
end

-- Start observing the variable 'x'
story:observe('x', xDidChange)

story:bind(funcName, handler)

Binds a function to external calling from the Ink. The function can returns the value or not.

local function beep()
  print('Beep! 😃')
end

local function sum(x, y)
  return x + y
end

-- Bind the function without params and returned value
story:bind('beep', beep)

-- Bind the function with params and returned value
story:bind('sum', sum)

story.globalTags

An array with book's global tags. Tags are strings of course.

-- Get the global tags
local globalTags = story.globalTags

-- A hacky way to get the same global tags
local globalTags = story:getTags()

story.constants

A table with book's constants. Just read them, constants changing is not a good idea.

-- Get the theme value from the Ink constants
local theme = story.constants['theme']

story.variables

A table with story's variables. You can read or change them by this way.

-- Get the mood variable value
local mood = story.variables['mood']

-- Set the mood variable value
story.variables['mood'] = 'sunny'

story.migrate

A function that you can specify for migration from old to new versions of your books. This is useful, for example, when you don't want to corrupt player's save after the game update.

This is the place where you can rename or change variables, visits, update the current path, etc. The default implementation returns the same state without any migration.

-- Default implementation
function(state, oldVersion, newVersion) return state end

The oldVersion is the version of the saved state, the newVersion is the version of the book. You can specify the verson of the book with the constant 'version' in the Ink content, otherwise it's equal to 0.

-- A migration function example
local function migrate(state, oldVersion, newVersion)

  -- Check the need for migration
  if newVersion == oldVersion then
    return state
  end

  -- Migration for the second version of the book
  if newVersion == 2 then

    -- Get the old value
    local oldMood = state.variables['mood']

    -- If it exists then migrate ...
    if oldMood then 
      -- ... migrate the old number value to the new string value
      state.variables['mood'] = oldMood < 50 and 'sadly' or 'sunny'
    end
  end

  return state
end

-- Assign the migration function before loading a saved game
story.migrate = migrate

-- Load the game
story:loadState(savedState)

Contribution

Development

There are some useful extensions and configs for VSCode that I use in development of Narrator.

Testing

To run tests you need to install busted.

$ luarocks install busted

Don't forget also to install lpeg as described in Common case installation section.

After that you can run tests from the terminal:

$ busted test/run.lua

Third Party Libraries

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].