private void AssignBiomes() { var biomes = LevelGenerationParams.GetBiomes(); float zoneWidth = Width / generationParams.DifficultyZones; List <Biome> allowedBiomes = new List <Biome>(10); for (int i = 0; i < generationParams.DifficultyZones; i++) { allowedBiomes.Clear(); allowedBiomes.AddRange(biomes.Where(b => b.AllowedZones.Contains(generationParams.DifficultyZones - i))); float zoneX = Width - zoneWidth * i; foreach (Location location in Locations) { if (location.MapPosition.X < zoneX) { location.Biome = allowedBiomes[Rand.Range(0, allowedBiomes.Count, Rand.RandSync.Server)]; } } } foreach (LocationConnection connection in Connections) { if (connection.Biome != null) { continue; } connection.Biome = connection.Locations[0].Biome; } System.Diagnostics.Debug.Assert(Locations.All(l => l.Biome != null)); System.Diagnostics.Debug.Assert(Connections.All(c => c.Biome != null)); }
private void AssignBiomes() { float locationRadius = size * 0.5f * generationParams.LocationRadius; var biomes = LevelGenerationParams.GetBiomes(); Vector2 centerPos = new Vector2(size, size) / 2; for (int i = 0; i < generationParams.DifficultyZones; i++) { List <Biome> allowedBiomes = biomes.FindAll(b => b.AllowedZones.Contains(generationParams.DifficultyZones - i)); float zoneRadius = locationRadius * ((i + 1.0f) / generationParams.DifficultyZones); foreach (LocationConnection connection in connections) { if (connection.Biome != null) { continue; } if (i == generationParams.DifficultyZones - 1 || Vector2.Distance(connection.Locations[0].MapPosition, centerPos) < zoneRadius || Vector2.Distance(connection.Locations[1].MapPosition, centerPos) < zoneRadius) { connection.Biome = allowedBiomes[Rand.Range(0, allowedBiomes.Count, Rand.RandSync.Server)]; } } } }
public LevelData(XElement element) { Seed = element.GetAttributeString("seed", ""); Difficulty = element.GetAttributeFloat("difficulty", 0.0f); Size = element.GetAttributePoint("size", new Point(1000)); Enum.TryParse(element.GetAttributeString("type", "LocationConnection"), out Type); string generationParamsId = element.GetAttributeString("generationparams", ""); GenerationParams = LevelGenerationParams.LevelParams.Find(l => l.Identifier == generationParamsId); if (GenerationParams == null) { DebugConsole.ThrowError($"Error while loading a level. Could not find level generation params with the ID \"{generationParamsId}\"."); GenerationParams = LevelGenerationParams.LevelParams.FirstOrDefault(l => l.Type == Type); if (GenerationParams == null) { GenerationParams = LevelGenerationParams.LevelParams.First(); } } string biomeIdentifier = element.GetAttributeString("biome", ""); Biome = LevelGenerationParams.GetBiomes().FirstOrDefault(b => b.Identifier == biomeIdentifier); if (Biome == null) { DebugConsole.ThrowError($"Error in level data: could not find the biome \"{biomeIdentifier}\"."); Biome = LevelGenerationParams.GetBiomes().First(); } string[] prefabNames = element.GetAttributeStringArray("eventhistory", new string[] { }); EventHistory.AddRange(EventSet.PrefabList.Where(p => prefabNames.Any(n => p.Identifier.Equals(n, StringComparison.InvariantCultureIgnoreCase)))); string[] nonRepeatablePrefabNames = element.GetAttributeStringArray("nonrepeatableevents", new string[] { }); NonRepeatableEvents.AddRange(EventSet.PrefabList.Where(p => prefabNames.Any(n => p.Identifier.Equals(n, StringComparison.InvariantCultureIgnoreCase)))); }
public static LevelData CreateRandom(string seed = "", float?difficulty = null, LevelGenerationParams generationParams = null) { if (string.IsNullOrEmpty(seed)) { seed = Rand.Range(0, int.MaxValue, Rand.RandSync.Server).ToString(); } Rand.SetSyncedSeed(ToolBox.StringToInt(seed)); LevelType type = generationParams == null ? LevelData.LevelType.LocationConnection : generationParams.Type; if (generationParams == null) { generationParams = LevelGenerationParams.GetRandom(seed, type); } var biome = LevelGenerationParams.GetBiomes().FirstOrDefault(b => generationParams.AllowedBiomes.Contains(b)) ?? LevelGenerationParams.GetBiomes().GetRandom(Rand.RandSync.Server); return(new LevelData( seed, difficulty ?? Rand.Range(30.0f, 80.0f, Rand.RandSync.Server), Rand.Range(0.0f, 1.0f, Rand.RandSync.Server), generationParams, biome)); }
public static LevelData CreateRandom(string seed = "", float?difficulty = null, LevelGenerationParams generationParams = null) { if (string.IsNullOrEmpty(seed)) { seed = Rand.Range(0, int.MaxValue, Rand.RandSync.Server).ToString(); } Rand.SetSyncedSeed(ToolBox.StringToInt(seed)); LevelType type = generationParams == null ? LevelData.LevelType.LocationConnection : generationParams.Type; if (generationParams == null) { generationParams = LevelGenerationParams.GetRandom(seed, type); } var biome = LevelGenerationParams.GetBiomes().FirstOrDefault(b => generationParams.AllowedBiomes.Contains(b)) ?? LevelGenerationParams.GetBiomes().GetRandom(Rand.RandSync.Server); var levelData = new LevelData( seed, difficulty ?? Rand.Range(30.0f, 80.0f, Rand.RandSync.Server), Rand.Range(0.0f, 1.0f, Rand.RandSync.Server), generationParams, biome); if (type == LevelType.LocationConnection) { float beaconRng = Rand.Range(0.0f, 1.0f, Rand.RandSync.Server); levelData.HasBeaconStation = beaconRng < 0.5f; levelData.IsBeaconActive = beaconRng > 0.25f; } GameMain.GameSession?.GameMode?.Mission?.AdjustLevelData(levelData); return(levelData); }
private void AssignBiomes() { List <LocationConnection> biomeSeeds = new List <LocationConnection>(); List <Biome> centerBiomes = LevelGenerationParams.GetBiomes().FindAll(b => b.Placement.HasFlag(Biome.MapPlacement.Center)); if (centerBiomes.Count > 0) { Vector2 mapCenter = new Vector2(locations.Sum(l => l.MapPosition.X), locations.Sum(l => l.MapPosition.Y)) / locations.Count; foreach (Biome centerBiome in centerBiomes) { LocationConnection closestConnection = null; float closestDist = float.PositiveInfinity; foreach (LocationConnection connection in connections) { if (connection.Biome != null) { continue; } float dist = Vector2.Distance(connection.CenterPos, mapCenter); if (closestConnection == null || dist < closestDist) { closestConnection = connection; closestDist = dist; } } closestConnection.Biome = centerBiome; biomeSeeds.Add(closestConnection); } } List <Biome> edgeBiomes = LevelGenerationParams.GetBiomes().FindAll(b => b.Placement.HasFlag(Biome.MapPlacement.Edge)); if (edgeBiomes.Count > 0) { List <LocationConnection> edges = GetMapEdges(); foreach (LocationConnection edge in edges) { edge.Biome = edgeBiomes[Rand.Range(0, edgeBiomes.Count, Rand.RandSync.Server)]; } } List <Biome> randomBiomes = LevelGenerationParams.GetBiomes().FindAll(b => b.Placement.HasFlag(Biome.MapPlacement.Random)); foreach (Biome biome in randomBiomes) { LocationConnection seed = connections[0]; while (seed.Biome != null) { seed = connections[Rand.Range(0, connections.Count, Rand.RandSync.Server)]; } seed.Biome = biome; biomeSeeds.Add(seed); } ExpandBiomes(biomeSeeds); }
public Biome GetBiome(float xPos) { float zoneWidth = Width / generationParams.DifficultyZones; int zoneIndex = (int)Math.Floor(xPos / zoneWidth) + 1; if (zoneIndex < 1) { return(LevelGenerationParams.GetBiomes().First()); } else if (zoneIndex >= generationParams.DifficultyZones) { return(LevelGenerationParams.GetBiomes().Last()); } return(LevelGenerationParams.GetBiomes().FirstOrDefault(b => b.AllowedZones.Contains(zoneIndex))); }
public LevelData(XElement element) { Seed = element.GetAttributeString("seed", ""); Difficulty = element.GetAttributeFloat("difficulty", 0.0f); Size = element.GetAttributePoint("size", new Point(1000)); Enum.TryParse(element.GetAttributeString("type", "LocationConnection"), out Type); HasBeaconStation = element.GetAttributeBool("hasbeaconstation", false); IsBeaconActive = element.GetAttributeBool("isbeaconactive", false); HasHuntingGrounds = element.GetAttributeBool("hashuntinggrounds", false); OriginallyHadHuntingGrounds = element.GetAttributeBool("originallyhadhuntinggrounds", HasHuntingGrounds); string generationParamsId = element.GetAttributeString("generationparams", ""); GenerationParams = LevelGenerationParams.LevelParams.Find(l => l.Identifier == generationParamsId || l.OldIdentifier == generationParamsId); if (GenerationParams == null) { DebugConsole.ThrowError($"Error while loading a level. Could not find level generation params with the ID \"{generationParamsId}\"."); GenerationParams = LevelGenerationParams.LevelParams.FirstOrDefault(l => l.Type == Type); if (GenerationParams == null) { GenerationParams = LevelGenerationParams.LevelParams.First(); } } InitialDepth = element.GetAttributeInt("initialdepth", GenerationParams.InitialDepthMin); string biomeIdentifier = element.GetAttributeString("biome", ""); Biome = LevelGenerationParams.GetBiomes().FirstOrDefault(b => b.Identifier == biomeIdentifier || b.OldIdentifier == biomeIdentifier); if (Biome == null) { DebugConsole.ThrowError($"Error in level data: could not find the biome \"{biomeIdentifier}\"."); Biome = LevelGenerationParams.GetBiomes().First(); } string[] prefabNames = element.GetAttributeStringArray("eventhistory", new string[] { }); EventHistory.AddRange(EventSet.PrefabList.Where(p => prefabNames.Any(n => p.Identifier.Equals(n, StringComparison.InvariantCultureIgnoreCase)))); string[] nonRepeatablePrefabNames = element.GetAttributeStringArray("nonrepeatableevents", new string[] { }); NonRepeatableEvents.AddRange(EventSet.PrefabList.Where(p => nonRepeatablePrefabNames.Any(n => p.Identifier.Equals(n, StringComparison.InvariantCultureIgnoreCase)))); }
/// <summary> /// Load a previously saved campaign map from XML /// </summary> private Map(CampaignMode campaign, XElement element) : this() { Seed = element.GetAttributeString("seed", "a"); Rand.SetSyncedSeed(ToolBox.StringToInt(Seed)); foreach (XElement subElement in element.Elements()) { switch (subElement.Name.ToString().ToLowerInvariant()) { case "location": int i = subElement.GetAttributeInt("i", 0); while (Locations.Count <= i) { Locations.Add(null); } Locations[i] = new Location(subElement); break; } } System.Diagnostics.Debug.Assert(!Locations.Contains(null)); for (int i = 0; i < Locations.Count; i++) { Locations[i].Reputation ??= new Reputation(campaign.CampaignMetadata, $"location.{i}", -100, 100, Rand.Range(-10, 10, Rand.RandSync.Server)); } foreach (XElement subElement in element.Elements()) { switch (subElement.Name.ToString().ToLowerInvariant()) { case "connection": Point locationIndices = subElement.GetAttributePoint("locations", new Point(0, 1)); if (locationIndices.X == locationIndices.Y) { continue; } var connection = new LocationConnection(Locations[locationIndices.X], Locations[locationIndices.Y]) { Passed = subElement.GetAttributeBool("passed", false), Difficulty = subElement.GetAttributeFloat("difficulty", 0.0f) }; Locations[locationIndices.X].Connections.Add(connection); Locations[locationIndices.Y].Connections.Add(connection); connection.LevelData = new LevelData(subElement.Element("Level")); string biomeId = subElement.GetAttributeString("biome", ""); connection.Biome = LevelGenerationParams.GetBiomes().FirstOrDefault(b => b.Identifier == biomeId) ?? LevelGenerationParams.GetBiomes().FirstOrDefault(b => b.OldIdentifier == biomeId) ?? LevelGenerationParams.GetBiomes().First(); Connections.Add(connection); break; } } int startLocationindex = element.GetAttributeInt("startlocation", -1); if (startLocationindex > 0 && startLocationindex < Locations.Count) { StartLocation = Locations[startLocationindex]; } else { DebugConsole.AddWarning($"Error while loading the map. Start location index out of bounds (index: {startLocationindex}, location count: {Locations.Count})."); foreach (Location location in Locations) { if (!location.Type.HasOutpost) { continue; } if (StartLocation == null || location.MapPosition.X < StartLocation.MapPosition.X) { StartLocation = location; } } } int endLocationindex = element.GetAttributeInt("endlocation", -1); if (endLocationindex > 0 && endLocationindex < Locations.Count) { EndLocation = Locations[endLocationindex]; } else { DebugConsole.AddWarning($"Error while loading the map. End location index out of bounds (index: {endLocationindex}, location count: {Locations.Count})."); foreach (Location location in Locations) { if (EndLocation == null || location.MapPosition.X > EndLocation.MapPosition.X) { EndLocation = location; } } } InitProjectSpecific(); }