Example #1
0
        public List <SpecialConnection> LoadSpecialConnections()
        {
            DAL.SpecialConnectionDA.ISpecialConnectionDA specialConnectionDA = new DAL.SpecialConnectionDA.SpecialConnectionDAO();
            List <DAL.SpecialConnection>    tmp = specialConnectionDA.GetSpecialConnections();
            List <Entity.SpecialConnection> res = new List <SpecialConnection>();

            foreach (DAL.SpecialConnection sc in tmp)
            {
                Entity.SpecialConnection tsc = new SpecialConnection();
                tsc.DAL_SetSpecialConnection(sc);
                tsc.MapItemFromEntity = _map.FastFinder[sc.MapItemFrom];
                tsc.MapItemToEntity   = _map.FastFinder[sc.MapItemTo];
                res.Add(tsc);
            }
            this._DAL_SpecialConnections = tmp;
            this._map.SpecialConnections = res;
            return(res);
        }
Example #2
0
        public List<SpecialConnection> CreateSpecialConnections(List<Node[,]> nodeLists)
        {
            Stopwatch sw;

            List<SpecialConnection> specialConnections = new List<SpecialConnection>();

            sw = Stopwatch.StartNew();
            // Add connections between floors created by stairs/rope holes (these are added if both image (i) and image (i + 1) have the color yellow at a pixel)
            for (int x = 0; x < mapImages[7].Width; x++) {
                for (int y = 0; y < mapImages[7].Height; y++) {
                    for (int i = 0; i < 15; i++) {
                        if (nodeLists[i][x, y] != null && nodeLists[i][x, y].color == stairsColor) {
                            Node other = null;
                            if (nodeLists[i + 1][x, y] != null && nodeLists[i + 1][x, y].color == stairsColor) {
                                other = nodeLists[i + 1][x, y];
                            } else if (nodeLists[i + 1][x + 1, y] != null && nodeLists[i + 1][x + 1, y].color == stairsColor) {
                                other = nodeLists[i + 1][x + 1, y];
                            } else if (nodeLists[i + 1][x - 1, y] != null && nodeLists[i + 1][x - 1, y].color == stairsColor) {
                                other = nodeLists[i + 1][x - 1, y];
                            } else if (nodeLists[i + 1][x, y - 1] != null && nodeLists[i + 1][x, y - 1].color == stairsColor) {
                                other = nodeLists[i + 1][x, y - 1];
                            } else if (nodeLists[i + 1][x, y + 1] != null && nodeLists[i + 1][x, y + 1].color == stairsColor) {
                                other = nodeLists[i + 1][x, y + 1];
                            }
                            if (other != null) {
                                SpecialConnection connection = new SpecialConnection { source = nodeLists[i][x, y], destination = other, cost = 50, name = "Stairs" };
                                SpecialConnection connection2 = new SpecialConnection { source = other, destination = nodeLists[i][x, y], cost = 50, name = "Stairs" };
                                specialConnections.Add(connection);
                                specialConnections.Add(connection2);
                                nodeLists[i][x, y].addNeighbor(other, connection);
                                other.addNeighbor(nodeLists[i][x, y], connection2);
                            }
                        }
                    }
                }
            }

            // Add connections that are stored in the teleports.xml file
            // These are teleports that cannot be deduced from the image files themselves (i.e. actual teleporters, boats)
            using (StreamReader str = new StreamReader("teleports.xml")) {
                using (XmlReader reader = XmlReader.Create(str)) {
                    Node startNode = null, endNode = null;
                    string currentNode = null;
                    string startName = "";
                    Dictionary<string, string> attributes = new Dictionary<string, string>();
                    while (reader.Read()) {
                        switch (reader.NodeType) {
                            case XmlNodeType.Element:
                                currentNode = reader.Name;
                                attributes.Clear();
                                if (currentNode == "Destination") {
                                    attributes.Add("Name", reader.GetAttribute("Name"));
                                    attributes.Add("Cost", reader.GetAttribute("Cost"));
                                }
                                break;
                            case XmlNodeType.Text:
                                if (currentNode == "Name") {
                                    startName = reader.Value;
                                } else if (currentNode == "Start") {
                                    string[] split = reader.Value.Split(',');
                                    int x = int.Parse(split[0]);
                                    int y = int.Parse(split[1]);
                                    int z = int.Parse(split[2]);
                                    startNode = nodeLists[z][x, y];
                                } else if (currentNode == "Destination") {
                                    string name = startName + " to " + attributes["Name"];
                                    int cost = int.Parse(attributes["Cost"]);
                                    string[] split = reader.Value.Split(',');
                                    int x = int.Parse(split[0]);
                                    int y = int.Parse(split[1]);
                                    int z = int.Parse(split[2]);
                                    endNode = nodeLists[z][x, y];

                                    SpecialConnection connection = new SpecialConnection { source = startNode, destination = endNode, cost = cost, name = name };
                                    SpecialConnection connection2 = new SpecialConnection { source = endNode, destination = startNode, cost = cost, name = ReverseName(name) };
                                    specialConnections.Add(connection);
                                    specialConnections.Add(connection2);
                                    startNode.addNeighbor(endNode, connection);
                                    endNode.addNeighbor(startNode, connection2);
                                }
                                break;
                            case XmlNodeType.XmlDeclaration:
                            case XmlNodeType.ProcessingInstruction:
                            case XmlNodeType.Comment:
                                break;
                            case XmlNodeType.EndElement:
                                currentNode = null;
                                if (reader.Name == "Teleport") {
                                    startNode = null;
                                }
                                break;
                        }
                    }
                }
            }
            // Add connections that are stored in the 'teleports' file
            using (StreamReader reader = new StreamReader("teleports")) {
                string line;
                while ((line = reader.ReadLine()) != null) {
                    string[] split = line.Split('-');
                    if (split.Length != 3) continue;

                    string[] source = split[0].Split(',');
                    string[] dest = split[1].Split(',');
                    string name = split[2];
                    bool one_way = false;
                    if (name.Contains(" ONE WAY")) {
                        one_way = true;
                        name = name.Replace(" ONE WAY", "");
                    }
                    int x, y, z;
                    x = int.Parse(source[0]);
                    y = int.Parse(source[1]);
                    z = int.Parse(source[2]);
                    Node startNode = nodeLists[z][x, y];
                    x = int.Parse(dest[0]);
                    y = int.Parse(dest[1]);
                    z = int.Parse(dest[2]);
                    Node endNode = nodeLists[z][x, y];

                    if (startNode == null || endNode == null) {
                        Console.WriteLine(String.Format("Warning: {0}", line));
                        continue;
                    }

                    SpecialConnection connection;
                    if (!startNode.hasNeighbor(endNode)) {
                        connection = new SpecialConnection { source = startNode, destination = endNode, cost = 100, name = name };
                        specialConnections.Add(connection);
                        startNode.addNeighbor(endNode, connection);
                    }

                    if (!one_way) {
                        if (!endNode.hasNeighbor(startNode)) {
                            connection = new SpecialConnection { source = endNode, destination = startNode, cost = 100, name = ReverseName(name) };
                            specialConnections.Add(connection);
                            endNode.addNeighbor(startNode, connection);
                        }
                    }
                }
            }

            sw.Stop();
            Console.WriteLine("Create teleports: {0}ms", sw.Elapsed.TotalMilliseconds);

            return specialConnections;
        }