From cc9ffc35577befead91def738e863747b7da1770 Mon Sep 17 00:00:00 2001 From: bridge Date: Sat, 23 Aug 2025 22:02:47 +0800 Subject: [PATCH] fix bug --- src/classes/age.py | 25 +++++++++++++++++++++++++ src/classes/avatar.py | 6 ++++++ src/sim/simulator.py | 1 + tests/run_front.py | 17 ++++++++++++----- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/classes/age.py b/src/classes/age.py index 143ad08..d755b20 100644 --- a/src/classes/age.py +++ b/src/classes/age.py @@ -1,4 +1,5 @@ import random +from src.classes.calendar import Month, Year from src.classes.cultivation import Realm class Age: @@ -55,6 +56,30 @@ class Age: 判断是否老死 """ return random.random() < self.get_death_probability(realm) + + def calculate_age(self, current_month: Month, current_year: Year, birth_month: Month, birth_year: Year) -> int: + """ + 计算准确的年龄(整数年) + + Args: + current_month: 当前月份 + current_year: 当前年份 + birth_month: 出生月份 + birth_year: 出生年份 + + Returns: + 整数年龄 + """ + age = current_year - birth_year + if current_month.value < birth_month.value: + age -= 1 + return max(0, age) + + def update_age(self, current_month: Month, current_year: Year, birth_month: Month, birth_year: Year): + """ + 更新年龄 + """ + self.age = self.calculate_age(current_month, current_year, birth_month, birth_year) def __str__(self) -> str: """返回年龄的字符串表示""" diff --git a/src/classes/avatar.py b/src/classes/avatar.py index 79db078..3d162ab 100644 --- a/src/classes/avatar.py +++ b/src/classes/avatar.py @@ -97,6 +97,12 @@ class Avatar: 如果老死返回True,否则返回False """ return self.age.death_by_old_age(self.cultivation_progress.realm) + + def update_age(self, current_month: Month, current_year: Year): + """ + 更新年龄 + """ + self.age.update_age(current_month, current_year, self.birth_month, self.birth_year) def get_age_info(self) -> dict: """ diff --git a/src/sim/simulator.py b/src/sim/simulator.py index 1e34255..409cb1b 100644 --- a/src/sim/simulator.py +++ b/src/sim/simulator.py @@ -26,6 +26,7 @@ class Simulator: death_avatar_ids.append(avatar_id) event = Event(self.year, self.month, f"{avatar.name} 老死了,时年{avatar.age.get_age()}岁") events.append(event) + avatar.update_age(self.month, self.year) for avatar_id in death_avatar_ids: self.avatars.pop(avatar_id) diff --git a/tests/run_front.py b/tests/run_front.py index 137f9dc..fe037a4 100644 --- a/tests/run_front.py +++ b/tests/run_front.py @@ -710,21 +710,23 @@ def random_gender() -> Gender: return Gender.MALE if random.random() < 0.5 else Gender.FEMALE -def make_avatars(world: World, count: int = 12) -> dict[int, Avatar]: +def make_avatars(world: World, count: int = 12, current_year: Year = Year(100)) -> dict[int, Avatar]: avatars: dict[int, Avatar] = {} width, height = world.map.width, world.map.height for i in range(count): name = f"NPC{i+1:03d}" - birth_year = Year(random.randint(1990, 2010)) - birth_month = random.choice(list(Month)) + # 随机生成年龄,范围从16到60岁 age_years = random.randint(16, 60) + # 根据当前年份和年龄计算出生年份 + birth_year = current_year - age_years + birth_month = random.choice(list(Month)) gender = random_gender() # 随机生成level,范围从0到120(对应四个大境界) level = random.randint(0, 120) cultivation_progress = CultivationProgress(level) - # 创建Age实例,传入年龄和境界 + # 创建Age实例,传入年龄 age = Age(age_years) # 找一个非海域的出生点 @@ -764,8 +766,13 @@ def main(): game_map = build_rich_random_map(width=width, height=height) world = World(map=game_map) + # 设置模拟器从第100年开始 sim = Simulator() - sim.avatars.update(make_avatars(world, count=14)) + sim.year = Year(100) # 设置初始年份为100年 + sim.month = Month.JANUARY # 设置初始月份为1月 + + # 创建角色,传入当前年份确保年龄与生日匹配 + sim.avatars.update(make_avatars(world, count=14, current_year=sim.year)) front = Front( world=world,