Пример #1
0
    // 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);
    }
Пример #2
0
    public static void Recompute(Vector3i pos, Queue <Vector3i> nodes)
    {
        int oldSunHeight = MapLight.GetRay(pos.x, pos.z);

        ComputeRayAtPosition(pos.x, pos.z);
        int newSunHeight = MapLight.GetRay(pos.x, pos.z);

        if (newSunHeight < oldSunHeight)
        {
            for (int ty = newSunHeight; ty <= oldSunHeight; ty++)
            {
                pos.y = ty;

                if (ty < Map.Height)
                {
                    MapLight.SetSunlight(pos.x, pos.y, pos.z, LightUtils.MinLight);
                }

                nodes.Enqueue(pos);
            }

            ScatterNodes(nodes, false);
            return;
        }

        if (newSunHeight > oldSunHeight)
        {
            for (int ty = oldSunHeight; ty <= newSunHeight; ty++)
            {
                pos.y = ty;

                if (ty < Map.Height)
                {
                    MapLight.SetSunlight(pos.x, pos.y, pos.z, LightUtils.MaxLight);
                }

                nodes.Enqueue(pos);
            }

            RemoveNodes(nodes);
            return;
        }

        if (newSunHeight == oldSunHeight)
        {
            if (Map.GetBlock(pos.x, pos.y, pos.z).IsTransparent())
            {
                Update(pos, nodes);
            }
            else
            {
                Remove(pos, nodes);
            }
        }
    }
Пример #3
0
    private static void RemoveNodes(Queue <Vector3i> nodes)
    {
        Queue <Vector3i> newNodes = new Queue <Vector3i>();

        while (nodes.Count > 0)
        {
            Vector3i pos = nodes.Dequeue();

            if (pos.y < 0 || pos.y >= Map.Height)
            {
                continue;
            }

            if (pos.y >= MapLight.GetRay(pos.x, pos.z))
            {
                newNodes.Enqueue(pos);
                continue;
            }

            byte light = (byte)(MapLight.GetSunlight(pos.x, pos.y, pos.z) - 1);
            MapLight.SetSunlight(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.GetSunlight(nextPos.x, nextPos.y, nextPos.z) <= light)
                        {
                            nodes.Enqueue(nextPos);
                            Map.FlagChunkForUpdate(nextPos.x, nextPos.y, nextPos.z);
                        }
                        else
                        {
                            newNodes.Enqueue(nextPos);
                        }
                    }
                }
            }
        }

        ScatterNodes(newNodes, false);
    }
Пример #4
0
    private static bool SetMax(byte light, int x, int y, int z)
    {
        if (y >= MapLight.GetRay(x, z))
        {
            return(false);
        }

        byte oldLight = MapLight.GetSunlight(x, y, z);

        if (oldLight < light)
        {
            MapLight.SetSunlight(x, y, z, light);
            return(true);
        }

        return(false);
    }
Пример #5
0
    protected Vector3 TryFindLand(Vector3i center)
    {
        Vector3i?land = null;

        for (int x = center.x - 20; x <= center.x + 20; x++)
        {
            for (int z = center.z - 20; z <= center.z + 20; z++)
            {
                int   height  = MapLight.GetRaySafe(x, z);
                Block surface = Map.GetBlockSafe(x, height - 1, z);

                if (!surface.IsFluid())
                {
                    Vector3i current = new Vector3i(x, height, z);

                    if (!land.HasValue)
                    {
                        land = current;
                    }
                    else
                    {
                        int dis    = center.DistanceSquared(current);
                        int oldDis = center.DistanceSquared(land.Value);

                        if (dis < oldDis)
                        {
                            land = current;
                        }
                    }
                }
            }
        }

        if (land.HasValue)
        {
            return(land.Value.ToVector3());
        }

        return(new Vector3(center.x, MapLight.GetRay(center.x, center.z), center.z));
    }