Пример #1
0
        //WARN: When load dist is a power of 2 chunks constantly ask to load more chunks.
        public void QueueNeighbors(Chunk chunk)
        {
            Chunk neighborchunk;
            ChunkCoordinate neighborcoords;
            Vector3 newpos;
            bool contains;

            //+x
            //Get new chunkcoords
            chunk.Coords.Add(1, 0, out neighborcoords);
            contains = ChunkStorage.Contains(ref neighborcoords, out neighborchunk);
            if ((chunk.NeighborsInMemoryFlag & 1) != 1)
            {
                if (!contains)
                {
                    //Shift this chunks position to get the new chunks relative position
                    newpos.X = chunk.Position.X + Chunk.WIDTH;
                    newpos.Y = chunk.Position.Y;
                    newpos.Z = chunk.Position.Z;
                    //Create a new chunk
                    neighborchunk = new Chunk(this, ref neighborcoords, ref newpos);
                    //Update flag and connect event chain to neighbor
                    chunk.NeighborsInMemoryFlag |= 1;
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                    //Add new chunk to manager
                    AddChunk(neighborchunk);
                }
                else if (contains)//Chunk is already in memory connect to it
                {
                    //Update flag and connect event chain to neighbor
                    chunk.ConsolidateFlags(neighborchunk, 1);
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                }
            }
            //-x
            //Get new chunkcoords
            chunk.Coords.Add(-1, 0, out neighborcoords);
            contains = ChunkStorage.Contains(ref neighborcoords, out neighborchunk);
            if ((chunk.NeighborsInMemoryFlag & 2) != 2)
            {
                if (!contains)
                {
                    //Shift this chunks position to get the new chunks relative position
                    newpos.X = chunk.Position.X - Chunk.WIDTH;
                    newpos.Y = chunk.Position.Y;
                    newpos.Z = chunk.Position.Z;
                    //Create a new chunk
                    neighborchunk = new Chunk(this, ref neighborcoords, ref newpos);
                    //Update flag and connect event chain to neighbor
                    chunk.NeighborsInMemoryFlag |= 2;
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                    //Add new chunk to manager
                    AddChunk(neighborchunk);
                }
                else if (contains)//Chunk is already in memory connect to it
                {
                    //Update flag and connect event chain to neighbor
                    chunk.ConsolidateFlags(neighborchunk, 2);
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                }
            }
            //+z
            //Get new chunkcoords
            chunk.Coords.Add(0, 1, out neighborcoords);
            contains = ChunkStorage.Contains(ref neighborcoords, out neighborchunk);
            if ((chunk.NeighborsInMemoryFlag & 4) != 4)
            {
                if (!contains)
                {
                    //Shift this chunks position to get the new chunks relative position
                    newpos.X = chunk.Position.X;
                    newpos.Y = chunk.Position.Y;
                    newpos.Z = chunk.Position.Z + Chunk.WIDTH;
                    //Create a new chunk
                    neighborchunk = new Chunk(this, ref neighborcoords, ref newpos);
                    //Update flag and connect event chain to neighbor
                    chunk.NeighborsInMemoryFlag |= 4;
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                    //Add new chunk to manager
                    AddChunk(neighborchunk);
                }
                else if (contains)//Chunk is already in memory connect to it
                {
                    //Update flag and connect event chain to neighbor
                    chunk.ConsolidateFlags(neighborchunk, 4);
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                }
            }
            //-z
            //Get new chunkcoords
            chunk.Coords.Add(0, -1, out neighborcoords);
            contains = ChunkStorage.Contains(ref neighborcoords, out neighborchunk);
            if ((chunk.NeighborsInMemoryFlag & 8) != 8)
            {
                if (!contains)
                {
                    //Shift this chunks position to get the new chunks relative position
                    newpos.X = chunk.Position.X;
                    newpos.Y = chunk.Position.Y;
                    newpos.Z = chunk.Position.Z - Chunk.WIDTH;
                    //Create a new chunk
                    neighborchunk = new Chunk(this, ref neighborcoords, ref newpos);
                    //Update flag and connect event chain to neighbor
                    chunk.NeighborsInMemoryFlag |= 8;
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                    //Add new chunk to manager
                    AddChunk(neighborchunk);
                }
                else if (contains)//Chunk is already in memory connect to it
                {
                    //Update flag and connect event chain to neighbor
                    chunk.ConsolidateFlags(neighborchunk, 8);
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                }
            }
            //+x +z
            //Get new chunkcoords
            chunk.Coords.Add(1, 1, out neighborcoords);
            contains = ChunkStorage.Contains(ref neighborcoords, out neighborchunk);
            if ((chunk.NeighborsInMemoryFlag & 16) != 16)
            {
                if (!contains)
                {
                    //Shift this chunks position to get the new chunks relative position
                    newpos.X = chunk.Position.X + Chunk.WIDTH;
                    newpos.Y = chunk.Position.Y;
                    newpos.Z = chunk.Position.Z + Chunk.WIDTH;
                    //Create a new chunk
                    neighborchunk = new Chunk(this, ref neighborcoords, ref newpos);
                    //Update flag and connect event chain to neighbor
                    chunk.NeighborsInMemoryFlag |= 16;
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                    //Add new chunk to manager
                    AddChunk(neighborchunk);
                }
                else if (contains)//Chunk is already in memory connect to it
                {
                    //Update flag and connect event chain to neighbor
                    chunk.ConsolidateFlags(neighborchunk, 16);
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                }
            }
            //-x +z
            //Get new chunkcoords
            chunk.Coords.Add(-1, 1, out neighborcoords);
            contains = ChunkStorage.Contains(ref neighborcoords, out neighborchunk);
            if ((chunk.NeighborsInMemoryFlag & 32) != 32)
            {
                if (!contains)
                {
                    //Shift this chunks position to get the new chunks relative position
                    newpos.X = chunk.Position.X - Chunk.WIDTH;
                    newpos.Y = chunk.Position.Y;
                    newpos.Z = chunk.Position.Z + Chunk.WIDTH;
                    //Create a new chunk
                    neighborchunk = new Chunk(this, ref neighborcoords, ref newpos);
                    //Update flag and connect event chain to neighbor
                    chunk.NeighborsInMemoryFlag |= 32;
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                    //Add new chunk to manager
                    AddChunk(neighborchunk);
                }
                else if (contains)//Chunk is already in memory connect to it
                {
                    //Update flag and connect event chain to neighbor
                    chunk.ConsolidateFlags(neighborchunk, 32);
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                }
            }
            //+x -z
            //Get new chunkcoords
            chunk.Coords.Add(1, -1, out neighborcoords);
            contains = ChunkStorage.Contains(ref neighborcoords, out neighborchunk);
            if ((chunk.NeighborsInMemoryFlag & 64) != 64)
            {
                if (!contains)
                {
                    //Shift this chunks position to get the new chunks relative position
                    newpos.X = chunk.Position.X + Chunk.WIDTH;
                    newpos.Y = chunk.Position.Y;
                    newpos.Z = chunk.Position.Z - Chunk.WIDTH;
                    //Create a new chunk
                    neighborchunk = new Chunk(this, ref neighborcoords, ref newpos);
                    //Update flag and connect event chain to neighbor
                    chunk.NeighborsInMemoryFlag |= 64;
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                    //Add new chunk to manager
                    AddChunk(neighborchunk);
                }
                else if (contains)//Chunk is already in memory connect to it
                {
                    //Update flag and connect event chain to neighbor
                    chunk.ConsolidateFlags(neighborchunk, 64);
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                }
            }
            //-x -z
            //Get new chunkcoords
            chunk.Coords.Add(-1, -1, out neighborcoords);
            contains = ChunkStorage.Contains(ref neighborcoords, out neighborchunk);
            if ((chunk.NeighborsInMemoryFlag & 128) != 128)
            {
                if (!contains)
                {
                    //Shift this chunks position to get the new chunks relative position
                    newpos.X = chunk.Position.X - Chunk.WIDTH;
                    newpos.Y = chunk.Position.Y;
                    newpos.Z = chunk.Position.Z - Chunk.WIDTH;
                    //Create a new chunk
                    neighborchunk = new Chunk(this, ref neighborcoords, ref newpos);
                    //Update flag and connect event chain to neighbor
                    chunk.NeighborsInMemoryFlag |= 128;
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                    //Add new chunk to manager
                    AddChunk(neighborchunk);
                }
                else if (contains) //Chunk is already in memory connect to it
                {
                    //Update flag and connect event chain to neighbor
                    chunk.ConsolidateFlags(neighborchunk, 128);
                    neighborchunk.StateChanged += chunk.NeighborChangedStateCallback;
                    chunk.StateChanged += neighborchunk.NeighborChangedStateCallback;
                }
            }
        }