Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update farewells to work similar to greetings #5852

Merged
39 changes: 39 additions & 0 deletions Monika After Story/game/definitions.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -3288,6 +3288,7 @@ init -991 python in mas_utils:
#import tempfile
from os.path import expanduser
from renpy.log import LogFile
from bisect import bisect

# LOG messges
_mas__failrm = "[ERROR] Failed remove: '{0}' | {1}\n"
Expand Down Expand Up @@ -3742,7 +3743,45 @@ init -991 python in mas_utils:
mas_log.raw_write = True
mas_log.write("VERSION: {0}\n".format(store.persistent.version_number))

def weightedChoice(choice_weight_tuple_list):
"""
Returns a random item based on weighting.
NOTE: That weight essentially corresponds to the equivalent of how many times to duplicate the choice

IN:
choice_weight_tuple_list - List of tuples with the form (choice, weighting)

OUT:
random choice value picked using choice weights
"""
#No items? Just return None
if not choice_weight_tuple_list:
return None

#Firstly, sort the choice_weight_tuple_list
choice_weight_tuple_list.sort(key=lambda x: x[1])

#Now split our tuples into individual lists for choices and weights
choices, weights = zip(*choice_weight_tuple_list)

#Some var setup
total_weight = 0
cumulative_weights = list()

#Now we collect all the weights and geneate a cumulative and total weight amount
for weight in weights:
total_weight += weight
cumulative_weights.append(total_weight)

#NOTE: At first glance this useage of bisect seems incorrect, however it is used to find the closest weight
#To the randomly selected weight. This is used to return the appropriate choice.
r_index = bisect(
ThePotatoGuy marked this conversation as resolved.
Show resolved Hide resolved
cumulative_weights,
renpy.random.random() * total_weight
)

#And return the weighted choice
return choices[r_index]

init -100 python in mas_utils:
# utility functions for other stores.
Expand Down
59 changes: 59 additions & 0 deletions Monika After Story/game/event-rules.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ init -1 python:
EV_RULE_FAREWELL_RANDOM = "farewell_random"
EV_RULE_AFF_RANGE = "affection_range"
EV_RULE_PRIORITY = "rule_priority"
EV_RULE_PROBABILITY = "rule_probability"


# special constants for numerical repeat rules
Expand Down Expand Up @@ -714,6 +715,64 @@ init -1 python:
return ev.rules.get(EV_RULE_PRIORITY, MASPriorityRule.DEF_PRIORITY)


class MASProbabilityRule(object):
"""
Static class used to create probability rules.

Probability rules are just integers that determine the probability of something being selected.


Probabilities must be greater than 1

This value is designed to be used with mas_utils.weightedChoice, and acts essentially akin to duplicating
the choice `probability` times in the list
"""
DEF_PROBABILITY = 1

@staticmethod
def create_rule(probability, ev=None):
"""
IN:
probability - the probability to set.
If None is passed in, we use the default probability value.
NOTE: If it is below 1 probability, is is set to 1

ev - Event to add this rule to. This will replace existing
rules of the same key.
(Default: None)
"""
if probability is None:
probability = MASProbabilityRule.DEF_PROBABILITY

elif probability < 1:
probability = 1

if not store.mas_ev_data_ver._verify_int(probability, allow_none=False):
raise Exception(
"'{0}' is not a valid in probability".format(probability)
)

rule = {EV_RULE_PROBABILITY: probability}

if ev:
ev.rules.update(rule)

return rule


@staticmethod
def get_probability(ev):
"""
Gets the probability of the given event.

IN:
ev - event to get probability of

OUT:
The probability of the given event, or def if no ProbilityRule is found
"""
return ev.rules.get(EV_RULE_PROBABILITY, MASProbabilityRule.DEF_PROBABILITY)

init python:
# these rules are NOT actually event rules since they don't create rule
# data in Event.
Expand Down
Loading