Esempio n. 1
0
        public override bool tryDrive(Vehicle vehicle, List<Tile> tilePath, Move move)
        {
            if (tilePath.Count < 4)
                return false;

            var current = MyStrategy.map.tileAt(vehicle.position);

            var currentToFirst = current.directionForTile(tilePath[0]).Value;
            var firstToSecond = tilePath[0].directionForTile(tilePath[1]).Value;
            var secondToThird = tilePath[1].directionForTile(tilePath[2]).Value;
            var thirdToFour = tilePath[2].directionForTile(tilePath[3]).Value;

            if (!currentToFirst.isSameAxis(firstToSecond))
                return false;

            if (firstToSecond.isSameAxis(secondToThird))
                return false;

            if (firstToSecond.back() != thirdToFour)
                return false;

            var turningFrom = new Vector(currentToFirst);
            var turningTo = new Vector(firstToSecond);

            move.EnginePower = 1;
            move.WheelTurn = vehicle.steeringAngleForDirection(vehicle.stabilizationDir(turningFrom, current.center, turningTo, -0.25 * Constants.tileSize));

            if (vehicle.speed.length > 15) {
                move.IsBrake = true;
            } else {
                move.IsBrake = false;
            }
            return true;
        }
Esempio n. 2
0
        public override bool tryDrive(Vehicle vehicle, List<Tile> tilePath, Move move)
        {
            if (tilePath.Count < 2)
                return false;

            var current = MyStrategy.map.tileAt(vehicle.position);

            var currentToFirst = current.directionForTile(tilePath[0]).Value;
            var firstToSecond = tilePath[0].directionForTile(tilePath[1]).Value;
            var secondToThird = tilePath[1].directionForTile(tilePath[2]).Value;

            if (currentToFirst.isSameAxis(firstToSecond))
                return false;

            if (currentToFirst.back() != secondToThird)
                return false;

            var turningFrom = new Vector(currentToFirst);
            var turningTo = new Vector(firstToSecond);

            if (vehicle.speed.length > 10) {

                var steering = Math.Sign(turningFrom.angleTo(turningTo));

                var backWall = new Ray(
                    tilePath[0].center + turningFrom * (Constants.tileSize * 0.5 - Constants.roadMargin) - turningTo * Constants.tileSize * 0.5,
                    turningTo * Constants.tileSize * 2
                );

                var backSideWall = new Ray(
                    backWall.p2 - turningTo * Constants.roadMargin,
                    -turningFrom * (Constants.tileSize - Constants.roadMargin)
                );

                var innerWall = new Ray(current.center + turningTo * (Constants.tileSize * 0.5 - Constants.roadMargin) - turningFrom * Constants.tileSize * 0.5,
                                    turningFrom * Constants.tileSize);

                var innerCircle = new Circle(current.center + (new Vector(currentToFirst) + new Vector(firstToSecond)) * Constants.tileSize * 0.5, Constants.roadMargin);

                backWall.draw(0x00FF00);
                backSideWall.draw(0x00FF00);
                innerWall.draw(0x00FF00);
                innerCircle.draw(0x00FF00);

                var vv = new VirtualVehicle(vehicle);

                for (int i = 0; i < 100; ++i) {

                    vv.simulateTick(0.5, steering);

                    var rect = vv.rect;

                    if (rect.isIntersect(innerWall) || rect.isIntersect(innerCircle)) {
                        move.EnginePower = 1;
                        move.WheelTurn = -steering;
                        move.IsBrake = false;
                        return true;
                    }

                    if (rect.isIntersect(backWall) || rect.isIntersect(backSideWall)) {
                        move.IsBrake = false;
                        move.EnginePower = -1;
                        //move.WheelTurn = vehicle.steeringAngleForDirection(vehicle.stabilizationDir(turningFrom, current.center, turningTo, -0.25 * Constants.tileSize));
                        move.WheelTurn = steering;
                        return true;
                    }

                    vv.position.draw(0xFF0000);
                    vv.rect.draw(0x0000FF);

                }
            }

            move.EnginePower = 0.5;
            move.WheelTurn = vehicle.steeringAngleForDirection(vehicle.stabilizationDir(turningFrom, current.center, turningTo, -0.25 * Constants.tileSize));
            move.IsBrake = false;
            return true;
        }
Esempio n. 3
0
        public override bool tryDrive(Vehicle vehicle, List<Tile> tilePath, Move move)
        {
            if (tilePath.Count < 3)
                return false;

            var currentTile = MyStrategy.map.tileAt(vehicle.position);

            Vector turningTo;
            Vector turningFrom;

            {
                var firstToSecond = tilePath[0].directionForTile(tilePath[1]).Value;

                turningTo = new Vector(firstToSecond);

                if (firstToSecond != tilePath[1].directionForTile(tilePath[2])) {
                    return false;
                }

                var currentToFirst = currentTile.directionForTile(tilePath[0]).Value;
                if (currentToFirst.isSameAxis(firstToSecond)) {
                    return false;
                }

                turningFrom = new Vector(currentToFirst);

            }

            if (vehicle.speed.length > 10) {

                var innerSide1 = Ray.line(
                    currentTile.center + turningTo * (Constants.tileSize * 0.5 - Constants.roadMargin) - turningFrom * Constants.tileSize * 0.5,
                    currentTile.center + turningTo * (Constants.tileSize * 0.5 - Constants.roadMargin) + turningFrom * Constants.tileSize * 0.5
                );

                //innerSide1.draw(0x00FF00);

                var innerCircle = new Circle(currentTile.center + (turningTo + turningFrom) * Constants.tileSize * 0.5, Constants.roadMargin);

                //innerCircle.draw(0x00FF00);

                var innerCircleExtended = new Circle(innerCircle.position, innerCircle.radius + 10);

                var backWall = new Ray(tilePath[0].center  + turningFrom * (Constants.tileSize * 0.5 - Constants.roadMargin) - turningTo * Constants.tileSize * 0.5,
                                   turningTo * Constants.tileSize * 2);

                //backWall.draw(0x00FF00);

                if (vehicle.forward * turningFrom > 0.45) {
                    var vv = new VirtualVehicle(vehicle);
                    var steering = Math.Sign(turningFrom.angleTo(turningTo));

                    for (int i = 0; i < 100; ++i) {
                        vv.simulateTick(1.0, steering);

                        if (vv.rect.isIntersect(innerSide1) || vv.rect.isIntersect(innerCircle)) {
                            move.EnginePower = 1;
                            move.WheelTurn = vehicle.steeringAngleForDirection(vehicle.stabilizationDir(turningFrom, currentTile.center, turningTo, -0.15 * Constants.tileSize));
                            move.IsBrake = false;
                            return true;
                        }

                        if (vv.rect.isIntersect(backWall)) {
                            move.IsBrake = true;
                            move.EnginePower = 1;
                            move.WheelTurn = steering;
                            return true;
                        }

                        if (tilePath[1].rect.contains(vv.position)) {

                            // защита от слишком быстрого захода в поворот и ударения в заднюю стенку

                            for (int j = 0; j < 50; ++j) {

                                vv.simulateTick(1, steering);
                                if (vv.rect.isIntersect(backWall)) {
                                    move.EnginePower = 1;
                                    move.WheelTurn = steering;
                                    move.IsBrake = true;
                                    return true;
                                }

                                //vv.position.draw(0x00FF00);
                                //vv.rect.draw(0x0000FF);

                            }

                            move.EnginePower = 1;
                            move.WheelTurn = steering;
                            move.IsBrake = false;
                            return true;
                        }

                        //vv.position.draw(0xFF0000);
                        //vv.rect.draw(0x0000FF);
                    }
                } else if (vehicle.forward * turningTo > 0.5) {

                    var vv = new VirtualVehicle(vehicle);
                    var steering = Math.Sign(turningFrom.angleTo(turningTo));

                    for (int i = 0; i < 100; ++i) {
                        vv.simulateTick(1.0, 0);

                        if (vv.rect.isIntersect(innerSide1) || vv.rect.isIntersect(innerCircleExtended)) {

                            move.EnginePower = 1;
                            move.WheelTurn = -steering;
                            move.IsBrake = false;
                            return true;
                        }

                        if (tilePath[1].rect.contains(vv.position)) {
                            move.EnginePower = 1;
                            move.WheelTurn = 0;
                            move.IsBrake = false;
                            return true;
                        }

                        //vv.position.draw(0xFF0000);
                        //vv.rect.draw(0x0000FF);
                    }
                } else {
                    move.IsBrake = true;
                    move.WheelTurn = 0;
                    move.EnginePower = 1;
                    return true;
                }

            }

            move.IsBrake = false;
            move.EnginePower = 1;
            var turn = turningTo + turningFrom;
            move.WheelTurn = vehicle.steeringAngleForDirection(turn);

            return true;
        }