private void GetAction(GoapNode parentNode, List <GoapNode> goapNodeList, List <GoapAction> goapActionList, GoapStatus goapStatus) { for (int i = 0; i < goapActionList.Count; ++i) { GoapAction goapAction = goapActionList[i]; GoapStatus preconditions = goapAction.GetPreconditions(); // 前置条件 GoapStatus effectsStatus = goapAction.GetEffect(); // 执行效果 if (!preconditions.IsContainIn(parentNode.CurrentStatus)) // 不满足前置条件 { continue; } GoapStatus newStatus = parentNode.CurrentStatus.Clone(); newStatus.AddFromStatus(effectsStatus); GoapNode goapNode = new GoapNode(parentNode, goapAction, newStatus, parentNode.Cost + goapAction.Cost); if (goapStatus.IsAnyContainIn(newStatus)) { goapNodeList.Add(goapNode); continue; } List <GoapAction> actionList = Remove(goapActionList, i); GetAction(goapNode, goapNodeList, actionList, goapStatus); } }
public Queue <GoapAction> Plan(List <GoapAction> goapActionList, GoapStatus worldStatus, GoapStatus goalStatus) { Queue <GoapAction> goapActionQueue = new Queue <GoapAction>(); List <GoapAction> validActionList = new List <GoapAction>(); for (int i = 0; i < goapActionList.Count; ++i) { GoapAction goapAction = goapActionList[i]; if (goapAction.CheckProceduralPrecondition()) { validActionList.Add(goapAction); } } List <GoapNode> goapNodeList = new List <GoapNode>(); GoapNode goapNode = new GoapNode(null, null, worldStatus, 0); GetAction(goapNode, goapNodeList, validActionList, goalStatus); if (goapNodeList.Count <= 0) { return(goapActionQueue); } GoapNode heighestNode = GetHighestNode(goapNodeList); goapActionQueue = GetActionQueue(heighestNode); return(goapActionQueue); }
/// <summary> /// 将 a 的值 加到自身 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public void AddFromStatus(GoapStatus a) { foreach (var pair in a.Status()) { AddState(pair.Key, pair.Value); } }
// Use this for initialization protected virtual void Start() { worldStats = new GoapStatus(); goalStatus = new GoapStatus(); SetActions(); }
public GoapStatus Clone() { GoapStatus goapStatus = new GoapStatus(); foreach (var pair in statusDic) { goapStatus.AddState(pair.Key, pair.Value); } return(goapStatus); }
/// <summary> /// 将 a 和 自己 key 相同且值也相同的元素移除 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public void RemoveFromStatus(GoapStatus a) { foreach (var pair in a.Status()) { object value = null; if (statusDic.TryGetValue(pair.Key, out value)) { if (Equals(pair.Value, value)) { RemoveState(pair.Key); } } } }
/// <summary> /// 有任何一个值包含在 a 中 /// </summary> /// <param name="a"></param> /// <returns></returns> public bool IsAnyContainIn(GoapStatus a) { Dictionary <GoapCondition, object> aStatusDic = a.Status(); foreach (var pair in aStatusDic) { object aValue = null; if (aStatusDic.TryGetValue(pair.Key, out aValue)) { if (Equals(pair.Value, aValue)) { return(true); } } } return(false); }
/// <summary> /// 包含于 a /// </summary> /// <param name="a"></param> /// <returns></returns> public bool IsContainIn(GoapStatus a) { Dictionary <GoapCondition, object> aStatusDic = a.Status(); bool isContentIn = true; foreach (var pair in statusDic) { object aValue = null; if (aStatusDic.TryGetValue(pair.Key, out aValue)) { if (Equals(pair.Value, aValue)) { continue; } } isContentIn = false; break; } return(isContentIn); }
public virtual void InitStatus() { preconditionsStatus = new GoapStatus(); effectsStatus = new GoapStatus(); }