public MonsterRole FindNearestMonster(Vector3 pos, Func <MonsterRole, bool> handle = null, bool useMark = false, PK_TYPE pkState = PK_TYPE.PK_PEACE, bool onTask = false)
    {
        float dis = 9999999f;

        MonsterRole role = null, t_role = null;
        float       t_dis;

        RoleMgr.ClearMark(!useMark, pkState,
                          filterHandle: (m) => (m.m_curPhy.position - pos).magnitude < (SelfRole.fsm.Autofighting ? StateInit.Instance.Distance : SelfRole._inst.m_LockDis));
        foreach (MonsterRole m in m_mapMonster.Values)
        {
            if (m.isDead || m is CollectRole || (m is MS0000 && ((MS0000)m).owner_cid == PlayerModel.getInstance().cid))
            {
                continue;
            }
            if (m.issummon)
            {
                continue;
            }
            if (SelfRole.fsm.Autofighting && (m.m_curPhy.position - StateInit.Instance.Origin).magnitude > StateInit.Instance.Distance)
            {
                continue;
            }
            else
            {
                float _off_dis = (m.m_curPhy.position - pos).magnitude;
                if (_off_dis < (SelfRole.fsm.Autofighting ? Mathf.Min(SelfRole._inst.m_LockDis, StateInit.Instance.Distance) : SelfRole._inst.m_LockDis) &&
                    _off_dis < dis)
                {
                    t_dis  = _off_dis;
                    t_role = m;
                }
            }
            if (m is MDC000 && ((MDC000)m).escort_name == A3_LegionModel.getInstance().myLegion.clname)
            {
                continue;
            }
            if (m is MDC000 && (int)(((float)((MDC000)m).curhp / (float)((MDC000)m).maxHp) * 100) <= 20)
            {
                continue;
            }
            if ((taskMonId?.applied ?? false) && taskMonId.value != m.monsterid)
            {
                continue;
            }
            if (handle?.Invoke(m) ?? false)
            {
                continue;
            }
            if (onTask &&
                PlayerModel.getInstance().task_monsterIdOnAttack.ContainsKey(A3_TaskModel.getInstance().main_task_id) &&
                m.monsterid != PlayerModel.getInstance().task_monsterIdOnAttack[A3_TaskModel.getInstance().main_task_id])
            {
                continue;
            }
            if (TeamProxy.getInstance().MyTeamData != null)
            {
                if (A3_ActiveModel.getInstance().mwlr_on)
                {
                    if (A3_ActiveModel.getInstance().mwlr_target_monId != m.monsterid)
                    {
                        continue;
                    }
                }
                if (m.ownerName != null && !TeamProxy.getInstance().MyTeamData.IsInMyTeam(m.ownerName))
                {
                    continue;
                }
            }
            else if (m.ownerName != null && m.ownerName != PlayerModel.getInstance().name)
            {
                continue;
            }
            if (pkState != PK_TYPE.PK_PKALL && m is MS0000 && ((MS0000)m).owner_cid != 0)
            {
                continue;
            }
            float off_dis = (m.m_curPhy.position - pos).magnitude;
            if (off_dis < (SelfRole.fsm.Autofighting ? Mathf.Min(SelfRole._inst.m_LockDis, StateInit.Instance.Distance) : SelfRole._inst.m_LockDis) &&
                off_dis < dis)
            {
                dis  = off_dis;
                role = m;
            }
        }
        if (role != null && useMark)
        {
            role.m_isMarked = true;
        }
        else if (role == null)
        {
            role = t_role;
        }
        return(role);
    }
    public MonsterRole AddMonster(int id, Vector3 pos, uint serverid = 0, float roatate = 0, int boset_num = 0, int carr = 0, string name = null)
    {
        init();
        if (m_mapMonster.ContainsKey(serverid))
        {
            return(m_mapMonster[serverid]);
        }

        MonsterRole mon = null;
        //if (monID == 1)
        //{
        SXML  xml    = dMon[id];
        int   tempid = xml.getInt("obj");
        float scale  = xml.getFloat("scale");

        //用来给美术测试新加的怪物用的
        if (serverid <= 0)
        {
            if (Globle.m_nTestMonsterID > 0)
            {
                tempid = Globle.m_nTestMonsterID;
            }
        }

        bool isCollect = xml.getInt("collect_tar") > 0;

        bool isboxCollect = xml.getInt("collect_box") > 0;   // 判断这个宝箱是不是采集用的    > 0 则是采集的用宝箱

        //Type TCls;
        //if (Globle.isHardDemo)
        //    TCls = getTypeHandle("Md" + tempid);
        //else
        //{
        //    if (isCollect)
        //    {
        //        if (tempid == 122 && !isboxCollect)
        //        {
        //            TCls = System.Type.GetType("CollectBox");
        //        }
        //        else TCls = System.Type.GetType("CollectRole");
        //    }
        //    else
        //    {
        //        TCls = System.Type.GetType("M" + tempid);
        //    }
        //}
        //if (id == 4002 || carr == 2) TCls = System.Type.GetType("M000P2");
        //else if (id == 4003 || carr == 3) TCls = System.Type.GetType("M000P3");
        //else if (id == 4005 || carr == 5) TCls = System.Type.GetType("M000P5");

        //if (TCls == null)
        //{
        //    TCls = System.Type.GetType("M00000");
        //}

        //mon = (MonsterRole)Activator.CreateInstance(TCls);

        if (isCollect)
        {
            if (tempid == 122)
            {
                mon = new CollectBox();
            }
            else
            {
                mon = new CollectRole();
            }
        }
        if (id == 4002 || carr == 2)
        {
            mon = new M000P2();
        }
        else if (id == 4003 || carr == 3)
        {
            mon = new M000P3();
        }
        else if (id == 4005 || carr == 5)
        {
            mon = new M000P5();
        }
        if (mon == null)
        {
            mon = new M00000();
        }


        if (name != null)
        {
            mon.ownerName = name;
        }
        if (carr == 0)
        {
            mon.roleName = xml.getString("name");
        }
        mon.tempXMl = xml;

        mon.m_circle_type = xml.getInt("boss_circle");
        if (xml.getFloat("boss_circle_scale") == -1)
        {
            mon.m_circle_scale = 1;
        }
        else
        {
            mon.m_circle_scale = xml.getFloat("boss_circle_scale");
        }

        mon.isBoos   = xml.getInt("boss") == 1;
        mon.isBoss_c = xml.getInt("boss_c") == 1;
        if (scale > 0f)
        {
            mon.scale = scale;
        }

        if (serverid > 0)
        {
            mon.m_unIID = serverid;
            m_mapMonster.Add(serverid, mon);
        }
        else
        {
            mon.isfake  = true;
            mon.m_unIID = idIdx;
            m_mapFakeMonster.Add(idIdx, mon);

            idIdx++;
        }

        if (mon != null)
        {
            if (id == 4002 || carr == 2)
            {
                mon.Init("profession_warrior_inst", EnumLayer.LM_MONSTER, pos, roatate);
            }
            else if (id == 4003 || carr == 3)
            {
                mon.Init("profession_mage_inst", EnumLayer.LM_MONSTER, pos, roatate);
            }
            else if (id == 4005 || carr == 5)
            {
                mon.Init("profession_assa_inst", EnumLayer.LM_MONSTER, pos, roatate);
            }
            else if (isCollect)
            {
                mon.Init("npc_" + tempid, EnumLayer.LM_MONSTER, pos, roatate);
            }
            else
            {
                mon.Init("monster_" + tempid, EnumLayer.LM_MONSTER, pos, roatate);
            }
            mon.monsterid = id;

            if (boset_num > 0)
            {
                PlayerNameUIMgr.getInstance().show(mon);
                PlayerNameUIMgr.getInstance().seticon_forDaobao(mon, boset_num);
            }

            if (A3_ActiveModel.getInstance().mwlr_map_info.Count > 0)
            {
                PlayerNameUIMgr.getInstance().seticon_forMonsterHunter(mon, A3_ActiveModel.getInstance().mwlr_map_info[0]["target_mid"] != id);
            }


            //if (GRMap.grmap_loading == false)
            //    mon.refreshViewType(2);
        }


        m_listMonster.Add(mon);


        //}
        //else if (monID == 2)
        //{
        //    mon = new M10002Srg();

        //    mon.Init("monster/10002", EnumLayer.LM_MONSTER, bornpt);
        //    m_mapMonster.Add(idIdx, mon);
        //}
        //else if (monID == 3)
        //{
        //    mon = new M10003Stl();

        //    mon.Init("monster/10003", EnumLayer.LM_MONSTER, bornpt);
        //    m_mapMonster.Add(idIdx, mon);
        //}
        //if (mon != null)
        //{
        //    MonsterData dta = new MonsterData();
        //    dta.roleId = idIdx;
        //    dta.monid = monID;
        //    mon.monsterDta = dta;
        //}

        //idIdx++;

        if (mon != null)
        {
            dispatchEvent(GameEvent.Create(MonsterMgr.EVENT_MONSTER_ADD, this, mon));
        }

        return(mon);
    }