yingqianjiang-lingoace commited on
Commit
4e30523
1 Parent(s): 4ece764
Files changed (5) hide show
  1. Character.py +4 -4
  2. WorldSimulation.py +9 -10
  3. app.py +33 -32
  4. plugins/CharacterCreationPlugin.py +5 -1
  5. run.py +2 -2
Character.py CHANGED
@@ -27,7 +27,7 @@ class Character:
27
  self.clan = clan # 宗族
28
 
29
  def die(self):
30
- if self.is_alive:
31
  self.history.append(f"{self.real_age}岁,死亡")
32
  self.special_history.append(f"{self.real_age}岁,死亡")
33
  self.is_alive = False
@@ -206,10 +206,10 @@ class Character:
206
  f"修仙阶段: ({self.cultivation_rank}){self.view_rank()}",
207
  f"特殊体质: {[special_constitution for (special_constitution, is_active) in zip(['战斗体质', '合欢体质', '灵龟体质', '蜉蝣体质'], self.special_constitution) if is_active]}",
208
  f"灵根: {[spiritual_root for (spiritual_root, is_active) in zip(['金', '木', '水', '火', '土'], self.spiritual_roots) if is_active]}",
209
- f"伴侣: {self.partner.name if self.partner is not None else ''}",
210
  f"存活: {self.is_alive}",
211
- f"宗族: {self.clan}",
212
- f"父母: {[parent.name for parent in self.parents]}",
213
  f"孩子: {[child.name for child in self.children]}",
214
  f"Combat Power: {self.combat_power}",
215
  f"Experience Points: {self.experience_points}",
 
27
  self.clan = clan # 宗族
28
 
29
  def die(self):
30
+ if self.is_alive and self.cultivation_rank < IMMORTAL_RANK:
31
  self.history.append(f"{self.real_age}岁,死亡")
32
  self.special_history.append(f"{self.real_age}岁,死亡")
33
  self.is_alive = False
 
206
  f"修仙阶段: ({self.cultivation_rank}){self.view_rank()}",
207
  f"特殊体质: {[special_constitution for (special_constitution, is_active) in zip(['战斗体质', '合欢体质', '灵龟体质', '蜉蝣体质'], self.special_constitution) if is_active]}",
208
  f"灵根: {[spiritual_root for (spiritual_root, is_active) in zip(['金', '木', '水', '火', '土'], self.spiritual_roots) if is_active]}",
209
+ f"伴侣: {self.partner.name if self.partner is not None else ''}",
210
  f"存活: {self.is_alive}",
211
+ f"宗族: {self.clan if self.clan is not None else '无'}",
212
+ f"父母: {[parent.name for parent in self.parents] if self.parents is not None else '无'}",
213
  f"孩子: {[child.name for child in self.children]}",
214
  f"Combat Power: {self.combat_power}",
215
  f"Experience Points: {self.experience_points}",
WorldSimulation.py CHANGED
@@ -1,4 +1,4 @@
1
- from config import DISASTER_FREQUENCY, MARRIAGE_RATE, DISASTER_PROB, NORMAL_BIRTH_RATE, LOW_BIRTH_RATE, NORMAL_BATTLE_RATE, HIGH_BATTLE_RATE
2
  from Character import Character
3
  from plugins.CharacterCreationPlugin import CharacterCreationPlugin
4
  from plugins.CultivationPlugin import CultivationPlugin
@@ -10,9 +10,9 @@ from plugins.BattlePlugin import BattlePlugin
10
  from plugins.ResourceDepletionPlugin import ResourceDepletionPlugin
11
 
12
  class WorldSimulation:
13
- def __init__(self, special_constitution_ratio, spiritual_roots_ratio, initial_population, world_spiritual_energy, resources = 100000, resources_threshold = 10):
14
  # 初始化插件
15
- self.character_creation_plugin = CharacterCreationPlugin(special_constitution_ratio, spiritual_roots_ratio)
16
  self.cultivation_plugin = CultivationPlugin()
17
  self.marriage_plugin = MarriagePlugin(MARRIAGE_RATE)
18
  self.birth_plugin = BirthPlugin(NORMAL_BIRTH_RATE)
@@ -22,7 +22,6 @@ class WorldSimulation:
22
  self.resource_depletion_plugin = ResourceDepletionPlugin(resources_threshold)
23
 
24
  # 初始化参数
25
- self.initial_population = initial_population
26
  self.resources = resources # 总资源量
27
  self.resources_threshold = resources_threshold # 人均最少资源需求
28
  self.init_world_spiritual_energy = world_spiritual_energy
@@ -55,10 +54,6 @@ class WorldSimulation:
55
  self.marriage_plugin.execute(self.characters)
56
  self.cultivation_plugin.execute(self.characters, self.world_spiritual_energy, self.init_world_spiritual_energy, self.consume_spiritual_energy)
57
 
58
- def start_simulation(self):
59
- self.nth_round = 0
60
- self.create_initial_population()
61
-
62
  def run_simulation(self, num_rounds):
63
  md = ""
64
  for _ in range(num_rounds):
@@ -81,13 +76,17 @@ class WorldSimulation:
81
  character = Character(name, gender, special_constitution, spiritual_roots)
82
  self.characters.append(character)
83
 
84
- def create_initial_population(self):
85
  # 创建角色
86
- for _ in range((self.initial_population - len(self.characters))):
 
87
  character = self.character_creation_plugin.create_character()
88
  self.characters.append(character)
89
 
90
  def character_die(self, character):
 
 
 
91
  character.die()
92
  self.world_spiritual_energy += character.consume_spiritual_energy # 灵气回归
93
  if character.partner:
 
1
+ from config import DISASTER_FREQUENCY, MARRIAGE_RATE, DISASTER_PROB, NORMAL_BIRTH_RATE, LOW_BIRTH_RATE, NORMAL_BATTLE_RATE, HIGH_BATTLE_RATE, IMMORTAL_RANK
2
  from Character import Character
3
  from plugins.CharacterCreationPlugin import CharacterCreationPlugin
4
  from plugins.CultivationPlugin import CultivationPlugin
 
10
  from plugins.ResourceDepletionPlugin import ResourceDepletionPlugin
11
 
12
  class WorldSimulation:
13
+ def __init__(self, world_spiritual_energy, resources = 100000, resources_threshold = 10):
14
  # 初始化插件
15
+ self.character_creation_plugin = CharacterCreationPlugin()
16
  self.cultivation_plugin = CultivationPlugin()
17
  self.marriage_plugin = MarriagePlugin(MARRIAGE_RATE)
18
  self.birth_plugin = BirthPlugin(NORMAL_BIRTH_RATE)
 
22
  self.resource_depletion_plugin = ResourceDepletionPlugin(resources_threshold)
23
 
24
  # 初始化参数
 
25
  self.resources = resources # 总资源量
26
  self.resources_threshold = resources_threshold # 人均最少资源需求
27
  self.init_world_spiritual_energy = world_spiritual_energy
 
54
  self.marriage_plugin.execute(self.characters)
55
  self.cultivation_plugin.execute(self.characters, self.world_spiritual_energy, self.init_world_spiritual_energy, self.consume_spiritual_energy)
56
 
 
 
 
 
57
  def run_simulation(self, num_rounds):
58
  md = ""
59
  for _ in range(num_rounds):
 
76
  character = Character(name, gender, special_constitution, spiritual_roots)
77
  self.characters.append(character)
78
 
79
+ def create_population(self, initial_population, special_constitution_ratio, spiritual_roots_ratio):
80
  # 创建角色
81
+ self.character_creation_plugin.set_parameters(special_constitution_ratio, spiritual_roots_ratio)
82
+ for _ in range((initial_population - len(self.characters))):
83
  character = self.character_creation_plugin.create_character()
84
  self.characters.append(character)
85
 
86
  def character_die(self, character):
87
+ # 成仙者不会死亡
88
+ if character.cultivation_level > IMMORTAL_RANK:
89
+ return
90
  character.die()
91
  self.world_spiritual_energy += character.consume_spiritual_energy # 灵气回归
92
  if character.partner:
app.py CHANGED
@@ -6,14 +6,11 @@ from WorldSimulation import WorldSimulation
6
  simulation = None
7
  num_rounds = 0
8
 
9
- def initialize_world(c1, c2, c3, c4, r1, r2, r3, r4, r5, initial_population=3000, world_spiritual_energy=1000000):
10
  global simulation
11
- special_constitution_ratio = [c1, c2, c3, c4]
12
- spiritual_roots_ratio = [r1, r2, r3, r4, r5]
13
- initial_population = int(initial_population)
14
- simulation = WorldSimulation(special_constitution_ratio, spiritual_roots_ratio, initial_population, world_spiritual_energy)
15
 
16
- return f"世界初始化成功:\n\n初始人口:{initial_population}\n\n世界灵气能量:{world_spiritual_energy}"
17
 
18
 
19
  def add_custom_character(name, gender, special_constitution, spiritual_roots):
@@ -22,8 +19,11 @@ def add_custom_character(name, gender, special_constitution, spiritual_roots):
22
  simulation.add_custom_character(name, gender, special_constitution, spiritual_roots)
23
  return str(simulation.characters[-1])
24
 
25
- def start_simulation():
26
- simulation.start_simulation()
 
 
 
27
  return f"世界人口数量达到{len(simulation.characters)},可以运行模拟了。"
28
 
29
  def run_simulation(rounds):
@@ -54,8 +54,27 @@ with gr.Blocks() as demo:
54
  # 初始化世界
55
  gr.Markdown("## 初始化世界")
56
  with gr.Row():
57
- initial_population_input = gr.Number(label="初始人口", value=3000, precision=0)
58
  world_spiritual_energy_input = gr.Number(label="世界灵气能量", value=1000000)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  # 特殊体质
60
  gr.Markdown("特殊体质比例")
61
  with gr.Row():
@@ -73,27 +92,9 @@ with gr.Blocks() as demo:
73
  gr.Number(label="水", minimum=0, maximum=1, value=0.04),
74
  gr.Number(label="火", minimum=0, maximum=1, value=0.04),
75
  gr.Number(label="土", minimum=0, maximum=1, value=0.04)]
76
- initialize_button = gr.Button("初始化世界")
77
- output_text = gr.Markdown(label="初始化结果")
78
- initialize_button.click(fn=initialize_world, inputs=[*special_constitution_ratio_input, *spiritual_roots_ratio_input, initial_population_input, world_spiritual_energy_input], outputs=output_text)
79
-
80
- # 添加角色
81
- with gr.Accordion("添加角色"):
82
- gr.Markdown("每次添加一个,添加完可以继续添加。")
83
- name_input = gr.Textbox(label="姓名")
84
- gender_input = gr.Radio(["男", "女"], label="性别")
85
- special_constitution_input = gr.CheckboxGroup(["战斗体质", "合欢体质", "灵龟体质", "蜉蝣体质"], label="特殊体质", type="index")
86
- spiritual_roots_input = gr.CheckboxGroup(["金", "木", "水", "火", "土"], label="灵根", type="index")
87
- add_character_button = gr.Button("添加角色")
88
- new_character_info_output = gr.Textbox(label="新角色信息")
89
- add_character_button.click(fn=add_custom_character, inputs=[name_input,
90
- gender_input,
91
- special_constitution_input, spiritual_roots_input], outputs=new_character_info_output)
92
-
93
- # 开始模拟
94
  start_simulation_button = gr.Button("生成初始人口")
95
  output_text = gr.Markdown("")
96
- start_simulation_button.click(fn=start_simulation, inputs=[], outputs=output_text)
97
 
98
  # 运行模拟
99
  gr.Markdown("## 运行模拟\n\n输入轮数,运行模拟,可以多次运行。")
@@ -104,10 +105,10 @@ with gr.Blocks() as demo:
104
  run_simulation_button.click(fn=run_simulation, inputs=[rounds_input], outputs=[simulation_result_output])
105
 
106
  # 查看统计信息
107
- gr.Markdown("## 查看统计信息")
108
- get_stats_button = gr.Button("查看统计信息")
109
- with gr.Box():
110
- world_stats_output = gr.Markdown("")
111
  get_stats_button.click(fn=get_world_stats, inputs=[], outputs=[world_stats_output])
112
 
113
  # 查看角色信息
 
6
  simulation = None
7
  num_rounds = 0
8
 
9
+ def initialize_world(world_spiritual_energy=1000000):
10
  global simulation
11
+ simulation = WorldSimulation(world_spiritual_energy=world_spiritual_energy)
 
 
 
12
 
13
+ return f"世界初始化成功:\n\n世界灵气能量:{world_spiritual_energy}"
14
 
15
 
16
  def add_custom_character(name, gender, special_constitution, spiritual_roots):
 
19
  simulation.add_custom_character(name, gender, special_constitution, spiritual_roots)
20
  return str(simulation.characters[-1])
21
 
22
+ def start_simulation(c1, c2, c3, c4, r1, r2, r3, r4, r5, initial_population=3000):
23
+ special_constitution_ratio = [c1, c2, c3, c4]
24
+ spiritual_roots_ratio = [r1, r2, r3, r4, r5]
25
+ initial_population = int(initial_population)
26
+ simulation.create_population(special_constitution_ratio=special_constitution_ratio, spiritual_roots_ratio=spiritual_roots_ratio, initial_population=initial_population)
27
  return f"世界人口数量达到{len(simulation.characters)},可以运行模拟了。"
28
 
29
  def run_simulation(rounds):
 
54
  # 初始化世界
55
  gr.Markdown("## 初始化世界")
56
  with gr.Row():
 
57
  world_spiritual_energy_input = gr.Number(label="世界灵气能量", value=1000000)
58
+ initialize_button = gr.Button("初始化世界")
59
+ output_text = gr.Markdown(label="初始化结果")
60
+ initialize_button.click(fn=initialize_world, inputs=[ world_spiritual_energy_input], outputs=output_text)
61
+
62
+ # 添加角色
63
+ with gr.Accordion("添加角色 (可选)", open=False):
64
+ gr.Markdown("每次添加一个,添加完可以继续添加。")
65
+ name_input = gr.Textbox(label="姓名")
66
+ gender_input = gr.Radio(["男", "女"], label="性别")
67
+ special_constitution_input = gr.CheckboxGroup(["战斗体质", "合欢体质", "灵龟体质", "蜉蝣体质"], label="特殊体质", type="index")
68
+ spiritual_roots_input = gr.CheckboxGroup(["金", "木", "水", "火", "土"], label="灵根", type="index")
69
+ add_character_button = gr.Button("添加角色")
70
+ new_character_info_output = gr.Textbox(label="新角色信息")
71
+ add_character_button.click(fn=add_custom_character, inputs=[name_input,
72
+ gender_input,
73
+ special_constitution_input, spiritual_roots_input], outputs=new_character_info_output)
74
+
75
+ # 生成初始人口
76
+ gr.Markdown("## 生成初始人口")
77
+ initial_population_input = gr.Number(label="初始人口", value=3000, precision=0)
78
  # 特殊体质
79
  gr.Markdown("特殊体质比例")
80
  with gr.Row():
 
92
  gr.Number(label="水", minimum=0, maximum=1, value=0.04),
93
  gr.Number(label="火", minimum=0, maximum=1, value=0.04),
94
  gr.Number(label="土", minimum=0, maximum=1, value=0.04)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  start_simulation_button = gr.Button("生成初始人口")
96
  output_text = gr.Markdown("")
97
+ start_simulation_button.click(fn=start_simulation, inputs=[*special_constitution_ratio_input, *spiritual_roots_ratio_input, initial_population_input], outputs=output_text)
98
 
99
  # 运行模拟
100
  gr.Markdown("## 运行模拟\n\n输入轮数,运行模拟,可以多次运行。")
 
105
  run_simulation_button.click(fn=run_simulation, inputs=[rounds_input], outputs=[simulation_result_output])
106
 
107
  # 查看统计信息
108
+ with gr.Accordion("查看统计信息", open=False):
109
+ get_stats_button = gr.Button("查看统计信息")
110
+ with gr.Box():
111
+ world_stats_output = gr.Markdown("")
112
  get_stats_button.click(fn=get_world_stats, inputs=[], outputs=[world_stats_output])
113
 
114
  # 查看角色信息
plugins/CharacterCreationPlugin.py CHANGED
@@ -3,7 +3,7 @@ from Character import Character
3
  from utils import get_random_name
4
 
5
  class CharacterCreationPlugin:
6
- def __init__(self, special_constitution_ratio, spiritual_roots_ratio):
7
  self.special_constitution_ratio = special_constitution_ratio
8
  self.spiritual_roots_ratio = spiritual_roots_ratio
9
 
@@ -16,3 +16,7 @@ class CharacterCreationPlugin:
16
 
17
  character = Character(get_random_name(), random.choice(["男", "女"]), special_constitution, spiritual_roots)
18
  return character
 
 
 
 
 
3
  from utils import get_random_name
4
 
5
  class CharacterCreationPlugin:
6
+ def __init__(self, special_constitution_ratio=None, spiritual_roots_ratio=None):
7
  self.special_constitution_ratio = special_constitution_ratio
8
  self.spiritual_roots_ratio = spiritual_roots_ratio
9
 
 
16
 
17
  character = Character(get_random_name(), random.choice(["男", "女"]), special_constitution, spiritual_roots)
18
  return character
19
+
20
+ def set_parameters(self, special_constitution_ratio, spiritual_roots_ratio):
21
+ self.special_constitution_ratio = special_constitution_ratio
22
+ self.spiritual_roots_ratio = spiritual_roots_ratio
run.py CHANGED
@@ -2,13 +2,13 @@ from CharacterStatistics import CharacterStatistics
2
  from WorldSimulation import WorldSimulation
3
 
4
  # 创建一个世界模拟对象
5
- simulation = WorldSimulation(special_constitution_ratio=[0.001, 0.001, 0.001, 0.001], spiritual_roots_ratio=[0.04, 0.04, 0.04, 0.04, 0.04], initial_population=3000, world_spiritual_energy=1000000)
6
 
7
  # 运行模拟
8
  simulation.add_custom_character("CoraBaby1", "女", special_constitution=[1, 0, 0, 1], spiritual_roots=[1, 1, 1, 1, 1])
9
  simulation.add_custom_character("CoraBaby2", "女", special_constitution=[0, 1, 0, 1], spiritual_roots=[1, 1, 1, 1, 1])
10
 
11
- simulation.start_simulation()
12
  simulation.run_simulation(num_rounds=10)
13
 
14
  # 查看世界统计
 
2
  from WorldSimulation import WorldSimulation
3
 
4
  # 创建一个世界模拟对象
5
+ simulation = WorldSimulation(world_spiritual_energy=1000000)
6
 
7
  # 运行模拟
8
  simulation.add_custom_character("CoraBaby1", "女", special_constitution=[1, 0, 0, 1], spiritual_roots=[1, 1, 1, 1, 1])
9
  simulation.add_custom_character("CoraBaby2", "女", special_constitution=[0, 1, 0, 1], spiritual_roots=[1, 1, 1, 1, 1])
10
 
11
+ simulation.create_population(special_constitution_ratio=[0.001, 0.001, 0.001, 0.001], spiritual_roots_ratio=[0.04, 0.04, 0.04, 0.04, 0.04], initial_population=3000)
12
  simulation.run_simulation(num_rounds=10)
13
 
14
  # 查看世界统计