public IEnumerable <Physical> GetWorldObjects(ChunkCoordinate coord)
        {
            var index = coord.GetUnrolledIndex();
            var ret   = data[index];

            return(ret);
        }
Exemple #2
0
        /// <inheritdoc/>
        Chunk IChunkGenerator.Generate(ChunkCoordinate location)
        {
            // TODO remove short circuit
            if (location.ToWorldPosition().ToVector3().magnitude > 100)
            {
                return(new Chunk(location));
            }

            var world = location.ToWorldPosition();
            var chunk = new Chunk(location);

            for (int x = 0; x < Chunk.Length; x++)
            {
                for (int z = 0; z < Chunk.Length; z++)
                {
                    var noise = _noise.Noise(world.X + x, world.Z + z);
                    chunk[new TileCoordinate(x, z)] = new Tile
                    {
                        Type     = (byte)(noise > 0.5f ? 1 : 0),
                        WallData =
                        {
                            Health = (byte)(noise * byte.MaxValue)
                        }
                    };
                }
            }
            return(chunk);
        }
        /// <summary>
        /// Returns all physicals that are partially or completely inside this chunk
        /// </summary>
        /// <param name="coord"></param>
        /// <returns></returns>
        public IEnumerable <Physical> GetWorldObjects(ChunkCoordinate coord)
        {
            var bb = coord.GetBoundingBox(new Vector3(), radius * 2);

            return
                (TW.Data.Objects.OfType <Physical>()
                 .Where(p => bb.xna().Contains(p.GetBoundingBox().xna()) != ContainmentType.Disjoint));
        }
Exemple #4
0
 public void Add(WorldPosition world, ChunkCoordinate chunk, TileCoordinate tile)
 {
     Add(new Entry()
     {
         WorldPosition   = world,
         ChunkCoordinate = chunk,
         TileCoordinate  = tile,
     });
 }
 private LineManager3DLines getChunkLines(ChunkCoordinate chunkCoordinate)
 {
     return(wireframes.GetOrCreate(chunkCoordinate, delegate
     {
         var ret = new LineManager3DLines(TW.Graphics.Device);
         updateWireframeBox(ret, chunkCoordinate);
         return ret;
     }));
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="size"></param>
 /// <param name="maxDepth">Root has 0 depth</param>
 public OptimizedWorldOctree(Vector3 size, int maxDepth)
 {
     this.size     = size;
     this.maxDepth = maxDepth;
     data          = new List <Physical> [ChunkCoordinate.GetCumulativeNbChunks(maxDepth + 1)];
     for (int i = 0; i < data.Length; i++)
     {
         data[i] = new List <Physical>();
     }
 }
        /// <summary>
        /// Returns all chunks with depth maxdepth for which condition is true.
        /// The condition has as constraint:
        ///     condition(chunk) => condition(parent(chunk))
        /// </summary>
        /// <returns></returns>
        public static ChunkCoordinate FindChunkUp(this IWorldOctree tree, ChunkCoordinate start,
                                                  Func <ChunkCoordinate, bool> condition)
        {
            if (start.IsEmtpy)
            {
                return(start);
            }
            if (condition(start))
            {
                return(start);
            }

            return(tree.FindChunkUp(start.GetParent(), condition));
        }
Exemple #8
0
        private void printChildren(ChunkCoordinate c, int depth)
        {
            for (int i = 0; i < c.Depth; i++)
            {
                Console.Write("---");
            }
            Console.WriteLine(c);
            if (c.Depth == depth)
            {
                return;
            }

            c.GetChildren().ForEach(v => printChildren(v, depth));
        }
        /// <summary>
        /// Returns the CacheData at a specific point from the grid array.  Does not
        /// interfere with the savedChunk data (loading or saving).  Throws an
        /// IndexOutOfRangeException if the specified chunk coordinates are out
        /// of the range of the grid.
        /// </summary>
        /// <param name="chunkX"></param>
        /// <param name="chunkZ"></param>
        /// <returns></returns>
        private CacheData this[int chunkX, int chunkZ]
        {
            get
            {
                lock (CacheLock)
                {
                    ChunkCoordinate coord = new ChunkCoordinate(chunkX, chunkZ);
                    if (savedChunkData.ContainsKey(coord))
                    {
                        return(savedChunkData[coord]);
                    }

                    if (!IsInGridRange(chunkX, chunkZ))
                    {
                        throw new IndexOutOfRangeException();
                    }

                    int xIndex = Numerical.IntMod(chunkX, Width);
                    int zIndex = Numerical.IntMod(chunkZ, Height);

                    return(cache[xIndex, zIndex]);
                }
            }

            set
            {
                lock (CacheLock)
                {
                    if (!IsInGridRange(chunkX, chunkZ))
                    {
                        throw new IndexOutOfRangeException();
                    }

                    int xIndex = Numerical.IntMod(chunkX, Width);
                    int zIndex = Numerical.IntMod(chunkZ, Height);

                    cache[xIndex, zIndex] = value;
                }
            }
        }
        public bool IsInvalid(DynamicBody body)
        {
            var bounds = body.GetGridBounds();

            // OPTIMIZE this could do chunk by chunk instead of looping through grid-item by grid-item.
            Chunk           currentChunk   = null;
            ChunkCoordinate lastCoordinate = ChunkCoordinate.Invalid;

            for (int x = bounds.XMin; x < bounds.XMax; x++)
            {
                for (int y = bounds.YMin; y < bounds.YMax; y++)
                {
                    var coordinate = new GridCoordinate(x, y);

                    var chunkCoordinate = coordinate.ChunkCoordinate;

                    if (!_world.IsValid(chunkCoordinate))
                    {
                        return(true);
                    }

                    if (lastCoordinate != chunkCoordinate)
                    {
                        currentChunk = _world[chunkCoordinate];
                    }

                    var gridItem = currentChunk[coordinate.InnerChunkGridCoordinate];
                    if (gridItem.IsFilled)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
 public BoundingBox GetChunkBoundingBox(ChunkCoordinate chunk)
 {
     return(chunk.GetBoundingBox(new Vector3(), radius * 2));
 }
 public bool IsLeaf(ChunkCoordinate parent)
 {
     return(false); // Infinite tree!
 }
 public Vector3 GetChunkRadius(ChunkCoordinate chunk)
 {
     return(chunk.GetChunkSize(radius * 2) * 0.5f);
 }
        public Vector3 GetChunkCenter(ChunkCoordinate parent)
        {
            var bb = parent.GetBoundingBox(new Vector3(), radius * 2);

            return((bb.Maximum + bb.Minimum) * 0.5f);
        }
 /// <inheritdoc />
 void IChunkLoader.Save(ChunkCoordinate location, Chunk chunk)
 {
     // noop
 }
        public static IEnumerable <ChunkCoordinate> FindChunksDown(this IWorldOctree tree, ChunkCoordinate parent, int maxDepth, Func <ChunkCoordinate, bool> condition)
        {
            if (!condition(parent))
            {
                yield break;
            }

            if (tree.IsLeaf(parent) || parent.Depth == maxDepth)
            {
                yield return(parent);

                yield break;
            }

            foreach (var child in parent.GetChildren())
            {
                foreach (var res in FindChunksDown(tree, child, maxDepth, condition))
                {
                    yield return(res);
                }
            }
        }
        /// <inheritdoc />
        Chunk IChunkLoader.Load(ChunkCoordinate location)
        {
            var chunk = _generator.Generate(location);

            return(chunk);
        }
        public Vector3 GetChunkCenter(ChunkCoordinate chunk)
        {
            var bb = chunk.GetBoundingBox(new Vector3(), size);

            return((bb.Minimum + bb.Maximum) * 0.5f);
        }
 private IEnumerable <IRenderable> getPhysicals(ChunkCoordinate arg)
 {
     return(worldOctree.GetWorldObjects(arg));
 }
 private void updateWireframeBox(LineManager3DLines ret, ChunkCoordinate chunk)
 {
     ret.ClearAllLines();
     worldOctree.GetWorldObjects(chunk)
     .ForEach(p => ret.AddAABB(p.BoundingBox, Matrix.Identity, new Color4(0, 0, 0)));
 }
 public bool IsLeaf(ChunkCoordinate chunk)
 {
     return(chunk.Depth == maxDepth);
 }