From 7ba65aa25d2103a58dd1a56169df71f59a8aad4c Mon Sep 17 00:00:00 2001 From: njlyon0 Date: Thu, 25 Apr 2024 15:38:54 -0400 Subject: [PATCH] Exploring a function variant for the bits that I am more confident in --- dev/encounter_creator.R | 114 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/dev/encounter_creator.R b/dev/encounter_creator.R index 377bd29..65a3679 100644 --- a/dev/encounter_creator.R +++ b/dev/encounter_creator.R @@ -79,11 +79,123 @@ if(current_cost < (max_xp - min(creatures_avail$creature_xp))){ creature_pick2$creature_name)), party_size = pc_size) - # If above allowed XP cost, drop creatures of this value of XP and try again with one tier lower + # If above allowed XP cost... if(attempt_cost > max_xp){ + + # Information message message("Exceeding allowable XP cost of encounter") + + # # Drop creatures of this value of XP and try again with one tier lower + # creatures_avail <- creatures_avail %>% + # dplyr::filter() + } + + +} + + +## --------------------------- ## +# Function Exploration ---- +## --------------------------- ## + +encounter_creator <- function(enemy_type = "undead", difficulty = "deadly", + party_level = 5, party_size = 4, balance = "tougher"){ + + # If enemy type is null + if(is.null(enemy_type) == TRUE){ + + # Just simplify the creature data + available <- dndR::creatures %>% + dplyr::filter(creature_xp > 0 & !is.na(creature_xp)) %>% + dplyr::select(creature_type, creature_name, creature_xp) + + # If enemy type is not null & is in the data + } else if(is.null(enemy_type) != TRUE & + stringr::str_detect(string = creature$creature_type, + pattern = enemy_type) == TRUE){ + + # Simplify **and filter** the creature data + available <- dndR::creatures %>% + dplyr::filter(creature_xp > 0 & !is.na(creature_xp)) %>% + dplyr::select(creature_type, creature_name, creature_xp) %>% + dplyr::filter(stringr::str_detect(string = creature_type, pattern = enemy_type)) + + # If enemy type is not null but isn't in the data + } else if(is.null(enemy_type) != TRUE & + stringr::str_detect(string = creature$creature_type, + pattern = enemy_type) == FALSE){ + + # Warning message + rlang::warn("Enemy type not found in data. Including all enemy types as possibilities") + + # Do same processing as when it isn't supplied + available <- dndR::creatures %>% + dplyr::filter(creature_xp > 0 & !is.na(creature_xp)) %>% + dplyr::select(creature_type, creature_name, creature_xp) + + } # Close conditional + + + + # HERE NOW ---- + + + + + # Check available XP manually + (max_xp <- xp_pool(party_level = pc_level, party_size = pc_size, difficulty = enc_diff) ) + + # Identify creature of the highest possible value still less than the max + creature_pick <- creatures_avail %>% + dplyr::filter(creature_xp == max(creature_xp[creature_xp < max_xp], na.rm = TRUE)) + + # If more than one, pick one at random + if(nrow(creature_pick) > 1){ + creature_pick <- creature_pick[sample(x = 1:nrow(creature_pick), size = 1), ] } + # Compare to XP cost + current_cost <- xp_cost(monster_xp = sum(creature_pick$creature_xp), + monster_count = nrow(creature_pick), + party_size = pc_size) + + # If that's less than the allowed, try to grab another + if(current_cost < (max_xp - min(creatures_avail$creature_xp))){ + + # Calculate difference in XP + remaining_xp <- max_xp - current_cost + + # Filter again + creature_pick2 <- creatures_avail %>% + dplyr::filter(creature_xp == max(creature_xp[creature_xp < remaining_xp], na.rm = TRUE)) + + # If more than one, pick one at random + if(nrow(creature_pick2) > 1){ + creature_pick2 <- creature_pick2[sample(x = 1:nrow(creature_pick2), size = 1), ] + } + + # Check XP cost again + attempt_cost <- xp_cost(monster_xp = sum(creature_pick$creature_xp, + creature_pick2$creature_xp), + monster_count = length(c(creature_pick$creature_name, + creature_pick2$creature_name)), + party_size = pc_size) + + # If above allowed XP cost... + if(attempt_cost > max_xp){ + + # Information message + message("Exceeding allowable XP cost of encounter") + + # # Drop creatures of this value of XP and try again with one tier lower + # creatures_avail <- creatures_avail %>% + # dplyr::filter() + } + + + } + + }