/// <summary> /// Copies a chunk from one location to another. /// </summary> /// <param name="src_cx">The global X-coordinate of the source chunk.</param> /// <param name="src_cz">The global Z-coordinate of the source chunk.</param> /// <param name="dst_cx">The global X-coordinate of the destination chunk.</param> /// <param name="dst_cz">The global Z-coordinate of the destination chunk.</param> /// <returns>A <see cref="ChunkRef"/> for the destination chunk.</returns> public ChunkRef CopyChunk(int src_cx, int src_cz, int dst_cx, int dst_cz) { IRegion src_r = GetRegion(src_cx, src_cz); if (src_r == null) { return(null); } IRegion dst_r = GetRegion(dst_cx, dst_cz); if (dst_r == null) { int rx = dst_cx >> REGION_XLOG; int rz = dst_cz >> REGION_ZLOG; dst_r = _regionMan.CreateRegion(rx, rz); } IChunk c = src_r.GetChunk(src_cx & REGION_XMASK, src_cz & REGION_ZMASK); c.SetLocation(dst_cx, dst_cz); dst_r.SaveChunk(c); return(dst_r.GetChunkRef(dst_cx & REGION_XMASK, dst_cz & REGION_ZMASK)); }
/// <summary> /// Saves an existing <see cref="IChunk"/> 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="IChunk"/> to save to the given location.</param> /// <returns>A <see cref="ChunkRef"/> represneting the <see cref="IChunk"/> 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="IChunk"/>'s internal global coordinates will be updated to reflect the new location.</remarks> public ChunkRef SetChunk(int lcx, int lcz, IChunk chunk) { if (!LocalBoundsCheck(lcx, lcz)) { IRegion 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); using (Stream chunkOutStream = GetChunkOutStream(lcx, lcz)) { chunk.Save(chunkOutStream); } ChunkRef cr = ChunkRef.Create(this, lcx, lcz); _cache.Insert(cr); return(cr); }
/// <inheritdoc/> public ChunkRef SetChunk(int cx, int cz, IChunk 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); }
/// <inheritdoc/> public ChunkRef SetChunk(int cx, int cz, IChunk chunk) { IRegion r = GetRegion(cx, cz); if (r == null) { int rx = cx >> REGION_XLOG; int rz = cz >> REGION_ZLOG; r = _regionMan.CreateRegion(rx, rz); } chunk.SetLocation(cx, cz); r.SaveChunk(chunk); return(r.GetChunkRef(cx & REGION_XMASK, cz & REGION_ZMASK)); }
/// <summary> /// Saves an existing <see cref="IChunk"/> 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="IChunk"/> to save to the given location.</param> /// <returns>A <see cref="ChunkRef"/> represneting the <see cref="IChunk"/> 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="IChunk"/>'s internal global coordinates will be updated to reflect the new location.</remarks> public ChunkRef SetChunk(int lcx, int lcz, IChunk chunk) { if (!LocalBoundsCheck(lcx, lcz)) { IRegion 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; }
/// <summary> /// Replaces the underlying physical chunk with a different one, updating its physical location to reflect the ChunkRef. /// </summary> /// <remarks> /// Use this function to save chunks that have been created or manipulated independently of a container, or to /// move a physical chunk between locations within a container (by taking the reference from another ChunkRef). /// </remarks> /// <param name="chunk">Physical Chunk to store into the location represented by this ChunkRef.</param> public void SetChunkRef(IChunk chunk) { _chunk = chunk; _chunk.SetLocation(X, Z); _dirty = true; }