public static DeltinScript Generate(string fileName, Pathmap map, OutputLanguage language)
        {
            string baseEditorFile = Extras.CombinePathWithDotNotation(null, "!PathfindEditor.del");

            return(new DeltinScript(new TranslateSettings(baseEditorFile)
            {
                AdditionalRules = (varCollection) => {
                    // Set the initial nodes.
                    Rule initialNodes = new Rule("Initial Nodes");
                    initialNodes.Actions = ArrayBuilder <Element> .Build(
                        // File name HUD.
                        Element.Hud(text: new V_CustomString(fileName), sortOrder: 1, textColor: Color.Orange, location: HudLocation.Right),

                        // Set nodes, segments, and attributes.
                        WorkshopArrayBuilder.SetVariable(null, map.NodesAsWorkshopData(), null, LoadNodes, false),
                        WorkshopArrayBuilder.SetVariable(null, map.SegmentsAsWorkshopData(), null, LoadSegments, false),
                        WorkshopArrayBuilder.SetVariable(null, map.AttributesAsWorkshopData(), null, LoadAttributes, false)
                        );

                    return new Rule[] { initialNodes };
                },
                OptimizeOutput = false,
                OutputLanguage = language
            }));
        }
Example #2
0
        public static Element Create(Pathmap map, int[] attributes)
        {
            var nodeArray = new Element[map.Nodes.Length];

            for (int i = 0; i < nodeArray.Length; i++)
            {
                int[] pathfindResult = Dijkstra(map, attributes, i);
                var   compressed     = CompressIntArray(pathfindResult);

                nodeArray[i] = Element.CreateArray(compressed.Select(s => Element.CustomString(s)).ToArray());
            }

            // Create the final node array.
            return(Element.CreateArray(nodeArray));
        }
        public static void FromPathmapFile(string file)
        {
            DeltinScript deltinScript = Generate(file, Pathmap.ImportFromFile(file), OutputLanguage.enUS);

            string code = deltinScript.WorkshopCode;

            if (code != null)
            {
                Program.WorkshopCodeResult(code);
            }
            else
            {
                Log.Write(LogLevel.Normal, new ColorMod("Build Failed.", ConsoleColor.Red));
                deltinScript.Diagnostics.PrintDiagnostics(Log);
            }
        }
Example #4
0
        private static int[] Dijkstra(Pathmap map, int[] attributes, int node)
        {
            int nodeCount = map.Nodes.Length;

            var unvisited = new List <int>();
            var prev      = new int[map.Nodes.Length];
            var dist      = new double[map.Nodes.Length];

            // Initialize unvisited, prev, and distances.
            for (int i = 0; i < dist.Length; i++)
            {
                dist[i] = double.PositiveInfinity;
                unvisited.Add(i);
            }

            dist[node] = 0;

            while (unvisited.Count > 0)
            {
                var current = unvisited.OrderBy(unvisited => dist[unvisited]).First();
                unvisited.Remove(current);

                var neighbors = map.Segments
                                // Get the list of segments that contain the current node.
                                .Where(segment => segment.Node1 == current || segment.Node2 == current)
                                // Select the opposite node in the segment to get the list of neighbors.
                                .Select(segment => segment.Node1 == current ? segment.Node2 : segment.Node1)
                                // Only use the unvisited neighbors.
                                .Where(node => unvisited.Contains(node));

                foreach (var neighbor in neighbors)
                {
                    var neighborAttributes = map.Attributes.Where(m => m.Node1 == neighbor && m.Node2 == current);

                    var newDist = dist[current] + map.Nodes[current].DistanceTo(map.Nodes[neighbor]);
                    if (newDist < dist[neighbor] && (neighborAttributes.Count() == 0 || neighborAttributes.Any(attribute => attributes.Contains(attribute.Attribute))))
                    {
                        dist[neighbor] = newDist;
                        prev[neighbor] = current + 1;
                    }
                }
            }

            return(prev);
        }
Example #5
0
        public static bool TryLoadFile(string file, out Pathmap pathmap)
        {
            try
            {
                using (var reader = XmlReader.Create(file))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(LegacyPathmap));
                    LegacyPathmap legacy     = (LegacyPathmap)serializer.Deserialize(reader);

                    if ((legacy.Nodes != null && legacy.Nodes.Length > 0) || (legacy.Segments != null && legacy.Segments.Length > 0))
                    {
                        pathmap = legacy.AsPathmap();
                        return(true);
                    }
                    pathmap = null;
                    return(false);
                }
            }
            catch
            {
                pathmap = null;
                return(false);
            }
        }
 public CompressedBakeCacheObject(Pathmap map, int[] attributes)
 {
     _map        = map;
     _attributes = attributes;
 }
 protected override void Update() => Pathmap = Pathmap.ImportFromText(GetContent());