//public void InitData(object sender, PeCreature.MainPlayerCreatedArg arg)
    //{
    //    if (PeCreature.Instance.mainPlayer != null)
    //    {
    //        playerTrans = PeCreature.Instance.mainPlayer.peTrans;
    //        born_pos = playerTrans.position;
    //        Debug.LogError("born_pos" + born_pos);
    //        start_pos = born_pos;
    //        last_pos = start_pos;
    //        last_time = timeCounter;

    //    }
    //}

    #region RandomItem func
    public void TryGenItem(Vector3 pos, int boxId, System.Random rand = null)
    {
        if (ContainsPos(pos))
        {
            return;
        }
        //--to do:

        //test
        if (PeGameMgr.IsMulti)
        {
            PlayerNetwork.mainPlayer.RPCServer(EPacketType.PT_InGame_RandomItem, pos, boxId);
        }
        else
        {
            //3. new & add
            string             path;
            List <ItemIdCount> itemIdNum = RandomItemDataMgr.GenItemDicByBoxId(boxId, out path, rand);
            if (itemIdNum == null)
            {
                Debug.LogError("boxId error: " + boxId);
                return;
            }
            List <ItemIdCount> invalidList = itemIdNum.FindAll(it => it.protoId <= 0 || it.count <= 0 || ItemAsset.ItemProto.Mgr.Instance.Get(it.protoId) == null);
            if (invalidList != null && invalidList.Count > 0)
            {
                foreach (ItemIdCount idcount in invalidList)
                {
                    Debug.LogError("randomItem error:" + idcount.protoId + " " + idcount.count);
                    itemIdNum.Remove(idcount);
                }
            }
            if (itemIdNum.Count == 0)
            {
                Debug.LogError("empty boxId:" + boxId);
                return;
            }
            int[] items = new int[itemIdNum.Count * 2];
            int   index = 0;
            foreach (ItemIdCount item in itemIdNum)
            {
                items[index++] = item.protoId;
                items[index++] = item.count;
            }

            RandomItemObj rio = new RandomItemObj(boxId, pos, items, path);
            if (pos.y >= 0)
            {
                AddToAllItems(rio);
                AddToIndex256(rio);
            }
            mRandomItemsDic.Add(pos, rio);
            //if(Application.isEditor)
            //    Debug.Log("<color=yellow>A RandomItem is Added!"+pos+"id:"+ boxId+"path:"+path+" </color>");
        }
    }
    public bool IsBoxNumAvailable(int boxId)
    {
        int boxNumMax = RandomItemDataMgr.GetBoxAmount(boxId);

        if (!allRandomItems.ContainsKey(boxId))
        {
            return(0 < boxNumMax);
        }
        return(allRandomItems[boxId].Count < boxNumMax);
    }
    public void TryGenRareItem(Vector3 pos, int boxId, System.Random rand = null, List <ItemIdCount> specifiedItems = null)
    {
        if (ContainsPos(pos))
        {
            return;
        }

        string             path;
        List <ItemIdCount> itemIdNum = RandomItemDataMgr.GenItemDicByBoxId(boxId, out path, rand);

        if (itemIdNum == null)
        {
            Debug.LogError("boxId error: " + boxId);
            itemIdNum.Add(new ItemIdCount(1, 1));
        }
        if (specifiedItems != null)
        {
            itemIdNum.AddRange(specifiedItems);
        }
        List <ItemIdCount> invalidList = itemIdNum.FindAll(it => it.protoId <= 0 || it.count <= 0 || ItemAsset.ItemProto.Mgr.Instance.Get(it.protoId) == null);

        if (invalidList != null && invalidList.Count > 0)
        {
            foreach (ItemIdCount idcount in invalidList)
            {
                Debug.LogError("randomItem error:" + idcount.protoId + " " + idcount.count);
                itemIdNum.Remove(idcount);
            }
        }

        int[] items = new int[itemIdNum.Count * 2];
        int   index = 0;

        foreach (ItemIdCount item in itemIdNum)
        {
            items[index++] = item.protoId;
            items[index++] = item.count;
        }

        RandomItemObj rio = new RandomItemObj(boxId, pos, items, path);

        if (pos.y >= 0)
        {
            AddToAllItems(rio);
            AddToIndex256(rio);
        }
        rio.AddRareProto(DunItemId.UNFINISHED_ISO, 5);
        mRandomItemsDic.Add(pos, rio);
        RandomDungenMgrData.AddRareItem(rio);
    }
    public bool IsTerrainAvailable(IntVector2 GenPos, out Vector3 pos, out int boxId)
    {
        //--to do: 1.random the data
        int  height  = VFDataRTGen.GetPosHeight(GenPos.x, GenPos.y, true);
        bool inWater = VFDataRTGen.IsSea(height);
        bool inCave  = false;

        pos = new Vector3(GenPos.x, height - 2, GenPos.y);
        RaycastHit hitTest;

        if (Physics.Raycast(pos, Vector3.down, out hitTest, 512, 1 << Pathea.Layer.VFVoxelTerrain))
        {
            inCave = true;
        }
        RandomMapType type = VFDataRTGen.GetXZMapType(GenPos.x, GenPos.x);

        IntVector2    tileIndex = new IntVector2(GenPos.x >> VoxelTerrainConstants._shift, GenPos.y >> VoxelTerrainConstants._shift);
        VArtifactTown vaTown    = null;

        if (VArtifactTownManager.Instance != null)
        {
            for (int i = -4; i < 5; i++)
            {
                for (int j = -4; j < 5; j++)
                {
                    vaTown = VArtifactTownManager.Instance.GetTileTown(new IntVector2(tileIndex.x + i, tileIndex.y + j));
                    if (vaTown != null)
                    {
                        break;
                    }
                }
                if (vaTown != null)
                {
                    break;
                }
            }
        }



        List <int> genCondition = new List <int>();


        if (vaTown != null)
        {
            if (vaTown.type == VArtifactType.NpcTown)
            {
                genCondition.Add(BoxMapTypeInt.NEAR_TOWN);
            }
            else
            {
                genCondition.Add(BoxMapTypeInt.NEAR_CAMP);
            }
        }

        switch (type)
        {
        case RandomMapType.Desert:
            genCondition.Add(BoxMapTypeInt.DESERT);
            break;

        case RandomMapType.Redstone:
            genCondition.Add(BoxMapTypeInt.REDSTONE);
            break;

        default:
            genCondition.Add(BoxMapTypeInt.GRASSLAND);
            break;
        }

        if (inWater)
        {
            genCondition.Add(BoxMapTypeInt.IN_WATER);
        }

        if (inCave)
        {
            genCondition.Add(BoxMapTypeInt.IN_CAVE);
        }


        boxId = -1;
        List <RandomItemBoxInfo> boxInfoList = RandomItemDataMgr.GetBoxIdByCondition(genCondition, height);

        if (boxInfoList == null || boxInfoList.Count == 0)
        {
            return(false);
        }
        List <RandomItemBoxInfo> boxInfoAvailable = new List <RandomItemBoxInfo> ();

        foreach (RandomItemBoxInfo rib in boxInfoList)
        {
            if (IsBoxNumAvailable(rib.boxId, rib.boxAmount))
            {
                boxInfoAvailable.Add(rib);
            }
        }
        if (boxInfoAvailable.Count == 0)
        {
            return(false);
        }

        RandomItemBoxInfo boxInfo = boxInfoAvailable[new System.Random((int)System.DateTime.UtcNow.Ticks).Next(boxInfoAvailable.Count)];

        boxId = boxInfo.boxId;
        float deep = boxInfo.boxDepth;


        //2.check with boxId
        if (deep <= 0)
        {
            deep = -3;
        }
        else if (deep < 2)
        {
            deep = 2;
        }
        pos = new Vector3(GenPos.x, height - deep, GenPos.y);
        if (pos.y < 0)
        {
            pos.y = 0;
        }
        else
        {
            RaycastHit hit;
            if (Physics.Raycast(pos, Vector3.down, out hit, 512, 1 << Pathea.Layer.VFVoxelTerrain))
            {
                pos.y = hit.point.y - boxInfo.boxDepth;
            }
        }

        if (pos.y < 0)
        {
            return(false);
        }

        return(true);
    }
    public static void LoadAllData()
    {
        if (s_localDatabase != null)
        {
            return;
        }

#if UNITY_EDITOR
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();
#endif
        s_localDatabase = LoadDb();
        SkillSystem.SkData.LoadData();
        Pathea.Effect.EffectData.LoadData();
        Pathea.Projectile.ProjectileData.LoadData();
        Pathea.RequestRelation.LoadData();
        Pathea.CampData.LoadData();
        Pathea.ThreatData.LoadData();
        Pathea.DamageData.LoadData();
        HumanSoundData.LoadData();
        ItemDropData.LoadData();

        PELocalization.LoadData();

        NaturalResAsset.NaturalRes.LoadData();
        //SkillAsset.EffCastData.LoadData();
        //SkillAsset.EffSkill.LoadData();
        //SkillAsset.MergeSkill.LoadData();
        //AnimData.LoadData();
        //AnimSoundData.LoadData();

        AiAsset.AiData.LoadData();

        SoundAsset.SESoundBuff.LoadData();
        SoundAsset.SESoundStory.LoadData();
        //CharacterData.LoadCharacterData();
        StoryDoodadMap.LoadData();
        StoreRepository.LoadData();
        NpcMissionDataRepository.LoadData();
        //PlayerAttribute.LoadData();
        MissionRepository.LoadData();
        TalkRespository.LoadData();
        //NpcRandomRepository.LoadData();
        ShopRespository.LoadData();
        WareHouseManager.LoadData();
        //HeroTalkRepository.LoadData();
        MutiPlayRandRespository.LoadData();
        PromptRepository.LoadData();

        //MapIconData.LoadDate();
        //MapMaskData.LoadDate();
        CampPatrolData.LoadDate();
        Camp.LoadData();
        RepProcessor.LoadData();

        CloudManager.LoadData();
        //BattleUnitData.LoadData();
        TutorialData.LoadData();
        //RepairMachineManager.LoadData();
        MapMaskData.LoadDate();
        MessageData.LoadData();         //lz-2016.07.13 Add it
        MonsterHandbookData.LoadData(); //lz-2016.07.20 Add it
        StoryRepository.LoadData();
        RMRepository.LoadRandMission();
        MisInitRepository.LoadData();
        CameraRepository.LoadCameraPlot();
        AdRMRepository.LoadData();
        VCConfig.InitConfig();
        Cutscene.LoadData();

//		BuildBrushData.LoadBrush();
        BSPattern.LoadBrush();
        BSVoxelMatMap.Load();
        BSBlockMatMap.Load();
        BlockBuilding.LoadBuilding();
        LifeFormRule.LoadData();
        PlantInfo.LoadData();
        MetalScanData.LoadData();
        BattleConstData.LoadData();
        CustomCharactor.CustomMetaData.LoadData();
        SkillTreeInfo.LoadData();
        VArtifactUtil.LoadData();
        Pathea.ActionRelationData.LoadActionRelation();

        //colony
        CSInfoMgr.LoadData();
        ProcessingObjInfo.LoadData();
        CSTradeInfoData.LoadData();
        CampTradeIdData.LoadData();
        AbnormalTypeTreatData.LoadData();
        CSMedicineSupport.LoadData();
        //RandomItemMgr
        RandomItemDataMgr.LoadData();
        FecesData.LoadData();
        //randomdungeon
        RandomDungeonDataBase.LoadData();
        AbnormalData.LoadData();
        PEAbnormalNoticeData.LoadData();

        RelationInfo.LoadData();
        EquipSetData.LoadData();
        SuitSetData.LoadData();

        CheatData.LoadData();

        Pathea.NpcProtoDb.Load();
        Pathea.MonsterProtoDb.Load();
        Pathea.MonsterRandomDb.Load();
        Pathea.MonsterGroupProtoDb.Load();
        Pathea.RandomNpcDb.Load();
        Pathea.PlayerProtoDb.Load();
        Pathea.TowerProtoDb.Load();
        Pathea.DoodadProtoDb.Load();
        Pathea.AttPlusNPCData.Load();
        Pathea.AttPlusBuffDb.Load();
        Pathea.NpcTypeDb.Load();
        Pathea.NpcRandomTalkDb.Load();
        Pathea.NpcThinkDb.LoadData();
        Pathea.NpcEatDb.LoadData();
        Pathea.NpcRobotDb.Load();
        Pathea.NPCScheduleData.Load();
        Pathea.NpcVoiceDb.LoadData();
        InGameAidData.LoadData(); //lz-2016.08.21 add it
        MountsSkillDb.LoadData();

#if UNITY_EDITOR
        sw.Stop();
        Debug.Log("Database Loaded : " + sw.ElapsedMilliseconds);
        sw.Reset();
#else
        Debug.Log("Database Loaded");
#endif
    }