From f7ffe718a79d009aa8a612eecd1b24af57480dd0 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 4 Jul 2015 08:41:27 -0600 Subject: [PATCH] Initial pass on lighting optimizations --- .../Lighting/WorldLighterTest.cs | 8 ++++++++ TrueCraft.Core/Lighting/WorldLighting.cs | 20 +++++++++++++++---- .../TerrainGen/FlatlandGenerator.cs | 1 + .../TerrainGen/StandardGenerator.cs | 1 + TrueCraft/MultiplayerServer.cs | 4 ++++ 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/TrueCraft.Core.Test/Lighting/WorldLighterTest.cs b/TrueCraft.Core.Test/Lighting/WorldLighterTest.cs index f5639e9..c0bdbd8 100644 --- a/TrueCraft.Core.Test/Lighting/WorldLighterTest.cs +++ b/TrueCraft.Core.Test/Lighting/WorldLighterTest.cs @@ -7,6 +7,7 @@ using TrueCraft.Core.Logic; using TrueCraft.API; using TrueCraft.Core.World; using TrueCraft.Core.Logic.Blocks; +using System.Diagnostics; namespace TrueCraft.Core.Test.Lighting { @@ -191,6 +192,9 @@ namespace TrueCraft.Core.Test.Lighting world.SetBlockID(new Coordinates3D(x, 1, 5), 0); // Dig a tunnel } + var watch = new Stopwatch(); + watch.Start(); + lighter.EnqueueOperation(new BoundingBox(new Vector3(5, 2, 5), new Vector3(6, 4, 6)), true); @@ -198,6 +202,8 @@ namespace TrueCraft.Core.Test.Lighting { } + watch.Stop(); + // Output lighting for (int y = 3; y >= 0; y--) { @@ -238,6 +244,8 @@ namespace TrueCraft.Core.Test.Lighting Assert.AreEqual(expected, world.GetSkyLight(new Coordinates3D(x, 1, 5))); expected--; } + + Console.WriteLine("{0}ms", watch.ElapsedMilliseconds); } [Test] diff --git a/TrueCraft.Core/Lighting/WorldLighting.cs b/TrueCraft.Core/Lighting/WorldLighting.cs index c14e306..ba24fa1 100644 --- a/TrueCraft.Core/Lighting/WorldLighting.cs +++ b/TrueCraft.Core/Lighting/WorldLighting.cs @@ -54,14 +54,20 @@ namespace TrueCraft.Core.Lighting private void GenerateHeightMap(IChunk chunk) { + Coordinates3D coords; var map = new byte[Chunk.Width, Chunk.Depth]; for (int x = 0; x < Chunk.Width; x++) { for (int z = 0; z < Chunk.Depth; z++) { - for (byte y = Chunk.Height - 1; y > 0; y--) + for (byte y = (byte)(chunk.GetHeight((byte)x, (byte)z) + 2); y > 0; y--) { - var id = chunk.GetBlockID(new Coordinates3D(x, y - 1, z)); + if (y >= Chunk.Height) + continue; + coords.X = x; coords.Y = y - 1; coords.Z = z; + var id = chunk.GetBlockID(coords); + if (id == 0) + continue; var provider = BlockRepository.GetBlockProvider(id); if (provider.LightOpacity != 0) { @@ -82,9 +88,15 @@ namespace TrueCraft.Core.Lighting return; var map = HeightMaps[chunk.Coordinates]; int x = adjusted.X; int z = adjusted.Z; - for (byte y = Chunk.Height - 1; y > 0; y--) + Coordinates3D _; + for (byte y = (byte)(chunk.GetHeight((byte)x, (byte)z) + 2); y > 0; y--) { - var id = chunk.GetBlockID(new Coordinates3D(x, y - 1, z)); + if (y >= Chunk.Height) + continue; + _.X = x; _.Y = y - 1; _.Z = z; + var id = chunk.GetBlockID(_); + if (id == 0) + continue; var provider = BlockRepository.GetBlockProvider(id); if (provider.LightOpacity != 0) { diff --git a/TrueCraft.Core/TerrainGen/FlatlandGenerator.cs b/TrueCraft.Core/TerrainGen/FlatlandGenerator.cs index a0b7abb..b549906 100644 --- a/TrueCraft.Core/TerrainGen/FlatlandGenerator.cs +++ b/TrueCraft.Core/TerrainGen/FlatlandGenerator.cs @@ -82,6 +82,7 @@ namespace TrueCraft.Core.TerrainGen for (int i = 0; i < chunk.Biomes.Length; i++) chunk.Biomes[i] = (byte)Biome; chunk.TerrainPopulated = true; + chunk.UpdateHeightMap(); return chunk; } diff --git a/TrueCraft.Core/TerrainGen/StandardGenerator.cs b/TrueCraft.Core/TerrainGen/StandardGenerator.cs index d2ea928..a61cde5 100644 --- a/TrueCraft.Core/TerrainGen/StandardGenerator.cs +++ b/TrueCraft.Core/TerrainGen/StandardGenerator.cs @@ -189,6 +189,7 @@ namespace TrueCraft.Core.TerrainGen foreach (var decorator in ChunkDecorators) decorator.Decorate(world, chunk, Biomes); chunk.TerrainPopulated = true; + chunk.UpdateHeightMap(); return chunk; } diff --git a/TrueCraft/MultiplayerServer.cs b/TrueCraft/MultiplayerServer.cs index 905c283..67617f4 100644 --- a/TrueCraft/MultiplayerServer.cs +++ b/TrueCraft/MultiplayerServer.cs @@ -192,11 +192,15 @@ namespace TrueCraft { var lighter = new WorldLighting(sender as IWorld, BlockRepository); var coords = e.Coordinates * new Coordinates2D(Chunk.Width, Chunk.Depth); + var watch = new Stopwatch(); + watch.Start(); lighter.EnqueueOperation(new BoundingBox(new Vector3(coords.X, 0, coords.Z), new Vector3(coords.X + Chunk.Width, Chunk.Height, coords.Z + Chunk.Depth)), true, true); while (lighter.TryLightNext()) // Initial lighting { } + watch.Stop(); + Console.WriteLine("Initial chunk lighting took {0}ms", watch.ElapsedMilliseconds); } else {