Ejemplo n.º 1
0
        static bool LoadRegionData(Index regionIndex)
        {
            // loads the region data into memory if file exists and it's not already loaded, returns true if data exists (and is loaded), else false
            string indexString = regionIndex.ToString();

            if (LoadedRegions.ContainsKey(indexString) == false)
            {
                // if not loaded
                // load data if region file exists
                string regionPath = GetRegionPath(regionIndex);
                if (File.Exists(regionPath))
                {
                    StreamReader reader     = new StreamReader(regionPath);
                    string[]     regionData = reader.ReadToEnd().Split((char)ushort.MaxValue);
                    reader.Close();
                    LoadedRegions[indexString] = regionData;

                    return(true);
                }
                else
                {
                    return(false); // return false if region file doesn't exist
                }
            }

            return(true); // return true if data is already loaded
        }
Ejemplo n.º 2
0
 public static Chunk GetChunkComponent(Index index)
 {
     string indexString = index.ToString();
     if ( ChunkManager.Chunks.ContainsKey (indexString) ) {
     return ChunkManager.Chunks [indexString];
     }
     else {
     return null;
     }
 }
Ejemplo n.º 3
0
 private static string[] GetRegionData(Index regionIndex)    // loads region data and from file returns it, or returns null if region file is not found
 {
     if (LoadRegionData(regionIndex) == true)
     {
         return(LoadedRegions[regionIndex.ToString()]);
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 4
0
        GameObject DoSpawnChunk(Index index)
        {
            GameObject chunkObject = Instantiate(ChunkObject, index.ToVector3(), transform.rotation) as GameObject;

            chunkObject.name             = "Chunk:" + index.ToString();
            chunkObject.transform.parent = transform;
            Chunk chunk = chunkObject.GetComponent <Chunk>();

            AddChunkToUpdateQueue(chunk);
            return(chunkObject);
        }
Ejemplo n.º 5
0
        public static Chunk GetChunkComponent(Index index)
        {
            string indexString = index.ToString();

            if (ChunkManager.Chunks.ContainsKey(indexString))
            {
                return(ChunkManager.Chunks[indexString]);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 6
0
        private string GetChunkData(Index index)    // returns the chunk data (from memory or from file), or an empty string if data can't be found
        // try to load from TempChunkData
        {
            string indexString = index.ToString();

            if (TempChunkData.ContainsKey(indexString))
            {
                return(TempChunkData[indexString]);
            }

            // try to load from region, return empty if not found
            int regionIndex = GetChunkRegionIndex(index);

            string[] regionData = GetRegionData(GetParentRegion(index));
            if (regionData == null)
            {
                return("");
            }
            return(regionData[regionIndex]);
        }
Ejemplo n.º 7
0
 private static string GetRegionPath(Index regionIndex)
 {
     return(Engine.WorldPath + (regionIndex.ToString() + ",.region"));
 }
Ejemplo n.º 8
0
 private void WriteChunkData(Index index, string data)    // writes the chunk data to the TempChunkData dictionary
 {
     TempChunkData[index.ToString()] = data;
 }
Ejemplo n.º 9
0
 private void WriteChunkData(Index index, string data)
 {
     // writes the chunk data to the TempChunkData dictionary
     TempChunkData[index.ToString()] = data;
 }
Ejemplo n.º 10
0
        private string GetChunkData(Index index)
        {
            // returns the chunk data (from memory or from file), or an empty string if data can't be found

            // try to load from TempChunkData
            string indexString = index.ToString();
            if (TempChunkData.ContainsKey (indexString)) {
            return TempChunkData[indexString];
            }

            // try to load from region, return empty if not found
            int regionIndex = GetChunkRegionIndex (index);
            string[] regionData = GetRegionData (GetParentRegion(index));
            if (regionData == null) {
            return "";
            }
            return regionData[regionIndex];
        }
Ejemplo n.º 11
0
        private static bool LoadRegionData(Index regionIndex)
        {
            // loads the region data into memory if file exists and it's not already loaded, returns true if data exists (and is loaded), else false

            string indexString = regionIndex.ToString();
            if (LoadedRegions.ContainsKey(indexString) == false) { // if not loaded

            // load data if region file exists
            string regionPath = GetRegionPath (regionIndex);
            if (File.Exists (regionPath)) {

                StreamReader reader = new StreamReader (regionPath);
                string[] regionData = reader.ReadToEnd().Split((char)ushort.MaxValue);
                reader.Close();
                LoadedRegions[indexString] = regionData;

                return true;

            }

            else {
                return false; // return false if region file doesn't exist
            }
            }

            return true; // return true if data is already loaded
        }
Ejemplo n.º 12
0
 private static string GetRegionPath(Index regionIndex)
 {
     return Engine.WorldPath + ( regionIndex.ToString() + ",.region");
 }
Ejemplo n.º 13
0
        private static string[] GetRegionData(Index regionIndex)
        {
            // loads region data and from file returns it, or returns null if region file is not found

            if (LoadRegionData(regionIndex) == true) {
            return LoadedRegions[regionIndex.ToString()];
            }
            else {
            return null;
            }
        }
Ejemplo n.º 14
0
        private IEnumerator SpawnMissingChunks(int originX, int originY, int originZ, int range)
        {
            int heightRange = Engine.HeightRange;

            ChunkUpdateQueue = new List <Chunk>(); // clear update queue - it will be repopulated again in the correct order in the following loop

            // flag chunks not in range for removal
            ChunksToDestroy = new List <Chunk>();
            foreach (Chunk chunk in Chunks.Values)
            {
                if (Vector2.Distance(new Vector2(chunk.ChunkIndex.x, chunk.ChunkIndex.z), new Vector2(originX, originZ)) > range + Engine.ChunkDespawnDistance)
                {
                    ChunksToDestroy.Add(chunk);
                }

                else if (Mathf.Abs(chunk.ChunkIndex.y - originY) > range + Engine.ChunkDespawnDistance)
                { // destroy chunks outside of vertical range
                    ChunksToDestroy.Add(chunk);
                }
            }


            // main loop
            for (int currentLoop = 0; currentLoop <= range; currentLoop++)
            {
                for (var x = originX - currentLoop; x <= originX + currentLoop; x++)
                { // iterate through all potential chunk indexes within range
                    for (var y = originY - currentLoop; y <= originY + currentLoop; y++)
                    {
                        for (var z = originZ - currentLoop; z <= originZ + currentLoop; z++)
                        {
                            if (Mathf.Abs(y) <= heightRange)
                            {     // skip chunks outside of height range
                                if (Mathf.Abs(originX - x) + Mathf.Abs(originZ - z) < range * 1.3f)
                                { // skip corners
                                    // pause loop while the queue is not empty
                                    while (ChunkUpdateQueue.Count > 0)
                                    {
                                        ProcessChunkQueue();
                                        if (frameStopwatch.Elapsed.TotalSeconds >= targetFrameDuration)
                                        {
                                            yield return(new WaitForEndOfFrame());
                                        }
                                    }

                                    Chunk currentChunk = ChunkManager.GetChunkComponent(x, y, z);


                                    // chunks that already exist but haven't had their mesh built yet should be added to the update queue
                                    if (currentChunk != null)
                                    {
                                        // chunks without meshes spawned by server should be changed to regular chunks
                                        if (currentChunk.DisableMesh || currentChunk.EnableTimeout)
                                        {
                                            currentChunk.DisableMesh   = false;
                                            currentChunk.EnableTimeout = false;
                                            currentChunk.Fresh         = true;
                                        }

                                        if (currentChunk.Fresh)
                                        {
                                            // spawn neighbor chunks
                                            for (int d = 0; d < 6; d++)
                                            {
                                                Index      neighborIndex = currentChunk.ChunkIndex.GetAdjacentIndex((Direction)d);
                                                GameObject neighborChunk = GetChunk(neighborIndex);
                                                if (neighborChunk == null)
                                                {
                                                    neighborChunk                  = Instantiate(ChunkObject, neighborIndex.ToVector3(), transform.rotation) as GameObject;
                                                    neighborChunk.name             = "Chunk:" + neighborIndex.ToString();
                                                    neighborChunk.transform.parent = transform;
                                                }
                                                currentChunk.NeighborChunks[d] = neighborChunk.GetComponent <Chunk>(); // always add the neighbor to NeighborChunks, in case it's not there already

                                                // continue loop in next frame if the current frame time is exceeded
                                                if (frameStopwatch.Elapsed.TotalSeconds >= targetFrameDuration)
                                                {
                                                    yield return(new WaitForEndOfFrame());
                                                }
                                                if (StopSpawning)
                                                {
                                                    EndSequence();
                                                    yield break;
                                                }
                                            }

                                            if (currentChunk != null)
                                            {
                                                currentChunk.AddToQueueWhenReady();
                                            }
                                        }
                                    }

                                    else
                                    {                                                                                                           // if chunk doesn't exist, create new chunk (it adds itself to the update queue when its data is ready)
                                        // spawn chunk
                                        GameObject newChunk = Instantiate(ChunkObject, new Vector3(x, y, z), transform.rotation) as GameObject; // Spawn a new chunk.
                                        currentChunk              = newChunk.GetComponent <Chunk>();
                                        newChunk.name             = "Chunk:" + currentChunk.ChunkIndex.ToString();
                                        newChunk.transform.parent = transform;


                                        // spawn neighbor chunks if they're not spawned yet
                                        for (int d = 0; d < 6; d++)
                                        {
                                            Index      neighborIndex = currentChunk.ChunkIndex.GetAdjacentIndex((Direction)d);
                                            GameObject neighborChunk = GetChunk(neighborIndex);
                                            if (neighborChunk == null)
                                            {
                                                neighborChunk                  = Instantiate(ChunkObject, neighborIndex.ToVector3(), transform.rotation) as GameObject;
                                                neighborChunk.name             = "Chunk:" + neighborIndex.ToString();
                                                neighborChunk.transform.parent = transform;
                                            }
                                            currentChunk.NeighborChunks[d] = neighborChunk.GetComponent <Chunk>(); // always add the neighbor to NeighborChunks, in case it's not there already

                                            // continue loop in next frame if the current frame time is exceeded
                                            if (frameStopwatch.Elapsed.TotalSeconds >= targetFrameDuration)
                                            {
                                                yield return(new WaitForEndOfFrame());
                                            }
                                            if (StopSpawning)
                                            {
                                                EndSequence();
                                                yield break;
                                            }
                                        }

                                        if (currentChunk != null)
                                        {
                                            currentChunk.AddToQueueWhenReady();
                                        }
                                    }
                                }
                            }



                            // continue loop in next frame if the current frame time is exceeded
                            if (frameStopwatch.Elapsed.TotalSeconds >= targetFrameDuration)
                            {
                                yield return(new WaitForEndOfFrame());
                            }
                            if (StopSpawning)
                            {
                                EndSequence();
                                yield break;
                            }
                        }
                    }
                }
            }

            yield return(new WaitForEndOfFrame());

            EndSequence();
        }
Ejemplo n.º 15
0
 private static string GetRegionPath_Show(Index regionIndex)
 {
     return(Engine.WorldPath + (regionIndex.ToString() + "_show.txt"));
 }
Ejemplo n.º 16
0
        public static Chunk GetChunkComponent(Index index)
        {
            string s = index.ToString();

            return(Chunks.ContainsKey(s) ? Chunks[s] : null);
        }