protected HashSet <Vector3> GetNewStepVoxels(Candidate newCandidate) { HashSet <Vector3> StepVoxelsPos = new HashSet <Vector3>(); Volume volume = newCandidate.Volume; ConnPointCandidate firstOpenConnPointCandidate = newCandidate.NewConnPointCandidate; int connPointsCount = newCandidate.ConnPointCandidates.Count; for (int i = 0; i < connPointsCount; i++) { ConnPointCandidate newConnPointCandidate = newCandidate.ConnPointCandidates[i]; //check all OPEN connection points BUT the one we're connecting with. if (!newConnPointCandidate.Open || newConnPointCandidate == firstOpenConnPointCandidate) { continue; } Vector3 connPointCandidateStep = newConnPointCandidate.Rotation * newCandidate.HalfStep; Vector3 newConnPointWorldPostion = newConnPointCandidate.WorldPosition; Vector3 StepVoxelPosition = (newConnPointWorldPostion + connPointCandidateStep).RoundVec3ToInt(); StepVoxelsPos.Add(StepVoxelPosition); } return(StepVoxelsPos); }
protected HashSet <Vector3> GetOldStepVoxels(ConnPointCandidate lastConnPoint) { HashSet <Vector3> stepVoxelsPos = new HashSet <Vector3>(); int openSetCount = openCandidates.Count; for (int i = 0; i < openSetCount; i++) { Candidate openCandidate = openCandidates[i]; Volume volume = openCandidate.Volume; int openSetConnPointsCount = openCandidates[i].ConnPointCandidates.Count; for (int j = 0; j < openSetConnPointsCount; j++) { ConnPointCandidate oldConnPointCandidate = openCandidate.ConnPointCandidates[j]; if (!oldConnPointCandidate.Open || oldConnPointCandidate == lastConnPoint) { continue; } Vector3 connPointCandidateStep = oldConnPointCandidate.Rotation * openCandidate.HalfStep; Vector3 oldConnPointWorldPostion = oldConnPointCandidate.WorldPosition; Vector3 oldConnPointStepVoxel = (oldConnPointWorldPostion + connPointCandidateStep).RoundVec3ToInt(); stepVoxelsPos.Add(oldConnPointStepVoxel); } } return(stepVoxelsPos); }
protected CandidatesConnection CreateCandidatesConnection(ConnPointCandidate lastConnPointCandidate, ConnPointCandidate newConnPointCandidate) { CandidatesConnection candidatesConnection = new CandidatesConnection { LastConnPointCandidate = lastConnPointCandidate, NewConnPointCandidate = newConnPointCandidate }; return(candidatesConnection); }
protected Candidate ConnectNewCandidateToConnPoint(ConnPointCandidate lastConnPointCandidate) { Candidate lastCandidate = lastConnPointCandidate.Owner; //LOOK IF ELEMENT IN THE SPAWN ZONE ZoneItem zoneItem = GetCandidateSpawnZoneItem(lastConnPointCandidate, lastCandidate.Step); //PUT IN SEPARATE FUNCTION DungeonSet spawnZoneSet = null; if (zoneItem != null) { spawnZoneSet = zoneItem.spawnSet; if (spawnZoneSet == null && zoneItem.randomizeSpawnSet) { int randomSetIndex = Random.Range(1, factoryOwner.Sets.Count); spawnZoneSet = factoryOwner.Sets[randomSetIndex]; zoneItem.spawnSet = spawnZoneSet; } } DungeonSet lastCandidateSet = lastCandidate.Set; DungeonSet candidatesSet = SetPotentialCandidates(spawnZoneSet, lastCandidateSet); //PUT IN SEPARATE FUNCTION if (zoneItem != null && zoneItem.emptySpace) { potentialCandidates = lastCandidateSet.GetAllClosingElementsShuffled(random); } // Candidate newCandidate = null; int timesLooped = 0; int maximumLoopsAllowed = potentialCandidates.Length << factoryOwner.loopCheckCount; do { if (timesLooped++ > maximumLoopsAllowed) { throw new System.Exception("CandidatesManager::Candidate generation takes too long - Possible infinite loop."); } newCandidate = CreateNewCandidate(lastConnPointCandidate, candidatesSet); } while (potentialCandidates.Length > 0 && newCandidate == null); if (newCandidate != null) { AcceptNewCandidate(newCandidate, lastCandidate); } return(newCandidate); }
protected Candidate CreateNewCandidate(ConnPointCandidate lastConnPointCandidate, DungeonSet candidatesSet) { VoxelStep newCandidateStepVoxel = new VoxelStep(); // GENERATE ROOM WITH ONE CONNECTION POINT (only after all target rooms have been exhausted) if (acceptedCandidates.Count >= factoryOwner.TargetElementsCount) { potentialCandidates = lastConnPointCandidate.OwnerSet.GetAllClosingElementsShuffled(random); } // GET RANDOM NEW CANDIDATE Candidate newCandidate = GetNewCandidate(potentialCandidates, candidatesSet); potentialCandidates = potentialCandidates.RemoveFromArray(newCandidate.Element); // CHECK IF NEW ELEMENT ALLOWED NEIGHBOUR TO LAST ELEMENT bool notAllowedNeighbours = CheckIfCandidatesCanBeNeighbours(GetFirstOpenCandidate(), newCandidate); if (notAllowedNeighbours) { potentialCandidates = candidatesSet.GetAllHallwayElementsShuffled(random); return(null); } // COONECT THE CANDIDATES ConnPointCandidate newConnPointCandidate = AlignNewCandidateWithConnPoint(lastConnPointCandidate, newCandidate); CandidatesConnection candidatesConnection = CreateCandidatesConnection(lastConnPointCandidate, newConnPointCandidate); candidatesConnection.LastCandidateWorldPos = GetFirstOpenCandidate().WorldPos; newCandidate.CandidatesConnection = candidatesConnection; //STEP VOXELS newCandidateStepVoxel.oldStepVoxelsPos = GetOldStepVoxels(lastConnPointCandidate); newCandidateStepVoxel.newStepVoxelsPos = GetNewStepVoxels(newCandidate); // //candidatesConnection.NewCandidateStepVoxel = newCandidateStepVoxel; //CHECK OVERLAP bool overlap = CheckIfNewElementOverlaps(newCandidateStepVoxel, newCandidate); if (overlap) { if (potentialCandidates.Length == 0) { potentialCandidates = candidatesSet.GetAllClosingElementsShuffled(random); } return(null); } return(newCandidate); }
protected ConnPointCandidate AlignNewCandidateWithConnPoint(ConnPointCandidate lastConnPointCandidate, Candidate newCandidate) { ConnPointCandidate newConnPointCandidate = newCandidate.GetRandomOpenConnPoint(random); float lastAngleY = lastConnPointCandidate.Rotation.eulerAngles.y; float newAngleY = newConnPointCandidate.Rotation.eulerAngles.y; float angleDiffernce = lastAngleY - newAngleY + 180f; newCandidate.Rotation = Quaternion.AngleAxis(angleDiffernce, Vector3.up); Vector3 lastConnPointWorldPos = lastConnPointCandidate.WorldPosition; Vector3 translation = lastConnPointWorldPos - newCandidate.Rotation * newConnPointCandidate.LocalPosition; newCandidate.WorldPos += translation; newCandidate.SetVoxelsWorldPos(translation, newCandidate.Rotation); newCandidate.UpdateConnPointsWorldPos(angleDiffernce); return(newConnPointCandidate); }
public void GenerateNextCandidate() { Candidate lastCandidate = GetFirstOpenCandidate(); //GET NEW RANDOM CONNECTION POINTS FROM THE CURRENTLY OPEN ELEMENT List <ConnPointCandidate> connPointCandidate = lastCandidate.GetAllOpenConnPoints(random); int connPointCandidateCount = connPointCandidate.Count; int connPointCandidateIndex = 0; Candidate newCandidate = null; do { ConnPointCandidate lastConnPointCandidate = connPointCandidate[connPointCandidateIndex++]; newCandidate = ConnectNewCandidateToConnPoint(lastConnPointCandidate); } while (newCandidate == null && connPointCandidateIndex < connPointCandidateCount); if (newCandidate == null) { throw new System.Exception("CandidateManager::No Candidate fits."); } }
protected ZoneItem GetCandidateSpawnZoneItem(ConnPointCandidate lastConnPointCandidate, Vector3 step) { Vector3 connPointCandidateStep = lastConnPointCandidate.Rotation * step; Vector3 oldConnPointWorldPostion = lastConnPointCandidate.WorldPosition; Vector3 oldConnPointStepVoxel = (oldConnPointWorldPostion + connPointCandidateStep).RoundVec3ToInt(); List <ZoneItem> spawnZones = zonesGenerator.SpawnZones; int spawnZonesCount = spawnZones.Count; for (int i = 0; i < spawnZonesCount; i++) { ZoneItem zoneItem = spawnZones[i]; bool candidateInSpawnZone = zoneItem.ContainsPoint(oldConnPointStepVoxel); if (candidateInSpawnZone) { return(zoneItem); } } return(null); }