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)))); }
private LevelGenerationParams(XElement element) { Name = element == null ? "default" : element.Name.ToString(); ObjectProperties = ObjectProperty.InitProperties(this, element); Vector3 colorVector = element.GetAttributeVector3("BackgroundColor", new Vector3(50, 46, 20)); BackgroundColor = new Color((int)colorVector.X, (int)colorVector.Y, (int)colorVector.Z); colorVector = element.GetAttributeVector3("WallColor", new Vector3(255, 255, 255)); WallColor = new Color((int)colorVector.X, (int)colorVector.Y, (int)colorVector.Z); VoronoiSiteInterval = element.GetAttributeVector2("VoronoiSiteInterval", new Vector2(3000, 3000)); VoronoiSiteVariance = element.GetAttributeVector2("VoronoiSiteVariance", new Vector2(voronoiSiteInterval.X, voronoiSiteInterval.Y) * 0.4f); MainPathNodeIntervalRange = element.GetAttributeVector2("MainPathNodeIntervalRange", new Vector2(5000.0f, 10000.0f)); SmallTunnelLengthRange = element.GetAttributeVector2("SmallTunnelLengthRange", new Vector2(5000.0f, 10000.0f)); string biomeStr = element.GetAttributeString("biomes", ""); if (string.IsNullOrWhiteSpace(biomeStr)) { allowedBiomes = new List <Biome>(biomes); } else { string[] biomeNames = biomeStr.Split(','); for (int i = 0; i < biomeNames.Length; i++) { string biomeName = biomeNames[i].Trim().ToLowerInvariant(); Biome matchingBiome = biomes.Find(b => b.Name.ToLowerInvariant() == biomeName); if (matchingBiome == null) { DebugConsole.ThrowError("Error in level generation parameters: biome \"" + biomeName + "\" not found."); continue; } allowedBiomes.Add(matchingBiome); } } }
private LevelGenerationParams(XElement element) { Identifier = element == null ? "default" : element.GetAttributeString("identifier", null) ?? element.Name.ToString(); SerializableProperties = SerializableProperty.DeserializeProperties(this, element); if (element == null) { return; } string biomeStr = element.GetAttributeString("biomes", ""); if (string.IsNullOrWhiteSpace(biomeStr) || biomeStr.Equals("any", StringComparison.OrdinalIgnoreCase)) { allowedBiomes = new List <Biome>(biomes); } else { string[] biomeNames = biomeStr.Split(','); for (int i = 0; i < biomeNames.Length; i++) { string biomeName = biomeNames[i].Trim().ToLowerInvariant(); if (biomeName == "none") { continue; } Biome matchingBiome = biomes.Find(b => b.Identifier.Equals(biomeName, StringComparison.OrdinalIgnoreCase)); if (matchingBiome == null) { matchingBiome = biomes.Find(b => b.DisplayName.Equals(biomeName, StringComparison.OrdinalIgnoreCase)); if (matchingBiome == null) { DebugConsole.ThrowError("Error in level generation parameters: biome \"" + biomeName + "\" not found."); continue; } else { DebugConsole.NewMessage("Please use biome identifiers instead of names in level generation parameter \"" + Identifier + "\".", Color.Orange); } } allowedBiomes.Add(matchingBiome); } } foreach (XElement subElement in element.Elements()) { switch (subElement.Name.ToString().ToLowerInvariant()) { case "background": BackgroundSprite = new Sprite(subElement); break; case "backgroundtop": BackgroundTopSprite = new Sprite(subElement); break; case "wall": WallSprite = new Sprite(subElement); break; case "wallspecular": WallSpriteSpecular = new Sprite(subElement); break; case "walledge": WallEdgeSprite = new Sprite(subElement); break; case "walledgespecular": WallEdgeSpriteSpecular = new Sprite(subElement); break; case "waterparticles": WaterParticles = new Sprite(subElement); break; } } }
public static LevelGenerationParams GetRandom(string seed, LevelData.LevelType type, Biome biome = null) { Rand.SetSyncedSeed(ToolBox.StringToInt(seed)); if (LevelParams == null || !LevelParams.Any()) { DebugConsole.ThrowError("Level generation presets not found - using default presets"); return(new LevelGenerationParams(null)); } var matchingLevelParams = LevelParams.FindAll(lp => lp.Type == type && lp.allowedBiomes.Any()); if (biome == null) { matchingLevelParams = matchingLevelParams.FindAll(lp => !lp.allowedBiomes.Any(b => b.IsEndBiome)); } else { matchingLevelParams = matchingLevelParams.FindAll(lp => lp.allowedBiomes.Contains(biome)); } if (matchingLevelParams.Count == 0) { DebugConsole.ThrowError($"Suitable level generation presets not found (biome \"{(biome?.Identifier ?? "null")}\", type: \"{type}\"!"); if (biome != null) { //try to find params that at least have a suitable type matchingLevelParams = LevelParams.FindAll(lp => lp.Type == type); if (matchingLevelParams.Count == 0) { //still not found, give up and choose some params randomly matchingLevelParams = LevelParams; } } } return(matchingLevelParams[Rand.Range(0, matchingLevelParams.Count, Rand.RandSync.Server)]); }
public static void OnBiomeDiscovered(Biome biome) { UnlockAchievement("discover" + biome.Name.ToLowerInvariant().Replace(" ", "")); }
private LevelGenerationParams(XElement element) { Name = element == null ? "default" : element.Name.ToString(); SerializableProperties = SerializableProperty.DeserializeProperties(this, element); string biomeStr = element.GetAttributeString("biomes", ""); if (string.IsNullOrWhiteSpace(biomeStr)) { allowedBiomes = new List <Biome>(biomes); } else { string[] biomeNames = biomeStr.Split(','); for (int i = 0; i < biomeNames.Length; i++) { string biomeName = biomeNames[i].Trim().ToLowerInvariant(); if (biomeName == "none") { continue; } Biome matchingBiome = biomes.Find(b => b.Name.ToLowerInvariant() == biomeName); if (matchingBiome == null) { DebugConsole.ThrowError("Error in level generation parameters: biome \"" + biomeName + "\" not found."); continue; } allowedBiomes.Add(matchingBiome); } } foreach (XElement subElement in element.Elements()) { switch (subElement.Name.ToString().ToLowerInvariant()) { case "background": BackgroundSprite = new Sprite(subElement); break; case "backgroundtop": BackgroundTopSprite = new Sprite(subElement); break; case "wall": WallSprite = new Sprite(subElement); break; case "wallspecular": WallSpriteSpecular = new Sprite(subElement); break; case "walledge": WallEdgeSprite = new Sprite(subElement); break; case "walledgespecular": WallEdgeSpriteSpecular = new Sprite(subElement); break; case "waterparticles": WaterParticles = new Sprite(subElement); break; } } }
public LevelData(string seed, float difficulty, float sizeFactor, LevelGenerationParams generationParams, Biome biome) { Seed = seed ?? throw new ArgumentException("Seed was null"); Biome = biome ?? throw new ArgumentException("Biome was null"); GenerationParams = generationParams ?? throw new ArgumentException("Level generation parameters were null"); Type = GenerationParams.Type; Difficulty = difficulty; sizeFactor = MathHelper.Clamp(sizeFactor, 0.0f, 1.0f); int width = (int)MathHelper.Lerp(generationParams.MinWidth, generationParams.MaxWidth, sizeFactor); InitialDepth = (int)MathHelper.Lerp(generationParams.InitialDepthMin, generationParams.InitialDepthMax, sizeFactor); Size = new Point( (int)MathUtils.Round(width, Level.GridCellSize), (int)MathUtils.Round(generationParams.Height, Level.GridCellSize)); }