private void Start() { Road r0 = segFactory.CreateRoad(new Vector3(0, 0, -10), new Vector3(0, 0, 0), 0, RoadType.Street); Road r1 = segFactory.CreateRoad(new Vector3(-10, 0, 10), new Vector3(0, 0, 0), 0, RoadType.Street); Road r2 = segFactory.CreateRoad(new Vector3(10, 0, 10), new Vector3(0, 0, 0), 0, RoadType.Street); print(LotsGenerator.GetAngle(r0.transform.rotation, r1.transform.rotation, true, false)); print(LotsGenerator.GetAngle(r0.transform.rotation, r2.transform.rotation, true, false)); }
void SetUpNewIntersection(Road road, Road otherRoad, Vector3 intersection, Junction j, RoadMap map) { // split road that is being intersected Road newRoad = segFactory.CreateRoad(intersection, otherRoad.end, otherRoad.t, otherRoad.type); otherRoad.MoveEnd(intersection); // set up links between roads newRoad.next.AddRange(otherRoad.next); newRoad.Parent = otherRoad; newRoad.prev.Add(new Road.Neighbour(road, true)); road.next.Add(new Road.Neighbour(newRoad, true)); road.next.Add(new Road.Neighbour(otherRoad, false)); foreach (Road.Neighbour n in otherRoad.next) { n.r.ReplaceNeighbour(otherRoad, newRoad); //n.r.RemoveNeighbour(otherRoad); //n.r.prev.Add(new Road.Neighbour(newRoad, true)); } otherRoad.next.Clear(); otherRoad.next.Add(new Road.Neighbour(newRoad, true)); otherRoad.next.Add(new Road.Neighbour(road, false)); j.AddIncoming(road); j.AddOutgoing(newRoad); j.AddIncoming(otherRoad); map.AddRoad(newRoad); }
public List <Road> Generate(Road prevSegment, RoadMap roadMap) { var newRoads = new List <Road>(); if (prevSegment.severed) { return(newRoads); } if (segFactory == null) { Initialize(); } Road continueStraight = segFactory.CreateRoad( prevSegment.end, prevSegment.transform.localRotation, prevSegment.length, prevSegment.t + 1, prevSegment.type ); float straightPop = Heatmap.Value(continueStraight.end); if (prevSegment.type == RoadType.Highway) { // generate a random deviation and compare to going straight, // chosing the road that leads to the highest population float newAngle = prevSegment.transform.localEulerAngles.y + CityConfig.RandomStraightAngle(); Road randomStraight = segFactory.CreateRoad( prevSegment.end, Quaternion.Euler(prevSegment.transform.localEulerAngles.x, newAngle, 0), prevSegment.length, prevSegment.t + 1, prevSegment.type ); float randomPop = Heatmap.Value(randomStraight.end); float roadPop; Road straight; if (randomPop > straightPop) { newRoads.Add(randomStraight); roadPop = randomPop; straight = randomStraight; DestroyImmediate(continueStraight.gameObject); } else { newRoads.Add(continueStraight); roadPop = straightPop; straight = continueStraight; DestroyImmediate(randomStraight.gameObject); } // highway branches off highway if (roadPop > CityConfig.HIGHWAY_BRANCH_POPULATION_THRESHOLD) { Junction j = null; if (Random.value < CityConfig.HIGHWAY_BRANCH_PROBABILITY) { Road leftRoad = AddBranch( prevSegment, straight, j, prevSegment.transform.localEulerAngles.y - 90 + CityConfig.RandomBranchAngle(), prevSegment.length, prevSegment.t + 1, prevSegment.type ); newRoads.Add(leftRoad); } if (Random.value < CityConfig.HIGHWAY_BRANCH_PROBABILITY) { Road rightRoad = AddBranch( prevSegment, straight, j, prevSegment.transform.localEulerAngles.y + 90 + CityConfig.RandomBranchAngle(), prevSegment.length, prevSegment.t + 1, prevSegment.type ); newRoads.Add(rightRoad); } } } else if (straightPop > CityConfig.STREET_BRANCH_POPULATION_THRESHOLD) { // do not delete continueStraight newRoads.Add(continueStraight); } else { DestroyImmediate(continueStraight.gameObject); } // street branches off either highway or streets if (straightPop > CityConfig.STREET_BRANCH_POPULATION_THRESHOLD) { int t = (prevSegment.type == RoadType.Highway) ? CityConfig.STREET_FROM_HIGHWAY_DELAY : 1; Junction j = null; if (Random.value < CityConfig.STREET_BRANCH_PROBABILITY) { Road leftRoad = AddBranch( prevSegment, continueStraight, j, prevSegment.transform.localEulerAngles.y - 90 + CityConfig.RandomBranchAngle(), CityConfig.STREET_SEGMENT_LENGTH, prevSegment.t + t, RoadType.Street ); newRoads.Add(leftRoad); } if (Random.value < CityConfig.STREET_BRANCH_PROBABILITY) { Road leftRoad = AddBranch( prevSegment, continueStraight, j, prevSegment.transform.localEulerAngles.y + 90 + CityConfig.RandomBranchAngle(), CityConfig.STREET_SEGMENT_LENGTH, prevSegment.t + t, RoadType.Street ); newRoads.Add(leftRoad); } } // set up links between roads foreach (Road r in newRoads) { r.Parent = prevSegment; prevSegment.next.Add(new Road.Neighbour(r, true)); foreach (Road r_other in newRoads) { if (r_other.id != r.id) { r.prev.Add(new Road.Neighbour(r_other, false)); } } } return(newRoads); }
public void Generate(int seed) { Random.InitState(seed); Heatmap.Seed(seed); if (CityConfig.SHOW_HEATMAP) { heatmapVis.Generate(); } PriorityQueue <Road> priorityQ = new PriorityQueue <Road>(); // set up first two segments in centre of map Road root1 = segFactory.CreateRoad( new Vector3(0, 0, 0), new Vector3(CityConfig.HIGHWAY_SEGMENT_LENGTH, 0, 0), 0, RoadType.Highway); Road root2 = segFactory.CreateRoad( new Vector3(0, 0, 0), new Vector3(-CityConfig.HIGHWAY_SEGMENT_LENGTH, 0, 0), 0, RoadType.Highway); root1.Parent = root2; root2.Parent = root1; priorityQ.Enqueue(root1); priorityQ.Enqueue(root2); int roadCount = 0; while (priorityQ.Count() > 0) { Road nextRoad = priorityQ.Dequeue(); if (roadCount < CityConfig.MAX_ROADS) { // check local constraints bool accepted = localConstraints.Check(nextRoad, map); if (accepted) { // set up branch links // activate road map.AddRoad(nextRoad); foreach (Junction j in nextRoad.attachedSegments) { map.AddJunction(j); } roadCount++; // generate new possible branches according to global goals List <Road> goals = globalGoals.Generate(nextRoad, map); foreach (Road r in goals) { priorityQ.Enqueue(r); } } else { DestroyImmediate(nextRoad.gameObject); } } else { // delete leftover roads after maximum has been exceeded DestroyImmediate(nextRoad.gameObject); } } print("Created " + roadCount + " roads!"); lotsGenerator.Generate(map.allRoads); }