public ValueNoise1D(int seed, InterpolationFunction interpolationFunction)
        {
            Random random = new Random(seed);

            for (int i = 0; i < MAX_VERTICES; i++)
            {
                values[i] = (float)random.NextDouble();
            }

            this.interpolationFunction = interpolationFunction;
        }
    public static float Interpolate(float t, InterpolationFunction function)
    {
        float s = 0.0f;

        // choose the right function and map t
        switch (function)
        {
        case InterpolationFunction.Linear:
            s = t;
            break;

        case InterpolationFunction.Exponential:
            s = t * t;
            break;

        case InterpolationFunction.ExponentialSquared:
            s = Mathf.Pow((t * t), 2);
            break;

        case InterpolationFunction.Cubic:
            s = t * t * t;
            break;

        case InterpolationFunction.Cubic2:
            s = ((t - 3.0f) * t + 3.0f) * t;
            break;

        case InterpolationFunction.EaseInOutCubic:
            s = t < 0.5f ? 4.0f * t * t * t : (t - 1.0f) * (2.0f * t - 2.0f) * (2.0f * t - 2.0f) + 1.0f;
            break;

        case InterpolationFunction.SuperSmooth:
            s = t * t * t * (t * (6.0f * t - 15.0f) + 10.0f);
            break;

        default:
            Debug.Log("Invalid InterpolationFunction : " + function);
            break;
        }

        return(s);
    }
Beispiel #3
0
    public static float[,] GenerateSmoothNoise(float[,] baseNoise, int octave, InterpolationFunction interpolate)
    {
        int width  = baseNoise.GetLength(0);
        int height = baseNoise.GetLength(1);

        float[,] smoothNoise = new float[width, height];

        int   samplePeriod    = 1 << octave; // calculates 2 ^ k
        float sampleFrequency = 1.0f / samplePeriod;

        for (int i = 0; i < width; i++)
        {
            // Calculate the horizontal sampling indices
            int   sample_i0        = (i / samplePeriod) * samplePeriod;
            int   sample_i1        = (sample_i0 + samplePeriod) % width; // Wrap around
            float horizontal_blend = (i - sample_i0) * sampleFrequency;

            for (int j = 0; j < height; j++)
            {
                // Calculate the vertical sampling indices
                int   sample_j0      = (j / samplePeriod) * samplePeriod;
                int   sample_j1      = (sample_j0 + samplePeriod) % height; //wrap around
                float vertical_blend = (j - sample_j0) * sampleFrequency;

                // Blend the top two corners
                float top = interpolate.Invoke(baseNoise[sample_i0, sample_j0], baseNoise[sample_i1, sample_j0], horizontal_blend);

                // Blend the bottom two corners
                float bottom = interpolate.Invoke(baseNoise[sample_i0, sample_j1], baseNoise[sample_i1, sample_j1], horizontal_blend);

                // Final blend
                smoothNoise[i, j] = interpolate.Invoke(top, bottom, vertical_blend);
            }
        }

        return(smoothNoise);
    }
Beispiel #4
0
    public static void GenerateStretchNoise(float[,] baseNoise, int offsetX, int offsetY, int octave, InterpolationFunction interpolate, ref float[,] result)
    {
        int width  = baseNoise.GetLength(0);
        int height = baseNoise.GetLength(1);

        if (result == null)
        {
            result = new float[width, height];
        }

        Debug.Assert(result.GetLength(0) == width && result.GetLength(1) == height, "Invalid array size");

        int   samplePeriod    = 1 << octave; // calculates 2 ^ k
        float sampleFrequency = 1.0f / samplePeriod;

        // Wrap the offset around the noise map size (handle negative offset as well).
        offsetX = (((offsetX % width) + width) % width) * (width / samplePeriod);
        offsetY = (((offsetY % height) + height) % height) * (height / samplePeriod);

        for (int i = 0; i < width; i++)
        {
            // Calculate the horizontal sampling indices
            int   sample_i0        = (offsetX + i / samplePeriod) % width;
            int   sample_i1        = (sample_i0 + 1) % width;
            float horizontal_blend = (i % samplePeriod) * sampleFrequency;

            for (int j = 0; j < height; j++)
            {
                // Calculate the vertical sampling indices
                int   sample_j0      = (offsetY + j / samplePeriod) % height;
                int   sample_j1      = (sample_j0 + 1) % height;
                float vertical_blend = (j % samplePeriod) * sampleFrequency;

                // Blend the top two corners
                float top = interpolate.Invoke(baseNoise[sample_i0, sample_j0], baseNoise[sample_i1, sample_j0], horizontal_blend);

                // Blend the bottom two corners
                float bottom = interpolate.Invoke(baseNoise[sample_i0, sample_j1], baseNoise[sample_i1, sample_j1], horizontal_blend);

                // Final blend
                result[i, j] = interpolate.Invoke(top, bottom, vertical_blend);
            }
        }
    }
Beispiel #5
0
        public void ProcessRoundAndSetTotalStage(Round round)
        {
            try
            {
                _drawBall = true;
                int partCount = 100;

                if (roundNumber - _lastGoalRoundNumber > 100 && roundNumber - _explosionStartedRound > 100)
                {
                    explosionPosition      = _ballPosition;
                    _explosionStartedRound = roundNumber;
                }

                if (roundNumber == _explosionStartedRound + 5)
                {
                    AddBallToGame();
                }

                var interpolationFunctionBall = new InterpolationFunction(_ballPosition, 1.0 / partCount);
                _ballAnimator = new Animator <Vector2d>(interpolationFunctionBall.GetInterpolated, Vector2d.Zero, Vector2d.Zero, 1); //todo govnokod

                var interpolateFunctionMan = new List <InterpolationFunction>();
                _manAnimators = new List <Animator <Vector2d> >();
                _manList.ForEach(x =>
                {
                    interpolateFunctionMan.Add(new InterpolationFunction(x.position, 1.0 / partCount));
                    _manAnimators.Add(new Animator <Vector2d>(interpolateFunctionMan.Last().GetInterpolated, Vector2d.Zero, Vector2d.Zero, 1));
                });

                var manAims = round.turns.SelectMany(x => x.manAims).ToList();

                bool setSpeedNormalized     = true;
                var  manSpeedExplosionCoeff = Enumerable.Repeat(1.0, 10).ToList();
                var  goBackSpeedBonus       = Enumerable.Repeat(1.0, 10).ToList();
                if (_lastGoalRoundNumber != -1 && roundNumber - _lastGoalRoundNumber <= _PARTY_AFTER_GOAL_TIME)
                {
                    for (int i = 0; i < _manList.Count; i++)
                    {
                        if (i < 5)
                        {
                            manAims[i] = _manList[i].position - Vector2d.UnitX * 10000;
                        }
                        else
                        {
                            manAims[i] = _manList[i].position + Vector2d.UnitX * 10000;
                        }
                    }

                    if (_lastGoalTeam == 0)
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            goBackSpeedBonus[i] = 1.6;
                        }
                    }
                    else
                    {
                        for (int i = 5; i < 10; i++)
                        {
                            goBackSpeedBonus[i] = 1.6;
                        }
                    }
                    _drawBall = false;
                }
                else if (roundNumber >= _explosionStartedRound && roundNumber - _explosionStartedRound < 5)
                {
                    if (_explosionStartedRound == roundNumber)
                    {
                        manAims = _manList.Select(man =>
                        {
                            if ((man.position - explosionPosition).Length < 0.00001)
                            {
                                return(man.position); //stays
                            }
                            else
                            {
                                return(man.position + (man.position - explosionPosition).Normalized() * 1000);
                            }
                        }).ToList();
                        explosionEffectCoeffBetween0and1 = _manList.Select(man => Math.Min(1.0, 200 / (man.position - explosionPosition).LengthSquared)).ToList();

                        _ballPosition = new Vector2d(50, 30);
                        _ballTimeLeft = 0;
                    }
                    else
                    {
                        setSpeedNormalized = false;
                    }

                    var coefficients = new List <double> {
                        10, 8, 6, 4, 2
                    };
                    manSpeedExplosionCoeff = explosionEffectCoeffBetween0and1.Select(x => x * coefficients[roundNumber - _explosionStartedRound]).ToList();
                    _drawBall = false;
                }



                if (setSpeedNormalized)
                {
                    manSpeedsNormalized = new List <Vector2d>();
                    for (int i = 0; i < _manList.Count; i++)
                    {
                        if ((manAims[i] - _manList[i].position).Length.DoubleLessOrEqual(0))
                        {
                            manSpeedsNormalized.Add(Vector2d.UnitX * 0.00000001);
                        }
                        else
                        {
                            manSpeedsNormalized.Add((manAims[i] - _manList[i].position).Normalized());
                        }
                    }
                }



                if (_lastGoalRoundNumber != -1 && roundNumber - _lastGoalRoundNumber <= _PARTY_AFTER_GOAL_TIME + 1)
                {
                    //празднование после гола

                    if (roundNumber - _lastGoalRoundNumber == _PARTY_AFTER_GOAL_TIME + 1)
                    {
                        //выбиваем мяч ближнему
                        var list         = players[(_lastGoalTeam + 1) % 2].manList;
                        var manToGetBall = list.OrderBy(x => (x.position - _ballPosition).Length).First();
                        if (_ballOwner != -1 && Debugger.IsAttached)
                        {
                            throw new Exception(); //мало ли
                        }
                        KickBall(manToGetBall.position);

                        _lastBallOwnerTeam = (_lastGoalTeam + 1) % 2; //даем бонус скорости защитникам
                    }
                    else if (roundNumber - _lastGoalRoundNumber == 2)
                    {
                        //смещаем мяч в центр
                        KickBall(new Vector2d(_ballPosition.X, 30)); //!! тут надо быть внимательным , если что то будем менять !! чтобы мяч успел долететь
                    }
                }


                var part = 1.0 / partCount;
                var thereWasCollisionWithMan = Enumerable.Repeat(false, 10).ToList();

                if (round.turns[0].ballAim != null && _ballOwner < 5 && _ballOwner >= 0)
                {
                    KickBall(round.turns[0].ballAim.Value);
                }
                if (round.turns[1].ballAim != null && _ballOwner >= 5)
                {
                    KickBall(round.turns[1].ballAim.Value);
                }


                for (int partIndex = 1; partIndex <= partCount; partIndex++)
                {
                    bool playersCanTouchBall = _lastGoalRoundNumber == -1 || roundNumber - _lastGoalRoundNumber > _PARTY_AFTER_GOAL_TIME;

                    //todo ball with wall
                    var changePosition        = Enumerable.Repeat(true, 10).ToList();
                    var shiffledIndices       = Enumerable.Range(0, 10).OrderBy(x => _rand.Next()).ToList();
                    var alreadyExchangedSpeed = new List <Tuple <int, int> >();
                    for (int shuffledIndex = 0; shuffledIndex < shiffledIndices.Count; shuffledIndex++)
                    {
                        int    i = shiffledIndices[shuffledIndex];
                        double manSpeed;
                        if (i < 5)
                        {
                            manSpeed = _lastBallOwnerTeam == -1 || _lastBallOwnerTeam == 1 ? MAN_SPEED : MAN_SPEED_IN_ATTACK;
                        }
                        else
                        {
                            manSpeed = _lastBallOwnerTeam == -1 || _lastBallOwnerTeam == 0 ? MAN_SPEED : MAN_SPEED_IN_ATTACK;
                        }

                        //  bool _useDefendersSpeedIncrease, _useDefendersLimit;

                        var man = _manList[i];
                        if (thereWasCollisionWithMan[i] == false && (man.position - manAims[i]).Length <= MAN_SPEED * part)
                        {
                            changePosition[i] = false; //уже в пункте назначения, и никто его не двигает
                        }
                        if (changePosition[i])
                        {
                            #region перемещение игрока
                            double speedIncreaseCoeff = 1;
                            if (_useDefendersSpeedIncrease)
                            {
                                if ((i < 5 && _ballPosition.X < _arena.size.X / 2) || (i >= 5 && _ballPosition.X > _arena.size.X / 2))
                                {
                                    speedIncreaseCoeff = 1.3;
                                }
                            }
                            var manWants = man.position + manSpeedsNormalized[i] * manSpeed * part * speedIncreaseCoeff * manSpeedExplosionCoeff[i] * goBackSpeedBonus[i];

                            //collided with man
                            for (int j = 0; j < _manList.Count; j++)
                            {
                                if (i == j)
                                {
                                    continue;
                                }

                                if ((manWants - _manList[j].position).Length < _manRadius * 2)
                                {
                                    thereWasCollisionWithMan[i] = thereWasCollisionWithMan[j] = true;
                                    changePosition[i]           = false;

                                    if (alreadyExchangedSpeed.Contains(Tuple.Create(i, j)) == false)
                                    {
                                        var newSpeeds = Engine.ProcessManCollision(man.position, _manList[j].position, manSpeedsNormalized[i], manSpeedsNormalized[j]);
                                        manSpeedsNormalized[i] = newSpeeds.Item1;
                                        manSpeedsNormalized[j] = newSpeeds.Item2;
                                        alreadyExchangedSpeed.Add(Tuple.Create(i, j));
                                        alreadyExchangedSpeed.Add(Tuple.Create(j, i));

                                        //if(explosionEffectCoeffBetween0and1 != null && explosionEffectCoeffBetween0and1.Count > 0)
                                        //{
                                        //    var p = explosionEffectCoeffBetween0and1[i];
                                        //    explosionEffectCoeffBetween0and1[i] = explosionEffectCoeffBetween0and1[j];
                                        //    explosionEffectCoeffBetween0and1[j] = p;
                                        //}

                                        if (_ballOwner == i)
                                        {
                                            _ballOwner         = j;
                                            _lastBallOwnerTeam = _ballOwner / 5;
                                        }
                                        else if (_ballOwner == j)
                                        {
                                            _ballOwner         = i;
                                            _lastBallOwnerTeam = _ballOwner / 5;
                                        }
                                    }
                                }
                            }

                            //collided with wall
                            if (changePosition[i])
                            {
                                double horizontalLimitLeft = 0, horizontalLimitRight = _arena.size.X;
                                //prohibited to go there
                                if (_useDefendersLimit)
                                {
                                    if (i == 3 || i == 4)
                                    {
                                        horizontalLimitLeft = _arena.size.X / 2;
                                    }
                                    else if (i == 8 || i == 9)
                                    {
                                        horizontalLimitRight = _arena.size.X / 2;
                                    }
                                }

                                if (manWants.X < horizontalLimitLeft + _manRadius || manWants.X > horizontalLimitRight - _manRadius)
                                {
                                    manSpeedsNormalized[i] = new Vector2d(-manSpeedsNormalized[i].X, manSpeedsNormalized[i].Y);
                                    changePosition[i]      = false;
                                }
                                if (manWants.Y < _manRadius || manWants.Y > _arena.size.Y - _manRadius)
                                {
                                    manSpeedsNormalized[i] = new Vector2d(manSpeedsNormalized[i].X, -manSpeedsNormalized[i].Y);
                                    changePosition[i]      = false;
                                }
                            }



                            if (changePosition[i])
                            {
                                man.position = manWants;
                            }
                            #endregion
                        }


                        interpolateFunctionMan[i].Add(man.position);


                        //interactions with ball
                        if (_ballOwner == -1 && _ballIgnoreCollisionsWith != i && playersCanTouchBall)
                        {
                            if ((man.position - _ballPosition).Length <= _manRadius + _ballRadius)
                            {
                                _ballOwner                = i;
                                _lastBallOwnerTeam        = _ballOwner / 5;
                                _ballIgnoreCollisionsWith = -1;
                                _ballPosition             = man.position;
                                if (_ballOwner < 5 && round.turns[0].ballAim != null)
                                {
                                    KickBall(round.turns[0].ballAim.Value);
                                }
                                else if (_ballOwner >= 5 && round.turns[1].ballAim != null)
                                {
                                    KickBall(round.turns[1].ballAim.Value);
                                }
                            }
                        }
                    }



                    if (_ballOwner == -1)
                    {
                        if (_ballTimeLeft > 0)
                        {
                            var  ballPart           = Math.Min(_ballTimeLeft, part);
                            var  ballWants          = _ballPosition + ballPart * BALL_SPEED * _ballSpeedNormalized;
                            bool changePositionBall = true;
                            if (ballWants.X < _ballRadius || ballWants.X > _arena.size.X - _ballRadius)
                            {
                                changePositionBall        = false;
                                _ballIgnoreCollisionsWith = -1;
                                if (ballWants.X < _ballRadius)
                                {
                                    Goal(1);
                                }
                                else
                                {
                                    Goal(0);
                                }
                                playersCanTouchBall = false;
                                _ballTimeLeft       = 0; //останавливается
                            }
                            if (ballWants.Y < _ballRadius || ballWants.Y > _arena.size.Y - _ballRadius)
                            {
                                _ballSpeedNormalized.Y    = -_ballSpeedNormalized.Y;
                                changePositionBall        = false;
                                _ballIgnoreCollisionsWith = -1;
                            }

                            if (changePositionBall)
                            {
                                _ballPosition = ballWants;
                            }
                        }
                    }
                    else
                    {
                        _ballPosition = _manList[_ballOwner].position;
                    }
                    interpolationFunctionBall.Add(_ballPosition);

                    if (_ballTimeLeft > 0)
                    {
                        _ballTimeLeft -= part;
                    }
                    if (_ballOwner != -1)
                    {
                        _ballTimeLeft = 0;
                    }

                    if (_ballTimeLeft.DoubleLessOrEqual(0))
                    {
                        _ballIgnoreCollisionsWith = -1;
                    }
                    //stop when goal

                    //if(_ballWasTouchedFirst)


                    //players

                    //if ballowner != -1

                    if (_lastBallOwnerTeam != -1)
                    {
                        players[_lastBallOwnerTeam].possession++;
                    }
                }


                if (roundNumber == 299)
                {
                    GameFinished = true;
                    if (_gameInstancePurpose == GamePurpose.fastGameInBackgroundWithoutVisualization)
                    {
                        var pos = players[0].possession * 1.0 / players.Sum(x => x.possession);

                        var one = (int)Math.Round(pos * 10000);
                        var two = 10000 - one;

                        var result = new GameResult
                        {
                            BotName    = players[1].name,
                            OurScore   = players[0].score,
                            BotScore   = players[1].score,
                            possession = one
                        };
                        _formState.LastGameResult = result;
                    }
                }
                round.totalStage = 1;
            }
            catch { if (Debugger.IsAttached)
                    {
                        throw;
                    }
            }
        }
 public Interpolation()
 {
     EvaluationFcn = PicewiseLinear;
 }