From 5a52de42707ebecd99702a1afce16aabf7eed7cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emek=20Vyhnal?= Date: Sat, 14 Jan 2017 01:29:56 +0100 Subject: [PATCH 1/9] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a5814c8..abc3c0d 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ An attempt to implement a subset of [ink](https://github.com/inkle/ink) in lua using [lpeg](http://www.inf.puc-rio.br/~roberto/lpeg) ## How to use this to run a game -See the examples directory. +See the examples directory for a simple text based example and a LÖVE integration. -This is how to run the simple text-based example: +This is how to run the text-based example: $ lua examples/game.lua @@ -13,6 +13,7 @@ And this example shows [LÖVE](https://love2d.org) integration: $ love examples/love2d +To use it in your project download the latest source or the latest [release](../../releases). You need just the [pink](../../tree/master/pink) directory. ## How to run tests $ lua test.lua From 19c9e86efbf8502c3e86f47a25b335ce055e0fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emek=20Vyhnal?= Date: Sat, 14 Jan 2017 01:41:27 +0100 Subject: [PATCH 2/9] Update README.md --- README.md | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index abc3c0d..5d64c41 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,54 @@ # pink An attempt to implement a subset of [ink](https://github.com/inkle/ink) in lua using [lpeg](http://www.inf.puc-rio.br/~roberto/lpeg) +_Ink is inkle's scripting language for writing interactive narrative, both for text-centric games as well as more graphical games that contain highly branching stories._ + ## How to use this to run a game +To use it in your project download the latest source or the latest [release](../../releases). You need just the [pink](../../tree/master/pink) directory. + +### Example +Given some .ink file like below, you can easily run it in your lua application using the pink library. + +```ink +=== back_in_london === +We arrived into London at 9.45pm exactly. +* "There is not a moment to lose!"[] I declared. -> hurry_outside +* "Monsieur, let us savour this moment!"[] I declared. + My master clouted me firmly around the head and dragged me out of the door. + -> dragged_outside + +=== hurry_outside === +We hurried home to Savile Row -> as_fast_as_we_could + +=== dragged_outside === +He insisted that we hurried home to Savile Row +-> as_fast_as_we_could + +=== as_fast_as_we_could === +<> as fast as we could. +``` + + +```lua +local pink = require('pink.pink') +-- 1) Load story +local story = pink.getStory('examples/game.ink') +while true do + -- 2) Game content, line by line + while story.canContinue do + print(story.continue()) + end + -- 3) Display story.currentChoices list, allow player to choose one + for i = 1, #story.currentChoices do + print(i .. "> " .. story.currentChoices[i].text) + end + if #story.currentChoices == 0 then break end -- cannot continue and there are no choices + local answer=io.read() + print (story.currentChoices[tonumber(answer)].choiceText) + story.chooseChoiceIndex(answer) +end +``` + See the examples directory for a simple text based example and a LÖVE integration. This is how to run the text-based example: @@ -13,7 +60,6 @@ And this example shows [LÖVE](https://love2d.org) integration: $ love examples/love2d -To use it in your project download the latest source or the latest [release](../../releases). You need just the [pink](../../tree/master/pink) directory. ## How to run tests $ lua test.lua From 1bef2c489074f873daf4ec21198c7eacbabc9ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emek=20Vyhnal?= Date: Sat, 14 Jan 2017 01:50:51 +0100 Subject: [PATCH 3/9] Update README.md --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 5d64c41..a06770c 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,21 @@ An attempt to implement a subset of [ink](https://github.com/inkle/ink) in lua u _Ink is inkle's scripting language for writing interactive narrative, both for text-centric games as well as more graphical games that contain highly branching stories._ +## Currently supported + +- Paragraphs +- Comments +- Choices + - choice and output text + - nested choices +- Knots +- Diverts +- Glue + + + +See https://github.com/inkle/ink/blob/master/Documentation/WritingWithInk.md for the description of the reference ink implementation. + ## How to use this to run a game To use it in your project download the latest source or the latest [release](../../releases). You need just the [pink](../../tree/master/pink) directory. From 37913d3ec12bc73f4c8c086ae638aaeab620d3b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emek=20Vyhnal?= Date: Sat, 14 Jan 2017 01:59:17 +0100 Subject: [PATCH 4/9] Update README.md --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a06770c..fee71e1 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,13 @@ _Ink is inkle's scripting language for writing interactive narrative, both for t - Diverts - Glue - - See https://github.com/inkle/ink/blob/master/Documentation/WritingWithInk.md for the description of the reference ink implementation. +## Used by +pink is used by my small game https://github.com/premek/enjoy + +Let me know if you want to use it too. + ## How to use this to run a game To use it in your project download the latest source or the latest [release](../../releases). You need just the [pink](../../tree/master/pink) directory. From 507c388858706b392a7469151d6d3ddea3d6b682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emek=20Vyhnal?= Date: Sat, 14 Jan 2017 13:43:27 +0100 Subject: [PATCH 5/9] Update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fee71e1..2ab5b31 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ _Ink is inkle's scripting language for writing interactive narrative, both for t ## Currently supported +### Parser - Paragraphs - Comments - Choices @@ -14,7 +15,10 @@ _Ink is inkle's scripting language for writing interactive narrative, both for t - Diverts - Glue -See https://github.com/inkle/ink/blob/master/Documentation/WritingWithInk.md for the description of the reference ink implementation. +### Runtime +- state.visitCountAtPathString() - allows the runtime to check if the story went through a scpecific point + +See [WritingWithInk](https://github.com/inkle/ink/blob/master/Documentation/WritingWithInk.md) and [RunningYourInk](https://github.com/inkle/ink/blob/master/Documentation/RunningYourInk.md) for the description of the reference ink implementation. ## Used by pink is used by my small game https://github.com/premek/enjoy From b9a7c2e73c32c2033fd20ab3ac34f1ec8a34433e Mon Sep 17 00:00:00 2001 From: premek Date: Sun, 15 Jan 2017 01:50:55 +0100 Subject: [PATCH 6/9] .love file packaging support --- pink/pink.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pink/pink.lua b/pink/pink.lua index ae80c21..db5e436 100644 --- a/pink/pink.lua +++ b/pink/pink.lua @@ -3,12 +3,17 @@ local parser = require(folderOfThisFile .. 'parser') local runtime = require(folderOfThisFile .. 'runtime') -local function read(file) +local function read(file) -- TODO should this be here or in client code? At lease allow to pass an ink content in a string + if love and love.filesystem and love.filesystem.read then + local content, size = love.filesystem.read(file) + return content + else local f = io.open(file, "rb") if not f then error('failed to open "'..file..'"') end local content = f:read("*all") f:close() return content + end end From ab5d6e82e7154f665b9890c6627d1e9302b2a377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emek=20Vyhnal?= Date: Sun, 15 Jan 2017 23:26:23 +0100 Subject: [PATCH 7/9] Update README.md --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 2ab5b31..0d5b7c2 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,25 @@ pink is used by my small game https://github.com/premek/enjoy Let me know if you want to use it too. +## Install dependencies + +1. install luarocks (Or see https://luarocks.org/#quick-start for instructions for other platforms) + + sudo aptitude install luarocks + +2. install lpeg + + luarocks install --local lpeg + +3. get pink + Clone this repo or download an archive from [releases](../../releases) page. You need just the `pink` subdirectory. + +### Note on dependencies +The pink **parser** depends on lpeg which can easily be instaled by luarocks (see above) but it may be difficult to distribute it with your game for each platform (it is a C library). Please consider compiling the .ink file into lua table, save it into a file and distribute just the compiled file with a lua table instead of compiling at runtime. (See: [#3](/../../issues/3)) + +Pink **runtime** is just a pure lua. + + ## How to use this to run a game To use it in your project download the latest source or the latest [release](../../releases). You need just the [pink](../../tree/master/pink) directory. From 29e96a33630b3bcbb1f535c7a2f054838213dc5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emek=20Vyhnal?= Date: Sun, 15 Jan 2017 23:33:28 +0100 Subject: [PATCH 8/9] Update README.md --- README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0d5b7c2..b7cd257 100644 --- a/README.md +++ b/README.md @@ -27,16 +27,17 @@ Let me know if you want to use it too. ## Install dependencies -1. install luarocks (Or see https://luarocks.org/#quick-start for instructions for other platforms) +Install luarocks (Or see https://luarocks.org/#quick-start for instructions for other platforms): sudo aptitude install luarocks - -2. install lpeg + +Install lpeg: luarocks install --local lpeg - -3. get pink - Clone this repo or download an archive from [releases](../../releases) page. You need just the `pink` subdirectory. + +Get pink: + +Clone this repo or download an archive from [releases](../../releases) page. You need just the `pink` subdirectory. ### Note on dependencies The pink **parser** depends on lpeg which can easily be instaled by luarocks (see above) but it may be difficult to distribute it with your game for each platform (it is a C library). Please consider compiling the .ink file into lua table, save it into a file and distribute just the compiled file with a lua table instead of compiling at runtime. (See: [#3](/../../issues/3)) From 9b15dd50272b23203875cb32963ce05263546fe5 Mon Sep 17 00:00:00 2001 From: premek Date: Mon, 16 Jan 2017 01:12:43 +0100 Subject: [PATCH 9/9] support serializing and loading a compiled story --- pink/pink.lua | 53 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/pink/pink.lua b/pink/pink.lua index db5e436..0b89755 100644 --- a/pink/pink.lua +++ b/pink/pink.lua @@ -1,8 +1,9 @@ -local folderOfThisFile = (...):match("(.-)[^%.]+$") -local parser = require(folderOfThisFile .. 'parser') +-- FIXME +if not arg[1] and not (...) then error("Usage: `require` this file from a script or call `lua pink/pink.lua parse game.ink`") end +local folderOfThisFile = arg[1] and string.sub(..., 1, string.len(arg[1]))==arg[1] and arg[0]:match("(.-)[^/\\]+$") or (...):match("(.-)[^%.]+$") +local getParser = function () return require(folderOfThisFile .. 'parser') end local runtime = require(folderOfThisFile .. 'runtime') - local function read(file) -- TODO should this be here or in client code? At lease allow to pass an ink content in a string if love and love.filesystem and love.filesystem.read then local content, size = love.filesystem.read(file) @@ -17,10 +18,48 @@ local function read(file) -- TODO should this be here or in client code? At leas end -return { +local api = { getStory = function (filename) - return runtime(parser:match(read(filename))) + local parsed + if not pcall(function () + parsed = require (string.sub(filename, 1, -5)) + print('loaded precompiled story') + end) then + parsed = getParser():match(read(filename)) + print('story compiled') + end + return runtime(parsed) end - - } + + +local function dump ( t ) -- tables only + local function sub_print_r(t) + if (type(t)=="table") then + local b = "" + for pos,val in pairs(t) do + if (type(val)=="table") then + b = b .. "{"..sub_print_r(val).."}," + elseif (type(val)=="string") then + b = b .. '"'..string.gsub(val,'"', '\\"')..'", ' + else + b = b .. tostring(val) .. ', ' + end + end + return b + else + return tostring(t) + end + end + return "-- This file was generated from an .ink file using the pink library - do not edit\nreturn {" .. sub_print_r(t) .. "}" +end + + + + +if arg[1] == 'parse' and arg[2] then + print(dump(getParser():match(read(arg[2])))) +end + + +return api