Skip to content

Commit

Permalink
Some cosmetic after tunnels pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
astrochili committed Jan 10, 2022
1 parent 8b1e9b2 commit 427b6e9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,14 @@ There are some useful extensions and configs for [VSCode](https://code.visualstu

### Testing

To run tests you need to install [busted](https://github.com/Olivine-Labs/busted) and lpeg.
To run tests you need to install [busted](https://github.com/Olivine-Labs/busted).

```shell
$ luarocks install busted
$ luarocks install lpeg
```

Don't forget also to install [lpeg](http://www.inf.puc-rio.br/~roberto/lpeg/) as described in [Common case](#common-case-löve-pure-lua-etc) installation section.

After that you can run tests from the terminal:
```shell
$ busted test/run.lua
Expand Down
11 changes: 8 additions & 3 deletions narrator/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local lpeg = require(lpegName)

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
local Cmt = lpeg.Cmt
lpeg.locale(lpeg)

--
Expand Down Expand Up @@ -49,9 +50,13 @@ function Parser.parse(content)
local id = (lpeg.alpha + '_') * (lpeg.alnum + '_') ^ 0
local label = Cg('(' * sp * C(id) * sp * ')', 'label')
local address = id * ('.' * id) ^ -2
local divert = Cg( Ct(divertSign * sp * Cg(address, "path") * sp * Cg(lpeg.Cmt(Cb("path"), function(s, i, a) local r = lpeg.match (sp * divertSign, s, i) return i, r~=nil end), "tunnel") * (sp * divertSign * nl)^-1), 'divert')

-- TODO: Clean divert expression to divert and tunnel
-- this is too long right now
local divert = Cg( Ct(divertSign * sp * Cg(address, 'path') * sp * Cg(Cmt(Cb('path'), function(s, i, a) local r = lpeg.match (sp * divertSign, s, i) return i, r ~= nil end), 'tunnel') * (sp * divertSign * nl) ^ -1), 'divert')

local divertToNothing = divertSign * none
local exitTunnel = Cg(divertSign * divertSign, "exit")
local exitTunnel = Cg(divertSign * divertSign, 'exit')
local tag = '#' * sp * V'text'
local tags = Cg(Ct(tag * (sp * tag) ^ 0), 'tags')

Expand Down Expand Up @@ -365,7 +370,7 @@ function Constructor:addStitch(stitch)
if self.currentStitch == '_' then
local rootStitchNode = self.book.tree[self.currentKnot]._
if #rootStitchNode == 0 then
local divertItem = { divert = {path = stitch} }
local divertItem = { divert = { path = stitch } }
table.insert(rootStitchNode, divertItem)
end
end
Expand Down
26 changes: 19 additions & 7 deletions narrator/story.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ function Story:new(book)
self.paragraphs = { }
self.output = { }
self.visits = { }
self.tunnels = { }
self.currentPath = nil
self.isOver = false

self.stack = { }
end

--
Expand Down Expand Up @@ -143,10 +142,13 @@ function Story:choose(index)
end

self:visit(choice.path)

if choice.divert ~= nil then
if choice.divert.tunnel then
table.insert(self.stack, {path = choice.path})
local tunnel = { path = choice.path }
table.insert(self.tunnels, tunnel)
end

self:jumpTo(choice.divert.path)
else
self:readPath(choice.path)
Expand Down Expand Up @@ -361,9 +363,15 @@ function Story:readItems(items, path, depth, mode, currentIndex)
-- Iterate items

for index = currentIndex or (deepIndex or 1), #items do
local pointer = {items = items, path = path, depth = depth, mode = mode, index = index + 1}
local item = items[index]
local skip = false
local pointer = {
items = items,
path = path,
depth = depth,
mode = mode,
index = index + 1
}

local itemType = enums.item.text
if type(item) == 'table' then
Expand Down Expand Up @@ -514,20 +522,24 @@ function Story:readText(item, pointer)
end

if item.exit ~= nil then
local state = assert(table.remove(self.stack), "Tunnel stack is empty.")
local state = assert(table.remove(self.tunnels), 'Tunnel stack is empty.')

if state.items == nil then
self:readPath(state.path)
else
self:readItems(state.items, state.path, state.depth, state.mode, state.index)
end

return enums.readMode.quit
end

if item.divert ~= nil then
if item.divert.tunnel then
table.insert(self.stack, pointer)
table.insert(self.tunnels, pointer)
end

self:jumpTo(item.divert.path)

return enums.readMode.quit
end
end
Expand Down Expand Up @@ -581,7 +593,7 @@ function Story:readChoice(item, path, pointer)
if #self.choices == 0 then
if item.divert ~= nil then
if item.divert.tunnel then
table.insert(self.stack, pointer)
table.insert(self.tunnels, pointer)
end
self:jumpTo(item.divert.path)
else
Expand Down

0 comments on commit 427b6e9

Please sign in to comment.