Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add asciihero-dice-footer extension #92

Merged
merged 5 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ Include several of these somehwere in your book and instruct the player to close

image::images/dicetable.png[align="center"]

[#dicefooter]
=== Dice Footer

Add the `:asciihero-dice-footer: <string>` attribute to generate and render two dice showing random numbers in the footer of every page containing a segment.
The `<string>` is used as the seed for generating the random dice numbers.

image::images/dicefooter.png[align="center"]

== Exporting

Exporting means to convert your AsciiHero document and turn it into something that is appropriate for reading by anyone.
Expand Down Expand Up @@ -481,31 +489,41 @@ If you are new to both, it's recommended to use the link:https://docs.asciidocto
|Name |Allowable Values |Modifiable |API/CLI Only |Notes

|`asciihero-combat-attributes`
| _comma-separated list of attributes_
| Yes
| No
| List of attributes for enemies when rendering a combat block.
|_comma-separated list of attributes_
|Yes
|No
|List of attributes for enemies when rendering a combat block.

|`asciihero-dice-footer`
|`<string>`
|Yes
|No
|Add a pair of dice to the footer of every page containing segments.
The `<string>` is used as the seed for the random dice.
See <<dicefooter>> for more information.

|`asciihero-shuffle-algorithm`
|`none` +
`random`
|Yes
|No
|Algorithm to use when shuffling the segments in the document. See <<shuffling>> for more information.
|Algorithm to use when shuffling the segments in the document.
See <<shuffling>> for more information.

|`asciihero-shuffle-seed`
|`<string>` +
`<integer>`
|Yes
|No
|Seed to use for the random shuffle. Using the same seed guarantees the same order every time you render your gamebook.
|Seed to use for the random shuffle.
Using the same seed guarantees the same order every time you render your gamebook.

|`asciihero-segment-title`
|`title` +
`number`
| Yes
| No
| Sets whether to show segment as titles (for debugging), or number (default).
|Yes
|No
|Sets whether to show segment as titles (for debugging), or number (default).
|===

[#macros]
Expand Down
68 changes: 68 additions & 0 deletions examples/dice_footer/dice_footer.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
= Dice Footer
:doctype: book
:asciihero-shuffle-style: random
:asciihero-shuffle-seed: 1234
:icons: font
:asciihero-dice-footer: 1234

== Introduction

If you set `:asciihero-dice-footer: true` it will add 2 random `d6` dice to the footer of each page.
Your readers can use this in a pinch if they don't have any dice handy!

[.gameplay]
== Gameplay

[.segment]
=== segment_1

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque interdum mauris quam, ut congue magna imperdiet at. Nunc ipsum turpis, pretium et congue vel, dictum eu ex. Integer tristique posuere ipsum, vel mattis nisi eleifend nec. Vivamus vitae sem ligula. Fusce ac nunc et massa viverra finibus. Vestibulum molestie quam sit amet pretium fermentum. Nunc posuere rutrum dolor eu tempus. Donec arcu ligula, consequat a enim nec, tincidunt hendrerit ipsum. Morbi quis metus leo. Cras a neque interdum, convallis ligula ut, convallis lacus. Phasellus efficitur tellus id orci sagittis, iaculis elementum ligula facilisis. Suspendisse blandit malesuada tellus, nec rutrum orci euismod sed. Integer dignissim eu arcu id ornare.

[.segment]
=== segment_2

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque interdum mauris quam, ut congue magna imperdiet at. Nunc ipsum turpis, pretium et congue vel, dictum eu ex. Integer tristique posuere ipsum, vel mattis nisi eleifend nec. Vivamus vitae sem ligula. Fusce ac nunc et massa viverra finibus. Vestibulum molestie quam sit amet pretium fermentum. Nunc posuere rutrum dolor eu tempus. Donec arcu ligula, consequat a enim nec, tincidunt hendrerit ipsum. Morbi quis metus leo. Cras a neque interdum, convallis ligula ut, convallis lacus. Phasellus efficitur tellus id orci sagittis, iaculis elementum ligula facilisis. Suspendisse blandit malesuada tellus, nec rutrum orci euismod sed. Integer dignissim eu arcu id ornare.

[.segment]
=== segment_3

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque interdum mauris quam, ut congue magna imperdiet at. Nunc ipsum turpis, pretium et congue vel, dictum eu ex. Integer tristique posuere ipsum, vel mattis nisi eleifend nec. Vivamus vitae sem ligula. Fusce ac nunc et massa viverra finibus. Vestibulum molestie quam sit amet pretium fermentum. Nunc posuere rutrum dolor eu tempus. Donec arcu ligula, consequat a enim nec, tincidunt hendrerit ipsum. Morbi quis metus leo. Cras a neque interdum, convallis ligula ut, convallis lacus. Phasellus efficitur tellus id orci sagittis, iaculis elementum ligula facilisis. Suspendisse blandit malesuada tellus, nec rutrum orci euismod sed. Integer dignissim eu arcu id ornare.

[.segment]
=== segment_4

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque interdum mauris quam, ut congue magna imperdiet at. Nunc ipsum turpis, pretium et congue vel, dictum eu ex. Integer tristique posuere ipsum, vel mattis nisi eleifend nec. Vivamus vitae sem ligula. Fusce ac nunc et massa viverra finibus. Vestibulum molestie quam sit amet pretium fermentum. Nunc posuere rutrum dolor eu tempus. Donec arcu ligula, consequat a enim nec, tincidunt hendrerit ipsum. Morbi quis metus leo. Cras a neque interdum, convallis ligula ut, convallis lacus. Phasellus efficitur tellus id orci sagittis, iaculis elementum ligula facilisis. Suspendisse blandit malesuada tellus, nec rutrum orci euismod sed. Integer dignissim eu arcu id ornare.

[.segment]
=== segment_5

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque interdum mauris quam, ut congue magna imperdiet at. Nunc ipsum turpis, pretium et congue vel, dictum eu ex. Integer tristique posuere ipsum, vel mattis nisi eleifend nec. Vivamus vitae sem ligula. Fusce ac nunc et massa viverra finibus. Vestibulum molestie quam sit amet pretium fermentum. Nunc posuere rutrum dolor eu tempus. Donec arcu ligula, consequat a enim nec, tincidunt hendrerit ipsum. Morbi quis metus leo. Cras a neque interdum, convallis ligula ut, convallis lacus. Phasellus efficitur tellus id orci sagittis, iaculis elementum ligula facilisis. Suspendisse blandit malesuada tellus, nec rutrum orci euismod sed. Integer dignissim eu arcu id ornare.

[.segment]
=== segment_6

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque interdum mauris quam, ut congue magna imperdiet at. Nunc ipsum turpis, pretium et congue vel, dictum eu ex. Integer tristique posuere ipsum, vel mattis nisi eleifend nec. Vivamus vitae sem ligula. Fusce ac nunc et massa viverra finibus. Vestibulum molestie quam sit amet pretium fermentum. Nunc posuere rutrum dolor eu tempus. Donec arcu ligula, consequat a enim nec, tincidunt hendrerit ipsum. Morbi quis metus leo. Cras a neque interdum, convallis ligula ut, convallis lacus. Phasellus efficitur tellus id orci sagittis, iaculis elementum ligula facilisis. Suspendisse blandit malesuada tellus, nec rutrum orci euismod sed. Integer dignissim eu arcu id ornare.

[.segment]
=== segment_7

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque interdum mauris quam, ut congue magna imperdiet at. Nunc ipsum turpis, pretium et congue vel, dictum eu ex. Integer tristique posuere ipsum, vel mattis nisi eleifend nec. Vivamus vitae sem ligula. Fusce ac nunc et massa viverra finibus. Vestibulum molestie quam sit amet pretium fermentum. Nunc posuere rutrum dolor eu tempus. Donec arcu ligula, consequat a enim nec, tincidunt hendrerit ipsum. Morbi quis metus leo. Cras a neque interdum, convallis ligula ut, convallis lacus. Phasellus efficitur tellus id orci sagittis, iaculis elementum ligula facilisis. Suspendisse blandit malesuada tellus, nec rutrum orci euismod sed. Integer dignissim eu arcu id ornare.

[.segment]
=== segment_8

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque interdum mauris quam, ut congue magna imperdiet at. Nunc ipsum turpis, pretium et congue vel, dictum eu ex. Integer tristique posuere ipsum, vel mattis nisi eleifend nec. Vivamus vitae sem ligula. Fusce ac nunc et massa viverra finibus. Vestibulum molestie quam sit amet pretium fermentum. Nunc posuere rutrum dolor eu tempus. Donec arcu ligula, consequat a enim nec, tincidunt hendrerit ipsum. Morbi quis metus leo. Cras a neque interdum, convallis ligula ut, convallis lacus. Phasellus efficitur tellus id orci sagittis, iaculis elementum ligula facilisis. Suspendisse blandit malesuada tellus, nec rutrum orci euismod sed. Integer dignissim eu arcu id ornare.

[.segment]
=== segment_9

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque interdum mauris quam, ut congue magna imperdiet at. Nunc ipsum turpis, pretium et congue vel, dictum eu ex. Integer tristique posuere ipsum, vel mattis nisi eleifend nec. Vivamus vitae sem ligula. Fusce ac nunc et massa viverra finibus. Vestibulum molestie quam sit amet pretium fermentum. Nunc posuere rutrum dolor eu tempus. Donec arcu ligula, consequat a enim nec, tincidunt hendrerit ipsum. Morbi quis metus leo. Cras a neque interdum, convallis ligula ut, convallis lacus. Phasellus efficitur tellus id orci sagittis, iaculis elementum ligula facilisis. Suspendisse blandit malesuada tellus, nec rutrum orci euismod sed. Integer dignissim eu arcu id ornare.

[.segment]
=== segment_10

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque interdum mauris quam, ut congue magna imperdiet at. Nunc ipsum turpis, pretium et congue vel, dictum eu ex. Integer tristique posuere ipsum, vel mattis nisi eleifend nec. Vivamus vitae sem ligula. Fusce ac nunc et massa viverra finibus. Vestibulum molestie quam sit amet pretium fermentum. Nunc posuere rutrum dolor eu tempus. Donec arcu ligula, consequat a enim nec, tincidunt hendrerit ipsum. Morbi quis metus leo. Cras a neque interdum, convallis ligula ut, convallis lacus. Phasellus efficitur tellus id orci sagittis, iaculis elementum ligula facilisis. Suspendisse blandit malesuada tellus, nec rutrum orci euismod sed. Integer dignissim eu arcu id ornare.

== The End

TODO
Binary file added examples/dice_footer/dice_footer.pdf
Binary file not shown.
43 changes: 43 additions & 0 deletions src/extensions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
const seedrandom = require('seedrandom')

// diceFooterProcessor processes all sections to add a hidden section with die
// numbers. The last set of hidden dice sections are then shown in the footer
// of the page.
function diceFooterProcessor () {
const self = this

const dice = [
"",
"icon:dice-one[]",
"icon:dice-two[]",
"icon:dice-three[]",
"icon:dice-four[]",
"icon:dice-five[]",
"icon:dice-six[]",
]

self.process(function (doc) {
if (!doc.hasAttribute('asciihero-dice-footer')) {
return
}

const seed = doc.getAttribute('asciihero-dice-footer')
const rng = seedrandom(seed)

const segmentNodes = doc.findBy({ role: 'segment' })

let sectionIndex = 0
for (const index in segmentNodes) {
const node = segmentNodes[index]

// NOTE: We probably should probably just rely on default autoId behaviour here...
if (node.getId().startsWith('_')) {
node.setId(node.title)
}

const rndInt1 = Math.floor(rng() * 6) + 1
const rndInt2 = Math.floor(rng() * 6) + 1
self.parseContent(node, `[.footer-die]#${dice[rndInt1]} ${dice[rndInt2]}#`)
}
})
}

// segmentTreeProcessor processes all sections with the `segment` style by
// assigning them the right IDs and seting a numeric title.
function segmentTreeProcessor () {
Expand Down Expand Up @@ -341,5 +383,6 @@ module.exports.register = function (registry) {
this.treeProcessor(segmentTreeProcessor)
this.treeProcessor(choicesTreeProcessor)
this.treeProcessor(shuffleTreeProcessor)
this.treeProcessor(diceFooterProcessor)
})
}
24 changes: 24 additions & 0 deletions src/styles/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,30 @@ table.dicetable td p {
width: 32px;
}

/*******************************************************************************
* Dice Footer
******************************************************************************/
.footer-die {
display: none;
font-size: 1.5em;
}

.footer-die {
position: running(footer-die);
}

@page gameplay :right {
@bottom-center {
content: element(footer-die, first)
}
}

@page gameplay :left {
@bottom-center {
content: element(footer-die, first)
}
}

/*******************************************************************************
* Counter
******************************************************************************/
Expand Down
Loading