1
0
Fork 0
mirror of https://github.com/Oreolek/gamebookformat.git synced 2024-05-15 07:28:18 +03:00

Codewords and items.

Need to think more about design and code is not pretty.
This commit is contained in:
Pelle Nilsson 2013-06-07 00:14:06 +02:00
parent f723ee0a9c
commit 1bf91169c5
19 changed files with 87 additions and 48 deletions

View file

@ -3,8 +3,8 @@ Demonstrating how codewords (AKA sightings) can be used.
Go to [[second]].
* 2 second
Simple enough to set. Turn to [[choice]].
FIXME not actually setting anything here yet.
[set]warrior[/set]
Simple enough to set a codeword. Turn to [[choice]].
* choice
If you have the codeword [has]warrior[/has] you may
@ -17,8 +17,9 @@ This is just to demonstrate choices allowed when not
having a codeword. Now go on to [[forced]].
* forced
OK, if you have the codeword [has forced]warrior[/has], turn to
[[the_end]].
OK, if you have the codeword [has]warrior[/has], turn to
[[auto the_end]] otherwise go back to [[second]]. Although
we both know you have that codeword.
* the_end
That was easy.

View file

@ -8,11 +8,11 @@ Here you find a [found]key[/found]. Go on to [[door]].
* door
There is a locked door here.
If you have a [has]key[/has] you can use that to
If you have a [carries]key[/carries] you can use that to
open the door, see [[inside]]. Being in the same sentence
should be enough for the formatter to figure out that the
key is required to be allowed to follow the link.
Else you can try to open with the [has]sword[/has],
Else you can try to open with the [carries]sword[/carries],
if you have it, see [[attempt_break_door_with_sword]].
Hopefully the same-sentence magic is enough to pair
pre-conditions to links, or more markup must be added later.

View file

@ -23,7 +23,7 @@ class OutputFormat (object):
refs = []
refsdict = ReferenceFormatter(section, shuffled_sections,
self.format_with_template("section_ref"))
formatted_text = section.format(refsdict)
formatted_text = self.format_section(section, refsdict)
print >> output, self.format_with_template("section", {
'nr' : shuffled_sections.to_nr[section],
'name' : section.name,
@ -31,6 +31,56 @@ class OutputFormat (object):
'refs' : '\n'.join(refsdict.getfound()) # hack for DOT output
}),
def format_section(self, section, references):
i = 0
res = ""
while i < len(section.text):
ref_start = section.text.find('[[', i)
tag_start = section.text.find('[', i)
if ref_start >= 0 and ref_start <= tag_start:
res += section.text[i:ref_start]
ref_end = section.text.find(']]', ref_start)
if ref_end > ref_start:
ref = section.text[ref_start+2:ref_end]
splitref = ref.split()
if len(splitref) > 1:
for refmod in splitref[:-1]:
res += self.format_with_template(refmod,
references)
res += references[splitref[-1]]
i = ref_end + 2
else:
raise Exception('Mismatched ref start [[ in section %s' %
self.name)
elif tag_start >= 0:
res += section.text[i:tag_start]
tag_end = section.text.find(']', tag_start)
if tag_end < 0:
raise Exception('Mismatched tag start [ in section %s' %
self.name)
tag = section.text[tag_start+1:tag_end].strip()
tagname = tag.split()[0].strip()
end_tag_start = section.text.find('[', tag_end)
if (not end_tag_start > tag_end
and section.text[end_tag_start].startswith('[/' + tagname
+ ']')):
raise Exception('Bad format %s tag in %s.' % (
tag, self.name))
inner = section.text[tag_end+1:end_tag_start]
# FIXME this pollutes the mutable references object
references['inner'] = inner
f = self.format_with_template(tag.replace(' ', '_'),
references)
if len(f) > 0:
res += f
else:
res += inner
i = section.text.find(']', end_tag_start) + 1
else:
res += section.text[i:]
break
return res
def write_empty_section(self, nr, output):
print >> output, self.format_with_template("empty_section", {
'nr' : nr,
@ -53,8 +103,11 @@ class ReferenceFormatter (object):
self.shuffled_sections = shuffled_sections
self.found = set()
self.ref_template = ref_template
self.items = {}
def __getitem__(self, key):
if key in self.items:
return self.items[key]
to_section = self.shuffled_sections.from_name[key]
res = self.ref_template % {
'nr' : self.shuffled_sections.to_nr[to_section],
@ -66,3 +119,9 @@ class ReferenceFormatter (object):
def getfound(self):
return list(self.found)
def __setitem__(self, key, value):
self.items[key] = value
def __delitem__(self, key):
del self.items[key]

View file

@ -18,43 +18,8 @@ class Section:
repr(self.tags))
def format(self, references):
i = 0
res = ""
while i < len(self.text):
ref_start = self.text.find('[[', i)
tag_start = self.text.find('[', i)
if ref_start >= 0 and ref_start <= tag_start:
res += self.text[i:ref_start]
ref_end = self.text.find(']]', ref_start)
if ref_end > ref_start:
ref = self.text[ref_start+2:ref_end]
res += references[ref]
i = ref_end + 2
else:
raise Exception('Mismatched ref start [[ in section %s' %
self.name)
elif tag_start >= 0:
res += self.text[i:tag_start]
tag_end = self.text.find(']', tag_start)
if tag_end < 0:
raise Exception('Mismatched tag start [ in section %s' %
self.name)
tag = self.text[tag_start+1:tag_end]
end_tag_start = self.text.find('[', tag_end)
if (not end_tag_start > tag_end
and self.text[end_tag_start].startswith('[/' + tag + ']')):
raise Exception('Bad format %s tag in %s.' % (
tag, self.name))
tagtext = self.text[tag_end+1:end_tag_start]
print tag, tagtext
#FIXME actually handle tags
res += tagtext
i = self.text.find(']', end_tag_start) + 1
else:
res += self.text[i:]
break
return res
pass
class ShuffledSections:
def __init__(self, as_list, from_nr, to_nr, from_name):
self.as_list = as_list

View file

@ -32,4 +32,3 @@ class Templates (object):
return os.path.join(templatedir,
self.extension,
name + "." + self.extension)

View file

@ -0,0 +1 @@
((automatic))

View file

@ -0,0 +1 @@
[CARRIES]%(inner)s[/CARRIES]

View file

@ -0,0 +1 @@
[DROP]%(inner)s[/DROP]

View file

@ -0,0 +1 @@
[FOUND]%(inner)s[/FOUND]

View file

@ -0,0 +1 @@
[HAS]%(inner)s[/HAS]

View file

@ -0,0 +1 @@
[HAS NOT]%(inner)s[/HAS NOT]

View file

@ -0,0 +1 @@
SET CODEWORD %(inner)s.

View file

@ -0,0 +1 @@
[TAKE]%(inner)s[/TAKE]

1
templates/tex/found.tex Normal file
View file

@ -0,0 +1 @@
\textbf{%(inner)s}

1
templates/tex/has.tex Normal file
View file

@ -0,0 +1 @@
\textit{%(inner)s}

1
templates/tex/hasnot.tex Normal file
View file

@ -0,0 +1 @@
\textit{%(inner)s}

1
templates/tex/set.tex Normal file
View file

@ -0,0 +1 @@
Codeword \textit{%(inner)s}.

1
templates/tex/take.tex Normal file
View file

@ -0,0 +1 @@
\textbf{%(inner)s}

View file

@ -1,4 +1,4 @@
* TODO [11/26] [42%]
* TODO [13/43] [30%]
- [X] Debug output
- [X] DOT output
- [X] LaTeX output
@ -12,14 +12,16 @@
- [X] Add section links in LaTeX output.
- [X] Prettier LaTeX output
Look at how some existing gamebooks are formatted.
- [ ] Parse wiki-style tags used to mark up sections
- [ ] New text formatting style for section references
- [X] Parse wiki-style tags used to mark up sections
- [X] New text formatting style for section references
- [ ] Inventory pick up items
- [ ] Codewords set
- [ ] Check if has inventory item
- [ ] Check if has codeword
- [ ] Remove item from inventory (forced by instructions)
- [ ] Optionally remove inventory item to make a choice
- [ ] Fix problem with map file from other book sometimes crashing formatter
- [ ] Keys from formatted book should be added to map file, not replace it
- [ ] More formatting possibilities in sections
Look at existing gamebooks to get ideas.
- [ ] Book option to set max section number to use