Esempio n. 1
0
        public async Task DoScan(byte size)
        {
            if (IsEnd)
            {
                return;
            }
            int distance = 2 * size + 1;
            var buffer   = new byte[2 + distance * distance];

            buffer[0] = (byte)Message.m_mapInfos;
            buffer[1] = (byte)(distance);
            UInt16 posByte = 2;
            int    posY    = bot.Y + size;

            for (UInt16 j = 0; j < (2 * size + 1); j++)
            {
                int posX = bot.X - size;
                for (UInt16 i = 0; i < (2 * size + 1); i++)
                {
                    if (posX < 0 || posX >= MainGame.Settings.MapWidth || posY < 0 || posY >= MainGame.Settings.MapHeight)
                    {
                        buffer[posByte++] = (byte)CaseState.Wall;
                    }
                    else
                    {
                        CaseState cs = MainGame.TheMap[posX, posY];
                        switch (cs)
                        {
                        case CaseState.Empty:
                        case CaseState.Wall:
                        case CaseState.Energy:
                            buffer[posByte++] = (byte)cs;
                            break;

                        case CaseState.Ennemy:
                            buffer[posByte++] = (byte)MainGame.IsEnnemyVisible(bot.X, bot.Y, posX, posY);
                            break;

                        default:
                            buffer[posByte++] = (byte)cs;
                            break;
                        }
                    }
                    posX++;
                }
                posY--;
            }
            try
            {
                State = BotState.WaitingAction;
                Console.WriteLine($"Sending 'I' to {bot.GUID}");
                if (size > 0)
                {
                    MainGame.SendCockpitInfo(bot.GUID, new ArraySegment <byte>(buffer, 0, buffer.Length));
                }
                await webSocket.SendAsync(new ArraySegment <byte>(buffer, 0, buffer.Length), WebSocketMessageType.Binary, true, CancellationToken.None);
            }
            catch (Exception err)
            {
                if (MainGame.TheMap[bot.X, bot.Y] == CaseState.Ennemy)
                {
                    MainGame.TheMap[bot.X, bot.Y] = CaseState.Empty;
                }
                Console.WriteLine($"[ERROR] {err.Message}");
                State = BotState.Error;
            }
        }