Beispiel #1
0
    public static Vec2I NaturalStep(Vec2I[] path, int maxSteps)
    {
        if (path.Length == 0)
        {
            return(Vec2I.zero);
        }
        if (path.Length < 3)
        {
            return(path[1]);
        }
        if (maxSteps < 2)
        {
            return(path[1]);
        }

        Vec2I[] lastLine = new Vec2I[0];

        int steps = 0;
        int e     = 2;

again:
        Vec2I[] line = AxMath.CartesianLine(path[0], path[e]);

        for (int i = 0; i < line.Length; i++)
        {
            HeatmapNode n = Heatmap.GetNode(line[i]);
            if (n == null)
            {
                goto failed;
            }
            if (!n.WideOpen)
            {
                goto failed;
            }
        }

        if (steps <= maxSteps)
        {
            if (e == path.Length - 1)
            {
                return(path[e]);
            }

            lastLine = line;
            steps++;
            e++;

            goto again;
        }

failed:
        if (e > 2)
        {
            return(lastLine[lastLine.Length - 1]);
        }

        return(path[1]);
    }
Beispiel #2
0
    public static bool CanSeeGridRay(Vec2I from, Vec2I to)
    {
        foreach (Vec2I step in AxMath.CartesianLine(from, to))
        {
            HeatmapNode node = Heatmap.GetNode(step);
            if (node == null)
            {
                return(false);
            }
            if (node.HasWall)
            {
                return(false);
            }
        }

        return(true);
    }
Beispiel #3
0
    public static void NaturalizePath(ref Vec2I[] path, int maxSteps)
    {
        if (path.Length < 3)
        {
            return;
        }
        if (maxSteps < 2)
        {
            return;
        }

        SingleLinkedList <Vec2I> naturalized = new SingleLinkedList <Vec2I>();

        naturalized.InsertBack(path[0]);

        int s = 0;
        int e = 2;

        Vec2I[] lastLine = new Vec2I[0];

        while (e < path.Length)
        {
            int steps = 0;

again:
            Vec2I[] line = AxMath.CartesianLine(path[s], path[e]);

            for (int i = 0; i < line.Length; i++)
            {
                HeatmapNode n = Heatmap.GetNode(line[i]);
                if (n == null)
                {
                    goto failed;
                }
                if (!n.WideOpen)
                {
                    goto failed;
                }
            }

            if (steps <= maxSteps)
            {
                if (e == path.Length - 1)
                {
                    naturalized.InsertBack(path[e]);
                    path = naturalized.ToArray();
                    return;
                }

                lastLine = line;
                steps++;
                e++;

                goto again;
            }

failed:
            if (e > s + 2)
            {
                naturalized.InsertBack(lastLine[lastLine.Length - 1]);

                s = e;
                e = s + 2;
                continue;
            }

            naturalized.InsertBack(path[++s]);
            e = s + 2;
        }

        while (s < path.Length)
        {
            naturalized.InsertBack(path[s++]);
        }

        path = naturalized.ToArray();
    }