private void UpdateGoapNodes <T, W>(IReGoapAgent <T, W> agent) { if (nodes == null) { nodes = new List <ReGoapNodeEditor>(); } if (agentHelper == null || agent == null || !agent.IsActive() || agent.GetMemory() == null) { return; } nodes.Clear(); var width = 300f; var height = 70f; var nodePosition = new Vector2(0f, 60f); var nodeMiddleY = new Vector2(0f, height * 0.5f); ReGoapNodeEditor?previousNode = null; foreach (var goal in agent.GetGoalsSet()) { if (goal.GetGoalState() == null) { continue; } var text = string.Format("<b>GOAL</b> <i>{0}</i>\n", goal); foreach (var keyValue in goal.GetGoalState().GetValues()) { text += string.Format("<b>'{0}'</b> = <i>'{1}'</i>", keyValue.Key, keyValue.Value); } var style = nodeStyle; if (agent.IsActive() && agent.GetCurrentGoal() == goal) { style = activeStyle; } var newNode = DrawGenericNode(text, width, height, style, ref nodePosition); if (previousNode.HasValue) { var startPosition = previousNode.Value.Rect.max - nodeMiddleY - new Vector2(10f, 0f); var endPosition = newNode.Rect.min + nodeMiddleY + new Vector2(10f, 0f); Handles.DrawLine(startPosition, endPosition); } previousNode = newNode; } previousNode = null; nodePosition = new Vector2(0f, nodePosition.y + height + 10); height = 66; var maxHeight = height; var emptyGoal = agent.InstantiateNewState(); GoapActionStackData <T, W> stackData; stackData.agent = agent; stackData.currentState = agent.GetMemory().GetWorldState(); stackData.goalState = emptyGoal; stackData.next = null; stackData.settings = null; foreach (var action in agent.GetActionsSet()) { var curHeight = height; var text = string.Format("<b>POSS.ACTION</b> <i>{0}</i>\n", action.GetName()); text += "-<b>preconditions</b>-\n"; var preconditionsDifferences = agent.InstantiateNewState(); var preconditions = action.GetPreconditions(stackData); if (preconditions == null) { continue; } preconditions.MissingDifference(stackData.currentState, ref preconditionsDifferences); foreach (var preconditionPair in preconditions.GetValues()) { curHeight += 13; var color = "#004d00"; if (preconditionsDifferences.GetValues().ContainsKey(preconditionPair.Key)) { color = "#800000"; } text += string.Format("<color={2}>'<b>{0}</b>' = '<i>{1}</i>'</color>\n", preconditionPair.Key, preconditionPair.Value, color); } preconditionsDifferences.Recycle(); text += "-<b>effects</b>-\n"; foreach (var effectPair in action.GetEffects(stackData).GetValues()) { curHeight += 13; text += string.Format("'<b>{0}</b>' = '<i>{1}</i>'\n", effectPair.Key, effectPair.Value); } curHeight += 13; var proceduralCheck = action.CheckProceduralCondition(stackData); text += string.Format("<color={0}>-<b>proceduralCondition</b>: {1}</color>\n", proceduralCheck ? "#004d00" : "#800000", proceduralCheck); maxHeight = Mathf.Max(maxHeight, curHeight); nodeMiddleY = new Vector2(0f, curHeight * 0.5f); var newNode = DrawGenericNode(text, width, curHeight, possibleActionStyle, ref nodePosition); if (previousNode.HasValue) { var startPosition = previousNode.Value.Rect.max - nodeMiddleY - new Vector2(10f, 0f); var endPosition = newNode.Rect.min + nodeMiddleY + new Vector2(10f, 0f); Handles.DrawLine(startPosition, endPosition); } previousNode = newNode; } previousNode = null; nodePosition.x = 0; nodePosition.y += maxHeight + 10; height = 40; nodeMiddleY = new Vector2(0f, height * 0.5f); if (agent.GetCurrentGoal() != null) { foreach (var action in agent.GetStartingPlan().ToArray()) { var style = actionNodeStyle; if (action.Action.IsActive()) { style = activeActionNodeStyle; } var text = string.Format("<b>ACTION</b> <i>{0}</i>\n", action.Action.GetName()); var newNode = DrawGenericNode(text, width, height, style, ref nodePosition); if (previousNode.HasValue) { var startPosition = previousNode.Value.Rect.max - nodeMiddleY - new Vector2(10f, 0f); var endPosition = newNode.Rect.min + nodeMiddleY + new Vector2(10f, 0f); Handles.DrawLine(startPosition, endPosition); } previousNode = newNode; } } if (agent.GetMemory() != null) { nodePosition = new Vector2(0, nodePosition.y + height + 10); width = 500; height = 40; nodeMiddleY = new Vector2(0f, height * 0.5f); var nodeText = "<b>WORLD STATE</b>\n"; foreach (var pair in agent.GetMemory().GetWorldState().GetValues()) { nodeText += string.Format("'<b>{0}</b>' = '<i>{1}</i>'\n", pair.Key, pair.Value); height += 13; } DrawGenericNode(nodeText, width, height, worldStateStyle, ref nodePosition); } }
private void UpdateGoapNodes(GameObject gameObj) { if (agent == null || !agent.IsActive() || agent.GetMemory() == null) { return; } if (nodes == null) { nodes = new List <ReGoapNodeEditor>(); } nodes.Clear(); var width = 250f; var height = 70f; var nodePosition = Vector2.zero; foreach (var goal in gameObj.GetComponents <IReGoapGoal>()) { if (goal.GetGoalState() == null) { continue; } var text = string.Format("<b>GOAL</b> <i>{0}</i>\n", goal); foreach (var keyValue in goal.GetGoalState().GetValues()) { text += string.Format("<b>'{0}'</b> = <i>'{1}'</i>", keyValue.Key, keyValue.Value); } var style = nodeStyle; if (agent.IsActive() && agent.GetCurrentGoal() == goal) { style = activeStyle; } DrawGenericNode(text, width, height, style, ref nodePosition); } nodePosition = new Vector2(0f, nodePosition.y + height + 10); height = 66; var maxHeight = height; var worldState = agent.GetMemory().GetWorldState(); foreach (var action in agent.GetActionsSet()) { var curHeight = height; var text = string.Format("<b>POSS.ACTION</b> <i>{0}</i>\n", action.GetName()); text += "-<b>preconditions</b>-\n"; ReGoapState preconditionsDifferences = new ReGoapState(); var preconditions = action.GetPreconditions(null); if (preconditions == null) { continue; } preconditions.MissingDifference(worldState, ref preconditionsDifferences); foreach (var preconditionPair in preconditions.GetValues()) { curHeight += 13; var color = "#004d00"; if (preconditionsDifferences.GetValues().ContainsKey(preconditionPair.Key)) { color = "#800000"; } text += string.Format("<color={2}>'<b>{0}</b>' = '<i>{1}</i>'</color>\n", preconditionPair.Key, preconditionPair.Value, color); } text += "-<b>effects</b>-\n"; foreach (var effectPair in action.GetEffects(null).GetValues()) { curHeight += 13; text += string.Format("'<b>{0}</b>' = '<i>{1}</i>'\n", effectPair.Key, effectPair.Value); } curHeight += 13; var proceduralCheck = action.CheckProceduralCondition(agent, null); text += string.Format("<color={0}>-<b>proceduralCondition</b>: {1}</color>\n", proceduralCheck ? "#004d00" : "#800000", proceduralCheck); maxHeight = Mathf.Max(maxHeight, curHeight); DrawGenericNode(text, width, curHeight, possibleActionStyle, ref nodePosition); } nodePosition.x = 0; nodePosition.y += maxHeight + 10; height = 40; if (agent.GetCurrentGoal() != null) { foreach (var action in agent.GetStartingPlan().ToArray()) { var style = actionNodeStyle; if (action.Action.IsActive()) { style = activeActionNodeStyle; } var text = string.Format("<b>ACTION</b> <i>{0}</i>\n", action.Action.GetName()); DrawGenericNode(text, width, height, style, ref nodePosition); } } if (agent.GetMemory() != null) { nodePosition = new Vector2(0, nodePosition.y + height + 10); width = 500; height = 40; var nodeText = "<b>WORLD STATE</b>\n"; foreach (var pair in agent.GetMemory().GetWorldState().GetValues()) { nodeText += string.Format("'<b>{0}</b>' = '<i>{1}</i>'\n", pair.Key, pair.Value); height += 13; } DrawGenericNode(nodeText, width, height, worldStateStyle, ref nodePosition); } }