Example #1
0
    void CreateTileObject()
    {
        CTimeCheck.Start();

        GameObject block_image = CResourceMgr.LoadMapImage(BlockImage);
        GameObject tile_image  = CResourceMgr.LoadMapImage(TileImage);

        GameObject images_parent = Instantiate <GameObject>(CResourceMgr.LoadMapImage("ImagesParent"), new Vector3(0.0f, 0.0f, ImageDefaultPosZ), new Quaternion());

        for (int i = 0; i < 336; ++i)
        {
            GameObject new_object = Instantiate <GameObject>(block_image, new Vector3(-1.0f, -1.0f, ImageDefaultPosZ), new Quaternion());
            new_object.isStatic = true;
            new_object.tag      = "BlockImage";
            new_object.transform.SetParent(images_parent.transform);
            block_objects.Add(new_object);
        }

        for (int i = 0; i < 336; ++i)
        {
            GameObject new_object = Instantiate <GameObject>(tile_image, new Vector3(-1.0f, -1.0f, ImageDefaultPosZ), new Quaternion());
            new_object.isStatic = true;
            new_object.tag      = "TileImage";
            new_object.transform.SetParent(images_parent.transform);
            tile_objects.Add(new_object);
        }

        CTimeCheck.End(ELogType.MapGenerator, "create tile object");
    }
Example #2
0
    void ShowTile()
    {
        RemoveAllTile();

        CTimeCheck.Start();

        GameObject block_image = CResourceMgr.LoadMapImage(BlockImage);
        GameObject tile_image  = CResourceMgr.LoadMapImage(TileImage);

        GameObject images_parent = Instantiate <GameObject>(CResourceMgr.LoadMapImage("ImagesParent"), new Vector3(0.0f, 0.0f, ImageDefaultPosZ), new Quaternion());


        for (int y = 0; y < Size; ++y)
        {
            for (int x = 0; x < Size; ++x)
            {
                if (Tiles[y * Size + x])
                {
                    GameObject NewGameObject = Instantiate <GameObject>(block_image, new Vector3(x, y, ImageDefaultPosZ), new Quaternion() /*, new_empty_resource.transform*/);
                    NewGameObject.isStatic = true;
                    NewGameObject.tag      = "BlockImage";
                    NewGameObject.transform.SetParent(images_parent.transform);
                }
                else
                {
                    GameObject NewGameObject = Instantiate <GameObject>(tile_image, new Vector3(x, y, ImageDefaultPosZ), new Quaternion() /*, new_empty_resource.transform*/);
                    NewGameObject.isStatic = true;
                    NewGameObject.tag      = "TileImage";
                    NewGameObject.transform.SetParent(images_parent.transform);
                }
            }
        }

        CTimeCheck.End(ELogType.MapGenerator, "show tiles");
    }
Example #3
0
    public void OnSaveWayPointPaths()
    {
        CTimeCheck.Start();

        string       FileFullName = FileName + "_WPP";
        string       Path         = GetPath(ref FileName);
        FileStream   fs           = new FileStream(Path, FileMode.Create);
        BinaryWriter bw           = new BinaryWriter(fs);

        bw.Write(WayPointPaths.Count);
        foreach (var kv in WayPointPaths)
        {
            bw.Write(kv.Key);
            bw.Write(kv.Value.Count);

            for (int i = 0; i < kv.Value.Count; ++i)
            {
                bw.Write((int)kv.Value[i]);
            }
        }

        bw.Close();
        fs.Close();

        CTimeCheck.End(ELogType.MapGenerator, "OnSaveWayPointPaths");
    }
Example #4
0
    public void OnSave()
    {
        CTimeCheck.Start();

        UpdateTilesFromAddImage();

        int Count     = Tiles.Length / 64;
        int Remainder = Tiles.Length % 64;

        if (0 < Remainder)
        {
            ++Count;
        }

        UInt64[] SaveData      = new UInt64[Count];
        int      SaveDataIndex = 0;

        for (int i = 0; i < Tiles.Length; ++i)
        {
            SaveDataIndex = i / 64;
            if (Tiles[i])
            {
                SaveData[SaveDataIndex] |= 0x01;
            }

            if ((i + 1) % 64 != 0 && i != Tiles.Length - 1)
            {
                SaveData[SaveDataIndex] = SaveData[SaveDataIndex] << 1;
            }
        }
        if (0 < Remainder)
        {
            SaveData[SaveDataIndex] = SaveData[SaveDataIndex] << (64 - Remainder);
        }

        string       Path = GetPath(ref FileName);
        FileStream   fs   = new FileStream(Path, FileMode.Create);
        BinaryWriter bw   = new BinaryWriter(fs);

        bw.Write((UInt16)Count);        // size 넘어 갈수 있으니 조심
        bw.Write((UInt16)Remainder);
        for (int i = 0; i < Count; ++i)
        {
            bw.Write(SaveData[i]);
        }

        CTimeCheck.End(ELogType.MapGenerator, "map generator save");

        SaveMapObject(ref bw);

        bw.Write(BlockImage);
        bw.Write(TileImage);

        bw.Close();
        fs.Close();
    }
Example #5
0
    public void OnLoadWayPointPaths()
    {
        CTimeCheck.Start();

        FileStream   fileStream   = null;
        BinaryReader binaryReader = null;

        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            CTextAssetMgr TextAssetMgr = GameObject.FindGameObjectWithTag("TextAssetMgr").GetComponent <CTextAssetMgr>();
            binaryReader = TextAssetMgr.LoadTextAsset(CTextAssetMgr.EFileType.MapWPP, FileName);
            if (binaryReader == null)
            {
                return;
            }
        }
        else
        {
            string FileFullPath = FileName + "_WPP";
            string Path         = GetPath(ref FileFullPath);
            if (false == File.Exists(Path))
            {
                return;
            }
            fileStream   = new FileStream(Path, FileMode.Open);
            binaryReader = new BinaryReader(fileStream);
        }
        WayPointPaths.Clear();
        int Count = binaryReader.ReadInt32();

        for (int i = 0; i < Count; ++i)
        {
            int       Key        = binaryReader.ReadInt32();
            int       ValueCount = binaryReader.ReadInt32();
            ArrayList paths      = new ArrayList();
            for (int j = 0; j < ValueCount; ++j)
            {
                paths.Add(binaryReader.ReadInt32());
            }
            WayPointPaths.Add(Key, paths);
        }
        binaryReader.Close();
        if (fileStream != null)
        {
            fileStream.Close();
        }
        CTimeCheck.End(ELogType.MapGenerator, "OnLoadWayPointPaths");
    }
Example #6
0
    void RemoveAllTile()
    {
        CTimeCheck.Start();
        GameObject[] BlockImagePrefabs = GameObject.FindGameObjectsWithTag("BlockImage");
        foreach (GameObject Block in BlockImagePrefabs)
        {
            Destroy(Block);
        }

        GameObject[] TileImagePrefabs = GameObject.FindGameObjectsWithTag("TileImage");
        foreach (GameObject Tile in TileImagePrefabs)
        {
            Destroy(Tile);
        }
        CTimeCheck.End(ELogType.MapGenerator, "RemoveAllTile");
    }
Example #7
0
    public override ArrayList FindPaths(int StartPos, int InEndPos, int InID)
    {
        ArrayList Paths = new ArrayList();

        if (!IsValidPosIndex(StartPos) || !IsValidPosIndex(InEndPos))
        {
            return(Paths);
        }

        CTimeCheck.Start(InID);
        EndPos = InEndPos;

        ClosedList.Clear();
        OpenList.Clear();
        foreach (Tile tile in Tiles)
        {
            if (tile.bBlock)
            {
                ClosedList.Add(tile.Index);
            }

            tile.Reset();
        }

        ClosedList.Add(StartPos);
        int FindNextPath         = StartPos;
        int path_make_start_time = Environment.TickCount;

        while (true)
        {
            //if (100 <= Environment.TickCount - path_make_start_time)
            //    return Paths;

            ArrayList MakedOpenList = MakeOpenList(FindNextPath);

            if (OpenList.Count == 0)
            {
                CDebugLog.Log(ELogType.AStar, "OpenList.Count == 0");
            }

            //FindNextPath = FindNextPathIndex(ref MakedOpenList);
            ////FindNextPath = FindNextPathIndexFromOpenList();

            //if (-1 == FindNextPath)
            //{
            FindNextPath = FindNextPathIndexFromOpenList();
            //}

            if (-1 == FindNextPath)
            {
                CTimeCheck.End(ELogType.AStar, "Failed FindPaths");
                return(Paths);
            }

            if (FindNextPath == EndPos)
            {
                break;
            }

            OpenList.Remove(FindNextPath);
            ClosedList.Add(FindNextPath);
        }

        int path_index = EndPos;

        Paths.Add(EndPos);
        while (true)
        {
            if (path_index == StartPos)
            {
                break;
            }

            Paths.Add(((Tile)Tiles[path_index]).ParentTile);
            path_index = ((Tile)Tiles[path_index]).ParentTile;
        }
        Paths.Reverse();


        CTimeCheck.End(ELogType.AStar, "Success FindPaths");
        return(Paths);
    }
Example #8
0
    public override ArrayList FindPaths(int StartPos, int EndPos, int InID)
    {
        ArrayList Paths = new ArrayList();

        if (!IsValidPosIndex(StartPos) || !IsValidPosIndex(EndPos))
        {
            return(Paths);
        }

        CTimeCheck.Start(InID);

        Tile StartTile = (Tile)Tiles[StartPos];
        Tile EndTile   = (Tile)Tiles[EndPos];

        if (StartTile.bBlock || EndTile.bBlock)
        {
            return(Paths);
        }

        bool bFindedBlock = StartAndEndPathBlockCheck(StartPos, EndPos);

        if (false == bFindedBlock)
        {
            Paths.Add(EndPos);
            CTimeCheck.End(ELogType.AStar, "Success FindPaths");
            return(Paths);
        }

        Vector2 StartVec = new Vector2(StartTile.PosX, StartTile.PosY);
        Vector2 EndVec   = new Vector2(EndTile.PosX, EndTile.PosY);

        int CloseWayPointIdxStart = -1;
        int CloseWayPointIdxEnd   = -1;

        float StartLenth          = 99999.0f;
        float StartHeuristicLenth = 99999.0f;
        float EndLenth            = 99999.0f;
        float EndHeuristicLenth   = 99999.0f;

        for (int i = 0; i < WayPoints.Count; ++i)
        {
            Tile    WayPointTile     = (Tile)Tiles[(int)WayPoints[i]];
            Vector2 WayPointPos      = new Vector2(WayPointTile.PosX, WayPointTile.PosY);
            float   CurStartLenth    = Vector2.Distance(StartVec, WayPointPos);
            float   CurEndLenth      = Vector2.Distance(EndVec, WayPointPos);
            float   CurHeristicLenth = CurStartLenth + CurEndLenth;
            if (CurStartLenth < StartLenth || (CurStartLenth == StartLenth && CurHeristicLenth < StartHeuristicLenth))
            {
                CloseWayPointIdxStart = WayPointTile.Index;
                StartLenth            = CurStartLenth;
                StartHeuristicLenth   = CurHeristicLenth;
            }
            if (CurEndLenth < EndLenth || (CurEndLenth == EndLenth && CurHeristicLenth < EndHeuristicLenth))
            {
                CloseWayPointIdxEnd = WayPointTile.Index;
                EndLenth            = CurEndLenth;
                EndHeuristicLenth   = CurHeristicLenth;
            }
        }
        if (CloseWayPointIdxStart == -1)
        {
            Paths.Add(EndPos);
        }
        else if (CloseWayPointIdxStart == CloseWayPointIdxEnd)
        {
            Paths.Add(CloseWayPointIdxStart);
            Paths.Add(EndPos);
        }
        else
        {
            int       Key = CloseWayPointIdxStart < CloseWayPointIdxEnd ? CloseWayPointIdxStart * 10000 + CloseWayPointIdxEnd : CloseWayPointIdxEnd * 10000 + CloseWayPointIdxStart;
            ArrayList FindPaths;
            if (WayPointPaths.TryGetValue(Key, out FindPaths))
            {
                Paths = (ArrayList)FindPaths.Clone();
                if ((int)Paths[0] == CloseWayPointIdxStart)
                {
                    Paths.Insert(Paths.Count, EndPos);
                }
                else
                {
                    Paths.Reverse();
                    Paths.Insert(Paths.Count, EndPos);
                }
            }
            else
            {
                return(Paths);
            }
        }

        CTimeCheck.End(ELogType.AStar, "Success FindPaths");

        return(Paths);
    }
Example #9
0
    public void OnBuildWayPointPaths()
    {
        CTimeCheck.Start(0);

        OnMakeWayPoints();

        CAStar AStar = new CAStar();

        AStar.InitTiles(ref Tiles);

        for (int i = WayPoints.Count / 2; i < WayPoints.Count; ++i)
        {
            for (int j = WayPoints.Count / 2 - 1; 0 <= j; --j)
            {
                int StartPosIndex = (int)WayPoints[i];
                int EndPosIndex   = (int)WayPoints[j];

                if (StartPosIndex == EndPosIndex)
                {
                    continue;
                }

                int Key = StartPosIndex < EndPosIndex ? StartPosIndex * 10000 + EndPosIndex : EndPosIndex * 10000 + StartPosIndex;
                if (WayPointPaths.ContainsKey(Key))
                {
                    continue;
                }


                ArrayList FindedPaths = AStar.FindPaths(StartPosIndex, EndPosIndex, 0);

                ArrayList RemoveIndexs = new ArrayList();
                for (int k = 0; k < FindedPaths.Count; ++k)
                {
                    if (!WayPoints.Contains(FindedPaths[k]))
                    {
                        RemoveIndexs.Add(FindedPaths[k]);
                    }
                }
                for (int a = 0; a < RemoveIndexs.Count; ++a)
                {
                    FindedPaths.Remove(RemoveIndexs[a]);
                }

                if (3 < FindedPaths.Count)
                {
                    int PathCount = FindedPaths.Count / 2;
                    for (int count = 1; count < PathCount; ++count)
                    {
                        int NewStartIndex = (int)FindedPaths[count];
                        int NewEndIndex   = (int)FindedPaths[FindedPaths.Count - 1 - count];
                        int NewKey        = NewStartIndex < NewEndIndex ? NewStartIndex * 10000 + NewEndIndex : NewEndIndex * 10000 + NewStartIndex;

                        if (WayPointPaths.ContainsKey(NewKey))
                        {
                            continue;
                        }

                        ArrayList clone = (ArrayList)FindedPaths.Clone();
                        clone.RemoveAt(FindedPaths.Count - count);
                        clone.RemoveAt(count - 1);
                        //clone = clone.GetRange(count, FindedPaths.Count - 1 - count);
                        WayPointPaths.Add(NewKey, clone);
                    }
                }
                WayPointPaths.Add(Key, FindedPaths);
            }
        }

        for (int i = 0; i < WayPoints.Count; ++i)
        {
            for (int j = i + 1; j < WayPoints.Count; ++j)
            {
                int StartPosIndex = (int)WayPoints[i];
                int EndPosIndex   = (int)WayPoints[j];

                if (StartPosIndex == EndPosIndex)
                {
                    continue;
                }

                int Key = StartPosIndex < EndPosIndex ? StartPosIndex * 10000 + EndPosIndex : EndPosIndex * 10000 + StartPosIndex;
                if (WayPointPaths.ContainsKey(Key))
                {
                    continue;
                }

                ArrayList FindedPaths = AStar.FindPaths(StartPosIndex, EndPosIndex, 0);

                ArrayList RemoveIndexs = new ArrayList();
                for (int k = 0; k < FindedPaths.Count; ++k)
                {
                    if (!WayPoints.Contains(FindedPaths[k]))
                    {
                        RemoveIndexs.Add(FindedPaths[k]);
                    }
                }
                for (int a = 0; a < RemoveIndexs.Count; ++a)
                {
                    FindedPaths.Remove(RemoveIndexs[a]);
                }

                if (3 < FindedPaths.Count)
                {
                    int PathCount = FindedPaths.Count / 2;
                    for (int count = 1; count < PathCount; ++count)
                    {
                        int NewStartIndex = (int)FindedPaths[count];
                        int NewEndIndex   = (int)FindedPaths[FindedPaths.Count - 1 - count];
                        int NewKey        = NewStartIndex < NewEndIndex ? NewStartIndex * 10000 + NewEndIndex : NewEndIndex * 10000 + NewStartIndex;

                        if (WayPointPaths.ContainsKey(NewKey))
                        {
                            continue;
                        }

                        ArrayList clone = (ArrayList)FindedPaths.Clone();
                        clone.RemoveAt(FindedPaths.Count - count);
                        clone.RemoveAt(count - 1);
                        //clone = clone.GetRange(count, FindedPaths.Count - 1 - count);
                        WayPointPaths.Add(NewKey, clone);
                    }
                }
                WayPointPaths.Add(Key, FindedPaths);
            }
        }

        CTimeCheck.End(ELogType.MapGenerator, "BuildFastPaths");
    }
Example #10
0
    public void OnLoad()
    {
        CTimeCheck.Start();

        BinaryReader binaryReader = null;
        FileStream   fileStream   = null;

        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            CTextAssetMgr TextAssetMgr = GameObject.FindGameObjectWithTag("TextAssetMgr").GetComponent <CTextAssetMgr>();
            binaryReader = TextAssetMgr.LoadTextAsset(CTextAssetMgr.EFileType.Map, FileName);
            if (binaryReader == null)
            {
                return;
            }
        }
        else
        {
            string Path = GetPath(ref FileName);
            fileStream   = new FileStream(Path, FileMode.Open);
            binaryReader = new BinaryReader(fileStream);
        }

        int Count     = (int)binaryReader.ReadUInt16();
        int Remainder = (int)binaryReader.ReadUInt16();

        UInt64[] SaveData = new UInt64[Count];
        for (int i = 0; i < Count; ++i)
        {
            SaveData[i] = binaryReader.ReadUInt64();
        }

        TileFullSize = 0 < Remainder ? (Count - 1) * 64 + Remainder : Count * 64;
        Size         = (int)Math.Sqrt(TileFullSize);
        Tiles        = new bool[TileFullSize];

        for (int i = 0; i < Count; ++i)
        {
            for (int j = 0; j < 64; ++j)
            {
                if (0 < Remainder && i == Count - 1 && Remainder == j)
                {
                    break;
                }

                if ((UInt64)0 < (SaveData[i] & 0x8000000000000000))
                {
                    Tiles[i * 64 + j] = true;
                }

                SaveData[i] = SaveData[i] << 1;
            }
        }

        LoadMapObjects(ref binaryReader);

        BlockImage = binaryReader.ReadString();
        TileImage  = binaryReader.ReadString();

        binaryReader.Close();
        if (fileStream != null)
        {
            fileStream.Close();
        }

        if (bShowAllTile)
        {
            ShowTile();
        }
        else
        {
            CreateTileObject();
        }

        CTimeCheck.End(ELogType.MapGenerator, "OnLoad");
    }
Example #11
0
    void SaveMapObject(ref BinaryWriter binaryWriter)
    {
        CTimeCheck.Start();

        Dictionary <string, CMapObject[]> SortMapObjects = new Dictionary <string, CMapObject[]>();

        CMapObject[] MapObjects = FindObjectsOfType <CMapObject>();
        foreach (var MapObject in MapObjects)
        {
            CMapObject[] FindMapObjects;
            string       FixName   = MapObject.name;
            int          FindIndex = FixName.IndexOf('(');
            if (-1 != FindIndex)
            {
                FixName = FixName.Remove(FindIndex);
            }
            FixName = FixName.Trim();
            if (SortMapObjects.TryGetValue(FixName, out FindMapObjects))
            {
                for (int i = 0; i < FindMapObjects.Length; ++i)
                {
                    if (FindMapObjects[i] == null)
                    {
                        FindMapObjects[i] = MapObject;
                        break;
                    }
                }
            }
            else
            {
                CMapObject[] AddMapObjects = new CMapObject[100];
                AddMapObjects[0] = MapObject;
                SortMapObjects.Add(FixName, AddMapObjects);
            }
        }

        binaryWriter.Write(SortMapObjects.Keys.Count);
        byte[] ObjectCountBuffer = new byte[SortMapObjects.Keys.Count];
        int    Index             = 0;

        foreach (var kv in SortMapObjects)
        {
            CMapObject[] Values = kv.Value;
            for (int i = 0; i < Values.Length; ++i)
            {
                if (Values[i] == null)
                {
                    ObjectCountBuffer[Index] = (byte)i;
                    break;
                }
            }
            ++Index;
        }
        binaryWriter.Write(ObjectCountBuffer);
        foreach (var kv in SortMapObjects)
        {
            binaryWriter.Write(kv.Key);
            CMapObject[] Values = kv.Value;
            for (int i = 0; i < Values.Length; ++i)
            {
                if (Values[i] != null)
                {
                    Values[i].Write(ref binaryWriter);
                }
                else
                {
                    break;
                }
            }
        }
        CTimeCheck.End(ELogType.MapGenerator, "map generator save map object");
    }