File size: 6,400 Bytes
4e30523
bcf0302
 
 
 
 
 
 
 
 
 
 
4e30523
bcf0302
4e30523
bcf0302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a0d866
 
 
 
bcf0302
 
 
 
 
 
 
 
 
 
0a0d866
 
 
bcf0302
 
 
 
 
 
 
 
 
0a0d866
bcf0302
 
 
 
 
 
 
0a0d866
 
bcf0302
 
 
0a0d866
 
bcf0302
 
 
0a0d866
 
bcf0302
 
0a0d866
 
bcf0302
 
 
 
 
 
0a0d866
 
bcf0302
 
4e30523
bcf0302
4e30523
 
bcf0302
 
 
 
4e30523
0a0d866
4e30523
bcf0302
 
 
 
 
 
 
0a0d866
 
 
 
 
bcf0302
 
b41b224
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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