library(import5eChar)
library(knitr)
library(rmarkdown)
library(kableExtra)
library(dplyr)
library(magrittr)
library(ogbox)
library(bindrcpp)
knitr::opts_chunk$set(echo = FALSE)

boldify = function(x){
    paste0('\\textbf{',x,'}')
}

cleanBackSl = function(x){
     x = gsub(x = x, pattern = '\\', replacement = "/" , fixed=  TRUE) 
     return(x)
}

# import your character here
# example:
# char = importCharacter(file = 'a local file')
# char = importCharacter(regex = 'regular expression that matches the filename in google drive')
# char = importCharacter(fileID = 'google drive file id')
# by default a sheet for an example character is created
characterFile <- system.file("Tim_Fighter5", package = "import5eChar")
char = import5eChar::importCharacter(file = characterFile)

r char$Name

# 
# kable(table,format= 'latex',booktabs = TRUE,col.names = NULL,align = 'l',escape=FALSE) %>%
#   kable_styling(position = 'center', latex_options = "scale_down")
AC = AC(char=char)

init = initBonus(char= char)


c1 = c('Class \\& Level',
       'Race',
       'Backrground',
       'Alignment',
       'Proficiency') %>% boldify()

c2 = c(char$ClassField,
       char$Race,
       char$Background,
       char$Alignment,
       char$proficiencyBonus) %>% cleanBackSl()
table = cbind(c1,c2)
kable(table,format= 'latex',booktabs = TRUE,col.names = NULL,align = 'l',escape=FALSE) %>%
    kable_styling(position = 'center', latex_options = c("HOLD_position","scale_down"))


table = data.frame(Score = char$abilityScores ,
           Modifier = char$abilityMods,
           Prof. = char$abilityProf %>% replaceElement(c('TRUE' = '\\XBox',
                                               "FALSE" = '\\Square')) %$% newVector,
           Save =  saveBonus(char = char))
rownames(table) %<>% boldify()

kable(table %>% t, format = 'latex',booktabs= TRUE,escape=FALSE)%>%
    kable_styling(position = 'center', latex_options = c("scale_down","HOLD_position"))
r1 = c(AC,
       init,
       char$speedMiscMod + char$baseSpeed,
       char$maxHealth,
       paste0('\\makecell{',char$hitDice %>% paste(collapse='\\\\'),'}'))

r2 = c('AC',
       'Initiative',
       'Speed',
       'HP',
       'Hit Dice') %>% boldify()


table = rbind(r1,r2)

kable(table,format= 'latex',booktabs = TRUE,col.names = NULL,align = 'c',escape=FALSE,row.names=FALSE) %>%
    kable_styling(position = 'center', latex_options = c("HOLD_position","scale_down"))

Skills

skillBonus = skillBonus(char = char)

profMark = char$skillProf %>% replaceElement(c('TRUE' = '\\XBox',
                                               "FALSE" = '\\Square')) %$% newVector

statSep = char$skillAttributes %>% duplicated() %>% not %>% which %>% {.[-1] -1}

skillAttributes = unique(char$skillAttributes)
attributeRows = data.frame(skillName = skillAttributes %>% boldify(),
                           profMark='',
                           skillBonus = '',
                           index = c(0,statSep+.1))



charAts = data.frame(skillName = names(skillBonus),
                     profMark,
                     skillBonus,
                     index = 1:length(skillBonus))

charAts %<>% rbind(attributeRows) %>% arrange(index) %>% select(-index)


charAts1 = charAts[1:12,]
charAts2 = rbind(charAts[13:nrow(charAts),],data.frame(skillName = '',
                                                       profMark = '',
                                                       skillBonus = ''))
charAts = cbind(charAts1,charAts2)

rownames(charAts) = NULL


out = kable(charAts, format = 'latex',booktabs= TRUE,escape=FALSE,col.names = NULL,linesep= '')%>%
    kable_styling(position = 'center',latex_options = "HOLD_position")

tableLines =out %>% stringr::str_replace_all(pattern = "\n\\\\hline", 
        "") %>% stringr::str_split("\n") %>% {
        .[[1]]  }

tableLines[3] = "\\begin{tabular}{lll|lll}"
outTable = tableLines
attributes(outTable) = attributes(out)
outTable

Weapons

if(length(char$weapons)>0){
    weaponTable = char$weapons %>% sapply(function(x){
    c(x$name %>% cleanBackSl(),
      weaponBonus(x,char=char)['weaponTypeAttackBonus'] +
          x$proficient*char$proficiencyBonus +
          char$abilityMods[x$attackStat],
      paste0(paste(x$dice,collapse=' + '),
             '+',
             weaponBonus(x,char=char)['weaponTypeDamageBonus'] + 
                 char$abilityMods[x$attackStat]),
      x$damageType,
      x$range %>% cleanBackSl()
      )
}) %>% t

colnames(weaponTable) = c('Name','Attack','Damage','Type','Range')
weaponTable[,'Damage'] %<>% gsub('\\+\\-','-',x=.)

kable(weaponTable, format = 'latex',booktabs= TRUE,escape=FALSE,row.names=FALSE,linesep= '')%>%
    kable_styling(position = 'center', latex_options = c("HOLD_position","scale_down"))
}

\newpage

Resource Summary

char$resources %>% select(name,ResourceDisplay) %>%
    mutate(name =  cleanBackSl(name)) %>% 
    kable(format='latex',booktabs = TRUE,escape = FALSE,row.names=FALSE,col.names=NULL,linesep='') %>%
    kable_styling(position = 'center', latex_options = c("HOLD_position"))

Features

r char$Features %>% cleanBackSl() %>% gsub(pattern = '\n',replacement = '\n\n',x = .)

Proficiencies

Armor: r char$ArmorProficiencies %>% cleanBackSl() %>% gsub(pattern = '\n',replacement = '\n\n',x = .)

Weapon: r char$WeaponProficiencies %>% cleanBackSl() %>% gsub(pattern = '\n',replacement = '\n\n',x = .)

Tool: r char$ToolProficiencies %>% cleanBackSl() %>% gsub(pattern = '\n',replacement = '\n\n',x = .)

Languages: r char$LanguagesKnown %>% cleanBackSl() %>% gsub(pattern = '\n',replacement = '\n\n',x = .)

Personality

Traits: r char$personality$traits %>% cleanBackSl() %>% gsub(pattern = '\n',replacement = '\n\n',x = .)

Ideals: r char$personality$ideals %>% cleanBackSl() %>% gsub(pattern = '\n',replacement = '\n\n',x = .)

Bonds: r char$personality$bonds %>% cleanBackSl() %>% gsub(pattern = '\n',replacement = '\n\n',x = .)

Flaws: r char$personality$flaws %>% cleanBackSl() %>% gsub(pattern = '\n',replacement = '\n\n',x = .)

Equipment

r char$Equipment %>% cleanBackSl() %>% gsub(pattern = '\n',replacement = '\n\n',x = .)

Currency: `r { currency = char$currency %>% unlist

currencyChar = length(currency):1 %>% sapply(function(i){ paste0(currency[i],' ', names(currency)[i]) }) %>% paste(collapse=', ') currencyChar }`

Spells

DC = spellDC(char = char)
spellAttack  =  spellAttack(char = char)

Spell DC: r DC

Spell Attack: r spellAttack

if(!is.null(char$spells)){

    groups = char$spells$level %>% duplicated %>% not %>% which
    groups = groups -1
    groups %<>% c(nrow(char$spells))
    out = char$spells %>% mutate(prepared = prepared %>%replaceElement(c('TRUE' = '\\XBox',
                                               "FALSE" = '\\Square')) %$% newVector) %>%
        select(-level) %>% 
        kable(format='latex',booktabs = TRUE,escape = FALSE,row.names=FALSE,col.names=NULL,linesep='')  %>% 
        kable_styling(position = 'center', latex_options = c("HOLD_position"))
    availableLevels = unique(char$spells$level)
    for(i in seq_along(availableLevels)){
        if (availableLevels[i] ==0){
            text=  'Cantrip'
        } else{
            text = paste('Level',availableLevels[i],'(',char$spellSlots[availableLevels[i] %>% as.character()],')')
        }
        out = out %>% kableExtra::group_rows(group_label = text,
                                 start_row = groups[i]+1,
                                 end_row = groups[i+1]-1+1, latex_gap_space = "1em")
    }
    out
}

Notes

r char$notes %>% gsub(pattern = '\n',replacement = '\n\n',x = .)



oganm/import5eChar documentation built on Dec. 22, 2020, midnight