public VehicleWPF(Vehicle vehicleIn) { InitializeComponent(); this.logicVehicle = vehicleIn; if (this.logicVehicle.type.Equals("FireStationVehicle")) { this.VehicleRectangle.Fill = Brushes.Red; } else if (this.logicVehicle.type.Equals("PoliceStationVehicle")) { this.VehicleRectangle.Fill = Brushes.Blue; } else if (this.logicVehicle.type.Equals("Ambulance")) { this.VehicleRectangle.Fill = Brushes.White; } }
protected void OnMoveCar(Vehicle vehicleIn) { if (this.moveCar != null) { this.moveCar(vehicleIn); } }
public void CitySim() { this.stopWatch.Start(); while (this.sim) { if ((this.stopWatch.ElapsedMilliseconds / 1000) - this.lastUpdate >= 1) { this.OnUpdateTimeSpan(this.stopWatch.ElapsedMilliseconds / 1000); this.lastUpdate = this.stopWatch.ElapsedMilliseconds / 1000; #region StreetLights Control for (int i = 0; i < this.streetLightsList.Count; i++) { if (this.streetLightsList[i].secondsLastUpdate + this.streetLightsList[i].secondsTime <= this.stopWatch.ElapsedMilliseconds / 1000) { this.streetLightsList[i].GreenLightsOn = !this.streetLightsList[i].GreenLightsOn; this.streetLightsList[i].secondsLastUpdate = this.stopWatch.ElapsedMilliseconds / 1000; } } #endregion #region Crime, Accidents, Fire and Sickness Management for (int i = 0; i < this.buildingsList.Count; i++) { if (!this.buildingsList[i].isBeingAttended) { if (!this.buildingsList[i].isInmune) { bool canCatchFire = false; bool canCatchSickness = false; for (int k = 0; k < this.buildingsList[i].cellList.Count; k++) { if (this.buildingsList[i].cellList[k].IsFire) { canCatchFire = true; } if (this.buildingsList[i].cellList[k].IsSick) { canCatchSickness = true; } } for (int j = 0; j < this.buildingsList[i].cellList.Count; j++) { bool hasSomething = (this.buildingsList[i].cellList[j].IsCrime || this.buildingsList[i].cellList[j].IsAccident || this.buildingsList[i].cellList[j].IsFire || this.buildingsList[i].cellList[j].IsSick); double tempCrimeProbability = GA.NextDouble(); double tempAccidentProbability = GA.NextDouble(); double tempFireProbability = GA.NextDouble(); double tempSicknessProbability = GA.NextDouble(); double tempInflamabilityProbability = GA.NextDouble(); double tempEpidemicProbability = GA.NextDouble(); if (!hasSomething && tempCrimeProbability <= this.crimeProbability) { this.buildingsList[i].cellList[j].IsCrime = true; } else if (!hasSomething && tempAccidentProbability <= this.accidentProbability) { this.buildingsList[i].cellList[j].IsAccident = true; } else if (!hasSomething && tempFireProbability <= this.fireProbability) { this.buildingsList[i].cellList[j].IsFire = true; } else if (!hasSomething && tempSicknessProbability <= this.sicknessProbability) { this.buildingsList[i].cellList[j].IsSick = true; } else if (!hasSomething && canCatchFire && tempInflamabilityProbability <= this.buildingsList[i].cellList[j].material.inflamability) { this.buildingsList[i].cellList[j].IsFire = true; } else if (!hasSomething && canCatchSickness && tempEpidemicProbability <= this.buildingsList[i].cellList[j].material.inflamability) { this.buildingsList[i].cellList[j].IsSick = true; } } } } } #endregion #region Emergency Vehicle Management for (int i = 0; i < this.buildingsList.Count; i++) { for (int j = 0; j < this.buildingsList[i].cellList.Count; j++) { if (this.buildingsList[i].cellList[j].IsCrime && !this.buildingsList[i].isBeingAttended && !this.buildingsList[i].unitOnItsWay) { if (this.policeStation.unitsAvailable > 0) { this.policeStation.unitsAvailable--; this.buildingsList[i].unitOnItsWay = true; EmergencyVehicle auxVehicle = new EmergencyVehicle("PoliceStationVehicle", this.policeStation.coordParking.X, this.policeStation.coordParking.Y, this.buildingsList[i]); auxVehicle.coordI = this.buildingsList[i].coordParking.X; auxVehicle.coordJ = this.buildingsList[i].coordParking.Y; this.emergencyVehicleList.Add(auxVehicle); this.vehicleList.Add(auxVehicle); this.OnCarEnters(auxVehicle); } } if (this.buildingsList[i].cellList[j].IsFire && !this.buildingsList[i].isBeingAttended && !this.buildingsList[i].unitOnItsWay) { if (this.fireStation.unitsAvailable > 0) { this.fireStation.unitsAvailable--; this.buildingsList[i].unitOnItsWay = true; EmergencyVehicle auxVehicle = new EmergencyVehicle("FireStationVehicle", this.fireStation.coordParking.X, this.fireStation.coordParking.Y, this.buildingsList[i]); auxVehicle.coordI = this.buildingsList[i].coordParking.X; auxVehicle.coordJ = this.buildingsList[i].coordParking.Y; this.emergencyVehicleList.Add(auxVehicle); this.vehicleList.Add(auxVehicle); this.OnCarEnters(auxVehicle); } } if ((this.buildingsList[i].cellList[j].IsAccident || this.buildingsList[i].cellList[j].IsSick) && !this.buildingsList[i].isBeingAttended && !this.buildingsList[i].unitOnItsWay) { if (this.hospital.unitsAvailable > 0) { this.hospital.unitsAvailable--; this.buildingsList[i].unitOnItsWay = true; EmergencyVehicle auxVehicle = new EmergencyVehicle("Ambulance", this.hospital.coordParking.X, this.hospital.coordParking.Y, this.buildingsList[i]); auxVehicle.coordI = this.buildingsList[i].coordParking.X; auxVehicle.coordJ = this.buildingsList[i].coordParking.Y; this.emergencyVehicleList.Add(auxVehicle); this.vehicleList.Add(auxVehicle); this.OnCarEnters(auxVehicle); } } } } for (int i = 0; i < this.emergencyVehicleList.Count; i++) { if (this.emergencyVehicleList[i].ticksSinceArrival > 3 && this.emergencyVehicleList[i].coordI == this.emergencyVehicleList[i].objectiveBuilding.coordParking.X && this.emergencyVehicleList[i].coordJ == this.emergencyVehicleList[i].objectiveBuilding.coordParking.Y) { this.emergencyVehicleList[i].objectiveBuilding.isBeingAttended = true; for (int j = 0; j < this.emergencyVehicleList[i].objectiveBuilding.cellList.Count; j++) { if (this.emergencyVehicleList[i].type.Equals("PoliceStationVehicle") && this.emergencyVehicleList[i].objectiveBuilding.cellList[j].IsCrime) { this.emergencyVehicleList[i].objectiveBuilding.cellList[j].IsCrime = false; break; } else if (this.emergencyVehicleList[i].type.Equals("FireStationVehicle") && this.emergencyVehicleList[i].objectiveBuilding.cellList[j].IsFire) { this.emergencyVehicleList[i].objectiveBuilding.cellList[j].IsFire = false; break; } else if (this.emergencyVehicleList[i].type.Equals("Ambulance") && this.emergencyVehicleList[i].objectiveBuilding.cellList[j].IsAccident) { this.emergencyVehicleList[i].objectiveBuilding.cellList[j].IsAccident = false; break; } else if (this.emergencyVehicleList[i].type.Equals("Ambulance") && this.emergencyVehicleList[i].objectiveBuilding.cellList[j].IsSick) { this.emergencyVehicleList[i].objectiveBuilding.cellList[j].IsSick = false; break; } } bool ready = true; bool isAccident = true; bool isSickness = true; for (int j = 0; j < this.emergencyVehicleList[i].objectiveBuilding.cellList.Count; j++) { if (this.emergencyVehicleList[i].type.Equals("PoliceStationVehicle") && this.emergencyVehicleList[i].objectiveBuilding.cellList[j].IsCrime) { ready = false; } else if (this.emergencyVehicleList[i].type.Equals("FireStationVehicle") && this.emergencyVehicleList[i].objectiveBuilding.cellList[j].IsFire) { ready = false; } else if (this.emergencyVehicleList[i].type.Equals("Ambulance") && this.emergencyVehicleList[i].objectiveBuilding.cellList[j].IsAccident) { ready = false; isAccident = false; } else if (this.emergencyVehicleList[i].type.Equals("Ambulance") && this.emergencyVehicleList[i].objectiveBuilding.cellList[j].IsSick) { ready = false; isSickness = false; } } if (ready) { lock (this) { if (this.emergencyVehicleList[i].type.Equals("PoliceStationVehicle")) { this.mrPoliceStation.Set(); } else if (this.emergencyVehicleList[i].type.Equals("FireStationVehicle")) { this.mrFireStation.Set(); } else if (this.emergencyVehicleList[i].type.Equals("Ambulance")) { this.mrHospital.Set(); if (isAccident) { this.hospital.peopleInList_Accident++; } if (isSickness) { this.hospital.peopleInList_Sickness++; } } this.emergencyVehicleList[i].objectiveBuilding.isBeingAttended = false; this.emergencyVehicleList[i].objectiveBuilding.unitOnItsWay = false; this.OnCarExits(this.emergencyVehicleList[i]); this.emergencyVehicleList.Remove(this.emergencyVehicleList[i]); } } } else if (this.emergencyVehicleList[i].coordI == this.emergencyVehicleList[i].objectiveBuilding.coordParking.X && this.emergencyVehicleList[i].coordJ == this.emergencyVehicleList[i].objectiveBuilding.coordParking.Y) { this.emergencyVehicleList[i].ticksSinceArrival++; } } #endregion #region Common Vehicle Management for (int i = 0; i < this.streetList.Count; i++) { double auxProbability = GA.NextDouble(); if (this.streetList[i].entrance && auxProbability <= this.entranceProbability) { Vehicle auxVehicle = new Vehicle(this.streetList[i].coordI, this.streetList[i].coordJ); this.vehicleList.Add(auxVehicle); this.OnCarEnters(auxVehicle); } } for (int i = 0; i < this.vehicleList.Count; i++) { try { if (this.vehicleList[i].automaticControl) { StreetCell currentStreet = MatchStreetWithCar(this.vehicleList[i].coordI, this.vehicleList[i].coordJ); int previousCoordI = this.vehicleList[i].coordI; int previousCoordJ = this.vehicleList[i].coordJ; this.vehicleList[i].coordI = this.vehicleList[i].coordI + currentStreet.horizontalVector; this.vehicleList[i].coordJ = this.vehicleList[i].coordJ + currentStreet.verticalVector; if (this.vehicleList[i].coordI < 0 || this.vehicleList[i].coordJ < 0 || this.vehicleList[i].coordI > this.map.GetLength(0) - 1 || this.vehicleList[i].coordJ > this.map.GetLength(1) - 1) { this.OnCarExits(this.vehicleList[i]); } else if (this.VerifyColision(this.vehicleList[i]) || !this.map[this.vehicleList[i].coordI, this.vehicleList[i].coordJ].transitable) { this.vehicleList[i].coordI = previousCoordI; this.vehicleList[i].coordJ = previousCoordJ; } else if (((StreetCell)this.map[this.vehicleList[i].coordI, this.vehicleList[i].coordJ]).isStreetLight && !((StreetCell)this.map[previousCoordI, previousCoordJ]).isStreetLight) { if (((StreetCell)this.map[this.vehicleList[i].coordI, this.vehicleList[i].coordJ]).horizontalVector != ((StreetCell)this.map[previousCoordI, previousCoordJ]).horizontalVector && ((StreetCell)this.map[this.vehicleList[i].coordI, this.vehicleList[i].coordJ]).verticalVector != ((StreetCell)this.map[previousCoordI, previousCoordJ]).verticalVector) { double auxProbability = GA.NextDouble(); if (auxProbability <= 0.2) { this.OnMoveCar(this.vehicleList[i]); } else { this.vehicleList[i].coordI = previousCoordI; this.vehicleList[i].coordJ = previousCoordJ; } } else { this.OnMoveCar(this.vehicleList[i]); } } else { this.OnMoveCar(this.vehicleList[i]); } } } catch { } } #endregion } } }
protected void OnCarExits(Vehicle vehicleIn) { if (this.carExits != null) { this.carExits(vehicleIn); } }
public bool VerifyColision(Vehicle vehicleIn) { for (int i = 0; i < this.vehicleList.Count; i++) { if (!this.vehicleList[i].Equals(vehicleIn) && this.vehicleList[i].coordI == vehicleIn.coordI && this.vehicleList[i].coordJ == vehicleIn.coordJ) { return true; } } return false; }
private void MoveCar(Vehicle vehicleIn) { this.Dispatcher.BeginInvoke((Action)(() => { for (int i = 0; i < this.vehicleList.Count; i++) { if (this.vehicleList[i].logicVehicle.Equals(vehicleIn)) { Grid.SetColumn(vehicleList[i], vehicleList[i].logicVehicle.coordI); Grid.SetRow(vehicleList[i], vehicleList[i].logicVehicle.coordJ); } } })); }
private void ExitCar(Vehicle vehicleIn) { this.Dispatcher.BeginInvoke((Action)(() => { for (int i = 0; i < this.vehicleList.Count; i++) { if (this.vehicleList[i].logicVehicle.Equals(vehicleIn)) { this.myCityGrid.Children.Remove(this.vehicleList[i]); this.miCiudad.vehicleList.Remove(this.vehicleList[i].logicVehicle); this.vehicleList.Remove(this.vehicleList[i]); break; } } })); }
private void EnterCar(Vehicle vehicleIn) { this.Dispatcher.BeginInvoke((Action)(() => { VehicleWPF auxCar = new VehicleWPF(vehicleIn); Grid.SetColumn(auxCar, vehicleIn.coordI); Grid.SetRow(auxCar, vehicleIn.coordJ); this.vehicleList.Add(auxCar); this.myCityGrid.Children.Add(auxCar); })); }