- Add Factions model (optimists/fearful/neutral) - Implement classify_faction() based on stance thresholds - Add update_factions() to track faction distribution - Add apply_faction_influence() for faction→mood feedback - Integrate faction system into tick flow 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
"""派系系统 - 基于立场分类角色并影响世界"""
|
|
from typing import Dict
|
|
from .models import WorldState, AgentState, Factions
|
|
|
|
# 派系分类阈值
|
|
OPTIMIST_THRESHOLD = 0.6
|
|
FEARFUL_THRESHOLD = 0.6
|
|
|
|
|
|
def classify_faction(agent: AgentState) -> str:
|
|
"""根据 stance 分类角色所属派系"""
|
|
if agent.stance.optimism > OPTIMIST_THRESHOLD:
|
|
return "optimists"
|
|
elif agent.stance.fear > FEARFUL_THRESHOLD:
|
|
return "fearful"
|
|
else:
|
|
return "neutral"
|
|
|
|
|
|
def update_factions(state: WorldState) -> None:
|
|
"""统计各派系人数并更新 world_state"""
|
|
counts = {"optimists": 0, "fearful": 0, "neutral": 0}
|
|
|
|
for agent in state.agents.values():
|
|
faction = classify_faction(agent)
|
|
counts[faction] += 1
|
|
|
|
state.factions = Factions(**counts)
|
|
|
|
|
|
def apply_faction_influence(state: WorldState) -> None:
|
|
"""派系分布影响世界情绪"""
|
|
optimists = state.factions.optimists
|
|
fearful = state.factions.fearful
|
|
|
|
if optimists > fearful:
|
|
state.town_mood = min(10, state.town_mood + 1)
|
|
elif fearful > optimists:
|
|
state.town_mood = max(-10, state.town_mood - 1)
|
|
# 平局时不变化
|