Ejemplo n.º 1
0
        // XXX: Consider revising foreign lookup support
        /// <summary>
        /// Gets a <see cref="ChunkRef"/> for a chunk at the given local coordinates relative to this region.
        /// </summary>
        /// <param name="lcx">The local X-coordinate of a chunk relative to this region.</param>
        /// <param name="lcz">The local Z-coordinate of a chunk relative to this region.</param>
        /// <returns>A <see cref="ChunkRef"/> at the given local coordinates, or null if no chunk exists.</returns>
        /// <remarks>The local coordinates do not strictly need to be within the bounds of the region.  If coordinates are detected
        /// as being out of bounds, the lookup will be delegated to the correct region and the lookup will be performed there
        /// instead.  This allows any <see cref="Region"/> to perform a similar task to <see cref="BetaChunkManager"/>, but with a
        /// region-local frame of reference instead of a global frame of reference.</remarks>
        public ChunkRef GetChunkRef(int lcx, int lcz)
        {
            if (!LocalBoundsCheck(lcx, lcz))
            {
                Region alt = GetForeignRegion(lcx, lcz);
                return((alt == null) ? null : alt.GetChunkRef(ForeignX(lcx), ForeignZ(lcz)));
            }

            int cx = lcx + _rx * XDIM;
            int cz = lcz + _rz * ZDIM;

            ChunkKey k = new ChunkKey(cx, cz);
            ChunkRef c = _cache.Fetch(k);

            if (c != null)
            {
                return(c);
            }

            c = ChunkRef.Create(this, lcx, lcz);
            if (c != null)
            {
                _cache.Insert(c);
            }

            return(c);
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public ChunkRef SetChunk(int cx, int cz, Chunk chunk)
        {
            DeleteChunk(cx, cz);
            chunk.SetLocation(cx, cz);
            chunk.Save(GetChunkOutStream(cx, cz));

            ChunkRef cr = ChunkRef.Create(this, cx, cz);
            ChunkKey k  = new ChunkKey(cx, cz);

            _cache[k] = cr;

            return(cr);
        }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        public ChunkRef CreateChunk(int cx, int cz)
        {
            DeleteChunk(cx, cz);
            Chunk c = Chunk.Create(cx, cz);

            c.Save(GetChunkOutStream(cx, cz));

            ChunkRef cr = ChunkRef.Create(this, cx, cz);
            ChunkKey k  = new ChunkKey(cx, cz);

            _cache[k] = cr;

            return(cr);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Saves an existing <see cref="Chunk"/> to the region at the given local coordinates.
        /// </summary>
        /// <param name="lcx">The local X-coordinate of a chunk relative to this region.</param>
        /// <param name="lcz">The local Z-coordinate of a chunk relative to this region.</param>
        /// <param name="chunk">A <see cref="Chunk"/> to save to the given location.</param>
        /// <returns>A <see cref="ChunkRef"/> represneting the <see cref="Chunk"/> at its new location.</returns>
        /// <remarks>If the local coordinates are out of bounds for this region, the action will be forwarded to the correct region
        /// transparently.  The <see cref="Chunk"/>'s internal global coordinates will be updated to reflect the new location.</remarks>
        public ChunkRef SetChunk (int lcx, int lcz, Chunk chunk)
        {
            if (!LocalBoundsCheck(lcx, lcz)) {
                Region alt = GetForeignRegion(lcx, lcz);
                return (alt == null) ? null : alt.CreateChunk(ForeignX(lcx), ForeignZ(lcz));
            }

            DeleteChunk(lcx, lcz);

            int cx = lcx + _rx * XDIM;
            int cz = lcz + _rz * ZDIM;

            chunk.SetLocation(cx, cz);
            chunk.Save(GetChunkOutStream(lcx, lcz));

            ChunkRef cr = ChunkRef.Create(this, lcx, lcz);
            _cache.Insert(cr);

            return cr;
        }
Ejemplo n.º 5
0
        /// <inheritdoc/>
        public ChunkRef GetChunkRef(int cx, int cz)
        {
            ChunkKey k = new ChunkKey(cx, cz);

            ChunkRef c = null;

            //WeakReference chunkref = null;
            if (_cache.TryGetValue(k, out c))
            {
                return(c);
            }

            c = ChunkRef.Create(this, cx, cz);
            if (c != null)
            {
                _cache[k] = c;
            }

            return(c);
        }