// Scatters the initial light throughout the map. public static void Scatter(int worldX, int worldZ, Queue <Vector3i> sunNodes, Queue <Vector3i> lightNodes) { for (int x = worldX; x < worldX + Chunk.Size; x++) { for (int z = worldZ; z < worldZ + Chunk.Size; z++) { int ray = MapLight.GetRay(x, z); int maxY = ComputeMaxY(x, z, ray); for (int y = ray; y <= maxY; y++) { if (y >= Map.Height) { continue; } sunNodes.Enqueue(new Vector3i(x, y, z)); byte light = Map.GetBlock(x, y, z).LightEmitted(); if (light > LightUtils.MinLight) { MapLight.SetLight(x, y, z, light); lightNodes.Enqueue(new Vector3i(x, y, z)); } } } } ScatterNodes(sunNodes, true); BlockLightEngine.ScatterNodes(lightNodes, true); }
private static void RemoveNodes(Queue <Vector3i> nodes) { Queue <Vector3i> newLights = new Queue <Vector3i>(); while (nodes.Count > 0) { Vector3i pos = nodes.Dequeue(); if (pos.y < 0 || pos.y >= Map.Height) { continue; } int light = MapLight.GetLight(pos.x, pos.y, pos.z) - 1; MapLight.SetLight(pos.x, pos.y, pos.z, LightUtils.MinLight); if (light <= LightUtils.MinLight) { continue; } for (int i = 0; i < 6; i++) { Vector3i nextPos = pos + Vector3i.directions[i]; if (Map.InBounds(nextPos.x, nextPos.z)) { Block block = Map.GetBlock(nextPos.x, nextPos.y, nextPos.z); if (block.IsTransparent()) { if (MapLight.GetLight(nextPos.x, nextPos.y, nextPos.z) <= light) { nodes.Enqueue(nextPos); } else { newLights.Enqueue(nextPos); } } if (block.LightEmitted() > LightUtils.MinLight) { newLights.Enqueue(nextPos); } Map.FlagChunkForUpdate(nextPos.x, nextPos.y, nextPos.z); } } } ScatterNodes(newLights, false); }
public static bool SetMax(byte light, int x, int y, int z) { byte oldLight = MapLight.GetLight(x, y, z); if (oldLight < light) { MapLight.SetLight(x, y, z, light); return(true); } return(false); }
public static void Recompute(Vector3i pos, Queue <Vector3i> nodes) { byte oldLight = MapLight.GetLight(pos.x, pos.y, pos.z); byte light = Map.GetBlock(pos.x, pos.y, pos.z).LightEmitted(); if (oldLight > light) { Remove(pos, nodes); } if (light > LightUtils.MinLight) { MapLight.SetLight(pos.x, pos.y, pos.z, light); Scatter(pos, nodes); } else { Update(pos, nodes); } }
private static void Remove(Vector3i pos, Queue <Vector3i> nodes) { MapLight.SetLight(pos.x, pos.y, pos.z, LightUtils.MaxLight); nodes.Enqueue(pos); RemoveNodes(nodes); }