static public uint EnvironmentBoostMax(uint cellID_x, uint cellID_y) //Requires an active Grid and BuildingsManager (which should always exist in this game, anyway) { int maxBoost = 0; List <ulong> coveringParks = Grid.grid.GetInfrastructureIDsCoveringCell(cellID_x, cellID_y, InfrastructureService.parks); foreach (ulong id in coveringParks) { Park_1 park = (Park_1)GameManager.buildingsMan.GetInfrastructureBuilding(InfrastructureService.parks, id); if (park != null) { maxBoost = Mathf.Max((int)park.environmentHappinessBoost, maxBoost); } } return((uint)maxBoost); }
void UpdateCitizenHappiness(Citizen citizen) //TODO consider moving this method to Citizen class. { Happiness newHappiness = new Happiness(50); int changeDirection = (int)Mathf.Sign((int)citizen.homeAddress.housingQuality - (int)citizen.happiness.home); newHappiness.home = (uint)Mathf.Clamp((int)citizen.happiness.home + (changeDirection * Citizen.happinessChangeRatePerDay), 0, 100); //compute environment happiness. We do that according to pollution in both house and work. //Work pollution contributes to happiness half that of home. uint homeEnvironmentHappiness = 0, workEnvironmentHappiness = 0; if (citizen.homeAddress != null) //Again, shouldn't happen, but better safe than sorry. { float pollutionAtHome = Grid.grid.pollutionLayer.GetCellValue(citizen.homeAddress.occupiedCell[0], citizen.homeAddress.occupiedCell[0]); homeEnvironmentHappiness = (uint)Mathf.RoundToInt((float)Globals.baseEnvironmentHappiness - ((float)Globals.baseEnvironmentHappiness * (pollutionAtHome - Citizen.minPollutionToAffectHealthHappiness) / (Citizen.maxPollutionToAffectHealthHappiness - Citizen.minPollutionToAffectHealthHappiness))); homeEnvironmentHappiness = (uint)Mathf.Clamp((int)homeEnvironmentHappiness, 0, (int)Globals.baseEnvironmentHappiness); homeEnvironmentHappiness += Park_1.EnvironmentBoostMax(citizen.homeAddress.occupiedCell[0], citizen.homeAddress.occupiedCell[1]); workEnvironmentHappiness = homeEnvironmentHappiness; //incase citizen.workAddress == null. } if (citizen.workAddress != null) { float pollutionAtWork = Grid.grid.pollutionLayer.GetCellValue(citizen.workAddress.GetComponent <Building>().occupiedCell[0], citizen.workAddress.GetComponent <Building>().occupiedCell[0]); workEnvironmentHappiness = (uint)Mathf.RoundToInt((float)Globals.baseEnvironmentHappiness - ((float)Globals.baseEnvironmentHappiness * (pollutionAtWork - Citizen.minPollutionToAffectHealthHappiness) / (Citizen.maxPollutionToAffectHealthHappiness - Citizen.minPollutionToAffectHealthHappiness))); workEnvironmentHappiness = (uint)Mathf.Clamp((int)workEnvironmentHappiness, 0, (int)Globals.baseEnvironmentHappiness); workEnvironmentHappiness += Park_1.EnvironmentBoostMax(citizen.workAddress.GetComponent <Building>().occupiedCell[0], citizen.workAddress.GetComponent <Building>().occupiedCell[1]); } newHappiness.environment = (uint)Mathf.RoundToInt((float)(2 * homeEnvironmentHappiness + workEnvironmentHappiness) / 3.0f); if (citizen.workAddress == null) //unemployed { newHappiness.job = 0; } else { newHappiness.job = citizen.workAddress.WorkplaceQuality(); } citizen.happiness.UpdateHappiness(newHappiness); }