예제 #1
0
        public UnitAction GetAction(Unit unit, Game game, Debug debug)
        {
            Unit?nearestEnemy = null;

            foreach (var other in game.Units)
            {
                if (other.PlayerId != unit.PlayerId)
                {
                    if (!nearestEnemy.HasValue || DistanceSqr(unit.Position, other.Position) < DistanceSqr(unit.Position, nearestEnemy.Value.Position))
                    {
                        nearestEnemy = other;
                    }
                }
            }
            LootBox?nearestWeapon = null;

            foreach (var lootBox in game.LootBoxes)
            {
                if (lootBox.Item is Item.Weapon)
                {
                    if (!nearestWeapon.HasValue || DistanceSqr(unit.Position, lootBox.Position) < DistanceSqr(unit.Position, nearestWeapon.Value.Position))
                    {
                        nearestWeapon = lootBox;
                    }
                }
            }
            Vec2Double targetPos = unit.Position;

            if (!unit.Weapon.HasValue && nearestWeapon.HasValue)
            {
                targetPos = nearestWeapon.Value.Position;
            }
            else if (nearestEnemy.HasValue)
            {
                targetPos = nearestEnemy.Value.Position;
            }
            debug.Draw(new CustomData.Log("Target pos: " + targetPos));
            Vec2Double aim = new Vec2Double(0, 0);

            if (nearestEnemy.HasValue)
            {
                aim = new Vec2Double(nearestEnemy.Value.Position.X - unit.Position.X, nearestEnemy.Value.Position.Y - unit.Position.Y);
            }
            bool jump = targetPos.Y > unit.Position.Y;

            if (targetPos.X > unit.Position.X && game.Level.Tiles[(int)(unit.Position.X + 1)][(int)(unit.Position.Y)] == Tile.Wall)
            {
                jump = true;
            }
            if (targetPos.X < unit.Position.X && game.Level.Tiles[(int)(unit.Position.X - 1)][(int)(unit.Position.Y)] == Tile.Wall)
            {
                jump = true;
            }
            UnitAction action = new UnitAction();

            action.Velocity   = targetPos.X - unit.Position.X;
            action.Jump       = jump;
            action.JumpDown   = !jump;
            action.Aim        = aim;
            action.Shoot      = true;
            action.Reload     = false;
            action.SwapWeapon = false;
            action.PlantMine  = false;
            return(action);
        }
예제 #2
0
        public static bool IsStraightVisible(CustomUnit me, Vec2Double target, Game game, World around, Debug debug = null)
        {
            var visibleLine = new List <Tile>();

            var diffX        = Math.Abs(me.Position.X - target.X);
            var diffY        = Math.Abs(me.Position.Y - target.Y);
            var pointsNumber = (int)GetDistance(me.Position, target);
            var intervalX    = diffX / (pointsNumber + 1);
            var intervalY    = diffY / (pointsNumber + 1);

            for (var i = 1; i <= pointsNumber; i++)
            {
                double x   = 0;
                double y   = 0;
                var    meX = me.Position.X;
                var    meY = me.Position.Y;
                var    tX  = target.X;
                var    tY  = target.Y + 0.9;

                if (meY < tY && meX > tX)
                {
                    x = meX - intervalX * i;
                    y = meY + intervalY * i;
                }
                else if (meY > tY && meX < tX)
                {
                    x = tX - intervalX * i;
                    y = tY + intervalY * i;
                }
                else if (meY < tY && meX < tX)
                {
                    x = tX - intervalX * i;
                    y = tY - intervalY * i;
                }
                else if (meY > tY && meX > tX)
                {
                    x = meX - intervalX * i;
                    y = meY - intervalY * i;
                }
                else if (meY == tY)
                {
                    if (meX > tX)
                    {
                        x = meX - intervalX * i;
                    }
                    else
                    {
                        x = meX + intervalX * i;
                    }

                    y = meY;
                }
                else if (meX == tX)
                {
                    if (meY > tY)
                    {
                        y = meY - intervalY * i;
                    }
                    else
                    {
                        y = meY + intervalY * i;
                    }

                    x = meX;
                }

                var tileX = (int)Math.Round(x) > Constants.MaxXArrayTile
                                ? Constants.MaxXArrayTile
                                : (int)Math.Round(x);
                var tileY = (int)Math.Round(y) > Constants.MaxYArrayTile
                                ? Constants.MaxYArrayTile
                                : (int)Math.Round(y);
                tileX = tileX < 0
                            ? 0
                            : tileX;
                tileY = tileY < 0
                            ? 0
                            : tileY;

                debug?.Draw(new CustomData.PlacedText("+",
                                                      new Vec2Float(tileX, tileY),
                                                      TextAlignment.Center,
                                                      15,
                                                      Constants.BlueColor));

                if (around.Teammate != null)
                {
                    var tile = GetDistance(new Vec2Double(tileX, tileY), around.Teammate.Position) <= 0.7 ||
                               GetDistance(new Vec2Double(tileX, tileY), new Vec2Double(around.Teammate.Position.X, around.Teammate.Position.Y + 1)) <= 0.7
                                   ? Tile.Wall
                                   : game.Level.Tiles[tileX][tileY];

                    visibleLine.Add(tile);
                }
                else
                {
                    visibleLine.Add(game.Level.Tiles[tileX][tileY]);
                }
            }

            var visible = !visibleLine.Exists(x => x == Tile.Wall);

            return(visible);
        }
예제 #3
0
 public static void Log(this UnitAction action, Debug debug, Unit player, Strategy strategy)
 {
     debug.Draw(new CustomData.Log($"Unit #{player.Id} | Velocity: {action.Velocity} | Jump: {action.Jump} | JumpDown: {action.JumpDown} | Aim: {action.Aim.X} {action.Aim.Y} | Shoot: {action.Shoot} | SwapWeapon: {action.SwapWeapon} | PlantMine: {action.PlantMine}"));
     debug.Draw(new CustomData.Log($"HP: {player.Health}"));
     debug.Draw(new CustomData.Log($"Strategy: {strategy.Type}"));
 }
예제 #4
0
        public static bool RLAimed(MyUnit me, Vec2Double target, Game game, Debug debug = null)
        {
            var Ax     = me.Position.X;
            var Ay     = me.Position.Y;
            var Bx     = target.X;
            var By     = target.Y + 0.9;
            var angle1 = me.WeaponSpread + Math.Atan2(By - Ay, Bx - Ax);
            var point1 = new Vec2Double(Ax + Math.Cos(angle1) * Constants.SaveAreaRays,
                                        Ay + Math.Sin(angle1) * Constants.SaveAreaRays);
            var angle2 = -me.WeaponSpread + Math.Atan2(By - Ay, Bx - Ax);
            var point2 = new Vec2Double(Ax + Math.Cos(angle2) * Constants.SaveAreaRays,
                                        Ay + Math.Sin(angle2) * Constants.SaveAreaRays);
            var list = new List <Vec2Double>
            {
                point1, point2
            };

            var visibleLine = new List <Tile>();

            foreach (var point in list)
            {
                var diffX        = Math.Abs(me.Position.X - point.X);
                var diffY        = Math.Abs(me.Position.Y - point.Y);
                var pointsNumber = (int)GetDistance(me.Position, point);
                var intervalX    = diffX / (pointsNumber + 1);
                var intervalY    = diffY / (pointsNumber + 1);

                for (var i = 1; i <= pointsNumber; i++)
                {
                    double x   = 0;
                    double y   = 0;
                    var    meX = me.Position.X;
                    var    meY = me.Position.Y;
                    var    tX  = point.X;
                    var    tY  = point.Y;

                    if (meY < tY && meX > tX)
                    {
                        x = meX - intervalX * i;
                        y = meY + intervalY * i;
                    }
                    else if (meY > tY && meX < tX)
                    {
                        x = tX - intervalX * i;
                        y = tY + intervalY * i;
                    }
                    else if (meY < tY && meX < tX)
                    {
                        x = tX - intervalX * i;
                        y = tY - intervalY * i;
                    }
                    else if (meY > tY && meX > tX)
                    {
                        x = meX - intervalX * i;
                        y = meY - intervalY * i;
                    }
                    else if (meY == tY)
                    {
                        if (meX > tX)
                        {
                            x = meX - intervalX * i;
                        }
                        else
                        {
                            x = meX + intervalX * i;
                        }

                        y = meY;
                    }
                    else if (meX == tX)
                    {
                        if (meY > tY)
                        {
                            y = meY - intervalY * i;
                        }
                        else
                        {
                            y = meY + intervalY * i;
                        }

                        x = meX;
                    }

                    var tileX = (int)Math.Round(x) > Constants.MaxXArrayTile
                                    ? Constants.MaxXArrayTile
                                    : (int)Math.Round(x);
                    var tileY = (int)Math.Round(y) > Constants.MaxYArrayTile
                                    ? Constants.MaxYArrayTile
                                    : (int)Math.Round(y);
                    tileX = tileX < 0
                                ? 0
                                : tileX;
                    tileY = tileY < 0
                                ? 0
                                : tileY;

                    debug?.Draw(new CustomData.PlacedText("+",
                                                          new Vec2Float(tileX, tileY),
                                                          TextAlignment.Center,
                                                          15,
                                                          Constants.RedColor));

                    visibleLine.Add(game.Level.Tiles[tileX][tileY]);
                }
            }

            var visible = !visibleLine.Exists(x => x == Tile.Wall);

            return(visible);
        }