Пример #1
0
 public Chunk(byte fill)
 {
     Initialize();
     _position = Int3.Zero;
     for (int xi = 0; xi < Control.CHUNK_SIZE; xi++)
         for (int yi = 0; yi < Control.CHUNK_SIZE; yi++)
             for (int zi = 0; zi < Control.CHUNK_SIZE; zi++)
                 _data[xi, yi, zi] = fill;
     this[CF.isLoaded] = true;
     if (fill != 0x00)
     {
         _numBlocks = Control.BLOCKS_IN_CHUNK;
     }
 }
Пример #2
0
 /// <summary>
 /// Converts from chunk-space to region-space
 /// </summary>
 /// <param name="chunk"></param>
 /// <returns></returns>
 public static Int3 ChunkToRegion(Int3 chunk)
 {
     return chunk % Control.REGION_SIZE;
 }
Пример #3
0
 /// <summary>
 /// Converts from world-space to chunk-space
 /// </summary>
 /// <param name="world"></param>
 /// <returns></returns>
 public static Int3 WorldToChunk(Int3 world)
 {
     return world / Control.CHUNK_SIZE;
 }
Пример #4
0
 /// <summary>
 /// Converts from world-space to block-space.
 /// </summary>
 /// <param name="world"></param>
 /// <returns></returns>
 public static Int3 WorldToBlock(Int3 world)
 {
     return world % Control.CHUNK_SIZE;
 }
Пример #5
0
 public static void Update(Vector3 inOrigin)
 {
     //TODO: Reorganize update function, include visibility call
     bool CheckForUnload = false;
     Int3 temp = (Int3)(inOrigin / Control.CHUNK_SIZE);
     if (_origin != temp)
     {
         _origin = temp;
         CheckForUnload = true;
     }
     for (int x = -VISIBILITY_RADIUS; x < VISIBILITY_RADIUS; x++)
     {
         int x2 = x * x;
         for (int y = -VISIBILITY_RADIUS; y < VISIBILITY_RADIUS; y++)
         {
             int y2 = y * y;
             for (int z = -VISIBILITY_RADIUS; z < VISIBILITY_RADIUS; z++)
             {
                 if ((x2 + y2 + z * z) > VISIBILITY_RADIUS_SQUARED)
                     continue;
                 Chunk c = GetChunk(x + _origin.X, y + _origin.Y, z + _origin.Z);
                 if (c != null)
                     if (c[CF.Load])
                         _Load.Add(c);
             }
         }
     }
     foreach (Chunk c in _AllChunks)
     {
         if (!c[CF.isLoaded])
             continue;
         if (CheckForUnload)
         {
             if ((_origin - c.Position).Square() > UNLOAD_RADIUS_SQUARED)
             {
                 _Unload.Add(c);
                 continue;
             }
         }
         if (c[CF.Build])
             _Rebuild.Add(c);
     }
     UpdateRebuild();
     UpdateLoad();
     UpdateUnload();
     UpdateRender();
 }
Пример #6
0
 public static void SetBlock(Int3 block, byte data)
 {
     Chunk c = GetChunk(WorldToChunk(block));
     if (c == null)
         return;
     c[WorldToBlock(block)] = data;
 }
Пример #7
0
 public static Chunk GetChunk(Int3 chunk)
 {
     //TODO: Optimize GetChunk
     foreach (Chunk c in _AllChunks)
     {
         if (c.Position == chunk)
             return c;
     }
     return null;
 }
Пример #8
0
 public static Block GetBlock(Int3 block)
 {
     Chunk c = GetChunk(WorldToChunk(block));
     if (c == null)
         return null;
     if (!c[CF.isLoaded])
         return null;
     return (c[WorldToBlock(block)]);
 }
Пример #9
0
 /// <summary>
 /// Converts from chunk-space to region-space
 /// </summary>
 /// <param name="chunk"></param>
 /// <returns></returns>
 public static Int3 ChunkToRegion(Int3 chunk)
 {
     return(chunk % Control.REGION_SIZE);
 }
Пример #10
0
 /// <summary>
 /// Converts from world-space to chunk-space
 /// </summary>
 /// <param name="world"></param>
 /// <returns></returns>
 public static Int3 WorldToChunk(Int3 world)
 {
     return(world / Control.CHUNK_SIZE);
 }
Пример #11
0
 /// <summary>
 /// Converts from world-space to block-space.
 /// </summary>
 /// <param name="world"></param>
 /// <returns></returns>
 public static Int3 WorldToBlock(Int3 world)
 {
     return(world % Control.CHUNK_SIZE);
 }
Пример #12
0
        private int WriteSubdiv(Int3 min, int size)
        {
            bool Subdivide = false;
            byte cData = _chunk[min];
            if (size > 1)
            {
                for (int x = min.X; x < min.X + size; x++)
                {
                    for (int y = min.Y; y < min.Y + size; y++)
                    {
                        for (int z = min.Z; z < min.Z + size; z++)
                        {
                            if (cData != _chunk[x, y, z])
                            {
                                Subdivide = true;
                                break;
                            }
                        }
                        if (Subdivide)
                            break;
                    }
                    if (Subdivide)
                        break;
                }
                if (Subdivide)
                {
                    long headerPosition = _filestream.Position;
                    byte header = (byte)CSF.None;
                    _filestream.Position += 1;
                    int nSize = size / 2;
                    Int3 mid = min + Int3.One * nSize;
                    int retValue;
                    #region LBF
                    retValue = WriteSubdiv(min, nSize);
                    if (retValue == -1)
                        return -1;
                    else if (retValue == 1)
                        header |= (byte)CSF.LBF;
                    #endregion
                    #region LBB
                    retValue = WriteSubdiv(new Int3(min.X, min.Y, mid.Z), nSize);
                    if (retValue == -1)
                        return -1;
                    else if (retValue == 1)
                        header |= (byte)CSF.LBB;
                    #endregion
                    #region LTF
                    retValue = WriteSubdiv(new Int3(min.X, mid.Y, min.Z), nSize);
                    if (retValue == -1)
                        return -1;
                    else if (retValue == 1)
                        header |= (byte)CSF.LTF;
                    #endregion
                    #region LTB
                    retValue = WriteSubdiv(new Int3(min.X, mid.Y, mid.Z), nSize);
                    if (retValue == -1)
                        return -1;
                    else if (retValue == 1)
                        header |= (byte)CSF.LTB;
                    #endregion
                    #region RBF
                    retValue = WriteSubdiv(new Int3(mid.X, min.Y, min.Z), nSize);
                    if (retValue == -1)
                        return -1;
                    else if (retValue == 1)
                        header |= (byte)CSF.RBF;
                    #endregion
                    #region RBB
                    retValue = WriteSubdiv(new Int3(mid.X, min.Y, mid.Z), nSize);
                    if (retValue == -1)
                        return -1;
                    else if (retValue == 1)
                        header |= (byte)CSF.RBB;
                    #endregion
                    #region RTF
                    retValue = WriteSubdiv(new Int3(mid.X, mid.Y, min.Z), nSize);
                    if (retValue == -1)
                        return -1;
                    else if (retValue == 1)
                        header |= (byte)CSF.RTF;
                    #endregion
                    #region RTB
                    retValue = WriteSubdiv(mid, nSize);
                    if (retValue == -1)
                        return -1;
                    else if (retValue == 1)
                        header |= (byte)CSF.RTB;
                    #endregion
                    long endPos = _filestream.Position;
                    _filestream.Position = headerPosition;
                    _filestream.WriteByte(header);
                    _filestream.Position = endPos;
                }
                else
                {
                    _filestream.WriteByte(cData);
                }
            }
            else
            {
                _filestream.WriteByte(cData);
            }

            //If the file is larger because of this encoding, return false.
            if (_filestream.Position - _pos > Control.BLOCKS_IN_CHUNK)
                return -1;

            if (Subdivide)
                return 1;
            else
                return 0;
        }
Пример #13
0
        private int WriteSubdiv(Int3 min, int size)
        {
            bool Subdivide = false;
            byte cData     = _chunk[min];

            if (size > 1)
            {
                for (int x = min.X; x < min.X + size; x++)
                {
                    for (int y = min.Y; y < min.Y + size; y++)
                    {
                        for (int z = min.Z; z < min.Z + size; z++)
                        {
                            if (cData != _chunk[x, y, z])
                            {
                                Subdivide = true;
                                break;
                            }
                        }
                        if (Subdivide)
                        {
                            break;
                        }
                    }
                    if (Subdivide)
                    {
                        break;
                    }
                }
                if (Subdivide)
                {
                    long headerPosition = _filestream.Position;
                    byte header         = (byte)CSF.None;
                    _filestream.Position += 1;
                    int  nSize = size / 2;
                    Int3 mid   = min + Int3.One * nSize;
                    int  retValue;
                    #region LBF
                    retValue = WriteSubdiv(min, nSize);
                    if (retValue == -1)
                    {
                        return(-1);
                    }
                    else if (retValue == 1)
                    {
                        header |= (byte)CSF.LBF;
                    }
                    #endregion
                    #region LBB
                    retValue = WriteSubdiv(new Int3(min.X, min.Y, mid.Z), nSize);
                    if (retValue == -1)
                    {
                        return(-1);
                    }
                    else if (retValue == 1)
                    {
                        header |= (byte)CSF.LBB;
                    }
                    #endregion
                    #region LTF
                    retValue = WriteSubdiv(new Int3(min.X, mid.Y, min.Z), nSize);
                    if (retValue == -1)
                    {
                        return(-1);
                    }
                    else if (retValue == 1)
                    {
                        header |= (byte)CSF.LTF;
                    }
                    #endregion
                    #region LTB
                    retValue = WriteSubdiv(new Int3(min.X, mid.Y, mid.Z), nSize);
                    if (retValue == -1)
                    {
                        return(-1);
                    }
                    else if (retValue == 1)
                    {
                        header |= (byte)CSF.LTB;
                    }
                    #endregion
                    #region RBF
                    retValue = WriteSubdiv(new Int3(mid.X, min.Y, min.Z), nSize);
                    if (retValue == -1)
                    {
                        return(-1);
                    }
                    else if (retValue == 1)
                    {
                        header |= (byte)CSF.RBF;
                    }
                    #endregion
                    #region RBB
                    retValue = WriteSubdiv(new Int3(mid.X, min.Y, mid.Z), nSize);
                    if (retValue == -1)
                    {
                        return(-1);
                    }
                    else if (retValue == 1)
                    {
                        header |= (byte)CSF.RBB;
                    }
                    #endregion
                    #region RTF
                    retValue = WriteSubdiv(new Int3(mid.X, mid.Y, min.Z), nSize);
                    if (retValue == -1)
                    {
                        return(-1);
                    }
                    else if (retValue == 1)
                    {
                        header |= (byte)CSF.RTF;
                    }
                    #endregion
                    #region RTB
                    retValue = WriteSubdiv(mid, nSize);
                    if (retValue == -1)
                    {
                        return(-1);
                    }
                    else if (retValue == 1)
                    {
                        header |= (byte)CSF.RTB;
                    }
                    #endregion
                    long endPos = _filestream.Position;
                    _filestream.Position = headerPosition;
                    _filestream.WriteByte(header);
                    _filestream.Position = endPos;
                }
                else
                {
                    _filestream.WriteByte(cData);
                }
            }
            else
            {
                _filestream.WriteByte(cData);
            }

            //If the file is larger because of this encoding, return false.
            if (_filestream.Position - _pos > Control.BLOCKS_IN_CHUNK)
            {
                return(-1);
            }

            if (Subdivide)
            {
                return(1);
            }
            else
            {
                return(0);
            }
        }
Пример #14
0
 public Chunk(Int3 pos)
 {
     Initialize();
     _position = pos;
     translation = Matrix.CreateTranslation(Position * Control.CHUNK_SIZE);
 }
Пример #15
0
 public byte this[Int3 a]
 {
     get
     {
         return this[a.X, a.Y, a.Z];
     }
     set
     {
         this[a.X, a.Y, a.Z] = value;
     }
 }