def __init__(self, ability_default=10): super(Character, self).__init__() self.equipment = Registry() self.slots = BodySlots() # adding abilities self.registry.set(Strength(ability_default)) self.registry.set(Dexterity(ability_default)) self.registry.set(Constitution(ability_default)) self.registry.set(Intelligence(ability_default)) self.registry.set(Wisdom(ability_default)) self.registry.set(Charisma(ability_default)) # adding saving throws self.registry.set(Fortitude()) self.registry.set(Reflex()) self.registry.set(Will()) # basic stuff self.registry.set(HitPoints(8)) self.registry.set(Initiative()) self.registry.set(Size.MEDIUM) # attacks self.attack = AttackModifierGroup() self.attack.base = self.registry.set(BaseAttack(0)) self.attack.melee = self.registry.set(BaseMeleeAttack(0)) self.attack.ranged = self.registry.set(BaseRangedAttack(0)) self.attack.grapple = self.registry.set(GrappleAttack(0)) # armor self.registry.set(ArmorClass(10)) self.registry.set(NaturalArmor(0)) self.registry.set(Touch()) self.registry.set(FlatFooted()) # skills, adding all found in the skill module for cls in self._base_skills(): self.registry.set(cls())
class Character(Actor): def __init__(self, ability_default=10): super(Character, self).__init__() self.equipment = Registry() self.slots = BodySlots() # adding abilities self.registry.set(Strength(ability_default)) self.registry.set(Dexterity(ability_default)) self.registry.set(Constitution(ability_default)) self.registry.set(Intelligence(ability_default)) self.registry.set(Wisdom(ability_default)) self.registry.set(Charisma(ability_default)) # adding saving throws self.registry.set(Fortitude()) self.registry.set(Reflex()) self.registry.set(Will()) # basic stuff self.registry.set(HitPoints(8)) self.registry.set(Initiative()) self.registry.set(Size.MEDIUM) # attacks self.attack = AttackModifierGroup() self.attack.base = self.registry.set(BaseAttack(0)) self.attack.melee = self.registry.set(BaseMeleeAttack(0)) self.attack.ranged = self.registry.set(BaseRangedAttack(0)) self.attack.grapple = self.registry.set(GrappleAttack(0)) # armor self.registry.set(ArmorClass(10)) self.registry.set(NaturalArmor(0)) self.registry.set(Touch()) self.registry.set(FlatFooted()) # skills, adding all found in the skill module for cls in self._base_skills(): self.registry.set(cls()) def configure(self, id, value): component = self.registry.get(id) if component.is_modifiable(): component.value = value else: self.linker.remove(component) self.registry.set(value) self.connect_links() def add_component(self, component): self.registry.set(component) self.connect_links() def remove_component(self, thing): component = (thing if isinstance(thing, Component) else self.registry.get(thing)) if component.is_destroyable(): for item in component.subcomponents: self.remove_component(item) self.linker.remove(component) self.registry.remove(component.id) def add_equipment(self, equipment): self.equipment.set(equipment) def remove_equipment(self, id): self.equipment.remove(id) def activate_equipment(self, id): item = self.equipment.get(id) if item.can_be_added(self.slots): self.slots.add(item) self.linker.apply(item) self.linker.process_all() return True return False def deactivate_equipment(self, id): item = self.equipment.get(id) self.slots.remove(item) self.linker.remove(self.equipment.get(id)) self.linker.process_all() def connect_links(self): self.linker.process_all() def __getattr__(self, name): if name in self.__dict__: return self.__dict__[name] component_id = self._attribute_to_id(name) if self.registry.has(component_id): return self.registry.get(component_id) raise AttributeError( "Unknown attribute '%s' on Character object" % name ) def __setattr__(self, name, value): # need to check for registry like this to prevent recursion in lookup component_id = self._attribute_to_id(name) registry = self.__dict__.get("registry") if not registry or not registry.has(component_id): super(Character, self).__setattr__(name, value) else: self.configure(component_id, value) def _attribute_to_id(self, name): return name.lower().replace("_", "-") def _base_skills(self): is_simple = lambda cls: cls not in (Craft, Perform, Profession) return filter(is_simple, get_all_skill_classes())