示例#1
0
        public void Simulate(List <Cell> cells)
        {
            if (RandomTools.Chance(0.0001))
            {
                var startPoint = RandomTools.NextElement(cells);

                _hotSpots.Add(new HotSpot(new List <CellInHotSpot> {
                    new CellInHotSpot(1.0, startPoint)
                }));
            }

            foreach (var hotspot in _hotSpots.ToList())
            {
                hotspot.Age++;
                if (hotspot.Age >= RandomTools.NextGaussian(1000, 50))
                {
                    _hotSpots.Remove(hotspot);
                }

                foreach (var cell in hotspot.Cells)
                {
                    cell.Cell.MantleLayer.Heat += RandomTools.NextInt(900, 1100);
                }
            }

            foreach (var cell in cells)
            {
                foreach (var neighbor in cell.Neighbors)
                {
                    var heatTransferred = 0.4 * (cell.MantleLayer.Heat - neighbor.MantleLayer.Heat);
                    cell.MantleLayer.Heat     -= heatTransferred;
                    neighbor.MantleLayer.Heat += heatTransferred;
                }
            }
        }
        public void CanReadAndWriteString()
        {
            const int SIZE = 100;
            // The first string is null, because we need to be able to support this concept.
            List <string> testStrings = new List <string>()
            {
                null
            };

            for (int i = 0; i < MAX_TEST; i++)
            {
                string test = RandomTools.GetAlphaString(SIZE);
                testStrings.Add(test);
            }
            foreach (var test in testStrings)
            {
                using (var ms = new MemoryStream())
                {
                    EZWriter.Write(ms, test);
                    ms.Seek(0, SeekOrigin.Begin);

                    string comp = EZReader.ReadString(ms);

                    Assert.AreEqual(test, comp, "The strings should be the same!");
                }
            }
        }
        public void CanReadAndWriteStringWithNullChar()
        {
            const int STR_LEN = 32;

            int    rnd = RandomTools.RNG.Next(STR_LEN);
            string p1  = RandomTools.GetAlphaString(rnd);

            rnd = RandomTools.RNG.Next(STR_LEN);
            string p2 = RandomTools.GetAlphaString(rnd);

            string test = p1 + (char)0 + p2;


            using (MemoryStream ms = new MemoryStream())
            {
                EZWriter.Write(ms, test);

                ms.Seek(0, SeekOrigin.Begin);


                string comp = EZReader.ReadString(ms);

                Assert.AreEqual(test, comp, "Strings don't match!");
            }
        }
        public void CanReadAndWriteBools()
        {
            const int MAX = 5;

            bool[] myBoools = new bool[MAX];

            for (int i = 0; i < MAX; i++)
            {
                myBoools[i] = RandomTools.FiftyFifty();
            }

            // Write the data, then read it back in.
            using (MemoryStream ms = new MemoryStream())
            {
                for (int i = 0; i < MAX; i++)
                {
                    EZWriter.Write(ms, myBoools[i]);
                }

                ms.Seek(0, SeekOrigin.Begin);

                for (int i = 0; i < MAX; i++)
                {
                    bool nextBool = EZReader.ReadBool(ms);
                    Assert.AreEqual(myBoools[i], nextBool, "Invalid boolean at index #{0}", i);
                }
            }
        }
示例#5
0
        /// <summary>
        /// Method choice random dimension(proportional to len) and random value for split
        /// </summary>
        void CreateSplitData()
        {
            // Get sum of all dimension and choice random number
            // All dimensions going one after one
            decimal splitValue = RandomTools.RandomDecimal(0, boxLen.Sum());

            decimal sum = 0;
            short   i   = 0;

            // Figure out in that dimension is splitValue(after cicle it will be i-1)
            do
            {
                sum += boxLen[i];
                i++;
            } while (sum < splitValue);

            // i==1 mean that splitValue in 0 dimension, simple calculation
            if (i == 1)
            {
                splitValue     = boxMin[0] + splitValue;
                SplitDimension = 0;
            }
            // i > 1, to get split value in current dimension
            // need cut off all previos dimensions(thay going one after one) from sum
            // and add to start position(min boundiong box)
            else
            {
                sum           -= boxLen[i - 1];
                splitValue     = boxMin[i - 1] + (splitValue - sum);
                SplitDimension = (byte)(i - 1);
            }
            SplitValue = splitValue;
        }
示例#6
0
        private IssueDto MapToIssueDto(IssueViewModel viewModel)
        {
            var orderNo = RandomTools.GetUniqueKey(10);
            var dto     = new IssueDto
            {
                Type   = viewModel.Type,
                Mode   = viewModel.Mode,
                Client = viewModel.Client,

                OrderNo      = orderNo,
                Language     = viewModel.Language,
                PayableCoins = viewModel.PayableCoins,

                CallbackUrl = GetSecureCallbackUrl(orderNo),
                WebhookUrl  = GetSecureWebhookUrl(orderNo),

                AllowReject   = viewModel.AllowReject,
                AllowTestNets = viewModel.AllowTestNets
            };

            if (viewModel.Type == Constants.PaymentType.Restricted)
            {
                dto.BaseAmount     = viewModel.BaseAmount;
                dto.BaseCurrencyId = viewModel.BaseCurrencyId;
            }

            if (dto.Language?.ToLower() == "auto")
            {
                dto.Language = null;
            }

            return(dto);
        }
示例#7
0
 public static int GetNextInt(int Min = 1, int Max = 20)
 {
     if (Min < 0)
     {
         throw new Exception("Min Must be > 0");
     }
     return(RandomTools.Next(Min, Max));
 }
 void ReGenerate()
 {
     RandomTools.ArrrayDurstenfeldShuffle(shuffle);
     for (int i = 0; i < shuffle.Length; i++)
     {
         int index = shuffle[i];
         int position = GetPosition(index);
         randomItems[i].UpdateText(randomString[index], position);
     }
 }
示例#9
0
    public void Activate(float power)
    {
        float count = Mathf.Lerp(minCount, maxCount, power);

        for (int i = 0; i < count; ++i)
        {
            float scale = Mathf.Lerp(minScale, maxScale, RandomTools.ClampedGaussian(power, power));
            Instantiate(entity, transform.position, transform.rotation, transform).transform.localScale = new Vector3(scale, scale, scale);
        }
    }
示例#10
0
    void RandomGUI()
    {
        GUILayout.Label("Randomize", EditorStyles.boldLabel);
        GUILayout.BeginVertical("box");

        EditorGUIUtility.labelWidth = 30;
        EditorGUIUtility.fieldWidth = 50;

        GUILayout.Label("Axis", EditorStyles.boldLabel);
        randomAxis = GUILayout.SelectionGrid(randomAxis, axisOption, axisOption.Length, "toggle");

        EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);

        GUILayout.Label("Local Position", EditorStyles.boldLabel);
        GUILayout.BeginHorizontal();
        transMin = EditorGUILayout.FloatField("Min", transMin);
        transMax = EditorGUILayout.FloatField("Max", transMax);
        if (GUILayout.Button("Position", GUILayout.MaxWidth(75)))
        {
            RandomTools.RandomPosition(randomAxis, transMin, transMax);
        }
        GUILayout.EndHorizontal();

        GUILayout.Label("Rotation", EditorStyles.boldLabel);
        GUILayout.BeginHorizontal();
        rotMin = EditorGUILayout.FloatField("Min", rotMin);
        rotMax = EditorGUILayout.FloatField("Max", rotMax);
        if (GUILayout.Button("Rotate", GUILayout.MaxWidth(75)))
        {
            RandomTools.RandomRotation(randomAxis, rotMin, rotMax);
        }
        GUILayout.EndHorizontal();

        GUILayout.Label("Local Scale", EditorStyles.boldLabel);
        GUILayout.BeginHorizontal();
        scaleMin = EditorGUILayout.FloatField("Min", scaleMin);
        scaleMax = EditorGUILayout.FloatField("Max", scaleMax);
        if (GUILayout.Button("Scale", GUILayout.MaxWidth(75)))
        {
            RandomTools.RandomScale(randomAxis, scaleMin, scaleMax);
        }

        GUILayout.EndHorizontal();

        GUILayout.EndVertical();

        EditorPrefs.SetInt(randomAxisKey, randomAxis);
        EditorPrefs.SetFloat(transMinKey, transMin);
        EditorPrefs.SetFloat(transMaxKey, transMax);
        EditorPrefs.SetFloat(scaleMinKey, scaleMin);
        EditorPrefs.SetFloat(scaleMaxKey, scaleMax);
    }
示例#11
0
        public void UpdateAgingAndRandomization()
        {
            double res = AggedScore();

            //probability reduces by reducingPerPointFactor for every res point
            var p = 100 / Math.Pow(ReducingPerPointFactor, res);

            //Randomize
            var rndFactor = Math.Pow(1.5, RandomTools.RandomNormal(0, 1));

            p = p * rndFactor;
            AggregateScore = p;
        }
        public void CanCompareStrings()
        {
            var testStrings = new List <string>()
            {
                "abc",
                "Hamster",
                "Hamster_Longer",
                "Apples & Oranges",
                "Apples & Carrots",
            };

            // Let's add some random ones....
            const int RND_COUNT = 50;

            for (int i = 0; i < RND_COUNT; i++)
            {
                string toAdd  = RandomTools.GetAlphaNumericString(10);
                string toAdd2 = RandomTools.RandomizeCase(toAdd);

                testStrings.Add(toAdd);
                testStrings.Add(toAdd2);
            }

            // We need to permute these....
            var permuted = new List <Tuple <string, string> >();
            int len      = testStrings.Count;

            for (int i = 0; i < len; i++)
            {
                for (int j = i + 1; j < len; j++)
                {
                    permuted.Add(new Tuple <string, string>(testStrings[i], testStrings[j]));
                }
            }

            //var sorted = new List<string>(testStrings);
            //sorted.Sort(
            int pLen = permuted.Count;

            for (int i = 0; i < pLen; i++)
            {
                string l = permuted[i].Item1;
                string r = permuted[i].Item2;

                int legacy = string.Compare(l, r, true); //  l.CompareTo(r);
                int comp   = StringTools.AlphaNumericStrCompare_NoCase(l, r);

                Assert.AreEqual(legacy, comp, "Non-matching comparison at index: {0} ({1} : {2})", i, l, r);
            }
        }
示例#13
0
        public void Test_RandomInt_CheckInterval()
        {
            var a1 = RandomTools.RandomInt(0, 1);
            var a2 = RandomTools.RandomInt(0, 9000);
            var a3 = RandomTools.RandomInt(-10, 10);

            var rez1 = (a1 == 0);
            var rez2 = ((a2 >= 0) && (a2 < 9000));
            var rez3 = ((a3 >= -10) && (a3 < 10));

            Assert.IsTrue(rez1);
            Assert.IsTrue(rez2);
            Assert.IsTrue(rez3);
        }
示例#14
0
        /// <summary>
        /// Method that make reservoir sampling.
        /// </summary>
        /// <param name="number">Number of current element</param>
        /// <returns></returns>
        int Vitter(int number)
        {
            if (number < MaxPointsInTree)
            {
                return(number);
            }
            var r = RandomTools.RandomInt(0, number);

            if (r < MaxPointsInTree)
            {
                return(r);
            }
            else
            {
                return(-1);
            }
        }
示例#15
0
        public void Test_RandomDecimal_CheckInterval()
        {
            var a1 = RandomTools.RandomDecimal(0, 0.0000003M);
            var a2 = RandomTools.RandomDecimal(0, 1.0000003M);
            var a3 = RandomTools.RandomDecimal(10.1M, 5250.023M);
            var a4 = RandomTools.RandomDecimal(-10.2M, 9.03M);

            var rez1 = ((a1 >= 0) && (a1 < 0.000003M));
            var rez2 = ((a2 >= 0) && (a2 < 1.0000003M));
            var rez3 = ((a3 >= 10.1M) && (a3 < 5250.023M));
            var rez4 = ((a4 >= -10.2M) && (a4 < 9.03M));

            Assert.IsTrue(rez1);
            Assert.IsTrue(rez2);
            Assert.IsTrue(rez3);
            Assert.IsTrue(rez4);
        }
示例#16
0
    private void CreateBox(Pos[] pts)
    {
        var pos = RandomTools.RandomElement(pts);
        var num = RandomTools.RandomElement(gameConfig.NumberRange);
        var box = new NumberBox(pos.X, pos.Y, num);

        var newViewBox = Instantiate(boxPrefab).GetComponent <NumberController>();

        newViewBox.SetPos(pos.X, pos.Y).SetNumberText(num);

        boxes     = box.Cons(boxes).ToArray();
        viewBoxes = newViewBox.Cons(viewBoxes).ToArray();

        PlusScore(box);
        SetProgressActive(true);
        ResetTimer();
    }
示例#17
0
    /// <summary>
    /// 增加建筑
    /// </summary>
    /// <param name="addRate"></param>
    /// <param name="randomData"></param>
    /// <param name="startPosition"></param>
    /// <param name="buildingType"></param>
    public static bool AddBuilding(float addRate, uint randomData, Vector3Int startPosition, BuildingTypeEnum buildingType)
    {
        float randomRate;

        if (addRate < 0.00001f)
        {
            //概率小于万分之一的用RandomTools
            int         seed        = WorldCreateHandler.Instance.manager.GetWorldSeed();
            RandomTools randomTools = RandomUtil.GetRandom(seed, startPosition.x, startPosition.y, startPosition.z);
            //生成概率
            randomRate = randomTools.NextFloat();
        }
        else
        {
            randomRate = WorldRandTools.GetValue(startPosition, randomData);
        }
        if (randomRate < addRate)
        {
            BuildingInfoBean buildingInfo = BiomeHandler.Instance.manager.GetBuildingInfo(buildingType);

            List <BuildingBean> listBuildingData = buildingInfo.listBuildingData;

            int randomAngle = WorldRandTools.Range(0, 4, startPosition) * 90;

            for (int i = 0; i < listBuildingData.Count; i++)
            {
                BuildingBean buildingData   = listBuildingData[i];
                Vector3Int   targetPosition = startPosition + buildingData.GetPosition();
                float        createRate     = WorldRandTools.GetValue(targetPosition);

                if (buildingData.randomRate == 0 || createRate < buildingData.randomRate)
                {
                    VectorUtil.GetRotatedPosition(startPosition, targetPosition, new Vector3(0, randomAngle, 0));
                    WorldCreateHandler.Instance.manager.AddUpdateBlock(targetPosition, buildingData.blockId, (BlockDirectionEnum)buildingData.direction);
                }
            }
            return(true);
        }
        return(false);
    }
        public void CanUseGenericEZReaderMethod()
        {
            // A more difficult one...
            const int MAX = 10;

            Int32[] data = new Int32[MAX];
            for (int i = 0; i < MAX; i++)
            {
                data[i] = i;
            }

            MemoryStream m = new MemoryStream();

            EZWriter.Write(m, data);

            m.Seek(0, SeekOrigin.Begin);

            Int32[] comp = EZReader.Read <Int32[]>(m);

            ObjectInspector oi     = new ObjectInspector();
            var             result = oi.CompareArrays <int>(data, comp);

            // Make sure that it matches!
            Assert.IsTrue(result.Item1, result.Item2);


            // Let's do a string to make sure!
            string testString = RandomTools.GetRandomCharString(MAX);

            m = new MemoryStream();
            EZWriter.Write(m, testString);

            m.Seek(0, SeekOrigin.Begin);

            string compString = EZReader.Read <string>(m);

            Assert.AreEqual(testString, compString);
        }
    /// <summary>
    /// 增加世界之树
    /// </summary>
    /// <param name="startPosition"></param>
    /// <param name="treeData"></param>
    public static void AddTreeForWorld(Vector3Int startPosition, BiomeForTreeData treeData)
    {
        //概率小于万分之一的用RandomTools
        int         seed        = WorldCreateHandler.Instance.manager.GetWorldSeed();
        RandomTools randomTools = RandomUtil.GetRandom(seed, startPosition.x, startPosition.y, startPosition.z);
        //生成概率
        float addRate = randomTools.NextFloat();
        //高度
        int treeHeight = WorldRandTools.Range(treeData.minHeight, treeData.maxHeight);

        if (addRate < treeData.addRate)
        {
            for (int y = 0; y < treeHeight; y++)
            {
                Vector3Int treeTrunkPosition = startPosition + Vector3Int.up * (y + 1);
                int        trunkRange        = treeData.trunkRange;
                for (int x = -trunkRange; x <= trunkRange; x++)
                {
                    for (int z = -trunkRange; z <= trunkRange; z++)
                    {
                        if ((x == -trunkRange || x == trunkRange) && (z == -trunkRange || z == trunkRange))
                        {
                            continue;
                        }
                        else if ((x == -trunkRange + 1 || x == trunkRange - 1) && (z == -trunkRange || z == trunkRange))
                        {
                            continue;
                        }
                        else if ((z == -trunkRange + 1 || z == trunkRange - 1) && (x == -trunkRange || x == trunkRange))
                        {
                            continue;
                        }
                        else
                        {
                            Vector3Int tempTrunkPosition = new(treeTrunkPosition.x + x, treeTrunkPosition.y, treeTrunkPosition.z + z);
                            //生成树干
                            WorldCreateHandler.Instance.manager.AddUpdateBlock(tempTrunkPosition, treeData.treeTrunk);
                        }

                        if ((x == -trunkRange || x == trunkRange || z == -trunkRange || z == trunkRange) ||
                            ((x == -trunkRange + 1 || x == trunkRange - 1) && (z == -trunkRange || z == trunkRange)) ||
                            ((z == -trunkRange + 1 || z == trunkRange - 1) && (x == -trunkRange || x == trunkRange)) ||
                            ((x == -trunkRange + 2 || x == trunkRange - 2) && (z == -trunkRange || z == trunkRange)) ||
                            ((z == -trunkRange + 2 || z == trunkRange - 2) && (x == -trunkRange || x == trunkRange)))
                        {
                            //树盘
                            if (y == 0 || y == 1)
                            {
                                Vector3Int baseStartPosition = treeTrunkPosition + new Vector3Int(x, -1, z);
                                int        baseWith          = WorldRandTools.Range(3, 6);
                                for (int b = 0; b < baseWith; b++)
                                {
                                    BlockDirectionEnum baseDirection =
                                        (x == -trunkRange ? BlockDirectionEnum.LeftForward
                                        : x == trunkRange ? BlockDirectionEnum.RightForward
                                        : z == -trunkRange ? BlockDirectionEnum.ForwardForward
                                        : z == trunkRange ? BlockDirectionEnum.BackForward
                                        : BlockDirectionEnum.LeftForward);

                                    int branchDirectionX = WorldRandTools.Range(0, 2);
                                    int branchDirectionY = WorldRandTools.Range(0, 2);
                                    int branchDirectionZ = WorldRandTools.Range(0, 2);

                                    int addPositionX;
                                    if (x == -trunkRange || x == trunkRange)
                                    {
                                        addPositionX = x > 0 ? 1 : -1;
                                    }
                                    else if (x == -trunkRange + 1 || x == trunkRange - 1 || x == -trunkRange + 2 || x == trunkRange - 2)
                                    {
                                        int tempRandom = WorldRandTools.Range(0, 2);
                                        if (tempRandom == 0)
                                        {
                                            addPositionX = x > 0 ? 1 : -1;
                                        }
                                        else
                                        {
                                            addPositionX = (branchDirectionX == 0 ? -1 : 1);
                                        }
                                    }
                                    else
                                    {
                                        addPositionX = (branchDirectionX == 0 ? -1 : 1);
                                    }

                                    int addPositionZ;
                                    if (z == -trunkRange || z == trunkRange)
                                    {
                                        addPositionZ = z > 0 ? 1 : -1;
                                    }
                                    else if (z == -trunkRange + 1 || z == trunkRange - 1 || z == -trunkRange + 2 || z == trunkRange - 2)
                                    {
                                        int tempRandom = WorldRandTools.Range(0, 2);
                                        if (tempRandom == 0)
                                        {
                                            addPositionZ = z > 0 ? 1 : -1;
                                        }
                                        else
                                        {
                                            addPositionZ = (branchDirectionZ == 0 ? -1 : 1);
                                        }
                                    }
                                    else
                                    {
                                        addPositionZ = (branchDirectionZ == 0 ? -1 : 1);
                                    }

                                    int addPositionY = (branchDirectionY == 0 ? -1 : 0);
                                    if (addPositionY == -1)
                                    {
                                        addPositionX  = 0;
                                        addPositionZ  = 0;
                                        baseDirection = BlockDirectionEnum.UpForward;
                                    }

                                    Vector3Int addPosition = new(addPositionX, addPositionY, addPositionZ);
                                    baseStartPosition += addPosition;
                                    //干
                                    WorldCreateHandler.Instance.manager.AddUpdateBlock(baseStartPosition, treeData.treeTrunk, baseDirection);
                                }
                            }

                            //枝
                            if (y > treeHeight / 2)
                            {
                                int addBranchRate = WorldRandTools.Range(0, 3);
                                if (addBranchRate == 0)
                                {
                                    int        branchWith          = WorldRandTools.Range(0, treeHeight / 2) + treeHeight / 4;
                                    Vector3Int branchStartPosition = treeTrunkPosition + new Vector3Int(x, 0, z);

                                    for (int b = 0; b < branchWith; b++)
                                    {
                                        BlockDirectionEnum branchDirection =
                                            (x == -trunkRange ? BlockDirectionEnum.LeftForward
                                            : x == trunkRange ? BlockDirectionEnum.RightForward
                                            : z == -trunkRange ? BlockDirectionEnum.ForwardForward
                                            : z == trunkRange ? BlockDirectionEnum.BackForward
                                            : BlockDirectionEnum.LeftForward);

                                        int branchDirectionX = WorldRandTools.Range(0, 2);
                                        int branchDirectionY = WorldRandTools.Range(0, 4);
                                        int branchDirectionZ = WorldRandTools.Range(0, 2);

                                        int addPositionX;
                                        if (x == -trunkRange || x == trunkRange)
                                        {
                                            addPositionX = x > 0 ? 1 : -1;
                                        }
                                        else if (x == -trunkRange + 1 || x == trunkRange - 1 || x == -trunkRange + 2 || x == trunkRange - 2)
                                        {
                                            int tempRandom = WorldRandTools.Range(0, 2);
                                            if (tempRandom == 0)
                                            {
                                                addPositionX = x > 0 ? 1 : -1;
                                            }
                                            else
                                            {
                                                addPositionX = (branchDirectionX == 0 ? -1 : 1);
                                            }
                                        }
                                        else
                                        {
                                            addPositionX = (branchDirectionX == 0 ? -1 : 1);
                                        }

                                        int addPositionZ;
                                        if (z == -trunkRange || z == trunkRange)
                                        {
                                            addPositionZ = z > 0 ? 1 : -1;
                                        }
                                        else if (z == -trunkRange + 1 || z == trunkRange - 1 || z == -trunkRange + 2 || z == trunkRange - 2)
                                        {
                                            int tempRandom = WorldRandTools.Range(0, 2);
                                            if (tempRandom == 0)
                                            {
                                                addPositionZ = z > 0 ? 1 : -1;
                                            }
                                            else
                                            {
                                                addPositionZ = (branchDirectionZ == 0 ? -1 : 1);
                                            }
                                        }
                                        else
                                        {
                                            addPositionZ = (branchDirectionZ == 0 ? -1 : 1);
                                        }

                                        int addPositionY = (branchDirectionY == 0 ? 1 : 0);

                                        Vector3Int addPosition = new(addPositionX, addPositionY, addPositionZ);
                                        branchStartPosition += addPosition;
                                        //干
                                        WorldCreateHandler.Instance.manager.AddUpdateBlock(branchStartPosition, treeData.treeTrunk, branchDirection);
                                        //叶
                                        if (b % 4 == 0)
                                        {
                                            int leavesRange = 2;
                                            for (int leavesX = -leavesRange; leavesX <= leavesRange; leavesX++)
                                            {
                                                for (int leavesY = -leavesRange; leavesY <= leavesRange; leavesY++)
                                                {
                                                    for (int leavesZ = -leavesRange; leavesZ <= leavesRange; leavesZ++)
                                                    {
                                                        if (((leavesX == leavesRange || leavesX == -leavesRange) && (leavesY == leavesRange || leavesY == -leavesRange)) ||
                                                            ((leavesY == leavesRange || leavesY == -leavesRange) && (leavesZ == leavesRange || leavesZ == -leavesRange)) ||
                                                            ((leavesX == leavesRange || leavesX == -leavesRange) && (leavesZ == leavesRange || leavesZ == -leavesRange)))
                                                        {
                                                            continue;
                                                        }
                                                        Vector3Int leavesPosition = branchStartPosition + new Vector3Int(leavesX, leavesY, leavesZ);
                                                        WorldCreateHandler.Instance.manager.AddUpdateBlock(leavesPosition, treeData.treeLeaves);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
示例#20
0
    public static RandomTools GetRandom(int seed, int offset1, int offset2, int offset3)
    {
        RandomTools randomTools = new RandomTools((ulong)(seed == 0 ? seed + 1 : seed) * (ulong)(offset1 == 0 ? offset1 + 1 : offset1) * (ulong)(offset2 == 0 ? offset2 + 1 : offset2) * (ulong)(offset3 == 0 ? offset3 + 1 : offset3));

        return(randomTools);
    }
    void GenerateHut(int numberOfResidents)
    {
        TiledArea newTiledArea = null;
        // Strategy: A hut is spawned in a circular radius around the center of the village.
        Vector3 hutPosition = UtilityFunctions.GetRandomVector3(transform.position, VillageData.hutSpawnRange);

        // Ensure no blockage by existing huts
        var dummyObj = new GameObject("Dummy Object");

        dummyObj.transform.position = hutPosition;
        var dummyArea = new TempArea(dummyObj.transform, hutDimensions);

        var collidingArea = FindAreaIfColliding(dummyArea);

        bool snappingOccurred = false;

        while (!(collidingArea is null))
        {
            var collidingTiledArea = collidingArea.TiledArea;
            hutPosition = collidingTiledArea.SnapToClosestOpenSpace(dummyArea);
            dummyObj.transform.position = hutPosition;
            collidingArea = FindAreaIfColliding(dummyArea);
            if (collidingArea is null)
            {
                snappingOccurred = true;
                newTiledArea     = collidingTiledArea;
                break;
            }
            hutPosition = UtilityFunctions.GetRandomVector3(transform.position, VillageData.hutSpawnRange);
            dummyObj.transform.position = hutPosition;
            collidingArea = FindAreaIfColliding(dummyArea);
        }

        // Checked for collision with existing hut

        // Orientation of the hut will be towards village center
        // But, if adding to an existing TiledArea, use that orientation instead

        if (snappingOccurred)
        {
            dummyObj.transform.rotation = newTiledArea.centerTransform.rotation;
        }
        else
        {
            var hutForward = transform.position - hutPosition;
            // Allignment
            dummyObj.transform.forward = hutForward;
        }

        // Instantiation
        HutNode hutNode = CreateResidence <HutNode>(VillageData.hutPrefab, hutPosition, dummyObj.transform.rotation, parentTiledArea: newTiledArea, residenceName: defaultHutName);

        Destroy(dummyObj);

        // TODO: Replace with sophisticated initialization


        // Pick a random subset of unhoused residents.
        var newResidents = RandomTools.RandomSubset(UnhousedVillagers.ToList(), numberOfResidents);

        hutNode.AddResidents(newResidents.Cast <INode>().ToList());
        hutNode.SetUpArea(hutNode.transform, hutDimensions);
        // Snap villagers to near the hut. Also rename them
        foreach (var villager in newResidents)
        {
            // spawn villager close to the hut
            var villagerSpawnDimensions = new Vector2(hutDimensions.x, hutDimensions.z);
            // spawn villager in the 2-D rectangular boundary around the hut, in the xz plane
            var villagerSpawnPosition = UtilityFunctions.GetRandomBoundaryPoint(villagerSpawnDimensions);
            // The rectangular boundary is oriented using transform.right and transform.forward
            villager.transform.position = hutNode.transform.position + hutNode.transform.forward * villagerSpawnPosition.x + hutNode.transform.right * villagerSpawnPosition.y;
            UtilityFunctions.PutObjectOnGround(villager.transform);
            // TODO set up regulated name/gameObject name schemes, so that gameObject name and actual name always match
            villager.gameObject.name = $"{villager.Name}";
        }

        UnhousedVillagers.ExceptWith(newResidents);
    }