예제 #1
0
def apply_effects(person_data={}, world_data={}, effect_data={}, tag_manager={}):
    # Sample events:
    # pay = good, father.leave, disease = infection, pay = low-fair, blessing, family.blessing,
    # cost = poor, disease = mutation, "gain [weapon], gain [armor]"

    # Sample Generators:
    #     generator =   # barmaid, country, wizard, sailor, horse, creature, scholar, royalty, "weapon, armor"
    #     power/refresh =  # 2
    #     years =  # 1d6
    #     role =   # caretaker, mount, pet, familiar

    tags = math_helpers.flatten_tags(tag_manager)
    items = person_data.get("children", [])
    qualities = person_data.get("qualities", {})

    generated_items = []
    if "generator" in effect_data:
        generator_list = effect_data.get("generator", "")
        if generator_list:
            power = effect_data.get("refresh", None) or effect_data.get("power", 1)
            years = effect_data.get("years", 4)
            role = effect_data.get("role", "friend")
            override = effect_data.get("override", "")
            override = math_helpers.convert_string_to_properties_object(override)

            effect_data.pop("generator", None)
            effect_data.pop("refresh", None)
            effect_data.pop("power", None)
            effect_data.pop("years", None)
            effect_data.pop("role", None)
            effect_data.pop("override", None)

            generators = generator_list.split(",")
            generators = [g.strip() for g in generators]
            for generator in generators:
                created = create_random_item(world_data=world_data, override=override, pattern=generator, tags=tags)
                generated = generate_object(generator=generator, world_data=world_data, item_template=created,
                                            power=power, tags=tags, role=role, years=years)

                data = generated.get("data", {})
                data["role"] = role
                years = effect_data.get("years", 1)
                years = math_helpers.roll_dice(years, use_numpy=True)
                data["years"] = years

                generated_items.append(generated)

                if role:
                    finished_year = int(world_data.get("year", 1100)) + int(years)
                    items.append({"type": role, "finished": finished_year, "active": True, "data": generated})

    family = person_data.get("family", {})
    father = family.get("father", {})
    mother = family.get("mother", {})
    year = world_data.get("year", 1100)

    for effect, variable in effect_data.items():
        # pay = good, father.leave, disease = infection, pay = low-fair, blessing, family.blessing,
        #  cost = poor, disease = mutation, "gain [weapon], gain [armor]"

        #TODO: All other non-generators, run functions
        if effect == "pay":
            if not variable:
                #TODO: Dynamically calculate difficulty
                variable = 5.0
            elif variable == 'exists':
                variable = 5.0
            if isinstance(variable, basestring):
                variable = 5.0
            math_helpers.add_or_increment_dict_val(person_data, "money", variable)

        elif effect == "father.leave":
            father["leave"] = year
            #TODO: reduce family income
            pass
        elif effect == "mother.leave":
            mother["leave"] = year
            #TODO: reduce family income
            pass

        elif effect == "disease":
            if not variable:
                variable = 3.0
            elif variable == 'exists':
                variable = 3.0
            if isinstance(variable, basestring):
                variable = 3.0
            math_helpers.add_or_increment_dict_val(qualities, 'lifespan', -variable)
            generated_items.append({"type": "disease", "name": "rickets"})  #TODO: Make a disease lookup table

        elif effect == "pay":
            if not variable:
                variable = 5.0
            elif variable == 'exists':
                variable = 5.0
            if isinstance(variable, basestring):
                variable = 5.0
            math_helpers.add_or_increment_dict_val(person_data, "money", variable)

        elif effect == "blessing":
            if not variable:
                variable = 3.0
            elif variable == 'exists':
                variable = 3.0
            if isinstance(variable, basestring):
                variable = 3.0
            math_helpers.add_or_increment_dict_val(qualities, 'lifespan', variable)
            generated_items.append({"type": "blessing", "name": "lucky"})

        elif effect == "family.blessing":
            if not variable:
                variable = 3.0
            elif variable == 'exists':
                variable = 3.0
            if isinstance(variable, basestring):
                variable = 3.0
            math_helpers.add_or_increment_dict_val(qualities, 'lifespan', -variable)
            generated_items.append({"type": "family blessing", "name": "wealth"})

        elif effect == "cost":
            if not variable:
                variable = 5.0
            elif variable == 'exists':
                variable = 5.0
            if isinstance(variable, basestring):
                variable = 5.0
            math_helpers.add_or_increment_dict_val(person_data, "money", -variable)

        elif effect == "gain":
            pass

    return generated_items
예제 #2
0
def apply_effects(person_data={}, world_data={}, effect_data={}, tag_manager={}):
    # Sample events:
    # pay = good, father.leave, disease = infection, pay = low-fair, blessing, family.blessing,
    # cost = poor, disease = mutation, "gain [weapon], gain [armor]"

    # Sample Generators:
    #     generator =   # barmaid, country, wizard, sailor, horse, creature, scholar, royalty, "weapon, armor"
    #     power/refresh =  # 2
    #     years =  # 1d6
    #     role =   # caretaker, mount, pet, familiar

    tags = math_helpers.flatten_tags(tag_manager)
    items = person_data.get("children", [])
    qualities = person_data.get("qualities", {})

    generated_items = []
    if "generator" in effect_data:
        generator_list = effect_data.get("generator", "")
        if generator_list:
            power = effect_data.get("refresh", None) or effect_data.get("power", 1)
            years = effect_data.get("years", 4)
            role = effect_data.get("role", "friend")
            override = effect_data.get("override", "")
            override = math_helpers.convert_string_to_properties_object(override)

            effect_data.pop("generator", None)
            effect_data.pop("refresh", None)
            effect_data.pop("power", None)
            effect_data.pop("years", None)
            effect_data.pop("role", None)
            effect_data.pop("override", None)

            generators = generator_list.split(",")
            generators = [g.strip() for g in generators]
            for generator in generators:
                created = create_random_item(world_data=world_data, override=override, pattern=generator, tags=tags)
                generated = generate_object(
                    generator=generator,
                    world_data=world_data,
                    item_template=created,
                    power=power,
                    tags=tags,
                    role=role,
                    years=years,
                )

                data = generated.get("data", {})
                data["role"] = role
                years = effect_data.get("years", 1)
                years = math_helpers.roll_dice(years, use_numpy=True)
                data["years"] = years

                generated_items.append(generated)

                if role:
                    finished_year = int(world_data.get("year", 1100)) + int(years)
                    items.append({"type": role, "finished": finished_year, "active": True, "data": generated})

    family = person_data.get("family", {})
    father = family.get("father", {})
    mother = family.get("mother", {})
    year = world_data.get("year", 1100)

    for effect, variable in effect_data.items():
        # pay = good, father.leave, disease = infection, pay = low-fair, blessing, family.blessing,
        #  cost = poor, disease = mutation, "gain [weapon], gain [armor]"

        # TODO: All other non-generators, run functions
        if effect == "pay":
            if not variable:
                # TODO: Dynamically calculate difficulty
                variable = 5.0
            elif variable == "exists":
                variable = 5.0
            if isinstance(variable, basestring):
                variable = 5.0
            math_helpers.add_or_increment_dict_val(person_data, "money", variable)

        elif effect == "father.leave":
            father["leave"] = year
            # TODO: reduce family income
            pass
        elif effect == "mother.leave":
            mother["leave"] = year
            # TODO: reduce family income
            pass

        elif effect == "disease":
            if not variable:
                variable = 3.0
            elif variable == "exists":
                variable = 3.0
            if isinstance(variable, basestring):
                variable = 3.0
            math_helpers.add_or_increment_dict_val(qualities, "lifespan", -variable)
            generated_items.append({"type": "disease", "name": "rickets"})  # TODO: Make a disease lookup table

        elif effect == "pay":
            if not variable:
                variable = 5.0
            elif variable == "exists":
                variable = 5.0
            if isinstance(variable, basestring):
                variable = 5.0
            math_helpers.add_or_increment_dict_val(person_data, "money", variable)

        elif effect == "blessing":
            if not variable:
                variable = 3.0
            elif variable == "exists":
                variable = 3.0
            if isinstance(variable, basestring):
                variable = 3.0
            math_helpers.add_or_increment_dict_val(qualities, "lifespan", variable)
            generated_items.append({"type": "blessing", "name": "lucky"})

        elif effect == "family.blessing":
            if not variable:
                variable = 3.0
            elif variable == "exists":
                variable = 3.0
            if isinstance(variable, basestring):
                variable = 3.0
            math_helpers.add_or_increment_dict_val(qualities, "lifespan", -variable)
            generated_items.append({"type": "family blessing", "name": "wealth"})

        elif effect == "cost":
            if not variable:
                variable = 5.0
            elif variable == "exists":
                variable = 5.0
            if isinstance(variable, basestring):
                variable = 5.0
            math_helpers.add_or_increment_dict_val(person_data, "money", -variable)

        elif effect == "gain":
            pass

    return generated_items
예제 #3
0
def apply_event_effects(person_data={}, world_data={}, event_data={}, event_type='birthplace', year=None,
                        age=None, tag_manager={}, event_id=42):
    message = ""

    if not year:
        year = world_data.get("year", 1100)
    world_data["year"] = year

    if age is None:
        age = person_data.get("age", 16 + numpy.random.randint(20))
    person_data["age"] = age

    name = event_data.get("name", "in a barn")
    properties = event_data.get("properties", {})

    if isinstance(properties, basestring):
        properties = math_helpers.convert_string_to_properties_object(properties)

    math_helpers.add_tags(tag_manager, 'event', event_data.get("tags", []))

    effect_was_applied = False
    generated_items = []
    addional_messages = []
    effects = event_data.get("effects", [])

    for effect in effects:
        # Don't consider if it's a unique effect
        prevent = effect.get("unique", False)

        if prevent and effect_was_applied:
            continue

        family = world_data.get("family", {})

        # Roll the dice, if the chance occurs then parse the rest
        chance = effect.get("chance", None)
        if chance:
            chance_modifier = family.get("conflict", 1)

            try:
                chance_modifier = float(chance_modifier)
            except ValueError:
                chance_modifier = 1

            try:
                chance = math_helpers.value_of_variable(chance)
                chance += chance_modifier
                chance /= 100

                if not numpy.random.random() < chance:
                    continue
            except ValueError:
                continue
        else:
            pass

        # Check the requirements # family.enemy has Elves, "family.*.profession sailor", family.*.profession has doctor
        requirement = effect.get("requirement", None)
        if requirement:
            passes = math_helpers.check_requirements(requirement, world_data)
            if not passes:
                continue

        # Update the message # "An innkeeper named [barmaid.name] assists, You were born the day your father was killed in battle. His [weapon.type] and [armor.type] are passed to you.
        ef_message = effect.get("message", None)
        if ef_message:
            addional_messages.append(ef_message)

        # Generators can create objects/items for use in the story
        #TODO: Generate a generator also if it's in a message text and not called out explicitly
        generator_data = {"generator": effect.get("generator", None),
                          "role": effect.get("role", None),
                          "years": effect.get("years", None),
                          "override": effect.get("override", None),
                          "power": effect.get("refresh", None) or effect.get("power", None)
        }
        # Run methods for everything in effects
        effect_data = effect.get("effect", None)  # pay = good, father.leave, disease = infection
        effect_data = math_helpers.convert_string_to_properties_object(effect_data)
        effect_data = math_helpers.add_or_merge_dicts(effect_data, generator_data)
        generated_items_new = apply_effects(person_data, world_data, effect_data, tag_manager)
        generated_items = generated_items + generated_items_new

        # Properties are always applied to the person after all effects are run
        ef_properties = effect.get("properties", None)  # JSON or str, "mother.profession = waitress, lifespan +2
        if ef_properties:
            if isinstance(ef_properties, basestring):
                ef_properties = math_helpers.convert_string_to_properties_object(ef_properties)

            properties = math_helpers.add_or_merge_dicts(properties, ef_properties)
        effect_was_applied = True

    severity = 1
    if event_type == 'birthplace':
        if isinstance(name, basestring) and name is not '':
            name_lower = name[0].lower() + name[1:]
        else:
            name_lower = name
        person_name = person_data.get("name", "Jon Snow")

        message += "<b>" + person_name + " was born " + str(name_lower) + "</b>"
        addional_messages = [message] + addional_messages

    if properties:
        message_new = apply_properties(person_data, world_data, properties, generated_items)
        if message_new:
            addional_messages.append(message_new)

    if generated_items and "item_list" in person_data:
        person_data["item_list"] += generated_items

    addional_messages = [m.strip() for m in addional_messages]
    message = "<br/>".join(addional_messages)

    return {"id": event_id, "age": age, "year": year, "message": message, "world_data": json.dumps(world_data),
            "severity": severity}
예제 #4
0
def apply_event_effects(
    person_data={},
    world_data={},
    event_data={},
    event_type="birthplace",
    year=None,
    age=None,
    tag_manager={},
    event_id=42,
):
    message = ""

    if not year:
        year = world_data.get("year", 1100)
    world_data["year"] = year

    if age is None:
        age = person_data.get("age", 16 + numpy.random.randint(20))
    person_data["age"] = age

    name = event_data.get("name", "in a barn")
    properties = event_data.get("properties", {})

    if isinstance(properties, basestring):
        properties = math_helpers.convert_string_to_properties_object(properties)

    math_helpers.add_tags(tag_manager, "event", event_data.get("tags", []))

    effect_was_applied = False
    generated_items = []
    addional_messages = []
    effects = event_data.get("effects", [])

    for effect in effects:
        # Don't consider if it's a unique effect
        prevent = effect.get("unique", False)

        if prevent and effect_was_applied:
            continue

        family = world_data.get("family", {})

        # Roll the dice, if the chance occurs then parse the rest
        chance = effect.get("chance", None)
        if chance:
            chance_modifier = family.get("conflict", 1)

            try:
                chance_modifier = float(chance_modifier)
            except ValueError:
                chance_modifier = 1

            try:
                chance = math_helpers.value_of_variable(chance)
                chance += chance_modifier
                chance /= 100

                if not numpy.random.random() < chance:
                    continue
            except ValueError:
                continue
        else:
            pass

        # Check the requirements # family.enemy has Elves, "family.*.profession sailor", family.*.profession has doctor
        requirement = effect.get("requirement", None)
        if requirement:
            passes = math_helpers.check_requirements(requirement, world_data)
            if not passes:
                continue

        # Update the message # "An innkeeper named [barmaid.name] assists, You were born the day your father was killed in battle. His [weapon.type] and [armor.type] are passed to you.
        ef_message = effect.get("message", None)
        if ef_message:
            addional_messages.append(ef_message)

        # Generators can create objects/items for use in the story
        # TODO: Generate a generator also if it's in a message text and not called out explicitly
        generator_data = {
            "generator": effect.get("generator", None),
            "role": effect.get("role", None),
            "years": effect.get("years", None),
            "override": effect.get("override", None),
            "power": effect.get("refresh", None) or effect.get("power", None),
        }
        # Run methods for everything in effects
        effect_data = effect.get("effect", None)  # pay = good, father.leave, disease = infection
        effect_data = math_helpers.convert_string_to_properties_object(effect_data)
        effect_data = math_helpers.add_or_merge_dicts(effect_data, generator_data)
        generated_items_new = apply_effects(person_data, world_data, effect_data, tag_manager)
        generated_items = generated_items + generated_items_new

        # Properties are always applied to the person after all effects are run
        ef_properties = effect.get("properties", None)  # JSON or str, "mother.profession = waitress, lifespan +2
        if ef_properties:
            if isinstance(ef_properties, basestring):
                ef_properties = math_helpers.convert_string_to_properties_object(ef_properties)

            properties = math_helpers.add_or_merge_dicts(properties, ef_properties)
        effect_was_applied = True

    severity = 1
    if event_type == "birthplace":
        if isinstance(name, basestring) and name is not "":
            name_lower = name[0].lower() + name[1:]
        else:
            name_lower = name
        person_name = person_data.get("name", "Jon Snow")

        message += "<b>" + person_name + " was born " + str(name_lower) + "</b>"
        addional_messages = [message] + addional_messages

    if properties:
        message_new = apply_properties(person_data, world_data, properties, generated_items)
        if message_new:
            addional_messages.append(message_new)

    if generated_items and "item_list" in person_data:
        person_data["item_list"] += generated_items

    addional_messages = [m.strip() for m in addional_messages]
    message = "<br/>".join(addional_messages)

    return {
        "id": event_id,
        "age": age,
        "year": year,
        "message": message,
        "world_data": json.dumps(world_data),
        "severity": severity,
    }