From 63d1e8411f5d1689f643a4d9e09384db17ee4e91 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 1 Jul 2015 11:45:29 -0600 Subject: [PATCH] Fix merging and enumerable issues --- TrueCraft.Core/Lighting/WorldLighting.cs | 16 ++++++++++------ TrueCraft.Core/World/World.cs | 12 +++++------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/TrueCraft.Core/Lighting/WorldLighting.cs b/TrueCraft.Core/Lighting/WorldLighting.cs index c6ccb43..c14e306 100644 --- a/TrueCraft.Core/Lighting/WorldLighting.cs +++ b/TrueCraft.Core/Lighting/WorldLighting.cs @@ -43,7 +43,11 @@ namespace TrueCraft.Core.Lighting HeightMaps = new Dictionary(); world.ChunkGenerated += (sender, e) => GenerateHeightMap(e.Chunk); world.ChunkLoaded += (sender, e) => GenerateHeightMap(e.Chunk); - world.BlockChanged += (sender, e) => UpdateHeightMap(e.Position); + world.BlockChanged += (sender, e) => + { + if (e.NewBlock.ID != e.OldBlock.ID) + UpdateHeightMap(e.Position); + }; foreach (var chunk in world) GenerateHeightMap(chunk); } @@ -74,6 +78,8 @@ namespace TrueCraft.Core.Lighting { IChunk chunk; var adjusted = World.FindBlockPosition(coords, out chunk, generate: false); + if (!HeightMaps.ContainsKey(chunk.Coordinates)) + return; var map = HeightMaps[chunk.Coordinates]; int x = adjusted.X; int z = adjusted.Z; for (byte y = Chunk.Height - 1; y > 0; y--) @@ -241,12 +247,10 @@ namespace TrueCraft.Core.Lighting for (int i = PendingOperations.Count - 1; i > PendingOperations.Count - 5 && i > 0; i--) { var op = PendingOperations[i]; - if (op.Box.Min.DistanceTo(box.Min) <= 2 && op.Box.Max.DistanceTo(box.Max) <= 2 - && op.Box.Volume - box.Volume <= 2) + if (op.Box.Intersects(box)) { - // TODO: Merge - //op.Box = new BoundingBox(Vector3.Min(op.Box.Min, box.Min), Vector3.Max(op.Box.Max, box.Max)); - //return; + op.Box = new BoundingBox(Vector3.Min(op.Box.Min, box.Min), Vector3.Max(op.Box.Max, box.Max)); + return; } } PendingOperations.Add(new LightingOperation { SkyLight = skyLight, Box = box, Initial = initial }); diff --git a/TrueCraft.Core/World/World.cs b/TrueCraft.Core/World/World.cs index 4dd7e8e..df1ef78 100644 --- a/TrueCraft.Core/World/World.cs +++ b/TrueCraft.Core/World/World.cs @@ -381,18 +381,16 @@ namespace TrueCraft.Core.World { public World World { get; set; } private int Index { get; set; } - private IList Chunks { get; set; } + private IList Chunks { get; set; } public ChunkEnumerator(World world) { World = world; - Index = 0; + Index = -1; var regions = world.Regions.Values.ToList(); - var chunks = new List(); + var chunks = new List(); foreach (var region in regions) - { - chunks.AddRange((IEnumerable)region.Chunks.Values); - } + chunks.AddRange(region.Chunks.Values); Chunks = chunks; } @@ -404,7 +402,7 @@ namespace TrueCraft.Core.World public void Reset() { - Index = 0; + Index = -1; } public void Dispose()