Beispiel #1
0
        private void GameHostResponse(BaseResponse e)
        {
            switch (e.Type)
            {
            case PacketType.UserEnter:
                Connected           = true;
                _changingConnection = false;
                break;

            case PacketType.UserLogin:
                RUserLogin userLogin = (RUserLogin)e;
                _sessionToken = userLogin.SessionToken;
                ShortSize screenRes = new ShortSize
                                      (
                    Screen.PrimaryScreen.Bounds.Width,
                    Screen.PrimaryScreen.Bounds.Height
                                      );
                TCPClientDataSend(new PUserEnter
                {
                    SessionToken = _sessionToken,
                    ScreenRes    = screenRes
                });
                break;
            }
        }
Beispiel #2
0
        private void RecreateMiniMap()
        {
            if (_miniMap != null)
            {
                Memory.HeapFree(_miniMap);
            }

            if (_width <= MINI_MAP_SIZE && _height <= MINI_MAP_SIZE)
            {
                MiniMapWidth  = _width;
                MiniMapHeight = _height;
            }
            else if (_width > _height)
            {
                MiniMapWidth  = MINI_MAP_SIZE;
                MiniMapHeight = (ushort)((float)_height / _width * MINI_MAP_SIZE);
            }
            else
            {
                MiniMapHeight = MINI_MAP_SIZE;
                MiniMapWidth  = (ushort)((float)_width / _height * MINI_MAP_SIZE);
            }

            MiniMapSize = new ShortSize(MiniMapWidth, MiniMapHeight);

            int miniMapArea = MiniMapWidth * MiniMapHeight;

            _miniMap = (byte *)Memory.HeapAlloc(miniMapArea / 2);

            float xCoef = (float)_width / MiniMapWidth;
            float yCoef = (float)_height / MiniMapHeight;

            for (int i = 0; i < miniMapArea; i++)
            {
                ushort mapX = (ushort)(i * xCoef % _width);
                ushort mapY = (ushort)(i * xCoef * yCoef / _width);

                Tile *tile = &_tiles[mapY * _width + mapX];
                byte  pxl  = (byte)((*tile).Type == TileType.Wall ? 1 : 0);

                if (i % 2 == 0)
                {
                    _miniMap[i / 2] = (byte)(pxl << 4);
                }
                else
                {
                    _miniMap[i / 2] = (byte)(_miniMap[i / 2] | pxl);
                }
            }
        }
Beispiel #3
0
        private BaseResponse Enter(int clientId, BasePacket packet)
        {
            PUserEnter pUserEnter = (PUserEnter)packet;

            PlayerDataEx playerData = World.PlayerDataGet(pUserEnter.SessionToken);

            if (playerData == null)
            {
                ThrowSessionError(packet.Type, packet.SessionToken);
                return(null);
            }

            int maxScreenSide = Math.Max(pUserEnter.ScreenRes.Width, pUserEnter.ScreenRes.Height);

            playerData.ScreenRes = new ShortSize(maxScreenSide, maxScreenSide);

            List <Player> nearestPlayers = World.PlayersGetNearest(playerData);

            ShortRect mapWindow;

            byte[]    mapData = World.MapWindowGet(playerData, out mapWindow);
            ShortSize mapSize = new ShortSize(playerData.Map.Width, playerData.Map.Height);

            ShortSize miniMapSize;

            byte[] miniMapData = World.MiniMapGet(playerData, out miniMapSize);

            RegisterWorldResponseForNearest(playerData, new RPlayerEnter
            {
                Player = playerData
            });

            return(new RUserEnter(pUserEnter)
            {
                MyPlayerId = playerData.Player.Id,
                PlayersOnLocation = nearestPlayers,

                MapSize = mapSize,
                MapData = mapData,
                MapWindow = mapWindow,

                MiniMapData = miniMapData,
                MiniMapSize = miniMapSize
            });
        }
Beispiel #4
0
        public void AppendMiniMapData(byte[] data, ShortSize size)
        {
            if (_miniMapData != null)
            {
                Memory.HeapFree(_miniMapData);
            }

            _miniMapSize = size;
            int miniMapArea = size.Width * size.Height;

            _miniMapData = (byte *)Memory.HeapAlloc(miniMapArea / 2);

            fixed(byte *bData = data)
            {
                int pos = 0, pxlCnt, curIdx = 0;

                Serializer.Read(bData, out pxlCnt, ref pos);
                for (int i = 0; i < pxlCnt; i++)
                {
                    byte   rleMark;
                    ushort seriesLength;
                    Serializer.Read(bData, out rleMark, ref pos);
                    if ((rleMark | 0x80) == rleMark)
                    {
                        byte rleMark2;
                        Serializer.Read(bData, out rleMark2, ref pos);
                        seriesLength = (ushort)((rleMark & ~0x80) << 8 | rleMark2);
                    }
                    else
                    {
                        seriesLength = rleMark;
                    }

                    byte pxl;
                    Serializer.Read(bData, out pxl, ref pos);
                    int maxIdx = curIdx + seriesLength;
                    for (; curIdx < maxIdx; curIdx++)
                    {
                        _miniMapData[curIdx] = pxl;
                    }
                }
            }
        }
Beispiel #5
0
        private BaseResponse Enter(int clientId, BasePacket packet)
        {
            PUserEnter pUserEnter = (PUserEnter) packet;

            PlayerDataEx playerData = World.PlayerDataGet(pUserEnter.SessionToken);
            if (playerData == null)
            {
                ThrowSessionError(packet.Type, packet.SessionToken);
                return null;
            }

            int maxScreenSide = Math.Max(pUserEnter.ScreenRes.Width, pUserEnter.ScreenRes.Height);
            playerData.ScreenRes = new ShortSize(maxScreenSide, maxScreenSide);

            List<Player> nearestPlayers = World.PlayersGetNearest(playerData);

            ShortRect mapWindow;
            byte[] mapData = World.MapWindowGet(playerData, out mapWindow);
            ShortSize mapSize = new ShortSize(playerData.Map.Width, playerData.Map.Height);

            ShortSize miniMapSize;
            byte[] miniMapData = World.MiniMapGet(playerData, out miniMapSize);

            RegisterWorldResponseForNearest(playerData, new RPlayerEnter
            {
                Player = playerData
            });

            return new RUserEnter(pUserEnter)
            {
                MyPlayerId = playerData.Player.Id,
                PlayersOnLocation = nearestPlayers,

                MapSize = mapSize,
                MapData = mapData,
                MapWindow = mapWindow,

                MiniMapData = miniMapData,
                MiniMapSize = miniMapSize
            };
        }
Beispiel #6
0
 public void Setup(ShortSize mapSize)
 {
     _width = mapSize.Width;
     _height = mapSize.Height;
 }
Beispiel #7
0
        public void AppendMiniMapData(byte[] data, ShortSize size)
        {
            if (_miniMapData != null)
            {
                Memory.HeapFree(_miniMapData);
            }

            _miniMapSize = size;
            int miniMapArea = size.Width*size.Height;
            _miniMapData = (byte*) Memory.HeapAlloc(miniMapArea/2);

            fixed (byte* bData = data)
            {
                int pos = 0, pxlCnt, curIdx = 0;
                Serializer.Read(bData, out pxlCnt, ref pos);
                for (int i = 0; i < pxlCnt; i++)
                {
                    byte rleMark;
                    ushort seriesLength;
                    Serializer.Read(bData, out rleMark, ref pos);
                    if ((rleMark | 0x80) == rleMark)
                    {
                        byte rleMark2;
                        Serializer.Read(bData, out rleMark2, ref pos);
                        seriesLength = (ushort)((rleMark & ~0x80) << 8 | rleMark2);
                    }
                    else
                    {
                        seriesLength = rleMark;
                    }

                    byte pxl;
                    Serializer.Read(bData, out pxl, ref pos);
                    int maxIdx = curIdx + seriesLength;
                    for (; curIdx < maxIdx; curIdx++)
                    {
                        _miniMapData[curIdx] = pxl;
                    }
                }
            }
        }
Beispiel #8
0
 public void Setup(ShortSize mapSize)
 {
     _width  = mapSize.Width;
     _height = mapSize.Height;
 }
Beispiel #9
0
        private void RecreateMiniMap()
        {
            if (_miniMap != null)
            {
                Memory.HeapFree(_miniMap);
            }

            if (_width <= MINI_MAP_SIZE && _height <= MINI_MAP_SIZE)
            {
                MiniMapWidth = _width;
                MiniMapHeight = _height;
            }
            else if (_width > _height)
            {
                MiniMapWidth = MINI_MAP_SIZE;
                MiniMapHeight = (ushort) ((float) _height/_width*MINI_MAP_SIZE);
            }
            else
            {
                MiniMapHeight = MINI_MAP_SIZE;
                MiniMapWidth = (ushort) ((float) _width/_height*MINI_MAP_SIZE);
            }

            MiniMapSize = new ShortSize(MiniMapWidth, MiniMapHeight);

            int miniMapArea = MiniMapWidth*MiniMapHeight;
            _miniMap = (byte*) Memory.HeapAlloc(miniMapArea/2);

            float xCoef = (float) _width/MiniMapWidth;
            float yCoef = (float) _height/MiniMapHeight;
            for (int i = 0; i < miniMapArea; i++)
            {
                ushort mapX = (ushort) (i*xCoef%_width);
                ushort mapY = (ushort) (i*xCoef*yCoef/_width);

                Tile* tile = &_tiles[mapY*_width + mapX];
                byte pxl = (byte) ((*tile).Type == TileType.Wall ? 1 : 0);

                if (i%2 == 0)
                    _miniMap[i/2] = (byte) (pxl << 4);
                else
                    _miniMap[i/2] = (byte) (_miniMap[i/2] | pxl);
            }
        }
Beispiel #10
0
        private void GameHostResponse(BaseResponse e)
        {
            if (e.HasError)
            {
                ReconnectToHost();
                return;
            }

            switch (e.Type)
            {
                case PacketType.UserLogin:
                    RUserLogin userLogin = (RUserLogin) e;
                    _sessionToken = userLogin.SessionToken;
                    ShortSize screenRes = new ShortSize
                        (
                        Screen.PrimaryScreen.Bounds.Width,
                        Screen.PrimaryScreen.Bounds.Height
                        );
                    TCPClientDataSend(new PUserEnter
                    {
                        SessionToken = _sessionToken,
                        ScreenRes = screenRes
                    });
                    break;

                case PacketType.UserEnter:
                    lock (_playersData)
                    {
                        RUserEnter userEnter = (RUserEnter) e; //!!!
                        _map.Setup(userEnter.MapSize);
                        _map.AppendMapData(userEnter.MapData, userEnter.MapWindow);
                        _map.AppendMiniMapData(userEnter.MiniMapData, userEnter.MiniMapSize);
                        OnMapChanged();

                        foreach (Player p in userEnter.PlayersOnLocation)
                        {
                            if (!_playersData.ContainsKey(p.Id))
                            {
                                _playersData.Add(p.Id, new PlayerDataEx(p));
                            }
                        }
                        _myPlayerData = _playersData[userEnter.MyPlayerId];
                        _playersRo = new ReadOnlyCollection<PlayerDataEx>(_playersData.Values.ToArray());
                    }
                    CenterTo((int) (_myPlayerData.Player.Position.X*_scaleFactor), //!!!
                             (int) (_myPlayerData.Player.Position.Y*_scaleFactor),
                             true);
                    break;

                case PacketType.PlayerEnter:
                    lock (_playersData)
                    {
                        RPlayerEnter playerEnter = (RPlayerEnter) e;
                        if (!_playersData.ContainsKey(playerEnter.Player.Id) && _myPlayerData != null &&
                            playerEnter.Player.Id != _myPlayerData.Player.Id)
                        {
                            _playersData.Add(playerEnter.Player.Id, new PlayerDataEx(playerEnter.Player));
                            _playersRo = new ReadOnlyCollection<PlayerDataEx>(_playersData.Values.ToArray());
                            _somethingChanged = true; //!!!
                        }
                    }
                    break;

                case PacketType.PlayerExit:
                    lock (_playersData)
                    {
                        RPlayerExit playerExit = (RPlayerExit) e;
                        if (_playersData.ContainsKey(playerExit.PlayerId) && _myPlayerData != null &&
                            playerExit.PlayerId != _myPlayerData.Player.Id)
                        {
                            _playersData.Remove(playerExit.PlayerId);
                            _playersRo = new ReadOnlyCollection<PlayerDataEx>(_playersData.Values.ToArray());
                            _somethingChanged = true; //!!!
                        }
                    }
                    break;

                case PacketType.PlayerRotate:
                    lock (_playersData)
                    {
                        PlayerDataEx playerData;
                        RPlayerRotate playerRotate = (RPlayerRotate) e;
                        if (_playersData.TryGetValue(playerRotate.PlayerId, out playerData))
                            playerData.Player.Angle = playerRotate.Angle;
                    }
                    break;

                case PacketType.PlayerMove:
                    lock (_playersData)
                    {
                        PlayerDataEx playerData;
                        RPlayerMove playerMove = (RPlayerMove) e;
                        if (_playersData.TryGetValue(playerMove.PlayerId, out playerData))
                        {
                            if (playerMove.Direction == Direction.None)
                                playerData.StopMoving(playerMove.Position);
                            else
                                playerData.StartMoving(playerMove.Position, playerMove.Direction);
                        }
                    }
                    break;

                case PacketType.MapData:
                    RMapData mapData = (RMapData) e;
                    _map.AppendMapData(mapData.MapData, mapData.MapWindow); //!!!
                    _myPlayerData.GettingMapWindow = false;
                    break;
            }
        }
Beispiel #11
0
 private void GameHostResponse(BaseResponse e)
 {
     switch (e.Type)
     {
         case PacketType.UserEnter:
             Connected = true;
             _changingConnection = false;
             break;
         case PacketType.UserLogin:
             RUserLogin userLogin = (RUserLogin) e;
             _sessionToken = userLogin.SessionToken;
             ShortSize screenRes = new ShortSize
                 (
                 Screen.PrimaryScreen.Bounds.Width,
                 Screen.PrimaryScreen.Bounds.Height
                 );
             TCPClientDataSend(new PUserEnter
             {
                 SessionToken = _sessionToken,
                 ScreenRes = screenRes
             });
             break;
     }
 }
Beispiel #12
0
 public byte[] MiniMapGet(PlayerDataEx playerData, out ShortSize miniMapSize)
 {
     miniMapSize = playerData.Map.MiniMapSize;
     return(playerData.Map.GetMiniMap());
 }
Beispiel #13
0
 public static void Read(byte *bData, out ShortSize val, ref int pos)
 {
     val  = *(ShortSize *)&bData[pos];
     pos += sizeof(ShortSize);
 }
Beispiel #14
0
 public static void Write(byte *bData, ShortSize val, ref int pos)
 {
     (*(ShortSize *)&bData[pos]) = val;
     pos += sizeof(ShortSize);
 }
Beispiel #15
0
 public byte[] MiniMapGet(PlayerDataEx playerData, out ShortSize miniMapSize)
 {
     miniMapSize = playerData.Map.MiniMapSize;
     return playerData.Map.GetMiniMap();
 }
Beispiel #16
0
        private void GameHostResponse(BaseResponse e)
        {
            if (e.HasError)
            {
                ReconnectToHost();
                return;
            }

            switch (e.Type)
            {
            case PacketType.UserLogin:
                RUserLogin userLogin = (RUserLogin)e;
                _sessionToken = userLogin.SessionToken;
                ShortSize screenRes = new ShortSize
                                      (
                    Screen.PrimaryScreen.Bounds.Width,
                    Screen.PrimaryScreen.Bounds.Height
                                      );
                TCPClientDataSend(new PUserEnter
                {
                    SessionToken = _sessionToken,
                    ScreenRes    = screenRes
                });
                break;

            case PacketType.UserEnter:
                lock (_playersData)
                {
                    RUserEnter userEnter = (RUserEnter)e;      //!!!
                    _map.Setup(userEnter.MapSize);
                    _map.AppendMapData(userEnter.MapData, userEnter.MapWindow);
                    _map.AppendMiniMapData(userEnter.MiniMapData, userEnter.MiniMapSize);
                    OnMapChanged();

                    foreach (Player p in userEnter.PlayersOnLocation)
                    {
                        if (!_playersData.ContainsKey(p.Id))
                        {
                            _playersData.Add(p.Id, new PlayerDataEx(p));
                        }
                    }
                    _myPlayerData = _playersData[userEnter.MyPlayerId];
                    _playersRo    = new ReadOnlyCollection <PlayerDataEx>(_playersData.Values.ToArray());
                }
                CenterTo((int)(_myPlayerData.Player.Position.X * _scaleFactor),    //!!!
                         (int)(_myPlayerData.Player.Position.Y * _scaleFactor),
                         true);
                break;

            case PacketType.PlayerEnter:
                lock (_playersData)
                {
                    RPlayerEnter playerEnter = (RPlayerEnter)e;
                    if (!_playersData.ContainsKey(playerEnter.Player.Id) && _myPlayerData != null &&
                        playerEnter.Player.Id != _myPlayerData.Player.Id)
                    {
                        _playersData.Add(playerEnter.Player.Id, new PlayerDataEx(playerEnter.Player));
                        _playersRo        = new ReadOnlyCollection <PlayerDataEx>(_playersData.Values.ToArray());
                        _somethingChanged = true;     //!!!
                    }
                }
                break;

            case PacketType.PlayerExit:
                lock (_playersData)
                {
                    RPlayerExit playerExit = (RPlayerExit)e;
                    if (_playersData.ContainsKey(playerExit.PlayerId) && _myPlayerData != null &&
                        playerExit.PlayerId != _myPlayerData.Player.Id)
                    {
                        _playersData.Remove(playerExit.PlayerId);
                        _playersRo        = new ReadOnlyCollection <PlayerDataEx>(_playersData.Values.ToArray());
                        _somethingChanged = true;     //!!!
                    }
                }
                break;

            case PacketType.PlayerRotate:
                lock (_playersData)
                {
                    PlayerDataEx  playerData;
                    RPlayerRotate playerRotate = (RPlayerRotate)e;
                    if (_playersData.TryGetValue(playerRotate.PlayerId, out playerData))
                    {
                        playerData.Player.Angle = playerRotate.Angle;
                    }
                }
                break;

            case PacketType.PlayerMove:
                lock (_playersData)
                {
                    PlayerDataEx playerData;
                    RPlayerMove  playerMove = (RPlayerMove)e;
                    if (_playersData.TryGetValue(playerMove.PlayerId, out playerData))
                    {
                        if (playerMove.Direction == Direction.None)
                        {
                            playerData.StopMoving(playerMove.Position);
                        }
                        else
                        {
                            playerData.StartMoving(playerMove.Position, playerMove.Direction);
                        }
                    }
                }
                break;

            case PacketType.MapData:
                RMapData mapData = (RMapData)e;
                _map.AppendMapData(mapData.MapData, mapData.MapWindow);     //!!!
                _myPlayerData.GettingMapWindow = false;
                break;
            }
        }