Esempio n. 1
0
        //public static Point GetEnemyVehicleRepulsiveFunction(Vehicle enemyVehicle, double coeff, IList<Vehicle> vehicles, bool isGroundAttak)
        //{
        //    var radius = GetActualShootingDistance(enemyVehicle, isGroundAttak) + MyStrategy.EnemyDangerousRadius;
        //    var centerPoint = MyStrategy.GetVehiclesCenter(vehicles);
        //    var dist = centerPoint.GetDistance(enemyVehicle.X, enemyVehicle.Y);
        //    if (dist > radius) return new Point(0d, 0d);
        //    var x = coeff * (centerPoint.X - enemyVehicle.X) * (1 / dist - 1 / radius) / Math.Pow(dist, 3d);
        //    var y = coeff * (centerPoint.Y - enemyVehicle.Y) * (1 / dist - 1 / radius) / Math.Pow(dist, 3d);
        //    return new Point(x, y);
        //}

        public static Point GetEnemyGroupRepulsiveFunction(GroupContainer groupContainer, double coeff,
                                                           IList <Vehicle> vehicles)
        {
            var enemyRectangle =
                MathHelper.GetJarvisRectangle(groupContainer.Vehicles.Select(v => new Point(v.X, v.Y)).ToList());
            var myCenter = MyStrategy.GetVehiclesCenter(vehicles);

            var enemyCp      = MathHelper.GetNearestRectangleCrossPoint(myCenter, enemyRectangle, groupContainer.Center);
            var myCenterDist = myCenter.GetDistance(groupContainer.Center);
            var radius       = groupContainer.Center.GetDistance(enemyCp) + MyStrategy.EnemyDangerousRadius;

            if (myCenterDist > radius)
            {
                return(new Point(0d, 0d));
            }

            double x, y;

            if (myCenterDist < radius / 2)
            {
                x = coeff * (myCenter.X - groupContainer.Center.X) / myCenterDist;
                y = coeff * (myCenter.Y - groupContainer.Center.Y) / myCenterDist;
            }
            else
            {
                x = 2 * coeff * (myCenter.X - groupContainer.Center.X) * (1 / myCenterDist - 1 / radius);
                y = 2 * coeff * (myCenter.Y - groupContainer.Center.Y) * (1 / myCenterDist - 1 / radius);
            }

            return(new Point(x, y));
        }
Esempio n. 2
0
        public static Point GetBorderRepulsiveFunction(IList <Vehicle> vehicles, double worldWidth, double worldHeight)
        {
            var resPoint = new Point(0d, 0d);

            var center = MyStrategy.GetVehiclesCenter(vehicles);
            var radius = MyStrategy.GetSandvichRadius(vehicles);

            if (center.X - radius < CloseBorderDist)
            {
                resPoint = new Point(resPoint.X + 1d, resPoint.Y);
            }
            if (center.Y - radius < CloseBorderDist)
            {
                resPoint = new Point(resPoint.X, resPoint.Y + 1d);
            }
            if (center.X + radius > worldWidth - CloseBorderDist)
            {
                resPoint = new Point(resPoint.X - 1d, resPoint.Y);
            }
            if (center.Y + radius > worldHeight - CloseBorderDist)
            {
                resPoint = new Point(resPoint.X, resPoint.Y - 1d);
            }

            return(resPoint);
        }
Esempio n. 3
0
    //结合简单工厂模式,产生策略
    public StrategyContext(string type)
    {
        switch (type)
        {
        case "A":
            strategy = new ConcreateStrategyA();
            break;

        case "B":
            strategy = new ConcreateStrategyB();
            break;
        }
    }
Esempio n. 4
0
        public static Point GetAllyNoGroupRepulsiveFunction(IList <Vehicle> thisVehicles, IList <Vehicle> otherVehicles, double coeff)
        {
            var isThisGround = MyStrategy.IsGroundGroup(thisVehicles);
            var isThisAir    = MyStrategy.IsAirGroup(thisVehicles);
            var myCenter     = MyStrategy.GetVehiclesCenter(thisVehicles);
            var myRadius     = MyStrategy.GetSandvichRadius(thisVehicles);

            var resPoint = new Point(0d, 0d);

            foreach (var v in otherVehicles)
            {
                var isGroundVehicle = v.Type == VehicleType.Arrv || v.Type == VehicleType.Ifv ||
                                      v.Type == VehicleType.Tank;
                if (isThisGround && !isGroundVehicle)
                {
                    continue;
                }
                if (isThisAir && isGroundVehicle)
                {
                    continue;
                }

                var centersDist = myCenter.GetDistance(v.X, v.Y);
                if (centersDist > myRadius + MyStrategy.EnemyDangerousRadius)
                {
                    continue;
                }

                double x, y;
                if (centersDist < myRadius)
                {
                    x = coeff * (myCenter.X - v.X) / centersDist;
                    y = coeff * (myCenter.Y - v.Y) / centersDist;
                }
                else
                {
                    x = 2 * coeff * (myCenter.X - v.X) * (1 / centersDist - 1 / (myRadius + MyStrategy.EnemyDangerousRadius));
                    y = 2 * coeff * (myCenter.Y - v.Y) * (1 / centersDist - 1 / (myRadius + MyStrategy.EnemyDangerousRadius));
                }

                resPoint = new Point(resPoint.X + x, resPoint.Y + y);
            }

            return(resPoint);
        }
Esempio n. 5
0
        public static Point GetAllyGroupRepulsiveFunction(IList <Vehicle> thisVehicles, IList <Vehicle> otherVehicles, double coeff)
        {
            var isThisGround  = MyStrategy.IsGroundGroup(thisVehicles);
            var isThisAir     = MyStrategy.IsAirGroup(thisVehicles);
            var isOtherGround = MyStrategy.IsGroundGroup(otherVehicles);
            var isOtherAir    = MyStrategy.IsAirGroup(otherVehicles);

            if (isThisGround && !isOtherGround || isThisAir && !isOtherAir || isOtherGround && !isThisGround ||
                isOtherAir && !isThisAir)
            {
                return(new Point(0d, 0d));
            }

            var myCenter    = MyStrategy.GetVehiclesCenter(thisVehicles);
            var myRadius    = MyStrategy.GetSandvichRadius(thisVehicles);
            var otherCenter = MyStrategy.GetVehiclesCenter(otherVehicles);
            var otherRadius = MyStrategy.GetSandvichRadius(otherVehicles);

            var centersDist = myCenter.GetDistance(otherCenter);

            if (centersDist > myRadius + otherRadius + MyStrategy.EnemyDangerousRadius)
            {
                return(new Point(0d, 0d));
            }

            //Debug.circle(myCenter.X, myCenter.Y, myRadius + otherRadius + EnemyDangerousRadius, 0x00FF00);

            double x, y;

            if (centersDist < myRadius + otherRadius)
            {
                x = coeff * (myCenter.X - otherCenter.X) / centersDist;
                y = coeff * (myCenter.Y - otherCenter.Y) / centersDist;
            }
            else
            {
                x = 2 * coeff * (myCenter.X - otherCenter.X) * (1 / centersDist - 1 / (myRadius + otherRadius + MyStrategy.EnemyDangerousRadius));
                y = 2 * coeff * (myCenter.Y - otherCenter.Y) * (1 / centersDist - 1 / (myRadius + otherRadius + MyStrategy.EnemyDangerousRadius));
            }

            return(new Point(x, y));
        }
    public StockPriceReaderModel ParsePrices(string file)
    {
        var stockPriceReaderModel = new StockPriceReaderModel();

        List <string>          row = new List <string>();
        List <StockPriceModel> s   = new List <StockPriceModel>();

        stockPriceReaderModel.name = "Stock: " + System.IO.Path.GetFileNameWithoutExtension(file);
        using (var reader = new CsvFileReader(file))
        {
            while (reader.ReadRow(row))
            {
                //Ignore premarket and aftermarket trading eg. before 0930AM and after 0400PM
                int time = int.Parse(row[1]);
                if (time >= 930 && time <= 1600)
                {
                    float open   = float.Parse(row[2]);
                    float high   = float.Parse(row[3]);
                    float low    = float.Parse(row[4]);
                    float close  = float.Parse(row[5]);
                    float volume = float.Parse(row[6]);

                    var stockPriceModel = new StockPriceModel(open, close, high, low, volume);
                    stockPriceReaderModel.prices.Add(stockPriceModel);
                }
            }
        }

        RSI.Calculate(stockPriceReaderModel.prices);
        SMA.Calculate(stockPriceReaderModel.prices);

        //identify and name patterns
        MyStrategy.Apply(stockPriceReaderModel.prices);

        return(stockPriceReaderModel);
    }
Esempio n. 7
0
        public void UpdateDynamicAStar(
            int groupIndex, IList <Vehicle> vehicles, IDictionary <int, VehicleType> reversedGroupIndexes, MyStrategy ms)
        {
            var centerX = vehicles.Average(v => v.X);
            var centerY = vehicles.Average(v => v.Y);

            var myLeftX = centerX - SquareSize / 2;
            var leftN   = (int)(myLeftX / SquareSize);

            var myTopY = centerY - SquareSize / 2;
            var topM   = (int)(myTopY / SquareSize);

            for (var i = 0; i < _n; ++i)
            {
                for (var j = 0; j < _m; ++j)
                {
                    var square = _table[i, j];
                    square.X      = _startX + i * SquareSize;
                    square.Y      = _startY + j * SquareSize;
                    square.Weight = 1d;
                }
            }

            _startSquare = _table[leftN, topM];

            foreach (var index in reversedGroupIndexes.Keys)
            {
                if (index == groupIndex)
                {
                    continue;
                }
                if (!MyStrategy.IsSameTypes(reversedGroupIndexes[groupIndex], reversedGroupIndexes[index]))
                {
                    continue; // они друг другу не мешают
                }
                var currVeichles = ms.GetVehicles(index, MyStrategy.Ownership.ALLY);
                if (!currVeichles.Any())
                {
                    continue;                      // все сдохли
                }
                var currCenterX = currVeichles.Average(v => v.X);
                var currCenterY = currVeichles.Average(v => v.Y);

                var currLeftX = currCenterX - SquareSize / 2;
                var currLeftN = (int)(currLeftX / SquareSize);

                var currTopY = currCenterY - SquareSize / 2;
                var currTopM = (int)(currTopY / SquareSize);

                _table[currLeftN, currTopM].Weight = BigWeight;
            }


            //if (leftN > 0 && _table[leftN - 1, topM].Weight == BigWeight)
            //{
            //    if (topM > 0) _table[leftN - 1, topM - 1].Weight = BigWeight;
            //    if (topM < _m - 1) _table[leftN - 1, topM + 1].Weight = BigWeight;
            //}

            //if (topM > 0 && _table[leftN, topM - 1].Weight == BigWeight)
            //{
            //    if (leftN > 0) _table[leftN - 1, topM - 1].Weight = BigWeight;
            //    if (leftN < _n - 1) _table[leftN + 1, topM - 1].Weight = BigWeight;
            //}

            //if (leftN < _n - 1 && _table[leftN + 1, topM].Weight == BigWeight)
            //{
            //    if (topM > 0) _table[leftN + 1, topM - 1].Weight = BigWeight;
            //    if (topM < _m - 1) _table[leftN + 1, topM + 1].Weight = BigWeight;
            //}

            //if (topM < _m - 1 && _table[leftN, topM + 1].Weight == BigWeight)
            //{
            //    if (leftN > 0) _table[leftN - 1, topM + 1].Weight = BigWeight;
            //    if (leftN < _n - 1) _table[leftN + 1, topM + 1].Weight = BigWeight;
            //}
        }
Esempio n. 8
0
 public StrategyContext(MyStrategy myStrategy)
 {
     strategy = myStrategy;
 }
        public static void SetBuild(EntityType buildEntityType, int size, Dictionary <int, EntityAction> entityActions, DebugInterface debugInterface)
        {
            Entity? builder       = null;
            Vec2Int?buildPosition = null;
            int     minDistance   = int.MaxValue;

            foreach (Entity entity in ScoreMap.MyBuilderUnits)
            {
                if (entityActions.ContainsKey(entity.Id))
                {
                    continue;
                }

                var range = entity.Position.Range(10);
                if (range.Any(e => ScoreMap.Get(e).AllDamage > 0))
                {
                    continue;
                }

                if (buildEntityType == EntityType.Turret)
                {
                    if (range.Count(e => ScoreMap.Get(e).Entity?.EntityType == EntityType.Resource) < 25 ||
                        range.Any(e => ScoreMap.Get(e).Entity?.EntityType == EntityType.Turret))
                    {
                        continue;
                    }
                }

                if (buildEntityType == EntityType.House)
                {
                    var range2 = entity.Position.Range(5);
                    if (range2.Count(e => ScoreMap.Get(e).Entity?.EntityType == EntityType.BuilderUnit) == 0)
                    {
                        continue;
                    }
                }

                var buildPositions = entity.Position.BuildPositions(size);
                foreach (var position in buildPositions)
                {
                    if (buildEntityType == EntityType.RangedBase &&
                        position.X <= 15 &&
                        position.Y <= 15)
                    {
                        continue;
                    }

                    var diagonals = position.Diagonals(size);
                    if (ScoreMap.Passable(position, size) &&
                        diagonals.All(ScoreMap.PassableInFutureOrResource))
                    {
                        int distance = buildEntityType == EntityType.House
                            ? position.Distance(ScoreMap.MyHouseBase)
                            : position.Distance(ScoreMap.EnemyBase);

                        if (distance < minDistance)
                        {
                            builder       = entity;
                            buildPosition = position;
                            minDistance   = distance;
                        }
                    }
                }
            }

            if (buildEntityType == EntityType.House && builder == null && buildPosition == null)
            {
                foreach (Entity entity in ScoreMap.MyBuilderUnits)
                {
                    if (entityActions.ContainsKey(entity.Id))
                    {
                        continue;
                    }

                    var buildPositions = entity.Position.BuildPositions(size);
                    foreach (var position in buildPositions)
                    {
                        var diagonals = position.Diagonals(size);
                        if (ScoreMap.Passable(position, size) &&
                            diagonals.All(ScoreMap.PassableInFutureOrResource))
                        {
                            int distance = position.Distance(ScoreMap.MyHouseBase);
                            if (distance < minDistance)
                            {
                                builder       = entity;
                                buildPosition = position;
                                minDistance   = distance;
                            }
                        }
                    }
                }
            }

            if (builder != null && buildPosition != null)
            {
                var buildAction = new BuildAction(buildEntityType, buildPosition.Value);
                entityActions.Add(builder.Value.Id, new EntityAction(null, buildAction, null, null));

                ScoreMap.Build(buildPosition.Value, size);

                if (Params.IsDebug)
                {
                    MyStrategy.DrawRegion(buildPosition.Value.X, buildPosition.Value.Y, MyStrategy.Lemon, debugInterface);
                }
            }
        }