public static Job Random(Rand.RandSync randSync = Rand.RandSync.Unsynced)
        {
            var prefab  = JobPrefab.Random(randSync);
            var variant = Rand.Range(0, prefab.Variants, randSync);

            return(new Job(prefab, variant));
        }
Exemplo n.º 2
0
        public static string GetRandomLine(string filePath, Rand.RandSync randSync = Rand.RandSync.Server)
        {
            List <string> lines;

            if (cachedLines.ContainsKey(filePath))
            {
                lines = cachedLines[filePath];
            }
            else
            {
                try
                {
                    lines = File.ReadAllLines(filePath).ToList();
                    cachedLines.Add(filePath, lines);
                    if (lines.Count == 0)
                    {
                        DebugConsole.ThrowError("File \"" + filePath + "\" is empty!");
                        return("");
                    }
                }
                catch (Exception e)
                {
                    DebugConsole.ThrowError("Couldn't open file \"" + filePath + "\"!", e);
                    return("");
                }
            }

            if (lines.Count == 0)
            {
                return("");
            }
            return(lines[Rand.Range(0, lines.Count, randSync)]);
        }
 public IEnumerable <HumanPrefab> GetHumanPrefabs(Rand.RandSync randSync)
 {
     if (humanPrefabLists == null || !humanPrefabLists.Any())
     {
         return(Enumerable.Empty <HumanPrefab>());
     }
     return(humanPrefabLists.GetRandom(randSync));
 }
Exemplo n.º 4
0
        /// <summary>
        /// Randomizes the list in place without creating a new collection, using a Fisher-Yates-based algorithm.
        /// </summary>
        public static void Shuffle <T>(this IList <T> list, Rand.RandSync randSync = Rand.RandSync.Unsynced)
        {
            int n = list.Count;

            while (n > 1)
            {
                n--;
                int k     = Rand.Int(n + 1, randSync);
                T   value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }
Exemplo n.º 5
0
        public static RuinGenerationParams GetRandom(Rand.RandSync randSync = Rand.RandSync.Server)
        {
            if (paramsList == null)
            {
                LoadAll();
            }

            if (paramsList.Count == 0)
            {
                DebugConsole.ThrowError("No ruin configuration files found in any content package.");
                return(new RuinGenerationParams(null, null));
            }

            return(paramsList[Rand.Int(paramsList.Count, randSync)]);
        }
Exemplo n.º 6
0
        public static T GetRandom <T>(this IEnumerable <T> source, Rand.RandSync randSync = Rand.RandSync.Unsynced)
        {
            if (source is IList <T> list)
            {
                int count = list.Count;

                return(count == 0 ? default : list[Rand.Range(0, count, randSync)]);
            }
            else
            {
                int count = source.Count();

                return(count == 0 ? default : source.ElementAt(Rand.Range(0, count, randSync)));
            }
        }
Exemplo n.º 7
0
        public List <RuinEntityConfig> GetPropList(RuinShape room, Rand.RandSync randSync)
        {
            Dictionary <int, List <RuinEntityConfig> > propGroups = new Dictionary <int, List <RuinEntityConfig> >();

            foreach (RuinEntityConfig entityConfig in entityList)
            {
                if (entityConfig.Type != RuinEntityType.Prop)
                {
                    continue;
                }
                if (room.Rect.Width < entityConfig.MinRoomSize.X || room.Rect.Height < entityConfig.MinRoomSize.Y)
                {
                    continue;
                }
                if (room.Rect.Width > entityConfig.MaxRoomSize.X || room.Rect.Height > entityConfig.MaxRoomSize.Y)
                {
                    continue;
                }
                if (!propGroups.ContainsKey(entityConfig.SingleGroupIndex))
                {
                    propGroups[entityConfig.SingleGroupIndex] = new List <RuinEntityConfig>();
                }
                propGroups[entityConfig.SingleGroupIndex].Add(entityConfig);
            }

            List <RuinEntityConfig> props = new List <RuinEntityConfig>();

            foreach (KeyValuePair <int, List <RuinEntityConfig> > propGroup in propGroups)
            {
                if (propGroup.Key == 0)
                {
                    props.AddRange(propGroup.Value);
                }
                else
                {
                    props.Add(propGroup.Value[Rand.Int(propGroup.Value.Count, randSync)]);
                }
            }
            return(props);
        }
 public static JobPrefab Random(Rand.RandSync sync = Rand.RandSync.Unsynced) => Prefabs.GetRandom(p => p.Identifier != "watchman", sync);
Exemplo n.º 9
0
 /// <summary>
 /// Randomizes the collection (using OrderBy) and returns it.
 /// </summary>
 public static IOrderedEnumerable <T> Randomize <T>(this IEnumerable <T> source, Rand.RandSync randSync = Rand.RandSync.Unsynced)
 {
     return(source.OrderBy(i => Rand.Value(randSync)));
 }
Exemplo n.º 10
0
        public static T RandomElementByWeight <T>(this IEnumerable <T> source, Func <T, float> weightSelector, Rand.RandSync randSync = Rand.RandSync.Unsynced)
        {
            float totalWeight = source.Sum(weightSelector);

            float itemWeightIndex    = Rand.Range(0f, 1f, randSync) * totalWeight;
            float currentWeightIndex = 0;

            foreach (T weightedItem in source)
            {
                float weight = weightSelector(weightedItem);
                currentWeightIndex += weight;

                if (currentWeightIndex >= itemWeightIndex)
                {
                    return(weightedItem);
                }
            }

            return(default);
Exemplo n.º 11
0
 public static CaveGenerationParams GetRandom(LevelGenerationParams generationParams, bool abyss, Rand.RandSync rand)
 {
     if (CaveParams.All(p => p.GetCommonness(generationParams, abyss) <= 0.0f))
     {
         return(CaveParams.First());
     }
     return(ToolBox.SelectWeightedRandom(CaveParams, CaveParams.Select(p => p.GetCommonness(generationParams, abyss)).ToList(), rand));
 }
Exemplo n.º 12
0
 public static T SelectWeightedRandom <T>(IList <T> objects, IList <float> weights, Rand.RandSync randSync)
 {
     return(SelectWeightedRandom(objects, weights, Rand.GetRNG(randSync)));
 }
Exemplo n.º 13
0
 public static CharacterInfo CreateCharacterInfo(string speciesName, string name = "", JobPrefab jobPrefab = null, string ragdollFileName = null, int variant = 0, Rand.RandSync randSync = Rand.RandSync.Unsynced)
 {
     return(new CharacterInfo(speciesName, name, name, jobPrefab, ragdollFileName, variant, randSync));
 }
Exemplo n.º 14
0
        protected Character CreateHuman(HumanPrefab humanPrefab, List <Character> characters, Dictionary <Character, List <Item> > characterItems, Submarine submarine, CharacterTeamType teamType, ISpatialEntity positionToStayIn = null, Rand.RandSync humanPrefabRandSync = Rand.RandSync.Server, bool giveTags = true)
        {
            var characterInfo = humanPrefab.GetCharacterInfo(Rand.RandSync.Server) ?? new CharacterInfo(CharacterPrefab.HumanSpeciesName, npcIdentifier: humanPrefab.Identifier, jobPrefab: humanPrefab.GetJobPrefab(humanPrefabRandSync), randSync: humanPrefabRandSync);

            characterInfo.TeamID = teamType;

            if (positionToStayIn == null)
            {
                positionToStayIn =
                    WayPoint.GetRandom(SpawnType.Human, characterInfo.Job?.Prefab, submarine) ??
                    WayPoint.GetRandom(SpawnType.Human, null, submarine);
            }

            Character spawnedCharacter = Character.Create(characterInfo.SpeciesName, positionToStayIn.WorldPosition, ToolBox.RandomSeed(8), characterInfo, createNetworkEvent: false);

            spawnedCharacter.Prefab = humanPrefab;
            humanPrefab.InitializeCharacter(spawnedCharacter, positionToStayIn);
            humanPrefab.GiveItems(spawnedCharacter, submarine, Rand.RandSync.Server, createNetworkEvents: false);

            characters.Add(spawnedCharacter);
            characterItems.Add(spawnedCharacter, spawnedCharacter.Inventory.FindAllItems(recursive: true));

            return(spawnedCharacter);
        }
Exemplo n.º 15
0
        private void InitEscort()
        {
            characters.Clear();
            characterItems.Clear();

            WayPoint explicitStayInHullPos = WayPoint.GetRandom(SpawnType.Human, null, Submarine.MainSub);

            Rand.RandSync randSync = Rand.RandSync.Server;

            if (terroristChance > 0f)
            {
                // in terrorist missions, reroll characters each retry to avoid confusion as to who the terrorists are
                randSync = Rand.RandSync.Unsynced;
            }

            //if any of the escortees have a job defined, try to use a spawnpoint designated for that job
            foreach (XElement element in characterConfig.Elements())
            {
                var humanPrefab = GetHumanPrefabFromElement(element);
                if (humanPrefab == null || string.IsNullOrEmpty(humanPrefab.Job) || humanPrefab.Job.Equals("any", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                var jobPrefab = humanPrefab.GetJobPrefab();
                if (jobPrefab != null)
                {
                    var jobSpecificSpawnPos = WayPoint.GetRandom(SpawnType.Human, jobPrefab, Submarine.MainSub);
                    if (jobSpecificSpawnPos != null)
                    {
                        explicitStayInHullPos = jobSpecificSpawnPos;
                        break;
                    }
                }
            }

            foreach (XElement element in characterConfig.Elements())
            {
                int count = CalculateScalingEscortedCharacterCount(inMission: true);
                for (int i = 0; i < count; i++)
                {
                    Character spawnedCharacter = CreateHuman(GetHumanPrefabFromElement(element), characters, characterItems, Submarine.MainSub, CharacterTeamType.FriendlyNPC, explicitStayInHullPos, humanPrefabRandSync: randSync);
                    if (spawnedCharacter.AIController is HumanAIController humanAI)
                    {
                        humanAI.InitMentalStateManager();
                    }
                }
            }

            if (terroristChance > 0f)
            {
                int terroristCount = (int)Math.Ceiling(terroristChance * Rand.Range(0.8f, 1.2f) * characters.Count);
                terroristCount = Math.Clamp(terroristCount, 1, characters.Count);

                terroristCharacters.Clear();
                characters.GetRange(0, terroristCount).ForEach(c => terroristCharacters.Add(c));

                terroristDistanceSquared = Vector2.DistanceSquared(Level.Loaded.StartPosition, Level.Loaded.EndPosition) * Rand.Range(0.35f, 0.65f);

#if DEBUG
                DebugConsole.AddWarning("Terrorists will trigger at range  " + Math.Sqrt(terroristDistanceSquared));
                foreach (Character character in terroristCharacters)
                {
                    DebugConsole.AddWarning(character.Name + " is a terrorist.");
                }
#endif
            }
        }
Exemplo n.º 16
0
 public static T GetRandom <T>(this IEnumerable <T> source, Func <T, bool> predicate, Rand.RandSync randSync = Rand.RandSync.Unsynced)
 {
     return(source.Where(predicate).GetRandom(randSync));
 }
Exemplo n.º 17
0
        public static Job Random(Rand.RandSync randSync)
        {
            JobPrefab prefab = JobPrefab.List[Rand.Int(JobPrefab.List.Count - 1, randSync)];

            return(new Job(prefab));
        }
Exemplo n.º 18
0
 public static JobPrefab Random(Rand.RandSync sync = Rand.RandSync.Unsynced) => Prefabs.GetRandom(p => !p.HiddenJob, sync);
Exemplo n.º 19
0
 public static JobPrefab Random(Rand.RandSync sync = Rand.RandSync.Unsynced) => List.Values.GetRandom(sync);
Exemplo n.º 20
0
 public static CorpsePrefab Random(Rand.RandSync sync = Rand.RandSync.Unsynced) => Prefabs.GetRandom(sync);
Exemplo n.º 21
0
        public static T GetRandom <T>(this IEnumerable <T> source, Rand.RandSync randSync = Rand.RandSync.Unsynced)
        {
            int count = source.Count();

            return(count == 0 ? default(T) : source.ElementAt(Rand.Range(0, count, randSync)));
        }
Exemplo n.º 22
0
 public static Job Random(Rand.RandSync randSync = Rand.RandSync.Unsynced) => new Job(JobPrefab.Random(randSync));