public void generateButtonPressed() { GeneratorUIManager.Instance.disableGenerateButton(); GeneratorUIManager.Instance.deleteMapOnUI(Content.transform); try { validateGeneratorParams(GeneratorsVect[(int)activeGenerator]); GeneratorsVect[(int)activeGenerator].TypeGrid = TypeGridVect[(int)activeTypeGrid]; //Typegrid change in generator TileObject[,] map = GeneratorsVect[(int)activeGenerator].initializeMap(); GeneratorsVect[(int)activeGenerator].generateMap(); GeneratorsVect[(int)activeGenerator].postprocessMap(); if (GeneratorsVect[(int)activeGenerator].useRandomSeed) { GeneratorUIManager.Instance.gameObject.GetComponent <UIParametersValueChange>().refreshUIParams(); } dataMap = MapEvaluator.computeMetrics(GeneratorsVect[(int)activeGenerator].getMap(), GeneratorsVect[(int)activeGenerator].TypeGrid, GeneratorsVect[(int)activeGenerator].startPos, GeneratorsVect[(int)activeGenerator].endPos); DisplayMainMap((GeneratorUIManager.Instance.isTrapsOnMapBorderToggleOn() ? GeneratorsVect[(int)activeGenerator].getMapWTrapBorder() : GeneratorsVect[(int)activeGenerator].getMap())); GeneratorUIManager.Instance.showUIMapInfo(MapHolder.transform, dataMap, GeneratorsVect[(int)activeGenerator].TypeGrid); } catch (Exception e) { ErrorManager.ManageError(ErrorManager.Error.SOFT_ERROR, e.Message); } GeneratorUIManager.Instance.enableGenerateButton(); }
public static GameObject renderAliasOnUI(RectTransform container, ITypeGrid typeGrid, StructuredAlias alias, GameObject AliasPrefab, bool attachMapMetrics) { GameObject AliasGO = GameObject.Instantiate(AliasPrefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject; container.parent.GetComponent <MapListManager>().addMapToDictionary(alias.AliasMap, alias.start, alias.end, alias.similarityDistance, AliasGO.GetInstanceID()); AliasGO.transform.SetParent(container, false); Transform t = AliasGO.transform.Find("BorderMask/Content"); RectTransform contentRect = t.GetComponent <RectTransform>(); RectTransform prefabRect = typeGrid.TilePrefab.GetComponent <RectTransform>(); initAliasGameObject(AliasGO); GeneratorUIManager.Instance.DisplayMap(alias.AliasMap, t, ParameterManager.Instance.GridType); GeneratorUIManager.Instance.ScaleToFitContainer(contentRect, new Rect(Vector2.zero, container.GetComponent <GridLayoutGroup>().cellSize)); if (attachMapMetrics) { AliasGO.GetComponentInChildren <HoverDisplayText>().textToDisplay = MapEvaluator.aggregateAliasDataMap(MapEvaluator.computeMetrics(alias.AliasMap, typeGrid, alias.start, alias.end), alias.similarityDistance); } else { AliasGO.GetComponentInChildren <HoverDisplayText>().gameObject.SetActive(false); } return(AliasGO); }
public override TileObject[,] postprocessMap() { if (obstacleThreshold > 0 || roomThreshold > 0) { List <HashSet <Vector2Int> > ObstacleConnectedRegions = new List <HashSet <Vector2Int> >(); List <HashSet <Vector2Int> > RoomConnectedRegions = new List <HashSet <Vector2Int> >(); MapEvaluator.findRoomObstacleConnectedRegions(RoomConnectedRegions, ObstacleConnectedRegions, map, TypeGrid); //Postprocess the regions if above the respective thresholds if (roomThreshold > 0) { foreach (HashSet <Vector2Int> rCR in RoomConnectedRegions) { if (rCR.Count <= roomThreshold) { foreach (Vector2Int cell in rCR) { map[cell.x, cell.y].type = wallChar; } } } } if (obstacleThreshold > 0) { foreach (HashSet <Vector2Int> oCR in ObstacleConnectedRegions) { if (oCR.Count <= obstacleThreshold) { foreach (Vector2Int cell in oCR) { map[cell.x, cell.y].type = roomChar; } } } } } //Fix start position if (startPos != null) { map[startPos.x, startPos.y].type = startChar; } //Fix end position if (endPos != null) { map[endPos.x, endPos.y].type = endChar; } return(map); }
public void showUIMapInfo(Transform mapHolderTransform, DataMap DataM, ITypeGrid t) { GameObject LineGO = Instantiate(LineUIPrefab, new Vector3(0, 0, 0), Quaternion.identity); Utility.displaySegmentedLineUI(LineGO, mapHolderTransform.Find("BorderMask/Content").GetComponent <RectTransform>(), DataM.solutionSteps, originUIMap, t.TilePrefab.GetComponent <RectTransform>().sizeDelta.x, t.TilePrefab.GetComponent <RectTransform>().sizeDelta.y); mapHolderTransform.Find("SaveButton").gameObject.SetActive(true); mapHolderTransform.Find("PlusButton").gameObject.SetActive(true); mapHolderTransform.Find("MinusButton").gameObject.SetActive(true); GameObject DialogBoxGO = mapHolderTransform.Find("BorderMask/MapDataBox").gameObject; Animator animatorDB = DialogBoxGO.GetComponent <Animator>(); TextMeshProUGUI textDataDB = mapHolderTransform.Find("BorderMask/MapDataBox/DataDialogBox").gameObject.GetComponent <TextMeshProUGUI>(); textDataDB.text = MapEvaluator.aggregateDataMap(DataM); if (DataM.isTraversable) { mapHolderTransform.GetComponent <Image>().color = correctMessageColor; DialogBoxGO.GetComponent <Image>().color = correctMessageColor; if (animatorDB != null) { animatorDB.SetBool("Show", true); animatorDB.SetBool("Error", false); } else { throw new Exception("Animator is missing on " + DialogBoxGO.name + " Game Object."); } } else { mapHolderTransform.GetComponent <Image>().color = errorMessageColor; DialogBoxGO.GetComponent <Image>().color = errorMessageColor; if (animatorDB != null) { animatorDB.SetBool("Show", true); animatorDB.SetBool("Error", true); } else { throw new Exception("Animator is missing on " + DialogBoxGO.name + " Game Object."); } } }
public Dictionary <int, StructuredAlias> .ValueCollection generateAliasOnTheFly() { mainMap = ParameterManager.Instance.MapToPlay; gridType = ParameterManager.Instance.GridType; SimilarMapsQueue = new SimplePriorityQueue <TileObject[, ]>(); K_CollisionSet = MapEvaluator.BuildKCollisionVec(mainMap, gridType, ParameterManager.Instance.StartCell, Mathf.Max(ParameterManager.Instance.minStepsSolution, ParameterManager.Instance.maxStepsSolution)); GenerateAndTestAliasMaps(); Dictionary <int, StructuredAlias> dic = new Dictionary <int, StructuredAlias>(); int i = UnityEngine.Random.Range(3, 7); //try to change while (i > 0) { dic.Add(i, new StructuredAlias(SimilarMapsQueue.Dequeue())); i--; } return(dic.Values); }
public void GenerateAndTestAliasMaps() { Vector2Int startMainMap = ParameterManager.Instance.StartCell; int width = ParameterManager.Instance.MapToPlay.GetLength(0); int height = ParameterManager.Instance.MapToPlay.GetLength(1); BaseAliasCollisionMask = getMainMapKMaxMinCells(mainMap, gridType, ParameterManager.Instance.minStepsSolution, ParameterManager.Instance.minStepsSolution, startMainMap, 0f); HashSet <Vector2Int>[,] AliasCollisionMaskMatrix = new HashSet <Vector2Int> [ParameterManager.Instance.maxStepsSolution - ParameterManager.Instance.minStepsSolution, MAX_ALIASMASKS]; if (ParameterManager.Instance.maxStepsSolution > ParameterManager.Instance.minStepsSolution) { for (int h = 0; h < ParameterManager.Instance.maxStepsSolution - ParameterManager.Instance.minStepsSolution; h++) { for (int k = 0; k < MAX_ALIASMASKS; k++) { AliasCollisionMaskMatrix[h, k] = getMainMapKMaxMinCells(mainMap, gridType, ParameterManager.Instance.minStepsSolution, ParameterManager.Instance.minStepsSolution + h + 1, startMainMap, ((k + 1) * 1.0f) / (MAX_ALIASMASKS));; } } } else { AliasCollisionMaskMatrix = null; } //Map initialization. int i = 0; Vector2Int startAlias = ParameterManager.Instance.StartCell; Vector2Int endAlias = ParameterManager.Instance.EndCell; while (i < MAX_ALIAS) { //define here the width, height, start and end of the chosen map TileObject[,] aliasMap = new TileObject[width, height]; genMan.connectedGenerator.setBaseGeneratorParameters(gridType, width, height, startAlias, endAlias, i, false); genMan.cellularAutomataGenerator.setBaseGeneratorParameters(gridType, width, height, startAlias, endAlias, i, false); genMan.primGenerator.setBaseGeneratorParameters(gridType, width, height, startAlias, endAlias, i, false); switch (i % 3) { case 0: aliasMap = genMan.connectedGenerator.generateMapGeneral(ParameterManager.Instance.IsTrapsOnMapBorder, (float)pRNG_Alias.NextDouble()); break; case 1: aliasMap = genMan.cellularAutomataGenerator.generateMapGeneral(ParameterManager.Instance.IsTrapsOnMapBorder, (float)pRNG_Alias.NextDouble(), 1 + i % 5, 5, i % 2 == 1, 0, 0); break; case 2: aliasMap = genMan.primGenerator.generateMapGeneral(ParameterManager.Instance.IsTrapsOnMapBorder, (float)pRNG_Alias.NextDouble()); break; default: ErrorManager.ManageError(ErrorManager.Error.HARD_ERROR, "AliasManager: no map generator found."); break; } //Apply mask... //from zero to kMinStep and up to some cells at kMaxStep if (ParameterManager.Instance.maxStepsSolution > ParameterManager.Instance.minStepsSolution) { for (int x = 0; x < aliasMap.GetLength(0); x++) { for (int y = 0; y < aliasMap.GetLength(1); y++) { if (Utility.in_bounds_General(new Vector2Int(startMainMap.x + x, startMainMap.y + y), width, height)) { if (AliasCollisionMaskMatrix[i % AliasCollisionMaskMatrix.GetLength(0), i % AliasCollisionMaskMatrix.GetLength(1)].Contains(new Vector2Int(x - startAlias.x, y - startAlias.y))) { aliasMap[x, y].type = ParameterManager.Instance.MapToPlay[startMainMap.x + x, startMainMap.y + y].type; } } } } } else { //only from zero to kMinStep foreach (var m in BaseAliasCollisionMask) { if (Utility.in_bounds_General(new Vector2Int(startMainMap.x + m.x, startMainMap.y + m.y), mainMap.GetLength(0), mainMap.GetLength(1)) && Utility.in_bounds_General(new Vector2Int(startAlias.x + m.x, startAlias.y + m.y), aliasMap.GetLength(0), aliasMap.GetLength(1))) { aliasMap[startAlias.x + m.x, startAlias.y + m.y].type = ParameterManager.Instance.MapToPlay[startMainMap.x + m.x, startMainMap.y + m.y].type; } } } if (MapEvaluator.isEndReachable(aliasMap, gridType, startAlias, endAlias, ParameterManager.Instance.allowAutosolverForAlias).First() == endAlias) {//if the map has a path from start to end, add it float dst = MapEvaluator.BinaryMapSimilarity(mainMap, aliasMap, startMainMap, startAlias); SimilarMapsQueue.Enqueue(aliasMap, dst); } i++; } }
public Tuple <int, StructuredAlias> Generate1RandomAliasFromRealMap(StructuredAlias realMap) { mainMap = realMap.AliasMap; gridType = ParameterManager.Instance.GridType; Vector2Int startMainMap = realMap.start; int width = realMap.AliasMap.GetLength(0); int height = realMap.AliasMap.GetLength(1); //Map initialization. int i = 0; Tuple <int, StructuredAlias> AliasTuple; Vector2Int startAlias = realMap.start; Vector2Int endAlias = realMap.end; float dst = 0; TileObject[,] aliasMap = new TileObject[width, height]; while (i < 1) { //define here the width, height, start and end of the chosen map genMan.connectedGenerator.setBaseGeneratorParameters(gridType, width, height, startAlias, endAlias, i, false); genMan.cellularAutomataGenerator.setBaseGeneratorParameters(gridType, width, height, startAlias, endAlias, i, false); genMan.primGenerator.setBaseGeneratorParameters(gridType, width, height, startAlias, endAlias, i, false); switch (pRNG_Alias.Next() % 3) { case 0: aliasMap = genMan.connectedGenerator.generateMapGeneral(ParameterManager.Instance.IsTrapsOnMapBorder, (float)pRNG_Alias.NextDouble()); break; case 1: float prcObsCA = (float)pRNG_Alias.NextDouble(); aliasMap = genMan.cellularAutomataGenerator.generateMapGeneral(ParameterManager.Instance.IsTrapsOnMapBorder, prcObsCA, 1 + (prcObsCA <= .5f && !(i % 2 == 1) ? 0 : pRNG_Alias.Next(0, 6)), 5, i % 2 == 1, 0, 0); break; case 2: aliasMap = genMan.primGenerator.generateMapGeneral(ParameterManager.Instance.IsTrapsOnMapBorder, (float)pRNG_Alias.NextDouble()); break; default: ErrorManager.ManageError(ErrorManager.Error.HARD_ERROR, "AliasManager: no map generator found."); break; } foreach (var m in BaseAliasCollisionMask) { if (Utility.in_bounds_General(new Vector2Int(startMainMap.x + m.x, startMainMap.y + m.y), mainMap.GetLength(0), mainMap.GetLength(1)) && Utility.in_bounds_General(new Vector2Int(startAlias.x + m.x, startAlias.y + m.y), aliasMap.GetLength(0), aliasMap.GetLength(1))) { aliasMap[startAlias.x + m.x, startAlias.y + m.y].type = ParameterManager.Instance.MapToPlay[startMainMap.x + m.x, startMainMap.y + m.y].type; } } if (MapEvaluator.isEndReachable(aliasMap, gridType, startAlias, endAlias, ParameterManager.Instance.allowAutosolverForAlias).First() == endAlias) { //if the map has a path from start to end, add it dst = MapEvaluator.BinaryMapSimilarity(mainMap, aliasMap, startMainMap, startAlias); i++; } } AliasTuple = new Tuple <int, StructuredAlias>(Guid.NewGuid().GetHashCode(), new StructuredAlias(aliasMap, startAlias, endAlias, dst)); return(AliasTuple); }
public Dictionary <int, StructuredAlias> GenerateNRandomAliasFromRealMap(StructuredAlias realMap, int N) { mainMap = realMap.AliasMap; gridType = ParameterManager.Instance.GridType; Vector2Int startMainMap = realMap.start; int width = realMap.AliasMap.GetLength(0); int height = realMap.AliasMap.GetLength(1); //Map initialization. int i = 0; Dictionary <int, StructuredAlias> AliasBatch = new Dictionary <int, StructuredAlias>(); Vector2Int startAlias = realMap.start; Vector2Int endAlias = realMap.end; SimplePriorityQueue <TileObject[, ]> tmpPQ = new SimplePriorityQueue <TileObject[, ]>(); bool isAliasSameAsReal = true; while (i < N) { //define here the width, height, start and end of the chosen map TileObject[,] aliasMap = new TileObject[width, height]; genMan.connectedGenerator.setBaseGeneratorParameters(gridType, width, height, startAlias, endAlias, (i % 2 == 0?1:-1) * RNG_Alias.Next(), false); genMan.cellularAutomataGenerator.setBaseGeneratorParameters(gridType, width, height, startAlias, endAlias, (i % 2 == 0 ? 1 : -1) * RNG_Alias.Next(), false); genMan.primGenerator.setBaseGeneratorParameters(gridType, width, height, startAlias, endAlias, (i % 2 == 0 ? 1 : -1) * RNG_Alias.Next(), false); switch (i % 3) { case 0: aliasMap = genMan.connectedGenerator.generateMapGeneral(ParameterManager.Instance.IsTrapsOnMapBorder, (float)RNG_Alias.NextDouble()); break; case 1: float prcObsCA = (float)RNG_Alias.NextDouble(); aliasMap = genMan.cellularAutomataGenerator.generateMapGeneral(ParameterManager.Instance.IsTrapsOnMapBorder, prcObsCA, 1 + (prcObsCA <= .5f && !(i % 2 == 1)?0:RNG_Alias.Next(0, 6)), 5, i % 2 == 1, 0, 0); break; case 2: aliasMap = genMan.primGenerator.generateMapGeneral(ParameterManager.Instance.IsTrapsOnMapBorder, (float)RNG_Alias.NextDouble()); break; default: ErrorManager.ManageError(ErrorManager.Error.HARD_ERROR, "AliasManager: no map generator found."); break; } foreach (var m in BaseAliasCollisionMask) { if (Utility.in_bounds_General(new Vector2Int(startMainMap.x + m.x, startMainMap.y + m.y), mainMap.GetLength(0), mainMap.GetLength(1)) && Utility.in_bounds_General(new Vector2Int(startAlias.x + m.x, startAlias.y + m.y), aliasMap.GetLength(0), aliasMap.GetLength(1))) { aliasMap[startAlias.x + m.x, startAlias.y + m.y].type = realMap.AliasMap[startMainMap.x + m.x, startMainMap.y + m.y].type; } } for (int h = 0; h < width; h++) { for (int k = 0; k < height; k++) { isAliasSameAsReal &= (aliasMap[h, k].type == realMap.AliasMap[h, k].type); if (!isAliasSameAsReal) { break; } } if (!isAliasSameAsReal) { break; } } if (MapEvaluator.isEndReachable(aliasMap, gridType, startAlias, endAlias, ParameterManager.Instance.allowAutosolverForAlias).First() == endAlias && !isAliasSameAsReal) { //if the map has a path from start to end, add it float dst = MapEvaluator.BinaryMapSimilarity(mainMap, aliasMap, startMainMap, startAlias); tmpPQ.Enqueue(aliasMap, dst); i++; } } i = 0; TileObject[,] tmpStrAlias; while (i < N) { if (ParameterManager.Instance.considerSimilar) { float dst = tmpPQ.GetPriority(tmpPQ.First()); tmpStrAlias = tmpPQ.Dequeue(); AliasBatch.Add(Guid.NewGuid().GetHashCode(), new StructuredAlias(tmpStrAlias, startAlias, endAlias, dst)); i++; } if (ParameterManager.Instance.considerNovelty && i < N) { tmpStrAlias = tmpPQ.Last(); float dst = tmpPQ.GetPriority(tmpStrAlias); tmpPQ.Remove(tmpStrAlias); AliasBatch.Add(Guid.NewGuid().GetHashCode(), new StructuredAlias(tmpStrAlias, startAlias, endAlias, dst)); i++; } } return(AliasBatch); }
public void CollabGameGeneration() { mainMap = ParameterManager.Instance.MapToPlay; gridType = ParameterManager.Instance.GridType; SimilarMapsQueue = new SimplePriorityQueue <TileObject[, ]>(); K_CollisionSet = MapEvaluator.BuildKCollisionVec(mainMap, gridType, ParameterManager.Instance.StartCell, Mathf.Max(ParameterManager.Instance.minStepsSolution, ParameterManager.Instance.maxStepsSolution)); if (ParameterManager.Instance.isOptimizerOn) { List <TileObject[, ]> alises = new List <TileObject[, ]>(); BaseAliasCollisionMask = getMainMapKMaxMinCells(mainMap, gridType, ParameterManager.Instance.minStepsSolution, ParameterManager.Instance.minStepsSolution, ParameterManager.Instance.StartCell, 0f); int experimentsNum = 1; while (experimentsNum > 0) { foreach (var a in AliasGeneratorManager.Instance.GetComponent <AliasGameEvaluator>().AliasGameOptimizerHandler()) { SimilarMapsQueue.Enqueue(a.Value.AliasMap, a.Value.similarityDistance); } experimentsNum--; } } else { GenerateAndTestAliasMaps(); } int i = 0; Vector2Int startMainMap = ParameterManager.Instance.StartCell; TileObject[,] tmpStrAlias; while (i < ParameterManager.Instance.aliasNum) { if (ParameterManager.Instance.considerSimilar) { float dst = SimilarMapsQueue.GetPriority(SimilarMapsQueue.First()); tmpStrAlias = SimilarMapsQueue.Dequeue(); Utility.renderAliasOnUI(AliasDragAreas[0].GetChild(0).GetComponent <RectTransform>(), ParameterManager.Instance.GridType, new StructuredAlias(tmpStrAlias, startMainMap, ParameterManager.Instance.EndCell, dst), AliasPrefab, true); i++; } if (ParameterManager.Instance.considerNovelty && i < ParameterManager.Instance.aliasNum) { tmpStrAlias = SimilarMapsQueue.Last(); float dst = SimilarMapsQueue.GetPriority(tmpStrAlias); SimilarMapsQueue.Remove(tmpStrAlias); Utility.renderAliasOnUI(AliasDragAreas[0].GetChild(0).GetComponent <RectTransform>(), ParameterManager.Instance.GridType, new StructuredAlias(tmpStrAlias, startMainMap, ParameterManager.Instance.EndCell, dst), AliasPrefab, true); i++; } } i = 0; while (SimilarMapsQueue.Count > 0 && i < BatchAliasNumber) { float dst = SimilarMapsQueue.GetPriority(SimilarMapsQueue.First()); tmpStrAlias = SimilarMapsQueue.Dequeue(); Utility.renderAliasOnUI(AliasDragAreas[1].GetChild(0).GetComponent <RectTransform>(), ParameterManager.Instance.GridType, new StructuredAlias(tmpStrAlias, startMainMap, ParameterManager.Instance.EndCell, dst), AliasPrefab, true); i++; } i = 0; while (SimilarMapsQueue.Count > 0 && i < BatchAliasNumber) { tmpStrAlias = SimilarMapsQueue.Last(); float dst = SimilarMapsQueue.GetPriority(tmpStrAlias); SimilarMapsQueue.Remove(tmpStrAlias); Utility.renderAliasOnUI(AliasDragAreas[2].GetChild(0).GetComponent <RectTransform>(), ParameterManager.Instance.GridType, new StructuredAlias(tmpStrAlias, startMainMap, ParameterManager.Instance.EndCell, dst), AliasPrefab, true); i++; } //reset horizontal and vertical bars if exists ScrollRect sR = AliasDragAreas[0].GetComponent <ScrollRect>(); if (sR != null) { Scrollbar hSb = sR.horizontalScrollbar; Scrollbar vSb = sR.verticalScrollbar; if (hSb != null) { hSb.value = .99f; } if (vSb != null) { vSb.value = .99f; } } gameObject.GetComponent <AliasGameEvaluator>().AliasGameEvaluatorHandler(); }