private int GetRoomNumberToTry(List <Room> possibleRooms) { int doors = 0; int r = random.range(0, possibleRooms.Count - 1); ////Debug.Log("r: " + r); ////Debug.Log(possibleRooms.Count); GameObject roomToTry = possibleRooms[r].gameObject; doors = roomToTry.GetComponent <Room>().doors.Count; //If we picked a room with with one door, try again UNLESS we've have no other rooms to try if (doors == 1 && possibleRooms.Count > 1) { //Debug.Log("we're adding a room with one door when we have other's we could try first.."); float chance = 1f - Mathf.Sqrt(((float)roomsCount / (float)targetRooms)); //the closer we are to target the less of a chance of changing rooms float randomValue = random.value(); //Debug.Log("Chance: " + chance + " | Random value: " + randomValue); if (randomValue < chance) { r = random.range(0, possibleRooms.Count - 1); roomToTry = possibleRooms[r].gameObject; //Debug.Log("trying a new room"); //Debug.Log("New room has doors: " + roomToTry.GetComponent<Room>().doors.Count); doors = roomToTry.GetComponent <Room>().doors.Count; if (doors == 1 && possibleRooms.Count > 1) { float chance2 = 1f - Mathf.Sqrt(((float)roomsCount / (float)targetRooms)); //the closer we are to target the less of a chance of changing rooms float randomValue2 = random.value(); if (randomValue2 < chance2) { r = random.range(0, possibleRooms.Count - 1); roomToTry = possibleRooms[r].gameObject; } else { //Debug.Log("Oh well again.."); } } } else { //Debug.Log("Oh well!"); } } return(r); }
void Start() { //instance = this; if (randomizeSeedOnStart) { seed = Random.Range(0, int.MaxValue); } random = new DRandom(); random.Init(seed); if (randomizeRoomSize) { targetRooms = 15 + (int)(random.value() * 50f); } roomsCount = 0; globalVoxels = new Dictionary <Vector3, GameObject>(); dungeonSet = random.range(0, data.sets.Count - 1); //Debug.Log("Generating dungeon with data:"); //Debug.Log("Rooms count: " + targetRooms); //Debug.Log("Using set: " + data.sets[dungeonSet].name); try { StartCoroutine(StartGeneration()); } catch (Exception e) { Console.WriteLine(e); throw; } Debug.Log("Generated in: " + stopwatch.ElapsedMilliseconds + "ms"); }
void Start() { //instance = this; if (randomizeSeedOnStart) { seed = Random.Range(0, int.MaxValue); } random = new DRandom(); random.Init(seed); if (randomizeRoomSize) { targetRooms = 30 + (int)(random.value() * 50f); } Debug.Log(targetRooms); roomsCount = 0; globalVoxels = new Dictionary <Vector3, GameObject>(); dungeonSet = random.range(0, data.sets.Count - 1); //Debug.Log("Generating dungeon with data:"); //Debug.Log("Rooms count: " + targetRooms); //Debug.Log("Using set: " + data.sets[dungeonSet].name); StartGeneration(); }
protected GameObject GetElemThatMightBeOneWay(Element[] possibleElements) { int randomRoomIndex = random.range(0, possibleElements.Length - 1); GameObject roomToTry = possibleElements[randomRoomIndex].gameObject; int connPointsCount = roomToTry.GetComponent <Element>().connPoints.Count; if (connPointsCount == 1 && possibleElements.Length > 1) { float elementsCountRatio = acceptedCandidates.Count / (float)factoryOwner.TargetElementsCount; float chance = 1f - Mathf.Sqrt(elementsCountRatio); //the closer we are to target the less of a chance of changing rooms float randomValue = random.value(); if (randomValue < chance) { return(GetElemThatMightBeOneWay(possibleElements)); } } return(roomToTry); }
private void GenerateNextRoom() { Room lastRoom = startRoom.GetComponent <Room>(); if (openSet.Count > 0) { lastRoom = openSet[0]; } //create a mutable list of all possible rooms List <Room> possibleRooms = new List <Room>(); for (int i = 0; i < data.sets[dungeonSet].roomTemplates.Count; i++) { possibleRooms.Add(data.sets[dungeonSet].roomTemplates[i]); } possibleRooms.Shuffle(random.random); GameObject newRoom; GeneratorDoor door; bool roomIsGood = false; //Debug.Log("count: " + data.sets[dungeonSet].roomTemplates.Count); do { for (int i = 0; i < doorVoxelsTest.Count; i++) { GameObject.DestroyImmediate(doorVoxelsTest[i]); } doorVoxelsTest.Clear(); if (roomsCount >= targetRooms) { possibleRooms = GetAllRoomsWithOneDoor(possibleRooms); //Debug.Log("ADDING END ROOMS TARGET REACHED!"); } //If we picked a room with with one door, try again UNLESS we've have no other rooms to try int doors = 0; bool tryAgain = false; GameObject roomToTry; int r = random.range(0, possibleRooms.Count - 1); ////Debug.Log("r: " + r); ////Debug.Log(possibleRooms.Count); roomToTry = possibleRooms[r].gameObject; doors = roomToTry.GetComponent <Room>().doors.Count; if (doors == 1 && possibleRooms.Count > 1) { //Debug.Log("we're adding a room with one door when we have other's we could try first.."); float chance = 1f - Mathf.Sqrt(((float)roomsCount / (float)targetRooms)); //the closer we are to target the less of a chance of changing rooms float randomValue = random.value(); //Debug.Log("Chance: " + chance + " | Random value: " + randomValue); if (randomValue < chance) { r = random.range(0, possibleRooms.Count - 1); roomToTry = possibleRooms[r].gameObject; //Debug.Log("trying a new room"); //Debug.Log("New room has doors: " + roomToTry.GetComponent<Room>().doors.Count); doors = roomToTry.GetComponent <Room>().doors.Count; if (doors == 1 && possibleRooms.Count > 1) { float chance2 = 1f - Mathf.Sqrt(((float)roomsCount / (float)targetRooms)); //the closer we are to target the less of a chance of changing rooms float randomValue2 = random.value(); if (randomValue2 < chance2) { r = random.range(0, possibleRooms.Count - 1); roomToTry = possibleRooms[r].gameObject; } else { //Debug.Log("Oh well again.."); } } } else { //Debug.Log("Oh well!"); } } possibleRooms.RemoveAt(r); newRoom = (GameObject)Instantiate(roomToTry); newRoom.transform.parent = this.gameObject.transform; door = ConnectRooms(lastRoom, newRoom.GetComponent <Room>()); //room is now generated and in position... we need to test overlap now! Volume v = newRoom.GetComponent <Volume>(); Room ro = newRoom.GetComponent <Room>(); bool overlap = false; for (int i = 0; i < v.voxels.Count; i++) { if (globalVoxels.ContainsKey(RoundVec3ToInt(v.voxels[i].gameObject.transform.position))) { //overlap found! bad! //Debug.Log("THERE IS AN OVERLAP!!"); overlap = true; continue; } for (int j = 0; j < openSet.Count; j++) { for (int k = 0; k < openSet[j].doors.Count; k++) { //check if door is in the globalVoxelList if (!openSet[j].doors[k].open) { continue; } //we also want to ignore the Door we're connecting with if (openSet[j].doors[k] == door) { continue; } float rot = NormalizeAngle(Mathf.RoundToInt(openSet[j].doors[k].transform.rotation.eulerAngles.y)); Vector3 direction = new Vector3(); if (rot == 0) { ////Debug.Log("Door: " + i + " is facing: +X"); direction = new Vector3(1f, 0f, 0f); } else if (rot == 180) { ////Debug.Log("Door: " + i + " is facing: -X"); direction = new Vector3(-1f, 0f, 0f); } else if (rot == 90) { ////Debug.Log("Door: " + i + " is facing: -Z"); direction = new Vector3(0f, 0f, -1f); } else if (rot == 270) { ////Debug.Log("Door: " + i + " is facing: +Z"); direction = new Vector3(0f, 0f, 1f); } GameObject g = GameObject.CreatePrimitive(PrimitiveType.Sphere); g.transform.position = openSet[j].doors[k].voxelOwner.transform.position + (direction * v.voxelScale); g.GetComponent <Renderer>().material.color = Color.red; doorVoxelsTest.Add(g); if (RoundVec3ToInt(v.voxels[i].gameObject.transform.position) == RoundVec3ToInt(openSet[j].doors[k].voxelOwner.transform.position + (direction * v.voxelScale))) { overlap = true; //Debug.Log("Room is overlapping a door voxel neighbour!!!"); } else { //Debug.Log("Room is NOT overlapping with a door voxel neighbour!"); } } } } bool hasSpace = true; if (!overlap) { //Debug.Log("NO overlap with the room...checking doors"); //check all the doors, and make sure there's at leas a 1x1x1 voxel of air out of it //this will enure we have room for a treasure room at least, and no doors will lead right into a wall! for (int i = 0; i < ro.doors.Count; i++) { //we need to find the direction the door is pointing in world space.. //Debug.Log(i + " : " + ro.doors[i].open); if (!ro.doors[i].open) { continue; //check all OPEN doors BUT the one we're connecting with.. } if (ro.doors[i] == newRoom.GetComponent <Room>().GetFirstOpenDoor()) { continue; } //Debug.Log("Actually checking door: " + i); float rot = NormalizeAngle(Mathf.RoundToInt(ro.doors[i].transform.rotation.eulerAngles.y)); Vector3 direction = new Vector3(); if (rot == 0) { ////Debug.Log("Door: " + i + " is facing: +X"); direction = new Vector3(1f, 0f, 0f); } else if (rot == 180) { ////Debug.Log("Door: " + i + " is facing: -X"); direction = new Vector3(-1f, 0f, 0f); } else if (rot == 90) { ////Debug.Log("Door: " + i + " is facing: -Z"); direction = new Vector3(0f, 0f, -1f); } else if (rot == 270) { ////Debug.Log("Door: " + i + " is facing: +Z"); direction = new Vector3(0f, 0f, 1f); } //Debug.Log("Drawing spheres"); GameObject g = GameObject.CreatePrimitive(PrimitiveType.Sphere); g.transform.position = ro.doors[i].voxelOwner.transform.position + (direction * v.voxelScale); doorVoxelsTest.Add(g); if (globalVoxels.ContainsKey(RoundVec3ToInt(ro.doors[i].voxelOwner.transform.position + (direction * v.voxelScale)))) { //we have a collision on the door neighbours //Debug.Log("WE HAVE A COLLISION WITH THE DOOR NEIGHBOURS"); hasSpace = false; break; } else { //we good! //Debug.Log("We don't have a collision witht he door neighbours"); //check doors against all other doors so that no door voxels overlap with other door voxels for (int j = 0; j < openSet.Count; j++) { for (int k = 0; k < openSet[j].doors.Count; k++) { if (!openSet[j].doors[k].open) { continue; } float rot2 = NormalizeAngle(Mathf.RoundToInt(openSet[j].doors[k].transform.rotation.eulerAngles.y)); Vector3 direction2 = new Vector3(); if (rot2 == 0) { ////Debug.Log("Door: " + i + " is facing: +X"); direction2 = new Vector3(1f, 0f, 0f); } else if (rot2 == 180) { ////Debug.Log("Door: " + i + " is facing: -X"); direction2 = new Vector3(-1f, 0f, 0f); } else if (rot2 == 90) { ////Debug.Log("Door: " + i + " is facing: -Z"); direction2 = new Vector3(0f, 0f, -1f); } else if (rot2 == 270) { ////Debug.Log("Door: " + i + " is facing: +Z"); direction2 = new Vector3(0f, 0f, 1f); } if (RoundVec3ToInt(ro.doors[i].voxelOwner.transform.position + (direction * v.voxelScale)) == RoundVec3ToInt(openSet[j].doors[k].voxelOwner.transform.position + (direction2 * v.voxelScale))) { hasSpace = false; //Debug.Log("TWo door voxels overlapping!"); break; } } if (!hasSpace) { break; } } } } } if (!overlap && hasSpace) { //Debug.Log("all next rooms will fit!"); roomIsGood = true; } else { GameObject.DestroyImmediate(newRoom); //Debug.Log("Try a different room!!!!--------"); //destroy the room we just tried to place } } while (possibleRooms.Count > 0 && !roomIsGood); if (!roomIsGood) { //we failed! //Debug.Log("NO ROoms THAT FIT, THIS IS BAAAAD! ... but should never happen!"); } else { GeneratorDoor otherDoor = newRoom.GetComponent <Room>().GetFirstOpenDoor(); door.sharedDoor = otherDoor; otherDoor.sharedDoor = door; door.open = false; newRoom.GetComponent <Room>().GetFirstOpenDoor().open = false; rooms.Add(newRoom.GetComponent <Room>()); AddGlobalVoxels(newRoom.GetComponent <Volume>().voxels); if (!lastRoom.hasOpenDoors()) { openSet.Remove(lastRoom); } if (newRoom.GetComponent <Room>().hasOpenDoors()) { openSet.Add(newRoom.GetComponent <Room>()); } roomsCount++; //Debug.Log("Openset: " + openSet.Count); } }