import random class MarriagePlugin: def __init__(self, marriage_rate, min_marriage_age=20): self.marriage_rate = marriage_rate self.min_marriage_age = min_marriage_age # 最小结婚年龄 def perform_marriages(self, characters, callback=None): # 随机一些表观年龄>20的角色结婚 eligible_characters = [character for character in characters if character.apparent_age > self.min_marriage_age and character.partner is None and character.check_is_alive() and not character.is_immortal] new_couple_count = 0 for _ in range(int(len(eligible_characters) * self.marriage_rate)): character = random.choice(eligible_characters) partner = random.choice(eligible_characters) if character != partner and character.gender != partner.gender and character.partner is None and partner.partner is None: character.marry(partner) new_couple_count += 1 print(f"{new_couple_count}对新人结婚了") if callback is not None: callback(new_couple_count) def set_marriage_rate(self, marriage_rate): self.marriage_rate = marriage_rate # 统一插件接口 def execute(self, *args, **kwargs): self.perform_marriages(*args, **kwargs)