Exemple #1
0
        public Nod GetPositionAtTile(uint xTile, uint yTile)
        {
            Nod nod = new Nod();

            nod.X = X_MULTIPLIKATOR * TILESIZE_X * GRIDSIZE + xTile * GRIDSIZE;
            nod.Y = Y_MULTIPLIKATOR * TILESIZE_Y * GRIDSIZE + yTile * GRIDSIZE;
            return(nod);
        }
Exemple #2
0
 private void InitSpawn()
 {
     for (int i = 0; i < entityCount; i++)
     {
         Nod start = spawn.SearchFreeNode();
         if (start != null)
         {
             Database.Mob mob = new Database.Mob(start, this, 1);
             world.AddMonster(mob);
         }
     }
 }
Exemple #3
0
        public Nod GetTileAddress(uint xPos, uint yPos)
        {
            if (xPos < mapMinX || xPos > mapMaxX || yPos < mapMinY || yPos > mapMaxY)
            {
                return(null);
            }
            Nod nod = new Nod();

            nod.X = (xPos - (X_MULTIPLIKATOR * TILESIZE_X * GRIDSIZE)) / GRIDSIZE;
            nod.Y = (yPos - (Y_MULTIPLIKATOR * TILESIZE_Y * GRIDSIZE)) / GRIDSIZE;
            //Output.WriteLine(ConsoleColor.Cyan, "Position at [" + x.ToString() + "," + y.ToString() + "] = [" + nod.X.ToString() + "," + nod.Y.ToString() + "]");
            return(nod);
        }
Exemple #4
0
 public void Respawn()
 {
     while (true)
     {
         Nod start = spawn.SearchFreeNode();
         if (start != null)
         {
             Database.Mob mob = new Database.Mob(start, this, 1);
             //Output.WriteLine("Spawn::Respawn Mob ID: " + mob.InternalID.ToString());
             world.AddMonster(mob);
             break;
         }
     }
 }
Exemple #5
0
        public List <Database.Mob> MobsInSightRange(int x, int y, uint sightRange)
        {
            Map.Nod             nod      = mapa.GetTileAddress((uint)x, (uint)y);
            List <int>          mobsList = mapa.GetAllMobsAtRange(nod, DEBUG_SIGHT_RANGE);
            List <Database.Mob> mobsL    = new List <Database.Mob>(mobsList.Capacity);

            Database.Mob m;
            foreach (int i in mobsList)
            {
                m = null;
                monsters.TryGetValue(i, out m);
                if (m != null)
                {
                    mobsL.Add(m);
                }
            }
            return(mobsL);
        }
Exemple #6
0
 public bool RemoveEntityAtTile(Nod nod, int entityID, bool isPlayer)
 {
     if (isPlayer)
     {
         if (mapData[nod.X, nod.Y] != null)
         {
             mapData[nod.X, nod.Y].RemovePlayer(entityID);
             return(true);
         }
         return(false);
     }
     else
     {
         if (mapData[nod.X, nod.Y] != null)
         {
             mapData[nod.X, nod.Y].RemoveEntity(entityID);
             return(true);
         }
         return(false);
     }
 }
Exemple #7
0
 public bool AddEntityAtTile(Nod nod, int entityID, bool isPlayer)
 {
     if (nod.X > realX || nod.X < 0 || nod.Y > realY || nod.Y < 0)
     {
         Output.WriteLine("MapData::AddEntityAtTile Coordinates out of map!");
         return(false);
     }
     if (mapData[nod.X, nod.Y] == null)
     {
         return(false);
     }
     if (isPlayer)
     {
         mapData[nod.X, nod.Y].AddPlayer(entityID);
     }
     else
     {
         mapData[nod.X, nod.Y].AddEntity(entityID);
     }
     return(true);
 }
Exemple #8
0
        public List <Database.Player> PlayersInSightRange(int x, int y, uint sightRange)
        {
            Map.Nod nod = mapa.GetTileAddress((uint)x, (uint)y); ////////////////////////////////////////////////     CHECK FOR NULL RETURN  !!!!!!!!!
            if (nod == null)
            {
                List <Database.Player> nullList = new List <Database.Player>();
                return(nullList);
            }
            List <int>             playersList = mapa.GetAllPlayersAtRange(nod, DEBUG_SIGHT_RANGE);
            List <Database.Player> playersL    = new List <Database.Player>(playersList.Count);

            Database.Player p;
            foreach (int i in playersList)
            {
                p = null;
                players.TryGetValue(i, out p);
                if (p != null)
                {
                    playersL.Add(p);
                }
            }
            return(playersL);
        }
Exemple #9
0
        /* Look randomly for a free node in the map         *
        * until one is found where the MOB can spawn       */
        public Nod SearchFreeNode()
        {
            Nod start   = new Nod();
            int howLong = 0;

            while (true)
            {
                start.X = (uint)(rnd.Next(this.x, this.x + width));
                start.Y = (uint)(rnd.Next(this.y, this.y + height));
                if (map[start.X, start.Y] != null)
                {
                    if (map[start.X, start.Y].CountEntitys == 0)
                    {
                        return(start);
                    }
                }
                howLong++;
                if (howLong > 1000)
                {
                    Output.WriteLine("Spawn:SpawnArea::SearchFreeNode Took too long!");
                    while (true)
                    {
                        start.X = (uint)(rnd.Next(this.x, this.x + width));
                        start.Y = (uint)(rnd.Next(this.y, this.y + height));
                        if (map[start.X, start.Y] != null)
                        {
                            //for (int i = map[start.X, start.Y].CountEntitys - 1; i >= 0; i--)
                            //{
                            //    Output.WriteLine("   Enity ID: " + map[start.X, start.Y].EntitysIDs[i].ToString());
                            //}
                            return(start);
                        }
                    }
                    return(null);
                }
            }
        }
Exemple #10
0
        public List <int> DEBUG_GetAllPlayers()
        {
            Nod n = new Nod((uint)(realX / 2), (uint)(realY / 2));

            return(GetAllPlayersAtRange(n, (uint)realX));
        }
Exemple #11
0
        public List <int> GetAllMobsAtTile(uint x, uint y)
        {
            Nod n = new Nod(x, y);

            return(GetAllMobsAtRange(n, 0));
        }
Exemple #12
0
        public List <int> GetAllMobsAtRange(Nod nod, uint sightRange)
        {
            List <int> tmpList = new List <int>();
            uint       sx;
            uint       sy;
            uint       ex;
            uint       ey;

            if (sightRange > nod.X)
            {
                sx = 0;
            }
            else
            {
                sx = nod.X - sightRange;
                if (sx < 0)
                {
                    sx = 0;
                }
            }
            if (sightRange > nod.Y)
            {
                sy = 0;
            }
            else
            {
                sy = nod.Y - sightRange;
                if (sy < 0)
                {
                    sy = 0;
                }
            }
            ex = nod.X + sightRange;
            if (ex > realX)
            {
                ex = (uint)realX;
            }
            ey = nod.Y + sightRange;
            if (ey > realY)
            {
                ey = (uint)realY;
            }
            for (uint i = sx; i < ex; i++)
            {
                for (uint j = sy; j < ey; j++)
                {
                    if (i >= 0 && i < realX && j >= 0 && j < realY)
                    {
                        if (mapData[i, j] != null)
                        {
                            tmpList.AddRange(mapData[i, j].EntitysIDs);
                        }
                    }
                    else
                    {
                        Output.WriteLine(ConsoleColor.Red, "MapData::GetAllMobsAtRange INDEX OUT OF BOUND " + i.ToString() + "," + j.ToString());
                    }
                }
            }
            HashSet <int> tmHash = new HashSet <int>(tmpList);

            tmpList = new List <int>(tmHash);
            return(tmpList);
        }
Exemple #13
0
        public List <int> GetAllMobsAtRange(uint x, uint y, uint sightRange)
        {
            Nod n = new Nod(x, y);

            return(GetAllMobsAtRange(n, sightRange));
        }
Exemple #14
0
        public List <int> GetAllPlayersAtRange(Nod nod, uint sightRange)
        {
            //Output.WriteLine(ConsoleColor.Yellow, "Check from [" + nod.X.ToString() + "," + nod.Y.ToString() + "] Range: " + sightRange.ToString());
            List <int> tmpList = new List <int>();
            uint       sx;
            uint       sy;
            uint       ex;
            uint       ey;

            if (sightRange > nod.X)
            {
                sx = 0;
            }
            else
            {
                sx = nod.X - sightRange;
                if (sx < 0)
                {
                    sx = 0;
                }
            }
            if (sightRange > nod.Y)
            {
                sy = 0;
            }
            else
            {
                sy = nod.Y - sightRange;
                if (sy < 0)
                {
                    sy = 0;
                }
            }
            ex = nod.X + sightRange;
            if (ex > realX)
            {
                ex = (uint)realX;
            }
            ey = nod.Y + sightRange;
            if (ey > realY)
            {
                ey = (uint)realY;
            }
            //Output.WriteLine(ConsoleColor.Yellow, "Check after bounds FROM " + sx.ToString() + "," + sy.ToString() + " TO: " + ex.ToString() + "," + ey.ToString());
            for (uint i = sx; i < ex; i++)
            {
                for (uint j = sy; j < ey; j++)
                {
                    if (i >= 0 && i < mapSizeX && j >= 0 && j < mapSizeY)
                    {
                        if (mapData[i, j] != null)
                        {
                            tmpList.AddRange(mapData[i, j].PlayersIDs);
                        }
                    }
                    else
                    {
                        Output.WriteLine(ConsoleColor.Red, "MapData::GetAllPlayersAtRange INDEX OUT OF BOUND " + i.ToString() + "," + j.ToString());
                    }
                }
            }
            HashSet <int> tmHash = new HashSet <int>(tmpList);

            tmpList = new List <int>(tmHash);
            return(tmpList);

            /*
             * //FOR CIRCLE USE THIS BUT IT WILL SLOW DOWN PROCESS
             * //m1 and m2 are x,y of center radius
             * List<int> indices = new List<int>();
             * for (int x = 0; x < width; x++)
             * {
             *  for (int y = 0; y < height; y++)
             *  {
             *      double dx = x - m1;
             *      double dy = y - m2;
             *      double distanceSquared = dx * dx + dy * dy;
             *
             *      if (distanceSquared <= radiusSquared)
             *      {
             *          indices.Add(x + y * width);
             *      }
             *  }
             * }
             */
        }
Exemple #15
0
        public void MoveMobOnMap(Database.Mob mb, Map.Nod from, Map.Nod to)
        {
            //Output.WriteLine("MoveMobOnMap ID " + mb.InternalID.ToString() + " at [" + mb.PosX.ToString() + "," + mb.PosY.ToString() + "]");
            bool rem = mapa.RemoveEntityAtTile(from, mb.InternalID, false);

            if (rem)
            {
                //Output.WriteLine("Removed mob ID " + mb.InternalID.ToString() + " at [" + from.X.ToString() + "," + from.Y.ToString() + "]");
            }
            else
            {
                Output.WriteLine("FAILED to remove mob ID " + mb.InternalID.ToString() + " at [" + from.X.ToString() + "," + from.Y.ToString() + "]");
            }
            if (!mapa.AddEntityAtTile(to, mb.InternalID, false))
            {
                //Output.WriteLine("Fail add mob ID " + mb.InternalID.ToString() + " at [" + to.X.ToString() + "," + to.Y.ToString() + "]");
                mb.Despawn(null);
                return;
            }

            /*
             * else
             * {
             *  //Output.WriteLine("Added mob ID " + mb.InternalID.ToString() + " at [" + to.X.ToString() + "," + to.Y.ToString() + "]");
             *  //send info to all players in sight range about new mob spawn
             *  List<int> playersList = mapa.GetAllPlayersAtRange(to, DEBUG_SIGHT_RANGE);
             *  Database.Player p;
             *  foreach (int i in playersList)
             *  {
             *      if (!mb.ContainsPlayer(i))//this is new player in range so send spawn and add to known list
             *      {
             *          p = null;
             *          players.TryGetValue(i, out p);
             *          if (p != null )
             *          {
             *              mb.AddPlayer(i);
             *              //p.Con.Send(new Packet.SendPacketHandlers.MobSpawn(mb));//newly added player data!
             *              //if (mb.moving)//mob is moving so send this info to new player in range
             *              //{
             *              //    p.Con.Send(new Packet.SendPacketHandlers.MobMoveStart(mb.InternalID, mb.newX, mb.newY));//newly added player data!
             *              //}
             *          }
             *      }
             *  }
             *  List<int> oldPlayersList = mb.KnownPlayers();
             *  foreach (int i in oldPlayersList)
             *  {
             *      if (!playersList.Contains(i))//this player isn't in range so send despawn
             *      {
             *          p = null;
             *          players.TryGetValue(i, out p);
             *          if (p != null)
             *          {
             *              mb.RemovePlayer(i);
             *              //p.Con.Send(new Packet.SendPacketHandlers.MobDespawn(mb.InternalID));//newly added player data!
             *          }
             *      }
             *  }
             * }
             */
        }
Exemple #16
0
        public void MovePlayerOnMap(Database.Player pl, Map.Nod from, Map.Nod to)
        {
            //Output.WriteLine("World::MovePlayerOnMap From [" + from.X.ToString() + "," + from.Y.ToString() + "] To [" + to.X.ToString() + "," + to.Y.ToString() + "]");
            mapa.RemoveEntityAtTile(from, pl.PlayerPID, true);
            if (!mapa.AddEntityAtTile(to, pl.PlayerPID, true))
            {
                pl.Con.Close();
                return;
            }

            /*
             * else
             * {
             *  //send info to all players in sight range about new player spawn
             *  List<int> playersList = mapa.GetAllPlayersAtRange(to, DEBUG_SIGHT_RANGE);
             *  Database.Player p;
             *  foreach (int i in playersList)
             *  {
             *      if (!pl.ContainsPlayer(i))//this is new player in range so send spawn and add to known list
             *      {
             *          p = null;
             *          players.TryGetValue(i, out p);
             *          if (p != null && p.PlayerPID != pl.PlayerPID)
             *          {
             *              pl.AddPlayer(i);
             *              p.Con.Send(new Packet.SendPacketHandlers.PlayerSpawn(pl.Con));//newly added player data!
             *              //if (pl.moving)//player is moving so send this info to new player in range
             *              //{
             *              //    p.Con.Send(new Packet.SendPacketHandlers.MoveStart(pl.PlayerPID, pl.newX, pl.newY));//newly added player data!
             *              //}
             *          }
             *      }
             *  }
             *  List<int> oldPlayersList = pl.KnownPlayers();
             *  foreach (int i in oldPlayersList)
             *  {
             *      if (!playersList.Contains(i))//this player isn't in range so send despawn
             *      {
             *          p = null;
             *          players.TryGetValue(i, out p);
             *          if (p != null && p.PlayerPID != pl.PlayerPID)
             *          {
             *              pl.RemovePlayer(i);
             *              pl.Con.Send(new Packet.SendPacketHandlers.PlayerDespawn(i));//newly added player data!
             *          }
             *      }
             *  }
             *  //check for new mobs in range
             *  List<int> mobList = mapa.GetAllMobsAtRange(to, DEBUG_SIGHT_RANGE);
             *  Database.Mob m;
             *  foreach (int i in mobList)
             *  {
             *      if (!pl.ContainsMob(i))//this is new mob in range so send spawn and add to known list
             *      {
             *          m = null;
             *          monsters.TryGetValue(i, out m);
             *          if (m != null)
             *          {
             *              pl.AddMob(i);
             *              m.AddPlayer(pl.PlayerPID);
             *              pl.Con.Send(new Packet.SendPacketHandlers.MobSpawn(m));//newly added player data!
             *              //if (m.moving)//mob is moving so send this info to new player in range
             *              //{
             *              //    pl.Con.Send(new Packet.SendPacketHandlers.MobMoveStart(m.InternalID, m.newX, m.newY));//newly added player data!
             *              //}
             *          }
             *      }
             *  }
             *  List<int> oldMobsList = pl.KnownMobs();
             *  foreach (int i in oldMobsList)
             *  {
             *      if (!mobList.Contains(i))//this mob isn't in range so send despawn
             *      {
             *          m = null;
             *          monsters.TryGetValue(i, out m);
             *          if (m != null)
             *          {
             *              pl.RemoveMob(i);
             *              pl.Con.Send(new Packet.SendPacketHandlers.MobDespawn(i));//newly added player data!
             *          }
             *      }
             *  }
             * }
             */
        }
Exemple #17
0
        public bool AddPlayer(Database.Player player)
        {
            Output.WriteLine("New player at position: " + player.PosX.ToString() + "," + player.PosY.ToString());
            Map.Nod nod = mapa.GetTileAddress((uint)player.PosX, (uint)player.PosY);
            player.MapPosition    = nod;
            player.OldMapPosition = new Map.Nod(nod.X, nod.Y);
            if (nod != null)
            {
                Output.WriteLine("Adding player at position: " + nod.X.ToString() + "," + nod.Y.ToString());
            }
            else
            {
                Output.WriteLine(ConsoleColor.Red, "Adding player at position: GetTileAddress return NULL !");
                player.Con.Close();
                return(false);
            }
            //Database.Player pl = new Database.Player(con, nod);
            try
            {
                //players.Add(pl.UniqueID, pl);
                players.TryAdd(player.PlayerPID, player);
            }
            catch (ArgumentException e)
            {
                Database.Player tp = null;
                //players.TryGetValue(pl.UniqueID, out tp);
                //players.Remove(pl.UniqueID);
                players.TryRemove(player.PlayerPID, out tp);
                if (tp != null)
                {
                    tp.Con.SendAsync(new Packet.SendPacketHandlers.LoginError(Packet.SendPacketHandlers.LOGIN_ERROR.CURRENTLY_LOGGED));
                    tp.Con.Close();
                }
                AddPlayer(player);
                return(true);
            }
            //Output.WriteLine("Adding player at position: " + nod.X.ToString() + "," + nod.Y.ToString());
            if (!mapa.AddEntityAtTile(player.MapPosition, player.PlayerPID, true))
            {
                player.Con.Close();
                return(false);
            }

            /*
             * else
             * {
             *  //send info to all players in sight range about new player spawn
             *  List<int> playersList = mapa.GetAllPlayersAtRange(player.MapPosition, DEBUG_SIGHT_RANGE);
             *  Database.Player p;
             *  foreach (int i in playersList)
             *  {
             *      p = null;
             *      players.TryGetValue(i, out p);
             *      if (p != null && p.PlayerPID != player.PlayerPID)
             *      {
             *          player.AddPlayer(i);
             *          p.Con.Send(new Packet.SendPacketHandlers.PlayerSpawn(player.Con));//newly added player data!
             *      }
             *  }
             *  //inform all mobs in sight range about new player spawn
             *  List<int> mobsList = mapa.GetAllMobsAtRange(player.MapPosition, DEBUG_SIGHT_RANGE);
             *  Database.Mob m;
             *  foreach (int i in mobsList)
             *  {
             *      m = null;
             *      monsters.TryGetValue(i, out m);
             *      if (m != null)
             *      {
             *          m.AddPlayer(player.PlayerPID);
             *      }
             *  }
             * }
             */
            return(true);
        }
Exemple #18
0
 public List <int> GetMobsAtRange(Map.Nod position, uint sightRange)
 {
     return(mapa.GetAllMobsAtRange(position, sightRange));
 }