Exemple #1
0
        public void LoadFile(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }

            BehaviorDataController.Instance.ConfigDataDic.Clear();

            BehaviorReadWrite readWrite        = new BehaviorReadWrite();
            BehaviorTreeData  behaviorTreeData = ReadFile(fileName, true);

            if (null == behaviorTreeData)
            {
                UnityEngine.Debug.LogError("file is null:" + fileName);
                return;
            }

            BehaviorDataController.Instance.PlayState = BehaviorPlayType.STOP;
            NodeNotify.SetPlayState((int)BehaviorPlayType.STOP);

            BehaviorDataController.Instance.SetBehaviorData(behaviorTreeData);

            BehaviorDataController.Instance.CurrentSelectId    = -1;
            BehaviorDataController.Instance.CurrentOpenSubTree = -1;

            BehaviorRunTime.Instance.Reset(behaviorTreeData);
        }
Exemple #2
0
        public static void ImportParameter()
        {
            BehaviorTreeData behaviorData = BehaviorManager.Instance.BehaviorTreeData;
            string           fileName     = "BehaviorTree";

            behaviorData = ImportParameter(behaviorData, fileName);
        }
        public void Update(string filePath)
        {
            DirectoryInfo dInfo = new DirectoryInfo(filePath);

            FileInfo[] fileInfoArr = dInfo.GetFiles("*.bytes", SearchOption.TopDirectoryOnly);
            for (int i = 0; i < fileInfoArr.Length; ++i)
            {
                string            fullName  = fileInfoArr[i].FullName;
                BehaviorReadWrite readWrite = new BehaviorReadWrite();
                BehaviorTreeData  treeData  = readWrite.ReadJson(fullName);

                treeData = DataNodeIdStandardTool.StandardID(treeData);
                CheckCircle(treeData, treeData.rootNodeId);

                treeData = UpdateData(treeData);
                string fileName = System.IO.Path.GetFileNameWithoutExtension(fullName);

                fileName = string.Format("{0}.bytes", fileName);
                string jsonFilePath = CommonUtils.FileUtils.CombinePath(filePath, "Json", fileName);
                bool   value        = readWrite.WriteJson(treeData, jsonFilePath);
                if (!value)
                {
                    Debug.LogError("WriteError:" + jsonFilePath);
                }
            }
        }
Exemple #4
0
        private static void CheckChildID(BehaviorTreeData treeData, int id)
        {
            NodeValue nodeValue = treeData.nodeList.Find((a) => {
                return(a.id == id);
            });

            if (null == nodeValue || nodeValue.childNodeList.Count <= 0)
            {
                return;
            }

            HashSet <int> hash = new HashSet <int>();

            for (int i = nodeValue.childNodeList.Count - 1; i >= 0; --i)
            {
                int childId = nodeValue.childNodeList[i];
                if (hash.Contains(childId))
                {
                    nodeValue.childNodeList.RemoveAt(i);
                    Debug.LogError(treeData.fileName + "   NodeValue:" + nodeValue.id + "  Has Same Child:" + childId);
                }
                else
                {
                    hash.Add(childId);
                }

                CheckChildID(treeData, childId);
            }
        }
        private List <NodeValue> GetSubTreeNode(int currentOpenSubTreeId)
        {
            List <NodeValue> nodeList = new List <NodeValue>();

            NodeValue subTreeNode = BehaviorDataController.Instance.GetNode(currentOpenSubTreeId);

            if (null == subTreeNode)
            {
                return(nodeList);
            }

            if (subTreeNode.subTreeType == (int)SUB_TREE_TYPE.NORMAL)
            {
                List <NodeValue> allNodeList = GetNodeList();
                for (int i = 0; i < allNodeList.Count; ++i)
                {
                    NodeValue nodeValue = allNodeList[i];
                    if (currentOpenSubTreeId >= 0 && nodeValue.parentSubTreeNodeId == currentOpenSubTreeId)
                    {
                        nodeList.Add(nodeValue);
                    }
                }
            }
            else if (subTreeNode.subTreeType == (int)SUB_TREE_TYPE.CONFIG)
            {
                ConfigFileLoad   configFileLoad = new ConfigFileLoad();
                BehaviorTreeData data           = configFileLoad.ReadFile(subTreeNode.subTreeConfig, false);
                if (null != data)
                {
                    nodeList.AddRange(data.nodeList);
                }
            }

            return(nodeList);
        }
Exemple #6
0
        public void Reset(BehaviorTreeData behaviorTreeData)
        {
            BehaviorAnalysis analysis = new BehaviorAnalysis();

            _iconditionCheck = new ConditionCheck();
            _rootNode        = analysis.Analysis(behaviorTreeData, ref _iconditionCheck);
        }
Exemple #7
0
        public BehaviorTreeData ReadJson(string filePath)
        {
            Debug.Log("Read:" + filePath);
            BehaviorTreeData behaviorData = new BehaviorTreeData();

            string content = FileReadWrite.Read(filePath);

            if (string.IsNullOrEmpty(content))
            {
                return(behaviorData);
            }

            JsonData jsonData = JsonMapper.ToObject(content);

            behaviorData.fileName = jsonData["fileName"].ToString();

            behaviorData.rootNodeId = int.Parse(jsonData["rootNodeId"].ToString());

            JsonData nodeList = jsonData["nodeList"];

            behaviorData.nodeList = GetNodeList(nodeList);

            JsonData parameterList = jsonData["parameterList"];

            behaviorData.parameterList = GetParameterList(parameterList);

            behaviorData.descript = jsonData["descript"].ToString();

            return(behaviorData);
        }
        public BehaviorTreeEntity(BehaviorTreeData data)
        {
            _iconditionCheck = new ConditionCheck();
            BehaviorAnalysis analysis = new BehaviorAnalysis();

            _rootNode = analysis.Analysis(data, _iconditionCheck, AddActionNode);
        }
        public static List <NodeValue> FindChild(BehaviorTreeData treeData, int nodeId)
        {
            List <NodeValue> nodeList = new List <NodeValue>();
            Queue <int>      queue    = new Queue <int>();

            queue.Enqueue(nodeId);
            while (queue.Count > 0)
            {
                int       id        = queue.Dequeue();
                NodeValue nodeValue = GetNode(treeData, id);
                if (null == nodeValue)
                {
                    continue;
                }

                if (id != nodeId)
                {
                    nodeList.Add(nodeValue);
                }

                foreach (var node in nodeValue.childNodeList)
                {
                    queue.Enqueue(node);
                }
            }

            return(nodeList);
        }
        public NodeBase Analysis(long aiFunction, BehaviorTreeData data, IConditionCheck iConditionCheck, Action <int> InvalidSubTreeCallBack)
        {
            int      entityId = NewEntityId;
            NodeBase rootNode = AnalysisTree(entityId, aiFunction, data, iConditionCheck, InvalidSubTreeCallBack);

            return(rootNode);
        }
Exemple #11
0
        public void DataDelGlobalParameter(NodeParameter parameter)
        {
            BehaviorTreeData     behaviorTreeData     = BehaviorDataController.Instance.BehaviorTreeData;
            DataParameterHandler dataParameterHandler = new DataParameterHandler();

            dataParameterHandler.DelParameter(behaviorTreeData.parameterList, parameter);
        }
        // 检测子节点是否包含圈
        private void CheckCircle(BehaviorTreeData treeData, int id)
        {
            HashSet <int> hash  = new HashSet <int>();
            Queue <int>   queue = new Queue <int>();

            queue.Enqueue(id);
            while (queue.Count > 0)
            {
                id = queue.Dequeue();
                NodeValue nodeValue = treeData.nodeList.Find((a) => {
                    return(a.id == id);
                });

                if (null == nodeValue || nodeValue.childNodeList.Count <= 0)
                {
                    continue;
                }

                foreach (var childId in nodeValue.childNodeList)
                {
                    if (hash.Contains(childId))
                    {
                        Debug.LogError(treeData.fileName + "   NodeValue:" + childId + "  Has multiple parent");
                        break;
                    }
                    else
                    {
                        hash.Add(childId);
                        queue.Enqueue(childId);
                    }
                }
            }
        }
Exemple #13
0
        private static void UpdateAllFile(string filePath)
        {
            //TableRead.Instance.Init();
            //string csvPath = string.Format("{0}/StreamingAssets/CSVAssets/", Application.dataPath); // Extend.GameUtils.CombinePath(Application.dataPath, "StreamingAssets", "CSV"); //string.Format("{0}/StreamingAssets/CSV/", Application.dataPath);
            //TableRead.Instance.ReadCustomPath(csvPath);


            DirectoryInfo dInfo = new DirectoryInfo(filePath);

            FileInfo[] fileInfoArr = dInfo.GetFiles("*.bytes", SearchOption.TopDirectoryOnly);
            for (int i = 0; i < fileInfoArr.Length; ++i)
            {
                string            fullName  = fileInfoArr[i].FullName;
                BehaviorReadWrite readWrite = new BehaviorReadWrite();
                BehaviorTreeData  treeData  = readWrite.ReadJson(fullName);

                if (null != BehaviorManager.behaviorStandardID)
                {
                    treeData = BehaviorManager.behaviorStandardID(treeData);
                }

                CheckChildID(treeData, treeData.rootNodeId);

                treeData = UpdateData(treeData);

                string jsonFilePath = System.IO.Path.GetDirectoryName(filePath) + "/Json/" + System.IO.Path.GetFileName(fullName);
                bool   value        = readWrite.WriteJson(treeData, jsonFilePath);
                if (!value)
                {
                    Debug.LogError("WriteError:" + jsonFilePath);
                }
            }
        }
        public static NodeValue GetNode(BehaviorTreeData treeData, int nodeId)
        {
            NodeValue nodeValue = treeData.nodeList.Find((a) => {
                return(a.id == nodeId);
            });

            return(nodeValue);
        }
Exemple #15
0
 public void Reset(BehaviorTreeData behaviorTreeData, BehaviorTreeEntity behaviorTreeEntity)
 {
     BehaviorManager.Instance.BehaviorTreeData = behaviorTreeData;
     _behaviorTreeEntity = behaviorTreeEntity;
     BehaviorTreeEntity.CurrentDebugEntityId = _behaviorTreeEntity.EntityId;
     SetRunTimeDrawNode(behaviorTreeEntity);
     BehaviorManager.behaviorRuntimePlay(BehaviorPlayType.PLAY);
     NodeNotify.Clear();
 }
 public void Reset(BehaviorTreeData behaviorTreeData, BehaviorTreeEntity behaviorTreeEntity)
 {
     BehaviorDataController.Instance.BehaviorTreeData = behaviorTreeData;
     _behaviorTreeEntity = behaviorTreeEntity;
     BehaviorTreeEntity.CurrentDebugEntityId = _behaviorTreeEntity.EntityId;
     SetRunTimeDrawNode(behaviorTreeEntity);
     BehaviorDataController.Instance.PlayState = BehaviorPlayType.PLAY;
     NodeNotify.Clear();
 }
Exemple #17
0
        private NodeBase AnalysisNode(int entityId, long aiFunction, BehaviorTreeData data, int nodeId, IConditionCheck iConditionCheck, Action <int> InvalidSubTreeCallBack)
        {
            NodeValue nodeValue = null;

            if (!data.nodeDic.TryGetValue(nodeId, out nodeValue))
            {
                return(null);
            }

            if (nodeValue.NodeType == (int)NODE_TYPE.SUB_TREE && nodeValue.subTreeValue > 0)
            {
                if ((aiFunction & nodeValue.subTreeValue) <= 0)
                {
                    InvalidSubTreeCallBack?.Invoke(nodeValue.id);
                    return(null);
                }
            }

            UnityEngine.Profiling.Profiler.BeginSample("AnalysisNode CreateNode");
            NodeBase nodeBase = AnalysisNode(nodeValue, iConditionCheck);

            nodeBase.NodeId   = nodeValue.id;
            nodeBase.EntityId = entityId;
            nodeBase.Priority = nodeValue.priority;
            UnityEngine.Profiling.Profiler.EndSample();

            if (nodeValue.NodeType == (int)NODE_TYPE.SUB_TREE && nodeValue.subTreeType == (int)SUB_TREE_TYPE.CONFIG)
            {
                BehaviorTreeData subTreeData = _loadConfigInfoEvent(nodeValue.subTreeConfig);
                if (null != subTreeData)
                {
                    NodeBase      subTreeNode = Analysis(entityId, subTreeData, iConditionCheck, InvalidSubTreeCallBack);
                    NodeComposite composite   = (NodeComposite)(nodeBase);
                    composite.AddNode(subTreeNode);
                    iConditionCheck.AddParameter(subTreeData.parameterList);
                }
            }

            UnityEngine.Profiling.Profiler.BeginSample("AnalysisNode IsLeafNode");
            if (!IsLeafNode(nodeValue.NodeType))
            {
                NodeComposite composite = (NodeComposite)nodeBase;
                for (int i = 0; i < nodeValue.childNodeList.Count; ++i)
                {
                    int      childNodeId = nodeValue.childNodeList[i];
                    NodeBase childNode   = AnalysisNode(entityId, aiFunction, data, childNodeId, iConditionCheck, InvalidSubTreeCallBack);
                    if (null != childNode)
                    {
                        composite.AddNode(childNode);
                    }
                }
            }
            UnityEngine.Profiling.Profiler.EndSample();

            return(nodeBase);
        }
Exemple #18
0
        public void Draw(BehaviorTreeData data)
        {
            Rect rect = GUILayoutUtility.GetRect(0f, 0, GUILayout.ExpandWidth(true));

            EditorGUILayout.BeginVertical("box");
            {
                data.descript = EditorGUILayout.TextArea(data.descript, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
            }
            EditorGUILayout.EndVertical();
        }
Exemple #19
0
        private void Init()
        {
            _behaviorTreeData = new BehaviorTreeData();
            _runTimeInvalidSubTreeHash.Clear();
            ConfigDataDic.Clear();
            _playState = BehaviorPlayType.STOP;
            _filePath  = CommonUtils.FileUtils.CombinePath(new string[] { "Assets", "SubAssets", "GameData", "BehaviorTree" });

            behaviorChangeSelectId += ChangeSelectId;
        }
 public BehaviorTreeEntity(long aiFunction, BehaviorTreeData data)
 {
     _iconditionCheck = new ConditionCheck();
     //UnityEngine.Profiling.Profiler.BeginSample("Analysis");
     _rootNode = BehaviorAnalysis.GetInstance().Analysis(aiFunction, data, _iconditionCheck, AddInvalidSubTree);
     //UnityEngine.Profiling.Profiler.EndSample();
     if (null != _rootNode)
     {
         _entityId = _rootNode.EntityId;
     }
 }
Exemple #21
0
        public NodeBase Analysis(string content, ref IConditionCheck iConditionCheck)
        {
            BehaviorTreeData behaviorTreeData = JsonMapper.ToObject <BehaviorTreeData>(content);

            if (null == behaviorTreeData)
            {
                Debug.LogError("behaviorTreeData is null");
                return(null);
            }
            return(Analysis(behaviorTreeData, ref iConditionCheck));
        }
        private BehaviorTreeData UpdateData(BehaviorTreeData treeData)
        {
            for (int i = 0; i < treeData.nodeList.Count; ++i)
            {
                NodeValue nodeValue = treeData.nodeList[i];
                if (nodeValue.identificationName.CompareTo("NodeIfJudge") == 0)
                {
                    nodeValue.identificationName = "NodeIfJudgeParallel";
                }
            }

            return(treeData);
        }
        public NodeBase Analysis(string content, IConditionCheck iConditionCheck, Action <NodeAction> ActionNodeCallBack)
        {
            BehaviorTreeData behaviorTreeData = JsonMapper.ToObject <BehaviorTreeData>(content);

            if (null == behaviorTreeData)
            {
                Debug.LogError("behaviorTreeData is null");
                return(null);
            }

            iConditionCheck.AddParameter(behaviorTreeData.parameterList);
            return(Analysis(behaviorTreeData, iConditionCheck, ActionNodeCallBack));
        }
        public void SaveSubTree(string subTreeConfigName, int subTreeNodeId)
        {
            UnityEngine.Debug.LogError("SaveSubTree:" + subTreeConfigName + "     " + subTreeNodeId);

            NodeValue subTreeNode = BehaviorDataController.Instance.GetNode(subTreeNodeId);

            if (null == subTreeNode || subTreeNode.NodeType != (int)NODE_TYPE.SUB_TREE)
            {
                return;
            }
            if (subTreeNode.subTreeType != (int)SUB_TREE_TYPE.NORMAL)
            {
                return;
            }

            BehaviorTreeData subTreeData = new BehaviorTreeData();

            subTreeData.fileName = subTreeConfigName;

            List <NodeValue> nodeList = BehaviorDataController.Instance.FindChild(BehaviorDataController.Instance.BehaviorTreeData, subTreeNodeId);

            List <NodeValue> newNodeList = new List <NodeValue>();

            for (int i = 0; i < nodeList.Count; ++i)
            {
                NodeValue childNode    = nodeList[i];
                NodeValue newNodeValue = childNode.Clone();

                if (newNodeValue.subTreeEntry)
                {
                    newNodeValue.isRootNode   = true;
                    newNodeValue.parentNodeID = -1;
                    subTreeData.rootNodeId    = newNodeValue.id;
                }
                newNodeValue.parentSubTreeNodeId = -1;

                subTreeData.nodeList.Add(newNodeValue);
            }

            for (int i = 0; i < BehaviorDataController.Instance.BehaviorTreeData.parameterList.Count; ++i)
            {
                NodeParameter parameter = BehaviorDataController.Instance.BehaviorTreeData.parameterList[i];
                subTreeData.parameterList.Add(parameter.Clone());
            }

            subTreeData = DataNodeIdStandardTool.StandardID(subTreeData);

            ConfigFileSave configFileSave = new ConfigFileSave();

            configFileSave.SaveFile(subTreeConfigName, subTreeData);
        }
Exemple #25
0
        public void SaveFile(string fileName, BehaviorTreeData data)
        {
            if (data == null)
            {
                UnityEngine.Debug.LogError("rootNode is null");
                return;
            }

            if (string.IsNullOrEmpty(fileName))
            {
                if (EditorUtility.DisplayDialog("警告", "文件名不能为空", "确定"))
                {
                    return;
                }
            }

            if (fileName.Length > 30)
            {
                if (EditorUtility.DisplayDialog("警告", "文件名过长,不能大于20个字符", "确定"))
                {
                    return;
                }
            }

            string path = BehaviorDataController.Instance.GetFilePath(fileName);

            if (File.Exists(path))
            {
                if (!EditorUtility.DisplayDialog("已存在文件", "是否替换新文件", "替换", "取消"))
                {
                    return;
                }
            }

            // 如果项目总不包含该路径,创建一个
            string directory = Path.GetDirectoryName(path);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            data.nodeDic.Clear();

            data          = DataNodeIdStandardTool.StandardID(data);
            data.fileName = fileName;

            BehaviorReadWrite readWrite = new BehaviorReadWrite();

            readWrite.WriteJson(data, path);
        }
        private BehaviorTreeData LoadConfig(string fileName)
        {
            ConfigFileLoad   configFileLoad   = new ConfigFileLoad();
            BehaviorTreeData behaviorTreeData = configFileLoad.ReadFile(fileName, false);

            behaviorTreeData.nodeDic.Clear();
            for (int i = 0; i < behaviorTreeData.nodeList.Count; ++i)
            {
                NodeValue nodeValue = behaviorTreeData.nodeList[i];
                behaviorTreeData.nodeDic.Add(nodeValue.id, nodeValue);
            }

            return(behaviorTreeData);
        }
Exemple #27
0
        public BehaviorTreeEntity(long aiFunction, BehaviorTreeData data, LoadConfigInfoEvent loadEvent)
        {
            _iconditionCheck = new ConditionCheck();
            BehaviorAnalysis analysis = new BehaviorAnalysis();

            analysis.SetLoadConfigEvent(loadEvent);
            UnityEngine.Profiling.Profiler.BeginSample("Analysis");
            _rootNode = analysis.Analysis(aiFunction, data, _iconditionCheck, AddInvalidSubTree);
            UnityEngine.Profiling.Profiler.EndSample();
            if (null != _rootNode)
            {
                _entityId = _rootNode.EntityId;
            }
        }
Exemple #28
0
        public void Reset(BehaviorTreeData behaviorTreeData)
        {
            behaviorTreeData.nodeDic.Clear();
            for (int i = 0; i < behaviorTreeData.nodeList.Count; ++i)
            {
                NodeValue nodeValue = behaviorTreeData.nodeList[i];
                behaviorTreeData.nodeDic.Add(nodeValue.id, nodeValue);
            }

            _behaviorTreeEntity = new BehaviorTreeEntity(long.MaxValue, behaviorTreeData, LoadConfig);
            BehaviorTreeEntity.CurrentDebugEntityId = _behaviorTreeEntity.EntityId;
            SetRunTimeDrawNode(_behaviorTreeEntity);
            NodeNotify.Clear();
        }
        public bool WriteJson(BehaviorTreeData data, string filePath)
        {
            string content = LitJson.JsonMapper.ToJson(data);
            bool   value   = FileReadWrite.Write(filePath, content);

            if (value)
            {
                Debug.Log("Write Sucess:" + filePath);
            }
            else
            {
                Debug.LogError("Write Fail:" + filePath);
            }

            return(value);
        }
        private NodeBase AnalysisTree(int entityId, long aiFunction, BehaviorTreeData data, IConditionCheck iConditionCheck, Action <int> InvalidSubTreeCallBack)
        {
            NodeBase rootNode = null;

            if (null == data || data.rootNodeId < 0)
            {
                //ProDebug.Logger.LogError("数据无效");
                return(rootNode);
            }

            iConditionCheck.AddParameter(data.parameterList);

            rootNode = AnalysisNode(entityId, aiFunction, data, data.rootNodeId, iConditionCheck, InvalidSubTreeCallBack);

            return(rootNode);
        }