R/cr_convert.R

Defines functions cr_convert

Documented in cr_convert

#' @title Convert Challenge Rating to Experience Points
#'
#' @description Converts challenge rating (CR) into experience points (XP) using two formulas for a parabola (one for CR less than/equal to 20 and one for greater than 20). The relationship between CR and XP in the Dungeon Master's Guide (DMG) is disjointed in this way so this is a reasonable move. Accepts '1/8', '1/4, and '1/2' in addition to numbers between 1 and 30.
#'
#' @param cr (numeric) Challenge rating for which you want to calculate experience points
#'
#' @return (numeric) value of XP equivalent to the user-supplied challenge rating
#' @importFrom magrittr %>%
#'
#' @export
#'
cr_convert <- function(cr = NULL){

  # Challenge rating must be specified as a single value in the supported set
  if(is.null(cr) || length(cr) != 1 || cr %in% c(0, "1/8", "1/4", "1/2", 0.125, 0.25, 0.5, 1:30) != TRUE)
    stop("'cr' must be one of '0', '1/8', '1/4', '1/2' or any number between 1 and 30")

  # Handle fraction CRs
  if(cr == "1/8"){ cr <- 1/8 }
  if(cr == "1/4"){ cr <- 1/4 }
  if(cr == "1/2"){ cr <- 1/2 }

  # Calculate XP depending on XP > / < than 20
  ## CR less than or equal to 20
  if(cr <= 20){ xp_raw <- ((56.8889 * cr^2) + (31.111 * cr) + 112.0001) }

  ## CR greater than 20
  if(cr > 20){ xp_raw <- ((642.8571 * cr^2) + (-19071.43 * cr) + 150000) }

  # Round this (up) to an integer
  xp <- base::ceiling(x = xp_raw)

  # Return XP
  return(xp) }

Try the dndR package in your browser

Any scripts or data that you put into this service are public.

dndR documentation built on June 11, 2025, 5:09 p.m.