#!/usr/bin/python3
import json
import random
from equipment import *
from spells import *

# functions
def roll_dice(count, sides):
    return sum(random.randint(1,sides) for _ in range(count))

# Player Character Classes
class Adventurer:
    def __init__(self, c_id: str, level=1, attributes={}) -> None:
        # using a get() method to pull an attribute else use a default value, https://python-academy.org/en/handbook/get
        self.identifier = c_id
        self.player_class = None
        self.level = level
        self.strength     = attributes.get('strength', roll_dice(3,6))
        self.intelligence = attributes.get('intelligence', roll_dice(3,6))
        self.wisdom       = attributes.get('wisdom', roll_dice(3,6))
        self.dexterity    = attributes.get('dexterity', roll_dice(3,6))
        self.constitution = attributes.get('constitution', roll_dice(3,6))
        self.charisma     = attributes.get('charisma', roll_dice(3,6))
        self.hp_rolls = []
        self.hp = 1
        self.atk = 0
        self.gold= roll_dice(3,6)
        self.torches = roll_dice(1,6)
        self.rations =  roll_dice(1,6)
        self.equipment = self.roll_equipment() 
        # all armor, individual classes may have overrides
        self.possible_armor = list(armor.keys())
        # all weapons, individual classes may have overrides
        self.possible_weapons = weapons
        self.possible_melee_weapons = list(filter(lambda d: 'melee' in d['traits'], weapons))
        self.possible_missile_weapons = list(filter(lambda d: 'missile' in d['traits'], weapons))
        # each character should get 1 melee and 1 random (missle or melee)
        self.weapons = [ random.choice(self.possible_melee_weapons), random.choice(self.possible_weapons) ]
        self.armor = random.choice(self.possible_armor)
        # special abilities
        self.spells = None
        self.spell_book = None
        self.thief_skills = None
        self.turn_undead = None

    def __str__(self):
        return f"{self.player_class}"

    def get_subclass_dict():
        subclasses = {}
        for subclass in Adventurer.__subclasses__():
            subclasses[subclass.adv_class] = subclass
        return subclasses

    def get_json(self):
        char_dict = self.__dict__
        char_json = json.dumps(char_dict)
        return char_dict

    def vertical_sheet(self):
        sheet = []
        sheet.append('{0: <28}'.format(f"| {self.player_class.title()} - Level {self.level}"))
        for key, val in self.get_attributes().items():
            key_string = "| " + '{0:16}'.format(f"{key}").capitalize() + f" {val}"
            sheet += ['{0: <28}'.format(key_string)]
        sheet.append('| ------------------------- ')
        sheet.append('{0: <28}'.format(f"| HP: {self.hp}  AC: {self.ac}"))
        sheet.append('{0: <28}'.format(f"| Torches: {self.torches}"))
        sheet.append('{0: <28}'.format(f"| Rations: {self.rations}"))
        sheet.append('{0: <28}'.format(f"| Armor: {self.armor}"))
        sheet.append('{0: <28}'.format(f"| Weapons:")) 
        sheet.append('{0: <28}'.format(f"|   {self.weapons[0]['name'].title()}"))
        sheet.append('{0: <28}'.format(f"|   {self.weapons[1]['name'].title()}"))
        sheet.append('{0: <28}'.format(f"| Equipment:")) 
        for item in self.equipment:
            item_string = "|   " + '{0:22}'.format(f"{item}").title()
            sheet += ['{0: <28}'.format(item_string)]
        sheet.append('{0: <28}'.format(f"| ")) 
        sheet.append('{0: <28}'.format(f"| Gold: {self.gold}")) 
        sheet.append('| ------------------------- ')
        for key, val in self.progression[self.level]['saves'].items():
            key_string = "| " + '{0:22}'.format(f"{key}").title() + f" {val}"
            sheet += ['{0: <28}'.format(key_string)]
        sheet.append('| ------------------------- ')
        #if self.spells:
        #    sheet.append('{0: <28}'.format("| Spellbook: "))
        #    sheet += ['{0: <28}'.format(f"|   {spell.title()}") for spell in self.spell_book ]
        # append a | to each string, after the formatted whitespace
        sheet = [ line + "|" for line in sheet ]
        return sheet

    def full_sheet(self):
        sheet = { 1: [], 2 : [] }
        sheet[1].append('{0: <60}'.format(f"| Character Name:"))
        sheet[1].append('{0: <60}'.format(f"| Player Name :"))
        sheet[1].append('{0: <60}'.format(f"| Class: {self.player_class.title()}   Alignment:"))
        sheet[1].append('{0: <60}'.format(f"| Title:                Level {self.level}"))
        sheet[1].append('| ------------------------------------------ ')
        sheet[1].append('{0: <60}'.format(f"|   ABILITY SCORES\t\t\tSAVING THROWS"))
        sheet[1].append('{0: <60}'.format(f"| Strength:\t{self.strength}        Death: {self.ac}"))
        sheet[1].append('{0: <60}'.format(f"| Intelligence:{self.intelligence}  Wands: {self.ac}"))
        sheet[1].append('{0: <60}'.format(f"| Wisdom:\t {self.wisdom}           Para: {self.ac}"))
        sheet[1].append('{0: <60}'.format(f"| Dexterity:\t{self.dexterity}      Breath: {self.ac}"))
        sheet[1].append('{0: <60}'.format(f"| Constitution: {self.constitution} Spells: {self.ac}"))
        sheet[1].append('{0: <60}'.format(f"| Charisma:\t{self.charisma}        SpellMod: {self.ac}"))
        return sheet

    def get_attributes(self):
        attribute_list = ['strength', 'intelligence', 'wisdom', 'dexterity', 'constitution', 'charisma']
        return {k: self.__dict__[k] for k in attribute_list}

    def get_best_prime_attribute(self):
        attribute_list = [ 'strength', 'intelligence', 'wisdom', 'dexterity' ]
        prime_attributes =  {k: self.__dict__[k] for k in attribute_list}
        # return highest, found this at https://stackoverflow.com/a/280156
        return max(prime_attributes, key=prime_attributes.get) 

    def roll_equipment(self):
        equipment = [ 'backpack', 'tinderbox', 'waterskin' ]
        random_items = [ random.choice(adventuring_gear) for i in range(2) ]
        for item in random_items:
            equipment += item.split("+")
        while len(equipment) < 7:
            equipment.append("")
        return equipment

    def select_spells(self, spell_list):
        spell_book = []
        for spell_level, count in self.spells.items():
            if count != "-":
                for i in range(count):
                    new_spell = ""
                    while new_spell not in spell_book:
                        new_spell = random.choice(spell_list[spell_level])
                        spell_book.append(new_spell)
        return spell_book

    def roll_all_hit_points(self):
        hp_list = []
        prev_hit_dice = 0
        for i in range(self.__class__.max_level):
            new_hit_dice = self.__class__.progression[i]['hit-dice']
            hp_mod = self.__class__.progression[i]['hp-mod']
            hit_dice = new_hit_dice - prev_hit_dice
            prev_hit_dice = new_hit_dice
            # check for constitution bonus to hp
            if hp_mod == 0 and self.constitution >= 13: hp_mod = 1
            if hp_mod == 0 and self.constitution >= 16: hp_mod = 2
            if hp_mod == 0 and self.constitution >= 18: hp_mod = 3
            hp_roll = roll_dice(hit_dice,self.__class__.hit_die) + hp_mod
            hp_list.append(hp_roll)
        return hp_list

    def get_class_progression_for_level(self):
        return list(filter(lambda d: d['level'] == self.level, self.__class__.progression))[0]

    def set_level(self, new_level):
        print('setting level')
        self.level = new_level
        self.hp = sum(self.hp_rolls[:self.level])

    def set_attack_bonus(self):
        prog = self.get_class_progression_for_level()
        thac0 = prog['thac0']
        atk = 19 - thac0
        return atk
        
class Fighter(Adventurer):
    adv_class = "fighter"
    requirements = None
    prime_requisite = "strength"
    hit_die = 8
    max_level = 14
    progression = [
        { "level" : 1,  "xp" : 0,      "hit-dice" : 1, "hp-mod" : 0,  "thac0" : 19, "saves" : { "death / poison" : 12, "wands" : 13, "paralysis / petrify" : 14, "breath attack" :  15, "spells / rods / staves" : 16 }},
        { "level" : 2,  "xp" : 2000,   "hit-dice" : 2, "hp-mod" : 0,  "thac0" : 19, "saves" : { "death / poison" : 12, "wands" : 13, "paralysis / petrify" : 14, "breath attack" :  15, "spells / rods / staves" : 16 }},
        { "level" : 3,  "xp" : 4000,   "hit-dice" : 3, "hp-mod" : 0,  "thac0" : 19, "saves" : { "death / poison" : 12, "wands" : 13, "paralysis / petrify" : 14, "breath attack" :  15, "spells / rods / staves" : 16 }},
        { "level" : 4,  "xp" : 8000,   "hit-dice" : 4, "hp-mod" : 0,  "thac0" : 17, "saves" : { "death / poison" : 10, "wands" : 11, "paralysis / petrify" : 12, "breath attack" :  13, "spells / rods / staves" : 14 }},
        { "level" : 5,  "xp" : 16000,  "hit-dice" : 5, "hp-mod" : 0,  "thac0" : 17, "saves" : { "death / poison" : 10, "wands" : 11, "paralysis / petrify" : 12, "breath attack" :  13, "spells / rods / staves" : 14 }},
        { "level" : 6,  "xp" : 32000,  "hit-dice" : 6, "hp-mod" : 0,  "thac0" : 17, "saves" : { "death / poison" : 10, "wands" : 11, "paralysis / petrify" : 12, "breath attack" :  13, "spells / rods / staves" : 14 }},
        { "level" : 7,  "xp" : 64000,  "hit-dice" : 7, "hp-mod" : 0,  "thac0" : 14, "saves" : { "death / poison" : 8,  "wands" : 9,  "paralysis / petrify" : 10, "breath attack" :  10, "spells / rods / staves" : 12 }},
        { "level" : 8,  "xp" : 120000, "hit-dice" : 8, "hp-mod" : 0,  "thac0" : 14, "saves" : { "death / poison" : 8,  "wands" : 9,  "paralysis / petrify" : 10, "breath attack" :  10, "spells / rods / staves" : 12 }},
        { "level" : 9,  "xp" : 240000, "hit-dice" : 9, "hp-mod" : 0,  "thac0" : 14, "saves" : { "death / poison" : 8,  "wands" : 9,  "paralysis / petrify" : 10, "breath attack" :  10, "spells / rods / staves" : 12 }},
        { "level" : 10, "xp" : 360000, "hit-dice" : 9, "hp-mod" : 2,  "thac0" : 12, "saves" : { "death / poison" : 6,  "wands" : 7,  "paralysis / petrify" : 8,  "breath attack" :  8,  "spells / rods / staves" : 10 }},
        { "level" : 11, "xp" : 480000, "hit-dice" : 9, "hp-mod" : 4,  "thac0" : 12, "saves" : { "death / poison" : 6,  "wands" : 7,  "paralysis / petrify" : 8,  "breath attack" :  8,  "spells / rods / staves" : 10 }},
        { "level" : 12, "xp" : 600000, "hit-dice" : 9, "hp-mod" : 6,  "thac0" : 12, "saves" : { "death / poison" : 6,  "wands" : 7,  "paralysis / petrify" : 8,  "breath attack" :  8,  "spells / rods / staves" : 10 }},
        { "level" : 13, "xp" : 720000, "hit-dice" : 9, "hp-mod" : 8,  "thac0" : 10, "saves" : { "death / poison" : 4,  "wands" : 5,  "paralysis / petrify" : 6,  "breath attack" :  5,  "spells / rods / staves" : 8  }},
        { "level" : 14, "xp" : 840000, "hit-dice" : 9, "hp-mod" : 10, "thac0" : 10, "saves" : { "death / poison" : 4,  "wands" : 5,  "paralysis / petrify" : 6,  "breath attack" :  5,  "spells / rods / staves" : 8  }}
    ]
    def __init__(self, c_id, level, attributes={}) -> None:
        Adventurer.__init__(self, c_id, level, attributes)
        self.player_class = Fighter.adv_class
        self.progression = Fighter.progression
        self.hp_rolls = self.roll_all_hit_points()
        self.hp = sum(self.hp_rolls[:self.level])
        self.ac = armor[self.armor]
        self.atk = self.set_attack_bonus()

class MagicUser(Adventurer):
    adv_class = "magic user"
    requirements = None
    prime_requisite = "intelligence"
    hit_die = 4
    max_level = 14
    progression = [
        { "level" : 1,  "xp" : 0,       "hit-dice" : 1, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 13, "wands" : 14, "paralysis / petrify" : 13, "breath attack" :  16, "spells / rods / staves" : 15 }},
        { "level" : 2,  "xp" : 2500,    "hit-dice" : 2, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 13, "wands" : 14, "paralysis / petrify" : 13, "breath attack" :  16, "spells / rods / staves" : 15 }},
        { "level" : 3,  "xp" : 5000,    "hit-dice" : 3, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 13, "wands" : 14, "paralysis / petrify" : 13, "breath attack" :  16, "spells / rods / staves" : 15 }},
        { "level" : 4,  "xp" : 10000,   "hit-dice" : 4, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 13, "wands" : 14, "paralysis / petrify" : 13, "breath attack" :  16, "spells / rods / staves" : 15 }},
        { "level" : 5,  "xp" : 20000,   "hit-dice" : 5, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 13, "wands" : 14, "paralysis / petrify" : 13, "breath attack" :  16, "spells / rods / staves" : 15 }},
        { "level" : 6,  "xp" : 40000,   "hit-dice" : 6, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 11, "wands" : 12, "paralysis / petrify" : 11, "breath attack" :  11, "spells / rods / staves" : 12 }},
        { "level" : 7,  "xp" : 80000,   "hit-dice" : 7, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 11, "wands" : 12, "paralysis / petrify" : 11, "breath attack" :  11, "spells / rods / staves" : 12 }},
        { "level" : 8,  "xp" : 150000,  "hit-dice" : 8, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 11, "wands" : 12, "paralysis / petrify" : 11, "breath attack" :  11, "spells / rods / staves" : 12 }},
        { "level" : 9,  "xp" : 300000,  "hit-dice" : 9, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 11, "wands" : 12, "paralysis / petrify" : 11, "breath attack" :  11, "spells / rods / staves" : 12 }},
        { "level" : 10, "xp" : 450000,  "hit-dice" : 9, "hp-mod" : 1, "thac0" : 17, "saves" : { "death / poison" : 11, "wands" : 12, "paralysis / petrify" : 11, "breath attack" :  11, "spells / rods / staves" : 12 }},
        { "level" : 11, "xp" : 600000,  "hit-dice" : 9, "hp-mod" : 2, "thac0" : 14, "saves" : { "death / poison" : 8,  "wands" : 9,  "paralysis / petrify" : 8,  "breath attack" :  8,  "spells / rods / staves" : 8  }},
        { "level" : 12, "xp" : 750000,  "hit-dice" : 9, "hp-mod" : 3, "thac0" : 14, "saves" : { "death / poison" : 8,  "wands" : 9,  "paralysis / petrify" : 8,  "breath attack" :  8,  "spells / rods / staves" : 8  }},
        { "level" : 13, "xp" : 900000,  "hit-dice" : 9, "hp-mod" : 4, "thac0" : 14, "saves" : { "death / poison" : 8,  "wands" : 9,  "paralysis / petrify" : 8,  "breath attack" :  8,  "spells / rods / staves" : 8  }},
        { "level" : 14, "xp" : 1050000, "hit-dice" : 9, "hp-mod" : 5, "thac0" : 14, "saves" : { "death / poison" : 8,  "wands" : 9,  "paralysis / petrify" : 8,  "breath attack" :  8,  "spells / rods / staves" : 8  }}
    ]
    spells = {
        1:  { 1: 1, 2: '-', 3: '-', 4: '-', 5: '-', 6: '-'}, 
        2:  { 1: 2, 2: '-', 3: '-', 4: '-', 5: '-', 6: '-'}, 
        3:  { 1: 2, 2: 1,   3: '-', 4: '-', 5: '-', 6: '-'}, 
        4:  { 1: 2, 2: 2,   3: '-', 4: '-', 5: '-', 6: '-'},
        5:  { 1: 2, 2: 2,   3: 1,   4: '-', 5: '-', 6: '-'},
        6:  { 1: 2, 2: 2,   3: 2,   4: '-', 5: '-', 6: '-'}, 
        7:  { 1: 3, 2: 2,   3: 2,   4: 1,   5: '-', 6: '-'},
        8:  { 1: 3, 2: 3,   3: 2,   4: 2,   5: '-', 6: '-'},
        9:  { 1: 3, 2: 3,   3: 3,   4: 2,   5: 1,   6: '-'},
        10: { 1: 3, 2: 3,   3: 3,   4: 3,   5: 2,   6: '-'},
        11: { 1: 4, 2: 3,   3: 3,   4: 3,   5: 2,   6: 1  },
        12: { 1: 4, 2: 4,   3: 3,   4: 3,   5: 2,   6: 1  },
        13: { 1: 4, 2: 4,   3: 4,   4: 3,   5: 3,   6: 3  },
        14: { 1: 4, 2: 4,   3: 4,   4: 4,   5: 3,   6: 3  } 
    }
    def __init__(self,c_id,  level, attributes={}) -> None:
        Adventurer.__init__(self, c_id, level, attributes)
        self.player_class = MagicUser.adv_class
        self.progression = MagicUser.progression
        self.hp_rolls = self.roll_all_hit_points()
        self.hp = sum(self.hp_rolls[:self.level])
        self.armor = "None"
        self.ac = armor[self.armor]
        self.weapons = [ list(filter(lambda d: 'silver dagger' in d['name'],weapons))[0], { "name" : "" } ]
        self.spells = MagicUser.spells[self.level]
        self.spell_book = self.select_spells(magic_user_spells)
        self.atk = self.set_attack_bonus()

class Cleric(Adventurer):
    adv_class = "cleric"
    requirements = None
    prime_requisite = "wisdom"
    hit_die = 6
    max_level = 14
    progression = [
        { "level" : 1,  "xp" : 0,      "hit-dice" : 1, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 11, "wands" : 12, "paralysis / petrify" : 14, "breath attack" :  16, "spells / rods / staves" : 15 }},
        { "level" : 2,  "xp" : 1500,   "hit-dice" : 2, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 11, "wands" : 12, "paralysis / petrify" : 14, "breath attack" :  16, "spells / rods / staves" : 15 }},
        { "level" : 3,  "xp" : 3000,   "hit-dice" : 3, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 11, "wands" : 12, "paralysis / petrify" : 14, "breath attack" :  16, "spells / rods / staves" : 15 }},
        { "level" : 4,  "xp" : 6000,   "hit-dice" : 4, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 11, "wands" : 12, "paralysis / petrify" : 14, "breath attack" :  16, "spells / rods / staves" : 15 }},
        { "level" : 5,  "xp" : 12000,  "hit-dice" : 5, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 9,  "wands" : 10, "paralysis / petrify" : 12, "breath attack" :  14, "spells / rods / staves" : 12 }},
        { "level" : 6,  "xp" : 25000,  "hit-dice" : 6, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 9,  "wands" : 10, "paralysis / petrify" : 12, "breath attack" :  14, "spells / rods / staves" : 12 }},
        { "level" : 7,  "xp" : 50000,  "hit-dice" : 7, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 9,  "wands" : 10, "paralysis / petrify" : 12, "breath attack" :  14, "spells / rods / staves" : 12 }},
        { "level" : 8,  "xp" : 100000, "hit-dice" : 8, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 9,  "wands" : 10, "paralysis / petrify" : 12, "breath attack" :  14, "spells / rods / staves" : 12 }},
        { "level" : 9,  "xp" : 200000, "hit-dice" : 9, "hp-mod" : 0, "thac0" : 14, "saves" : { "death / poison" : 6,  "wands" : 7,  "paralysis / petrify" : 9,  "breath attack" :  14, "spells / rods / staves" : 9  }},
        { "level" : 10, "xp" : 300000, "hit-dice" : 9, "hp-mod" : 1, "thac0" : 14, "saves" : { "death / poison" : 6,  "wands" : 7,  "paralysis / petrify" : 9,  "breath attack" :  11, "spells / rods / staves" : 9  }},
        { "level" : 11, "xp" : 400000, "hit-dice" : 9, "hp-mod" : 2, "thac0" : 14, "saves" : { "death / poison" : 6,  "wands" : 7,  "paralysis / petrify" : 9,  "breath attack" :  11, "spells / rods / staves" : 9  }},
        { "level" : 12, "xp" : 500000, "hit-dice" : 9, "hp-mod" : 3, "thac0" : 14, "saves" : { "death / poison" : 6,  "wands" : 7,  "paralysis / petrify" : 9,  "breath attack" :  11, "spells / rods / staves" : 9  }},
        { "level" : 13, "xp" : 600000, "hit-dice" : 9, "hp-mod" : 4, "thac0" : 12, "saves" : { "death / poison" : 3,  "wands" : 5,  "paralysis / petrify" : 7,  "breath attack" :  8,  "spells / rods / staves" : 7  }},
        { "level" : 14, "xp" : 700000, "hit-dice" : 9, "hp-mod" : 5, "thac0" : 12, "saves" : { "death / poison" : 3,  "wands" : 5,  "paralysis / petrify" : 7,  "breath attack" :  8,  "spells / rods / staves" : 7  }}
    ]
    spells = {
        1:  { 1: '-', 2: '-', 3: '-', 4: '-', 5: '-'}, 
        2:  { 1: 1,   2: '-', 3: '-', 4: '-', 5: '-'}, 
        3:  { 1: 2,   2: '-', 3: '-', 4: '-', 5: '-'}, 
        4:  { 1: 2,   2: 1,   3: '-', 4: '-', 5: '-'}, 
        5:  { 1: 2,   2: 2,   3: '-', 4: '-', 5: '-'},
        6:  { 1: 2,   2: 2,   3: 1,   4: 1,   5: '-'},
        7:  { 1: 2,   2: 2,   3: 2,   4: 1,   5: 1  },
        8:  { 1: 3,   2: 3,   3: 2,   4: 2,   5: 1  },
        9:  { 1: 3,   2: 3,   3: 3,   4: 2,   5: 2  },
        10: { 1: 4,   2: 4,   3: 3,   4: 3,   5: 2  },
        11: { 1: 4,   2: 4,   3: 4,   4: 3,   5: 3  },
        12: { 1: 5,   2: 5,   3: 4,   4: 4,   5: 3  },
        13: { 1: 5,   2: 5,   3: 4,   4: 4,   5: 4  },
        14: { 1: 6,   2: 5,   3: 5,   4: 5,   5: 4  } 
    }
    turn_undead = {
        1:  { '1': '7', '2': '9', '2*': '11', 3: '-',  4: '-',  5: '-',  '6' : '-',  '7-9' : '-' }, 
        2:  { '1': 'T', '2': '7', '2*': '9',  3: '11', 4: '-',  5: '-',  '6' : '-',  '7-9' : '-' }, 
        3:  { '1': 'T', '2': 'T', '2*': '7',  3: '9',  4: '11', 5: '-',  '6' : '-',  '7-9' : '-' }, 
        4:  { '1': 'D', '2': 'T', '2*': 'T',  3: '7',  4: '9',  5: '11', '6' : '-',  '7-9' : '-' }, 
        5:  { '1': 'D', '2': 'D', '2*': 'T',  3: 'T',  4: '7',  5: '9',  '6' : '11', '7-9' : '-' }, 
        6:  { '1': 'D', '2': 'D', '2*': 'D',  3: 'T',  4: 'T',  5: '7',  '6' : '9',  '7-9' : '11'}, 
        7:  { '1': 'D', '2': 'D', '2*': 'D',  3: 'D',  4: 'T',  5: 'T',  '6' : '7',  '7-9' : '9' }, 
        8:  { '1': 'D', '2': 'D', '2*': 'D',  3: 'D',  4: 'D',  5: 'T',  '6' : 'T',  '7-9' : '7' }, 
        9:  { '1': 'D', '2': 'D', '2*': 'D',  3: 'D',  4: 'D',  5: 'D',  '6' : 'D',  '7-9' : 'T' }, 
        11: { '1': 'D', '2': 'D', '2*': 'D',  3: 'D',  4: 'D',  5: 'D',  '6' : 'D',  '7-9' : 'T' }, 
        12: { '1': 'D', '2': 'D', '2*': 'D',  3: 'D',  4: 'D',  5: 'D',  '6' : 'D',  '7-9' : 'D' }, 
        13: { '1': 'D', '2': 'D', '2*': 'D',  3: 'D',  4: 'D',  5: 'D',  '6' : 'D',  '7-9' : 'D' }, 
        14: { '1': 'D', '2': 'D', '2*': 'D',  3: 'D',  4: 'D',  5: 'D',  '6' : 'D',  '7-9' : 'D' } 
    }

    def __init__(self,c_id,  level, attributes={}) -> None:
        Adventurer.__init__(self, c_id, level, attributes)
        self.player_class = Cleric.adv_class
        self.progression = Cleric.progression
        self.hp_rolls = self.roll_all_hit_points()
        self.hp = sum(self.hp_rolls[:self.level])
        self.armor = random.choice(list(armor.keys()))
        self.ac = armor[self.armor]
        # clerics can only wield blunt weapons
        self.possible_melee_weapons = list(filter(lambda d: 'blunt' in d['traits'] and 'melee' in d['traits'], weapons))
        self.possible_weapons = list(filter(lambda d: 'blunt' in d['traits'], weapons))
        self.weapons = [ random.choice(self.possible_melee_weapons), random.choice(self.possible_weapons) ]
        self.spells = Cleric.spells[self.level]
        self.spell_book = self.select_spells(cleric_spells)
        self.atk = self.set_attack_bonus()
        self.turn_undead = self.__class__.turn_undead

class Thief(Adventurer):
    adv_class = "thief"
    prime_requisite = "dexterity"
    requirements = None
    hit_die = 4
    max_level = 14
    progression = [
        { "level" : 1,  "xp" : 0,      "hit-dice" : 1, "hp-mod" : 0,  "thac0" : 19, "saves" : { "death / poison" : 13, "wands" : 14, "paralysis / petrify" : 13, "breath attack" :  16, "spells / rods / staves" : 15 }},
        { "level" : 2,  "xp" : 1200,   "hit-dice" : 2, "hp-mod" : 0,  "thac0" : 19, "saves" : { "death / poison" : 13, "wands" : 14, "paralysis / petrify" : 13, "breath attack" :  16, "spells / rods / staves" : 15 }},
        { "level" : 3,  "xp" : 2400,   "hit-dice" : 3, "hp-mod" : 0,  "thac0" : 19, "saves" : { "death / poison" : 13, "wands" : 14, "paralysis / petrify" : 13, "breath attack" :  16, "spells / rods / staves" : 15 }},
        { "level" : 4,  "xp" : 4800,   "hit-dice" : 4, "hp-mod" : 0,  "thac0" : 19, "saves" : { "death / poison" : 13, "wands" : 14, "paralysis / petrify" : 13, "breath attack" :  16, "spells / rods / staves" : 15 }},
        { "level" : 5,  "xp" : 9600,   "hit-dice" : 5, "hp-mod" : 0,  "thac0" : 17, "saves" : { "death / poison" : 12, "wands" : 13, "paralysis / petrify" : 11, "breath attack" :  14, "spells / rods / staves" : 13 }},
        { "level" : 6,  "xp" : 20000,  "hit-dice" : 6, "hp-mod" : 0,  "thac0" : 17, "saves" : { "death / poison" : 12, "wands" : 13, "paralysis / petrify" : 11, "breath attack" :  14, "spells / rods / staves" : 13 }},
        { "level" : 7,  "xp" : 40000,  "hit-dice" : 7, "hp-mod" : 0,  "thac0" : 17, "saves" : { "death / poison" : 12, "wands" : 13, "paralysis / petrify" : 11, "breath attack" :  14, "spells / rods / staves" : 13 }},
        { "level" : 8,  "xp" : 80000,  "hit-dice" : 8, "hp-mod" : 0,  "thac0" : 17, "saves" : { "death / poison" : 12, "wands" : 13, "paralysis / petrify" : 11, "breath attack" :  14, "spells / rods / staves" : 13 }},
        { "level" : 9,  "xp" : 160000, "hit-dice" : 9, "hp-mod" : 0,  "thac0" : 14, "saves" : { "death / poison" : 10, "wands" : 11, "paralysis / petrify" : 9,  "breath attack" :  11, "spells / rods / staves" : 10 }},
        { "level" : 10, "xp" : 280000, "hit-dice" : 9, "hp-mod" : 2,  "thac0" : 14, "saves" : { "death / poison" : 10, "wands" : 11, "paralysis / petrify" : 9,  "breath attack" :  11, "spells / rods / staves" : 10 }},
        { "level" : 11, "xp" : 400000, "hit-dice" : 9, "hp-mod" : 4,  "thac0" : 14, "saves" : { "death / poison" : 10, "wands" : 11, "paralysis / petrify" : 9,  "breath attack" :  11, "spells / rods / staves" : 10 }},
        { "level" : 12, "xp" : 520000, "hit-dice" : 9, "hp-mod" : 6,  "thac0" : 14, "saves" : { "death / poison" : 10, "wands" : 11, "paralysis / petrify" : 9,  "breath attack" :  11, "spells / rods / staves" : 10 }},
        { "level" : 13, "xp" : 640000, "hit-dice" : 9, "hp-mod" : 8,  "thac0" : 12, "saves" : { "death / poison" : 8,  "wands" : 9,  "paralysis / petrify" : 7,  "breath attack" :  10, "spells / rods / staves" : 8  }},
        { "level" : 14, "xp" : 760000, "hit-dice" : 9, "hp-mod" : 10, "thac0" : 12, "saves" : { "death / poison" : 8,  "wands" : 9,  "paralysis / petrify" : 7,  "breath attack" :  10, "spells / rods / staves" : 8  }}
    ]
    def __init__(self,c_id,  level, attributes={}) -> None:
        Adventurer.__init__(self, c_id, level, attributes)
        self.player_class = Thief.adv_class
        self.progression = Thief.progression
        self.hp_rolls = self.roll_all_hit_points()
        self.hp = sum(self.hp_rolls[:self.level])
        self.armor = random.choice(list(armor.keys()))
        self.ac = armor[self.armor]
        self.atk = self.set_attack_bonus()

class Dwarf(Adventurer):
    adv_class = "dwarf"
    requirements = {'constitution' : 9 }
    prime_requisite = "strength"
    hit_die = 8
    max_level = 12
    progression = [
        { "level" : 1,  "xp" : 0,      "hit-dice" : 1, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 8, "wands" : 9, "paralysis / petrify" : 10, "breath attack" :  13, "spells / rods / staves" : 12 }},
        { "level" : 2,  "xp" : 2200,   "hit-dice" : 2, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 8, "wands" : 9, "paralysis / petrify" : 10, "breath attack" :  13, "spells / rods / staves" : 12 }},
        { "level" : 3,  "xp" : 4400,   "hit-dice" : 3, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 8, "wands" : 9, "paralysis / petrify" : 10, "breath attack" :  13, "spells / rods / staves" : 12 }},
        { "level" : 4,  "xp" : 8800,   "hit-dice" : 4, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 6, "wands" : 7, "paralysis / petrify" : 8,  "breath attack" :  10, "spells / rods / staves" : 10 }},
        { "level" : 5,  "xp" : 17000,  "hit-dice" : 5, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 6, "wands" : 7, "paralysis / petrify" : 8,  "breath attack" :  10, "spells / rods / staves" : 10 }},
        { "level" : 6,  "xp" : 35000,  "hit-dice" : 6, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 6, "wands" : 7, "paralysis / petrify" : 8,  "breath attack" :  10, "spells / rods / staves" : 10 }},
        { "level" : 7,  "xp" : 70000,  "hit-dice" : 7, "hp-mod" : 0, "thac0" : 14, "saves" : { "death / poison" : 4, "wands" : 5, "paralysis / petrify" : 6,  "breath attack" :  7,  "spells / rods / staves" : 8  }},
        { "level" : 8,  "xp" : 140000, "hit-dice" : 8, "hp-mod" : 0, "thac0" : 14, "saves" : { "death / poison" : 4, "wands" : 5, "paralysis / petrify" : 6,  "breath attack" :  7,  "spells / rods / staves" : 8  }},
        { "level" : 9,  "xp" : 270000, "hit-dice" : 9, "hp-mod" : 0, "thac0" : 14, "saves" : { "death / poison" : 4, "wands" : 5, "paralysis / petrify" : 6,  "breath attack" :  7,  "spells / rods / staves" : 8  }},
        { "level" : 10, "xp" : 400000, "hit-dice" : 9, "hp-mod" : 3, "thac0" : 12, "saves" : { "death / poison" : 2, "wands" : 3, "paralysis / petrify" : 4,  "breath attack" :  4,  "spells / rods / staves" : 6  }},
        { "level" : 11, "xp" : 530000, "hit-dice" : 9, "hp-mod" : 6, "thac0" : 12, "saves" : { "death / poison" : 2, "wands" : 3, "paralysis / petrify" : 4,  "breath attack" :  4,  "spells / rods / staves" : 6  }},
        { "level" : 12, "xp" : 660000, "hit-dice" : 9, "hp-mod" : 9, "thac0" : 12, "saves" : { "death / poison" : 2, "wands" : 3, "paralysis / petrify" : 4,  "breath attack" :  4,  "spells / rods / staves" : 6  }}
    ]
    def __init__(self,c_id,  level, attributes={}) -> None:
        Adventurer.__init__(self, c_id, level, attributes)
        self.player_class = Dwarf.adv_class
        self.progression = Dwarf.progression
        self.hp_rolls = self.roll_all_hit_points()
        self.hp = sum(self.hp_rolls[:self.level])
        self.armor = random.choice(list(armor.keys()))
        self.ac = armor[self.armor]
        self.atk = self.set_attack_bonus()

class Elf(Adventurer):
    adv_class = "elf"
    requirements = {'intelligence' : 9 }
    prime_requisite = "intellgence"
    hit_die = 6
    max_level = 10
    progression = [
        { "level" : 1,  "xp" : 0,      "hit-dice" : 1, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 12, "wands" : 13, "paralysis / petrify" : 13, "breath attack" :  15, "spells / rods / staves" : 15 }},
        { "level" : 2,  "xp" : 4000,   "hit-dice" : 2, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 12, "wands" : 13, "paralysis / petrify" : 13, "breath attack" :  15, "spells / rods / staves" : 15 }},
        { "level" : 3,  "xp" : 8000,   "hit-dice" : 3, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 12, "wands" : 13, "paralysis / petrify" : 13, "breath attack" :  15, "spells / rods / staves" : 15 }},
        { "level" : 4,  "xp" : 16000,  "hit-dice" : 4, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 10, "wands" : 11, "paralysis / petrify" : 11, "breath attack" :  13, "spells / rods / staves" : 12 }},
        { "level" : 5,  "xp" : 32000,  "hit-dice" : 5, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 10, "wands" : 11, "paralysis / petrify" : 11, "breath attack" :  13, "spells / rods / staves" : 12 }},
        { "level" : 6,  "xp" : 64000,  "hit-dice" : 6, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 10, "wands" : 11, "paralysis / petrify" : 11, "breath attack" :  13, "spells / rods / staves" : 12 }},
        { "level" : 7,  "xp" : 120000, "hit-dice" : 7, "hp-mod" : 0, "thac0" : 14, "saves" : { "death / poison" : 8,  "wands" : 9,  "paralysis / petrify" : 9,  "breath attack" :  10, "spells / rods / staves" : 10 }},
        { "level" : 8,  "xp" : 250000, "hit-dice" : 8, "hp-mod" : 0, "thac0" : 14, "saves" : { "death / poison" : 8,  "wands" : 9,  "paralysis / petrify" : 9,  "breath attack" :  10, "spells / rods / staves" : 10 }},
        { "level" : 9,  "xp" : 400000, "hit-dice" : 9, "hp-mod" : 0, "thac0" : 14, "saves" : { "death / poison" : 8,  "wands" : 9,  "paralysis / petrify" : 9,  "breath attack" :  10, "spells / rods / staves" : 10 }},
        { "level" : 10, "xp" : 600000, "hit-dice" : 9, "hp-mod" : 2, "thac0" : 12, "saves" : { "death / poison" : 6,  "wands" : 7,  "paralysis / petrify" : 8,  "breath attack" :  8,  "spells / rods / staves" : 8  }}
    ]                                                   
    spells = {                                          
        1:  { 1: 1, 2: '-', 3: '-', 4: '-', 5: '-', 6: '-'}, 
        2:  { 1: 2, 2: '-', 3: '-', 4: '-', 5: '-', 6: '-'}, 
        3:  { 1: 2, 2: 1,   3: '-', 4: '-', 5: '-', 6: '-'}, 
        4:  { 1: 2, 2: 2,   3: '-', 4: '-', 5: '-', 6: '-'},
        5:  { 1: 2, 2: 2,   3: 1,   4: '-', 5: '-', 6: '-'},
        6:  { 1: 2, 2: 2,   3: 2,   4: '-', 5: '-', 6: '-'}, 
        7:  { 1: 3, 2: 2,   3: 2,   4: 1,   5: '-', 6: '-'},
        8:  { 1: 3, 2: 3,   3: 2,   4: 2,   5: '-', 6: '-'},
        9:  { 1: 3, 2: 3,   3: 3,   4: 2,   5: 1,   6: '-'},
        10: { 1: 3, 2: 3,   3: 3,   4: 3,   5: 2,   6: '-'}
    }
    def __init__(self,c_id,  level, attributes={}) -> None:
        Adventurer.__init__(self, c_id, level, attributes)
        self.player_class = Elf.adv_class
        self.progression = Elf.progression
        self.hp_rolls = self.roll_all_hit_points()
        self.hp = sum(self.hp_rolls[:self.level])
        self.armor = random.choice(list(armor.keys()))
        self.ac = armor[self.armor]
        self.spells = Elf.spells[self.level]
        self.spell_book = self.select_spells(magic_user_spells)
        self.atk = self.set_attack_bonus()

class Halfling(Adventurer):
    adv_class = "halfling"
    requirements = {'constitution' : 9, 'dexterity' : 9 }
    prime_requisite = "dexterity"
    hit_die = 6
    max_level = 8
    progression = [
        { "level" : 1,  "xp" : 0,      "hit-dice" : 1, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 8, "wands" : 9, "paralysis / petrify" : 10, "breath attack" :  13, "spells / rods / staves" : 12 }},
        { "level" : 2,  "xp" : 2000,   "hit-dice" : 2, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 8, "wands" : 9, "paralysis / petrify" : 10, "breath attack" :  13, "spells / rods / staves" : 12 }},
        { "level" : 3,  "xp" : 4000,   "hit-dice" : 3, "hp-mod" : 0, "thac0" : 19, "saves" : { "death / poison" : 8, "wands" : 9, "paralysis / petrify" : 10, "breath attack" :  13, "spells / rods / staves" : 12 }},
        { "level" : 4,  "xp" : 8000,   "hit-dice" : 4, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 6, "wands" : 7, "paralysis / petrify" : 8,  "breath attack" :  10, "spells / rods / staves" : 10 }},
        { "level" : 5,  "xp" : 16000,  "hit-dice" : 5, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 6, "wands" : 7, "paralysis / petrify" : 8,  "breath attack" :  10, "spells / rods / staves" : 10 }},
        { "level" : 6,  "xp" : 32000,  "hit-dice" : 6, "hp-mod" : 0, "thac0" : 17, "saves" : { "death / poison" : 6, "wands" : 7, "paralysis / petrify" : 8,  "breath attack" :  10, "spells / rods / staves" : 10 }},
        { "level" : 7,  "xp" : 64000,  "hit-dice" : 7, "hp-mod" : 0, "thac0" : 14, "saves" : { "death / poison" : 4, "wands" : 5, "paralysis / petrify" : 6,  "breath attack" :  7,  "spells / rods / staves" : 8  }},
        { "level" : 8,  "xp" : 120000, "hit-dice" : 8, "hp-mod" : 0, "thac0" : 14, "saves" : { "death / poison" : 4, "wands" : 5, "paralysis / petrify" : 6,  "breath attack" :  7,  "spells / rods / staves" : 8  }},
    ]
    def __init__(self,c_id,  level, attributes={}) -> None:
        Adventurer.__init__(self, c_id, level, attributes)
        self.player_class = Halfling.adv_class
        self.progression = Halfling.progression
        self.hp_rolls = self.roll_all_hit_points()
        self.hp = sum(self.hp_rolls[:self.level])
        self.armor = random.choice(list(armor.keys()))
        self.ac = armor[self.armor]
        self.atk = self.set_attack_bonus()
