1
0
Fork 0
mirror of https://github.com/Oreolek/ink-instead.git synced 2024-05-14 15:08:23 +03:00
ink-instead/pink/runtime.lua

121 lines
2.4 KiB
Lua
Raw Normal View History

2016-12-03 04:05:00 +02:00
local is = function (what, node)
2016-12-04 03:47:33 +02:00
return node ~= nil
and (type(node) == "table" and node[1] == what)
or (type(node) == "string" and node == what)
end
local getPara = function (node)
2016-12-03 04:05:00 +02:00
if is('para', node) then return node[2] end
end
return function (tree)
2016-12-04 03:47:33 +02:00
--print(to_string(tree))
2016-12-03 04:05:00 +02:00
local s = {}
2016-12-04 03:47:33 +02:00
local pointer = nil
local tab = {}
2016-12-03 04:05:00 +02:00
2016-12-03 05:13:50 +02:00
local knots = {}
2016-12-31 01:27:11 +02:00
-- TODO state should contain tab/pointer to be able to save / load
s.state = {
visitCount = {}
}
2016-12-03 05:13:50 +02:00
local process = function ()
2016-12-04 03:47:33 +02:00
for _, v in ipairs(tree) do
2016-12-03 05:13:50 +02:00
if is('knot', v) then
knots[v[2]] = v
end
end
end
2016-12-31 01:27:11 +02:00
local goToKnot = function(knotName)
if knots[knotName] then
s.state.visitCount[knotName] = (s.state.visitCount[knotName] or 0) + 1
tab = knots[knotName]
pointer = 3
return tab[pointer]
else
print('unknown knot', knotName)
end
end
2016-12-03 04:05:00 +02:00
local update = function ()
local next = tab[pointer]
2016-12-31 01:27:11 +02:00
if is('knot', next) then -- FIXME: we shouldn't continue to next knot automatically probably - how about stitches?
next = goToKnot(next)
2016-12-03 05:13:50 +02:00
end
2016-12-31 01:27:11 +02:00
if is('divert', next) then next = goToKnot(next[2]) end
2016-12-03 05:13:50 +02:00
2016-12-03 04:05:00 +02:00
s.canContinue = is('para', next)
s.currentChoices = {}
if is('choice', next) then
for i=2, #next do
--print(to_string(next[i]))
table.insert(s.currentChoices, {
2016-12-03 05:13:50 +02:00
text = (next[i][2] or '') .. (next[i][3] or ''),
2016-12-03 04:05:00 +02:00
choiceText = next[i][2] .. (next[i][4] or ''),
})
end
end
end
2016-12-04 03:47:33 +02:00
local step = function ()
pointer = pointer + 1
update()
return tab[pointer]
end
local stepTo = function (table, pos)
tab = table
pointer = pos
update()
return tab[pointer]
end
2016-12-03 05:13:50 +02:00
s.canContinue = nil
2016-12-03 04:05:00 +02:00
s.continue = function()
2016-12-03 04:05:00 +02:00
local res = getPara(tab[pointer])
2016-12-04 03:47:33 +02:00
local next = step()
if is('glue', next) then
step()
res = res .. s.continue()
end
return res;
end
2016-12-03 04:05:00 +02:00
2016-12-03 05:13:50 +02:00
s.currentChoices = nil
2016-12-03 04:05:00 +02:00
s.chooseChoiceIndex = function(index)
s.currentChoices = {}
2016-12-03 04:05:00 +02:00
local choice = tab[pointer]
local option = choice[1 + index]
2016-12-04 03:47:33 +02:00
stepTo(option, 5)
end
2016-12-03 04:05:00 +02:00
2016-12-31 01:27:11 +02:00
s.choosePathString = function(knotName)
goToKnot(knotName)
update()
end
s.state.visitCountAtPathString = function(knotName)
return s.state.visitCount[knotName] or 0
end
s.variablesState = {}
-- s.state.ToJson();s.state.LoadJson(savedJson);
2016-12-03 05:13:50 +02:00
2016-12-04 03:47:33 +02:00
stepTo(tree, 1)
2016-12-03 05:13:50 +02:00
process()
return s
end