public static Mission LoadRandom(Location[] locations, MTRandom rand, bool requireCorrectLocationType, MissionType missionType, bool isSinglePlayer = false) { List <MissionPrefab> allowedMissions = new List <MissionPrefab>(); if (missionType == MissionType.Random) { allowedMissions.AddRange(MissionPrefab.List); } else if (missionType == MissionType.None) { return(null); } else { allowedMissions = MissionPrefab.List.FindAll(m => m.type == missionType); } allowedMissions.RemoveAll(m => isSinglePlayer ? m.MultiplayerOnly : m.SingleplayerOnly); if (requireCorrectLocationType) { allowedMissions.RemoveAll(m => !m.IsAllowed(locations[0], locations[1])); } if (allowedMissions.Count == 0) { return(null); } int probabilitySum = allowedMissions.Sum(m => m.Commonness); int randomNumber = rand.NextInt32() % probabilitySum; foreach (MissionPrefab missionPrefab in allowedMissions) { if (randomNumber <= missionPrefab.Commonness) { return(missionPrefab.Instantiate(locations)); } randomNumber -= missionPrefab.Commonness; } return(null); }
private void CreateEvents() { //don't create new events if docked to the start oupost if (Level.Loaded?.StartOutpost != null && Submarine.MainSub.DockedTo.Contains(Level.Loaded.StartOutpost)) { return; } for (int i = selectedEventSets.Count - 1; i >= 0; i--) { ScriptedEventSet eventSet = selectedEventSets[i]; float distFromStart = Vector2.Distance(Submarine.MainSub.WorldPosition, level.StartPosition); float distFromEnd = Vector2.Distance(Submarine.MainSub.WorldPosition, level.EndPosition); float distanceTraveled = MathHelper.Clamp( (Submarine.MainSub.WorldPosition.X - level.StartPosition.X) / (level.EndPosition.X - level.StartPosition.X), 0.0f, 1.0f); //don't create new events if within 50 meters of the start/end of the level if (distanceTraveled <= 0.0f || distFromStart * Physics.DisplayToRealWorldRatio < 50.0f || distFromEnd * Physics.DisplayToRealWorldRatio < 50.0f) { continue; } if ((Submarine.MainSub == null || distanceTraveled < eventSet.MinDistanceTraveled) && roundDuration < eventSet.MinMissionTime) { continue; } if (CurrentIntensity < eventSet.MinIntensity || CurrentIntensity > eventSet.MaxIntensity) { continue; } selectedEventSets.RemoveAt(i); if (eventSet.ChooseRandom) { if (eventSet.EventPrefabs.Count > 0) { MTRandom rand = new MTRandom(ToolBox.StringToInt(level.Seed)); var newEvent = eventSet.EventPrefabs[rand.NextInt32() % eventSet.EventPrefabs.Count].CreateInstance(); newEvent.Init(true); DebugConsole.Log("Initialized event " + newEvent.ToString()); events.Add(newEvent); } if (eventSet.ChildSets.Count > 0) { MTRandom rand = new MTRandom(ToolBox.StringToInt(level.Seed)); var newEventSet = SelectRandomEvents(eventSet.ChildSets); if (newEventSet != null) { selectedEventSets.Add(newEventSet); } } } else { foreach (ScriptedEventPrefab eventPrefab in eventSet.EventPrefabs) { var newEvent = eventPrefab.CreateInstance(); newEvent.Init(true); DebugConsole.Log("Initialized event " + newEvent.ToString()); events.Add(newEvent); } selectedEventSets.AddRange(eventSet.ChildSets); } } }