def get_chance_to_happen(self):
# Suports integer allowing fixed chance_to_happen.
# Supports tuple/list of 2 integers, allowing random range.
# Supports dict i.e {"input_weights": {"arousal": 0.25, "corruption": 0.75}, "min_chance": 0.5, "max_chance": 6.5}
def calculate_chance_to_happen():
if self.girl is None: # Currently doesn't support dynamic chance for non-girl events.
renpy.log(f"[EVENT]: ERROR: {self.name} Dynamic chance to happen doesn't support non-girl events")
return 0
input_weights = self.chance_to_happen.get("input_weights", {})
min_chance = self.chance_to_happen.get("min_chance", 0)
max_chance = self.chance_to_happen.get("max_chance", 0)
total_weight = sum(input_weights.values())
renpy.log(f"[EVENT]: Calculate Chance to Happen: total_weight: {total_weight}")
if total_weight == 0:
renpy.log(f"[EVENT]: total_weight is zero. Returning min_chance")
return min_chance
weighted_sum = 0
for input_name, weight in input_weights.items():
input_value = getattr(self.girl, input_name) * weight
weighted_sum += input_value
renpy.log(f"[EVENT]: Calculating weighted_sum: input_name: {input_name}({weight}) = {input_value}")
renpy.log(f"[EVENT]: weighted_sum: {weighted_sum}")
normalized_weighted_sum = weighted_sum / (total_weight * 100.0)
renpy.log(f"[EVENT]: normalized_weighted_sum: {normalized_weighted_sum}")
chance_to_happen = min_chance + (max_chance - min_chance) * normalized_weighted_sum
renpy.log(f"[EVENT]: chance_to_happen before clamping: {chance_to_happen}")
# Clamp the result between min_chance and max_chance
chance_to_happen = max(min_chance, min(chance_to_happen, max_chance))
renpy.log(f"[EVENT]: chance_to_happen after clamping: {chance_to_happen}")
return chance_to_happen
renpy.log(f"[EVENT]: Get Chance to Happen: {self.name}\n chance_to_happen: {self.chance_to_happen}")
chance_to_happen = self.chance_to_happen
if isinstance(self.chance_to_happen, dict):
chance_to_happen = calculate_chance_to_happen()
elif isinstance(self.chance_to_happen, tuple) or isinstance(self.chance_to_happen, list):
min_range, max_range = self.chance_to_happen
chance_to_happen = renpy.random.randint(min_range, max_range)
renpy.log(f"[EVENT]: Get Chance to Happen: Final Return: {chance_to_happen}")
return chance_to_happen