/// <summary> /// Add the roon in parameters to all the necessary lists and update all the rooms' neighbors /// </summary> /// <param name="room"></param> public void SpawnRoom(LayoutRoom room) { rooms[room.Position.x, room.Position.y] = room; spawnedRooms.Add(room); UpdateAllRoomsNeighbors(); SetCircleValue(room.Position, room.Distance); }
public void ExpandVertically(LayoutRoom room, bool allowDoubleSpawn) { //on détecte les free slots autours List <Vector2Int> possiblePos = GetEmptyValidSpotsAtPos(room.Position, false, true); //on fait spawn des rooms int rdm; if (possiblePos.Count == 1) { rdm = Random.Range(0, possiblePos.Count + 1); } else { rdm = Random.Range(0, possiblePos.Count); } for (int i = 0; i < rdm; i++) { LayoutRoom spawnedRoom = new LayoutRoom(possiblePos[Random.Range(0, possiblePos.Count)], 0); SpawnRoom(spawnedRoom); if (allowDoubleSpawn && Random.Range(0.0f, 1.0f) > 1 - chanceDoubleVertical) { ExpandVertically(spawnedRoom, false); } } }
public void Dijkstra() { List <LayoutRoom> roomsToExplore = new List <LayoutRoom>(); LayoutRoom firstRoom = spawnedRooms[0]; firstRoom.Distance = 0; roomsToExplore.Add(firstRoom); SetCircleValue(firstRoom.Position, firstRoom.Distance); int i = 0; while (roomsToExplore.Count > 0) { List <LayoutRoom> loopRoomsToExplore = new List <LayoutRoom>(roomsToExplore); foreach (LayoutRoom room in loopRoomsToExplore) { List <LayoutRoom> neighbors = GetNeighborsAtPos(room.Position); List <LayoutRoom> addedRooms = new List <LayoutRoom>(); foreach (LayoutRoom neighbor in neighbors) { if (neighbor.Distance == -1 || room.Distance + 1 < neighbor.Distance) { neighbor.Distance = room.Distance + 1; addedRooms.Add(neighbor); SetCircleValue(neighbor.Position, neighbor.Distance); } } roomsToExplore.AddRange(addedRooms); roomsToExplore.Remove(room); } i++; } }
public void PutRoomOfInterest() { //choix de la room de boss LayoutRoom bossRoom = FindFurthestRoom(); bossRoom.RoomType = 1; Debug.Log("Boss Room placée en : " + bossRoom.Position); }
//public static Random rnd = new Random(1337); private static List <LayoutRoom> generateLayout(MapParams parameter) { LayoutRoom r = new LayoutRoom(new[] { new Point(0, 0), new Point(parameter.width, 0), new Point(parameter.width, parameter.height), new Point(0, parameter.height) }); List <LayoutRoom> rooms = new List <LayoutRoom>(); Queue <LayoutRoom> toCompute = new Queue <LayoutRoom>(); toCompute.Enqueue(r); LayoutRoom current; int spl; bool splitH = false; while (toCompute.Count != 0) { current = toCompute.Dequeue(); Tuple <LayoutRoom, LayoutRoom> ret; if (current.AvailableForSplitH(MIN_ROOM_SIZE, MAX_ROOM_SIZE) && current.AvailableForSplitV(MIN_ROOM_SIZE, MAX_ROOM_SIZE)) { spl = RandomGen.GetRandom(MIN_ROOM_SIZE, (splitH ? current.Bounds.Height : current.Bounds.Width) - MIN_ROOM_SIZE); ret = splitH ? current.SplitHorizontal(spl) : current.SplitVertical(spl); } else if (current.AvailableForSplitV(MIN_ROOM_SIZE, MAX_ROOM_SIZE)) { spl = RandomGen.GetRandom(MIN_ROOM_SIZE, current.Bounds.Width - MIN_ROOM_SIZE); ret = current.SplitVertical(spl); } else { spl = RandomGen.GetRandom(MIN_ROOM_SIZE, current.Bounds.Height - MIN_ROOM_SIZE); ret = current.SplitHorizontal(spl); } splitH = !splitH; if (ret.Item1.AvailableForSplit(MIN_ROOM_SIZE, MAX_ROOM_SIZE)) { toCompute.Enqueue(ret.Item1); } else { rooms.Add(ret.Item1); } if (ret.Item2.AvailableForSplit(MIN_ROOM_SIZE, MAX_ROOM_SIZE)) { toCompute.Enqueue(ret.Item2); } else { rooms.Add(ret.Item2); } } return(rooms); }
/// <summary> /// Create a room that has only one neighbor /// </summary> public void CreateRoom1Neighbor(bool canAlterate) { bool done = false; int cptInfinite = 0; int cptTries = 0; while (!done) { LayoutRoom chosenRoom = PickRandomRoom(); //on préfère faire spawn une nouvelle room de façon à ne pas transformer une room qui a déjà qu'un seul voisin en une qui en a deux if (!canAlterate && cptTries < 100) { if (chosenRoom.NbNeighbors > 1) { List <Vector2Int> freeSlots = GetEmptyValidSpotsAtPos(chosenRoom.Position, true, true); if (freeSlots.Count > 0) { Vector2Int spawnPosition = freeSlots[Random.Range(0, freeSlots.Count)]; if (CountNeighborsAtPos(spawnPosition) == 1) { SpawnRoom(new LayoutRoom(spawnPosition, 0)); done = true; } } } cptTries++; } else //Si aucune possibilité de spawn 1 room sans altérer celle qui n'ont déjà qu'un seul voisin, on spawn quand même une pour débloquer des possiblités { List <Vector2Int> freeSlots = GetEmptyValidSpotsAtPos(chosenRoom.Position, true, true); if (freeSlots.Count > 0) { Vector2Int spawnPosition = freeSlots[Random.Range(0, freeSlots.Count)]; if (CountNeighborsAtPos(spawnPosition) == 1) { SpawnRoom(new LayoutRoom(spawnPosition, 0)); done = true; } } } //protection anti boucle infinie de haute technologie cptInfinite++; if (cptInfinite > 1000) { Debug.Log("boucle infinie "); break; } } DisplayLayout(); }
/// <summary> /// parcours toutes les room et retourne la plus éloignée. Si il y en a plusieurs, retourne la première occurence. /// </summary> /// <returns></returns> public LayoutRoom FindFurthestRoom() { LayoutRoom furthestRoom = null; int currentMaxDistance = -1; foreach (LayoutRoom room in spawnedRooms) { if (furthestRoom == null || room.Distance > currentMaxDistance) { furthestRoom = room; currentMaxDistance = room.Distance; } } return(furthestRoom); }
public void GenerateLayout() { Init(); LayoutRoom firstRoom = new LayoutRoom(mapCenter, 0); SpawnRoom(firstRoom); int nbRoomToSpawn = Mathf.RoundToInt(nbRoom * (1 - randomessRatio)); int nbRdmSoloRoomAdded = Mathf.RoundToInt(nbRoom * randomessRatio); //première boucle pour faire spawn le layout général while (spawnedRooms.Count < nbRoomToSpawn && cpt < 1000) { LayoutRoom chosenRoom = PickRandomRoom(); float rdmBranch = Random.Range(0.0f, 1.0f); if (rdmBranch > 1 - verticalityRatio) //branch { ExpandVertically(chosenRoom, true); } else //expand normally { ExpandHorizontally(chosenRoom); } cpt++; } //seconde boucle pour mettre des room random par si par là cpt = 0; while (spawnedRooms.Count < nbRoomToSpawn + nbRdmSoloRoomAdded && cpt < 1000) { CreateRoom1Neighbor(true); cpt++; } DisplayLayout(); }
public DungeonLayout(int pathLength, int sidePathLength) { this.pathLength = pathLength; this.maxSidePathLength = sidePathLength; this.numRooms = 0; maxSize = (int)1.4 * pathLength; rooms = new LayoutRoom[maxSize, maxSize]; for (int i = 0; i < maxSize; i++) { for (int j = 0; j < maxSize; j++) { rooms[i, j] = new LayoutRoom(RoomType.NULL); } } startOffset = maxSize / 2; rooms[startOffset, startOffset].type = RoomType.START; numRooms += 1; ExtendPath(startOffset, startOffset, 0, true); Debug.Log(PrintLayout()); }
public void Expand(LayoutRoom room) { //on détecte les free slots autours List <Vector2Int> possiblePos = GetEmptyValidSpotsAtPos(room.Position, true, true); //on fait spawn des rooms int rdm; if (possiblePos.Count == 1) { rdm = Random.Range(0, possiblePos.Count + 1); } else { rdm = Random.Range(0, possiblePos.Count); } for (int i = 0; i < rdm; i++) { SpawnRoom(new LayoutRoom(possiblePos[Random.Range(0, possiblePos.Count)], 0)); } }
public string PrintLayout() { string returnString = ""; for (int i = 0; i < maxSize; i++) { returnString += "xxx"; } returnString += "xx\n"; for (int i = 0; i < maxSize; i++) { for (int j = 0; j < 3; j++) { returnString += "x"; for (int k = 0; k < maxSize; k++) { LayoutRoom r = rooms[k, i]; if (j == 0) { returnString += " "; if ((r.connections & NORTH) > 0) { returnString += (((r.connections >> JOIN_SHIFT) & NORTH) > 0) ? "o" : "| "; } else { returnString += " "; } returnString += " "; } else if (j == 1) { if ((r.connections & WEST) > 0) { returnString += (((r.connections >> JOIN_SHIFT) & WEST) > 0) ? "o" : "- "; } else { returnString += " "; } switch (r.type) { case RoomType.NULL: returnString += " "; break; case RoomType.START: returnString += "S"; break; case RoomType.DEAD_END: returnString += "D"; break; case RoomType.END: returnString += "E"; break; case RoomType.ENEMY: returnString += "O"; break; default: returnString += " "; break; } if ((r.connections & EAST) > 0) { returnString += (((r.connections >> JOIN_SHIFT) & EAST) > 0) ? "o" : " -"; } else { returnString += " "; } } else { returnString += " "; if ((r.connections & SOUTH) > 0) { returnString += (((r.connections >> JOIN_SHIFT) & SOUTH) > 0) ? "o" : "| "; } else { returnString += " "; } returnString += " "; } } returnString += "x"; returnString += "\n"; } } for (int i = 0; i < maxSize; i++) { returnString += "xxx"; } returnString += "xx"; Debug.Log(rooms[0, 0].connections); return(returnString); }
public int SortByNeighborCount(LayoutRoom r1, LayoutRoom r2) { return(r1.NbNeighbors.CompareTo(r2.NbNeighbors)); }
//Generes the level's layout based on the solution path private void GenerateLayout(List <int> path) { //Setting lists up roomLayout = new List <int> (); //roomLayoutRtrn = new List<int> (); List <int> sSolutionPath = new List <int> (); sSolutionPath.AddRange(solutionPath); sSolutionPath.Sort(); List <int> tSolutionPath = new List <int> (); List <LayoutRoom> indexList = new List <LayoutRoom> (); firstRun = false; int counter = 0; //Create a room type solution path equivalent for (int i = 0; i < solutionPath.Count; i++) { if (!firstRun) { if (solutionPath[i + 1] == (solutionPath[i] + 4)) { tSolutionPath.Add(2); firstRun = true; } else { print("Compared: " + solutionPath [i + 1] + " and " + (solutionPath [i] + 4) + " and it was true"); tSolutionPath.Add(1); firstRun = true; } } else if (i == solutionPath.Count - 1) { if (solutionPath[i - 1] == (solutionPath[i] - 4)) { tSolutionPath.Add(3); } else { print("Compared: " + solutionPath [i - 1] + " and " + (solutionPath [i] - 4) + " and it was false"); tSolutionPath.Add(1); } } else { if (solutionPath[i + 1] == (solutionPath[i] + 4) && solutionPath[i - 1] == (solutionPath[i] - 4)) { tSolutionPath.Add(4); } else if (solutionPath[i + 1] == (solutionPath[i] + 4)) { tSolutionPath.Add(2); } else if (solutionPath[i - 1] == (solutionPath[i] - 4)) { tSolutionPath.Add(3); } else { tSolutionPath.Add(1); } } } for (int i = 0; i < solutionPath.Count; i++) { LayoutRoom temp = new LayoutRoom(); temp.SetIndex(solutionPath[i]); temp.SetType(tSolutionPath[i]); indexList.Add(temp); } indexList.Sort(delegate(LayoutRoom x, LayoutRoom y) { return(x.index.CompareTo(y.index)); }); for (int i = 0; i < 16; i++) { roomLayout.Add(0); } for (int i = 1; i < roomLayout.Count + 1; i++) { if (counter < indexList.Count) { if (i == indexList[counter].GetIndex()) { roomLayout[i - 1] = indexList[counter].GetRType(); counter++; } } } }