Skip to content

Commit

Permalink
MSavage latex template: Overflow spell sheets
Browse files Browse the repository at this point in the history
The fillable pdf format has overflow sheets for when
the number of spells exceeds the number of spells
for one spell page. This patch implements such
overflow sheets for the latex character sheets.
  • Loading branch information
PJBrs committed Aug 24, 2024
1 parent febf095 commit 0b16ccd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
12 changes: 3 additions & 9 deletions dungeonsheets/forms/MSavage_template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,6 @@

\OrganizationName{[[ char.org_name ]]}

[% if char.is_spellcaster %]
%Magic
[[ char | spellsheetparser ]]
[% endif %]


\begin{document}
\newgeometry{left=0cm,right=0cm,top=0cm,bottom=0cm}
\onecolumn
Expand All @@ -242,10 +236,10 @@
\pdfbookmark[0]{Background Sheet}{Background Sheet}
\renderbackgroundsheet

[% if char.is_spellcaster %]
[% if char.is_spellcaster -%]
% SPELLCASTING PAGE
\pdfbookmark[0]{Spellcasting Sheet}{Spellcasting Sheet}
\spellsheetchoice
[% endif %]
[[ char | spellsheetparser ]]
[%- endif -%]

\end{document}
81 changes: 55 additions & 26 deletions dungeonsheets/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ def rst_to_latex(rst, top_heading_level: int=0, format_dice: bool = True, use_dn

return tex


def rst_to_boxlatex(rst):
"""Adapted rst translation from dungeonsheets latex module, removing
dice parsing and indentation."""
Expand All @@ -262,6 +263,7 @@ def rst_to_boxlatex(rst):
tex = tex.replace('\n\n', ' \\\\\n')
return tex


def msavage_spell_info(char):
"""Generates the spellsheet for msavage template."""
headinfo = char.spell_casting_info["head"]
Expand Down Expand Up @@ -310,33 +312,60 @@ def msavage_spell_info(char):
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z"]

# spellList is a dict.
# The keys in spellList are the level names, the values are lists of tuples:
# Each tuple consists of two elements: a spell name and a boolean for prepared.
# So: spellList = {levelname : [(spellname, preparedbool), ]}
spellList = char.spell_casting_info["list"]
# As default, assume fullcaster character and set spellsheet accordingly
tex4="\\newcommand{\spellsheetchoice}{\\renderspellsheet}"
for k, v in spellList.items():
slots_max = fullcaster_sheet_spaces[k]
halfcaster_slots_max = halfcaster_sheet_spaces[k]
only_low_level = all((char.spell_slots(level) == 0 for level in range(6, 10)))
# Determine which sheet to use (caster or half-caster).
# Prefer caster, unless we have no spells > 5th level and
# would overflow the caster sheet, then use half-caster.
if len(v) > slots_max and only_low_level:
slots_max=halfcaster_slots_max
tex4="\\newcommand{\spellsheetchoice}{\\renderhalfspellsheet}"
if len(v) > slots_max:
vsel = sorted(v, key=lambda x: x[1], reverse=True)
else:
vsel = v[:]
for spinfo, slot in zip(vsel[:slots_max], comp_letters):
slot_command = "\\"+k+'Slot'+slot
slot_command_name = slot_command+"{"+spinfo[0]+"}"
if k == "Cantrip":
texT = texT + [slot_command_name]
continue
slot_command_prep = slot_command+"Prepared"+"{"+str(spinfo[1])+"}"
texT = texT + [slot_command_name, slot_command_prep]
tex3 = "\n".join(texT) + '\n'
return "\n".join([tex1, tex2, tex3, tex4])
# Default, assume fullcaster character and associated spellsheet.
fullcaster = True
# Determine which sheet to use (fullcaster or halfcaster).
# Only use halfcaster when we have no spells > 5th level and
# would overflow the fullcaster sheet.
# Keep the same sheet for overflow pages, if any.
only_low_level = all((char.spell_slots(level) == 0 for level in range(6, 10)))
if (any(len(spellList[key]) > fullcaster_sheet_spaces[key] for key in spellList.keys())
and only_low_level):
fullcaster = False

def AddSpellPage(fullcaster = True):
texT = []
for k, v in spellList.items():
spells_this_level_and_page = len(v)
if fullcaster:
spellsheet_command = "\\renderspellsheet"
slots_max = fullcaster_sheet_spaces[k]
else:
spellsheet_command = "\\renderhalfspellsheet"
slots_max = halfcaster_sheet_spaces[k]
for spinfo, slot in zip(v[:slots_max], comp_letters):
spellList[k].remove(spinfo)
slot_command = "\\"+k+'Slot'+slot
slot_command_name = slot_command+"{"+spinfo[0]+"}"
if k == "Cantrip":
texT = texT + [slot_command_name]
continue
slot_command_prep = slot_command+"Prepared"+"{"+str(spinfo[1])+"}"
texT = texT + [slot_command_name, slot_command_prep]
# Set remaining slots empty
for empty_slot in comp_letters[spells_this_level_and_page:slots_max]:
slot_command = "\\"+k+'Slot'+empty_slot
slot_command_name = slot_command+"{}"
if k == "Cantrip":
texT = texT + [slot_command_name]
continue
slot_command_prep = slot_command+"Prepared{False}"
texT = texT + [slot_command_name, slot_command_prep]
if (not len(spellList[k]) == 0
and not spellList[k][0][0] == "--- Overflow ---"):
spellList[k].insert(0, ("--- Overflow ---", False))
return "\n".join(texT) + '\n\n' + spellsheet_command + '\n\n'

tex3 = ""
while any(spellList.values()):
tex3 = tex3 + AddSpellPage(fullcaster)
return "\n".join([tex1, tex2, tex3])


def RPGtex_monster_info(char):
"""Generates the headings for the monster block info in the DND latex style"""
Expand Down

0 comments on commit 0b16ccd

Please sign in to comment.