Beispiel #1
0
 /// <summary>
 /// Check of the bot queue contains a given bot.
 /// </summary>
 /// <param name="bot"></param>
 /// <returns></returns>
 public bool Contains(VTankBot bot)
 {
     lock (this)
     {
         return(botQueue.Contains(bot));
     }
 }
Beispiel #2
0
 public PlayerMoveEvent(VTankBot _game, int id, VTankObject.Point point,
                        VTankObject.Direction direction) : base(_game)
 {
     this.id        = id;
     this.direction = direction;
     this.point     = point;
 }
Beispiel #3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="parentBot"></param>
 public GameTracker(VTankBot parentBot)
 {
     players              = new Dictionary <int, MetaPlayer>();
     projectiles          = new Dictionary <int, MetaProjectile>();
     bot                  = parentBot;
     PathfindingAlgorithm = new PathAlgorithms.AStarAlgorithm();
 }
Beispiel #4
0
        /// <summary>
        /// Gets a list of currently online players.
        /// </summary>
        /// <param name="includeBots">If true, the bots ran by this bot runner are
        /// included in the list. If false, they are excluded.</param>
        /// <returns>List of tank names of online players. The list is null if the
        /// online player list cannot be accessed.</returns>
        public List <Player> GetOnlinePlayers(bool includeBots)
        {
            List <VTankBot> bots = BotRunner.GetRunningBots();

            List <Player> result = new List <Player>();

            for (int i = 0; i < bots.Count; ++i)
            {
                VTankBot bot = bots[i];
                if (bot.GameServer != null && bot.GameServer.Connected)
                {
                    AIFramework.Util.GameTracker game = bot.Game;
                    List <Player> playerList          = game.GetPlayerList();
                    foreach (AIFramework.Bot.Game.Player player in playerList)
                    {
                        if (includeBots)
                        {
                            result.Add(player);
                        }
                        else
                        {
                            if (!ContainsTank(bots, player.Name))
                            {
                                result.Add(player);
                            }
                        }
                    }

                    break;
                }
            }

            return(result);
        }
Beispiel #5
0
 public SpawnUtilityEvent(VTankBot bot, int utilityId, VTankObject.Utility utility, VTankObject.Point pos)
     : base(bot)
 {
     ID           = utilityId;
     this.utility = utility;
     position     = pos;
 }
Beispiel #6
0
        /// <summary>
        /// Add a bot to the runner.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public void AddBot(Type type, string username, string password)
        {
            List <VTankBot> bots = BotRunner.GetRunningBots();

            // Remove old bot if it exists.
            for (int i = 0; i < bots.Count; ++i)
            {
                VTankBot bot = bots[i];
                if (bot.AuthInfo.Username == username)
                {
                    Print("Removed duplicate bot {0}.", username);
                    BotRunner.Kill(bot);
                    break;
                }
            }

            AuthInfo auth = new AuthInfo(username, password);

            BotRunner.Register(type, auth, null);
            BotRunner.Start(false);

            Print("Added new bot, {0}.", username);

            if (OnBotChange != null)
            {
                OnBotChange(null);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Delegate method called by the invocation buffer after a bot has crashed.
        /// This method attempts to reconnect the bot.
        /// </summary>
        /// <param name="botObj"></param>
        private void HandleCrash(object botObj)
        {
            VTankBot bot = (VTankBot)botObj;

            Print("Restarting bot {0}...", bot.TankName);

            AddBot(bot.GetType(), bot.AuthInfo.Username, bot.AuthInfo.Password);

            /*try
             * {
             *  AddBot(bot.GetType(), bot.AuthInfo.Username, bot.AuthInfo.Password);
             * }
             * catch (Exception e)
             * {
             *  string errorMessage = e.Message;
             *  Exception inner = e.InnerException;
             *  while (inner != null)
             *  {
             *      errorMessage = inner.Message;
             *      inner = inner.InnerException;
             *  }
             *
             *  // Re-connect attempt failed -- try again soon.
             *  Print("{0} can't reconnect (trying again soon): {1}",
             *      bot.AuthInfo.Username, errorMessage);
             *  Console.Error.WriteLine();
             *  Console.Error.WriteLine(e.ToString());
             *  Console.Error.WriteLine();
             *
             *  buffer.Enqueue(new Invocation.InvocationTarget(HandleCrash),
             *      botObj, WaitTimeReconnect * 2);
             * }*/
        }
Beispiel #8
0
 public FlagDroppedEvent(VTankBot _game, int droppedId, VTankObject.Point where, GameSession.Alliance flagColor)
     : base(_game)
 {
     this.droppedId = droppedId;
     this.where     = where;
     this.flagColor = flagColor;
 }
Beispiel #9
0
        /// <summary>
        /// Attempt to get the player list.
        /// </summary>
        /// <returns></returns>
        public List <string> TryGetPlayerList()
        {
            List <VTankBot> bots       = botManager.GetBots();
            List <string>   playerList = new List <string>();

            try
            {
                for (int i = 0; i < bots.Count; ++i)
                {
                    VTankBot bot = bots[i];
                    if (!bot.GameServer.Connected)
                    {
                        continue;
                    }

                    List <Player> players = bot.Game.GetPlayerList();
                    foreach (Player player in players)
                    {
                        playerList.Add(player.Name);
                    }
                }
            }
            catch { }

            return(playerList);
        }
Beispiel #10
0
 /// <summary>
 /// Enqueue a bot onto the queue.
 /// </summary>
 /// <param name="bot"></param>
 public void Enqueue(VTankBot bot)
 {
     lock (this)
     {
         botQueue.Enqueue(bot);
     }
 }
Beispiel #11
0
 public PlayerRotateEvent(VTankBot _game, int id, double angle, VTankObject.Direction direction)
     : base(_game)
 {
     this.id        = id;
     this.angle     = angle;
     this.direction = direction;
 }
Beispiel #12
0
 public TurretSpinningEvent(VTankBot _game, int id, double angle, VTankObject.Direction direction)
     : base(_game)
 {
     this.id        = id;
     this.angle     = angle;
     this.direction = direction;
 }
Beispiel #13
0
 public ApplyUtilityEvent(VTankBot _game, int _utilityID,
                          VTankObject.Utility _utility, int _playerID)
     : base(_game)
 {
     utilityID = _utilityID;
     utility   = _utility;
     playerID  = _playerID;
 }
Beispiel #14
0
 public BotPackage(VTankBot bot, GameServerInfo info, Thread thread)
 {
     Bot            = bot;
     Server         = info;
     Thread         = thread;
     state          = BotRunnerState.Offline;
     IsShuttingDown = false;
 }
Beispiel #15
0
 public AddUtilityEvent(VTankBot _game,
                        int _utilityID, VTankObject.Utility _utility, VTankObject.Point _position)
     : base(_game)
 {
     utilityID = _utilityID;
     utility   = _utility;
     position  = _position;
 }
Beispiel #16
0
 public PlayerDamagedByEnvironmentEvent(VTankBot _game, int playerId, int environId, int damageTaken, bool killingBlow)
     : base(_game)
 {
     this.playerId    = playerId;
     this.environId   = environId;
     this.damageTaken = damageTaken;
     this.killingBlow = killingBlow;
 }
Beispiel #17
0
 public BaseCapturedEvent(int eventId, GameSession.Alliance newBaseColor, int capturerId,
                          GameSession.Alliance oldBaseColor, VTankBot _game) : base(_game)
 {
     this.baseEventId  = eventId;
     this.newBaseColor = newBaseColor;
     this.capturerId   = capturerId;
     this.oldBaseColor = oldBaseColor;
 }
 public SpawnEnvironmentEffectEvent(VTankBot _game, int id, int typeId, VTankObject.Point location, int ownerId)
     : base(_game)
 {
     this.id       = id;
     this.typeId   = typeId;
     this.location = location;
     this.ownerId  = ownerId;
     envEffect     = WeaponLoader.GetEnvironmentProperty(typeId);
 }
Beispiel #19
0
 public PlayerDamagedEvent(VTankBot _game, int victimId, int projectileId, int ownerId, int damageTaken, bool killingBlow)
     : base(_game)
 {
     this.victimId     = victimId;
     this.projectileId = projectileId;
     this.ownerId      = ownerId;
     this.damageTaken  = damageTaken;
     this.killingBlow  = killingBlow;
 }
Beispiel #20
0
        /// <summary>
        /// Move a bot from the reserved list to the online list.
        /// </summary>
        /// <param name="username"></param>
        public void MoveBotFromReserve(string username)
        {
            VTankBot bot = reservedList.Find((b) => { return(b.AuthInfo.Username == username); });

            if (bot != null)
            {
                MoveBotFromReserve(bot);
            }
        }
Beispiel #21
0
 public CreateProjectileEvent(VTankBot _game, int projectileTypeId,
                              VTankObject.Point point, int ownerId,
                              int projectileId)
     : base(_game)
 {
     this.type         = WeaponLoader.GetProjectile(projectileTypeId);
     this.point        = point;
     this.ownerId      = ownerId;
     this.projectileId = projectileId;
 }
Beispiel #22
0
        /// <summary>
        /// Move an online bot to the reserved bot list.
        /// </summary>
        /// <param name="username"></param>
        public void MoveBotToReserve(string username)
        {
            List <VTankBot> bots = BotRunner.GetRunningBots();
            VTankBot        bot  = bots.Find((b) => { return(b.AuthInfo.Username == username); });

            if (bot != null)
            {
                MoveBotToReserve(bot);
            }
        }
Beispiel #23
0
 public DamageBaseEvent(int eventId, int damageAmount, int playerId,
                        int projectileId, bool isDestroyed, VTankBot _game)
     : base(_game)
 {
     this.baseEventId  = eventId;
     this.damageAmount = damageAmount;
     this.playerId     = playerId;
     this.projectileId = projectileId;
     this.isDestroyed  = isDestroyed;
 }
 public DamageBaseByEnvironmentEvent(VTankBot _game, GameSession.Alliance baseColor, int baseId, int envId,
                                     int damage, bool isDestroyed)
     : base(_game)
 {
     this.baseColor           = baseColor;
     this.baseId              = baseId;
     this.environmentEffectId = envId;
     this.damage              = damage;
     this.isDestroyed         = isDestroyed;
 }
Beispiel #25
0
        /// <summary>
        /// Runs the bot on a separate thread.
        /// </summary>
        /// <param name="botKlass"></param>
        private void HandleBot(object botObj)
        {
            BotPackage package = (BotPackage)botObj;

            package.State = BotRunnerState.Offline;
            VTankBot       bot    = package.Bot;
            GameServerInfo server = package.Server;

            try
            {
                if (!bot.MainServer.Connected)
                {
                    bot.MainServer.Connect();
                }

                SetState(package, BotRunnerState.SelectingTank);

                // TODO: Let the user pick their own tank..
                if (string.IsNullOrEmpty(bot.TankName))
                {
                    VTankObject.TankAttributes[] tanks = bot.MainServer.GetTankList();
                    if (tanks.Length == 0)
                    {
                        Console.Error.WriteLine("Error: Bot {0} cannot play: he has no tanks.",
                                                bot.ToString());
                        SetState(package, BotRunnerState.Offline);
                        return;
                    }
                    bot.SelectTank(tanks[0].name);
                }
                else
                {
                    bot.SelectTank(bot.TankName);
                }

                ConnectToServer(bot, server);
                SetState(package, BotRunnerState.Playing);

                DoLoop(package, bot);
            }
            catch (Exception e)
            {
                using (Logger logger = new Logger("error.log", FileMode.Append))
                {
                    logger.Exception(e);
                }

                Console.Error.WriteLine(e);
            }
            finally
            {
                package.IsShuttingDown = true;
                Kill(bot);
            }
        }
Beispiel #26
0
        public VTankBot Register(Type botType, AuthInfo authInfo, string tankName)
        {
            // TODO: User still can't specify custom server.
            VTankBot bot = (VTankBot)Activator.CreateInstance(botType,
                                                              this, EchelonAddress, authInfo);

            bot.TankName = tankName;
            bots.Add(bot);

            return(bot);
        }
Beispiel #27
0
        /// <summary>
        /// Move an online bot to the reserved bot list.
        /// </summary>
        /// <param name="username"></param>
        public void MoveBotToReserve(VTankBot bot)
        {
            reservedList.Add(bot);
            BotRunner.Kill(bot);

            Print("{0} moved to reserve.", bot.AuthInfo.Username);

            if (OnBotChange != null)
            {
                OnBotChange(bot);
            }
        }
Beispiel #28
0
        /// <summary>
        /// Get a path ideal to the current state and current game mode.
        /// </summary>
        /// <returns></returns>
        public Path GetIdealPath(VTankBot bot, Player player)
        {
            if (refreshSpawnPointList)
            {
                RefreshSpawnPointList();
            }

            Path path = null;

            if (State == BotState.SEEK_AND_DESTROY)
            {
                path = GetSeekAndDestroyPath(player);
            }
            else if (State == BotState.COMPLETE_OBJECTIVE)
            {
                GameModeHandler handler = bot.Game.GameModeHandler;
                if (handler is CaptureTheBaseMode)
                {
                    const int          RANGE_MINIMUM = 50;
                    CaptureTheBaseMode mode          = (CaptureTheBaseMode)handler;
                    Base targetBase = mode.GetAttackableBase(player);
                    if (targetBase != null)
                    {
                        Point  p1       = player.GetPosition();
                        Point  p2       = targetBase.GetPosition();
                        double distance = GetDistance(p1, p2);

                        if (distance > RANGE_MINIMUM)
                        {
                            int      tilePositionX = (int)Math.Round((p2.x));
                            int      tilePositionY = (int)Math.Round(-(p2.y));
                            TileNode node          = new TileNode(tilePositionX, tilePositionY);
                            path = PathFinder.FindPath(CurrentMap, player, node.X, node.Y);
                        }
                    }
                }
                // TODO: CTF.
                else
                {
                    path = GetSeekAndDestroyPath(player);
                }
            }

            if (spawnPointIndex >= spawnPointList.Count)
            {
                spawnPointIndex = 0;
            }

            return(path);
        }
Beispiel #29
0
        /// <summary>
        /// Event called by the bot runner when a bot's state changes.
        /// </summary>
        /// <param name="bot"></param>
        /// <param name="newState"></param>
        private void BotRunner_OnBotStateChange(VTankBot bot,
                                                AIFramework.Runner.BotRunner.BotRunnerState newState)
        {
            if (newState != BotRunner.BotRunnerState.SelectingTank && OnBotChange != null)
            {
                buffer.Enqueue(new Invocation.InvocationTarget((o) =>
                {
                    OnBotChange((VTankBot)o);
                }),
                               bot);

                NeedsBalance = true;
            }

            buffer.Enqueue(new Invocation((o) =>
            {
                Print("{0}'s state: {1}", bot.AuthInfo.Username, newState.ToString());
            }));
        }
Beispiel #30
0
        /// <summary>
        /// Move a bot from the reserved list to the online list.
        /// </summary>
        /// <param name="username"></param>
        public void MoveBotFromReserve(VTankBot bot)
        {
            try
            {
                AddBot(bot.GetType(), bot.AuthInfo.Username, bot.AuthInfo.Password);
                reservedList.Remove(bot);

                Print(String.Format("Bot {0} moved from reserve.", bot.AuthInfo.Username));
            }
            catch (Exception ex)
            {
                Print(ex.ToString());
            }

            if (OnBotChange != null)
            {
                OnBotChange(bot);
            }
        }