import random class ResourceDepletionPlugin: def __init__(self, depletion_threshold, death_rate = 0.3): self.depletion_threshold = depletion_threshold self.death_rate = death_rate def trigger_disaster(self, characters, character_die_callback): print("资源耗尽! 发生灾难...") # 按照宗族大小排序 clan_size = {} for c in characters: clan_size[c.clan] = clan_size.get(c.clan, 0) + 1 clans = sorted(clan_size.items(), key=lambda x: x[1], reverse=True) # 前 50% 大小的宗族,死亡 30%, 后 50% 大小的宗族,死亡 60% for i, (clan, size) in enumerate(clans): if i < len(clans) // 2: num_killed = int(size * self.death_rate) else: num_killed = int(size * (self.death_rate * 2)) # 随机选择死亡成员 clan_members = [c for c in characters if c.clan == clan] killed = random.sample(clan_members, num_killed) for c in killed: c.history.append(f"{c.real_age}岁,因资源耗尽死亡") character_die_callback(c) def execute(self, resources, characters, character_die_callback): # 检查资源是否耗尽,如果耗尽则有概率触发灾难 if resources < self.depletion_threshold: probability = random.random() if probability < 0.3: self.trigger_disaster(characters, character_die_callback)