public void RoadNetworkTraversal_LaneSectionTraversalSmokeTest() { var roadNetwork = LoadTestFile("CircleCourse"); var firstRoad = roadNetwork.AllRoads.First(); var firstRoadId = new NativeString64(firstRoad.roadId); var numLaneSections = 3; var numLoops = 10; var traversalState = new TraversalState(roadNetwork, firstRoad.roadId, 0, -1, TraversalDirection.Forward); for (var step = 0; step < numLaneSections - 1; ++step) { Assert.IsTrue(RoadNetworkTraversal.TryAdvanceOneLaneSection(roadNetwork, ref traversalState, false), $"Failed to advance to step #{step} - {numLaneSections} total"); Assert.AreEqual(-1, traversalState.LaneId); } // Without allowing looping, we shouldn't be allowed to go to the next lane section Assert.IsFalse(RoadNetworkTraversal.TryAdvanceOneLaneSection(roadNetwork, ref traversalState, false)); Assert.IsTrue(RoadNetworkTraversal.TryAdvanceOneLaneSection(roadNetwork, ref traversalState, true)); Assert.AreEqual(firstRoadId, traversalState.RoadId); for (var step = 0; step < numLaneSections * numLoops; ++step) { Assert.IsTrue(RoadNetworkTraversal.TryAdvanceOneLaneSection(roadNetwork, ref traversalState, true), $"Failed to advance to step #{step} - {numLaneSections} total"); Assert.AreEqual(-1, traversalState.LaneId); } Assert.AreEqual(firstRoadId, traversalState.RoadId); }
void Start() { SetupConfig(); // --- RoadNetworkGenerator.DebugData debugData; RoadNetworkGenerator.Generate(out segments, out quadtree, out debugData); Debug.Log(segments.Count + " segments"); // --- mask = ((generateHighways) ? RoadNetworkTraversal.HIGHWAYS_MASK : 0) | ((generateStreets) ? RoadNetworkTraversal.STREETS_MASK : 0); float minX = float.MaxValue, maxX = -float.MaxValue, minY = float.MaxValue, maxY = -float.MaxValue; HashSet <Segment> visited = new HashSet <Segment>(); foreach (var segment in segments) { RoadNetworkTraversal.PreOrder(segment, (a) => { minX = Mathf.Min(Mathf.Min(a.Start.x, a.End.x), minX); minY = Mathf.Min(Mathf.Min(a.Start.y, a.End.y), minY); maxX = Mathf.Max(Mathf.Max(a.Start.x, a.End.x), maxX); maxY = Mathf.Max(Mathf.Max(a.Start.y, a.End.y), maxY); return(true); }, mask, ref visited); } Vector2 size = new Vector2(maxX - minX, maxY - minY); Vector2 center = new Vector2(minX, minY); boundingBox = new Rect(center, size); finished = true; }
public void RoadNetworkTraversal_RoadGroupingSmokeTest() { var roadNetwork = LoadTestFile("Crossing8Course"); var traversalState = new TraversalState(roadNetwork); var numGroups = 0; while (traversalState.AllRoadIds.AnyNotTraversed()) { var roadId = traversalState.AllRoadIds.GetNotTraversed(); if (roadNetwork.GetRoadById(roadId).junction != "-1") { traversalState.AllRoadIds.Traverse(roadId); continue; } var roadGroup = RoadNetworkTraversal.IdentifyGraphEdgeGroup(roadNetwork, traversalState, roadId); numGroups++; Assert.AreEqual(3, roadGroup.numRoads); Assert.AreEqual(TraversalDirection.Forward, roadGroup.startingDirection); } Assert.AreEqual(2, numGroups); }
void Update() { if (action == 0) { return; } if (action == 1) { if (!end) { end = Input.GetKeyDown(KeyCode.F5); } if (!step) { step = Input.GetKeyDown(KeyCode.F10); } if (!step && !end) { return; } if (end) { RoadNetworkGenerator.EndInteractiveGeneration(ref context); action = 0; } else { if (!RoadNetworkGenerator.InteractiveGenerationStep(speed, ref context)) { action = 0; } } foreach (Segment segment in context.segments) { RoadNetworkTraversal.PreOrder(segment, Visitor, mask, ref visited); } RemoveIcons(); if (context.debugData.intersections.Count > 0 || context.debugData.snaps.Count > 0 || context.debugData.intersectionsRadius.Count > 0) { foreach (Vector2 intersection in context.debugData.intersections) { iconGOs.Add(CreateIcon(intersection, iconSize, downscaleFactor, z, intersectionIcon)); } foreach (Vector2 snap in context.debugData.snaps) { iconGOs.Add(CreateIcon(snap, iconSize, downscaleFactor, z, snapIcon)); } foreach (Vector2 intersectionRadius in context.debugData.intersectionsRadius) { iconGOs.Add(CreateIcon(intersectionRadius, iconSize, downscaleFactor, z, intersectionRadiusIcon)); } context.debugData.intersections.Clear(); context.debugData.snaps.Clear(); context.debugData.intersectionsRadius.Clear(); } step = false; } }
void Start() { if (roadNetwork == null) { Debug.LogError("RandomSettlementsSpawner needs a reference to a RoadNetwork"); return; } if (!roadNetwork.Finished) { Debug.LogError("RandomSettlementsSpawner script can only execute after RoadNetwork (Configure execution order that in Edit > Project Settings > Script Execution Order)"); return; } if (heightmapGameObject != null) { heightmap = UnityEngineHelper.GetInterface <IHeightmap>(heightmapGameObject); } if (heightmap == null) { Debug.LogError("RandomSettlementsSpawner needs a reference to a game object containing at least one component that implements IHeightmap"); return; } if (!heightmap.Finished()) { Debug.LogError("RandomSettlementsSpawner script can only execute after the components that implements IHeightmap (Configure execution order that in Edit > Project Settings > Script Execution Order)"); return; } List <IAllotmentBuilder> allotmentBuilders = null; if (allotmentBuilderGameObject != null) { allotmentBuilders = UnityEngineHelper.GetInterfaces <IAllotmentBuilder>(allotmentBuilderGameObject); } if (allotmentBuilders == null) { Debug.LogError("RoadDensitySettlementSpawner needs a reference to a game object containing at least one component that implements IAllotmentBuilder"); return; } Context context = new Context(roadNetwork.Quadtree); HashSet <Segment> visited = new HashSet <Segment>(); foreach (var segment in roadNetwork.Segments) { RoadNetworkTraversal.PreOrder(segment, ref context, -1, SegmentVisitor, roadNetwork.Mask, ref visited); } GameObject allotmentsGO = new GameObject("Allotments"); allotmentsGO.transform.parent = transform; foreach (var allotment in context.allotments) { foreach (var allotmentBuilder in allotmentBuilders) { GameObject allotmentGO = allotmentBuilder.Build(allotment, heightmap); allotmentGO.transform.parent = allotmentsGO.transform; } } Debug.Log(context.allotments.Count + " allotments spawned"); }
void Start() { if (roadNetwork == null) { Debug.LogError("RoadDensityBasedSettlementSpawner needs a reference to a RoadNetwork"); return; } if (!roadNetwork.Finished) { Debug.LogError("RoadDensityBasedSettlementSpawner script can only execute after RoadNetwork (Configure execution order that in Edit > Project Settings > Script Execution Order)"); return; } if (roadDensityMap == null) { Debug.LogError("RoadDensityBasedSettlementSpawner needs a reference to a RoadDensityMap"); return; } if (!roadDensityMap.Finished()) { Debug.LogError("RoadDensityBasedSettlementSpawner script can only execute after RoadDensityMap (Configure execution order that in Edit > Project Settings > Script Execution Order)"); return; } if (heightmapGameObject != null) { heightmap = UnityEngineHelper.GetInterface <IHeightmap>(heightmapGameObject); } if (heightmap == null) { Debug.LogError("RoadDensityBasedSettlementSpawner needs a reference to a game object containing at least one component that implements IHeightmap"); return; } if (!heightmap.Finished()) { Debug.LogError("RoadDensityBasedSettlementSpawner script can only execute after the components that implements IHeightmap (Configure execution order that in Edit > Project Settings > Script Execution Order)"); return; } List <IAllotmentBuilder> allotmentBuilders = null; if (allotmentBuilderGameObject != null) { allotmentBuilders = UnityEngineHelper.GetInterfaces <IAllotmentBuilder>(allotmentBuilderGameObject); } if (allotmentBuilders == null) { Debug.LogError("RoadDensityBasedSettlementSpawner needs a reference to a game object containing at least one component that implements IAllotmentBuilder"); return; } { float minThreshold = -float.MaxValue; foreach (var densityTier in densityTiers) { if (densityTier.threshold < minThreshold) { Debug.LogError("Density tier has a threshold smaller than it's predecessor"); return; } minThreshold = densityTier.threshold; } } minAllotmentWidth = Allotment.GetWidth(Config.allotmentMinHalfDiagonal, Config.allotmentMinAspect); allotments = new List <Tuple <float, Allotment> >(); HashSet <Segment> visited = new HashSet <Segment>(); foreach (var segment in roadNetwork.Segments) { RoadNetworkTraversal.PreOrder(segment, SegmentVisitor, roadNetwork.Mask, ref visited); } if (maxNumAllotments > 0 && allotments.Count > maxNumAllotments) { allotments.Sort((a, b) => b.Item1.CompareTo(a.Item1)); allotments = allotments.GetRange(0, maxNumAllotments); } GameObject allotmentsGO = new GameObject("Allotments"); allotmentsGO.transform.parent = transform; { foreach (var allotment in allotments) { foreach (var allotmentBuilder in allotmentBuilders) { GameObject allotmentGO = allotmentBuilder.Build(allotment.Item2, heightmap); allotmentGO.transform.parent = allotmentsGO.transform; } } } Debug.Log(allotments.Count + " allotments spawned"); }