from config import DISASTER_FREQUENCY, MARRIAGE_RATE, DISASTER_PROB, NORMAL_BIRTH_RATE, LOW_BIRTH_RATE, NORMAL_BATTLE_RATE, HIGH_BATTLE_RATE 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, special_constitution_ratio, spiritual_roots_ratio, initial_population, world_spiritual_energy, resources = 100000, resources_threshold = 10): # 初始化插件 self.character_creation_plugin = CharacterCreationPlugin(special_constitution_ratio, spiritual_roots_ratio) 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.initial_population = initial_population 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 = [] 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: 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.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 start_simulation(self): self.nth_round = 0 self.create_initial_population() def run_simulation(self, num_rounds): md = "" for _ in range(num_rounds): self.nth_round += 1 print(f"第{self.nth_round}轮模拟:") md += f"第{self.nth_round}轮模拟:\n" self.simulate_round() print(f"当前世界灵气总量:{self.world_spiritual_energy}") print(f"当前存活角色数量:{len(self.characters)}") print("") md += f"当前世界灵气总量:{self.world_spiritual_energy}\n" md += f"当前存活角色数量:{len(self.characters)}\n\n" if len(self.characters) == 0: print("所有角色死亡,模拟结束") md += "所有角色死亡,模拟结束\n" break return md def add_custom_character(self, name, gender, special_constitution, spiritual_roots): character = Character(name, gender, special_constitution, spiritual_roots) self.characters.append(character) def create_initial_population(self): # 创建角色 for _ in range((self.initial_population - len(self.characters))): character = self.character_creation_plugin.create_character() self.characters.append(character) def character_die(self, character): 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) 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