from config import DISASTER_FREQUENCY, MARRIAGE_RATE, DISASTER_PROB, NORMAL_BIRTH_RATE, LOW_BIRTH_RATE, NORMAL_BATTLE_RATE, HIGH_BATTLE_RATE, IMMORTAL_RANK from Character import Character from plugins.CharacterCreationPlugin import CharacterCreationPlugin from plugins.CultivationPlugin import CultivationPlugin from plugins.MarriagePlugin import MarriagePlugin from plugins.BirthPlugin import BirthPlugin from plugins.CharacterGrowthPlugin import CharacterGrowthPlugin from plugins.DisasterPlugin import DisasterPlugin from plugins.BattlePlugin import BattlePlugin from plugins.ResourceDepletionPlugin import ResourceDepletionPlugin class WorldSimulation: def __init__(self, world_spiritual_energy, resources = 100000, resources_threshold = 10): # 初始化插件 self.character_creation_plugin = CharacterCreationPlugin() self.cultivation_plugin = CultivationPlugin() self.marriage_plugin = MarriagePlugin(MARRIAGE_RATE) self.birth_plugin = BirthPlugin(NORMAL_BIRTH_RATE) self.character_growth_plugin = CharacterGrowthPlugin(100) self.disaster_plugin = DisasterPlugin(DISASTER_FREQUENCY, DISASTER_PROB) self.battle_plugin = BattlePlugin(NORMAL_BATTLE_RATE) self.resource_depletion_plugin = ResourceDepletionPlugin(resources_threshold) # 初始化参数 self.resources = resources # 总资源量 self.resources_threshold = resources_threshold # 人均最少资源需求 self.init_world_spiritual_energy = world_spiritual_energy self.world_spiritual_energy = world_spiritual_energy self.characters = [] self.dead_characters = [] self.nth_round = 0 self.world_log = [] self.history_population = [] self.history_spi_energy = [] self.history_new_birth_count = [] self.history_dead_count = [] def log(self, msg): self.world_log.append(msg) def simulate_round(self): self.character_growth_plugin.execute(self.characters, self.character_die) self.disaster_plugin.execute(self.characters, self.nth_round, self.character_die) # 每人可用资源 per_capita_resources = self.resources / len(self.characters) # 资源较少时会降低生育率 if per_capita_resources < self.resources_threshold * 1.8: self.birth_plugin.set_birth_rate(LOW_BIRTH_RATE) self.battle_plugin.set_battle_rate(HIGH_BATTLE_RATE) else: self.birth_plugin.set_birth_rate(NORMAL_BIRTH_RATE) self.battle_plugin.set_battle_rate(NORMAL_BATTLE_RATE) self.resource_depletion_plugin.execute(per_capita_resources, self.characters, self.character_die) self.battle_plugin.execute(self.characters, self.character_die) self.birth_plugin.execute(self.characters, self.register_character) self.marriage_plugin.execute(self.characters) self.cultivation_plugin.execute(self.characters, self.world_spiritual_energy, self.init_world_spiritual_energy, self.consume_spiritual_energy) def run_simulation(self, num_rounds): md = "" for _ in range(num_rounds): self.nth_round += 1 self.history_dead_count.append(0) self.history_new_birth_count.append(0) print(f"第{self.nth_round}轮模拟:") md += f"第{self.nth_round}轮模拟:\n" self.simulate_round() print(f"共死亡{self.history_dead_count[-1]}人!") print(f"共{self.history_new_birth_count[-1]}对夫妻生了孩子!") print(f"当前世界灵气总量:{self.world_spiritual_energy}") print(f"当前存活角色数量:{len(self.characters)}") print("") md += f"共死亡{self.history_dead_count[-1]}人!\n" md += f"共{self.history_new_birth_count[-1]}对夫妻生了孩子!\n" md += f"当前世界灵气总量:{self.world_spiritual_energy}\n" md += f"当前存活角色数量:{len(self.characters)}\n\n" self.history_spi_energy.append(self.world_spiritual_energy) self.history_population.append(len(self.characters)) if len(self.characters) == 0: print("所有角色死亡,模拟结束") md += "所有角色死亡,模拟结束\n" break return md def add_custom_character(self, name, gender, special_constitution, spiritual_roots, clan=None): character = Character(name, gender, special_constitution, spiritual_roots, clan) self.characters.append(character) def create_population(self, initial_population, special_constitution_ratio, spiritual_roots_ratio): # 创建角色 self.character_creation_plugin.set_parameters(special_constitution_ratio, spiritual_roots_ratio) for _ in range((initial_population - len(self.characters))): character = self.character_creation_plugin.create_character() self.characters.append(character) def character_die(self, character): # 成仙者不会死亡 if character.is_immortal: return character.die() self.world_spiritual_energy += character.consume_spiritual_energy # 灵气回归 if character.partner: character.partner.history.append(f"{character.partner.real_age}岁,配偶{character.name}({character.real_age}岁)死亡") character.partner.partner = None # 解除配偶关系 self.characters.remove(character) self.dead_characters.append(character) self.history_dead_count[-1] += 1 def register_character(self, character): self.characters.append(character) self.history_new_birth_count[-1] += 1 def consume_spiritual_energy(self, amount): self.world_spiritual_energy -= amount def find_characters_by_name(self, name): # 可能会有重名,把所有都返回 res = [c for c in self.characters if c.name == name] if len(res) == 0: return [c for c in self.dead_characters if c.name == name] return res def find_characters_by_clan(self, clan): res = [c for c in self.characters if c.clan == clan] if len(res) == 0: return [c for c in self.dead_characters if c.clan == clan] return res