add more relations

This commit is contained in:
bridge
2025-10-02 21:40:30 +08:00
parent 939b1102eb
commit 3711987379
3 changed files with 64 additions and 15 deletions

View File

@@ -23,7 +23,7 @@ from src.classes.magic_stone import MagicStone
from src.classes.hp_and_mp import HP, MP, HP_MAX_BY_REALM, MP_MAX_BY_REALM
from src.utils.id_generator import get_avatar_id
from src.utils.config import CONFIG
from src.classes.relation import Relation
from src.classes.relation import Relation, get_reciprocal
from src.run.log import get_logger
from src.classes.alignment import Alignment
@@ -398,14 +398,16 @@ class Avatar:
def set_relation(self, other: "Avatar", relation: Relation) -> None:
"""
设置与另一个角色的关系(对称)
设置与另一个角色的关系。
- 对称关系(如 FRIEND/ENEMY/LOVERS/SIBLING/KIN会在对方处写入相同的关系。
- 有向关系(如 MASTER、APPRENTICE、PARENT、CHILD会在对方处写入对偶关系。
"""
if other is self:
return
self.relations[other] = relation
# 保持对称
# 写入对方的对偶关系(对称关系会得到同一枚举值)
if getattr(other, "relations", None) is not None:
other.relations[self] = relation
other.relations[self] = get_reciprocal(relation)
def get_relation(self, other: "Avatar") -> Optional[Relation]:
return self.relations.get(other)

View File

@@ -4,22 +4,69 @@ from enum import Enum
class Relation(Enum):
KINSHIP = "kinship" # 亲子/亲属
LOVERS = "lovers" # 情侣/道侣
MASTER_APPRENTICE = "mentorship" # 师徒
FRIEND = "friend" # 朋友
ENEMY = "enemy" # 仇人
# —— 血缘(先天) ——
PARENT = "parent" # 父/母 -> 子(有向)
CHILD = "child" # 子 -> 父/母(有向)
SIBLING = "sibling" # 兄弟姐妹(对称)
KIN = "kin" # 其他亲属(对称,泛化)
# —— 后天(社会/情感) ——
MASTER = "master" # 师傅 -> 徒弟(有向)
APPRENTICE = "apprentice" # 徒弟 -> 师傅(有向)
LOVERS = "lovers" # 情侣/道侣(对称)
FRIEND = "friend" # 朋友(对称)
ENEMY = "enemy" # 仇人/敌人(对称)
def __str__(self) -> str:
return relation_strs.get(self, self.value)
return relation_display_names.get(self, self.value)
relation_strs = {
Relation.KINSHIP: "亲属",
relation_display_names = {
# 血缘(先天)
Relation.PARENT: "父母",
Relation.CHILD: "子女",
Relation.SIBLING: "兄弟姐妹",
Relation.KIN: "亲属",
# 后天(社会/情感)
Relation.MASTER: "师傅",
Relation.APPRENTICE: "徒弟",
Relation.LOVERS: "情侣",
Relation.MASTER_APPRENTICE: "师徒",
Relation.FRIEND: "朋友",
Relation.ENEMY: "仇人",
}
# 关系是否属于“先天”(血缘),其余为“后天”
INNATE_RELATIONS: set[Relation] = {
Relation.PARENT, Relation.CHILD, Relation.SIBLING, Relation.KIN,
}
def is_innate(relation: Relation) -> bool:
return relation in INNATE_RELATIONS
# 有向关系的对偶映射;对称关系映射到自身
RECIPROCAL_RELATION: dict[Relation, Relation] = {
# 血缘
Relation.PARENT: Relation.CHILD,
Relation.CHILD: Relation.PARENT,
Relation.SIBLING: Relation.SIBLING,
Relation.KIN: Relation.KIN,
# 后天
Relation.MASTER: Relation.APPRENTICE,
Relation.APPRENTICE: Relation.MASTER,
Relation.LOVERS: Relation.LOVERS,
Relation.FRIEND: Relation.FRIEND,
Relation.ENEMY: Relation.ENEMY,
}
def get_reciprocal(relation: Relation) -> Relation:
"""
给定 A->B 的关系,返回应当写入 B->A 的关系。
对于对称关系(如 FRIEND/ENEMY/LOVERS/SIBLING/KIN返回其本身。
"""
return RECIPROCAL_RELATION.get(relation, relation)