Example #1
0
 private void GenerateRandomMap()
 {
     mTimer.Begin(TIMER_KEY_CREATE_MAP);
     mCurrentSeed = RandomMapCreator.Instance.TryToCreate(Probability, RandomSeed, MaxTimes);
     mTimer.End(TIMER_KEY_CREATE_MAP);
     HTLogger.Debug(string.Format("生成随机地图耗时:{0}毫秒", mTimer.GetSumTime(TIMER_KEY_CREATE_MAP)));
     HTLogger.Debug("随机种子:" + mCurrentSeed);
     SpawnMap();
 }
        protected override BTNodeStatus OnExcute(IAgent agent, Blackboard bb)
        {
            AIEntity entity = (AIEntity)agent;

            bb.SetValue(BlackboardKey.TargetMovePos, entity.Company.position);
            entity.Model.Thinking.Value = mThinkingArray[UnityEngine.Random.Range(0, mThinkingArray.Length)];
            HTLogger.Debug("感觉贫穷,设置目标地点:Company,坐标:" + entity.Company.position);
            return(BTNodeStatus.Finished);
        }
Example #3
0
        public void Save <T>(T data, string path)
        {
            string json = JsonUtility.ToJson(data);

            File.WriteAllText(path, json);
#if UNITY_EDITOR
            UnityEditor.AssetDatabase.Refresh();
#endif
            HTLogger.Debug("Save map done.");
        }
Example #4
0
        public T Load <T>(string path)
        {
            string json = File.ReadAllText(path);
            T      data = JsonUtility.FromJson <T>(json);

            if (data == null)
            {
                HTLogger.Debug("Load map fail.");
            }
            HTLogger.Debug("Load map done.");
            return(data);
        }
Example #5
0
        protected override BTNodeStatus OnExcute(IAgent agent, Blackboard bb)
        {
            AIEntity entity = (AIEntity)agent;
            Vector3  target = bb.GetValue(BlackboardKey.TargetMovePos, Vector3.one);

            entity.Move(target);
            if (entity.AlreadyReachTarget(target, 1.0f))
            {
                HTLogger.Debug("已经到达目标点:" + target);
                return(BTNodeStatus.Finished);
            }
            //HTLogger.Debug("持续向目标点:" + target + "移动中");
            return(BTNodeStatus.Running);
        }
Example #6
0
        public void LoadRunningData(int key)
        {
            RunningData data = mSaveMgr.Load <RunningData>(mSaveMgr.PackageSavePath(key + ".json"));

            if (mDataDict.ContainsKey(key))
            {
                mDataDict[key] = data;
            }
            else
            {
                mDataDict.Add(key, data);
            }
            HTLogger.Debug("Load RunningData done.");
        }
Example #7
0
        public void SaveRunningData(int key)
        {
            RunningData data;

            if (mDataDict.TryGetValue(key, out data) && data != null)
            {
                mSaveMgr.Save(data, mSaveMgr.PackageSavePath(key + ".json"));
                HTLogger.Debug("Save RunningData done.");
            }
            else
            {
                HTLogger.Debug("Save RunningData fail.");
            }
        }
Example #8
0
        protected override BTNodeStatus OnExcute(IAgent agent, Blackboard bb)
        {
            AIEntity entity = (AIEntity)agent;
            Vector3  target = bb.GetValue(BlackboardKey.TargetMovePos, Vector3.zero);

            entity.Turn(target);
            if (entity.AlreadyTurnToTarget(target, 0.1f))
            {
                HTLogger.Debug("已经转向目标点:" + target + "方向");
                return(BTNodeStatus.Finished);
            }
            HTLogger.Debug("持续向目标点:" + target + "方向转向中");
            return(BTNodeStatus.Running);
        }
Example #9
0
        protected override BTNodeStatus OnExcute(IAgent agent, Blackboard bb)
        {
            AIModel model = ((AIEntity)agent).Model;

            if (CheckOver(model))
            {
                HTLogger.Debug("退出吃东西");
                return(BTNodeStatus.Finished);
            }
            model.Food.Value   += BTExampleConsts.FOOD_TO_FOOD * Time.deltaTime;
            model.Water.Value  += BTExampleConsts.FOOD_TO_WATER * Time.deltaTime;
            model.Energy.Value += BTExampleConsts.FOOD_TO_ENERGY * Time.deltaTime;
            model.Mood.Value   += BTExampleConsts.FOOD_TO_MOOD * Time.deltaTime;
            model.Money.Value  += BTExampleConsts.FOOD_TO_MONEY * Time.deltaTime;
            return(BTNodeStatus.Running);
        }
Example #10
0
        private void ClickPathFindingBtn(int times, int count, int mapNum)
        {
            List <float> resTimesIn  = new List <float>();
            List <float> resTimesOut = new List <float>();
            Vector2      start       = new Vector2(0, 0);
            Vector2      end         = new Vector2(mMapMgr.Width - 1, mMapMgr.Height - 1);
            //mMapMgr.BuildMap();
            List <int> seedList = mRandomMapCreator.GetValidSeedList(mapNum, Probability, RandomSeed, MaxTimes);

            for (int p = 0; p < seedList.Count; p++)
            {
                mRandomMapCreator.Create(seedList[p], Probability);
                mMapMgr.BuildMap();
                for (int j = 0; j < count; j++)
                {
                    mTimer.Begin(TIMER_KEY_ASTAR_PF);
                    for (int i = 0; i < times; i++)
                    {
                        mAStarMgr.FindPath(start, end);
                    }
                    mTimer.End(TIMER_KEY_ASTAR_PF);
                    resTimesIn.Add(mTimer.GetSumTime(TIMER_KEY_ASTAR_PF));
                }
                resTimesIn.Sort();
                float sumTimeIn = 0.0f;
                for (int i = 1; i <= resTimesIn.Count - 2; i++)
                {
                    sumTimeIn += resTimesIn[i];
                }
                resTimesOut.Add((sumTimeIn / (resTimesIn.Count - 2)) / times);
            }
            resTimesOut.Sort();
            float sumTimeOut = 0.0f;

            for (int i = 1; i <= resTimesOut.Count - 2; i++)
            {
                sumTimeOut += resTimesOut[i];
            }
            mFindPathTime = sumTimeOut / (resTimesOut.Count - 2);
            mPath         = mAStarMgr.FindPath(start, end);
            //mFindPathTime = mTimer.GetSumTime(TIMER_KEY_ASTAR_PF);
            mFindPathCost = CalculatePathConsume(mPath);
            SpawnMap();
            HTLogger.Debug(string.Format("寻路 {0} 次:{1}毫秒", times, mFindPathTime * times));
            HTLogger.Debug(string.Format("寻路 1 次:{0}毫秒", mFindPathTime));
            HTLogger.Debug("路径消耗:" + mFindPathCost);
        }
Example #11
0
        protected override bool InternalCondition(IAgent agent, Blackboard bb)
        {
            AIModel model = ((AIEntity)agent).Model;
            bool    ret   = model.Food.Value <= 50 &&
                            model.Water.Value >= 15 &&
                            model.Money.Value >= 20;

            if (ret)
            {
                HTLogger.Debug("通过food检测");
            }
            else
            {
                HTLogger.Debug("未通过food检测");
            }
            return(ret);
        }
Example #12
0
        private void Start()
        {
            UnityDebugListener   unityDebugListener   = new UnityDebugListener();
            UnityWarningListener unityWarningListener = new UnityWarningListener();
            UnityErrorListener   unityErrorListener   = new UnityErrorListener();

            HTLogger.Instance.Init(true);
            HTLogger.Instance.AddListener(HTLogger.Channel.Info, unityDebugListener);
            HTLogger.Instance.AddListener(HTLogger.Channel.Todo, unityDebugListener);
            HTLogger.Instance.AddListener(HTLogger.Channel.Debug, unityDebugListener);
            HTLogger.Instance.AddListener(HTLogger.Channel.Warning, unityWarningListener);
            HTLogger.Instance.AddListener(HTLogger.Channel.Error, unityErrorListener);


            HTLogger.Info("测试 Logger 成功!");
            HTLogger.Todo("测试 Logger 成功!");
            HTLogger.Debug("测试 Logger 成功!");
            HTLogger.Warning("测试 Logger 成功!");
            HTLogger.Error("测试 Logger 成功!");
        }
Example #13
0
 private void SpawnMap()
 {
     mNodeTransArray = new Transform[mMapMgr.Width, mMapMgr.Height];
     for (int i = 0; i < mMapMgr.Width; i++)
     {
         for (int j = 0; j < mMapMgr.Height; j++)
         {
             Node node = mMapMgr.Map[i, j];
             mNodeTransArray[i, j] = SpawnNode(node).transform;
             if (node.IsConnected)
             {
                 SetColor(node, NormalColor);
             }
             else
             {
                 SetColor(node, ObstacleColor);
             }
         }
     }
     SetColor(mMapMgr.Map[0, 0], StartColor);
     SetColor(mMapMgr.Map[mMapMgr.Width - 1, mMapMgr.Height - 1], EndColor);
     HTLogger.Debug("实例化地图成功");
 }
Example #14
0
        /// <summary>
        /// 尝试创建随机 Map
        /// </summary>
        /// <param 障碍物生成概率(0,1)="probability"></param>
        /// <param 起始随机种子>-1="startSeed"></param>
        /// <param 尝试最大次数="times"></param>
        /// <returns></returns>
        public int TryToCreate(float probability, int startSeed, int times)
        {
            Node[,] mapTemp = new Node[mMapMgr.Width, mMapMgr.Height];
            mapTemp         = CopyMap();
            int count = 0;

            while (count < times)
            {
                Create(startSeed, probability);
                if (CheckMapIsConnected())
                {
                    //HTLogger.Debug("随机地图生成成功");
                    return(startSeed);
                }
                else
                {
                    RecoverMap(mapTemp);
                }
                startSeed++;
                count++;
            }
            HTLogger.Debug("随机地图生成失败");
            return(-1);
        }
Example #15
0
 protected override bool InternalCondition(IAgent agent, Blackboard bb)
 {
     HTLogger.Debug("通过money检测");
     return(true);
 }
Example #16
0
        private void SimpleAStarPathFinding()
        {
            SimpleAStar.Point start = new SimpleAStar.Point(0, 0);
            SimpleAStar.Point end   = new SimpleAStar.Point(MapWidth - 1, MapHeight - 1);

            List <float> resTimeListIn  = new List <float>();
            List <float> resTimeListOut = new List <float>();
            List <int>   seedList       = mRandomMapCreator.GetValidSeedList(MapNums, Probability, RandomSeed, MaxTimes);

            for (int p = 0; p < seedList.Count; p++)
            {
                mRandomMapCreator.Create(seedList[p], Probability);
                List <SimpleAStar.Point> obstacleList = new List <SimpleAStar.Point>();
                for (int i = 0; i < MapWidth; i++)
                {
                    for (int j = 0; j < MapHeight; j++)
                    {
                        if (mMapMgr.Map[i, j].IsConnected == false)
                        {
                            obstacleList.Add(new SimpleAStar.Point(i, j));
                        }
                    }
                }
                SimpleAStar.AStarS aStars = new SimpleAStar.AStarS(MapWidth, MapHeight, obstacleList);
                for (int j = 0; j < Count; j++)
                {
                    mTimer.Begin(TIMER_KEY_ASTAR_PF);
                    for (int i = 0; i < LoopTimes; i++)
                    {
                        aStars.FindPath(start, end);
                    }
                    mTimer.End(TIMER_KEY_ASTAR_PF);
                    resTimeListIn.Add(mTimer.GetSumTime(TIMER_KEY_ASTAR_PF));
                }
                resTimeListIn.Sort();
                float sumTimeIn = 0.0f;
                for (int i = 1; i <= resTimeListIn.Count - 2; i++)
                {
                    sumTimeIn += resTimeListIn[i];
                }
                resTimeListOut.Add((sumTimeIn / (resTimeListIn.Count - 2)) / LoopTimes);
                mSimplePath = aStars.FindPath(start, end);
            }
            resTimeListOut.Sort();
            float sumTimeOut = 0.0f;

            for (int i = 1; i <= resTimeListOut.Count - 2; i++)
            {
                sumTimeOut += resTimeListOut[i];
            }
            float time = sumTimeOut / (resTimeListOut.Count - 2);

            List <Node> pathNodeList = new List <Node>();

            for (int i = 0; i < mSimplePath.Count; i++)
            {
                SimpleAStar.Point point = mSimplePath[i];
                pathNodeList.Add(new Node(point.X, point.Y));
            }
            int cost = CalculatePathConsume(pathNodeList);

            //float time = mTimer.GetSumTime(TIMER_KEY_ASTAR_PF);
            //SpawnMap();
            HTLogger.Debug(string.Format("(简易 AStar)寻路 {0} 次:{1}毫秒", LoopTimes, time * LoopTimes));
            HTLogger.Debug(string.Format("(简易 AStar)寻路 1 次:{0}毫秒", time));
            HTLogger.Debug("(简易 AStar)路径消耗:" + cost);
        }