private bool IsLeaderDead(EntityInfo entity)
        {
            bool           ret    = true;
            AiStateInfo    info   = entity.GetAiStateInfo();
            AiData_General data   = GetAiData(entity);
            EntityInfo     leader = AiLogicUtility.GetLivingCharacterInfoHelper(entity, info.LeaderID);

            if (null != leader)
            {
                ret = leader.IsDead();
            }
            return(ret);
        }
        private void SkillCommandHandler(EntityInfo entity, long deltaTime)
        {
            AiData_General data = GetAiData(entity);

            if (null != data)
            {
                AiLogicUtility.DoSkillCommandState(entity, deltaTime, this, data.ManualSkillId);
                if (data.ManualSkillId > 0)
                {
                    data.ManualSkillId = 0;
                }
            }
            else
            {
                ChangeToState(entity, (int)AiStateId.Idle);
            }
        }
        internal static void DoPursuitCommandState(EntityInfo entity, long deltaTime, AbstractAiStateLogic logic)
        {
            AiStateInfo info = entity.GetAiStateInfo();

            info.Time += deltaTime;
            if (info.Time > 200)
            {
                EntityInfo target = AiLogicUtility.GetLivingCharacterInfoHelper(entity, info.Target);
                if (null != target)
                {
                    float   minDist              = entity.GetRadius() + target.GetRadius();
                    float   dist                 = (float)entity.GetActualProperty().AttackRange + minDist;
                    float   distGoHome           = entity.GohomeRange;
                    Vector3 targetPos            = target.GetMovementStateInfo().GetPosition3D();
                    ScriptRuntime.Vector3 srcPos = entity.GetMovementStateInfo().GetPosition3D();
                    float dir = Geometry.GetYRadian(new Vector2(targetPos.X, targetPos.Z), new Vector2(srcPos.X, srcPos.Z));
                    targetPos.X += (float)(minDist * Math.Sin(dir));
                    targetPos.Z += (float)(minDist * Math.Cos(dir));
                    float powDist = Geometry.DistanceSquare(srcPos, targetPos);
                    if (powDist < dist * dist)
                    {
                        logic.AiSendStoryMessage(entity, "npc_pursuit_finish:" + entity.GetUnitId(), entity.GetId());
                        logic.AiSendStoryMessage(entity, "obj_pursuit_finish", entity.GetId());
                        logic.NotifyAiStopPursue(entity);
                        logic.ChangeToState(entity, (int)AiStateId.Idle);
                    }
                    else
                    {
                        logic.NotifyAiPursue(entity, targetPos);
                    }
                }
                else
                {
                    logic.AiSendStoryMessage(entity, "npc_pursuit_exit:" + entity.GetUnitId(), entity.GetId());
                    logic.AiSendStoryMessage(entity, "obj_pursuit_exit", entity.GetId());
                    logic.NotifyAiStopPursue(entity);
                    logic.ChangeToState(entity, (int)AiStateId.Idle);
                }
            }
        }
        private void IdleHandler(EntityInfo npc, long deltaTime)
        {
            AiData_Leader aiData = GetAiData(npc);
            SkillInfo     skInfo = AiLogicUtility.NpcFindCanUseSkill(npc, aiData, aiData.IsAutoOperate);

            if (skInfo == null)
            {
                return;
            }
            CharacterRelation relation =
                (skInfo.TargetType == SkillTargetType.Friend ||
                 skInfo.TargetType == SkillTargetType.RandFriend) ?
                CharacterRelation.RELATION_FRIEND :
                CharacterRelation.RELATION_ENEMY;

            EntityInfo attackTarget = AiLogicUtility.GetNearstTargetHelper(npc, aiData.IsAutoOperate ? npc.ViewRange : skInfo.Distance, relation);

            if (attackTarget != null)
            {
                ChangeToState(npc, (int)AiStateId.Combat);
            }
        }
        private void GoHomeHandler(EntityInfo entity, long deltaTime)
        {
            AiStateInfo info = entity.GetAiStateInfo();

            info.Time += deltaTime;
            if (info.Time > c_IntervalTime)
            {
                info.Time = 0;
                AiData_General data = GetAiData(entity);
                if (null != data)
                {
                    EntityInfo leader = AiLogicUtility.GetLivingCharacterInfoHelper(entity, info.LeaderID);
                    if (null != leader)
                    {
                        float   minDist              = entity.GetRadius() + leader.GetRadius();
                        Vector3 targetPos            = GetHomePos(entity.GetMovementStateInfo().FormationIndex, leader);
                        ScriptRuntime.Vector3 srcPos = entity.GetMovementStateInfo().GetPosition3D();
                        float powDistToHome          = Geometry.DistanceSquare(srcPos, targetPos);
                        if (powDistToHome <= (minDist + 1) * (minDist + 1))
                        {
                            NotifyAiStopPursue(entity);
                            ChangeToState(entity, (int)AiStateId.Idle);
                        }
                        else
                        {
                            NotifyAiPursue(entity, targetPos);
                        }
                    }
                    else
                    {
                        NotifyAiStopPursue(entity);
                        ChangeToState(entity, (int)AiStateId.Idle);
                    }
                }
            }
        }
 private void PatrolCommandHandler(EntityInfo entity, long deltaTime)
 {
     AiLogicUtility.DoPatrolCommandState(entity, deltaTime, this);
 }
        private void CombatHandler(EntityInfo npc, long deltaTime)
        {
            if (npc.GetSkillStateInfo().IsSkillActivated())
            {
                return;
            }

            AiStateInfo info          = npc.GetAiStateInfo();
            EntityInfo  leader        = AiLogicUtility.GetLivingCharacterInfoHelper(npc, info.LeaderID);
            bool        isAutoOperate = IsAutoOperate(leader);

            ScriptRuntime.Vector3 srcPos = npc.GetMovementStateInfo().GetPosition3D();
            Vector3 homePos = Vector3.Zero;

            if (null != leader)
            {
                GetHomePos(npc.GetMovementStateInfo().FormationIndex, leader);
            }
            float distSqrToHome = Geometry.DistanceSquare(srcPos, homePos);

            if (distSqrToHome > npc.GohomeRange * npc.GohomeRange)
            {
                NotifyAiStopPursue(npc);
                ChangeToState(npc, (int)AiStateId.GoHome);
                return;
            }

            ///
            EntityInfo     attackTarget = null;
            SkillStateInfo currSkInfo   = npc.GetSkillStateInfo();
            ///找到可以使用的技能
            SkillInfo skInfo = AiLogicUtility.NpcFindCanUseSkill(npc, this.GetAiData(npc), isAutoOperate);

            NotifyAiSelectSkill(npc, skInfo);
            if (skInfo == null)
            {
                //没有可以使用的技能就切换到Idle状态
                ChangeToState(npc, (int)AiStateId.Idle);
                return;
            }

            CharacterRelation relation =
                (skInfo.TargetType == SkillTargetType.Friend ||
                 skInfo.TargetType == SkillTargetType.RandFriend) ?
                CharacterRelation.RELATION_FRIEND :
                CharacterRelation.RELATION_ENEMY;

            attackTarget = AiLogicUtility.GetNearstTargetHelper(
                npc, skInfo.Distance, relation);

            if (attackTarget != null && null != skInfo) //攻击范围内找到可攻击目标
            {
                info.Target = attackTarget.GetId();
                NotifyAiStopPursue(npc);
                NotifyAiSkill(npc, skInfo.SkillId); //攻击目标
                return;
            }
            attackTarget = AiLogicUtility.GetNearstTargetHelper(
                npc, npc.ViewRange, relation);
            if (attackTarget != null && isAutoOperate)                                    //视野范围内找到可攻击目标
            {
                NotifyAiPursue(npc, attackTarget.GetMovementStateInfo().GetPosition3D()); // 追赶目标
                return;
            }

            currSkInfo.SetCurSkillInfo(0);
            NotifyAiStopPursue(npc);
            ChangeToState(npc, (int)AiStateId.GoHome);
        }
        internal static void DoPatrolCommandState(EntityInfo entity, long deltaTime, AbstractAiStateLogic logic)
        {
            AiStateInfo info = entity.GetAiStateInfo();

            info.Time += deltaTime;
            if (info.Time > 100)
            {
                info.Time = 0;
                EntityInfo target = null;
                if (info.IsExternalTarget)
                {
                    target = AiLogicUtility.GetSeeingLivingCharacterInfoHelper(entity, info.Target);
                    if (null == target)
                    {
                        target = AiLogicUtility.GetNearstTargetHelper(entity, CharacterRelation.RELATION_ENEMY);
                        if (null != target)
                        {
                            info.Target = target.GetId();
                        }
                    }
                }
                else
                {
                    target = AiLogicUtility.GetNearstTargetHelper(entity, CharacterRelation.RELATION_ENEMY);
                    if (null != target)
                    {
                        info.Target = target.GetId();
                    }
                }
                if (null != target)
                {
                    logic.AiSendStoryMessage(entity, "obj_patrol_exit", entity.GetId());
                    logic.AiSendStoryMessage(entity, string.Format("npc_patrol_exit:{0}", entity.GetUnitId()), entity.GetId());
                    logic.ChangeToState(entity, (int)AiStateId.Idle);
                }
                else
                {
                    AiData_ForPatrolCommand data = GetAiDataForPatrolCommand(entity);
                    if (null != data)
                    {
                        ScriptRuntime.Vector3 srcPos = entity.GetMovementStateInfo().GetPosition3D();
                        if (data.PatrolPath.HavePathPoint && !data.PatrolPath.IsReached(srcPos))
                        {
                            logic.NotifyAiPursue(entity, data.PatrolPath.CurPathPoint);
                        }
                        else
                        {
                            data.PatrolPath.UseNextPathPoint();
                            if (data.PatrolPath.HavePathPoint)
                            {
                                logic.NotifyAiPursue(entity, data.PatrolPath.CurPathPoint);
                            }
                            else
                            {
                                if (data.IsLoopPatrol)
                                {
                                    logic.AiSendStoryMessage(entity, "obj_patrol_restart", entity.GetId());
                                    logic.AiSendStoryMessage(entity, string.Format("npc_patrol_restart:{0}", entity.GetUnitId()), entity.GetId());
                                    data.PatrolPath.Restart();
                                }
                                else
                                {
                                    logic.AiSendStoryMessage(entity, "obj_patrol_finish", entity.GetId());
                                    logic.AiSendStoryMessage(entity, string.Format("npc_patrol_finish:{0}", entity.GetUnitId()), entity.GetId());
                                    logic.NotifyAiStopPursue(entity);
                                    logic.ChangeToState(entity, (int)AiStateId.Idle);
                                }
                            }
                        }
                        info.HomePos = entity.GetMovementStateInfo().GetPosition3D();
                    }
                    else
                    {
                        logic.NotifyAiStopPursue(entity);
                        logic.ChangeToState(entity, (int)AiStateId.Idle);
                    }
                }
            }
        }
        private void CombatHandler(EntityInfo npc, long deltaTime)
        {
            AiStateInfo   aiInfo = npc.GetAiStateInfo();
            AiData_Leader aiData = GetAiData(npc);

            if (npc.GetSkillStateInfo().IsSkillActivated())
            {
                return;
            }
            ///
            SkillStateInfo currSkInfo = npc.GetSkillStateInfo();
            ///找到可以使用的技能
            SkillInfo skInfo = AiLogicUtility.NpcFindCanUseSkill(npc, this.GetAiData(npc), aiData.IsAutoOperate);

            NotifyAiSelectSkill(npc, skInfo);
            if (skInfo == null)
            {
                //没有可以使用的技能就切换到Idle状态
                ChangeToState(npc, (int)AiStateId.Idle);
                return;
            }

            CharacterRelation relation =
                (skInfo.TargetType == SkillTargetType.Friend ||
                 skInfo.TargetType == SkillTargetType.RandFriend) ?
                CharacterRelation.RELATION_FRIEND :
                CharacterRelation.RELATION_ENEMY;
            EntityInfo attackTarget = AiLogicUtility.GetNearstAttackerHelper(npc, relation, aiData);

            if (null != attackTarget)
            {
                NotifyAiTarget(npc, attackTarget);
                if (Geometry.DistanceSquare(npc.GetMovementStateInfo().GetPosition3D(), attackTarget.GetMovementStateInfo().GetPosition3D()) < skInfo.Distance * skInfo.Distance)
                {
                    aiInfo.Target = attackTarget.GetId();
                    NotifyAiStopPursue(npc);
                    NotifyAiSkill(npc, skInfo.SkillId);
                    return;
                }
            }
            attackTarget = AiLogicUtility.GetNearstTargetHelper(npc, skInfo.Distance, relation);
            if (attackTarget != null && null != skInfo)   //攻击范围内找到可攻击目标
            {
                NotifyAiTarget(npc, attackTarget);
                aiInfo.Target = attackTarget.GetId();
                NotifyAiStopPursue(npc);
                NotifyAiSkill(npc, skInfo.SkillId); //攻击目标
                return;
            }
            if (aiData.IsAutoOperate)
            {
                attackTarget = AiLogicUtility.GetNearstTargetHelper(npc, npc.ViewRange, relation);
                if (attackTarget != null && null != skInfo)   //视野内找到可攻击目标
                {
                    NotifyAiPursue(npc, attackTarget.GetMovementStateInfo().GetPosition3D());
                    return;
                }
            }

            ///退出战斗模式清理一下手动技能
            currSkInfo.SetCurSkillInfo(0);
            aiData.ManualSkillId = 0;
            NotifyAiStopPursue(npc);
            ChangeToState(npc, (int)AiStateId.Idle);
        }