Ejemplo n.º 1
0
        public CellWPF(StreetCell logicIn)
        {
            InitializeComponent();

            this.logic = logicIn;

            this.rectangle.Fill     = Brushes.Gray;
            this.rectangle.Stroke   = Brushes.Yellow;
        }
Ejemplo n.º 2
0
        public void CargarDatos()
        {
            OpenFileDialog loadDialog = new OpenFileDialog();

            loadDialog.ShowDialog();

            Stream stream = loadDialog.OpenFile();
            StreamReader sr = new StreamReader(stream);

            string xmlText = sr.ReadToEnd();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xmlText);

            XmlNodeList cityStats = xmlDoc.GetElementsByTagName("city");
            XmlNodeList buildings = xmlDoc.GetElementsByTagName("building");
            XmlNodeList materials = xmlDoc.GetElementsByTagName("material");
            XmlNodeList streetLights = xmlDoc.GetElementsByTagName("streetlight");

            this.name = ((XmlElement)cityStats[0]).GetAttribute("name");
            this.width = Int32.Parse(((XmlElement)cityStats[0]).GetAttribute("width"));
            this.height = Int32.Parse(((XmlElement)cityStats[0]).GetAttribute("height"));
            this.accidentProbability = Double.Parse(((XmlElement)cityStats[0]).GetAttribute("accident"));
            this.sicknessProbability = Double.Parse(((XmlElement)cityStats[0]).GetAttribute("sickness"));
            this.crimeProbability = Double.Parse(((XmlElement)cityStats[0]).GetAttribute("crime"));
            this.fireProbability = Double.Parse(((XmlElement)cityStats[0]).GetAttribute("fire"));

            this.map = new Cell[width, height];

            #region Materials
            foreach (XmlElement node in materials)
            {
                Material auxMaterial = new Material(node.GetAttribute("name"), Double.Parse(node.GetAttribute("inflamability")), Double.Parse(node.GetAttribute("sick")));

                this.materialsList.Add(auxMaterial);
            }
            #endregion

            #region Buildings
            foreach (XmlElement node in buildings)
            {
                string auxName = node.GetAttribute("name");

                Building auxBuilding;
                if (auxName.Contains("Comisaría"))
                {
                    auxBuilding = new PoliceStation(auxName);
                    this.policeStation = (PoliceStation)auxBuilding;
                }
                else if (auxName.Contains("Bomberos"))
                {
                    auxBuilding = new FireStation(auxName);
                    this.fireStation = (FireStation)auxBuilding;
                }
                else if (auxName.Contains("Hospital"))
                {
                    auxBuilding = new Hospital(auxName);
                    this.hospital = (Hospital)auxBuilding;
                }
                else
                {
                    auxBuilding = new Building(node.GetAttribute("name"));
                }

                XmlNodeList currentCells = node.GetElementsByTagName("cell");

                foreach (XmlElement cell in currentCells)
                {
                    int i = Int32.Parse(cell.GetAttribute("i"));
                    int j = Int32.Parse(cell.GetAttribute("j"));

                    string auxMaterial = cell.GetAttribute("material");

                    for (int k = 0; k < this.materialsList.Count; k++)
                    {
                        if (this.materialsList[k].name.Equals(auxMaterial))
                        {
                            BuildingCell auxCell = new BuildingCell(auxBuilding.name, i, j, this.materialsList[k]);
                            auxBuilding.cellList.Add(auxCell);
                            this.map[i, j] = auxCell;
                            break;
                        }
                    }
                }

                this.buildingsList.Add(auxBuilding);
            }
            #endregion

            #region StreetLights
            foreach (XmlElement node in streetLights)
            {
                StreetLight auxStreetLight = new StreetLight(Int32.Parse(node.GetAttribute("time")));

                XmlNodeList currentCells = node.GetElementsByTagName("cell");

                foreach (XmlElement cell in currentCells)
                {
                    int i = Int32.Parse(cell.GetAttribute("i"));
                    int j = Int32.Parse(cell.GetAttribute("j"));

                    StreetLightCell auxCell = new StreetLightCell(i, j);

                    auxStreetLight.cellList.Add(auxCell);

                    this.map[i, j] = auxCell;
                }

                this.streetLightsList.Add(auxStreetLight);
            }
            #endregion

            #region Streets
            for (int i = 0; i < this.width; i++)
            {
                for (int j = 0; j < this.height; j++)
                {
                    if (this.map[i, j] == null)
                    {
                        StreetCell auxStreetCell = new StreetCell(i, j);

                        this.map[i, j] = auxStreetCell;
                        this.streetList.Add(auxStreetCell);
                    }
                }
            }

            #region CasoBase
            for (int i = 0; i < this.streetList.Count; i++)
            {
                int J = this.streetList[i].coordJ;
                int I = this.streetList[i].coordI;

                if (J > 0 && I > 0 && J < this.map.GetLength(1) - 1 && I < this.map.GetLength(0) - 1)
                {
                    if (!this.map[I - 1, J].transitable)
                    {
                        this.streetList[i].verticalVector = 1;
                    }

                    if (!this.map[I + 1, J].transitable)
                    {
                        this.streetList[i].verticalVector = -1;
                    }

                    if (!this.map[I, J - 1].transitable)
                    {
                        this.streetList[i].horizontalVector = -1;
                    }

                    if (!this.map[I, J + 1].transitable)
                    {
                        this.streetList[i].horizontalVector = 1;
                    }
                }
            }
            #endregion

            #region CasoParticular
            for (int i = 0; i < this.streetList.Count; i++)
            {
                int J = this.streetList[i].coordJ;
                int I = this.streetList[i].coordI;

                if (J > 0 && I > 0 && J < this.map.GetLength(1) - 1 && I < this.map.GetLength(0) - 1)
                {
                    if (this.map[I, J - 1].transitable && this.map[I + 1, J].transitable && this.map[I - 1, J].transitable && this.map[I, J + 1].transitable)
                    {
                        if (((StreetCell)(this.map[I, J - 1])).verticalVector == -1 && ((StreetCell)(this.map[I + 1, J])).horizontalVector == -1)
                        {
                            this.streetList[i].verticalVector = -1;
                            this.streetList[i].horizontalVector = 0;
                        }
                    }

                    if (this.map[I, J - 1].transitable && this.map[I + 1, J].transitable && !this.map[I - 1, J].transitable && !this.map[I, J + 1].transitable)
                    {
                        if (((StreetCell)(this.map[I, J - 1])).verticalVector == 1 && ((StreetCell)(this.map[I + 1, J])).horizontalVector == 1)
                        {
                            this.streetList[i].horizontalVector = 1;
                            this.streetList[i].verticalVector = 0;
                        }
                    }

                    if (this.map[I, J - 1].transitable && this.map[I - 1, J].transitable && this.map[I + 1, J].transitable && this.map[I, J + 1].transitable)
                    {
                        if (((StreetCell)(this.map[I, J - 1])).verticalVector == -1 && ((StreetCell)(this.map[I - 1, J])).horizontalVector == -1)
                        {
                            this.streetList[i].horizontalVector = -1;
                            this.streetList[i].verticalVector = 0;
                        }
                    }

                    if (this.map[I, J - 1].transitable && this.map[I - 1, J].transitable && !this.map[I + 1, J].transitable && !this.map[I, J + 1].transitable)
                    {
                        if (((StreetCell)(this.map[I, J - 1])).verticalVector == 1 && ((StreetCell)(this.map[I - 1, J])).horizontalVector == 1)
                        {
                            this.streetList[i].verticalVector = 1;
                            this.streetList[i].horizontalVector = 0;
                        }
                    }

                    if (this.map[I, J + 1].transitable && this.map[I + 1, J].transitable && !this.map[I - 1, J].transitable && !this.map[I, J - 1].transitable)
                    {
                        if (((StreetCell)(this.map[I, J + 1])).verticalVector == -1 && ((StreetCell)(this.map[I + 1, J])).horizontalVector == -1)
                        {
                            this.streetList[i].verticalVector = -1;
                            this.streetList[i].horizontalVector = 0;
                        }
                    }

                    if (this.map[I, J + 1].transitable && this.map[I + 1, J].transitable && this.map[I, J - 1].transitable && this.map[I - 1, J].transitable)
                    {
                        if (((StreetCell)(this.map[I, J + 1])).verticalVector == 1 && ((StreetCell)(this.map[I + 1, J])).horizontalVector == 1)
                        {
                            this.streetList[i].horizontalVector = 1;
                            this.streetList[i].verticalVector = 0;
                        }
                    }

                    if (this.map[I, J + 1].transitable && this.map[I - 1, J].transitable && this.map[I, J - 1].transitable && this.map[I + 1, J].transitable)
                    {
                        if (((StreetCell)(this.map[I, J + 1])).verticalVector == -1 && ((StreetCell)(this.map[I - 1, J])).horizontalVector == 1)
                        {
                            this.streetList[i].verticalVector = 1;
                            this.streetList[i].horizontalVector = 0;
                        }
                    }

                    if (this.map[I, J + 1].transitable && this.map[I - 1, J].transitable && !this.map[I, J - 1].transitable && !this.map[I + 1, J].transitable)
                    {
                        if (((StreetCell)(this.map[I, J + 1])).verticalVector == 1 && ((StreetCell)(this.map[I - 1, J])).horizontalVector == -1)
                        {
                            this.streetList[i].verticalVector = 1;
                            this.streetList[i].horizontalVector = 0;
                        }
                    }
                }
            }
            #endregion

            #region EntradasYSalidas
            for (int i = 0; i < this.streetList.Count; i++)
            {
                int J = this.streetList[i].coordJ;
                int I = this.streetList[i].coordI;

                if (I == 0)
                {
                    if (this.map[I + 1, J].transitable)
                    {
                        if (((StreetCell)this.map[I + 1, J]).horizontalVector == 1)
                        {
                            ((StreetCell)this.map[I, J]).entrance = true;
                        }
                        else if (((StreetCell)this.map[I + 1, J]).horizontalVector == -1)
                        {
                            ((StreetCell)this.map[I, J]).exit = true;
                        }

                        ((StreetCell)this.map[I, J]).horizontalVector = ((StreetCell)this.map[I + 1, J]).horizontalVector;
                    }
                }

                if (I == this.map.GetLength(0) - 1)
                {
                    if (this.map[I - 1, J].transitable)
                    {
                        if (((StreetCell)this.map[I - 1, J]).horizontalVector == 1)
                        {
                            ((StreetCell)this.map[I, J]).exit = true;
                        }
                        else if (((StreetCell)this.map[I - 1, J]).horizontalVector == -1)
                        {
                            ((StreetCell)this.map[I, J]).entrance = true;
                        }

                        ((StreetCell)this.map[I, J]).horizontalVector = ((StreetCell)this.map[I - 1, J]).horizontalVector;
                    }
                }

                if (J == 0)
                {
                    if (this.map[I, J + 1].transitable)
                    {
                        if (((StreetCell)this.map[I, J + 1]).verticalVector == 1)
                        {
                            ((StreetCell)this.map[I, J]).entrance = true;
                        }
                        else if (((StreetCell)this.map[I, J + 1]).verticalVector == -1)
                        {
                            ((StreetCell)this.map[I, J]).exit = true;
                        }

                        ((StreetCell)this.map[I, J]).verticalVector = ((StreetCell)this.map[I, J + 1]).verticalVector;
                    }
                }

                if (J == this.map.GetLength(1) - 1)
                {
                    if (this.map[I, J - 1].transitable)
                    {
                        if (((StreetCell)this.map[I, J - 1]).verticalVector == 1)
                        {
                            ((StreetCell)this.map[I, J]).exit = true;
                        }
                        else if (((StreetCell)this.map[I, J - 1]).verticalVector == -1)
                        {
                            ((StreetCell)this.map[I, J]).entrance = true;
                        }

                        ((StreetCell)this.map[I, J]).verticalVector = ((StreetCell)this.map[I, J - 1]).verticalVector;
                    }
                }
            }
            #endregion
            #endregion

            #region Adjust StreetLights
            for (int i = 0; i < this.streetLightsList.Count; i++)
            {
                for (int j = 0; j < this.streetLightsList[i].cellList.Count; j++)
                {
                    int I = streetLightsList[i].cellList[j].coordI;
                    int J = streetLightsList[i].cellList[j].coordJ;

                    if (I != 0 && I != (this.map.GetLength(0) - 1) && J != 0 && (J != this.map.GetLength(1) - 1))
                    {
                        if (this.map[I + 1, J].transitable)
                        {
                            if (((StreetCell)this.map[I + 1, J]).horizontalVector == -1)
                            {
                                this.streetLightsList[i].cellList[j].NE = true;
                                this.streetLightsList[i].cellList[j].verticalVector = -1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }
                        else if (this.map[I, J - 1].transitable)
                        {
                            if (((StreetCell)this.map[I, J - 1]).verticalVector == -1)
                            {
                                this.streetLightsList[i].cellList[j].NE = true;
                                this.streetLightsList[i].cellList[j].verticalVector = -1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }

                        if (this.streetLightsList[i].cellList[j].NE && !this.map[I, J - 1].transitable)
                        {
                            this.streetLightsList[i].cellList[j].verticalVector = 0;
                            this.streetLightsList[i].cellList[j].horizontalVector = -1;
                            this.streetLightsList[i].cellList[j].anomalous = true;
                        }

                        if (this.map[I - 1, J].transitable)
                        {
                            if (((StreetCell)this.map[I - 1, J]).horizontalVector == -1)
                            {
                                this.streetLightsList[i].cellList[j].NW = true;
                                this.streetLightsList[i].cellList[j].verticalVector = 1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }
                        else if (this.map[I, J - 1].transitable)
                        {
                            if (((StreetCell)this.map[I, J - 1]).verticalVector == 1)
                            {
                                this.streetLightsList[i].cellList[j].NW = true;
                                this.streetLightsList[i].cellList[j].verticalVector = 1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }

                        if (this.streetLightsList[i].cellList[j].NW && !this.map[I - 1, J].transitable)
                        {
                            this.streetLightsList[i].cellList[j].verticalVector = 1;
                            this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            this.streetLightsList[i].cellList[j].anomalous = true;
                        }

                        if (this.map[I + 1, J].transitable)
                        {
                            if (((StreetCell)this.map[I + 1, J]).horizontalVector == 1)
                            {
                                this.streetLightsList[i].cellList[j].SE = true;
                                this.streetLightsList[i].cellList[j].verticalVector = -1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }
                        else if (this.map[I, J + 1].transitable)
                        {
                            if (((StreetCell)this.map[I, J + 1]).verticalVector == -1)
                            {
                                this.streetLightsList[i].cellList[j].SE = true;
                                this.streetLightsList[i].cellList[j].verticalVector = -1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }

                        if (this.streetLightsList[i].cellList[j].SE && !this.map[I + 1, J].transitable)
                        {
                            this.streetLightsList[i].cellList[j].verticalVector = -1;
                            this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            this.streetLightsList[i].cellList[j].anomalous = true;
                        }

                        if (this.map[I - 1, J].transitable)
                        {
                            if (((StreetCell)this.map[I - 1, J]).horizontalVector == 1)
                            {
                                this.streetLightsList[i].cellList[j].SW = true;
                                this.streetLightsList[i].cellList[j].verticalVector = 1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }
                        else if (this.map[I, J + 1].transitable)
                        {
                            if (((StreetCell)this.map[I, J + 1]).verticalVector == 1)
                            {
                                this.streetLightsList[i].cellList[j].SW = true;
                                this.streetLightsList[i].cellList[j].verticalVector = 1;
                                this.streetLightsList[i].cellList[j].horizontalVector = 0;
                            }
                        }

                        if (this.streetLightsList[i].cellList[j].SW && !this.map[I, J + 1].transitable)
                        {
                            this.streetLightsList[i].cellList[j].verticalVector = 0;
                            this.streetLightsList[i].cellList[j].horizontalVector = -1;
                            this.streetLightsList[i].cellList[j].anomalous = true;
                        }
                    }
                }
            }
            #endregion

            #region Create Parking Areas
            for (int i = 0; i < this.buildingsList.Count; i++)
            {
                this.buildingsList[i].createParking(this.map);
            }
            #endregion
        }