def __init__(self, level, martial=False, random_state=None): if random_state is None: self.random_state = Random() else: self.random_state = random_state if martial: self.item_list = unique_items else: self.item_list = magic_items self.level = level self.item = self.get_item() self.properties = self.get_properties() self.name_generator = NameGenerator(random_state=self.random_state) self.name = self.get_name()
class NPC_item: def __init__(self, level, martial=False, random_state=None): if random_state is None: self.random_state = Random() else: self.random_state = random_state if martial: self.item_list = unique_items else: self.item_list = magic_items self.level = level self.item = self.get_item() self.properties = self.get_properties() self.name_generator = NameGenerator(random_state=self.random_state) self.name = self.get_name() def get_name(self): if 'shield' in self.item.lower(): return self.name_generator.shield() elif 'armor' in self.item.lower(): return self.name_generator.armour() else: return self.name_generator.weapon() def rarities(self): return [ 'uncommon', 'rare', 'very rare', 'legendary' ][int(round(self.level / 10)):max([int(round((self.level) / 4)), 1])] def get_item(self): possible_items = sorted([ item for item, rarity in self.item_list.items() if rarity in self.rarities() ]) if len(possible_items) == 0: item = 'None' else: item = self.random_state.choice(possible_items) return item def get_properties(self, n_properties=1): tables = self.random_state.sample(item_properties.keys(), n_properties) properties = [] for table in tables: properties.append(self.random_state.choice(item_properties[table])) return properties
def __init__(self, level, layout, terrain=None, random_state=None): if random_state is None: self.random_state = Random() else: self.random_state = random_state self.level = level self.layout = layout self.encounter_sources = {} self.encounters = {} self.signs = {} treasure = RawHoardSource( level, random_state=self.random_state).get_treasure() self.treasure_manager = TreasureManager(treasure, random_state=self.random_state) self.terrain = terrain self.monster_manual = MonsterManual(terrain=terrain) self.events = [] self.name_generator = NameGenerator(random_state=self.random_state) self.wandering_monsters = {}
def add_books(self, room, dungeon_purpose): namer = NameGenerator(random_state=self.random_state) if purpose == 'stronghold': topics = self.random_state.sample(['history', 'nature', 'arcana'], 2) elif purpose == 'temple': topics = ['religion', 'arcana'] else: topics = self.random_state.sample( ['history', 'nature', 'arcana', 'religion'], 2) if 'study' in room['tags']: n_books = 1 else: n_books = self.random_state.randint(1, 6) library = [] for _ in range(n_books): book = namer.book(self.random_state.choice(topics)) library.append({ 'title': book['title'], 'contents': book['contents'] + ' ' + book.get('extra', '') }) room['library'] = library
def __init__(self, random_state=None): if random_state is None: self.random_state = Random() else: self.random_state = random_state self.role = self.random_state.choice(list(roles.keys())) self.sex = self.random_state.choice(['male', 'male', 'female', 'female', 'male', 'female', 'other']) self.traits = {} for trait in roles[self.role].keys(): self.traits[trait] = self.random_state.choice(roles[self.role][trait]) self.traits['personality'] = [] for trait in self.random_state.sample(traits.keys(), 3): self.traits['personality'].append({'type': trait, 'text': self.random_state.choice(traits[trait])}) self.name = NameGenerator(random_state=self.random_state).simple_person_name(sex=self.sex) if self.random_state.randint(1, 6) >= 5: self.traits['item'] = NPC_item(level=5, random_state=self.random_state).item
def get_description(self): base = ' This room contains' motif, other_motif = self.random_state.sample( special_furnishings_data['statue']['motif'], 2) fancy_name = NameGenerator().fancy_name() template = self.random_state.choice( special_furnishings_data['statue']['templates']) enticement = self.random_state.choice( special_furnishings_data['statue']['enticements']) effect = self.random_state.choice( special_furnishings_data['statue']['effects']) d = { 'motif': motif, 'other_motif': other_motif, 'fancy_name': fancy_name } for word in [ 'single_activity', 'material', 'pair_activity', 'penalty' ]: d[word] = self.random_state.choice( special_furnishings_data['statue'][word]) return Template(' '.join([base, template, enticement, effect])).substitute(d)
def test_names(): namer = NameGenerator()
class DungeonManager: def __init__(self, level, layout, terrain=None, random_state=None): if random_state is None: self.random_state = Random() else: self.random_state = random_state self.level = level self.layout = layout self.encounter_sources = {} self.encounters = {} self.signs = {} treasure = RawHoardSource( level, random_state=self.random_state).get_treasure() self.treasure_manager = TreasureManager(treasure, random_state=self.random_state) self.terrain = terrain self.monster_manual = MonsterManual(terrain=terrain) self.events = [] self.name_generator = NameGenerator(random_state=self.random_state) self.wandering_monsters = {} def add_encounter_source(self, source_name, monster_set, event_description, wandering=False): logger.debug('Adding encounter source %s (%s)' % (source_name, monster_set)) encounter_source = EncounterSource( encounter_level=self.level, monster_sets=[monster_set], supplied_monster_manual=MonsterManual(terrain=self.terrain), random_state=self.random_state) self.encounter_sources[source_name] = encounter_source self.encounters[source_name] = 0 self.signs[source_name] = [] self.add_event(source_name, event_description, monster_set, wandering=wandering) def add_special_encounter_source(self, source_name, encounter_source): logger.debug('Adding special encounter source %s' % (source_name)) self.encounter_sources[source_name] = encounter_source self.encounters[source_name] = 0 self.signs[source_name] = [] def add_event(self, source_name, description, monster_set, wandering=False): logger.debug('Adding event for source %s (%s)' % (source_name, monster_set)) event = { 'event': description, 'monster_set': monster_set, 'source_name': source_name } self.events.append(event) if wandering: self.wandering_monsters[source_name] = monster_set def get_encounter(self, source_name, **kwargs): logger.debug('Getting encouter from encounter source %s' % (source_name)) encounter = self.encounter_sources[source_name].get_encounter(**kwargs) if encounter.get('success'): self.encounters[source_name] += 1 encounter['source name'] = source_name return encounter else: return None def delete_encounter(self, encounter): logger.debug('Deleting encouter from encounter source %s' % (encounter.get('source name'))) source_name = encounter.get('source name') self.encounters[source_name] -= 1 def get_sign(self, source_name): logger.debug('Getting sign from encounter source %s' % (source_name)) sign = Sign(self.encounter_sources[source_name].get_sign()) self.signs[source_name].append(sign) return sign def delete_signs(self, source_name): logger.debug('Deleting sign from encounter source %s' % (source_name)) for sign in self.signs[source_name]: sign.delete() def __enter__(self, *args): return self def get_treasure(self, shares=1): return self.treasure_manager.get_treasure(shares) def delete_treasure(self, treasure_to_delete): self.treasure_manager.delete_treasure(treasure_to_delete) def get_monster_sets(self, **kwargs): return self.monster_manual.get_monster_sets(**kwargs) def parse_event(self, event): text = event['event'] monster_set = event['monster_set'] return f'{text}: ({monster_set})' def __exit__(self, eType, eValue, eTrace): logger.debug('Exiting dungeon manager') for source_name in self.encounter_sources: if self.encounters[source_name] == 0: self.delete_signs(source_name) wandering = [] for source_name, set_name in self.wandering_monsters.items(): if self.encounters[source_name] > 0: wandering.append(set_name) self.layout.wandering_monster_table = WanderingMonsters( self.level, wandering, random_state=self.random_state).table self.layout.name = self.name_generator.dungeon_name( self.layout.purpose, terrain=self.terrain) self.layout.terrain = self.terrain self.layout.events = [ self.parse_event(event) for event in self.events if event['source_name'] == 'special' or self.encounters[event['source_name']] > 0 ] self.layout.level = self.level