Esempio n. 1
0
        public static ShortPoint? FindPlayerStartPoint(ServerMap map, Player player,
            int minAreaSpace)
        {
            var areas = map.SpaceAreas;
            int iStart = _rnd.Next(areas.Count), iEnd = areas.Count;

            bool secondIter = false;
            for (int i = iStart; i <= iEnd; i++)
            {
                if (i == iEnd)
                {
                    if (secondIter)
                    {
                        return null;
                    }
                    i = 0;
                    iEnd = iStart - 1;
                    secondIter = true;
                }
                MapArea area = areas[i];
                if (area.CellsCount * ConstMap.PIXEL_SIZE_SQR >= minAreaSpace)
                {
                    int playerMargin = (int)Math.Floor((float)player.Size.HighValue / ConstMap.PIXEL_SIZE);
                    ShortPoint? cell = area.FindFreeCell(map, playerMargin, _rnd);
                    if (cell != null)
                    {
                        return cell;
                    }
                }
            }

            return null;
        }
Esempio n. 2
0
 public PlayerDataEx(Player player, Dictionary<int, ServerMap> maps)
     : base(player)
 {
     if (!maps.TryGetValue(player.MapId, out Map))
     {
         throw new Exception(string.Format("Player [{0}] has a wrong map ID", player.Id));
     }
 }
Esempio n. 3
0
 public static Player Create(string name)
 {
     Player player = new Player
     {
         Name = name,
         Size = new TinySize(32, 32),
         Health = 100,
         Speed = 10f
     };
     player.SetNewId();
     return player;
 }
Esempio n. 4
0
 private static Point CalculateNewPlayerPos(Player player, int traveled)
 {
     Point newPos;
     Point startPos = player.Position;
     switch (player.Direction)
     {
         case Direction.N:
             newPos = new Point(startPos.X, startPos.Y - traveled);
             break;
         case Direction.NW:
             newPos = new Point(startPos.X - traveled, startPos.Y - traveled);
             break;
         case Direction.NE:
             newPos = new Point(startPos.X + traveled, startPos.Y - traveled);
             break;
         case Direction.S:
             newPos = new Point(startPos.X, startPos.Y + traveled);
             break;
         case Direction.SW:
             newPos = new Point(startPos.X - traveled, startPos.Y + traveled);
             break;
         case Direction.SE:
             newPos = new Point(startPos.X + traveled, startPos.Y + traveled);
             break;
         case Direction.W:
             newPos = new Point(startPos.X - traveled, startPos.Y);
             break;
         case Direction.E:
             newPos = new Point(startPos.X + traveled, startPos.Y);
             break;
         default:
             newPos = default(Point);
             break;
     }
     return newPos;
 }
Esempio n. 5
0
        private void RegisterWorldResponseForNearest(Player player, BaseResponse response)
        {
            List<Player> nearestPlayers = World.PlayersGetNearest(player);
            int cnt = nearestPlayers.Count;
            if (cnt > 0)
            {
                lock (_responsesPool)
                {
                    for (int i = 0; i < cnt; i++)
                    {
                        Pair<int, BaseResponse> item = _responsesPool.PopExpandAsync();
                        item.Key = nearestPlayers[i].Id;
                        item.Value = response;
                        _responses.Add(item);
//                        _responses.Add(new Pair<int, BaseResponse>(nearestPlayers[i].Id, response));
                    }
                    _unsentResponsesAvailable = true;
                }
            }
        }
Esempio n. 6
0
 private bool IsCloseToBorder(ShortRect window, ShortPoint playerPos, Player player,
     float borderAreaSpacePart)
 {
     int horzSpace = (int) (window.Width/borderAreaSpacePart);
     int vertSpace = (int) (window.Height/borderAreaSpacePart);
     return (playerPos.X <= window.X + horzSpace && playerPos.X - horzSpace >= 0 &&
             (player.Direction == D.W || player.Direction == D.NW || player.Direction == D.SW)) ||
            (playerPos.X >= window.X + window.Width - horzSpace && playerPos.X + horzSpace < _width &&
             (player.Direction == D.E || player.Direction == D.NE || player.Direction == D.SE)) ||
            (playerPos.Y <= window.Y + vertSpace && playerPos.Y - vertSpace >= 0 &&
             (player.Direction == D.N || player.Direction == D.NE || player.Direction == D.NW)) ||
            (playerPos.Y >= window.Y + window.Height - vertSpace && playerPos.Y + vertSpace < _height &&
             (player.Direction == D.S || player.Direction == D.SE || player.Direction == D.SW));
 }
Esempio n. 7
0
 public bool NeedsToLoadMapWindow(Player player, float borderAreaSpacePart)
 {
     ShortPoint playerPos = player.Position.ToShortPoint(ConstMap.PIXEL_SIZE);
     foreach (ShortRect window in _windows)
     {
         if (!window.Contains(playerPos)) continue;
         if (!IsCloseToBorder(window, playerPos, player, borderAreaSpacePart)) return false;
     }
     return true;
 }
Esempio n. 8
0
 public PlayerDataEx(Player player)
     : base(player)
 {
 }
Esempio n. 9
0
        private void UpdateScreenPosByPlayerPos(Player player)
        {
            int wPart = (int)(Width / BORDER_AREA_SPACE_PART);
            int leftWinBorder = _posX + wPart;
            int rightWinBorder = _posX + Width - wPart;
            int hPart = (int)(Height / BORDER_AREA_SPACE_PART);
            int topWinBorder = _posY + hPart;
            int bottomWinBorder = _posY + Height - hPart;

            float pSizeW = player.Size.Width * _scaleFactor;
            float pSizeH = player.Size.Height * _scaleFactor;
            float nPosX = player.Position.X * _scaleFactor;
            float nPosY = player.Position.Y * _scaleFactor;

            if (nPosX + pSizeW > rightWinBorder)
            {
                PosX = (int)(nPosX - (wPart * 2) + pSizeW);
            }
            else if (nPosX < leftWinBorder)
            {
                PosX = (int)(nPosX - wPart);
            }
            if (nPosY + pSizeH > bottomWinBorder)
            {
                PosY = (int)(nPosY - (hPart * 2) + pSizeH);
            }
            else if (nPosY < topWinBorder)
            {
                PosY = (int)(nPosY - hPart);
            }
        }
Esempio n. 10
0
 public ValidationInfo(Player player)
 {
     _lastX = player.Position.X;
     _lastY = player.Position.Y;
     _lastMoveTime = DateTime.Now;
     _lastPlayerSpeed = player.Speed;
 }
Esempio n. 11
0
        private static void PlayerPosProcessCollision(Player player, IBaseMap map, ref Point newPos)
        {
            Point pos = player.Position;
            TinySize size = player.Size;
            bool stop = false, xStop = false, yStop = false;

            int mapWidth = map.Width, mapHeight = map.Height;

            int cellTs = (int)Math.Floor((float)pos.Y / ConstMap.PIXEL_SIZE);
            int cellTe = (int)Math.Floor((float)newPos.Y / ConstMap.PIXEL_SIZE);
            int cellLs = (int)Math.Floor((float)pos.X / ConstMap.PIXEL_SIZE);
            int cellLe = (int)Math.Floor((float)newPos.X / ConstMap.PIXEL_SIZE);

            int cellBs = (int)Math.Floor((float)(pos.Y + size.Height - 1) / ConstMap.PIXEL_SIZE);
            int cellBe = (int)Math.Floor((float)(newPos.Y + size.Height - 1) / ConstMap.PIXEL_SIZE);
            int cellRs = (int)Math.Floor((float)(pos.X + size.Width - 1) / ConstMap.PIXEL_SIZE);
            int cellRe = (int)Math.Floor((float)(newPos.X + size.Width - 1) / ConstMap.PIXEL_SIZE);

            int cellT = (int)Math.Floor((float)pos.Y / ConstMap.PIXEL_SIZE);
            int cellB = (int)Math.Floor((float)(pos.Y + size.Height - 1) / ConstMap.PIXEL_SIZE);

            int cellL = (int)Math.Floor((float)pos.X / ConstMap.PIXEL_SIZE);
            int cellR = (int)Math.Floor((float)(pos.X + size.Width - 1) / ConstMap.PIXEL_SIZE);

            switch (player.Direction)
            {
                case Direction.N:
                    {
                        for (int y = cellTs; y >= cellTe; y--)
                        {
                            if (y < 0 || y >= mapHeight || stop) break;
                            for (int x = cellL; x <= cellR; x++)
                            {
                                if (x < 0 || x >= mapWidth) continue;
                                if ((*map[(ushort)x, (ushort)y]).Type != TileType.Nothing)
                                {
                                    newPos.Y = y * ConstMap.PIXEL_SIZE + ConstMap.PIXEL_SIZE;
                                    stop = true;
                                    break;
                                }
                            }
                        }
                        break;
                    }
                case Direction.NW:
                    {
                        int tTotalDist = cellTs - cellTe;
                        int lTotalDist = cellLs - cellLe;
                        int maxTotalDist = Math.Max(tTotalDist, lTotalDist);

                        for (int i = 1; i <= maxTotalDist; i++)
                        {
                            if (xStop && yStop) break;

                            cellT--; cellB--; cellL--; cellR--;

                            for (int y = cellT; y <= cellB; y++)
                            {
                                if (y < 0 || y >= mapHeight) continue;

                                //Y
                                if (y == cellT && i <= tTotalDist)
                                {
                                    for (int x = cellL; x <= cellR; x++)
                                    {
                                        if (x < 0 || x >= mapWidth) continue;
                                        if (!yStop && (*map[(ushort)(x + 1), (ushort)cellT]).Type != TileType.Nothing)
                                        {
                                            newPos.Y = cellT * ConstMap.PIXEL_SIZE + ConstMap.PIXEL_SIZE;
                                            yStop = true;
                                            break;
                                        }
                                    }
                                }

                                //X
                                if (!xStop && i <= lTotalDist &&
                                    (*map[(ushort)cellL, (ushort)(y + 1)]).Type != TileType.Nothing)
                                {
                                    newPos.X = cellL * ConstMap.PIXEL_SIZE + ConstMap.PIXEL_SIZE;
                                    xStop = true;
                                    break;
                                }

                                if (xStop && yStop) break;
                            }
                        }
                        break;
                    }
                case Direction.NE:
                    {
                        int tTotalDist = cellTs - cellTe;
                        int rTotalDist = cellRe - cellRs;
                        int maxTotalDist = Math.Max(tTotalDist, rTotalDist);

                        for (int i = 1; i <= maxTotalDist; i++)
                        {
                            if (xStop && yStop) break;

                            cellT--; cellB--; cellL++; cellR++;

                            for (int y = cellT; y <= cellB; y++)
                            {
                                if (y < 0 || y >= mapHeight) continue;

                                //Y
                                if (y == cellT && i <= tTotalDist)
                                {
                                    for (int x = cellL; x <= cellR; x++)
                                    {
                                        if (x < 0 || x >= mapWidth) continue;
                                        if (!yStop && (*map[(ushort)(x - 1), (ushort)cellT]).Type != TileType.Nothing)
                                        {
                                            newPos.Y = cellT * ConstMap.PIXEL_SIZE + ConstMap.PIXEL_SIZE;
                                            yStop = true;
                                            break;
                                        }
                                    }
                                }

                                //X
                                if (!xStop && i <= rTotalDist &&
                                    (*map[(ushort)cellR, (ushort)(y + 1)]).Type != TileType.Nothing)
                                {
                                    newPos.X = cellR * ConstMap.PIXEL_SIZE - size.Width;
                                    xStop = true;
                                    break;
                                }

                                if (xStop && yStop) break;
                            }
                        }
                        break;
                    }
                case Direction.S:
                    {
                        for (int y = cellBs; y <= cellBe; y++)
                        {
                            if (y < 0 || y >= mapHeight || stop) break;
                            for (int x = cellL; x <= cellR; x++)
                            {
                                if (x < 0 || x >= mapWidth) continue;
                                if ((*map[(ushort)x, (ushort)y]).Type != TileType.Nothing)
                                {
                                    newPos.Y = y * ConstMap.PIXEL_SIZE - size.Height;
                                    stop = true;
                                    break;
                                }
                            }
                        }
                        break;
                    }
                case Direction.SW:
                    {
                        int bTotalDist = cellBe - cellBs;
                        int lTotalDist = cellLs - cellLe;
                        int maxTotalDist = Math.Max(bTotalDist, lTotalDist);

                        for (int i = 1; i <= maxTotalDist; i++)
                        {
                            if (xStop && yStop) break;

                            cellT++; cellB++; cellL--; cellR--;

                            for (int y = cellT; y <= cellB; y++)
                            {
                                if (y < 0 || y >= mapHeight) continue;

                                //Y
                                if (y == cellT && i <= bTotalDist)
                                {
                                    for (int x = cellL; x <= cellR; x++)
                                    {
                                        if (x < 0 || x >= mapWidth) continue;
                                        if (!yStop && (*map[(ushort)(x + 1), (ushort)cellB]).Type != TileType.Nothing)
                                        {
                                            newPos.Y = cellB * ConstMap.PIXEL_SIZE - size.Height;
                                            yStop = true;
                                            break;
                                        }
                                    }
                                }

                                //X
                                if (!xStop && i <= lTotalDist &&
                                    (*map[(ushort)cellL, (ushort)(y - 1)]).Type != TileType.Nothing)
                                {
                                    newPos.X = cellL * ConstMap.PIXEL_SIZE + ConstMap.PIXEL_SIZE;
                                    xStop = true;
                                    break;
                                }

                                if (xStop && yStop) break;
                            }
                        }
                        break;
                    }
                case Direction.SE:
                    {
                        int bTotalDist = cellBe - cellBs;
                        int rTotalDist = cellRe - cellRs;
                        int maxTotalDist = Math.Max(bTotalDist, rTotalDist);

                        for (int i = 1; i <= maxTotalDist; i++)
                        {
                            if (xStop && yStop) break;

                            cellT++; cellB++; cellL++; cellR++;

                            for (int y = cellT; y <= cellB; y++)
                            {
                                if (y < 0 || y >= mapHeight) continue;

                                //Y
                                if (y == cellT && i <= bTotalDist)
                                {
                                    for (int x = cellL; x <= cellR; x++)
                                    {
                                        if (x < 0 || x >= mapWidth) continue;
                                        if (!yStop && (*map[(ushort)(x - 1), (ushort)cellB]).Type != TileType.Nothing)
                                        {
                                            newPos.Y = cellB * ConstMap.PIXEL_SIZE - size.Height;
                                            yStop = true;
                                            break;
                                        }
                                    }
                                }

                                //X
                                if (!xStop && i <= rTotalDist &&
                                    (*map[(ushort)cellR, (ushort)(y - 1)]).Type != TileType.Nothing)
                                {
                                    newPos.X = cellR * ConstMap.PIXEL_SIZE - size.Width;
                                    xStop = true;
                                    break;
                                }

                                if (xStop && yStop) break;
                            }
                        }
                        break;
                    }
                case Direction.W:
                    {
                        for (int x = cellLs; x >= cellLe; x--)
                        {
                            if (x < 0 || x >= mapWidth || stop) break;
                            for (int y = cellT; y <= cellB; y++)
                            {
                                if (y < 0 || y >= mapHeight) continue;
                                if ((*map[(ushort)x, (ushort)y]).Type != TileType.Nothing)
                                {
                                    newPos.X = x * ConstMap.PIXEL_SIZE + ConstMap.PIXEL_SIZE;
                                    stop = true;
                                    break;
                                }
                            }
                        }
                        break;
                    }
                case Direction.E:
                    {
                        for (int x = cellRs; x <= cellRe; x++)
                        {
                            if (x < 0 || x >= mapWidth || stop) break;
                            for (int y = cellT; y <= cellB; y++)
                            {
                                if (y < 0 || y >= mapHeight) continue;
                                if ((*map[(ushort)x, (ushort)y]).Type != TileType.Nothing)
                                {
                                    newPos.X = x * ConstMap.PIXEL_SIZE - size.Width;
                                    stop = true;
                                    break;
                                }
                            }
                        }
                        break;
                    }
            }
        }
Esempio n. 12
0
 public ShortPoint? MapFindPlayerStartPoint(Player player)
 {
     ServerMap map;
     return _maps.TryGetValue(player.MapId, out map)
         ? Game.FindPlayerStartPoint(map, player, MIN_FREE_AREA_SIZE)
         : null;
 }
Esempio n. 13
0
 public List<Player> PlayersGetNearest(Player player)
 {
     lock (_players)
     {
         List<Player> players = new List<Player>();
         int playersCnt = _players.Count;
         for (int i = 0; i < playersCnt; i++)
         {
             PlayerDataEx p = _players[i];
             if (p.Player.MapId == player.MapId &&
                 p.Player.Position.CloseTo(player.Position, NEAREST_AREA_HALF_SIZE))
             {
                 players.Add(p.Player);
             }
         }
         return players;
     }
 }
Esempio n. 14
0
 public bool PlayerAdd(long sessionToken, Player player)
 {
     if (!_sessionPlayers.ContainsKey(sessionToken))
     {
         PlayerDataEx playerData = new PlayerDataEx(player, _maps);
         lock (_sessionPlayers) lock (_players)
         {
             _players.Add(playerData);
             _sessionPlayers.Add(sessionToken, playerData);
             return true;
         }
     }
     return false;
 }
Esempio n. 15
0
 public PlayerData(Player player)
 {
     Player = player;
 }