Files
ai-town/engine-python/app/models.py
empty 296e65ff95 feat(engine): add faction system for social emergence
- 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>
2025-12-30 11:03:52 +08:00

109 lines
2.4 KiB
Python

from __future__ import annotations
from pydantic import BaseModel, Field
from typing import List, Dict, Optional
from enum import Enum
class Emotion(str, Enum):
CALM = "calm"
HAPPY = "happy"
ANXIOUS = "anxious"
class Weather(str, Enum):
SUNNY = "sunny"
RAINY = "rainy"
class GlobalMeter(BaseModel):
"""全体能量条"""
value: int = 0
threshold: int = 100
cooldown: int = 0
class WorldEffect(BaseModel):
"""持续影响效果"""
type: str
name: str
intensity: int = 1
remaining_ticks: int = 5
mood_modifier: int = 0
class Opinion(BaseModel):
"""角色对事件的观点"""
about: str # 事件类型 (effect_type)
text: str # 观点内容
tick: int # 生成时的 tick
class Stance(BaseModel):
"""角色立场"""
optimism: float = Field(default=0.5, ge=0.0, le=1.0)
fear: float = Field(default=0.5, ge=0.0, le=1.0)
class Factions(BaseModel):
"""派系分布"""
optimists: int = 0
fearful: int = 0
neutral: int = 0
class AgentState(BaseModel):
emotion: Emotion = Emotion.CALM
goal: str = ""
memory: List[str] = Field(default_factory=list)
opinion: Optional[Opinion] = None
# 记录已评论过的事件,防止重复生成
commented_effects: List[str] = Field(default_factory=list)
# 角色立场
stance: Stance = Field(default_factory=Stance)
class WorldState(BaseModel):
tick: int = 0
weather: Weather = Weather.SUNNY
town_mood: int = Field(default=0, ge=-10, le=10)
agents: Dict[str, AgentState] = Field(default_factory=dict)
events: List[str] = Field(default_factory=list)
global_meter: GlobalMeter = Field(default_factory=GlobalMeter)
world_effects: List[WorldEffect] = Field(default_factory=list)
factions: Factions = Field(default_factory=Factions)
class Event(BaseModel):
type: str
text: str
user: str
ts: float
class StepRequest(BaseModel):
events: List[Event] = Field(default_factory=list)
class Action(BaseModel):
agent_id: str
say: str
do: str
class GlobalEventInfo(BaseModel):
"""世界级事件信息"""
name: str
description: str
class GlobalEventResult(BaseModel):
"""世界级事件触发结果"""
triggered: bool = False
event: Optional[GlobalEventInfo] = None
class StepResponse(BaseModel):
world_state: WorldState
actions: List[Action]
global_event: GlobalEventResult = Field(default_factory=GlobalEventResult)