/// <summary> /// Instantiates level data using the properties of the connection (seed, size, difficulty) /// </summary> public LevelData(LocationConnection locationConnection) { Seed = locationConnection.Locations[0].BaseName + locationConnection.Locations[1].BaseName; Biome = locationConnection.Biome; Type = LevelType.LocationConnection; GenerationParams = LevelGenerationParams.GetRandom(Seed, LevelType.LocationConnection, Biome); Difficulty = locationConnection.Difficulty; float sizeFactor = MathUtils.InverseLerp( MapGenerationParams.Instance.SmallLevelConnectionLength, MapGenerationParams.Instance.LargeLevelConnectionLength, locationConnection.Length); int width = (int)MathHelper.Lerp(GenerationParams.MinWidth, GenerationParams.MaxWidth, sizeFactor); Size = new Point( (int)MathUtils.Round(width, Level.GridCellSize), (int)MathUtils.Round(GenerationParams.Height, Level.GridCellSize)); var rand = new MTRandom(ToolBox.StringToInt(Seed)); InitialDepth = (int)MathHelper.Lerp(GenerationParams.InitialDepthMin, GenerationParams.InitialDepthMax, (float)rand.NextDouble()); //minimum difficulty of the level before hunting grounds can appear float huntingGroundsDifficultyThreshold = 25; //probability of hunting grounds appearing in 100% difficulty levels float maxHuntingGroundsProbability = 0.3f; HasHuntingGrounds = OriginallyHadHuntingGrounds = rand.NextDouble() < MathUtils.InverseLerp(huntingGroundsDifficultyThreshold, 100.0f, Difficulty) * maxHuntingGroundsProbability; HasBeaconStation = !HasHuntingGrounds && rand.NextDouble() < locationConnection.Locations.Select(l => l.Type.BeaconStationChance).Max(); IsBeaconActive = false; }
private EventSet SelectRandomEvents(List <EventSet> eventSets) { if (level == null) { return(null); } MTRandom rand = new MTRandom(ToolBox.StringToInt(level.Seed)); var allowedEventSets = eventSets.Where(es => level.Difficulty >= es.MinLevelDifficulty && level.Difficulty <= es.MaxLevelDifficulty && level.LevelData.Type == es.LevelType); if (GameMain.GameSession?.GameMode is CampaignMode campaign && campaign.Map?.CurrentLocation?.Type != null) { allowedEventSets = allowedEventSets.Where(set => set.LocationTypeIdentifiers == null || set.LocationTypeIdentifiers.Any(identifier => string.Equals(identifier, campaign.Map.CurrentLocation.Type.Identifier, StringComparison.OrdinalIgnoreCase))); } float totalCommonness = allowedEventSets.Sum(e => e.GetCommonness(level)); float randomNumber = (float)rand.NextDouble() * totalCommonness; foreach (EventSet eventSet in allowedEventSets) { float commonness = eventSet.GetCommonness(level); if (randomNumber <= commonness) { return(eventSet); } randomNumber -= commonness; } return(null); }
/// <summary> /// Instantiates level data using the properties of the connection (seed, size, difficulty) /// </summary> public LevelData(LocationConnection locationConnection) { Seed = locationConnection.Locations[0].BaseName + locationConnection.Locations[1].BaseName; Biome = locationConnection.Biome; Type = LevelType.LocationConnection; GenerationParams = LevelGenerationParams.GetRandom(Seed, LevelType.LocationConnection, Biome); Difficulty = locationConnection.Difficulty; float sizeFactor = MathUtils.InverseLerp( MapGenerationParams.Instance.SmallLevelConnectionLength, MapGenerationParams.Instance.LargeLevelConnectionLength, locationConnection.Length); int width = (int)MathHelper.Lerp(GenerationParams.MinWidth, GenerationParams.MaxWidth, sizeFactor); Size = new Point( (int)MathUtils.Round(width, Level.GridCellSize), (int)MathUtils.Round(GenerationParams.Height, Level.GridCellSize)); var rand = new MTRandom(ToolBox.StringToInt(Seed)); InitialDepth = (int)MathHelper.Lerp(GenerationParams.InitialDepthMin, GenerationParams.InitialDepthMax, (float)rand.NextDouble()); HasBeaconStation = rand.NextDouble() < locationConnection.Locations.Select(l => l.Type.BeaconStationChance).Max(); IsBeaconActive = false; }
public static Mission LoadRandom(Location[] locations, MTRandom rand, string missionType = "", bool isSinglePlayer = false) { //todo: use something else than strings to define the mission type missionType = missionType.ToLowerInvariant(); List <MissionPrefab> allowedMissions = new List <MissionPrefab>(); if (missionType == "random") { allowedMissions.AddRange(MissionPrefab.List); if (GameMain.Server != null) { allowedMissions.RemoveAll(mission => !GameMain.Server.AllowedRandomMissionTypes.Any(a => mission.TypeMatches(a))); } } else if (missionType == "none") { return(null); } else if (string.IsNullOrWhiteSpace(missionType)) { allowedMissions.AddRange(MissionPrefab.List); } else { allowedMissions = MissionPrefab.List.FindAll(m => m.TypeMatches(missionType)); } if (isSinglePlayer) { allowedMissions.RemoveAll(m => m.MultiplayerOnly); } else { allowedMissions.RemoveAll(m => m.SingleplayerOnly); } float probabilitySum = allowedMissions.Sum(m => m.Commonness); float randomNumber = (float)rand.NextDouble() * probabilitySum; foreach (MissionPrefab missionPrefab in allowedMissions) { if (randomNumber <= missionPrefab.Commonness) { return(missionPrefab.Instantiate(locations)); } randomNumber -= missionPrefab.Commonness; } return(null); }
private ScriptedEventSet SelectRandomEvents(List <ScriptedEventSet> eventSets) { MTRandom rand = new MTRandom(ToolBox.StringToInt(level.Seed)); var allowedEventSets = eventSets.Where(es => level.Difficulty >= es.MinLevelDifficulty && level.Difficulty <= es.MaxLevelDifficulty); float totalCommonness = allowedEventSets.Sum(e => e.GetCommonness(level)); float randomNumber = (float)rand.NextDouble() * totalCommonness; foreach (ScriptedEventSet eventSet in allowedEventSets) { float commonness = eventSet.GetCommonness(level); if (randomNumber <= commonness) { return(eventSet); } randomNumber -= commonness; } return(null); }
private void CreateDummyLocations() { dummyLocations = new Location[2]; string seed = ""; if (GameMain.GameSession != null && GameMain.GameSession.Level != null) { seed = GameMain.GameSession.Level.Seed; } else if (GameMain.NetLobbyScreen != null) { seed = GameMain.NetLobbyScreen.LevelSeed; } MTRandom rand = new MTRandom(ToolBox.StringToInt(seed)); for (int i = 0; i < 2; i++) { dummyLocations[i] = Location.CreateRandom(new Vector2((float)rand.NextDouble() * 10000.0f, (float)rand.NextDouble() * 10000.0f)); } }
public static Mission LoadRandom(Location[] locations, MTRandom rand, string missionType = "", bool isSinglePlayer = false) { missionType = missionType.ToLowerInvariant(); var files = GameMain.SelectedPackage.GetFilesOfType(ContentType.Missions); string configFile = files[rand.Next(files.Count)]; XDocument doc = ToolBox.TryLoadXml(configFile); if (doc == null) { return(null); } int eventCount = doc.Root.Elements().Count(); //int[] commonness = new int[eventCount]; float[] eventProbability = new float[eventCount]; float probabilitySum = 0.0f; List <XElement> matchingElements = new List <XElement>(); if (missionType == "random") { matchingElements = doc.Root.Elements().ToList(); } else if (missionType == "none") { return(null); } else if (string.IsNullOrWhiteSpace(missionType)) { matchingElements = doc.Root.Elements().ToList(); } else { matchingElements = doc.Root.Elements().ToList().FindAll(m => m.Name.ToString().ToLowerInvariant().Replace("mission", "") == missionType); } if (isSinglePlayer) { matchingElements.RemoveAll(m => ToolBox.GetAttributeBool(m, "multiplayeronly", false)); } else { matchingElements.RemoveAll(m => ToolBox.GetAttributeBool(m, "singleplayeronly", false)); } int i = 0; foreach (XElement element in matchingElements) { eventProbability[i] = ToolBox.GetAttributeInt(element, "commonness", 1); probabilitySum += eventProbability[i]; i++; } float randomNumber = (float)rand.NextDouble() * probabilitySum; i = 0; foreach (XElement element in matchingElements) { if (randomNumber <= eventProbability[i]) { Type t; string type = element.Name.ToString(); try { t = Type.GetType("Barotrauma." + type, true, true); if (t == null) { DebugConsole.ThrowError("Error in " + configFile + "! Could not find a mission class of the type \"" + type + "\"."); continue; } } catch { DebugConsole.ThrowError("Error in " + configFile + "! Could not find a mission class of the type \"" + type + "\"."); continue; } ConstructorInfo constructor = t.GetConstructor(new[] { typeof(XElement), typeof(Location[]) }); object instance = constructor.Invoke(new object[] { element, locations }); Mission mission = (Mission)instance; return(mission); } randomNumber -= eventProbability[i]; i++; } return(null); }
/// <summary> /// Instantiates level data using the properties of the location /// </summary> public LevelData(Location location) { Seed = location.BaseName; Biome = location.Biome; Type = LevelType.Outpost; GenerationParams = LevelGenerationParams.GetRandom(Seed, LevelType.Outpost, Biome); Difficulty = 0.0f; var rand = new MTRandom(ToolBox.StringToInt(Seed)); int width = (int)MathHelper.Lerp(GenerationParams.MinWidth, GenerationParams.MaxWidth, (float)rand.NextDouble()); InitialDepth = (int)MathHelper.Lerp(GenerationParams.InitialDepthMin, GenerationParams.InitialDepthMax, (float)rand.NextDouble()); Size = new Point( (int)MathUtils.Round(width, Level.GridCellSize), (int)MathUtils.Round(GenerationParams.Height, Level.GridCellSize)); }