Files
ai-town/engine-python/app/state.py
empty 3f75eb26c4 feat(engine): 实现阵营技能树系统
- 新增 FactionSkill、FactionSkillTree、FactionSkills 数据模型
- 创建 faction_skills.py 模块处理技能解锁和效果应用
- 技能解锁条件:power >= cost 且前置技能已解锁
- 技能效果:increase_positive_emotion、reduce_conflict、increase_fear、suppress_others
- 添加 4 个技能树测试用例,全部 30 个测试通过

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-30 13:25:20 +08:00

56 lines
1.8 KiB
Python

import json
from pathlib import Path
from .models import WorldState, AgentState, GlobalMeter, Stance, Factions, FactionData
from .story_arcs import get_default_story_arcs
from .faction_skills import get_default_faction_skills
STATE_FILE = Path(__file__).parent.parent / "state.json"
def get_default_state() -> WorldState:
"""创建默认的世界状态"""
return WorldState(
tick=0,
weather="sunny",
town_mood=0,
agents={
"alice": AgentState(
emotion="calm",
goal="探索小镇",
memory=[],
stance=Stance(optimism=0.6, fear=0.4),
faction="neutral",
),
"bob": AgentState(
emotion="calm",
goal="与人交流",
memory=[],
stance=Stance(optimism=0.4, fear=0.6),
faction="neutral",
),
},
events=[],
global_meter=GlobalMeter(value=0, threshold=100, cooldown=0),
factions=Factions(
optimists=FactionData(power=0, threshold=10, skill="festival", members=[]),
fearful=FactionData(power=0, threshold=10, skill="panic", members=[])
),
story_arcs=get_default_story_arcs(),
faction_skills=get_default_faction_skills(),
)
def load_state() -> WorldState:
"""从文件加载状态,如果不存在则创建默认状态"""
if STATE_FILE.exists():
with open(STATE_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
return WorldState(**data)
return get_default_state()
def save_state(state: WorldState) -> None:
"""保存状态到文件"""
with open(STATE_FILE, "w", encoding="utf-8") as f:
json.dump(state.model_dump(), f, ensure_ascii=False, indent=2)