Skip to content

Commit

Permalink
Added choice tags
Browse files Browse the repository at this point in the history
  • Loading branch information
astrochili committed Jul 27, 2020
1 parent fcada0f commit 458387a
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 21 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ while story:canContinue() do
-- Get available choices and output them to the player
local choices = story:getChoices()
for i, choice in ipairs(choices) do
print(i .. ') ' .. choice)
print(i .. ') ' .. choice.text)
end

-- Read the choice from the player input
Expand All @@ -68,7 +68,7 @@ end
- [x] Comments: singleline, multiline, todo's
- [x] Tags: global tags, knot tags, stitch tags, paragraph tags
- [x] Paths and sections: inclusions, knots, stitches, labels
- [x] Choices: suppressing and mixing, labels, conditions, sticky and fallback choices
- [x] Choices: suppressing and mixing, labels, conditions, sticky and fallback choices, tags
- [x] Branching: divertions, glues, gathers, nesting
- [x] Alternatives: sequences, cycles, once-only, shuffles, empty steps, nesting
- [x] Multiline alternatives: all the same + shuffle options
Expand Down Expand Up @@ -219,13 +219,17 @@ end

### story:getChoices()

Returns an array of available choice titles. Returns an empty array if there are available paragraphs to continue.
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' } }```. The most of choices doesn't 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.

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

Expand Down
8 changes: 6 additions & 2 deletions bot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function Bot.play(story, instructor, params)
for _, paragraph in ipairs(paragraphs or { }) do
local text = paragraph.text or ''
if paragraph.tags then
text = text .. ' #' .. table.concat(paragraph.tags, ' #')
local hashtag = #text > 0 and ' #' or '#'
text = text .. hashtag .. table.concat(paragraph.tags, ' #')
end
output(text)
end
Expand All @@ -45,7 +46,10 @@ function Bot.play(story, instructor, params)
output('')
for i, choice in ipairs(choices) do
local prefix = (i == answer and '>' or i) .. ') '
local text = prefix .. choice
local text = prefix .. choice.text
if choice.tags then
text = text .. ' #' .. table.concat(choice.tags, ' #')
end
output(text)
end
output('')
Expand Down
2 changes: 1 addition & 1 deletion example-defold/example.script
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function init(self)
-- Get available choices and output them to the player
local choices = story:getChoices()
for i, choice in ipairs(choices) do
print(i .. ') ' .. choice)
print(i .. ') ' .. choice.text)
end

-- Send an answer to the story to generate new paragraphs
Expand Down
2 changes: 1 addition & 1 deletion game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ while story:canContinue() do
-- Get available choices and output them to the player
local choices = story:getChoices()
for i, choice in ipairs(choices) do
print(i .. ') ' .. choice)
print(i .. ') ' .. choice.text)
end

-- Read the choice from the player input
Expand Down
2 changes: 1 addition & 1 deletion narrator/narrator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ local Narrator = { }
-- @return a book
function Narrator.parseFile(path, params)
local params = params or { save = false }
assert(parser, "Can't parse anything without a parser.")
assert(parser, "Can't parse anything without lpeg, sorry.")

local content = readFile(path)
local book = parser.parse(content)
Expand Down
17 changes: 9 additions & 8 deletions narrator/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ local lpeg = require(lpegName)
--
-- LPeg

local S, C, Ct, Cc, Cg = lpeg.S, lpeg.C, lpeg.Ct, lpeg.Cc, lpeg.Cg
local Cb, Cf, Cmt, P, V = lpeg.Cb, lpeg.Cf, lpeg.Cmt, lpeg.P, lpeg.V
local S, C, P, V = lpeg.S, lpeg.C, lpeg.P, lpeg.V
local Cb, Ct, Cc, Cg = lpeg.Cb, lpeg.Ct, lpeg.Cc, lpeg.Cg
lpeg.locale(lpeg)

--
Expand Down Expand Up @@ -139,8 +139,8 @@ function Parser.parse(content)
assignmentPair = Cg(sentenceBefore(nl + comment) / unwrapAssignment, 'name') * Cg(Cb('name') / 2, 'value'),

choiceCondition = Cg(V'expression' + none, 'condition'),
choiceFallback = choiceLevel * sp * V'labelOptional' * sp * V'choiceCondition' * sp * (divert + divertToNothing),
choiceNormal = choiceLevel * sp * V'labelOptional' * sp * V'choiceCondition' * sp * Cg(V'text', 'text') * divert ^ -1,
choiceFallback = choiceLevel * sp * V'labelOptional' * sp * V'choiceCondition' * sp * (divert + divertToNothing) * sp * V'tagsOptional',
choiceNormal = choiceLevel * sp * V'labelOptional' * sp * V'choiceCondition' * sp * Cg(V'text', 'text') * divert ^ -1 * sp * V'tagsOptional',
choice = V'choiceFallback' + V'choiceNormal',

-- Paragraph
Expand Down Expand Up @@ -319,8 +319,8 @@ function Constructor:addNode(items, isRestricted)
-- level, label, parts, tags
Constructor.addParagraph(self, item.level, item.label, item.parts, item.tags)
elseif item.type == 'choice' then
-- level, sticky, label, condition, text, divert
Constructor.addChoice(self, item.level, item.sticky, item.label, item.condition, item.text, item.divert)
-- level, sticky, label, condition, text, divert, tags
Constructor.addChoice(self, item.level, item.sticky, item.label, item.condition, item.text, item.divert, item.tags)
end
end
end
Expand Down Expand Up @@ -547,12 +547,13 @@ function Constructor.convertParagraphPartsToItems(parts, isRoot)
return items
end

function Constructor:addChoice(level, sticky, label, condition, sentence, divert)
function Constructor:addChoice(level, sticky, label, condition, sentence, divert, tags)
local item = {
sticky = sticky or nil,
condition = condition,
label = label,
divert = divert
divert = divert,
tags = tags
}

if sentence == nil then
Expand Down
15 changes: 12 additions & 3 deletions narrator/story.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ function Story:getChoices()
end

for _, choice in ipairs(self.choices) do
table.insert(choices, choice.title)
local model = {
text = choice.title,
tags = choice.tags
}
table.insert(choices, model)
end

return choices
Expand All @@ -126,8 +130,12 @@ function Story:choose(index)
self.paragraphs = { }
self.choices = { }

if choice.text ~= nil and #choice.text > 0 then
table.insert(self.paragraphs, { text = choice.text })
if choice.text and #choice.text > 0 then
local paragraph = {
text = choice.text,
tags = choice.tags
}
table.insert(self.paragraphs, paragraph)
end

self:visit(choice.path)
Expand Down Expand Up @@ -563,6 +571,7 @@ function Story:readChoice(item, path)
title = title,
text = item.text ~= nil and self:replaceExpressions(item.text) or title,
divert = item.divert,
tags = item.tags,
path = path
}

Expand Down
1 change: 1 addition & 0 deletions test/cases.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ local units = {
'text-glue',

'choices-basic',
'choices-tags',
'choices-conditional',
'choices-sticky',
'choices-fallback',
Expand Down
2 changes: 1 addition & 1 deletion test/run.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ local function getPossibleResults(case)
-- Transform a current sequence to branches for each available choice
table.remove(sequences, seqIndex)

for index, choice in ipairs(choices) do
for index, _ in ipairs(choices) do
local newSeq = lume.concat(curSeq, { index })
table.insert(sequences, newSeq)
end
Expand Down
6 changes: 6 additions & 0 deletions test/units/choices-tags.ink
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Let's try an experemental feature:
The Choice Tags!
* Are you seriously? #tag1
Yeap, absolutely!
* [Eat answer!] #tag2
Are you hungry?
8 changes: 8 additions & 0 deletions test/units/choices-tags/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Let's try an experemental feature:
The Choice Tags!

>) Are you seriously? #tag1
2) Eat answer! #tag2

Are you seriously? #tag1
Yeap, absolutely!
7 changes: 7 additions & 0 deletions test/units/choices-tags/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Let's try an experemental feature:
The Choice Tags!

1) Are you seriously? #tag1
>) Eat answer! #tag2

Are you hungry?

0 comments on commit 458387a

Please sign in to comment.