Exemplo n.º 1
0
        internal void NpcLife()
        {
            double time = (DateTime.Now - LastEffect).TotalMilliseconds;

            if (Effect > 0 && time > EffectDelay)
            {
                Map.Broadcast(GenerateEff());
                LastEffect = DateTime.Now;
            }

            Random random = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);

            time = (DateTime.Now - LastMove).TotalSeconds;
            if (IsMoving && time > 1.3 * random.Next(1, 3) * (0.5 + random.NextDouble()))
            {
                byte point  = (byte)random.Next(2, 4);
                byte fpoint = (byte)random.Next(0, 2);

                byte xpoint = (byte)random.Next(fpoint, point);
                byte ypoint = (byte)(point - xpoint);

                short mapX = FirstX;
                short mapY = FirstY;
                if (ServerManager.GetMap(MapId).GetFreePosition(ref mapX, ref mapY, xpoint, ypoint))
                {
                    this.MapX = mapX;
                    this.MapY = mapY;
                    LastMove  = DateTime.Now;

                    string movePacket = $"mv 2 {this.MapNpcId} {this.MapX} {this.MapY} {Npc.Speed}";
                    Map.Broadcast(movePacket);
                }
            }
        }
Exemplo n.º 2
0
        public void DropItemByMonster(DropDTO drop, short mapX, short mapY)
        {
            Random  rnd         = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
            int     random      = 0;
            MapItem droppedItem = null;
            short   MapX        = (short)(rnd.Next(mapX - 1, mapX + 1));
            short   MapY        = (short)(rnd.Next(mapY - 1, mapY + 1));

            while (IsBlockedZone(MapX, MapY))
            {
                MapX = (short)(rnd.Next(mapX - 1, mapX + 1));
                MapY = (short)(rnd.Next(mapY - 1, mapY + 1));
            }

            ItemInstance newInstance = InventoryList.CreateItemInstance(drop.ItemVNum, 0);

            newInstance.Amount = drop.Amount;

            droppedItem = new MapItem(MapX, MapY, true)
            {
                ItemInstance = newInstance
            };

            while (ServerManager.GetMap(MapId).DroppedList.ContainsKey(random = rnd.Next(1, 999999)))
            {
            }
            droppedItem.ItemInstance.ItemInstanceId = random;
            ServerManager.GetMap(MapId).DroppedList.Add(random, droppedItem);

            Broadcast($"drop {droppedItem.ItemInstance.ItemVNum} {random} {droppedItem.PositionX} {droppedItem.PositionY} {droppedItem.ItemInstance.Amount} 0 0 -1");
        }
Exemplo n.º 3
0
        public List <String> Generatein2()
        {
            List <String> in2List = new List <String>();

            foreach (Npc npc in ServerManager.GetMap(this.MapId).Npcs)
            {
                in2List.Add(String.Format("in 2 {0} {1} {2} {3} {4} 100 100 9632 0 0 - 1 1 0 - 1 - 0 - 1 0 0 0 0 0 0 0 0", npc.Vnum, npc.NpcId, npc.MapX, npc.MapY, npc.Position));
            }
            return(in2List);
        }
Exemplo n.º 4
0
        public List <string> GenerateShopOnMap()
        {
            List <String> droplist = new List <String>();

            foreach (KeyValuePair <long, MapShop> shop in ServerManager.GetMap(this.MapId).ShopUserList)
            {
                droplist.Add(String.Format("shop 1 {0} 1 3 0 {1}", shop.Key + 1, shop.Value.Name));
            }
            return(droplist);
        }
Exemplo n.º 5
0
        public List <String> GenerateGp()
        {
            List <String> gpList = new List <String>();

            foreach (Portal portal in ServerManager.GetMap(this.MapId).Portals)
            {
                gpList.Add(String.Format("gp {0} {1} {2} {3} {4}", portal.SourceX, portal.SourceY, portal.DestinationMapId, portal.Type, 0));
            }
            return(gpList);
        }
Exemplo n.º 6
0
        public List <string> GenerateDroppedItem()
        {
            List <String> droplist = new List <String>();

            foreach (KeyValuePair <long, MapItem> item in ServerManager.GetMap(this.MapId).DroppedList)
            {
                droplist.Add(String.Format("drop {0} {1} {2} {3} {4} {5} {6}", item.Value.ItemVNum, item.Key, item.Value.PositionX, item.Value.PositionY, item.Value.Amount, 0, -1));
            }
            return(droplist);
        }
Exemplo n.º 7
0
        public List <string> GenerateNPCShopOnMap()
        {
            List <String> droplist = new List <String>();

            foreach (Npc npc in ServerManager.GetMap(this.MapId).Npcs)
            {
                if (npc.Shop != null)
                {
                    droplist.Add(String.Format("shop 2 {0} {1} {2} {3} {4}", npc.NpcId, 1, 0, npc.Shop.MenuType, npc.Shop.Name));
                }
            }
            return(droplist);
        }
Exemplo n.º 8
0
        public async void TaskControl()
        {
            Task TaskMap;

            while (true)
            {
                foreach (var GroupedSession in Sessions.Where(s => s.Character != null).GroupBy(s => s.Character.MapId))
                {
                    foreach (ClientSession Session in GroupedSession)
                    {
                        TaskMap = new Task(() => ServerManager.GetMap(Session.Character.MapId).MapTaskManager());
                        TaskMap.Start();
                    }
                }
                await Task.Delay(300);
            }
        }
Exemplo n.º 9
0
        internal void NpcLife()
        {
            LifeTaskIsRunning = true;
            NpcMonster npc = ServerManager.GetNpc(this.NpcVNum);

            if (npc == null)
            {
                LifeTaskIsRunning = false;
                return;
            }
            double time = (DateTime.Now - LastEffect).TotalMilliseconds;

            if (Effect > 0 && time > EffectDelay + 1000)
            {
                Map.Broadcast(GenerateEff());
                LastEffect = DateTime.Now;
            }

            Random r = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);

            time = (DateTime.Now - LastMove).TotalSeconds;
            if (IsMoving && time > r.Next(1, 3) * (0.5 + r.NextDouble()))
            {
                byte point  = (byte)r.Next(2, 5);
                byte fpoint = (byte)r.Next(0, 2);

                byte xpoint = (byte)r.Next(fpoint, point);
                byte ypoint = (byte)(point - xpoint);

                short mapX = FirstX;
                short mapY = FirstY;
                if (ServerManager.GetMap(MapId).GetFreePosition(ref mapX, ref mapY, xpoint, ypoint))
                {
                    this.MapX = mapX;
                    this.MapY = mapY;
                    LastMove  = DateTime.Now;

                    string movepacket = $"mv 2 {this.MapNpcId} {this.MapX} {this.MapY} {npc.Speed}";
                    Map.Broadcast(movepacket);
                }
            }
            LifeTaskIsRunning = false;
        }
Exemplo n.º 10
0
        public List <MapCellAStar> GetSuccessors()
        {
            List <MapCellAStar> successors = new List <MapCellAStar>();

            for (short xd = -1; xd <= 1; xd++)
            {
                for (short yd = -1; yd <= 1; yd++)
                {
                    if (!ServerManager.GetMap(MapId).IsBlockedZone(X + xd, Y + yd))
                    {
                        MapCellAStar n = new MapCellAStar(this, this._goalcell, (short)(X + xd), (short)(Y + yd), MapId);
                        if (!n.IsMatch(this.parentcell) && !n.IsMatch(this))
                        {
                            successors.Add(n);
                        }
                    }
                }
            }
            return(successors);
        }
Exemplo n.º 11
0
        //Map ??
        private async void TaskLauncherProcess()
        {
            Task TaskMap = null;

            while (true)
            {
                foreach (var GroupedSession in Sessions.Where(s => s.Character != null).GroupBy(s => s.Character.MapId))
                {
                    foreach (ClientSession Session in GroupedSession)
                    {
                        TaskMap = new Task(() => ServerManager.GetMap(Session.Character.MapId).MapTaskManager());
                        TaskMap.Start();
                    }
                }
                if (TaskMap != null)
                {
                    await TaskMap;
                }
                await Task.Delay(300);
            }
        }
Exemplo n.º 12
0
        internal void NpcLife()
        {
            double time = (DateTime.Now - LastEffect).TotalMilliseconds;

            if (Effect > 0 && time > EffectDelay)
            {
                ServerManager.GetMap(MapId).Broadcast(GenerateEff(), MapX, MapY, 10);
                LastEffect = DateTime.Now;
            }

            time = (DateTime.Now - LastMove).TotalMilliseconds;
            if (IsMoving && Npc.Speed > 0 && time > _movetime)
            {
                _movetime = _random.Next(500, 3000);
                byte point  = (byte)_random.Next(2, 4);
                byte fpoint = (byte)_random.Next(0, 2);

                byte xpoint = (byte)_random.Next(fpoint, point);
                byte ypoint = (byte)(point - xpoint);

                short mapX = FirstX;
                short mapY = FirstY;

                if (ServerManager.GetMap(MapId).GetFreePosition(ref mapX, ref mapY, xpoint, ypoint))
                {
                    Task.Factory.StartNew(async() =>
                    {
                        await Task.Delay(1000 * (xpoint + ypoint) / (2 * Npc.Speed));
                        this.MapX = mapX;
                        this.MapY = mapY;
                    });
                    LastMove = DateTime.Now.AddSeconds((xpoint + ypoint) / (2 * Npc.Speed));

                    string movePacket = $"mv 2 {this.MapNpcId} {this.MapX} {this.MapY} {Npc.Speed}";
                    ServerManager.GetMap(MapId).Broadcast(movePacket);
                }
            }
        }
Exemplo n.º 13
0
        internal void NpcLife()
        {
            NpcMonster npc = ServerManager.GetNpc(this.NpcVNum);

            if (npc == null)
            {
                return;
            }
            double time = (DateTime.Now - LastEffect).TotalMilliseconds;

            if (Effect > 0 && time > EffectDelay)
            {
                ClientLinkManager.Instance.RequiereBroadcastFromMap(MapId, GenerateEff());
                LastEffect = DateTime.Now;
            }
            time = (DateTime.Now - LastMove).TotalSeconds;
            if (Move && time > 2.2)
            {
                Random r = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);

                byte point = (byte)r.Next(2, 6);

                byte xpoint = (byte)r.Next(0, point);
                byte ypoint = (byte)(point - xpoint);

                short MapX = (short)r.Next(-xpoint + firstX, xpoint + firstX);
                short MapY = (short)r.Next(-ypoint + firstY, ypoint + firstY);
                if (!ServerManager.GetMap(MapId).IsBlockedZone(MapX, MapY))
                {
                    this.MapX = MapX;
                    this.MapY = MapY;
                    LastMove  = DateTime.Now;

                    string movepacket = $"mv 2 {this.MapNpcId} {this.MapX} {this.MapY} {npc.Speed}";
                    ClientLinkManager.Instance.RequiereBroadcastFromMap(MapId, movepacket);
                }
            }
        }
Exemplo n.º 14
0
        internal bool GetFreePosition(ref short firstX, ref short firstY, byte xpoint, byte ypoint)
        {
            Random r    = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
            short  MinX = (short)(-xpoint + firstX);
            short  MaxX = (short)(xpoint + firstX);

            short MinY = (short)(-ypoint + firstY);
            short MaxY = (short)(ypoint + firstY);

            List <MapCell> cells = new List <MapCell>();

            for (short y = MinY; y <= MaxY; y++)
            {
                for (short x = MinX; x <= MaxX; x++)
                {
                    if (x != firstX && y != firstY)
                    {
                        cells.Add(new MapCell()
                        {
                            X = x, Y = y, MapId = MapId
                        });
                    }
                }
            }

            foreach (MapCell cell in cells.OrderBy(s => r.Next(int.MaxValue)))
            {
                if (!ServerManager.GetMap(MapId).IsBlockedZone(firstX, firstY, cell.X, cell.Y))
                {
                    firstX = cell.X;
                    firstY = cell.Y;
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 15
0
        public MapItem PutItem(byte type, short slot, byte amount, ref Inventory inv)
        {
            Logger.Debug($"type: {type} slot: {slot} amount: {amount}", Owner.Session.SessionId);
            Random  rnd         = new Random();
            int     random      = 0;
            int     i           = 0;
            MapItem droppedItem = null;
            short   MapX        = (short)(rnd.Next(Owner.MapX - 1, Owner.MapX + 2));
            short   MapY        = (short)(rnd.Next(Owner.MapY - 1, Owner.MapY + 2));

            while (ServerManager.GetMap(Owner.MapId).IsBlockedZone(MapX, MapY) && i < 5)
            {
                MapX = (short)(rnd.Next(Owner.MapX - 1, Owner.MapX + 2));
                MapY = (short)(rnd.Next(Owner.MapY - 1, Owner.MapY + 2));
                i++;
            }
            if (i == 5)
            {
                return(null);
            }
            if (amount > 0 && amount <= inv.ItemInstance.Amount)
            {
                droppedItem = new MapItem(MapX, MapY, false)
                {
                    ItemInstance = (inv.ItemInstance as ItemInstance).DeepCopy()
                };
                while (ServerManager.GetMap(Owner.MapId).DroppedList.ContainsKey(random = rnd.Next(1, 999999)))
                {
                }
                droppedItem.ItemInstance.ItemInstanceId = random;
                droppedItem.ItemInstance.Amount         = amount;
                ServerManager.GetMap(Owner.MapId).DroppedList.Add(random, droppedItem);
                inv.ItemInstance.Amount -= amount;
            }
            return(droppedItem);
        }
Exemplo n.º 16
0
        internal void MonsterLife()
        {
            LifeTaskIsRunning = true;
            NpcMonster monster = ServerManager.GetNpc(this.MonsterVNum);

            //Respawn
            if (!Alive)
            {
                double timeDeath = (DateTime.Now - Death).TotalSeconds;
                if (timeDeath >= monster.RespawnTime / 10)
                {
                    Alive     = true;
                    Target    = -1;
                    CurrentHp = monster.MaxHP;
                    CurrentMp = monster.MaxMP;
                    MapX      = firstX;
                    MapY      = firstY;

                    Map.Broadcast(GenerateIn3());
                    Map.Broadcast(GenerateEff(7));
                }
                LifeTaskIsRunning = false;
                return;
            }
            else if (Target == -1)
            {
                //Normal Move Mode
                if (monster == null || Alive == false)
                {
                    LifeTaskIsRunning = false;
                    return;
                }
                Random r            = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
                double time         = (DateTime.Now - LastMove).TotalSeconds;
                int    MoveFrequent = 5 - (int)Math.Round((double)(monster.Speed / 5));
                if (MoveFrequent < 1)
                {
                    MoveFrequent = 1;
                }
                if (IsMoving)
                {
                    if (path.Where(s => s != null).ToList().Count > 0)//fix a path problem
                    {
                        if ((DateTime.Now - LastMove).TotalSeconds > 1.0 / monster.Speed)
                        {
                            short MapX = path.ElementAt(0).X;
                            short MapY = path.ElementAt(0).Y;
                            path.RemoveAt(0);
                            LastMove = DateTime.Now;
                            Map.Broadcast($"mv 3 {this.MapMonsterId} {this.MapX} {this.MapY} {monster.Speed}");

                            Task.Factory.StartNew(async() =>
                            {
                                await Task.Delay(500);
                                this.MapX = MapX;
                                this.MapY = MapY;
                            });
                            LifeTaskIsRunning = false;
                            return;
                        }
                    }
                    else if (time > r.Next(1, MoveFrequent) + 1)
                    {
                        int  MoveDistance = (int)Math.Round((double)monster.Speed / 2);
                        byte xpoint       = (byte)(r.Next(1, MoveDistance));
                        byte ypoint       = (byte)(r.Next(1, MoveDistance));

                        short MapX = firstX;
                        short MapY = firstY;
                        if (ServerManager.GetMap(MapId).GetFreePosition(ref MapX, ref MapY, xpoint, ypoint))
                        {
                            LastMove = DateTime.Now;

                            string movepacket = $"mv 3 {this.MapMonsterId} {MapX} {MapY} {monster.Speed}";
                            Map.Broadcast(movepacket);

                            Task.Factory.StartNew(async() =>
                            {
                                await Task.Delay(500);
                                this.MapX = MapX;
                                this.MapY = MapY;
                            });
                        }
                    }
                }
                if (monster.IsHostile)
                {
                    Character character = ServerManager.Instance.Sessions.Where(s => s.Character != null && s.Character.Hp > 0).OrderBy(s => Map.GetDistance(new MapCell()
                    {
                        X = MapX, Y = MapY
                    }, new MapCell()
                    {
                        X = s.Character.MapX, Y = s.Character.MapY
                    })).FirstOrDefault(s => s.Character != null && !s.Character.Invisible && s.Character.MapId == MapId)?.Character;
                    if (character != null)
                    {
                        if (Map.GetDistance(new MapCell()
                        {
                            X = character.MapX, Y = character.MapY
                        }, new MapCell()
                        {
                            X = MapX, Y = MapY
                        }) < 7)
                        {
                            Target = character.CharacterId;
                            if (!monster.NoAggresiveIcon)
                            {
                                ServerManager.Instance.Sessions.FirstOrDefault(s => s != null && s.Client != null && s.Character != null && s.Character.CharacterId.Equals(Target)).Client.SendPacket(GenerateEff(5000));
                            }
                        }
                    }
                }
            }
            else
            {
                short?MapX      = ServerManager.Instance.GetProperty <short?>(Target, "MapX");
                short?MapY      = ServerManager.Instance.GetProperty <short?>(Target, "MapY");
                int?  Hp        = ServerManager.Instance.GetProperty <int?>(Target, "Hp");
                short?mapId     = ServerManager.Instance.GetProperty <short?>(Target, "MapId");
                bool? invisible = ServerManager.Instance.GetProperty <bool?>(Target, "Invisible");

                if (MapX == null || MapY == null || Hp <= 0 || invisible != null && (bool)invisible)
                {
                    Target = -1; LifeTaskIsRunning = false; return;
                }

                Random          r   = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
                NpcMonsterSkill ski = monster.Skills.Where(s => !s.Used && (DateTime.Now - s.LastUse).TotalMilliseconds >= 100 * ServerManager.GetSkill(s.SkillVNum).Cooldown).OrderBy(rnd => r.Next()).FirstOrDefault();
                Skill           sk  = null;
                if (ski != null)
                {
                    sk = ServerManager.GetSkill(ski.SkillVNum);
                }

                ClientSession targetSession = Map.Sessions.SingleOrDefault(s => s.Character.CharacterId == Target);

                int damage = 100;

                if (targetSession != null && (sk != null && Map.GetDistance(new MapCell()
                {
                    X = this.MapX, Y = this.MapY
                }, new MapCell()
                {
                    X = (short)MapX, Y = (short)MapY
                }) < sk.Range) || (Map.GetDistance(new MapCell()
                {
                    X = this.MapX, Y = this.MapY
                }, new MapCell()
                {
                    X = (short)MapX, Y = (short)MapY
                }) <= monster.BasicRange))
                {
                    if ((sk != null && ((DateTime.Now - LastEffect).TotalMilliseconds >= sk.Cooldown * 100 + 1000)) || ((DateTime.Now - LastEffect).TotalMilliseconds >= (monster.BasicCooldown < 4 ? 4 : monster.BasicCooldown) * 100 + 100))
                    {
                        if (ski != null)
                        {
                            ski.Used    = true;
                            ski.LastUse = DateTime.Now;
                            Map.Broadcast($"ct 3 {MapMonsterId} 1 {Target} {sk.CastAnimation} -1 {sk.SkillVNum}");
                        }

                        LastMove = DateTime.Now;

                        // deal 0 damage to GM with GodMode
                        if (targetSession == null)
                        {
                            damage = 0;
                        }
                        else
                        {
                            damage = targetSession.Character.HasGodMode ? 0 : 100;
                        }

                        if (sk != null && sk.CastEffect != 0)
                        {
                            Map.Broadcast(GenerateEff(sk.CastEffect));
                            Thread.Sleep(sk.CastTime * 100);
                        }
                        path = new List <MapCell>();
                        targetSession.Character.LastDefence = DateTime.Now;
                        targetSession.Character.GetDamage(damage);
                        Map.Broadcast(null, ServerManager.Instance.GetUserMethod <string>(Target, "GenerateStat"), ReceiverType.OnlySomeone, "", Target);

                        if (sk != null)
                        {
                            Map.Broadcast($"su 3 {MapMonsterId} 1 {Target} {ski.SkillVNum} {sk.Cooldown} {sk.AttackAnimation} {sk.Effect} {this.MapX} {this.MapY} {(targetSession.Character.Hp > 0 ? 1 : 0)} {(int)((double)targetSession.Character.Hp / ServerManager.Instance.GetUserMethod<double>(Target, "HPLoad"))} {damage} 0 0");
                        }
                        else
                        {
                            Map.Broadcast($"su 3 {MapMonsterId} 1 {Target} 0 {monster.BasicCooldown} 11 {monster.BasicSkill} 0 0 {(targetSession.Character.Hp > 0 ? 1 : 0)} {(int)((double)targetSession.Character.Hp / ServerManager.Instance.GetUserMethod<double>(Target, "HPLoad"))} {damage} 0 0");
                        }

                        if (ski != null)
                        {
                            ski.Used = false;
                        }
                        LastEffect = DateTime.Now;
                        if (targetSession.Character.Hp <= 0)
                        {
                            Thread.Sleep(1000);
                            ServerManager.Instance.AskRevive(Target);
                            Target = -1;
                        }
                        if ((sk != null && (sk.Range > 0 || sk.TargetRange > 0)))
                        {
                            foreach (Character chara in ServerManager.GetMap(MapId).GetListPeopleInRange(sk.TargetRange == 0 ? this.MapX : (short)MapX, sk.TargetRange == 0 ? this.MapY : (short)MapY, (byte)(sk.TargetRange + sk.Range)).Where(s => s.CharacterId != Target))
                            {
                                damage = chara.HasGodMode ? 0 : 100;
                                bool AlreadyDead2 = chara.Hp <= 0;
                                chara.GetDamage(damage);
                                chara.LastDefence = DateTime.Now;
                                Map.Broadcast(null, chara.GenerateStat(), ReceiverType.OnlySomeone, "", chara.CharacterId);
                                Map.Broadcast($"su 3 {MapMonsterId} 1 {chara.CharacterId} 0 {monster.BasicCooldown} 11 {monster.BasicSkill} 0 0 {(chara.Hp > 0 ? 1 : 0)} {(int)((double)chara.Hp / chara.HPLoad())} {damage} 0 0");
                                if (chara.Hp <= 0 && !AlreadyDead2)
                                {
                                    Thread.Sleep(1000);
                                    ServerManager.Instance.AskRevive(chara.CharacterId);
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (IsMoving == true)
                    {
                        short maxdistance = 22;

                        if (path.Count() == 0)
                        {
                            path = ServerManager.GetMap(MapId).AStar(new MapCell()
                            {
                                X = this.MapX, Y = this.MapY, MapId = this.MapId
                            }, new MapCell()
                            {
                                X = (short)MapX, Y = (short)MapY, MapId = this.MapId
                            });
                        }
                        if (path.Count > 0 && Map.GetDistance(new MapCell()
                        {
                            X = this.MapX, Y = this.MapY, MapId = this.MapId
                        }, new MapCell()
                        {
                            X = (short)MapX, Y = (short)MapY, MapId = this.MapId
                        }) > 1)
                        {
                            this.MapX = path.ElementAt(0).X;
                            this.MapY = path.ElementAt(0).Y;
                            path.RemoveAt(0);
                        }
                        if (MapId != mapId || (Map.GetDistance(new MapCell()
                        {
                            X = this.MapX, Y = this.MapY
                        }, new MapCell()
                        {
                            X = (short)MapX, Y = (short)MapY
                        }) > maxdistance))
                        {
                            path = ServerManager.GetMap(MapId).AStar(new MapCell()
                            {
                                X = this.MapX, Y = this.MapY, MapId = this.MapId
                            }, new MapCell()
                            {
                                X = firstX, Y = firstY, MapId = this.MapId
                            });
                            Target = -1;
                        }
                        else
                        {
                            if ((DateTime.Now - LastMove).TotalSeconds > 1.0 / monster.Speed)
                            {
                                LastMove = DateTime.Now;
                                Map.Broadcast($"mv 3 {this.MapMonsterId} {this.MapX} {this.MapY} {monster.Speed}");
                            }
                        }
                    }
                }
            }
            LifeTaskIsRunning = false;
        }
Exemplo n.º 17
0
        //Both partly
        public void ChangeMap(long id)
        {
            ClientSession session = Sessions.FirstOrDefault(s => s.Character != null && s.Character.CharacterId == id);

            if (session != null)
            {
                session.CurrentMap.UnregisterSession(session);
                session.CurrentMap = ServerManager.GetMap(session.Character.MapId);
                session.CurrentMap.RegisterSession(session);
                session.Client.SendPacket(session.Character.GenerateCInfo());
                session.Client.SendPacket(session.Character.GenerateCMode());
                session.CurrentMap?.Broadcast(session, session.Character.GenerateEq(), ReceiverType.All);
                session.Client.SendPacket(session.Character.GenerateEquipment());
                session.Client.SendPacket(session.Character.GenerateLev());
                session.Client.SendPacket(session.Character.GenerateStat());
                session.Client.SendPacket(session.Character.GenerateAt());
                session.Client.SendPacket(session.Character.GenerateCond());
                session.Client.SendPacket(session.Character.GenerateCMap());
                session.Client.SendPacket(session.Character.GenerateStatChar());
                session.Client.SendPacket($"gidx 1 {session.Character.CharacterId} -1 - 0"); // family
                session.Client.SendPacket("rsfp 0 -1");
                //cond 2 // partner only send when partner present
                session.Client.SendPacket("pinit 0"); // partner initialization
                session.Client.SendPacket(session.Character.GeneratePairy());
                session.CurrentMap?.Broadcast(session, session.Character.GeneratePairy(), ReceiverType.AllExceptMe);
                session.Client.SendPacket("act6"); // act6 1 0 14 0 0 0 14 0 0 0

                foreach (String portalPacket in session.Character.GenerateGp())
                {
                    session.Client.SendPacket(portalPacket);
                }
                // wp 23 124 4 4 12 99
                foreach (String monsterPacket in session.Character.GenerateIn3())
                {
                    session.Client.SendPacket(monsterPacket);
                }
                foreach (String npcPacket in session.Character.GenerateIn2())
                {
                    session.Client.SendPacket(npcPacket);
                }
                foreach (String ShopPacket in session.Character.GenerateNPCShopOnMap())
                {
                    session.Client.SendPacket(ShopPacket);
                }
                foreach (String droppedPacket in session.Character.GenerateDroppedItem())
                {
                    session.Client.SendPacket(droppedPacket);
                }
                foreach (String ShopPacket in session.Character.GenerateShopOnMap())
                {
                    session.Client.SendPacket(ShopPacket);
                }
                foreach (String ShopPacketChar in session.Character.GeneratePlayerShopOnMap())
                {
                    session.Client.SendPacket(ShopPacketChar);
                }
                ServerManager.Instance.Sessions.Where(s => s.Character != null && s.Character.MapId.Equals(session.Character.MapId) && s.Character.Name != session.Character.Name && !s.Character.InvisibleGm).ToList().ForEach(s => RequireBroadcastFromUser(session, s.Character.CharacterId, "GenerateIn"));
                if (session.Character.InvisibleGm == false)
                {
                    session.CurrentMap?.Broadcast(session, session.Character.GenerateIn(), ReceiverType.AllExceptMe);
                }
                if (session.Character.Size != 10)
                {
                    session.Client.SendPacket(session.Character.GenerateScal());
                }
                if (session.CurrentMap.IsDancing == 2 && session.Character.IsDancing == 0)
                {
                    session.CurrentMap?.Broadcast("dance 2");
                }
                else if (session.CurrentMap.IsDancing == 0 && session.Character.IsDancing == 1)
                {
                    session.Character.IsDancing = 0;
                    session.CurrentMap?.Broadcast("dance");
                }
                foreach (Group g in Groups)
                {
                    foreach (ClientSession groupSession in g.Characters)
                    {
                        ClientSession chara = Sessions.FirstOrDefault(s => s.Character != null && s.Character.CharacterId == groupSession.Character.CharacterId && s.CurrentMap.MapId == groupSession.CurrentMap.MapId);
                        if (chara != null)
                        {
                            groupSession.Client.SendPacket($"pidx 1 1.{chara.Character.CharacterId}");
                        }
                        if (groupSession.Character.CharacterId == groupSession.Character.CharacterId)
                        {
                            session.CurrentMap?.Broadcast(groupSession, $"pidx 1 1.{groupSession.Character.CharacterId}", ReceiverType.All);
                        }
                    }
                }
            }
        }
Exemplo n.º 18
0
        internal void MonsterLife()
        {
            //Respawn
            if (!Alive && Respawn)
            {
                double timeDeath = (DateTime.Now - Death).TotalSeconds;
                if (timeDeath >= Monster.RespawnTime / 10)
                {
                    DamageList = new Dictionary <long, long>();
                    Alive      = true;
                    Target     = -1;
                    CurrentHp  = Monster.MaxHP;
                    CurrentMp  = Monster.MaxMP;
                    MapX       = firstX;
                    MapY       = firstY;
                    Path       = new List <MapCell>();
                    Map.Broadcast(GenerateIn3());
                    Map.Broadcast(GenerateEff(7));
                }
                return;
            }
            else if (Target == -1)
            {
                //Normal Move Mode
                if (Alive == false)
                {
                    return;
                }
                Random random       = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
                double time         = (DateTime.Now - LastMove).TotalSeconds;
                int    MoveFrequent = 5 - (int)Math.Round((double)(Monster.Speed / 5));
                if (MoveFrequent < 1)
                {
                    MoveFrequent = 1;
                }
                if (IsMoving)
                {
                    if (Path.Where(s => s != null).ToList().Count > 0)
                    {
                        if ((DateTime.Now - LastMove).TotalSeconds > 1.0 / Monster.Speed)
                        {
                            short mapX = Path.ElementAt(0).X;
                            short mapY = Path.ElementAt(0).Y;
                            Path.RemoveAt(0);
                            LastMove = DateTime.Now;
                            Map.Broadcast($"mv 3 {this.MapMonsterId} {this.MapX} {this.MapY} {Monster.Speed}");

                            Task.Factory.StartNew(async() =>
                            {
                                await Task.Delay(400);
                                this.MapX = mapX;
                                this.MapY = mapY;
                            });
                            return;
                        }
                    }
                    else if (time > 0.5 * random.Next(1, 3) * (0.5 + random.NextDouble()))
                    {
                        byte point  = (byte)random.Next(2, 4);
                        byte fpoint = (byte)random.Next(0, 2);

                        byte xpoint = (byte)random.Next(fpoint, point);
                        byte ypoint = (byte)(point - xpoint);

                        short mapX = firstX;
                        short mapY = firstY;
                        if (ServerManager.GetMap(MapId).GetFreePosition(ref mapX, ref mapY, xpoint, ypoint))
                        {
                            Task.Factory.StartNew(async() =>
                            {
                                await Task.Delay(400);
                                this.MapX = mapX;
                                this.MapY = mapY;
                            });
                            LastMove = DateTime.Now;

                            string movePacket = $"mv 3 {this.MapMonsterId} {mapX} {mapY} {Monster.Speed}";
                            Map.Broadcast(movePacket);
                        }
                    }
                }
                if (Monster.IsHostile)
                {
                    Character character = ServerManager.Instance.Sessions.Where(s => s.Character != null && s.Character.Hp > 0).OrderBy(s => Map.GetDistance(new MapCell()
                    {
                        X = MapX, Y = MapY
                    }, new MapCell()
                    {
                        X = s.Character.MapX, Y = s.Character.MapY
                    })).FirstOrDefault(s => s.Character != null && !s.Character.Invisible && s.Character.MapId == MapId)?.Character;
                    if (character != null)
                    {
                        if (Map.GetDistance(new MapCell()
                        {
                            X = character.MapX, Y = character.MapY
                        }, new MapCell()
                        {
                            X = MapX, Y = MapY
                        }) < 10)
                        {
                            Target = character.CharacterId;
                            if (!Monster.NoAggresiveIcon)
                            {
                                character.Session.SendPacket(GenerateEff(5000));
                            }
                        }
                    }
                }
            }
            else
            {
                ClientSession targetSession = Map.Sessions.SingleOrDefault(s => s.Character.CharacterId == Target);

                if (targetSession == null || targetSession.Character.Invisible)
                {
                    Target = -1;
                    return;
                }
                Random          random          = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
                NpcMonsterSkill npcMonsterSkill = null;
                if (random.Next(10) > 8 || inWaiting)
                {
                    inWaiting = false;
                    if ((DateTime.Now - LastEffect).TotalMilliseconds < Monster.BasicCooldown * 200)
                    {
                        inWaiting = true;
                    }
                    npcMonsterSkill = Skills.Where(s => (DateTime.Now - s.LastUse).TotalMilliseconds >= 100 * s.Skill.Cooldown).OrderBy(rnd => random.Next()).FirstOrDefault();
                }

                int damage = 100;

                if (targetSession != null && targetSession.Character.Hp > 0 && ((npcMonsterSkill != null && CurrentMp - npcMonsterSkill.Skill.MpCost >= 0 && Map.GetDistance(new MapCell()
                {
                    X = this.MapX, Y = this.MapY
                }, new MapCell()
                {
                    X = targetSession.Character.MapX, Y = targetSession.Character.MapY
                }) < npcMonsterSkill.Skill.Range) || (Map.GetDistance(new MapCell()
                {
                    X = this.MapX, Y = this.MapY
                }, new MapCell()
                {
                    X = targetSession.Character.MapX, Y = targetSession.Character.MapY
                }) <= Monster.BasicRange)))
                {
                    if ((DateTime.Now - LastEffect).TotalMilliseconds >= Monster.BasicCooldown * 200 && !inWaiting)
                    {
                        if (npcMonsterSkill != null)
                        {
                            npcMonsterSkill.LastUse = DateTime.Now;
                            CurrentMp -= npcMonsterSkill.Skill.MpCost;
                            Map.Broadcast($"ct 3 {MapMonsterId} 1 {Target} {npcMonsterSkill.Skill.CastAnimation} {npcMonsterSkill.Skill.CastEffect} {npcMonsterSkill.Skill.SkillVNum}");
                        }
                        LastMove = DateTime.Now;

                        // deal 0 damage to GM with GodMode
                        damage = targetSession.Character.HasGodMode ? 0 : 100;
                        if (targetSession.Character.IsSitting)
                        {
                            targetSession.Character.IsSitting = false;
                            Map.Broadcast(null, targetSession.Character.GenerateRest(), ReceiverType.OnlySomeone, "", targetSession.Character.CharacterId);
                        }
                        if (npcMonsterSkill != null && npcMonsterSkill.Skill.CastEffect != 0)
                        {
                            Map.Broadcast(GenerateEff(npcMonsterSkill.Skill.CastEffect));
                            Thread.Sleep(npcMonsterSkill.Skill.CastTime * 100);
                        }
                        Path = new List <MapCell>();
                        targetSession.Character.LastDefence = DateTime.Now;
                        targetSession.Character.GetDamage(damage);

                        Map.Broadcast(null, ServerManager.Instance.GetUserMethod <string>(Target, "GenerateStat"), ReceiverType.OnlySomeone, "", Target);

                        if (npcMonsterSkill != null)
                        {
                            Map.Broadcast($"su 3 {MapMonsterId} 1 {Target} {npcMonsterSkill.SkillVNum} {npcMonsterSkill.Skill.Cooldown} {npcMonsterSkill.Skill.AttackAnimation} {npcMonsterSkill.Skill.Effect} {this.MapX} {this.MapY} {(targetSession.Character.Hp > 0 ? 1 : 0)} { (int)(targetSession.Character.Hp / targetSession.Character.HPLoad() * 100) } {damage} 0 0");
                        }
                        else
                        {
                            Map.Broadcast($"su 3 {MapMonsterId} 1 {Target} 0 {Monster.BasicCooldown} 11 {Monster.BasicSkill} 0 0 {(targetSession.Character.Hp > 0 ? 1 : 0)} { (int)(targetSession.Character.Hp / targetSession.Character.HPLoad() * 100) } {damage} 0 0");
                        }


                        LastEffect = DateTime.Now;
                        if (targetSession.Character.Hp <= 0)
                        {
                            Thread.Sleep(1000);
                            ServerManager.Instance.AskRevive(targetSession.Character.CharacterId);
                            Target = -1;
                        }
                        if (npcMonsterSkill != null && (npcMonsterSkill.Skill.Range > 0 || npcMonsterSkill.Skill.TargetRange > 0))
                        {
                            foreach (Character chara in ServerManager.GetMap(MapId).GetListPeopleInRange(npcMonsterSkill.Skill.TargetRange == 0 ? this.MapX : targetSession.Character.MapX, npcMonsterSkill.Skill.TargetRange == 0 ? this.MapY : targetSession.Character.MapY, (byte)(npcMonsterSkill.Skill.TargetRange + npcMonsterSkill.Skill.Range)).Where(s => s.CharacterId != Target && s.Hp > 0))
                            {
                                if (chara.IsSitting)
                                {
                                    chara.IsSitting = false;
                                    Map.Broadcast(null, chara.GenerateRest(), ReceiverType.OnlySomeone, "", chara.CharacterId);
                                }
                                damage = chara.HasGodMode ? 0 : 100;
                                bool AlreadyDead2 = chara.Hp <= 0;
                                chara.GetDamage(damage);
                                chara.LastDefence = DateTime.Now;
                                Map.Broadcast(null, chara.GenerateStat(), ReceiverType.OnlySomeone, "", chara.CharacterId);
                                Map.Broadcast($"su 3 {MapMonsterId} 1 {chara.CharacterId} 0 {Monster.BasicCooldown} 11 {Monster.BasicSkill} 0 0 {(chara.Hp > 0 ? 1 : 0)} { (int)(chara.Hp / chara.HPLoad() * 100) } {damage} 0 0");
                                if (chara.Hp <= 0 && !AlreadyDead2)
                                {
                                    Thread.Sleep(1000);
                                    ServerManager.Instance.AskRevive(chara.CharacterId);
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (IsMoving == true)
                    {
                        short maxDistance = 22;

                        if (Path.Count() == 0 && targetSession != null && (Map.GetDistance(new MapCell()
                        {
                            X = this.MapX, Y = this.MapY
                        }, new MapCell()
                        {
                            X = targetSession.Character.MapX, Y = targetSession.Character.MapY
                        }) < maxDistance))
                        {
                            Path = ServerManager.GetMap(MapId).StraightPath(new MapCell()
                            {
                                X = this.MapX, Y = this.MapY, MapId = this.MapId
                            }, new MapCell()
                            {
                                X = targetSession.Character.MapX, Y = targetSession.Character.MapY, MapId = this.MapId
                            });
                            if (!Path.Any())
                            {
                                Path = ServerManager.GetMap(MapId).JPSPlus(new MapCell()
                                {
                                    X = this.MapX, Y = this.MapY, MapId = this.MapId
                                }, new MapCell()
                                {
                                    X = targetSession.Character.MapX, Y = targetSession.Character.MapY, MapId = this.MapId
                                });
                            }
                        }
                        if (Path.Count > 0 && Map.GetDistance(new MapCell()
                        {
                            X = this.MapX, Y = this.MapY, MapId = this.MapId
                        }, new MapCell()
                        {
                            X = targetSession.Character.MapX, Y = targetSession.Character.MapY, MapId = this.MapId
                        }) > 1)
                        {
                            this.MapX = Path.ElementAt(0).X;
                            this.MapY = Path.ElementAt(0).Y;
                            Path.RemoveAt(0);
                        }
                        if (targetSession == null || MapId != targetSession.Character.MapId || (Map.GetDistance(new MapCell()
                        {
                            X = this.MapX, Y = this.MapY
                        }, new MapCell()
                        {
                            X = targetSession.Character.MapX, Y = targetSession.Character.MapY
                        }) > maxDistance))
                        {
                            Path = ServerManager.GetMap(MapId).StraightPath(new MapCell()
                            {
                                X = this.MapX, Y = this.MapY, MapId = this.MapId
                            }, new MapCell()
                            {
                                X = firstX, Y = firstY, MapId = this.MapId
                            });
                            if (!Path.Any())
                            {
                                Path = ServerManager.GetMap(MapId).JPSPlus(new MapCell()
                                {
                                    X = this.MapX, Y = this.MapY, MapId = this.MapId
                                }, new MapCell()
                                {
                                    X = firstX, Y = firstY, MapId = this.MapId
                                });
                            }
                            Target = -1;
                        }
                        else
                        {
                            if ((DateTime.Now - LastMove).TotalSeconds > 1.0 / Monster.Speed)
                            {
                                LastMove = DateTime.Now;
                                Map.Broadcast($"mv 3 {this.MapMonsterId} {this.MapX} {this.MapY} {Monster.Speed}");
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 19
0
        public void ChangeMap(long id)
        {
            foreach (ClientSession Session in Sessions.Where(s => s.Character != null && s.Character.CharacterId == id))
            {
                Session.CurrentMap = ServerManager.GetMap(Session.Character.MapId);
                Session.Client.SendPacket(Session.Character.GenerateCInfo());
                Session.Client.SendPacket(Session.Character.GenerateCMode());
                Session.Client.SendPacket(Session.Character.GenerateFaction());
                Session.Client.SendPacket(Session.Character.GenerateFd());
                Session.Client.SendPacket(Session.Character.GenerateLev());
                Session.Client.SendPacket(Session.Character.GenerateStat());
                // ski
                Session.Client.SendPacket(Session.Character.GenerateAt());
                Session.Client.SendPacket(Session.Character.GenerateCMap());
                if (Session.Character.Size != 10)
                {
                    Session.Client.SendPacket(Session.Character.GenerateScal());
                }
                foreach (String portalPacket in Session.Character.GenerateGp())
                {
                    Session.Client.SendPacket(portalPacket);
                }
                foreach (String npcPacket in Session.Character.Generatein2())
                {
                    Session.Client.SendPacket(npcPacket);
                }
                foreach (String monsterPacket in Session.Character.Generatein3())
                {
                    Session.Client.SendPacket(monsterPacket);
                }
                foreach (String ShopPacket in Session.Character.GenerateNPCShopOnMap())
                {
                    Session.Client.SendPacket(ShopPacket);
                }
                foreach (String droppedPacket in Session.Character.GenerateDroppedItem())
                {
                    Session.Client.SendPacket(droppedPacket);
                }

                Session.Client.SendPacket(Session.Character.GenerateStatChar());
                Session.Client.SendPacket(Session.Character.GenerateCond());
                ClientLinkManager.Instance.Broadcast(Session, Session.Character.GeneratePairy(), ReceiverType.AllOnMap);
                Session.Client.SendPacket($"rsfi 1 1 0 9 0 9"); // Act completion
                ClientLinkManager.Instance.RequiereBroadcastFromAllMapUsersNotInvisible(Session, "GenerateIn");
                if (Session.Character.InvisibleGm == false)
                {
                    ClientLinkManager.Instance.Broadcast(Session, Session.Character.GenerateIn(), ReceiverType.AllOnMapExceptMe);
                }
                if (Session.CurrentMap.IsDancing == 2 && Session.Character.IsDancing == 0)
                {
                    ClientLinkManager.Instance.RequiereBroadcastFromMap(Session.Character.MapId, "dance 2");
                }
                else if (Session.CurrentMap.IsDancing == 0 && Session.Character.IsDancing == 1)
                {
                    Session.Character.IsDancing = 0;
                    ClientLinkManager.Instance.RequiereBroadcastFromMap(Session.Character.MapId, "dance");
                }
                foreach (String ShopPacket in Session.Character.GenerateShopOnMap())
                {
                    Session.Client.SendPacket(ShopPacket);
                }

                foreach (String ShopPacketChar in Session.Character.GeneratePlayerShopOnMap())
                {
                    Session.Client.SendPacket(ShopPacketChar);
                }

                ClientLinkManager.Instance.Broadcast(Session, Session.Character.GenerateEq(), ReceiverType.AllOnMap);
                Session.Client.SendPacket(Session.Character.GenerateEquipment());
                string clinit  = "clinit";
                string flinit  = "flinit";
                string kdlinit = "kdlinit";

                foreach (CharacterDTO character in DAOFactory.CharacterDAO.GetTopComplimented())
                {
                    clinit += $" {character.CharacterId}|{character.Level}|{character.Compliment}|{character.Name}";
                }
                foreach (CharacterDTO character in DAOFactory.CharacterDAO.GetTopReputation())
                {
                    flinit += $" {character.CharacterId}|{character.Level}|{character.Reput}|{character.Name}";
                }
                foreach (CharacterDTO character in DAOFactory.CharacterDAO.GetTopPoints())
                {
                    kdlinit += $" {character.CharacterId}|{character.Level}|{character.Act4Points}|{character.Name}";
                }

                Session.Client.SendPacket(clinit);
                Session.Client.SendPacket(flinit);
                Session.Client.SendPacket(kdlinit);
            }
        }
Exemplo n.º 20
0
        internal void MonsterLife()
        {
            NpcMonster monster = ServerManager.GetNpc(this.MonsterVNum);

            if (monster == null)
            {
                return;
            }

            double time = (DateTime.Now - LastMove).TotalSeconds;

            if (Move && time > 2.2)
            {
                Random r     = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
                byte   point = (byte)r.Next(2, 6);

                byte xpoint = (byte)r.Next(0, point);
                byte ypoint = (byte)(point - xpoint);

                short MapX = (short)r.Next(-xpoint + firstX, xpoint + firstX);
                short MapY = (short)r.Next(-ypoint + firstY, ypoint + firstY);
                bool  ok   = true;
                if (MapX > firstX)
                {
                    for (int i = 0; i < MapX - firstX; i++)
                    {
                        if (ServerManager.GetMap(MapId).IsBlockedZone(firstX + i, firstY))
                        {
                            ok = false;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < firstX - MapX; i++)
                    {
                        if (ServerManager.GetMap(MapId).IsBlockedZone(MapX + i, MapY))
                        {
                            ok = false;
                        }
                    }
                }

                if (MapY > firstY)
                {
                    for (int i = 0; i < MapY - firstY; i++)
                    {
                        if (ServerManager.GetMap(MapId).IsBlockedZone(firstX, firstY + i))
                        {
                            ok = false;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < firstX - MapX; i++)
                    {
                        if (ServerManager.GetMap(MapId).IsBlockedZone(MapX, MapY + i))
                        {
                            ok = false;
                        }
                    }
                }
                if (ok)
                {
                    this.MapX = MapX;
                    this.MapY = MapY;
                    LastMove  = DateTime.Now;

                    string movepacket = $"mv 3 {this.MapMonsterId} {this.MapX} {this.MapY} {monster.Speed}";
                    ClientLinkManager.Instance.RequiereBroadcastFromMap(MapId, movepacket);
                }
            }
        }
Exemplo n.º 21
0
        internal void MonsterLife()
        {
            // Respawn
            if (!Alive && Respawn)
            {
                double timeDeath = (DateTime.Now - Death).TotalSeconds;
                if (timeDeath >= Monster.RespawnTime / 10)
                {
                    DamageList = new Dictionary <long, long>();
                    Alive      = true;
                    Target     = -1;
                    CurrentHp  = Monster.MaxHP;
                    CurrentMp  = Monster.MaxMP;
                    MapX       = FirstX;
                    MapY       = FirstY;
                    Path       = new List <MapCell>();
                    Map.Broadcast(GenerateIn3());
                    Map.Broadcast(GenerateEff(7), 10);
                }
                return;
            }
            else if (Target == -1)
            {
                // Normal Move Mode
                if (!Alive)
                {
                    return;
                }
                if (IsMoving && Monster.Speed > 0)
                {
                    double time = (DateTime.Now - LastMove).TotalMilliseconds;

                    if (Path.Where(s => s != null).ToList().Count > 0)
                    {
                        int timetowalk = 1000 / (2 * Monster.Speed);
                        if (time > timetowalk)
                        {
                            short mapX = Path.ElementAt(0).X;
                            short mapY = Path.ElementAt(0).Y;
                            Path.RemoveAt(0);
                            LastMove = DateTime.Now;
                            Map.Broadcast($"mv 3 {this.MapMonsterId} {this.MapX} {this.MapY} {Monster.Speed}");

                            Task.Factory.StartNew(async() =>
                            {
                                await Task.Delay(timetowalk);
                                MapX = mapX;
                                MapY = mapY;
                            });
                            return;
                        }
                    }
                    else if (time > _movetime)
                    {
                        _movetime = _random.Next(400, 3200);
                        byte point  = (byte)_random.Next(2, 4);
                        byte fpoint = (byte)_random.Next(0, 2);

                        byte xpoint = (byte)_random.Next(fpoint, point);
                        byte ypoint = (byte)(point - xpoint);

                        short mapX = FirstX;
                        short mapY = FirstY;
                        if (Map.GetFreePosition(ref mapX, ref mapY, xpoint, ypoint))
                        {
                            Task.Factory.StartNew(async() =>
                            {
                                await Task.Delay(1000 * (xpoint + ypoint) / (2 * Monster.Speed));
                                this.MapX = mapX;
                                this.MapY = mapY;
                            });
                            LastMove = DateTime.Now.AddSeconds((xpoint + ypoint) / (2 * Monster.Speed));

                            string movePacket = $"mv 3 {this.MapMonsterId} {mapX} {mapY} {Monster.Speed}";
                            Map.Broadcast(movePacket);
                        }
                    }
                }
                if (Monster.IsHostile)
                {
                    Character character = ServerManager.Instance.Sessions.FirstOrDefault(s => s != null && s.Character != null && s.Character.Hp > 0 && !s.Character.InvisibleGm && !s.Character.Invisible && s.Character.MapId == MapId && Map.GetDistance(new MapCell()
                    {
                        X = MapX, Y = MapY
                    }, new MapCell()
                    {
                        X = s.Character.MapX, Y = s.Character.MapY
                    }) < 10)?.Character;
                    if (character != null)
                    {
                        Target = character.CharacterId;
                        if (!Monster.NoAggresiveIcon)
                        {
                            character.Session.SendPacket(GenerateEff(5000));
                        }
                    }
                }
            }
            else
            {
                ClientSession targetSession = Map.GetSessionByCharacterId(Target);

                if (targetSession == null || targetSession.Character.Invisible)
                {
                    Target = -1;
                    Path   = ServerManager.GetMap(MapId).StraightPath(new MapCell()
                    {
                        X = this.MapX, Y = this.MapY, MapId = this.MapId
                    }, new MapCell()
                    {
                        X = FirstX, Y = FirstY, MapId = this.MapId
                    });
                    if (!Path.Any())
                    {
                        Path = ServerManager.GetMap(MapId).JPSPlus(new MapCell()
                        {
                            X = this.MapX, Y = this.MapY, MapId = this.MapId
                        }, new MapCell()
                        {
                            X = FirstX, Y = FirstY, MapId = this.MapId
                        });
                    }
                }
                NpcMonsterSkill npcMonsterSkill = null;
                if (_random.Next(10) > 8)
                {
                    npcMonsterSkill = Skills.Where(s => (DateTime.Now - s.LastUse).TotalMilliseconds >= 100 * s.Skill.Cooldown).OrderBy(rnd => _random.Next()).FirstOrDefault();
                }

                int damage = 500;

                if (targetSession != null && targetSession.Character.Hp > 0 && ((npcMonsterSkill != null && CurrentMp - npcMonsterSkill.Skill.MpCost >= 0 && Map.GetDistance(new MapCell()
                {
                    X = this.MapX, Y = this.MapY
                }, new MapCell()
                {
                    X = targetSession.Character.MapX, Y = targetSession.Character.MapY
                }) < npcMonsterSkill.Skill.Range) || (Map.GetDistance(new MapCell()
                {
                    X = this.MapX, Y = this.MapY
                }, new MapCell()
                {
                    X = targetSession.Character.MapX, Y = targetSession.Character.MapY
                }) <= Monster.BasicRange)))
                {
                    if (((DateTime.Now - LastEffect).TotalMilliseconds >= 1000 + Monster.BasicCooldown * 200 && !Skills.Any()) || npcMonsterSkill != null)
                    {
                        if (npcMonsterSkill != null)
                        {
                            npcMonsterSkill.LastUse = DateTime.Now;
                            CurrentMp -= npcMonsterSkill.Skill.MpCost;
                            Map.Broadcast($"ct 3 {MapMonsterId} 1 {Target} {npcMonsterSkill.Skill.CastAnimation} {npcMonsterSkill.Skill.CastEffect} {npcMonsterSkill.Skill.SkillVNum}");
                        }
                        LastMove = DateTime.Now;

                        // deal 0 damage to GM with GodMode
                        damage = targetSession.Character.HasGodMode ? 0 : 100;
                        if (targetSession.Character.IsSitting)
                        {
                            targetSession.Character.IsSitting = false;
                            Map.Broadcast(targetSession.Character.GenerateRest());
                            Thread.Sleep(500);
                        }
                        if (npcMonsterSkill != null && npcMonsterSkill.Skill.CastEffect != 0)
                        {
                            Map.Broadcast(GenerateEff(npcMonsterSkill.Skill.CastEffect));
                            Thread.Sleep(npcMonsterSkill.Skill.CastTime * 100);
                        }
                        Path = new List <MapCell>();
                        targetSession.Character.LastDefence = DateTime.Now;
                        targetSession.Character.GetDamage(damage);

                        Map.Broadcast(null, ServerManager.Instance.GetUserMethod <string>(Target, "GenerateStat"), ReceiverType.OnlySomeone, "", Target);

                        if (npcMonsterSkill != null)
                        {
                            Map.Broadcast($"su 3 {MapMonsterId} 1 {Target} {npcMonsterSkill.SkillVNum} {npcMonsterSkill.Skill.Cooldown} {npcMonsterSkill.Skill.AttackAnimation} {npcMonsterSkill.Skill.Effect} {this.MapX} {this.MapY} {(targetSession.Character.Hp > 0 ? 1 : 0)} { (int)(targetSession.Character.Hp / targetSession.Character.HPLoad() * 100) } {damage} 0 0");
                        }
                        else
                        {
                            Map.Broadcast($"su 3 {MapMonsterId} 1 {Target} 0 {Monster.BasicCooldown} 11 {Monster.BasicSkill} 0 0 {(targetSession.Character.Hp > 0 ? 1 : 0)} { (int)(targetSession.Character.Hp / targetSession.Character.HPLoad() * 100) } {damage} 0 0");
                        }

                        LastEffect = DateTime.Now;
                        if (targetSession.Character.Hp <= 0)
                        {
                            Thread.Sleep(1000);
                            ServerManager.Instance.AskRevive(targetSession.Character.CharacterId);
                            Path = ServerManager.GetMap(MapId).StraightPath(new MapCell()
                            {
                                X = this.MapX, Y = this.MapY, MapId = this.MapId
                            }, new MapCell()
                            {
                                X = FirstX, Y = FirstY, MapId = this.MapId
                            });
                            if (!Path.Any())
                            {
                                Path = ServerManager.GetMap(MapId).JPSPlus(new MapCell()
                                {
                                    X = this.MapX, Y = this.MapY, MapId = this.MapId
                                }, new MapCell()
                                {
                                    X = FirstX, Y = FirstY, MapId = this.MapId
                                });
                            }
                        }
                        if (npcMonsterSkill != null && (npcMonsterSkill.Skill.Range > 0 || npcMonsterSkill.Skill.TargetRange > 0))
                        {
                            foreach (Character chara in ServerManager.GetMap(MapId).GetListPeopleInRange(npcMonsterSkill.Skill.TargetRange == 0 ? this.MapX : targetSession.Character.MapX, npcMonsterSkill.Skill.TargetRange == 0 ? this.MapY : targetSession.Character.MapY, (byte)(npcMonsterSkill.Skill.TargetRange + npcMonsterSkill.Skill.Range)).Where(s => s.CharacterId != Target && s.Hp > 0))
                            {
                                if (chara.IsSitting)
                                {
                                    chara.IsSitting = false;
                                    Map.Broadcast(chara.GenerateRest());
                                    Thread.Sleep(500);
                                }
                                damage = chara.HasGodMode ? 0 : 100;
                                bool AlreadyDead2 = chara.Hp <= 0;
                                chara.GetDamage(damage);
                                chara.LastDefence = DateTime.Now;
                                Map.Broadcast(null, chara.GenerateStat(), ReceiverType.OnlySomeone, "", chara.CharacterId);
                                Map.Broadcast($"su 3 {MapMonsterId} 1 {chara.CharacterId} 0 {Monster.BasicCooldown} 11 {Monster.BasicSkill} 0 0 {(chara.Hp > 0 ? 1 : 0)} { (int)(chara.Hp / chara.HPLoad() * 100) } {damage} 0 0");
                                if (chara.Hp <= 0 && !AlreadyDead2)
                                {
                                    Thread.Sleep(1000);
                                    ServerManager.Instance.AskRevive(chara.CharacterId);
                                }
                            }
                        }
                    }
                }
                else
                {
                    int distance = 0;
                    if (targetSession != null)
                    {
                        distance = Map.GetDistance(new MapCell()
                        {
                            X = this.MapX, Y = this.MapY
                        }, new MapCell()
                        {
                            X = targetSession.Character.MapX, Y = targetSession.Character.MapY
                        });
                    }
                    if (IsMoving)
                    {
                        short maxDistance = 22;
                        if (Path.Count() == 0 && targetSession != null && distance > 1 && distance < maxDistance)
                        {
                            short xoffset = (short)_random.Next(-1, 1);
                            short yoffset = (short)_random.Next(-1, 1);

                            Path = ServerManager.GetMap(MapId).StraightPath(new MapCell()
                            {
                                X = this.MapX, Y = this.MapY, MapId = this.MapId
                            }, new MapCell()
                            {
                                X = (short)(targetSession.Character.MapX + xoffset), Y = (short)(targetSession.Character.MapY + yoffset), MapId = this.MapId
                            });
                            if (!Path.Any())
                            {
                                Path = ServerManager.GetMap(MapId).JPSPlus(new MapCell()
                                {
                                    X = this.MapX, Y = this.MapY, MapId = this.MapId
                                }, new MapCell()
                                {
                                    X = (short)(targetSession.Character.MapX + xoffset), Y = (short)(targetSession.Character.MapY + yoffset), MapId = this.MapId
                                });
                            }
                        }
                        if (DateTime.Now > LastMove && Monster.Speed > 0 && Path.Count > 0)
                        {
                            short mapX;
                            short mapY;
                            int   maxindex = Path.Count > Monster.Speed / 2 ? Monster.Speed / 2 : Path.Count;
                            mapX = Path.ElementAt(maxindex - 1).X;
                            mapY = Path.ElementAt(maxindex - 1).Y;
                            double waitingtime = (double)(Map.GetDistance(new MapCell()
                            {
                                X = mapX, Y = mapY, MapId = MapId
                            }, new MapCell()
                            {
                                X = MapX, Y = MapY, MapId = MapId
                            })) / (double)(Monster.Speed);
                            Map.Broadcast($"mv 3 {this.MapMonsterId} {mapX} {mapY} {Monster.Speed}");
                            LastMove = DateTime.Now.AddSeconds((waitingtime > 1 ? 1 : waitingtime));
                            Task.Factory.StartNew(async() =>
                            {
                                await Task.Delay((int)((waitingtime > 1 ? 1 : waitingtime) * 1000));
                                this.MapX = mapX;
                                this.MapY = mapY;
                            });

                            for (int j = maxindex; j > 0; j--)
                            {
                                Path.RemoveAt(0);
                            }
                        }
                        if (Path.Count() == 0 && (targetSession == null || MapId != targetSession.Character.MapId || distance > maxDistance))
                        {
                            Path = ServerManager.GetMap(MapId).StraightPath(new MapCell()
                            {
                                X = this.MapX, Y = this.MapY, MapId = this.MapId
                            }, new MapCell()
                            {
                                X = FirstX, Y = FirstY, MapId = this.MapId
                            });
                            if (!Path.Any())
                            {
                                Path = ServerManager.GetMap(MapId).JPSPlus(new MapCell()
                                {
                                    X = this.MapX, Y = this.MapY, MapId = this.MapId
                                }, new MapCell()
                                {
                                    X = FirstX, Y = FirstY, MapId = this.MapId
                                });
                            }
                            Target = -1;
                        }
                    }
                }
            }
        }