fix save bugs
This commit is contained in:
@@ -69,6 +69,14 @@ class Action(ABC):
|
||||
"""
|
||||
return str(self.__class__.__name__)
|
||||
|
||||
def get_save_data(self) -> dict:
|
||||
"""获取需要存档的运行时数据"""
|
||||
return {}
|
||||
|
||||
def load_save_data(self, data: dict) -> None:
|
||||
"""加载运行时数据"""
|
||||
pass
|
||||
|
||||
|
||||
class DefineAction(Action):
|
||||
def __init__(self, avatar: Avatar, world: World):
|
||||
@@ -99,6 +107,24 @@ class DefineAction(Action):
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_save_data(self) -> dict:
|
||||
data = super().get_save_data()
|
||||
# 很多长态动作(包括MoveToDirection)都会设置此属性
|
||||
if hasattr(self, 'start_monthstamp'):
|
||||
val = self.start_monthstamp
|
||||
data['start_monthstamp'] = int(val) if val is not None else None
|
||||
return data
|
||||
|
||||
def load_save_data(self, data: dict) -> None:
|
||||
super().load_save_data(data)
|
||||
if 'start_monthstamp' in data:
|
||||
val = data['start_monthstamp']
|
||||
if val is not None:
|
||||
from src.classes.calendar import MonthStamp
|
||||
self.start_monthstamp = MonthStamp(val)
|
||||
else:
|
||||
self.start_monthstamp = None
|
||||
|
||||
|
||||
class LLMAction(Action):
|
||||
"""
|
||||
@@ -199,5 +225,3 @@ class TimedAction(DefineAction, ActualActionMixin):
|
||||
self._execute(**params_for_execute)
|
||||
done = (self.world.month_stamp - self.start_monthstamp) >= (self.duration_months - 1)
|
||||
return ActionResult(status=(ActionStatus.COMPLETED if done else ActionStatus.RUNNING), events=[])
|
||||
|
||||
|
||||
|
||||
@@ -182,15 +182,20 @@ class AvatarLoadMixin:
|
||||
avatar.planned_actions = [ActionPlan.from_dict(plan_data) for plan_data in planned_actions_data]
|
||||
|
||||
# 重建current_action(如果有)
|
||||
current_action_data = data.get("current_action")
|
||||
if current_action_data is not None:
|
||||
current_action_dict = data.get("current_action")
|
||||
if current_action_dict is not None:
|
||||
try:
|
||||
action = avatar.create_action(current_action_data["action_name"])
|
||||
action = avatar.create_action(current_action_dict["action_name"])
|
||||
|
||||
# 恢复动作内部状态
|
||||
if "state" in current_action_dict:
|
||||
action.load_save_data(current_action_dict["state"])
|
||||
|
||||
from src.classes.action_runtime import ActionInstance
|
||||
avatar.current_action = ActionInstance(
|
||||
action=action,
|
||||
params=current_action_data["params"],
|
||||
status=current_action_data["status"]
|
||||
params=current_action_dict["params"],
|
||||
status=current_action_dict["status"]
|
||||
)
|
||||
except Exception:
|
||||
# 如果动作无法重建,跳过(容错)
|
||||
|
||||
@@ -42,7 +42,8 @@ class AvatarSaveMixin:
|
||||
current_action_dict = {
|
||||
"action_name": self.current_action.action.__class__.__name__,
|
||||
"params": self.current_action.params,
|
||||
"status": self.current_action.status
|
||||
"status": self.current_action.status,
|
||||
"state": self.current_action.action.get_save_data()
|
||||
}
|
||||
|
||||
# 序列化planned_actions
|
||||
|
||||
Reference in New Issue
Block a user