public void Run()
        {
            var myStrategy     = new MyStrategy();
            var debugInterface = new DebugInterface(reader, writer);
            var running        = true;

            while (running)
            {
                switch (Model.ServerMessage.ReadFrom(reader))
                {
                case Model.ServerMessage.GetAction message:
                    new Model.ClientMessage.ActionMessage(myStrategy.GetAction(message.PlayerView, message.DebugAvailable ? debugInterface : null)).WriteTo(writer);
                    writer.Flush();
                    break;

                case Model.ServerMessage.Finish message:
                    running = false;
                    break;

                case Model.ServerMessage.DebugUpdate message:
                    myStrategy.DebugUpdate(message.PlayerView, debugInterface);
                    new Model.ClientMessage.DebugUpdateDone().WriteTo(writer);
                    writer.Flush();
                    break;

                default:
                    throw new Exception("Unexpected server message");
                }
            }
        }
        public Action GetAction(PlayerView playerView, DebugInterface debugInterface)
        {
            PlayerView.Instance = playerView;
            PathFinder.Refresh();
            BuilderAnalyzer.Refresh();

            var actions = new Dictionary <int, EntityAction>();

            foreach (var builder in playerView.Entities.Where(i => i.EntityType == EntityType.BuilderUnit && i.PlayerId == playerView.MyId))
            {
                var resource = playerView.Entities.Where(i => i.EntityType == EntityType.Resource).OrderBy(i => i.Distance(builder)).First();
                var path     = builder.RouteTo(resource);

                if (path != null && path.Count > 1)
                {
                    actions.Add(builder.Id, new EntityAction(new MoveAction(path[1], true, true), null, new AttackAction(resource.Id, new AutoAttack(1, new[] { EntityType.Resource })), null));
                }
                else
                {
                    actions.Add(builder.Id, new EntityAction(new MoveAction(resource.Position, true, true), null, new AttackAction(resource.Id, new AutoAttack(1, new[] { EntityType.Resource })), null));
                }
            }

            var builderBase = playerView.Entities.First(i => i.EntityType == EntityType.BuilderBase && i.PlayerId == playerView.MyId);

            if (EntityHelper.FindBestBuilderSpawnPosition(builderBase, out var spawn))
            {
                actions.Add(builderBase.Id, new EntityAction(null, new BuildAction(EntityType.BuilderUnit, spawn), null, null));
            }

            return(new Action(actions));
        }
        public static void DrawLine(Vec2Int p1, Vec2Int p2, Color color, DebugInterface debugInterface)
        {
            var vertex1 = new ColoredVertex(new Vec2Float(p1.X + 0.5f, p1.Y + 0.5f), new Vec2Float(0, 0), color);
            var vertex2 = new ColoredVertex(new Vec2Float(p2.X + 0.5f, p2.Y + 0.5f), new Vec2Float(0, 0), color);

            var debugData    = new DebugData.Primitives(new[] { vertex1, vertex2 }, PrimitiveType.Lines);
            var debugCommand = new DebugCommand.Add(debugData);

            debugInterface.Send(debugCommand);
        }
Exemple #4
0
        public Action GetAction(PlayerView playerView, DebugInterface debugInterface)
        {
            Log.DebugInterface = debugInterface;
            Log.Clear();
            World.Update(playerView);
            Bot.Do();
            Dictionary <int, EntityAction> actions = World.All(e => e.My).ToDictionary(
                k => k.Id,
                v => v.Action);

            return(new Action(actions));
        }
        public static void DrawRegion(int x, int y, Color color, DebugInterface debugInterface)
        {
            var vertex1 = new ColoredVertex(new Vec2Float(x + 0.25f, y + 0.25f), new Vec2Float(0, 0), color);
            var vertex2 = new ColoredVertex(new Vec2Float(x + 0.75f, y + 0.25f), new Vec2Float(0, 0), color);
            var vertex3 = new ColoredVertex(new Vec2Float(x + 0.75f, y + 0.75f), new Vec2Float(0, 0), color);
            var vertex4 = new ColoredVertex(new Vec2Float(x + 0.25f, y + 0.75f), new Vec2Float(0, 0), color);

            var debugData    = new DebugData.Primitives(new[] { vertex1, vertex2, vertex3, vertex4 }, PrimitiveType.Lines);
            var debugCommand = new DebugCommand.Add(debugData);

            debugInterface.Send(debugCommand);
        }
        private static void DrawScoreMap(DebugInterface debugInterface)
        {
            if (!Params.IsDebug)
            {
                return;
            }

            for (int x = 0; x < 80; x++)
            {
                for (int y = 0; y < 80; y++)
                {
                    var scoreCell = ScoreMap.Get(x, y);
                    if (scoreCell.ResourceScore > 0)
                    {
                        DrawRegion(x, y, Green, debugInterface);
                    }

                    if (scoreCell.RepairScore > 0)
                    {
                        DrawRegion(x, y, Green, debugInterface);
                    }

                    //if (scoreCell.MeleeAttack > 0)
                    //{
                    //    DrawRegion(x, y, Blue, debugInterface);
                    //}

                    //if (scoreCell.RangedAttack > 0)
                    //{
                    //    DrawRegion(x, y, Blue, debugInterface);
                    //}

                    if (scoreCell.MeleeDamage > 0)
                    {
                        DrawRegion(x, y, Red, debugInterface);
                    }

                    if (scoreCell.TurretDamage > 0)
                    {
                        DrawRegion(x, y, Red, debugInterface);
                    }

                    if (scoreCell.RangedDamage > 0)
                    {
                        DrawRegion(x, y, Blue, debugInterface);
                    }
                }
            }
        }
        public Action GetAction(PlayerView playerView, DebugInterface debugInterface)
        {
            actions = new Dictionary <int, EntityAction>();
            View    = playerView;
            Debug   = debugInterface;

            Around.Scan(View);

            CommandBuildingsWorkers();
            CommandBuildingsRange();

            CommandUnitsWorkers();
            CommandUnitsRanged();
            CommandUnitsMelee();
            CommandUnitsTurrets();

            return(new Action(actions));
        }
Exemple #8
0
        public Action GetAction(PlayerView playerView, DebugInterface debugInterface)
        {
            Action result = new Action(new System.Collections.Generic.Dictionary <int, Model.EntityAction>());
            int    myId   = playerView.MyId;

            foreach (var entity in playerView.Entities)
            {
                if (entity.PlayerId != myId)
                {
                    continue;
                }

                EntityAction entityAction = GetEntityAction(playerView, entity);
                result.EntityActions[entity.Id] = entityAction;
            }

            return(result);
        }
Exemple #9
0
        public Action GetAction(PlayerView playerView, DebugInterface debugInterface)
        {
            WorldConfig.Init(playerView);

            int entityCount = playerView.Entities.Count(e => e.IsMyEntity);

            var actions = new Dictionary <int, Model.EntityAction>(entityCount);

            TurretManager.Manage(playerView, actions);

            BuilderUnitManager.Manage(playerView, actions);

            BuilderBaseManager.Manage(playerView, actions);

            RangedBaseManager.Manage(playerView, actions);

            WarriorManager.ManageUnits(playerView, actions);

            return(new Action(actions));
        }
Exemple #10
0
 public void DebugUpdate(PlayerView playerView, DebugInterface debugInterface)
 {
     debugInterface.Send(new DebugCommand.Clear());
     debugInterface.GetState();
 }
Exemple #11
0
 public Action GetAction(PlayerView playerView, DebugInterface debugInterface)
 {
     return(new Action(new System.Collections.Generic.Dictionary <int, Model.EntityAction>(0)));
 }
Exemple #12
0
        public Action GetAction(PlayerView playerView, DebugInterface debugInterface)
        {
            Action result = new Action(new System.Collections.Generic.Dictionary <int, Model.EntityAction>());
            int    myId   = playerView.MyId;

            foreach (var entity in playerView.Entities)
            {
                if (entity.PlayerId != myId)
                {
                    continue;
                }
                EntityProperties properties  = playerView.EntityProperties[entity.EntityType];
                MoveAction?      moveAction  = null;
                BuildAction?     buildAction = null;
                if (properties.CanMove)
                {
                    moveAction = new MoveAction(
                        new Vec2Int(playerView.MapSize - 1, playerView.MapSize - 1),
                        true,
                        true
                        );
                }
                else if (properties.Build != null)
                {
                    EntityType entityType   = properties.Build.Value.Options[0];
                    int        currentUnits = 0;
                    foreach (var otherEntity in playerView.Entities)
                    {
                        if (otherEntity.PlayerId != null && otherEntity.PlayerId == myId &&
                            otherEntity.EntityType == entityType)
                        {
                            currentUnits++;
                        }
                    }
                    if ((currentUnits + 1) * playerView.EntityProperties[entityType].PopulationUse <= properties.PopulationProvide)
                    {
                        buildAction = new BuildAction(
                            entityType,
                            new Vec2Int(entity.Position.X + properties.Size, entity.Position.Y + properties.Size - 1)
                            );
                    }
                }
                EntityType[] validAutoAttackTargets;
                if (entity.EntityType == EntityType.BuilderUnit)
                {
                    validAutoAttackTargets = new EntityType[] { EntityType.Resource };
                }
                else
                {
                    validAutoAttackTargets = new EntityType[0];
                }
                result.EntityActions[entity.Id] = new EntityAction(
                    moveAction,
                    buildAction,
                    new AttackAction(
                        null,
                        new AutoAttack(properties.SightRange, validAutoAttackTargets)
                        ),
                    null
                    );
            }
            return(result);
        }
        private static Vec2Int GetMoveTarget(Vec2Int current, Vec2Int target, Dictionary <Vec2Int, Vec2Int> cameFrom, Color color, DebugInterface debugInterface)
        {
            var fromPosition = target;

            while (cameFrom[fromPosition].X != current.X ||
                   cameFrom[fromPosition].Y != current.Y)
            {
                if (Params.IsDebug)
                {
                    DrawLine(fromPosition, cameFrom[fromPosition], color, debugInterface);
                }

                fromPosition = cameFrom[fromPosition];
            }

            return(fromPosition);
        }
        private static void SetMoveAction(Entity entity, Vec2Int approxTarget, Dictionary <int, EntityAction> entityActions, DebugInterface debugInterface)
        {
            if (entityActions.ContainsKey(entity.Id))
            {
                return;
            }

            var     bestTarget = ASearchMove(entity, approxTarget, out var cameFrom, out var costSoFar);
            Vec2Int moveTarget = GetMoveTarget(entity.Position, bestTarget, cameFrom, Blue, debugInterface);

            ScoreMap.Set(entity.Position, null);
            ScoreMap.Set(moveTarget, entity);

            var moveAction = new MoveAction(moveTarget, false, false);

            entityActions.Add(entity.Id, new EntityAction(moveAction, null, null, null));
        }
        public Action GetAction(PlayerView playerView, DebugInterface debugInterface)
        {
            if (Params.IsDebug)
            {
                debugInterface.Send(new DebugCommand.SetAutoFlush(true));
            }

            if (playerView.CurrentTick == 10 && playerView.Players.Length == 2)
            {
                Params.RangedBaseBuildingLimit = 35;
                Params.MaxBuilderUnitsCount   += 30;
                Params.MaxRangedUnitsCount    += 20;
                Params.MaxHouseCount          += 10;
            }

            var entityActions = new Dictionary <int, EntityAction>();

            ScoreMap.InitMap(playerView);
            DrawScoreMap(debugInterface);
            IsDanger = DangerCheck();

            // repairing
            Entity? builder     = null;
            Vec2Int?moveTarget  = null;
            int     minDistance = int.MaxValue;

            foreach (Entity entity in playerView.Entities)
            {
                if (entity.PlayerId != ScoreMap.MyId || entity.EntityType != EntityType.BuilderUnit)
                {
                    continue;
                }

                BuilderUnitActions.SetRepair(playerView, entity, entityActions);
                if (!ScoreMap.AnyRepairScoreMoreThanOne || entityActions.ContainsKey(entity.Id))
                {
                    continue;
                }

                var approxTarget = BuilderUnitActions.GetApproxTarget(entity, entityActions);

                var bestTarget = ASearchMove(entity, approxTarget, out var cameFrom, out var costSoFar);
                var cell       = ScoreMap.Get(bestTarget);
                if (cell.RepairScore > 1 && costSoFar[bestTarget] < minDistance)
                {
                    builder     = entity;
                    moveTarget  = GetMoveTarget(builder.Value.Position, bestTarget, cameFrom, Blue, debugInterface);
                    minDistance = costSoFar[bestTarget];
                }
            }

            if (builder != null && moveTarget != null)
            {
                ScoreMap.Set(builder.Value.Position, null);
                ScoreMap.Set(moveTarget.Value, builder.Value);

                var moveAction = new MoveAction(moveTarget.Value, false, false);
                entityActions.Add(builder.Value.Id, new EntityAction(moveAction, null, null, null));
            }

            // building turret
            if ((playerView.Players.Length > 2 || playerView.CurrentTick >= 300) &&
                ScoreMap.MyActiveRangedBases.Count > 0 &&
                (ScoreMap.Limit >= Params.TurretBuildingLimit || playerView.CurrentTick >= 300) &&
                ScoreMap.MyResource >= ScoreMap.TurretProperties.InitialCost &&
                ScoreMap.MyNotActiveTurrets.Count <= 1)
            {
                BuilderUnitActions.SetBuild(EntityType.Turret, ScoreMap.TurretProperties.Size, entityActions, debugInterface);
                ScoreMap.MyResource -= ScoreMap.TurretProperties.InitialCost;
            }

            // building ranged base
            if (ScoreMap.MyActiveRangedBases.Count == 0 &&
                ScoreMap.MyResource >= ScoreMap.RangedBaseProperties.InitialCost &&
                ScoreMap.MyNotActiveRangedBases.Count == 0)
            {
                BuilderUnitActions.SetBuild(EntityType.RangedBase, ScoreMap.RangedBaseProperties.Size, entityActions, debugInterface);
                ScoreMap.MyResource -= ScoreMap.RangedBaseProperties.InitialCost;
            }

            // building house
            if (((ScoreMap.MyActiveRangedBases.Count == 0 &&
                  ScoreMap.Limit >= Params.RangedBaseBuildingLimit &&
                  ScoreMap.MyResource >=
                  ScoreMap.RangedBaseProperties.InitialCost + ScoreMap.HouseProperties.InitialCost) ||

                 ((ScoreMap.MyActiveRangedBases.Count > 0 ||
                   ScoreMap.MyNotActiveRangedBases.Count > 0 ||
                   ScoreMap.Limit < Params.RangedBaseBuildingLimit) &&
                  ScoreMap.MyResource >= ScoreMap.HouseProperties.InitialCost)
                 ) &&
                ScoreMap.Limit + 10 >= ScoreMap.AvailableLimit &&
                ScoreMap.MyNotActiveHouses.Count <= 1 &&
                ScoreMap.MyNotActiveHouses.Count + ScoreMap.MyActiveHouses.Count < Params.MaxHouseCount)
            {
                BuilderUnitActions.SetBuild(EntityType.House, ScoreMap.HouseProperties.Size, entityActions, debugInterface);
                ScoreMap.MyResource -= ScoreMap.HouseProperties.InitialCost;
            }

            foreach (Entity entity in playerView.Entities)
            {
                if (entity.PlayerId != ScoreMap.MyId)
                {
                    continue;
                }

                switch (entity.EntityType)
                {
                case EntityType.BuilderUnit:
                {
                    BuilderUnitActions.SetAttack(entity, entityActions);
                    var approxTarget = BuilderUnitActions.GetApproxTarget(entity, entityActions);
                    SetMoveAction(entity, approxTarget, entityActions, debugInterface);
                    continue;
                }

                case EntityType.MeleeUnit:
                {
                    CombatUnitAction.SetAttack(entity, 1, 1, entityActions);
                    var approxTarget = CombatUnitAction.GetAttackTarget(entity, entityActions);
                    SetMoveAction(entity, approxTarget, entityActions, debugInterface);
                    continue;
                }

                case EntityType.RangedUnit:
                {
                    CombatUnitAction.SetAttack(entity, 5, 1, entityActions);
                    var approxTarget = CombatUnitAction.GetAttackTarget(entity, entityActions);
                    SetMoveAction(entity, approxTarget, entityActions, debugInterface);
                    continue;
                }

                case EntityType.Turret:
                {
                    if (!entity.Active)
                    {
                        continue;
                    }

                    CombatUnitAction.SetAttack(entity, 5, 2, entityActions);
                    continue;
                }

                case EntityType.BuilderBase:
                {
                    int unitCost = ScoreMap.BuilderUnitProperties.InitialCost + ScoreMap.MyBuilderUnits.Count;

                    if ((IsDanger && ScoreMap.MyResource >= unitCost * 2 ||
                         !IsDanger && ScoreMap.MyResource >= unitCost) &&
                        ScoreMap.MyBuilderUnits.Count < ScoreMap.BuilderUnitTargets.Count &&
                        ScoreMap.MyBuilderUnits.Count < Params.MaxBuilderUnitsCount)
                    {
                        var approxTarget = BuilderUnitActions.GetApproxTarget(entity, entityActions);
                        SetBuildUnitAction(entity, approxTarget, EntityType.BuilderUnit, unitCost, entityActions);
                    }

                    if (!entityActions.ContainsKey(entity.Id))
                    {
                        entityActions.Add(entity.Id, new EntityAction(null, null, null, null));
                    }

                    continue;
                }

                case EntityType.MeleeBase:
                {
                    int unitCost = ScoreMap.MeleeUnitProperties.InitialCost + ScoreMap.MyMeleeUnits.Count;

                    if ((IsDanger && ScoreMap.MyResource >= unitCost ||
                         !IsDanger && ScoreMap.MyResource >= unitCost * 2) &&
                        ScoreMap.MyRangedUnits.Count * 2 >= Params.MaxRangedUnitsCount &&
                        ScoreMap.MyMeleeUnits.Count < Params.MaxMeleeUnitsCount)
                    {
                        var approxTarget = CombatUnitAction.GetAttackTarget(entity, entityActions);
                        SetBuildUnitAction(entity, approxTarget, EntityType.MeleeUnit, unitCost, entityActions);
                    }

                    if (!entityActions.ContainsKey(entity.Id))
                    {
                        entityActions.Add(entity.Id, new EntityAction(null, null, null, null));
                    }

                    continue;
                }

                case EntityType.RangedBase:
                {
                    if (!entity.Active)
                    {
                        continue;
                    }

                    int unitCost = ScoreMap.RangedUnitProperties.InitialCost + ScoreMap.MyRangedUnits.Count;

                    if ((IsDanger && ScoreMap.MyResource >= unitCost ||
                         !IsDanger && ScoreMap.MyResource >= unitCost * 3) &&
                        ScoreMap.MyRangedUnits.Count < Params.MaxRangedUnitsCount)
                    {
                        var approxTarget = CombatUnitAction.GetAttackTarget(entity, entityActions);
                        SetBuildUnitAction(entity, approxTarget, EntityType.RangedUnit, unitCost, entityActions);
                    }

                    if (!entityActions.ContainsKey(entity.Id))
                    {
                        entityActions.Add(entity.Id, new EntityAction(null, null, null, null));
                    }

                    continue;
                }
                }
            }

            return(new Action(entityActions));
        }