Ejemplo n.º 1
0
        public static void SaveAll(String filename, ArrayList mazeWalls, ArrayList mazeRobots, ArrayList mazeVictims, ArrayList mazeRooms, MazeGraph mazeGraph,string sWorldName,int iTimeout)
        {
            ArrayList jWalls = new ArrayList();
            ArrayList jGates = new ArrayList();

            foreach (MazeWall mazeWall in mazeWalls)
            {
                if (mazeWall.MazeWallType == MazeWallType.wall)
                {
                    jWalls.Add(new JObject(
                           new JProperty("type", "wall"),
                           new JProperty("id", mazeWall.ID),
                           new JProperty("width", mazeWall.Width / 100),
                           new JProperty("height", mazeWall.Height / 100),
                           new JProperty("color", mazeWall.Color.ToArgb().ToString("x")),
                           new JProperty("from", new JObject(new JProperty("x", mazeWall.points[0].X / 100), new JProperty("y", mazeWall.points[0].Y / 100))),
                           new JProperty("to", new JObject(new JProperty("x", mazeWall.points[1].X / 100), new JProperty("y", mazeWall.points[1].Y / 100)))));
                }
                else
                {
                     jGates.Add(new JObject(
                          new JProperty("type", "gate"),
                          new JProperty("id", mazeWall.ID),
                          new JProperty("kind", (mazeWall.MazeDoorType == MazeGateType.doorOneWayFromTo || mazeWall.MazeDoorType == MazeGateType.doorOneWayToFrom ? "doorOneWay" : mazeWall.MazeDoorType.ToString())),
                          new JProperty("blocked", mazeWall.blocked),
                          new JProperty("from", new JObject(new JProperty("x", mazeWall.points[0].X / 100), new JProperty("y", mazeWall.points[0].Y / 100))),
                          new JProperty("to", new JObject(new JProperty("x", mazeWall.points[1].X / 100), new JProperty("y", mazeWall.points[1].Y / 100)))));
                }
            }

            ArrayList jRobots = new ArrayList();
            foreach (MazeRobot mazeRobot in mazeRobots)
            {
                jRobots.Add(new JObject(
                           new JProperty("type", "robot"),
                           new JProperty("id", mazeRobot.ID),
                           new JProperty("kind", mazeRobot.Type),
                           new JProperty("location", new JObject(
                               new JProperty("x", mazeRobot.position.X / 100),
                               new JProperty("y", mazeRobot.position.Y / 100),
                               new JProperty("z", mazeRobot.Height / 100),
                               new JProperty("alpha", 0.0001))),
                           new JProperty("target", new JObject(
                               new JProperty("x", mazeRobot.target.X),
                               new JProperty("y", mazeRobot.target.Y)))
                              ));
            }

            ArrayList jVictims = new ArrayList();
            foreach (MazeVictim mazeVictim in mazeVictims)
            {
                jVictims.Add(new JObject(
                           new JProperty("type", "victim"),
                           new JProperty("id", mazeVictim.ID),
                           new JProperty("position", new JObject(new JProperty("x", mazeVictim.position.X / 100), new JProperty("y", mazeVictim.position.Y / 100)))));
            }

            ArrayList jSpaces = new ArrayList();
            ArrayList jrSpaceWalls = new ArrayList();
            ArrayList jrSpaceGates = new ArrayList();
            foreach (MazeSpace mazeRoom in mazeRooms )
            {
                jSpaces.Add(new JObject(
                           new JProperty("type", "space"),
                           new JProperty("id", mazeRoom.ID),
                           new JProperty("kind", mazeRoom.MazeRoomType.ToString()),
                           new JProperty("name", mazeRoom.Name),
                           new JProperty("function", mazeRoom.Function),
                           new JProperty("expectedPersonCount", mazeRoom.ExpectedPersonCount),
                           new JProperty("area", mazeRoom.Area),
                           new JProperty("diameter", mazeRoom.Diameter),
                           new JProperty("searched",mazeRoom.Searched)));

                foreach (MazeWall roomWall in mazeRoom.Walls)
                {
                    if (roomWall.MazeWallType == MazeWallType.wall)
                    {
                        jrSpaceWalls.Add(new JObject(
                             new JProperty("type", "space-wall"),
                             new JProperty("spaceId", mazeRoom.ID),
                             new JProperty("wallId", roomWall.ID)));
                    }
                    else
                    {
                        bool passable = true;
                        if (    roomWall.MazeDoorType == MazeGateType.doorOneWayFromTo && roomWall.RoomTo == mazeRoom
                            ||  roomWall.MazeDoorType == MazeGateType.doorOneWayToFrom && roomWall.RoomFrom == mazeRoom)
                            passable = false;

                        jrSpaceGates.Add(new JObject(
                              new JProperty("type", "space-gate"),
                             new JProperty("spaceId", mazeRoom.ID),
                             new JProperty("gateId", roomWall.ID),
                              new JProperty("passable", passable)));
                    }
                }
            }

            ArrayList jNodes = new ArrayList();
            ArrayList jrNodeNodes = new ArrayList();
            ArrayList jrGateNodes = new ArrayList();
            ArrayList jrSpaceNodes = new ArrayList();
            foreach (MazeNode node in mazeGraph.MazeGraphNodes)
            {
                jNodes.Add(new JObject(
                        new JProperty("type", "node"),
                        new JProperty("id", node.ID),
                        new JProperty("kind", (node.MazeGraphNodeType == MazeNodeType.SpaceNode ? "spaceNode" : "gateNode")),
                        new JProperty("position", new JObject(new JProperty("x", node.position.x / 100), new JProperty("y", node.position.y / 100)))));

                foreach (MazeArc arc in node.OutgoingGraphArcs )
                    jrNodeNodes.Add(new JObject(
                         new JProperty("type", "node-nodes"),
                         new JProperty("nodeFromId", node.ID),
                         new JProperty("nodeToId", arc.to.ID),
                         new JProperty("cost", arc.Weight / 100),
                         new JProperty("blocked", (node.Door != null && node.Door == arc.to.Door ? node.Door.blocked : 0.0)) ));

                if (node.Door != null)
                {
                    jrGateNodes.Add(new JObject(
                        new JProperty("type", "gate-node"),
                        new JProperty("nodeId", node.ID),
                        new JProperty("gateId", node.Door.ID)));
                }

                jrSpaceNodes.Add(new JObject(
                    new JProperty("type", "space-node"),
                    new JProperty("spaceId", node.Room.ID),
                    new JProperty("nodeId", node.ID)));
            }

            ArrayList jrSpaceRobots = new ArrayList();
            ArrayList jrSpaceVictims = new ArrayList();
            foreach (MazeSpace mazeRoom in mazeRooms)
            {

                foreach (MazeRobot mazeRobot in mazeRobots)
                {
                    if (mazeRoom.ContainsPoint(mazeRobot.position))
                    {
                        jrSpaceRobots.Add(new JObject(
                            new JProperty("type", "space-robot"),
                            new JProperty("spaceId", mazeRoom.ID),
                            new JProperty("robotId", mazeRobot.ID)));
                    }
                }
                foreach (MazeVictim mazeVictim in mazeVictims)
                {
                    if (mazeRoom.ContainsPoint(mazeVictim.position))
                    {
                        jrSpaceVictims.Add(new JObject(
                            new JProperty("type", "space-victim"),
                            new JProperty("spaceId", mazeRoom.ID),
                            new JProperty("robotId", mazeVictim.ID)));
                    }
                }
            }

            ///////////////////////////////////////////////////////////////////
            ///////////////////////////////////////////////////////////////////
            ///////////////////////////////////////////////////////////////////

            JObject data = new JObject(
                new JProperty("name", sWorldName),
                new JProperty("timeout", iTimeout),
                new JProperty("walls", new JArray(jWalls)),
                new JProperty("gates", new JArray(jGates)),
                new JProperty("spaces", new JArray(jSpaces)),
                new JProperty("nodes", new JArray(jNodes)),
                new JProperty("robots", new JArray(jRobots)),
                new JProperty("victims", new JArray(jVictims)),
                new JProperty("space-walls", new JArray(jrSpaceWalls)),
                new JProperty("space-gates", new JArray(jrSpaceGates)),
                new JProperty("space-nodes", new JArray(jrSpaceNodes)),
                new JProperty("space-robots", new JArray(jrSpaceRobots)),
                new JProperty("space-victims", new JArray(jrSpaceVictims)),
                new JProperty("gate-nodes", new JArray(jrGateNodes)),
                new JProperty("node-nodes", new JArray(jrNodeNodes))
              );

            StreamWriter outfile = new StreamWriter(filename);
            outfile.Write(data.ToString());
            outfile.Close();
        }
Ejemplo n.º 2
0
        public void LoadAll(String filename)
        {
            JToken tempJObject;
            StreamReader reader = File.OpenText(filename);
            JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(reader));

            if (o.TryGetValue("name", out tempJObject))
                worldName =  o.GetValue("name").ToString();

            if (o.TryGetValue("timeout", out tempJObject))
                iWorldTimeout = (int)o.GetValue("timeout");

            mazeWalls = new ArrayList();
            Hashtable mazeWallsById = new Hashtable();
            mazeRobots = new ArrayList();
            mazeVictims = new ArrayList();

            mazeSpaces = new ArrayList();
            Hashtable mazeSpacesById = new Hashtable();
            mazeGraph = new MazeGraph();

            mazeNodeNodes = new ArrayList();
            mazeSpaceNode = new ArrayList();
            mazeSpaceRobots = new ArrayList();

            foreach (JObject jObject in o.GetValue("walls").Children())
            {
                MazeWall mazeWall = new MazeWall(
                    new Point2D((double)((JObject)jObject.GetValue("from")).GetValue("x") * 100, (double)((JObject)jObject.GetValue("from")).GetValue("y") * 100),
                    new Point2D((double)((JObject)jObject.GetValue("to")).GetValue("x") * 100, (double)((JObject)jObject.GetValue("to")).GetValue("y") * 100),
                    (float)jObject.GetValue("width") * 100,
                    (float)jObject.GetValue("height") * 100,
                    Color.FromArgb(Convert.ToInt32((string)jObject.GetValue("color"), 16)));
                if (mazeWall.Color.A == 0)
                    mazeWall.Color = Color.LightGray;
                mazeWall.ID = (string)jObject.GetValue("id");
                mazeWalls.Add(mazeWall);
                mazeWallsById.Add((string)jObject.GetValue("id"), mazeWall);
            }
            if (o.GetValue("gates") != null)
            {
                foreach (JObject jObject in o.GetValue("gates").Children())
                {
                    MazeWall mazeWall = new MazeWall(
                        new Point2D((double)((JObject)jObject.GetValue("from")).GetValue("x") * 100, (double)((JObject)jObject.GetValue("from")).GetValue("y") * 100),
                        new Point2D((double)((JObject)jObject.GetValue("to")).GetValue("x") * 100, (double)((JObject)jObject.GetValue("to")).GetValue("y") * 100),
                        0, 0, Color.Black);
                    mazeWall.MazeWallType = MazeWallType.gate;
                    string kind = (string)jObject.GetValue("kind");
                    mazeWall.MazeDoorType = (kind == "door" ? MazeGateType.door : (kind == "passage" ? MazeGateType.passage : MazeGateType.doorOneWayFromTo));      //doorOneWayFromTo will be changed later
                    mazeWall.blocked = (double)jObject.GetValue("blocked");
                    mazeWall.ID = (string)jObject.GetValue("id");
                    mazeWalls.Add(mazeWall);
                    mazeWallsById.Add((string)jObject.GetValue("id"), mazeWall);
                }
            }
            if (o.GetValue("robots") != null)
            {
                JToken tmp;
                Point2D? target;

                foreach (JObject jObject in o.GetValue("robots").Children())
                {
                    if (jObject.TryGetValue("target", out tmp))
                        target = new Point2D((double)((JObject)jObject.GetValue("target")).GetValue("x"), (double)((JObject)jObject.GetValue("target")).GetValue("y"));
                    else
                        target = null;

                    MazeRobot mazeRobot = new MazeRobot(
                        (string)jObject.GetValue("type"),
                        (string)jObject.GetValue("id"),
                        new Point2D((double)((JObject)jObject.GetValue("location")).GetValue("x") * 100, (double)((JObject)jObject.GetValue("location")).GetValue("y") * 100),
                        (float)((JObject)jObject.GetValue("location")).GetValue("z") * 100,
                        //new Point2D((double)((JObject)jObject.GetValue("target")).GetValue("x"),(double)((JObject)jObject.GetValue("target")).GetValue("y"))
                       target

                        );
                    mazeRobot.ID = (string)jObject.GetValue("id");
                    mazeRobots.Add(mazeRobot);
                }
            }
            if (o.GetValue("victims") != null)
            {
                foreach (JObject jObject in o.GetValue("victims").Children())
                {
                    MazeVictim mazeVictim = new MazeVictim(
                        new Point2D((double)((JObject)jObject.GetValue("position")).GetValue("x") * 100, (double)((JObject)jObject.GetValue("position")).GetValue("y") * 100));
                    mazeVictim.ID = (string)jObject.GetValue("id");
                    mazeVictims.Add(mazeVictim);
                }
            }

            Hashtable wallsBySpaceId = new Hashtable();
            if (o.GetValue("space-walls") != null)
            {
                foreach (JObject jObject in o.GetValue("space-walls").Children())
                {
                    string spaceId = (string)jObject.GetValue("spaceId");
                    string wallId = (string)jObject.GetValue("wallId");
                    if (wallsBySpaceId.ContainsKey(spaceId))
                    {
                        ((ArrayList)wallsBySpaceId[spaceId]).Add(mazeWallsById[wallId]);
                    }
                    else
                    {
                        ArrayList newArrayList = new ArrayList();
                        newArrayList.Add(mazeWallsById[wallId]);
                        wallsBySpaceId[spaceId] = newArrayList;
                    }
                }
            }
            if (o.GetValue("space-gates") != null)
            {
                foreach (JObject jObject in o.GetValue("space-gates").Children())
                {
                    string spaceId = (string)jObject.GetValue("spaceId");
                    string wallId = (string)jObject.GetValue("gateId");
                    if (wallsBySpaceId.ContainsKey(spaceId))
                    {
                        ((ArrayList)wallsBySpaceId[spaceId]).Add(mazeWallsById[wallId]);
                    }
                    else
                    {
                        ArrayList newArrayList = new ArrayList();
                        newArrayList.Add(mazeWallsById[wallId]);
                        wallsBySpaceId[spaceId] = newArrayList;
                    }
                }
            }

            if (o.GetValue("spaces") != null)
            {
                foreach (JObject jObject in o.GetValue("spaces").Children())
                {
                    MazeSpace newRoom = new MazeSpace((ArrayList)wallsBySpaceId[(string)jObject.GetValue("id")]);
                    newRoom.ID = (string)jObject.GetValue("id");
                    newRoom.MazeRoomType = (MazeSpaceType)System.Enum.Parse(typeof(MazeSpaceType), (string)jObject.GetValue("kind"));
                    newRoom.Function = (string)jObject.GetValue("function");
                    newRoom.Name = (string)jObject.GetValue("name");
                    newRoom.ExpectedPersonCount = (int)jObject.GetValue("expectedPersonCount");

                    if (jObject.TryGetValue("searched", out tempJObject))
                        newRoom.Searched = (int)jObject.GetValue("searched");

                    mazeSpaces.Add(newRoom);
                    mazeSpacesById[newRoom.ID] = newRoom;

                    foreach (MazeWall roomWall in newRoom.Walls)
                    {
                        if (roomWall.RoomFrom == null)
                            roomWall.RoomFrom = newRoom;
                        else
                            roomWall.RoomTo = newRoom;
                    }
                }
            }

            ///////////////////////////  graph

            Hashtable spacesByNodeId = new Hashtable();
            if (o.GetValue("space-nodes") != null)
            {
                foreach (JObject jObject in o.GetValue("space-nodes").Children())
                {
                    spacesByNodeId[(string)jObject.GetValue("nodeId")] = mazeSpacesById[(string)jObject.GetValue("spaceId")];
                }
            }
            Hashtable gatesByNodeId = new Hashtable();
            if (o.GetValue("gate-nodes") != null)
            {
                foreach (JObject jObject in o.GetValue("gate-nodes").Children())
                {
                    gatesByNodeId[(string)jObject.GetValue("nodeId")] = mazeWallsById[(string)jObject.GetValue("gateId")];
                }
            }

            Hashtable nodesById = new Hashtable();
            if (o.GetValue("nodes") != null)
            {
                foreach (JObject jObject in o.GetValue("nodes").Children())
                {
                    MazeNode node = new MazeNode(new Point2D((double)((JObject)jObject.GetValue("position")).GetValue("x") * 100, (double)((JObject)jObject.GetValue("position")).GetValue("y") * 100),
                        (MazeSpace)spacesByNodeId[(string)jObject.GetValue("id")],
                        gatesByNodeId[(string)jObject.GetValue("id")] as MazeWall);
                    node.ID = (string)jObject.GetValue("id");
                    nodesById[node.ID] = node;
                    mazeGraph.AddNode(node);
                }
            }

            if (o.GetValue("node-nodes") != null)
            {
                foreach (JObject jObject in o.GetValue("node-nodes").Children())
                {
                    MazeNode fromNode = (MazeNode)nodesById[(string)jObject.GetValue("nodeFromId")];
                    MazeNode toNode = (MazeNode)nodesById[(string)jObject.GetValue("nodeToId")];

                    if (fromNode.Door != null && (fromNode.Door.MazeDoorType == MazeGateType.doorOneWayFromTo || fromNode.Door.MazeDoorType == MazeGateType.doorOneWayToFrom))
                    {
                        if (fromNode.Room == fromNode.Door.RoomFrom)
                            fromNode.Door.MazeDoorType = MazeGateType.doorOneWayFromTo;
                        else
                            fromNode.Door.MazeDoorType = MazeGateType.doorOneWayToFrom;
                    }
                    mazeGraph.AddArc(fromNode, toNode);

                    mazeNodeNodes.Add(new MazeNodeNodes(jObject.GetValue("nodeFromId").ToString(), jObject.GetValue("nodeToId").ToString(), double.Parse(jObject.GetValue("cost").ToString()), double.Parse(jObject.GetValue("blocked").ToString())));
                }
            }

            if (o.GetValue("space-nodes") != null)
            {
                foreach (JObject jObject in o.GetValue("space-nodes").Children())
                {
                    mazeSpaceNode.Add(new MazeSpaceNodes(jObject.GetValue("type").ToString(),
                                                         jObject.GetValue("spaceId").ToString(),
                                                         jObject.GetValue("nodeId").ToString()));
                }
            }

            if (o.GetValue("space-robots") != null)
            {
                foreach (JObject jObject in o.GetValue("space-robots").Children())
                {
                    mazeSpaceRobots.Add(new MazeSpaceRobots(jObject.GetValue("type").ToString(),
                                                         jObject.GetValue("spaceId").ToString(),
                                                         jObject.GetValue("robotId").ToString()));
                }
            }

            reader.Close();
        }
Ejemplo n.º 3
0
        private void CreateNewMaze()
        {
            if (mazeWalls != null || mazeRobots != null)
            {
                if (MessageBox.Show("Current Maze will be lost. Do you want to proceed?", "Create new Maze", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    != DialogResult.Yes)
                {
                    return;
                }
            }
            mazeWalls = new ArrayList();
            mazeRobots = new ArrayList();
            mazeVictims = new ArrayList();
            mazeRooms = new ArrayList();
            mazeGraph = new MazeGraph();
            mazeTargets = new ArrayList();
            mazeNodeNodes = new ArrayList();
            mazeSpaceNode = new ArrayList();
            mazeSpaceRobots = new ArrayList();
            MazeIdentifiable.ClearBusyIdsCache();

            recreateRoomsTreeView();
            recreateGraphTreeView();
            RefreshMazeWallsTreeView();

            mazePanel.Size = new Size((int)((float)widthNumericUpDown.Value * 100 * zoom), (int)((float)heightNumericUpDown.Value * 100 * zoom));
            CreateMazeGraphics();

            createWallInProgress = false;
            moveInProgress = false;
            snapToAngle = false;
            snapToGrid = false;

            robotNameIndex = 0;

            viewPanel.AutoScrollPosition = new Point(mazePanel.Width / -2, mazePanel.Width / -2);
        }
Ejemplo n.º 4
0
        private void recreateRoomsButton_Click(object sender, EventArgs e)
        {
            if (mazeGraph != null && MessageBox.Show("This operation will remove the rooms and the graph. It cannot be undone.\n Are you sure?", "", MessageBoxButtons.OKCancel) != DialogResult.OK)
                return;
            mazeGraph = new MazeGraph();
            mazeRooms = new ArrayList();

            roomsTreeView.Nodes.Clear();

            RoomsGraphBuilder roomsGraphBuilder = new RoomsGraphBuilder();
            if (!ConvexOnlyCheckBox.Checked)
            {
                roomsGraphBuilder.GenerateConvexRoomsOnly = false;
                roomsGraphBuilder.SplitRoomsIntoConvex = false;
            }
            foreach (MazeWall wall in mazeWalls)
            {
                if (wall.MazeWallType == MazeWallType.wall)
                    roomsGraphBuilder.AddWall(new Point2D(wall.points[0].X, wall.points[0].Y), new Point2D(wall.points[1].X, wall.points[1].Y));
                else
                    roomsGraphBuilder.AddDoor(new Point2D(wall.points[0].X, wall.points[0].Y), new Point2D(wall.points[1].X, wall.points[1].Y));
            }
            roomsGraphBuilder.BuildRegion();

            Hashtable mazeNodeByRoomasGraphNodes = new Hashtable();
            Hashtable mazeWallsByRoomasGraphWalls = new Hashtable();

            ArrayList oldMazeWalls = mazeWalls;
            mazeWalls = new ArrayList();
            foreach (Room graphBuilderRoom in roomsGraphBuilder.Rooms)
            {
                ArrayList roomWalls = new ArrayList();
                foreach (Wall graphBuilderWall in graphBuilderRoom.Walls)
                {
                    bool oldWallFound = false;
                    foreach (MazeWall mazeWall in oldMazeWalls)     //find old
                    {
                        if (mazeWall.Segment.ContainsSegment(graphBuilderWall.WallSegment))
                        {
                            oldWallFound = true;
                            MazeWall roomWall = new MazeWall(graphBuilderWall.From, graphBuilderWall.To, mazeWall.Width, mazeWall.Height, mazeWall.Color);
                            roomWall.MazeWallType = (graphBuilderWall.Type == Wall.WallType.Solid ? MazeWallType.wall : MazeWallType.gate);
                            int indexOf = mazeWalls.IndexOf(roomWall);
                            if (indexOf >= 0)
                            {
                                roomWall = (MazeWall)mazeWalls[indexOf];
                            }
                            else
                            {
                                mazeWalls.Add(roomWall);
                                mazeWallsByRoomasGraphWalls[graphBuilderWall] = roomWall;
                            }
                            roomWalls.Add(roomWall);
                            break;
                        }
                    }
                    if (!oldWallFound)
                    {
                        MazeWall roomWall = new MazeWall(graphBuilderWall.From, graphBuilderWall.To, 0, 0, Color.Black);
                        int indexOf = mazeWalls.IndexOf(roomWall);
                        if (indexOf >= 0)
                        {
                            roomWall = (MazeWall)mazeWalls[indexOf];
                        }
                        else
                        {
                            roomWall.MazeWallType = MazeWallType.gate;
                            mazeWalls.Add(roomWall);
                            mazeWallsByRoomasGraphWalls[graphBuilderWall] = roomWall;
                        }
                        roomWalls.Add(roomWall);
                    }
                }

                /*              ///reorder walls
                              ArrayList orderedRoomWalls = new ArrayList();
                              orderedRoomWalls.Add(roomWalls[0]);
                              roomWalls.RemoveAt(0);
                              Point2D nextPoint = (roomWalls[0] as Wall).To;
                              while (roomWalls.Count > 0)
                              {
                                  foreach (Wall wall in roomWalls)
                                      if (wall.From.GetDistance(nextPoint) < 0.01)
                                      {
                                          nextPoint = wall.From;
                                          roomWalls.Remove(wall);
                                          orderedRoomWalls.Add(wall);
                                          break;
                                      }
                                      else if (wall.To.GetDistance(nextPoint) < 0.01)
                                      {
                                          nextPoint = wall.To;
                                          roomWalls.Remove(wall);
                                          orderedRoomWalls.Add(wall);
                                          break;
                                      }
                              }
              */
                MazeSpace room = new MazeSpace(roomWalls);
                mazeRooms.Add(room);
                foreach (MazeWall roomWall in roomWalls)
                {
                    if (roomWall.RoomFrom == null)
                        roomWall.RoomFrom = room;
                    else
                        roomWall.RoomTo = room;
                }

                //////////////////////////////////////   nodes

                foreach (RoomsGraph.Node borderNode in graphBuilderRoom.BorderNodes)        //push door-nodes into rooms
                {
                    Vector2D v = new Vector2D(borderNode.DoorWall.From, borderNode.DoorWall.To);
                    v.MakePerpendicular();
                    v.Length = (double)wallWidthNumericUpDown.Value * 100;
                    if (Math.Abs(v.AngleBetween(new Vector2D(borderNode.DoorWall.Center, borderNode.Location))) > Math.PI / 2)
                        v.Inverse();
                    borderNode.Position.X += v.x;
                    borderNode.Position.Y += v.y;
                }

                room.CetralNode = new MazeNode(graphBuilderRoom.CentralNode.Location, room, null);
                mazeNodeByRoomasGraphNodes[graphBuilderRoom.CentralNode] = room.CetralNode;
                mazeGraph.AddNode(room.CetralNode);
                foreach (RoomsGraph.Node n in graphBuilderRoom.BorderNodes)
                {
                    MazeNode mazeGraphNode = new MazeNode(n.Location, room, (MazeWall)mazeWallsByRoomasGraphWalls[n.DoorWall]);
                    mazeNodeByRoomasGraphNodes[n] = mazeGraphNode;
                    room.BorderNodes.Add(mazeGraphNode);
                    mazeGraph.AddNode(mazeGraphNode);
                }
            }

            foreach (RoomsGraph.Arc arc in roomsGraphBuilder.Graph.Arcs)
            {
                if (mazeNodeByRoomasGraphNodes[arc.StartNode] != null && mazeNodeByRoomasGraphNodes[arc.EndNode] != null)
                    mazeGraph.AddArc((MazeNode)mazeNodeByRoomasGraphNodes[arc.StartNode], (MazeNode)mazeNodeByRoomasGraphNodes[arc.EndNode]);
            }

            recreateRoomsTreeView();
            recreateGraphTreeView();
            RefreshMazeWallsTreeView();

            mazePanel_Paint(this, null);
        }
Ejemplo n.º 5
0
        private void LoadMazeFromRoBoss()
        {
            if (mazeWalls != null || mazeRobots != null)
            {
                if (MessageBox.Show("Current Maze will be lost. Do you want to proceed?", "Load Maze", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    != DialogResult.Yes)
                {
                    return;
                }
            }

            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "XML file (*.xml)|*.xml";
            if (fileDialog.ShowDialog() != DialogResult.OK)
                return;

            mazeWalls = new ArrayList();
            mazeRobots = new ArrayList();
            mazeVictims = new ArrayList();
            mazeRooms = new ArrayList();
            mazeGraph = new MazeGraph();
            MazeIdentifiable.ClearBusyIdsCache();

            recreateRoomsTreeView();
            recreateGraphTreeView();
            RefreshMazeWallsTreeView();

            try
            {

                XmlTextReader reader = new XmlTextReader(fileDialog.FileName);
                reader.WhitespaceHandling = WhitespaceHandling.None;

                XmlDocument worldDocumnet = new XmlDocument();
                worldDocumnet.Load(reader);
                reader.Close();
                reader = null;

                XmlNode worldNode = worldDocumnet.SelectSingleNode("/World");		//whole settings document
                if (worldNode == null)
                {
                    MessageBox.Show("World node missing - probaly not a World Definition file.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                worldNameTextBox.Text = XmlHelper.GetStringAttributeFromNode(worldNode, "name", "");

                XmlNodeList robotsNodeList = worldDocumnet.SelectNodes("/World/Robots/Robot");
                XmlNodeList environmentNodeList = worldDocumnet.SelectNodes("/World/Environment/Geoms/Geom");

                XmlNode floorNode = environmentNodeList.Item(0);
                widthNumericUpDown.Value = (decimal)XmlHelper.GetDoubleAttributeFromNode(floorNode, "size_x");
                heightNumericUpDown.Value = (decimal)XmlHelper.GetDoubleAttributeFromNode(floorNode, "size_y");
                //mazePanel.Size = new Size(
                //    (int)(XmlHelper.GetDoubleAttributeFromNode(floorNode, "size_x") * zoom * 100),
                //    (int)(XmlHelper.GetDoubleAttributeFromNode(floorNode, "size_y") * zoom * 100));
                //CreateMazeGraphics();
                //widthNumericUpDown.Value = (decimal)mazePanel.Size.Width / 100;
                //heightNumericUpDown.Value = (decimal)mazePanel.Size.Height / 100;

                for (int i = 1; i < environmentNodeList.Count; i++)
                {
                    mazeWalls.Add(MazeWall.BuildFromXmlNode(environmentNodeList.Item(i)));
                }

                for (int i = 0; i < robotsNodeList.Count; i++)
                {
                    mazeRobots.Add(MazeRobot.BuildFromXmlNode(robotsNodeList.Item(i)));
                }

                worldDocumnet = null;

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            createWallInProgress = false;
            moveInProgress = false;
            snapToAngle = false;
            snapToGrid = false;

            robotNameIndex = 0;

            viewPanel.AutoScrollPosition = new Point(0, 0);
            mazePanel_Paint(this, null);
        }
Ejemplo n.º 6
0
        private void LoadMaze(string sFileName)
        {
            MazeIdentifiable.ClearBusyIdsCache();

            JsonHelper jsonHelper = new JsonHelper();
            jsonHelper.LoadAll(sFileName);

            mazeWalls = jsonHelper.mazeWalls;
            mazeRobots = jsonHelper.mazeRobots;
            mazeVictims = jsonHelper.mazeVictims;
            mazeGraph = jsonHelper.mazeGraph;
            mazeRooms = jsonHelper.mazeSpaces;
            worldName = jsonHelper.worldName;
            mazeNodeNodes = jsonHelper.mazeNodeNodes;
            mazeSpaceNode = jsonHelper.mazeSpaceNode;
            mazeSpaceRobots = jsonHelper.mazeSpaceRobots;

            iWorldTimeout = jsonHelper.iWorldTimeout;

            double maxX = 500;
            double maxY = 500;
            foreach (MazeWall mazeWall in mazeWalls)
            {
                maxX = Math.Max(maxX, Math.Max(mazeWall.points[0].X, mazeWall.points[1].X));
                maxY = Math.Max(maxY, Math.Max(mazeWall.points[0].Y, mazeWall.points[1].Y));
            }
            widthNumericUpDown.Value = (decimal)(maxX + 10) / 100;
            heightNumericUpDown.Value = (decimal)(maxY + 10) / 100;

            recreateRoomsTreeView();
            recreateGraphTreeView();
            RefreshMazeWallsTreeView();

            mazePanel_Paint(this, null);
        }