protected bool makeStreets( RoadSegment <MetaInformation> approvedSeg, List <RoadSegment <MetaInformation> > potentialSegs) { // Vaialble declarations bool branchAppeared = false; var pendingBranchSegs = new List <RoadSegment <MetaInformation> >(); float branchDigreeDiff = 0f; Road approvedRoad = approvedSeg.getLastRoad(); // If segment grows beyond the fixed length, // several branches will appear. if (approvedSeg.TotalLength >= Config.STREET_SEGMENT_MAX_LENGTH) { branchAppeared = true; } // growth segment will grow along last segment. var pendingGrowthSegs = new List <RoadSegment <MetaInformation> >(); float growthDigreeDiff = Config.STREET_GROWTH_MAX_DEGREE - Config.STREET_GROWTH_MIN_DEGREE; // branch segment will make a branch from the end. if (branchAppeared) { branchDigreeDiff = Config.STREET_BRANCH_MAX_DEGREE - Config.STREET_BRANCH_MIN_DEGREE; } for (int i = 0; i < 4; ++i) { // get a growth digree randomly float growthDigree = Random.Range(-growthDigreeDiff, growthDigreeDiff); growthDigree += (growthDigree > 0) ? Config.STREET_GROWTH_MIN_DEGREE : -Config.STREET_GROWTH_MIN_DEGREE; // figure out the end point of new road var rotation = Quaternion.Euler(0, growthDigree, 0); var potentialRoadEnd = approvedRoad.end.position + rotation * approvedRoad.Direction.normalized * Config.STREET_DEFAULT_LENGTH; // create new road var potentialRoad = new Road(approvedRoad.end, potentialRoadEnd, Config.STREET_DEFAULT_WIDTH); var metaInfo = new StreetMetaInfo(); metaInfo.populationDensity = perlin.getValue(potentialRoadEnd.x, potentialRoadEnd.z); var potentialSeg = new RoadSegment <MetaInformation>(0, potentialRoad, metaInfo); pendingGrowthSegs.Add(potentialSeg); if (branchAppeared) { // get a branch digree randomly float branchDigree = Random.Range(-branchDigreeDiff, branchDigreeDiff); branchDigree += (branchDigree > 0) ? Config.STREET_BRANCH_MIN_DEGREE : -Config.STREET_BRANCH_MIN_DEGREE; // Original solution 1: // While a branch is being generated, it will appear at // the centre of whole segment. This is to avoid those // which are too close to other roads. var branchStart = approvedSeg.roads[approvedSeg.roads.Count / 2].start; // figure out the end point of new branch road rotation = Quaternion.Euler(0, branchDigree, 0); potentialRoadEnd = branchStart.position + rotation * approvedRoad.Direction.normalized * Config.STREET_DEFAULT_LENGTH; // create new branch road // appears where people live only var populationDensity = perlin.getValue(potentialRoadEnd.x, potentialRoadEnd.z); if (populationDensity > Config.MIN_STREET_APPEAR_POPULATION_DENSITY_VALUE) { potentialRoad = new Road(branchStart, potentialRoadEnd, Config.STREET_DEFAULT_WIDTH); var streetMetaInfo = new StreetMetaInfo(); streetMetaInfo.populationDensity = populationDensity; potentialSeg = new RoadSegment <MetaInformation>(0, potentialRoad, streetMetaInfo); pendingBranchSegs.Add(potentialSeg); } } } // pick out the road where has the most population density var maxDensityGrowthRoad = pendingGrowthSegs .OrderByDescending(x => ((StreetMetaInfo)x.metaInformation).populationDensity) .FirstOrDefault(); if (branchAppeared) { var maxDensityBranchRoad = pendingBranchSegs .OrderByDescending(x => ((StreetMetaInfo)x.metaInformation).populationDensity) .Take(1); // add the street potentialSegs.AddRange(maxDensityBranchRoad); // as for growth road, add it to result directly if (maxDensityBranchRoad.Count() > 0) { potentialSegs.Add(maxDensityGrowthRoad); } } else { // segment grows approvedSeg.grow(maxDensityGrowthRoad.getLastRoad()); approvedSeg.metaInformation = maxDensityGrowthRoad.metaInformation; potentialSegs.Add(approvedSeg); } return(potentialSegs.Count > 0); }
protected bool makeHighwaysByPopulationDensity( RoadSegment <MetaInformation> approvedSeg, List <RoadSegment <MetaInformation> > potentialSegs) { // Vaialble declarations bool highwayBranchAppeared = false; var pendingBranchSegs = new List <RoadSegment <MetaInformation> >(); float branchDigreeDiff = 0f; // If segment grows beyond the fixed length, // several branches will appear. if (approvedSeg.TotalLength >= Config.HIGHWAY_SEGMENT_MAX_LENGTH) { highwayBranchAppeared = true; } // growth segment will grow along last segment. var pendingGrowthSegs = new List <RoadSegment <MetaInformation> >(); float growthDigreeDiff = Config.HIGHWAY_GROWTH_MAX_DEGREE - Config.HIGHWAY_GROWTH_MIN_DEGREE; // branch segment will make a branch from the end. if (highwayBranchAppeared) { branchDigreeDiff = Config.HIGHWAY_BRANCH_MAX_DEGREE - Config.HIGHWAY_BRANCH_MIN_DEGREE; } else { branchDigreeDiff = Config.STREET_BRANCH_MAX_DEGREE - Config.STREET_BRANCH_MIN_DEGREE; } Road approvedRoad = approvedSeg.getLastRoad(); for (int i = 0; i < 4; ++i) { // get a growth digree randomly float growthDigree = Random.Range(-growthDigreeDiff, growthDigreeDiff); growthDigree += (growthDigree > 0) ? Config.HIGHWAY_GROWTH_MIN_DEGREE : -Config.HIGHWAY_GROWTH_MIN_DEGREE; // figure out the end point of new road var rotation = Quaternion.Euler(0, growthDigree, 0); var potentialRoadEnd = approvedRoad.end.position + rotation * approvedRoad.Direction.normalized * Config.HIGHWAY_DEFAULT_LENGTH; // create new road var potentialRoad = new Road(approvedRoad.end, potentialRoadEnd, Config.HIGHWAY_DEFAULT_WIDTH); var metaInfo = new HighwayMetaInfo(); metaInfo.populationDensity = perlin.getValue(potentialRoadEnd.x, potentialRoadEnd.z); var potentialSeg = new RoadSegment <MetaInformation>(0, potentialRoad, metaInfo); pendingGrowthSegs.Add(potentialSeg); if (highwayBranchAppeared) { // get a branch digree randomly float branchDigree = Random.Range(-branchDigreeDiff, branchDigreeDiff); branchDigree += (branchDigree > 0) ? Config.HIGHWAY_BRANCH_MIN_DEGREE : -Config.HIGHWAY_BRANCH_MIN_DEGREE; // figure out the end point of new branch road rotation = Quaternion.Euler(0, branchDigree, 0); potentialRoadEnd = approvedRoad.end.position + rotation * approvedRoad.Direction.normalized * Config.HIGHWAY_DEFAULT_LENGTH; // create new branch road potentialRoad = new Road(approvedRoad.end, potentialRoadEnd, Config.HIGHWAY_DEFAULT_WIDTH); metaInfo = new HighwayMetaInfo(); metaInfo.populationDensity = perlin.getValue(potentialRoadEnd.x, potentialRoadEnd.z); potentialSeg = new RoadSegment <MetaInformation>(0, potentialRoad, metaInfo); pendingBranchSegs.Add(potentialSeg); } else { // get a branch digree randomly float branchDigree = Random.Range(-branchDigreeDiff, branchDigreeDiff); branchDigree += (branchDigree > 0) ? Config.STREET_BRANCH_MIN_DEGREE : -Config.STREET_BRANCH_MIN_DEGREE; // figure out the end point of new branch road rotation = Quaternion.Euler(0, branchDigree, 0); potentialRoadEnd = approvedRoad.end.position + rotation * approvedRoad.Direction.normalized * Config.STREET_DEFAULT_LENGTH; // create new branch road // appears where people live only var populationDensity = perlin.getValue(potentialRoadEnd.x, potentialRoadEnd.z); if (populationDensity > Config.MIN_POPULATION_DENSITY_VALUE + 0.15f) { potentialRoad = new Road(approvedRoad.end, potentialRoadEnd, Config.STREET_DEFAULT_WIDTH); var streetMetaInfo = new StreetMetaInfo(); streetMetaInfo.populationDensity = populationDensity; potentialSeg = new RoadSegment <MetaInformation>(0, potentialRoad, streetMetaInfo); pendingBranchSegs.Add(potentialSeg); } } } // pick out the road where has the most population density var maxDensityGrowthRoad = pendingGrowthSegs .OrderByDescending(x => ((HighwayMetaInfo)x.metaInformation).populationDensity) .FirstOrDefault(); if (highwayBranchAppeared) { var maxDensityBranchRoad = pendingBranchSegs .OrderByDescending(x => ((HighwayMetaInfo)x.metaInformation).populationDensity) .Take(1); // as for growth road, add it to result directly potentialSegs.Add(maxDensityGrowthRoad); // as for branch road, add if it has higher population density var growthRoadDensity = ((HighwayMetaInfo)maxDensityGrowthRoad.metaInformation).populationDensity; foreach (var road in maxDensityBranchRoad) { if (((HighwayMetaInfo)road.metaInformation).populationDensity > growthRoadDensity) { potentialSegs.Add(road); } else { break; } } } else { var maxDensityBranchRoad = pendingBranchSegs .OrderByDescending(x => ((StreetMetaInfo)x.metaInformation).populationDensity) .Take(1); // segment grows approvedSeg.grow(maxDensityGrowthRoad.getLastRoad()); approvedSeg.metaInformation = maxDensityGrowthRoad.metaInformation; potentialSegs.Add(approvedSeg); // add the street potentialSegs.AddRange(maxDensityBranchRoad); } return(potentialSegs.Count > 0); }