public IEnumerator BuildFromIslandStructure(CartographicIsland islandStructure, GameObject islandGameObject) { int randomSeed = islandStructure.Name.GetHashCode() + 200; _random = new System.Random(randomSeed); islandGameObject.name = islandStructure.Name; Island islandComponent = islandGameObject.AddComponent <Island>(); Interactable interactable = islandGameObject.AddComponent <Interactable>(); islandGameObject.transform.parent = UIManager.Instance.BundleContainer.transform; islandComponent.CartographicIsland = islandStructure; islandStructure.IslandGameObject = islandGameObject; interactable.Type = InteractableType.Bundle; interactable.Focusable = true; interactable.Selectable = true; yield return(BuildRegionAreas(islandComponent)); yield return(BuildIslandCoastline(islandComponent)); yield return(BuildDock(islandComponent, DockType.Import)); yield return(BuildDock(islandComponent, DockType.Export)); SetLayerRecursively(islandGameObject, LayerMask.NameToLayer("Ignore Raycast")); AddRegionColliders(islandComponent.Regions); SetIslandPosition(islandComponent); AddIslandCollider(islandComponent); UIManager.Instance.Visualization.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f); }
private void ConstructRegion(Package package, CartographicIsland island, Dictionary <int, VFace> startCells, int maxCount) { float cohesionMultiplier = (float)(package.CompilationUnitCount / maxCount); cohesionMultiplier *= cohesionMultiplier * MaxCohesion; cohesionMultiplier = Mathf.Max(MinCohesion, cohesionMultiplier); var newCells = ConstructRegionFromPackage(package, island, startCells, cohesionMultiplier); UpdateAndFuseCells(startCells, newCells); }
private void LinkDependencyVertex(Bundle bundle, CartographicIsland island) { var dependencyGraph = bundle.OSGiProject.DependencyGraph; List <GraphVertex> allVertices = dependencyGraph.Vertices.ToList(); GraphVertex vertex = allVertices.Find(v => string.Equals(v.Name, bundle.Name)); if (vertex != null) { vertex.Island = island; island.DependencyVertex = vertex; } }
public IEnumerator AdjustDockTransform(Island island) { CartographicIsland islandStructure = island.CartographicIsland; var dependencyGraph = islandStructure.Bundle.OSGiProject.DependencyGraph; GraphVertex vertex = islandStructure.DependencyVertex; GameObject importDock = island.ImportDock; GameObject exportDock = island.ExportDock; yield return(FindSuitablePosition2D(island, importDock, exportDock)); if (vertex == null) { yield break; } float importSize = 0.04f * 25f; // Don't hardcode. float exportSize = 0.04f * 25f; // Don't hardcode. IEnumerable <GraphEdge> outEdges; dependencyGraph.TryGetOutEdges(vertex, out outEdges); List <GraphEdge> edgeList = outEdges.ToList(); importDock.transform.localScale = new Vector3(importSize, importSize, importSize); DependencyDock dockComponent = importDock.GetComponent <DependencyDock>(); dockComponent.setDockType(DockType.Import); foreach (GraphEdge e in edgeList) { GameObject ed = e.Target.Island.IslandGameObject.GetComponent <Island>().ExportDock; dockComponent.addDockConnection(ed.GetComponent <DependencyDock>(), e.Weight); } dependencyGraph.TryGetInEdges(vertex, out outEdges); edgeList = outEdges.ToList(); float eDockWidth = exportDock.GetComponent <MeshFilter>().sharedMesh.bounds.size.x *exportSize; float iDockWidth = importDock.GetComponent <MeshFilter>().sharedMesh.bounds.size.x *importSize; exportDock.transform.localScale = new Vector3(exportSize, exportSize, exportSize); dockComponent = exportDock.GetComponent <DependencyDock>(); dockComponent.setDockType(DockType.Export); foreach (GraphEdge e in edgeList) { GameObject id = e.Source.Island.IslandGameObject.GetComponent <Island>().ImportDock; dockComponent.addDockConnection(id.GetComponent <DependencyDock>(), e.Weight); } }
private float CalculateRadius(Dictionary <int, VFace> startCells, CartographicIsland cartographicIsland) { List <float> radii = new List <float>(); foreach (KeyValuePair <int, VFace> kvp in startCells) { float x = (float)kvp.Value.Generator.X - cartographicIsland.WeightedCenter.x; float z = (float)kvp.Value.Generator.Y - cartographicIsland.WeightedCenter.z; float radius = Mathf.Sqrt(x * x + z * z); radii.Add(radius); } return(ComputeMax(radii)); }
private Boolean checkOverlap(Vector3 newPosition, CartographicIsland newIsland, float minDistance) { int cc = 0; foreach (GraphVertex existingVertex in graph.Vertices) { Vector3 existingPos = existingVertex.getPosition(); float distance = Vector3.Distance(existingPos, newPosition); float existingRadius = existingVertex.getIsland().getRadius(); float newRadius = newIsland.getRadius(); if (distance <= (minDistance + (existingRadius + newRadius))) { return(false); } } return(true); }
private void SetCoastline(Dictionary <int, VFace> startCells, CartographicIsland island) { List <VFace> coastline = new List <VFace>(); Vector3 weightedCenter = Vector3.zero; foreach (KeyValuePair <int, VFace> kvp in startCells) { coastline.Add(kvp.Value); float x = (float)kvp.Value.Generator.X; float z = (float)kvp.Value.Generator.Y; Vector3 tilePos = new Vector3(x, 0, z); weightedCenter += tilePos; } weightedCenter /= startCells.Count; island.WeightedCenter = weightedCenter; island.CoastlineCells.AddRange(coastline); }
private bool CheckOverlap(Vector3 newPosition, CartographicIsland newIsland, float minDist) { var vertices = _project.DependencyGraph.Vertices; foreach (GraphVertex existingVertex in vertices) { Vector3 existingPos = existingVertex.Position; float distance = Vector3.Distance(existingPos, newPosition); float existingRadius = existingVertex.Island.Radius; float newRadius = newIsland.Radius; if (distance <= (minDist + (existingRadius + newRadius))) { return(false); } } return(true); }
//return: the unused candidates //b[1, 10]: cohesion factor. higher b -> more compact "cohesive" islands private Dictionary <int, VFace> constructRegionFromPackage(Package package, CartographicIsland island, Dictionary <int, VFace> startingCandidates, float b) { BoundedVoronoi islandVoronoi = island.getVoronoi(); List <CompilationUnit> cuList = package.getCompilationUnits(); List <VFace> cellMap = new List <VFace>(); Dictionary <int, VFace> newCandidates = new Dictionary <int, VFace>(); VFace startingCell = selectFromCandidates(startingCandidates, b).Value; int maxIterations = 10; int counter = 0; while (expandCountries(cuList, cellMap, newCandidates, startingCell, b) == false && counter < maxIterations) { //Debug.Log("Backtracking"); startingCell = selectFromCandidates(startingCandidates, b).Value; counter++; } island.addPackage(package); island.addPackageCells(cellMap); return(newCandidates); }
private CartographicIsland BuildFromBundle(Bundle bundle) { int seed = bundle.GetHashCode(); TriangleNet.Configuration config = new TriangleNet.Configuration(); BoundedVoronoi voronoi = VoronoiMaker.Instance.CreateRelaxedVoronoi(1, CELL_SCALE, seed); VFace initCell = ClosestCell(0, 0, voronoi); var startCells = new Dictionary <int, VFace>(); startCells.Add(initCell.ID, initCell); var island = new CartographicIsland(bundle, voronoi); int maxCount = bundle.MostCompilationUnits.CompilationUnitCount; foreach (Package package in bundle.Packages) { ConstructRegion(package, island, startCells, maxCount); } ShapeCoastArea(startCells, HEIGHT_PROFILE); SetCoastline(startCells, island); island.Radius = CalculateRadius(startCells, island); foreach (List <VFace> cellMap in island.PackageCells) { List <TNetMesh> packageMeshes = ConstructTNetMeshFromCellmap(cellMap); island.PackageMeshes.Add(packageMeshes); } List <TNetMesh> coastlineMeshes = ConstructTNetMeshFromCellmap(island.CoastlineCells); island.CoastlineMeshes.AddRange(coastlineMeshes); LinkDependencyVertex(bundle, island); return(island); }
private Dictionary <int, VFace> ConstructRegionFromPackage(Package package, CartographicIsland cartographicIsland, Dictionary <int, VFace> startCells, float cohesionMultiplier) { List <VFace> cellMap = new List <VFace>(); Dictionary <int, VFace> newCells = new Dictionary <int, VFace>(); VFace startCell = SelectFromCells(startCells, cohesionMultiplier).Value; int maxIterations = 20; int counter = 0; bool expand = ExpandCountries(package.CompilationUnits, cellMap, newCells, startCell, cohesionMultiplier); while (!expand && counter < maxIterations) { startCell = SelectFromCells(startCells, cohesionMultiplier).Value; counter++; } cartographicIsland.Packages.Add(package); cartographicIsland.PackageCells.Add(cellMap); return(newCells); }
public void setIslandStructure(CartographicIsland i) { island = i; }
public void setIsland(CartographicIsland i) { island = i; }
private IslandGO constructIslandGO(CartographicIsland island, Vector3 pos) { int rngSeed = island.getName().GetHashCode() + 200; RNG = new System.Random(rngSeed); GameObject islandGO = new GameObject(island.getName()); IslandGO islandGOComponent = islandGO.AddComponent <IslandGO>(); islandGOComponent.setIslandStructure(island); island.setIslandGO(islandGO); #region create countries List <List <TnetMesh> > tmeshList = island.getPackageMeshes(); List <List <VFace> > islandCells = island.getPackageCells(); List <Package> packageList = island.getPackages(); float maximumBuildingBoundSize = 0; int counter = 0; foreach (List <TnetMesh> tmesh in tmeshList) { Package pckg = packageList[counter]; GameObject region = new GameObject(pckg.getName()); Region regionComponent = region.AddComponent <Region>(); regionComponent.setParentIsland(islandGOComponent); region.transform.SetParent(islandGO.transform); islandGOComponent.addRegion(regionComponent); #region RegionArea GameObject regionArea = new GameObject("Region area"); regionArea.transform.SetParent(region.transform); MeshFilter mFilter = regionArea.AddComponent <MeshFilter>(); MeshRenderer mRender = regionArea.AddComponent <MeshRenderer>(); mRender.sharedMaterial = combinedHoloMaterial; regionComponent.setRegionArea(regionArea); regionComponent.setPackage(pckg); #endregion List <VFace> packageCells = islandCells[counter]; CombineInstance[] combineCellMeshes = new CombineInstance[tmesh.Count]; int cc = 0; #region Combine package cell meshes foreach (TnetMesh tm in tmesh) { Mesh m = Helperfunctions.convertTriangleNETMesh(tm); combineCellMeshes[cc].mesh = m; cc++; } mFilter.mesh = new Mesh(); mFilter.mesh.CombineMeshes(combineCellMeshes, true, false); float rndU = (float)RNG.NextDouble(); float rndV = (float)RNG.NextDouble() * 0.4f; Vector2 rndUV = new Vector2(rndU, rndV); setUVsToSingularCoord(rndUV, mFilter); #endregion cc = 0; #region Create CUs foreach (CompilationUnit cu in pckg.getCompilationUnits()) { float x = (float)packageCells[cc].generator.X; float y = (float)packageCells[cc].generator.Z; float z = (float)packageCells[cc].generator.Y; int heightLevel = Helperfunctions.mapLOCtoLevel(cu.getLoc()); GameObject building; if (cu.implementsServiceComponent()) { building = GameObject.Instantiate(SIPrefabs[heightLevel], region.transform); building.AddComponent <ServiceLayerGO>(); } else if (cu.declaresService()) { building = GameObject.Instantiate(SDPrefabs[heightLevel], region.transform); building.AddComponent <ServiceLayerGO>(); } else { building = GameObject.Instantiate(CUPrefabs[heightLevel], region.transform); } building.name = cu.getName(); Vector3 randomRotation = new Vector3(0f, UnityEngine.Random.Range(-180, 180), 0f); building.transform.localEulerAngles = randomRotation; Building buildingComponent = building.AddComponent <Building>(); buildingComponent.setCU(cu); cu.setGameObject(building); building.transform.position = new Vector3(x, y, z); building.transform.localScale = new Vector3(GlobalVar.cuScale, GlobalVar.cuScale, GlobalVar.cuScale); regionComponent.addBuilding(buildingComponent); ////////////////////////// #region BuildingCollider building.layer = LayerMask.NameToLayer("InteractionSystemLayer"); CapsuleCollider capsuleCol = building.AddComponent <CapsuleCollider>(); capsuleCol.isTrigger = true; #endregion float currentBuildingExtent = capsuleCol.bounds.size.magnitude; if (currentBuildingExtent > maximumBuildingBoundSize) { maximumBuildingBoundSize = currentBuildingExtent; } ////////////////////////// cc++; } #endregion counter++; } #endregion #region Combine CU meshes /* * GameObject combinedCUs = new GameObject("Combined CUs"); * MeshFilter mFilterCU = combinedCUs.AddComponent<MeshFilter>(); * MeshRenderer mRenderCU = combinedCUs.AddComponent<MeshRenderer>(); * mRenderCU.material = defaultMaterial; * mFilterCU.mesh = new Mesh(); * mFilterCU.mesh.CombineMeshes(combineCuMeshes, true, true); * combinedCUs.transform.SetParent(islandGO.transform); */ #endregion #region create coastline GameObject coastline = new GameObject("Coastline"); islandGOComponent.setCoast(coastline); coastline.transform.SetParent(islandGO.transform); MeshFilter coastMFilter = coastline.AddComponent <MeshFilter>(); MeshRenderer coastMRender = coastline.AddComponent <MeshRenderer>(); coastMRender.sharedMaterial = combinedHoloMaterial; List <TnetMesh> tmeshCoastList = island.getCoastlineMeshes(); CombineInstance[] combineCoastInstance = new CombineInstance[tmeshCoastList.Count]; counter = 0; foreach (TnetMesh tmesh in tmeshCoastList) { Mesh m = Helperfunctions.convertTriangleNETMesh(tmesh); combineCoastInstance[counter].mesh = m; counter++; } coastMFilter.mesh = new Mesh(); coastMFilter.mesh.CombineMeshes(combineCoastInstance, true, false); setUVsToSingularCoord(new Vector2(0f, 0.7f), coastMFilter); #endregion #region init docks //get graph vertex associated with the island GraphVertex vert = island.getDependencyVertex(); if (vert != null) { //Relative dock position Vector3 dockDirection = new Vector3(UnityEngine.Random.value, 0, UnityEngine.Random.value); dockDirection.Normalize(); dockDirection *= island.getRadius(); //Import Dock Vector3 dockPosition = island.getWeightedCenter() + dockDirection; dockPosition.y -= Mathf.Abs(GlobalVar.islandHeightProfile[GlobalVar.islandHeightProfile.Length - 1]) * GlobalVar.islandAboveOcean; GameObject importD = Instantiate(importDockPrefab, dockPosition, Quaternion.identity); importD.layer = LayerMask.NameToLayer("InteractionSystemLayer"); importD.name = island.getName() + " import dock"; importD.transform.localScale = new Vector3(1, 1, 1); importD.transform.SetParent(islandGO.transform); islandGOComponent.setImportDock(importD); //setUVsToSingularCoord(new Vector2(0.7f, 0.1f), importD.GetComponent<MeshFilter>()); //Export Dock GameObject exportD = Instantiate(exportDockPrefab, dockPosition, Quaternion.identity); exportD.layer = LayerMask.NameToLayer("InteractionSystemLayer"); exportD.name = island.getName() + " export dock"; exportD.transform.localScale = new Vector3(1, 1, 1); exportD.transform.SetParent(islandGO.transform); islandGOComponent.setExportDock(exportD); //setUVsToSingularCoord(new Vector2(0.1f, 0.1f), exportD.GetComponent<MeshFilter>()); } #endregion islandGO.transform.position = pos; islandGO.transform.SetParent(VisualizationContainer.transform); #region rise Islands above ocean float newIslandHeight = Mathf.Abs(GlobalVar.islandHeightProfile[GlobalVar.islandHeightProfile.Length - 1]) * GlobalVar.islandAboveOcean; Vector3 newIslandPos = islandGO.transform.localPosition; newIslandPos.y = newIslandHeight; islandGO.transform.localPosition = newIslandPos; #endregion #region Create colliders #region CountryCollider List <Region> regions = islandGOComponent.getRegions(); foreach (Region region in regions) { GameObject countryGO = region.gameObject; countryGO.layer = LayerMask.NameToLayer("InteractionSystemLayer"); MeshCollider cColliderCountry = countryGO.AddComponent <MeshCollider>(); MeshFilter mFilter = region.getRegionArea().GetComponent <MeshFilter>(); cColliderCountry.sharedMesh = mFilter.sharedMesh; cColliderCountry.convex = true; cColliderCountry.isTrigger = true; } #endregion #region IslandCollider islandGO.layer = LayerMask.NameToLayer("InteractionSystemLayer"); CapsuleCollider cColliderIsland = islandGO.AddComponent <CapsuleCollider>(); float b = island.getRadius(); cColliderIsland.radius = b; float newColliderHeight = islandGOComponent.getCoast().GetComponent <MeshFilter>().sharedMesh.bounds.size.y; cColliderIsland.height = newColliderHeight; Vector3 newCenter = island.getWeightedCenter(); newCenter.y = -islandGOComponent.getCoast().GetComponent <MeshFilter>().sharedMesh.bounds.size.y + (newColliderHeight * 0.5f); cColliderIsland.center = newCenter; cColliderIsland.isTrigger = true; #endregion #endregion return(islandGOComponent); }
private void constructDockGO(IslandGO island) { CartographicIsland islandStructure = island.getIslandStructure(); //Get graph vertex associated with the island BidirectionalGraph <GraphVertex, GraphEdge> depGraph = islandStructure.getBundle().getParentProject().getDependencyGraph(); GraphVertex vert = islandStructure.getDependencyVertex(); if (vert != null) { float importSize = GlobalVar.minDockSize; float exportSize = GlobalVar.minDockSize; //Outgoing edges -Bundle depends on... IEnumerable <GraphEdge> outEdges; depGraph.TryGetOutEdges(vert, out outEdges); List <GraphEdge> edgeList = outEdges.ToList(); importSize = Helperfunctions.mapDependencycountToSize(edgeList.Count); //Import Dock GameObject importD = island.getImportDock(); importD.transform.localScale = new Vector3(importSize, importSize, importSize); //Link dependencies DependencyDock dockComponent = importD.GetComponent <DependencyDock>(); dockComponent.setDockType(DockType.ImportDock); foreach (GraphEdge e in edgeList) { GameObject ed = e.Target.getIsland().getIslandGO().GetComponent <IslandGO>().getExportDock(); dockComponent.addDockConnection(ed.GetComponent <DependencyDock>(), e.getWeight()); } #region determine optimal Position for ImportDock List <GameObject> doNotCollideList = new List <GameObject>(); doNotCollideList.Add(island.getCoast()); bool foundLocation = findSuitablePosition2D(importD, doNotCollideList, island.gameObject, 500); if (!foundLocation) { Debug.Log("Could not find suitable location for " + importD.name); } #endregion //Ingoing edges -Other Bundles depends on this one... depGraph.TryGetInEdges(vert, out outEdges); edgeList = outEdges.ToList(); exportSize = Helperfunctions.mapDependencycountToSize(edgeList.Count); //Export Dock GameObject exportD = island.getExportDock(); float eDockWidth = exportD.GetComponent <MeshFilter>().sharedMesh.bounds.size.x *exportSize; float iDockWidth = importD.GetComponent <MeshFilter>().sharedMesh.bounds.size.x *importSize; //exportD.transform.position = importD.transform.position + Vector3.left * (iDockWidth + eDockWidth) * 0.5f; exportD.transform.localScale = new Vector3(exportSize, exportSize, exportSize); //Link dependencies dockComponent = exportD.GetComponent <DependencyDock>(); dockComponent.setDockType(DockType.ExportDock); foreach (GraphEdge e in edgeList) { GameObject id = e.Source.getIsland().getIslandGO().GetComponent <IslandGO>().getImportDock(); dockComponent.addDockConnection(id.GetComponent <DependencyDock>(), e.getWeight()); } #region determine optimal Position for ExportDock doNotCollideList.Clear(); doNotCollideList.Add(island.getCoast()); foundLocation = findSuitablePosition2D(exportD, doNotCollideList, importD, 500); if (!foundLocation) { Debug.Log("Could not find suitable location for " + exportD.name); } #endregion #region extend Island collider based on new Docksizes island.GetComponent <CapsuleCollider>().radius += Mathf.Max(importSize, exportSize) * Mathf.Sqrt(2f); #endregion } }
private CartographicIsland constructIslandFromBundle(Bundle b) { int rngSeed = b.getName().GetHashCode() + 200; RNG = new System.Random(rngSeed); #region VoronoiPlane TriangleNet.Configuration conf = new TriangleNet.Configuration(); List <Vertex> vertices = Helperfunctions.createPointsOnPlane(GlobalVar.voronoiCellScalefactor, GlobalVar.voronoiCellScalefactor, 50, 50, 1.0f, RNG); BoundedVoronoi voronoi = Helperfunctions.createRelaxedVoronoi(vertices, 1); #endregion #region initFirstCell VFace firstCell = Helperfunctions.closestCell(0, 0, voronoi); Dictionary <int, VFace> startingCandidates = new Dictionary <int, VFace>(); startingCandidates.Add(firstCell.ID, firstCell); #endregion List <Package> packages = b.getPackages(); CartographicIsland island = new CartographicIsland(b); island.setVoronoi(voronoi); #region sort package list packages.Sort((x, y) => x.getCompilationUnits().Count.CompareTo(y.getCompilationUnits().Count)); packages.Reverse(); #endregion //Compute maximal compilation unit count in bundle float maxCUCountInIsland = 0; foreach (Package package in packages) { long cuCount = package.getCuCount(); if (cuCount > maxCUCountInIsland) { maxCUCountInIsland = cuCount; } } #region construct regions foreach (Package package in packages) { float cohesionMult = (float)package.getCuCount() / maxCUCountInIsland; cohesionMult *= maxCohesion; cohesionMult = Mathf.Max(minCohesion, cohesionMult); Dictionary <int, VFace> newCandidates = constructRegionFromPackage(package, island, startingCandidates, cohesionMult); updateAndFuseCandidates(startingCandidates, newCandidates); } #endregion #region Shape island coast //Advance startingCandidates X cells outwards and ajdust the height of all vertices shapeCoastArea(startingCandidates, GlobalVar.islandHeightProfile); #endregion #region WeightedCenter & set coast List <VFace> coastlineList = new List <VFace>(); Vector3 weightedCenter = Vector3.zero; foreach (KeyValuePair <int, VFace> kvp in startingCandidates) { coastlineList.Add(kvp.Value); float x = (float)kvp.Value.generator.X; float z = (float)kvp.Value.generator.Y; Vector3 tilePos = new Vector3(x, 0, z); weightedCenter += tilePos; } weightedCenter /= startingCandidates.Count; island.setWeightedCenter(weightedCenter); island.setCoastlineCells(coastlineList); #endregion #region conservative Radius List <float> radii = new List <float>(); foreach (KeyValuePair <int, VFace> kvp in startingCandidates) { float x = (float)kvp.Value.generator.X - island.getWeightedCenter().x; float z = (float)kvp.Value.generator.Y - island.getWeightedCenter().z; float radius = Mathf.Sqrt(x * x + z * z); radii.Add(radius); } float maxRadius = Helperfunctions.computeMax(radii); island.setRadius(maxRadius); #endregion #region TnetMeshesConstruction foreach (List <VFace> cellMap in island.getPackageCells()) { island.addPackageMesh(constructTnetMeshFromCellmap(cellMap)); } island.setCoastlineMesh(constructTnetMeshFromCellmap(coastlineList)); #endregion #region link dependency vertex //Find graph vertex associated with the island BidirectionalGraph <GraphVertex, GraphEdge> depGraph = b.getParentProject().getDependencyGraph(); List <GraphVertex> allVertices = depGraph.Vertices.ToList(); GraphVertex vert = allVertices.Find(v => string.Equals(v.getName(), b.getName())); if (vert != null) { //Link GraphVertex-Island vert.setIsland(island); island.setDependencyVertex(vert); } #endregion return(island); }