Пример #1
0
        /// <summary>Creates a new, AI controlled ship.</summary>
        /// <param name="manager">The manager.</param>
        /// <param name="blueprint">The blueprint.</param>
        /// <param name="faction">The faction the ship will belong to.</param>
        /// <param name="position">The position.</param>
        /// <param name="random">The random.</param>
        /// <param name="configuration">The configuration.</param>
        /// <returns>The new ship.</returns>
        public static int CreateAIShip(
            IManager manager,
            string blueprint,
            Factions faction,
            FarPosition position,
            IUniformRandom random,
            ArtificialIntelligence.AIConfiguration configuration = null)
        {
            var entity = FactoryLibrary.SampleShip(manager, blueprint, faction, position, random);

            var input = (ShipControl)manager.GetComponent(entity, ShipControl.TypeId);

            input.Stabilizing = true;
            manager.AddComponent <ArtificialIntelligence>(entity).
            Initialize(random != null ? random.NextUInt32() : 0, configuration).Enabled = false;

            // Add to the index from which entities will automatically removed
            // on cell death and mark it (for translation checks into empty space).
            manager.AddComponent <Indexable>(entity).Initialize(CellSystem.CellDeathAutoRemoveIndexId);
            manager.AddComponent <CellDeath>(entity).Initialize(true);

            // Add to AI index, to allow putting the AI to sleep.
            manager.AddComponent <Indexable>(entity).Initialize(SleepSystem.IndexId);

            return(entity);
        }
Пример #2
0
        /// <summary>Get the next sample value from the gaussian distribution.</summary>
        public double NextSample()
        {
            for (;;)
            {
                // Select box at random.
                var u    = (byte)_rng.NextUInt32();
                var i    = u & 0x7F;
                var sign = ((u & 0x80) == 0) ? -1.0 : 1.0;

                // Generate uniform random value with range [0,0xFFFFFFFF].
                var u2 = _rng.NextUInt32();

                // Special case for the base segment.
                if (0 == i)
                {
                    if (u2 < _xComp[0])
                    {
                        // Generated x is within R0.
                        return(u2 * UIntToU * _aDivY0 * sign);
                    }
                    // Generated x is in the tail of the distribution.
                    return(SampleTail() * sign);
                }

                // All other segments.
                if (u2 < _xComp[i])
                {
                    // Generated x is within the rectangle.
                    return(u2 * UIntToU * _x[i] * sign);
                }

                // Generated x is outside of the rectangle.
                // Generate a random y coordinate and test if our (x,y) is within the distribution curve.
                // This execution path is relatively slow/expensive (makes a call to Math.Exp()) but relatively rarely executed,
                // although more often than the 'tail' path (above).
                double x = u2 * UIntToU * _x[i];
                if (_y[i - 1] + ((_y[i] - _y[i - 1]) * _rng.NextDouble()) < GaussianPdfDenorm(x))
                {
                    return(x * sign);
                }
            }
        }