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)
                {
                    Vector3 targetPos            = info.HomePos;
                    ScriptRuntime.Vector3 srcPos = entity.GetMovementStateInfo().GetPosition3D();
                    float powDistToHome          = Geometry.DistanceSquare(srcPos, info.HomePos);
                    if (powDistToHome <= 1)
                    {
                        NotifyAiStopPursue(entity);
                        ChangeToState(entity, (int)AiStateId.Idle);
                    }
                    else
                    {
                        NotifyAiPursue(entity, targetPos);
                    }
                }
            }
        }
        internal static void Execute(object msg, User user)
        {
            Msg_CR_Skill use_skill = msg as Msg_CR_Skill;

            if (null == use_skill)
            {
                return;
            }
            EntityInfo userObj = user.Info;

            if (null == userObj)
            {
                LogSys.Log(LOG_TYPE.DEBUG, "UseSkillHandler, charactor {0}({1},{2},{3}) not exist", user.RoleId, user.GetKey(), user.Guid, user.Name);
                return;
            }
            Scene scene = user.OwnRoom.ActiveScene;

            if (null != scene)
            {
                EntityInfo obj = scene.GetEntityById(use_skill.role_id);
                if (null != obj)
                {
                    AiStateInfo aiInfo = obj.GetAiStateInfo();
                    if (use_skill.target_id > 0)
                    {
                        aiInfo.Target = use_skill.target_id;
                    }
                    else if (use_skill.target_dir > 0)
                    {
                        float dir = ProtoHelper.DecodeFloat(use_skill.target_dir);
                        obj.GetMovementStateInfo().SetFaceDir(dir);
                        aiInfo.Target = 0;
                    }
                    if (aiInfo.AiLogic == (int)AiStateLogicId.Entity_Leader)
                    {
                        AiData_Leader data = aiInfo.AiDatas.GetData <AiData_Leader>();
                        if (null == data)
                        {
                            data = new AiData_Leader();
                            aiInfo.AiDatas.AddData(data);
                        }
                        data.ManualSkillId = use_skill.skill_id;
                    }
                    else
                    {
                        AiData_General data = aiInfo.AiDatas.GetData <AiData_General>();
                        if (null == data)
                        {
                            data = new AiData_General();
                            aiInfo.AiDatas.AddData(data);
                        }
                        data.ManualSkillId = use_skill.skill_id;
                    }
                    aiInfo.ChangeToState((int)AiStateId.SkillCommand);
                }
            }
        }
 private AiData_General GetAiData(EntityInfo entity)
 {
     AiData_General data = entity.GetAiStateInfo().AiDatas.GetData<AiData_General>();
     if (null == data) {
         data = new AiData_General();
         entity.GetAiStateInfo().AiDatas.AddData(data);
     }
     return data;
 }
        public bool CastSkill(int objId, int skillId)
        {
            bool       ret = false;
            EntityInfo obj = GetEntityById(objId);

            if (null != obj)
            {
                SkillInfo skillInfo = obj.GetSkillStateInfo().GetSkillInfoById(skillId);
                if (null != skillInfo)
                {
                    if (obj.Energy >= obj.GetActualProperty().EnergyMax)
                    {
                        if (!skillInfo.IsInCd(TimeUtility.GetLocalMilliseconds()))
                        {
                            int targetId = 0;
                            if (null != SelectedTarget)
                            {
                                targetId = SelectedTarget.TargetId;
                            }
                            if (IsRoomScene)
                            {
                                Network.NetworkSystem.Instance.SyncPlayerSkill(obj, skillId, targetId, obj.GetMovementStateInfo().GetFaceDir());
                            }
                            else
                            {
                                AiStateInfo aiInfo = obj.GetAiStateInfo();
                                aiInfo.Target = targetId;
                                if (aiInfo.AiLogic == (int)AiStateLogicId.Entity_Leader)
                                {
                                    AiData_Leader data = aiInfo.AiDatas.GetData <AiData_Leader>();
                                    if (null == data)
                                    {
                                        data = new AiData_Leader();
                                        aiInfo.AiDatas.AddData(data);
                                    }
                                    data.ManualSkillId = skillId;
                                }
                                else
                                {
                                    AiData_General data = aiInfo.AiDatas.GetData <AiData_General>();
                                    if (null == data)
                                    {
                                        data = new AiData_General();
                                        aiInfo.AiDatas.AddData(data);
                                    }
                                    data.ManualSkillId = skillId;
                                }
                                aiInfo.ChangeToState((int)AiStateId.SkillCommand);
                            }
                            ret = true;
                        }
                    }
                }
            }
            return(ret);
        }
        private AiData_General GetAiData(EntityInfo entity)
        {
            AiData_General data = entity.GetAiStateInfo().AiDatas.GetData <AiData_General>();

            if (null == data)
            {
                data = new AiData_General();
                entity.GetAiStateInfo().AiDatas.AddData(data);
            }
            return(data);
        }
        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);
        }
        internal static SkillInfo NpcFindCanUseSkill(EntityInfo npc, AiData_General aidata, bool includeManualSkill)
        {
            SkillInfo      selectSkill = null;
            SkillStateInfo skStateInfo = npc.GetSkillStateInfo();
            int            priority    = -1;
            SkillInfo      skInfo      = null;
            long           curTime     = TimeUtility.GetLocalMilliseconds();

            if (includeManualSkill && npc.ManualSkillId > 0)
            {
                skInfo = skStateInfo.GetSkillInfoById(npc.ManualSkillId);
                if (null != skInfo && !skInfo.IsInCd(curTime))
                {
                    selectSkill = skInfo;
                }
            }
            if (null == selectSkill)
            {
                if (npc.AutoSkillIds.Count <= 0)
                {
                    return(null);
                }
                int randIndex = Helper.Random.Next(0, npc.AutoSkillIds.Count);
                skInfo = skStateInfo.GetSkillInfoById(npc.AutoSkillIds[randIndex]);
                if (null != skInfo && !skInfo.IsInCd(curTime))
                {
                    selectSkill = skInfo;
                }
                else
                {
                    for (int i = 0; i < npc.AutoSkillIds.Count; i++)
                    {
                        skInfo = skStateInfo.GetSkillInfoById(npc.AutoSkillIds[i]);
                        if (null != skInfo && !skInfo.IsInCd(curTime) && skInfo.InterruptPriority > priority)
                        {
                            selectSkill = skInfo;
                            priority    = skInfo.InterruptPriority;
                        }
                    }
                }
            }
            if (null != selectSkill)
            {
                aidata.LastUseSkillTime = TimeUtility.GetLocalMilliseconds();
            }
            return(selectSkill);
        }
        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);
            }
        }
 public bool CastSkill(int objId, int skillId)
 {
     bool ret = false;
     EntityInfo obj = GetEntityById(objId);
     if (null != obj) {
         SkillInfo skillInfo = obj.GetSkillStateInfo().GetSkillInfoById(skillId);
         if (null != skillInfo) {
             if (obj.Energy >= obj.GetActualProperty().EnergyMax) {
                 if (!skillInfo.IsInCd(TimeUtility.GetLocalMilliseconds())) {
                     int targetId = 0;
                     if (null != SelectedTarget) {
                         targetId = SelectedTarget.TargetId;
                     }
                     if (IsRoomScene) {
                         Network.NetworkSystem.Instance.SyncPlayerSkill(obj, skillId, targetId, obj.GetMovementStateInfo().GetFaceDir());
                     } else {
                         AiStateInfo aiInfo = obj.GetAiStateInfo();
                         aiInfo.Target = targetId;
                         if (aiInfo.AiLogic == (int)AiStateLogicId.Entity_Leader) {
                             AiData_Leader data = aiInfo.AiDatas.GetData<AiData_Leader>();
                             if (null == data) {
                                 data = new AiData_Leader();
                                 aiInfo.AiDatas.AddData(data);
                             }
                             data.ManualSkillId = skillId;
                         } else {
                             AiData_General data = aiInfo.AiDatas.GetData<AiData_General>();
                             if (null == data) {
                                 data = new AiData_General();
                                 aiInfo.AiDatas.AddData(data);
                             }
                             data.ManualSkillId = skillId;
                         }
                         aiInfo.ChangeToState((int)AiStateId.SkillCommand);
                     }
                     ret = true;
                 }
             }
         }
     }
     return ret;
 }
 public static EntityInfo GetNearstAttackerHelper(EntityInfo srcObj, CharacterRelation relation, AiData_General aidata)
 {
     EntityInfo ret = null;
     float minDistSqr = 999999;
     Vector2 dir = srcObj.GetMovementStateInfo().GetFaceDir2D();
     Vector2 pos = srcObj.GetMovementStateInfo().GetPosition2D();
     if (relation == CharacterRelation.RELATION_ENEMY) {
         foreach (var pair in srcObj.AttackerInfos) {
             EntityInfo target = srcObj.SceneContext.GetEntityById(pair.Key);
             if (null != target) {
                 float distSqr = Geometry.DistanceSquare(pos, target.GetMovementStateInfo().GetPosition2D());
                 if (distSqr <= (srcObj.ViewRange + target.GetRadius()) * (srcObj.ViewRange + target.GetRadius())) {
                     if (distSqr <= minDistSqr) {
                         ret = target;
                         minDistSqr = distSqr;
                     }
                 }
             }
         }
     }
     return ret;
 }
        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);
                    }
                }
            }
        }
 internal static SkillInfo NpcFindCanUseSkill(EntityInfo npc, AiData_General aidata, bool includeManualSkill)
 {
     SkillInfo selectSkill = null;
     SkillStateInfo skStateInfo = npc.GetSkillStateInfo();
     int priority = -1;
     SkillInfo skInfo = null;
     long curTime = TimeUtility.GetLocalMilliseconds();
     if (includeManualSkill && npc.ManualSkillId > 0) {
         skInfo = skStateInfo.GetSkillInfoById(npc.ManualSkillId);
         if (null != skInfo && !skInfo.IsInCd(curTime)) {
             selectSkill = skInfo;
         }
     }
     if (null == selectSkill) {
         if (npc.AutoSkillIds.Count <= 0)
             return null;
         int randIndex = Helper.Random.Next(0, npc.AutoSkillIds.Count);
         skInfo = skStateInfo.GetSkillInfoById(npc.AutoSkillIds[randIndex]);
         if (null != skInfo && !skInfo.IsInCd(curTime)) {
             selectSkill = skInfo;
         } else {
             for (int i = 0; i < npc.AutoSkillIds.Count; i++) {
                 skInfo = skStateInfo.GetSkillInfoById(npc.AutoSkillIds[i]);
                 if (null != skInfo && !skInfo.IsInCd(curTime) && skInfo.InterruptPriority > priority) {
                     selectSkill = skInfo;
                     priority = skInfo.InterruptPriority;
                 }
             }
         }
     }
     if (null != selectSkill) {
         aidata.LastUseSkillTime = TimeUtility.GetLocalMilliseconds();
     }
     return selectSkill;
 }
        public static EntityInfo GetNearstAttackerHelper(EntityInfo srcObj, CharacterRelation relation, AiData_General aidata)
        {
            EntityInfo ret        = null;
            float      minDistSqr = 999999;
            Vector2    dir        = srcObj.GetMovementStateInfo().GetFaceDir2D();
            Vector2    pos        = srcObj.GetMovementStateInfo().GetPosition2D();

            if (relation == CharacterRelation.RELATION_ENEMY)
            {
                foreach (var pair in srcObj.AttackerInfos)
                {
                    EntityInfo target = srcObj.SceneContext.GetEntityById(pair.Key);
                    if (null != target)
                    {
                        float distSqr = Geometry.DistanceSquare(pos, target.GetMovementStateInfo().GetPosition2D());
                        if (distSqr <= (srcObj.ViewRange + target.GetRadius()) * (srcObj.ViewRange + target.GetRadius()))
                        {
                            if (distSqr <= minDistSqr)
                            {
                                ret        = target;
                                minDistSqr = distSqr;
                            }
                        }
                    }
                }
            }
            return(ret);
        }
 internal static void Execute(object msg, User user)
 {
     Msg_CR_Skill use_skill = msg as Msg_CR_Skill;
     if (null == use_skill)
         return;
     EntityInfo userObj = user.Info;
     if (null == userObj) {
         LogSys.Log(LOG_TYPE.DEBUG, "UseSkillHandler, charactor {0}({1},{2},{3}) not exist", user.RoleId, user.GetKey(), user.Guid, user.Name);
         return;
     }
     Scene scene = user.OwnRoom.ActiveScene;
     if (null != scene) {
         EntityInfo obj = scene.GetEntityById(use_skill.role_id);
         if (null != obj) {
             AiStateInfo aiInfo = obj.GetAiStateInfo();
             if (use_skill.target_id > 0) {
                 aiInfo.Target = use_skill.target_id;
             } else if (use_skill.target_dir > 0) {
                 float dir = ProtoHelper.DecodeFloat(use_skill.target_dir);
                 obj.GetMovementStateInfo().SetFaceDir(dir);
                 aiInfo.Target = 0;
             }
             if (aiInfo.AiLogic == (int)AiStateLogicId.Entity_Leader) {
                 AiData_Leader data = aiInfo.AiDatas.GetData<AiData_Leader>();
                 if (null == data) {
                     data = new AiData_Leader();
                     aiInfo.AiDatas.AddData(data);
                 }
                 data.ManualSkillId = use_skill.skill_id;
             } else {
                 AiData_General data = aiInfo.AiDatas.GetData<AiData_General>();
                 if (null == data) {
                     data = new AiData_General();
                     aiInfo.AiDatas.AddData(data);
                 }
                 data.ManualSkillId = use_skill.skill_id;
             }
             aiInfo.ChangeToState((int)AiStateId.SkillCommand);
         }
     }
 }