def parse_text(text): p = re.findall(r"\[.*?\]", text, re.I) results = text if len(p) > 0: for x in p: if re.match(re.escape("[Hero"), x, re.I): new_hero = Hero(Name("Ахиллес", 3, "male"), Name("Пелей", 2, "male")) text_type, style = x[1:-1].split(':') if not re.match("F", style, re.I): base, case = style.split(',') else: base, form, case = style.split(',') if base == "F": results = (results.replace( x, new_hero.father.get_possessive_base( "male", case).text).replace("^", "")) else: results = (results.replace( x, new_hero.name.get_case(case).text).replace("^", "")) elif re.match(re.escape("[Verb"), x, re.I): text_type, verb_text = x[1:-1].split(':') given_verb, form = verb_text.split(';') base, stress_pos, single = given_verb.split(',') stress_pos = int(stress_pos) new_verb = Verb(base, stress_pos, single) results = (results.replace( x, get_imperative(new_verb, single).text).replace("^", "")) print(results) else: print(text.replace("^", ""))
def get_creature(self, location=None): name = Name(self.get_adjective()+" ghoul") ghoul = actor.Person(location, name=name, sched=self.schedule) ai.WanderingMonsterAI(ghoul) ghoul.body = body.UndeadBody(ghoul) ghoul.combat_skill = 60 return ghoul
def build_actors(self): for adjective in self.adjectives: kobold = actor.SquadActor(location=None, name=Name(adjective) + "kobold") self.actors.append(kobold) boss = actor.Person( location=None, name=Name("kobold leader"), ) self.actors.append(boss) boss.ai = ai.WanderingMonsterAI(boss) sword = game_object.Item(location=boss, name=Name("crude sword")) sword.damage_type = "sharp" sword.damage_mult = 6 self.location_functions[boss] = self.boss_location_function
def create_bandit(self): if random() < 0.5: weapon_kind = "sword" damage_type = "sharp" damage_mult = 4 + int(self.power) // 5 title = "bandit swordsman" else: weapon_kind = "mace" damage_type = "blunt" damage_mult = 4 + int(self.power) // 5 title = "bandit maceman" given_name = namemaker.make_name() name_and_title = given_name.add(title, template="{}, {}") bandit = actor.Humanoid(location=None, name=name_and_title) bandit.ai = ai.SquadAI(bandit) # TODO: Replace this with selection from a world-level posture list stance = posture.random_posture(posture.Stance) guard = posture.random_posture(posture.Guard) for p in (stance, guard): bandit.learn_posture(p) bandit.adopt_posture(p) weapon = game_object.Item(location=bandit, name=Name(weapon_kind)) weapon.damage_type = damage_type weapon.damage_mult = damage_mult return bandit
def build(self): for kobold in self.create_random_creatures(): self.actors.append(kobold) boss = actor.Person( location=None, name=Name("kobold leader"), ) boss.traits.add(trait.kobold()) boss.ai = ai.WanderingMonsterAI(boss) boss.combat_skill = 75 sword = game_object.Item(location=boss, name=Name("crude sword")) sword.damage_type = "sharp" sword.damage_mult = 6 self.location_functions[boss] = self.boss_location_function self.actors.append(boss)
def build(self): queen = actor.AntQueen(location=None, name=Name("giant ant queen")) self.actors.append(queen) self.location_functions[queen] = self.boss_location_function for ant in self.create_random_creatures(): queen.ants.append(ant) self.actors.append(ant)
def __init__(self, start, end, direct="random"): self.start = start self.end = end locations = (start, end) if direct == "random": direct = direction.random() directions = (direct.opposite, direct) name_pair = [Name("door to") + place.name for place in (end, start)] super().__init__(locations=locations, directions=directions, name_pair=name_pair)
def __init__(self, name, location=None, coordinates=None, basis=None): self.name = Name.accept_string(name) if basis: self.basis = basis self.location = basis.location self.coordinates = basis.coordinates else: self.basis = None self.location = location self.coordinates = coordinates assert coordinates and location
def create_random_creatures(self, amount=None): out = [] if amount is None: amount = len(self.adjectives) shuffle(self.adjectives) for adjective in self.adjectives[:amount]: kobold = actor.Humanoid(location=None, name=Name(adjective) + "kobold") kobold.ai = ai.SquadAI(kobold) kobold.traits.add(trait.kobold()) out.append(kobold) return out
def create_random_creatures(self, amount=None): if amount is None: amount = max(7, min(25, int(self.power))) out = [] for _ in range(amount): ant = actor.Person(location=None, name=Name("giant ant")) ant.damage_type = "sharp" ant.damage_mult = 2 ant.combat_skill = 30 ant.ai = ai.WanderingMonsterAI(ant) out.append(ant) return out
def create_random_creatures(self, amount=None): if amount is None: amount = len(self.adjectives) shuffle(self.adjectives) actors = [] for adjective in self.adjectives[:amount]: ghoul = actor.Person(name=Name(adjective + " ghoul")) ai.WanderingMonsterAI(ghoul) ghoul.body = body.UndeadBody(ghoul) ghoul.combat_skill = 60 actors.append(ghoul) return actors
def __init__(self, location=None, name="", coordinates=None, other_names=(), sched=None, traits=(), names=(), *args, **kwargs): self.damage_type = "blunt" self.damage_mult = 1 self.physical = True self.trapping_item = None self.coordinates = coordinates self.things = set() self.traits = trait.TraitSet() self.location = location self.original_location = location self.arranged = True self.schedule = sched if self.location is not None: self.location.things.add(self) if self.schedule is None and self.location.schedule is not None: self.schedule = self.location.schedule if isinstance(name, Name): self.name_object = name elif names: self.name_object = Name( display_string=names[0], definition_string=" ".join(names), ) else: self.name_object = Name(name) self.name = self.name_object.get_text() self.nearest_portal = None self.owner: Optional[Thing] = None self.price: Optional[int] = None
def at_point( cls, location, direction, coordinates=None, portal_type=game_object.PortalEdge, landmark_name=None, **kwargs ): if "name" not in kwargs: kwargs["name_pair"] = ( landmark_name, Name("exit") ) portal = portal_type.free_portal( location, direction, coordinates, **kwargs ) schedule = location.schedule output_site = cls(schedule, portal.target, **kwargs) if landmark_name: output_site.landmark = portal.source.create_landmark(landmark_name) return output_site
def get_patronym(self): last_two_letters = self.father_name[len(self.father_name) - 2:] last_letter = self.father_name[len(self.father_name) - 1] if re.match("ей|ий|ес|ья|ид", last_two_letters): if self.name.grammatical_gender == "male": patronym = self.father_name[:-2] + "ид" else: patronym = self.father_name[:-2] + "ида" elif last_two_letters == "он": patronym = self.father_name[:-2] + "ос" elif last_letter == "ь": if self.name.grammatical_gender == "male": patronym = self.father_name[:-1] + "ид" else: patronym = self.father_name[:-1] + "ида" else: if self.name.grammatical_gender == "male": patronym = self.father_name + "ид" else: patronym = self.father_name + "ида" return Name(patronym, self.name.stress_position, self.grammatical_gender)
class Thing: def __init__(self, location=None, name="", coordinates=None, other_names=(), sched=None, traits=(), names=(), *args, **kwargs): self.damage_type = "blunt" self.damage_mult = 1 self.physical = True self.trapping_item = None self.coordinates = coordinates self.things = set() self.traits = trait.TraitSet() self.location = location self.original_location = location self.arranged = True self.schedule = sched if self.location is not None: self.location.things.add(self) if self.schedule is None and self.location.schedule is not None: self.schedule = self.location.schedule if isinstance(name, Name): self.name_object = name elif names: self.name_object = Name( display_string=names[0], definition_string=" ".join(names), ) else: self.name_object = Name(name) self.name = self.name_object.get_text() self.nearest_portal = None self.owner: Optional[Thing] = None self.price: Optional[int] = None def get_identifier(self, viewer=None): return "the " + self.get_name(viewer) def line_of_sight(self, first, second): return second.physical def hear_announcement(self, action): try: self_is_target = (action.target == self) except AttributeError: self_is_target = False if self_is_target: return self.action_targets_self(action) def action_targets_self(self, action): pass def broadcast_announcement(self, action): # This function gets hit pretty hard - should be more optimized target_set = set(action.target_list) subscribers = self.things_with_trait(trait.listener) | target_set broadcast_source = action.actor if (getattr(action, "traverses_portals", False) and not action.actor.has_location(self)): # Departing actor: Announce from portal, not actor broadcast_source = action.target for sub in subscribers: if (sub in target_set or self.line_of_sight(sub, broadcast_source)): # the order of those conditions matters: # line_of_sight will throw errors on targets outside location sub.hear_announcement(action) def take_damage(self, amt, damage_type, perpetrator=None): pass def get_coordinates(self, viewing_location): assert viewing_location == self.location assert self.coordinates is not None return self.coordinates def vanish(self): if self.location is not None: self.location.things.remove(self) self.location = None def materialize(self, location, coordinates=None): if self.location is not None: raise AttributeError self.change_location(location, coordinates) def add_thing(self, thing, coordinates=None): self.things.add(thing) def change_location(self, new_location, coordinates=None, keep_arranged=False): if self.location is not None: self.location.things.remove(self) self.location = new_location new_location.add_thing(self) if not keep_arranged: self.arranged = False else: self.original_location = new_location self.coordinates = coordinates U = TypeVar("U") def has_trait(self, trait: Type[U]): assert type(trait) == type if self.traits.contains_trait(trait): return True else: return False def has_name(self, name, viewer=None): if self.get_name(viewer).lower() == name.lower(): return True elif self.name_object: return self.name_object.matches(name) else: return False def things_with_trait(self, trait): assert type(trait) == type return {thing for thing in self.things if thing.has_trait(trait)} def things_with_name(self, name, viewer=None): return { thing for thing in self.things if thing.has_name(name, viewer=viewer) } def has_location(self, location): if location == self.location: return True else: return False def has_thing(self, thing): if thing in self.things: return True else: return False def get_interactables(self, viewer=None): '''PUBLIC: returns a list with all interactables at the location.''' output = set(self.things) if viewer: output = {t for t in output if self.line_of_sight(viewer, t)} nested = set() for thing in output: nested |= thing.get_interactables() output |= nested return output def has_nested_thing(self, thing): if self.has_thing(thing): return True else: for container in self.things_with_trait(trait.container): if container.has_nested_thing(thing): return True return False def has_furnishing(self, name): for thing in self.things_with_name(name): if thing.original_location == self and thing.arranged: return True return False def __repr__(self): return "{}({})".format(self.__class__.__name__, self.name) def be_targeted(self, action): """ This method implements any special behavior or checks that the interactable performs when an action is done to it. Allowed to have side effects. At the end, implementation should return super().be_targeted(self, action), so that special behaviors of the parent class can be inheritted properly. Note that this sometimes results in multiple special behaviors being executed at the same time. Returns (Bool, String) Bool = True if the action is successful and the affect_game method should be called, and success_string returned to the player false if the action is unsuccessful and the affect_game method should not be called, and the failure string provided by this method should be returned. String = A description of the actor's success or failure. If string == "", then the actions default failure or success string should be displayed. """ debug("Special target effects called") return True, "" def get_name(self, viewer=None): """PUBLIC: Return a name appropriate to the viewer arg viewer: The actor who is looking at this object. return: A name for this object appropriate to the viewer""" if viewer is not None: try: web = viewer.ai.web_output except AttributeError: web = False else: web = False out = self.name if web: return self.menu_wrap(out) else: return out @staticmethod def get_suggested_verbs(): return "take", "examine" def menu_wrap(self, text): return menu_wrap(text, self.get_suggested_verbs()) def get_look_text(self, viewer=None): return self.get_name(viewer) def show_text_to_hero(self, text): for i in self.things_with_trait(trait.hero): i.receive_text_message(text)
def get_creature(self, location=None): adjective = self.get_adjective() name = Name(adjective) + self.enemy_name return self.enemy_type(location, name=name, sched=self.schedule)
def generate_items(self): super().generate_items() Item(location=self, name=Name("animal skulls", "animal skull skulls")) Item(location=self, name=Name("animal hides"))
import random import re from name_object import Name from verb_object import Verb, get_form, get_imperative from hero_object import Hero hex_structure = "^--^--^--^--^--^-" male_names = [ Name("Тамерлан", 3, "male"), Name("Владислав", 3, "male"), Name("Юрий", 1, "male"), Name("Владимир", 2, "male"), Name("Никита", 2, "male"), Name("Илья", 2, "male"), Name("Николай", 3, "male") ] female_names = [ Name("Камисса", 2, "female"), Name("Ольга", 1, "female"), Name("Катерина", 3, "female"), Name("Полина", 2, "female"), Name("Анна", 1, "female"), Name("Александра", 3, "female"), Name("Мария", 2, "female") ] fathers = [ Name("Руслан", 2, "male"), Name("Игорь", 1, "male"),
def generate_items(self): runes.Rune(location=self, name=Name("runes", "rune runes"))
def generate_items(self): d = self.decor_dict d["table"] = Thing(self, "table") d["crate"] = Thing(self, Name("stool", "stool crate cushion bag"))
def get_default(cls): return cls({}, Name("neutral guard"), is_default=True)
def generate_items(self): d = self.decor_dict d["bedroll"] = Thing(self, "bedroll") d["lamp"] = Item(self, Name("lamp", "lamp lantern"))
def create_head(self): game_object.Item(location=self.location, name=self.name_object + Name("head"))
def get_default(cls): return cls({}, Name("neutral stance"), is_default=True)