Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Barbarus/saving_throw.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
105 lines (87 sloc)
3.93 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from character import Character | |
def print_saving_throw_info(character: Character): | |
"""Print all saving throw bonuses for the character""" | |
print("\nSaving Throw Bonuses:") | |
abilities = ["strength", "dexterity", "constitution", "intelligence", "wisdom", "charisma"] | |
for ability in abilities: | |
bonus = character.get_saving_throw_bonus(ability) | |
prof = "✓" if character.saving_throws[ability] else " " | |
adv = "ADV" if character.saving_throw_advantages[ability] else " " | |
mod = getattr(character, f"{ability}_mod") | |
prof_bonus = character.proficiency_bonus if character.saving_throws[ability] else 0 | |
print(f"{ability.capitalize():12} {prof} {adv} : {bonus:+2d} = {mod:+2d} ability mod {prof_bonus:+2d} prof bonus") | |
def roll_saving_throw(character: Character, ability: str, advantage_status: str = 'n') -> None: | |
"""Roll a saving throw with advantage/disadvantage""" | |
# Check if character has inherent advantage on this save | |
has_advantage = character.saving_throw_advantages[ability] | |
if has_advantage: | |
if advantage_status == 'd': | |
print(f"(Note: You have advantage on {ability} saves, this cancels out the disadvantage)") | |
advantage_status = 'n' | |
else: | |
advantage_status = 'a' | |
print(f"(Rolling with advantage due to character ability)") | |
# Get first roll | |
total1, roll1, is_proficient = character.roll_saving_throw(ability) | |
# Get second roll if advantage/disadvantage | |
total2 = None | |
if advantage_status in ['a', 'd']: | |
total2, roll2, _ = character.roll_saving_throw(ability) | |
# Calculate final result | |
if advantage_status == 'a': | |
final_total = max(total1, total2) | |
final_roll = max(roll1, roll2) | |
print(f"Rolling with advantage: {roll1} and {roll2}, using {final_roll}") | |
elif advantage_status == 'd': | |
final_total = min(total1, total2) | |
final_roll = min(roll1, roll2) | |
print(f"Rolling with disadvantage: {roll1} and {roll2}, using {final_roll}") | |
else: | |
final_total = total1 | |
final_roll = roll1 | |
print(f"Rolling: {final_roll}") | |
# Show modifier breakdown | |
mod = getattr(character, f"{ability}_mod") | |
prof_bonus = character.proficiency_bonus if is_proficient else 0 | |
print(f"Adding: {mod:+d} {ability} modifier {prof_bonus:+d} proficiency bonus") | |
print(f"Final {ability.capitalize()} save: {final_total}") | |
def main(): | |
character = Character("Your Character") | |
while True: | |
print("\nAvailable commands:") | |
print("1. Show saving throw bonuses") | |
print("2. Roll a saving throw") | |
print("3. Exit") | |
choice = input("\nEnter choice (1-3): ") | |
if choice == "1": | |
print_saving_throw_info(character) | |
elif choice == "2": | |
print("\nChoose ability:") | |
print("1. Strength") | |
print("2. Dexterity") | |
print("3. Constitution") | |
print("4. Intelligence") | |
print("5. Wisdom") | |
print("6. Charisma") | |
ability_map = { | |
"1": "strength", | |
"2": "dexterity", | |
"3": "constitution", | |
"4": "intelligence", | |
"5": "wisdom", | |
"6": "charisma" | |
} | |
ability_choice = input("Choose ability (1-6): ") | |
if ability_choice in ability_map: | |
ability = ability_map[ability_choice] | |
advantage = input("Roll with (a)dvantage, (d)isadvantage, or (n)ormal?: ").lower() | |
print() # blank line for readability | |
roll_saving_throw(character, ability, advantage) | |
else: | |
print("Invalid ability choice") | |
elif choice == "3": | |
break | |
else: | |
print("Invalid choice") | |
if __name__ == "__main__": | |
main() |