private void mainPictureBox_MouseClick(object sender, MouseEventArgs e)
        {
            int x = e.Location.X;
            int y = e.Location.Y;
            int x_matrix_position = x / 32;
            int y_matrix_position = y / 32;

            if (x_matrix_position < roomwidth && y_matrix_position < roomheight)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    matrix_coord m_coord = new matrix_coord(x_matrix_position, y_matrix_position);

                    if (current_brush_mode == brush_mode.Tiles)
                    {
                        roomMatrix[x_matrix_position, y_matrix_position] = current_tile;
                        roomGraphics.DrawImage(current_tile_img, new Rectangle(x_matrix_position * 32, y_matrix_position * 32, 32, 32));
                    }
                    else if (current_brush_mode == brush_mode.Doodads)
                    {
                        doodads.Add(current_doodad);
                        doodad_coords.Add(m_coord);
                        doodad_appearance_chances.Add((int)doodad_chance_numeric.Value);
                        c_doodads_listBox.Items.Add(c_doodad_name + " " + m_coord.ToString() + " " + doodad_chance_numeric.Value.ToString() + "%");
                        roomGraphics.DrawImage(current_doodad_img, new Rectangle(x_matrix_position * 32, y_matrix_position * 32, 32, 32));
                    }
                    else if (current_brush_mode == brush_mode.Monsters)
                    {
                        monsters.Add(current_monster);
                        monster_coords.Add(m_coord);
                        monster_appearance_chances.Add((int)doodad_chance_numeric.Value);
                        c_monster_listbox.Items.Add(c_monster_name + " " + m_coord.ToString() + " " + doodad_chance_numeric.Value.ToString() + "%");
                        roomGraphics.DrawImage(current_monster_img, new Rectangle(x_matrix_position * 32, y_matrix_position * 32, 32, 32));
                    }
                }
                else
                {
                    roomMatrix[x_matrix_position, y_matrix_position] = current_tile;
                    roomGraphics.DrawImage(void_tile_brush.Image, new Rectangle(x_matrix_position * 32, y_matrix_position * 32, 32, 32));

                    int original_size = doodads.Count;
                    for (int i = 0; i < original_size; i++)
                    {
                        for (int j = 0; j < doodads.Count; j++)
                        {
                            if (doodad_coords[j].x == x_matrix_position && doodad_coords[j].y == y_matrix_position)
                            {
                                doodads.RemoveAt(j);
                                doodad_coords.RemoveAt(j);
                                doodad_appearance_chances.RemoveAt(j);
                                c_doodads_listBox.Items.RemoveAt(j);
                            }
                        }
                    }

                    original_size = monsters.Count;
                    for (int i = 0; i < original_size; i++)
                    {
                        for (int j = 0; j < original_size; j++)
                        {
                            if (monster_coords[j].x == x_matrix_position && monster_coords[j].y == y_matrix_position)
                            {
                                monsters.RemoveAt(j);
                                monster_coords.RemoveAt(j);
                                monster_appearance_chances.RemoveAt(j);
                                c_monster_listbox.Items.RemoveAt(j);
                            }
                        }
                    }
                }

                mainPictureBox.Invalidate();
            }
        }
        private void mainPictureBox_MouseClick(object sender, MouseEventArgs e)
        {
            int x = e.Location.X;
            int y = e.Location.Y;
            int x_matrix_position = x / 32;
            int y_matrix_position = y / 32;

            Rectangle drawing_rect = new Rectangle(x_matrix_position * 32, y_matrix_position * 32, 32, 32);
            //Make sure it's within the desired coordinate area.
            if (x_matrix_position < roomwidth && y_matrix_position < roomheight)
            {
                //Left click.
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    matrix_coord m_coord = new matrix_coord(x_matrix_position, y_matrix_position);

                    //Conditional branch depending on brush mode
                    //Tiles
                    if (current_brush_mode == brush_mode.Tiles)
                    {
                        roomMatrix[x_matrix_position, y_matrix_position] = current_tile;
                        roomGraphics.DrawImage(current_tile_img, drawing_rect);
                    }
                    //Doodads
                    else if (current_brush_mode == brush_mode.Doodads)
                    {
                        if (current_doodad == doodad_type.HallAnchor)
                        {
                            hall_anchors.Add(m_coord);
                            c_hallanchor_listbox.Items.Add(m_coord.ToString());
                        }
                        else if (current_doodad == doodad_type.Boss_Spawn)
                        {
                            boss_spawns.Add(m_coord);
                        }
                        else
                        {
                            doodads.Add(current_doodad);
                            doodad_coords.Add(m_coord);
                            doodad_appearance_chances.Add((int)doodad_chance_numeric.Value);
                            c_doodads_listBox.Items.Add(c_doodad_name + " " + m_coord.ToString() + " " + doodad_chance_numeric.Value.ToString() + "%");
                        }

                        roomGraphics.DrawImage(current_doodad_img, drawing_rect);
                    }
                    //Monsters
                    else if (current_brush_mode == brush_mode.Monsters)
                    {
                        monsters.Add(current_monster);
                        monster_coords.Add(m_coord);
                        monster_appearance_chances.Add((int)doodad_chance_numeric.Value);
                        c_monster_listbox.Items.Add(c_monster_name + " " + m_coord.ToString() + " " + doodad_chance_numeric.Value.ToString() + "%");
                        roomGraphics.DrawImage(current_monster_img, drawing_rect);
                    }
                    //Monster families
                    else if (current_brush_mode == brush_mode.Monster_Family)
                    {
                        monster_families.Add(c_monster_family);
                        m_family_coords.Add(m_coord);
                        m_family_appearance_chances.Add((int)doodad_chance_numeric.Value);
                        c_monster_listbox.Items.Add(c_monster_family.ToString() + " " + m_coord.ToString() + " " + doodad_chance_numeric.Value.ToString() + "%");
                        roomGraphics.DrawImage(current_m_family_img, drawing_rect);
                    }
                }
                //Right click.
                else
                {
                    roomMatrix[x_matrix_position, y_matrix_position] = current_tile;
                    roomGraphics.DrawImage(void_tile_brush.Image, drawing_rect);

                    int original_size = doodads.Count;
                    for (int i = 0; i < original_size; i++)
                        for (int j = 0; j < doodads.Count; j++)
                        {
                            if (doodad_coords[j].x == x_matrix_position && doodad_coords[j].y == y_matrix_position)
                            {
                                doodads.RemoveAt(j);
                                doodad_coords.RemoveAt(j);
                                doodad_appearance_chances.RemoveAt(j);
                                c_doodads_listBox.Items.RemoveAt(j);
                            }
                        }

                    original_size = monsters.Count;
                    for (int i = 0; i < original_size; i++)
                        for (int j = 0; j < monsters.Count; j++)
                        {
                            if (monster_coords[j].x == x_matrix_position && monster_coords[j].y == y_matrix_position)
                            {
                                monsters.RemoveAt(j);
                                monster_coords.RemoveAt(j);
                                monster_appearance_chances.RemoveAt(j);
                                c_monster_listbox.Items.RemoveAt(j);
                            }
                        }

                    original_size = hall_anchors.Count;
                    for(int i = 0; i < original_size; i++)
                        for (int j = 0; j < hall_anchors.Count; j++)
                        {
                            if (hall_anchors[j].x == x_matrix_position && hall_anchors[j].y == y_matrix_position)
                            {
                                hall_anchors.RemoveAt(j);
                                c_hallanchor_listbox.Items.RemoveAt(j);
                            }
                        }

                    roomMatrix[x_matrix_position, y_matrix_position] = tile_type.Void;
                }

                mainPictureBox.Invalidate();
            }
        }
        private void compress_room_to_xml(XmlDocument target_doc, string target_path)
        {
            target_node = target_doc.SelectSingleNode("XnaContent/Asset");
            XmlNode roomNode = target_doc.CreateNode(XmlNodeType.Element, "Item", null);

            XmlNode      roomCoordNode = target_doc.CreateElement("Coordinate");
            matrix_coord r_coord       = new matrix_coord((int)floorx_numeric.Value, (int)floory_numeric.Value);

            roomCoordNode.InnerText = r_coord.compressedString();
            XmlNode roomTypeNode = target_doc.CreateElement("RoomType");

            if (room_type_listbox.SelectedIndex >= 0)
            {
                roomTypeNode.InnerText = room_type_listbox.Items[room_type_listbox.SelectedIndex].ToString();
            }
            else
            {
                roomTypeNode.InnerText = "Generic";
            }
            XmlNode roomGoldNode = target_doc.CreateElement("RoomGold");

            roomGoldNode.InnerText = room_gold_numeric.Value.ToString();
            XmlNode roomWidthNode = target_doc.CreateElement("RoomWidth");

            roomWidthNode.InnerText = roomwidth.ToString();
            XmlNode roomHeightNode = target_doc.CreateElement("RoomHeight");

            roomHeightNode.InnerText = roomheight.ToString();

            XmlNode roomMatrixNode = target_doc.CreateElement("Room_Matrix");

            for (int y = 0; y < roomheight; y++)
            {
                string row = "";
                for (int x = 0; x < roomwidth; x++)
                {
                    row += tile_type_to_string(roomMatrix[x, y]) + " ";
                }
                XmlNode currentRowNode = target_doc.CreateElement("Item");
                currentRowNode.InnerText = row;
                roomMatrixNode.AppendChild(currentRowNode);
            }

            //Handle Doodads.
            XmlNode roomDoodadsNode = target_doc.CreateElement("Room_Doodads");

            for (int i = 0; i < doodads.Count; i++)
            {
                XmlNode currentDoodadNode = target_doc.CreateElement("Item");
                currentDoodadNode.InnerText = doodad_type_to_string(doodads[i]);
                roomDoodadsNode.AppendChild(currentDoodadNode);
            }
            XmlNode roomDoodadCoordsNode = target_doc.CreateElement("Room_Doodad_Coordinates");

            for (int i = 0; i < doodad_coords.Count; i++)
            {
                XmlNode currentDoodadCoordNode = target_doc.CreateElement("Item");
                currentDoodadCoordNode.InnerText = doodad_coords[i].compressedString();
                roomDoodadCoordsNode.AppendChild(currentDoodadCoordNode);
            }
            XmlNode roomDoodadChancesNode = target_doc.CreateElement("Room_Doodad_Chances");

            for (int i = 0; i < doodad_appearance_chances.Count; i++)
            {
                XmlNode currentDoodadChanceNode = target_doc.CreateElement("Item");
                currentDoodadChanceNode.InnerText = doodad_appearance_chances[i].ToString();
                roomDoodadChancesNode.AppendChild(currentDoodadChanceNode);
            }

            XmlNode roomMonstersNode = target_doc.CreateElement("Room_Monsters");

            for (int i = 0; i < monsters.Count; i++)
            {
                XmlNode currentMonsterNode = target_doc.CreateElement("Item");
                currentMonsterNode.InnerText = monster_type_to_string(monsters[i]);
                roomMonstersNode.AppendChild(currentMonsterNode);
            }
            XmlNode roomMonstersCoordsNode = target_doc.CreateElement("Room_Monster_Coordinates");

            for (int i = 0; i < monster_coords.Count; i++)
            {
                XmlNode currentMonsterCoordNode = target_doc.CreateElement("Item");
                currentMonsterCoordNode.InnerText = monster_coords[i].compressedString();
                roomMonstersCoordsNode.AppendChild(currentMonsterCoordNode);
            }
            XmlNode roomMonstersChancesNode = target_doc.CreateElement("Room_Monster_Chances");

            for (int i = 0; i < monster_appearance_chances.Count; i++)
            {
                XmlNode currentMonsterChanceNode = target_doc.CreateElement("Item");
                currentMonsterChanceNode.InnerText = monster_appearance_chances[i].ToString();
                roomMonstersChancesNode.AppendChild(currentMonsterChanceNode);
            }

            roomNode.AppendChild(roomCoordNode);
            roomNode.AppendChild(roomTypeNode);
            roomNode.AppendChild(roomGoldNode);
            roomNode.AppendChild(roomWidthNode);
            roomNode.AppendChild(roomHeightNode);
            roomNode.AppendChild(roomMatrixNode);
            roomNode.AppendChild(roomDoodadsNode);
            roomNode.AppendChild(roomDoodadCoordsNode);
            roomNode.AppendChild(roomDoodadChancesNode);
            roomNode.AppendChild(roomMonstersNode);
            roomNode.AppendChild(roomMonstersCoordsNode);
            roomNode.AppendChild(roomMonstersChancesNode);

            target_node.AppendChild(roomNode);

            target_doc.Save(target_path);
            target_doc.Load(target_path);
        }
        //Compress room to XML
        private void compress_room_to_xml(XmlDocument target_doc, string target_path)
        {
            target_node = target_doc.SelectSingleNode("XnaContent/Asset");
            XmlNode roomNode = target_doc.CreateNode(XmlNodeType.Element, "Item", null);

            XmlNode roomCoordNode = target_doc.CreateElement("Coordinate");
            matrix_coord r_coord = new matrix_coord((int)floorx_numeric.Value, (int)floory_numeric.Value);
            roomCoordNode.InnerText = r_coord.compressedString();
            XmlNode roomTypeNode = target_doc.CreateElement("RoomType");
            if (room_type_listbox.SelectedIndex >= 0)
                roomTypeNode.InnerText = room_type_listbox.Items[room_type_listbox.SelectedIndex].ToString();
            else
                roomTypeNode.InnerText = "Specific";
            XmlNode roomGoldNode = target_doc.CreateElement("RoomGold");
            roomGoldNode.InnerText = room_gold_numeric.Value.ToString();
            XmlNode roomWidthNode = target_doc.CreateElement("RoomWidth");
            roomWidthNode.InnerText = roomwidth.ToString();
            XmlNode roomHeightNode = target_doc.CreateElement("RoomHeight");
            roomHeightNode.InnerText = roomheight.ToString();
            XmlNode roomHasDoorsNode = target_doc.CreateElement("RoomDoors");
            roomHasDoorsNode.InnerText = door_chkbox.Checked.ToString().ToLower();
            XmlNode roomDoorsLockedNode = target_doc.CreateElement("DoorsLocked");
            roomDoorsLockedNode.InnerText = door_locked_chkbox.Checked.ToString().ToLower();

            XmlNode overlapNode = target_doc.CreateElement("AllowOverlap");
            string overlap = "false";
            if(OverlapChkBox.Checked)
                overlap = "true";
            overlapNode.InnerText = overlap;

            XmlNode roomMatrixNode = target_doc.CreateElement("Room_Matrix");
            for (int y = 0; y < roomheight; y++)
            {
                string row = "";
                for (int x = 0; x < roomwidth; x++)
                {
                    row += tile_type_to_string(roomMatrix[x, y]) + " ";
                }
                XmlNode currentRowNode = target_doc.CreateElement("Item");
                currentRowNode.InnerText = row;
                roomMatrixNode.AppendChild(currentRowNode);
            }

            //Hall Anchors
            XmlNode roomHallAnchors = target_doc.CreateElement("Room_Hallway_Anchors");
            for (int i = 0; i < hall_anchors.Count; i++)
            {
                XmlNode currentAnchorNode = target_doc.CreateElement("Item");
                currentAnchorNode.InnerText = hall_anchors[i].compressedString();
                roomHallAnchors.AppendChild(currentAnchorNode);
            }

            //Handle Doodads.
            XmlNode roomDoodadsNode = target_doc.CreateElement("Room_Doodads");
            for (int i = 0; i < doodads.Count; i++)
            {
                XmlNode currentDoodadNode = target_doc.CreateElement("Item");
                currentDoodadNode.InnerText = doodad_type_to_string(doodads[i]);
                roomDoodadsNode.AppendChild(currentDoodadNode);
            }
            XmlNode roomDoodadCoordsNode = target_doc.CreateElement("Room_Doodad_Coordinates");
            for (int i = 0; i < doodad_coords.Count; i++)
            {
                XmlNode currentDoodadCoordNode = target_doc.CreateElement("Item");
                currentDoodadCoordNode.InnerText = doodad_coords[i].compressedString();
                roomDoodadCoordsNode.AppendChild(currentDoodadCoordNode);
            }
            XmlNode roomDoodadChancesNode = target_doc.CreateElement("Room_Doodad_Chances");
            for (int i = 0; i < doodad_appearance_chances.Count; i++)
            {
                XmlNode currentDoodadChanceNode = target_doc.CreateElement("Item");
                currentDoodadChanceNode.InnerText = doodad_appearance_chances[i].ToString();
                roomDoodadChancesNode.AppendChild(currentDoodadChanceNode);
            }

            //Monster families
            XmlNode roomMonFamiliesNode = target_doc.CreateElement("Monster_Families");
            for (int i = 0; i < monster_families.Count; i++)
            {
                XmlNode cMonsterFamilyNode = target_doc.CreateElement("Item");
                cMonsterFamilyNode.InnerText = monster_families[i].ToString();
                roomMonFamiliesNode.AppendChild(cMonsterFamilyNode);
            }
            XmlNode roomFamilyCoordNode = target_doc.CreateElement("Monster_Family_Coordinates");
            for (int i = 0; i < m_family_coords.Count; i++)
            {
                XmlNode cFamilyCoordNode = target_doc.CreateElement("Item");
                cFamilyCoordNode.InnerText =  m_family_coords[i].compressedString();
                roomFamilyCoordNode.AppendChild(cFamilyCoordNode);
            }
            XmlNode roomMonFamilyChance = target_doc.CreateElement("Monster_Family_Chances");
            for (int i = 0; i < m_family_appearance_chances.Count; i++)
            {
                XmlNode cFamilyChanceNode = target_doc.CreateElement("Item");
                cFamilyChanceNode.InnerText = m_family_appearance_chances[i].ToString();
                roomMonFamilyChance.AppendChild(cFamilyChanceNode);
            }

            //Monsters
            XmlNode roomMonstersNode = target_doc.CreateElement("Room_Monsters");
            for (int i = 0; i < monsters.Count; i++)
            {
                XmlNode currentMonsterNode = target_doc.CreateElement("Item");
                currentMonsterNode.InnerText = monster_type_to_string(monsters[i]);
                roomMonstersNode.AppendChild(currentMonsterNode);
            }
            XmlNode roomMonstersCoordsNode = target_doc.CreateElement("Room_Monster_Coordinates");
            for (int i = 0; i < monster_coords.Count; i++)
            {
                XmlNode currentMonsterCoordNode = target_doc.CreateElement("Item");
                currentMonsterCoordNode.InnerText = monster_coords[i].compressedString();
                roomMonstersCoordsNode.AppendChild(currentMonsterCoordNode);
            }
            XmlNode roomMonstersChancesNode = target_doc.CreateElement("Room_Monster_Chances");
            for (int i = 0; i < monster_appearance_chances.Count; i++)
            {
                XmlNode currentMonsterChanceNode = target_doc.CreateElement("Item");
                currentMonsterChanceNode.InnerText = monster_appearance_chances[i].ToString();
                roomMonstersChancesNode.AppendChild(currentMonsterChanceNode);
            }

            //Boss spawns
            XmlNode bossSpawnsNode = target_doc.CreateElement("Boss_Spawn_Coordinates");
            for (int i = 0; i < boss_spawns.Count; i++)
            {
                XmlNode c_boss_spawn_node = target_doc.CreateElement("Item");
                c_boss_spawn_node.InnerText = boss_spawns[i].compressedString();
                bossSpawnsNode.AppendChild(c_boss_spawn_node);
            }

            roomNode.AppendChild(roomCoordNode);
            roomNode.AppendChild(roomTypeNode);
            roomNode.AppendChild(roomGoldNode);
            roomNode.AppendChild(roomWidthNode);
            roomNode.AppendChild(roomHeightNode);
            roomNode.AppendChild(roomHasDoorsNode);
            roomNode.AppendChild(roomDoorsLockedNode);
            roomNode.AppendChild(overlapNode);
            roomNode.AppendChild(roomMatrixNode);
            roomNode.AppendChild(roomHallAnchors);
            roomNode.AppendChild(roomDoodadsNode);
            roomNode.AppendChild(roomDoodadCoordsNode);
            roomNode.AppendChild(roomDoodadChancesNode);
            roomNode.AppendChild(roomMonFamiliesNode);
            roomNode.AppendChild(roomFamilyCoordNode);
            roomNode.AppendChild(roomMonFamilyChance);
            roomNode.AppendChild(roomMonstersNode);
            roomNode.AppendChild(roomMonstersCoordsNode);
            roomNode.AppendChild(roomMonstersChancesNode);
            roomNode.AppendChild(bossSpawnsNode);

            target_node.AppendChild(roomNode);

            target_doc.Save(target_path);
            target_doc.Load(target_path);
        }