Esempio n. 1
0
        public static void Move(this INonPlayableEntity nonPlayableEntity)
        {
            if (!nonPlayableEntity.IsAlive)
            {
                return;
            }

            if (nonPlayableEntity.IsMoving && nonPlayableEntity.Speed > 0)
            {
                var time = (DateTime.Now - nonPlayableEntity.LastMove).TotalMilliseconds;

                if (time > ServerManager.Instance.RandomNumber(400, 3200))
                {
                    short mapX = nonPlayableEntity.MapX, mapY = nonPlayableEntity.MapY;
                    if (nonPlayableEntity.MapInstance.Map.GetFreePosition(ref mapX, ref mapY, (byte)ServerManager.Instance.RandomNumber(0, 3), (byte)ServerManager.Instance.RandomNumber(0, 3)))
                    {
                        var distance = (int)Heuristic.Octile(Math.Abs(nonPlayableEntity.PositionX - mapX), Math.Abs(nonPlayableEntity.PositionY - mapY));
                        var value    = 1000d * distance / (2 * nonPlayableEntity.Speed);
                        Observable.Timer(TimeSpan.FromMilliseconds(value))
                        .Subscribe(
                            x =>
                        {
                            nonPlayableEntity.PositionX = mapX;
                            nonPlayableEntity.PositionY = mapY;
                        });

                        nonPlayableEntity.LastMove = DateTime.Now.AddMilliseconds(value);
                        nonPlayableEntity.MapInstance.Broadcast(new BroadcastPacket(null, nonPlayableEntity.GenerateMove(mapX, mapY), ReceiverType.All));
                    }
                }
            }
        }
Esempio n. 2
0
        public static Task MoveAsync(this INonPlayableEntity nonPlayableEntity, IDistanceCalculator distanceCalculator)
        {
            if (!nonPlayableEntity.IsAlive)
            {
                return(Task.CompletedTask);
            }

            if (!nonPlayableEntity.IsMoving || (nonPlayableEntity.Speed <= 0))
            {
                return(Task.CompletedTask);
            }

            var time = (SystemTime.Now() - nonPlayableEntity.LastMove).TotalMilliseconds;

            if (!(time > RandomHelper.Instance.RandomNumber(400, 3200)))
            {
                return(Task.CompletedTask);
            }

            var mapX = nonPlayableEntity.MapX;
            var mapY = nonPlayableEntity.MapY;

            if (!nonPlayableEntity.MapInstance.Map.GetFreePosition(ref mapX, ref mapY,
                                                                   (byte)RandomHelper.Instance.RandomNumber(0, 3),
                                                                   (byte)RandomHelper.Instance.RandomNumber(0, 3)))
            {
                return(Task.CompletedTask);
            }

            var distance = (int)distanceCalculator.GetDistance(new MapCell {
                X = nonPlayableEntity.PositionX, Y = nonPlayableEntity.PositionY
            }, new MapCell {
                X = mapX, Y = mapY
            });
            var value = 1000d * distance / (2 * nonPlayableEntity.Speed);

            Observable.Timer(TimeSpan.FromMilliseconds(value))
            .Subscribe(
                _ =>
            {
                nonPlayableEntity.PositionX = mapX;
                nonPlayableEntity.PositionY = mapY;
            });

            nonPlayableEntity.LastMove = SystemTime.Now().AddMilliseconds(value);
            return(nonPlayableEntity.MapInstance.SendPacketAsync(
                       nonPlayableEntity.GenerateMove(mapX, mapY)));
        }
 //Pet Monster in packet
 public static InPacket GenerateIn(this INonPlayableEntity visualEntity)
 {
     return(new InPacket
     {
         VisualType = visualEntity.VisualType,
         Name = visualEntity.Name,
         VNum = visualEntity.VNum == 0 ? string.Empty : visualEntity.VNum.ToString(),
         PositionX = visualEntity.PositionX,
         PositionY = visualEntity.PositionY,
         Direction = visualEntity.Direction,
         InNonPlayerSubPacket = new InNonPlayerSubPacket()
         {
             Dialog = 0,
             InAliveSubPacket = new InAliveSubPacket()
             {
                 MP = (int)(visualEntity.Mp / (float)((visualEntity.Monster?.MaxMP ?? 1) * 100)),
                 HP = (int)(visualEntity.Hp / (float)((visualEntity.Monster?.MaxHP ?? 1) * 100))
             }
         }
     });
 }
Esempio n. 4
0
 //Pet Monster in packet
 public static InPacket GenerateIn(this INonPlayableEntity visualEntity)
 {
     return(new InPacket
     {
         VisualType = visualEntity.VisualType,
         Name = visualEntity is INamedEntity namedEntity ? namedEntity.Name : null,
         VisualId = visualEntity.VisualId,
         VNum = visualEntity.VNum == 0 ? string.Empty : visualEntity.VNum.ToString(),
         PositionX = visualEntity.PositionX,
         PositionY = visualEntity.PositionY,
         Direction = visualEntity.Direction,
         InNonPlayerSubPacket = new InNonPlayerSubPacket
         {
             Dialog = visualEntity is MapNpc npc ? npc.Dialog : (short)0,
             InAliveSubPacket = new InAliveSubPacket
             {
                 Mp = (int)(visualEntity.Mp / (float)(visualEntity.NpcMonster?.MaxMp ?? 1) * 100),
                 Hp = (int)(visualEntity.Hp / (float)(visualEntity.NpcMonster?.MaxHp ?? 1) * 100)
             },
             IsSitting = visualEntity.IsSitting
         }
     });
Esempio n. 5
0
        public static void Move(this INonPlayableEntity nonPlayableEntity)
        {
            if (!nonPlayableEntity.IsAlive)
            {
                return;
            }

            if (nonPlayableEntity.IsMoving && nonPlayableEntity.Speed > 0)
            {
                var time = (SystemTime.Now() - nonPlayableEntity.LastMove).TotalMilliseconds;

                if (time > RandomFactory.Instance.RandomNumber(400, 3200))
                {
                    short mapX = nonPlayableEntity.MapX;
                    short mapY = nonPlayableEntity.MapY;
                    if (nonPlayableEntity.MapInstance.Map.GetFreePosition(ref mapX, ref mapY,
                                                                          (byte)RandomFactory.Instance.RandomNumber(0, 3),
                                                                          (byte)RandomFactory.Instance.RandomNumber(0, 3)))
                    {
                        var distance = (int)Heuristic.Octile(Math.Abs(nonPlayableEntity.PositionX - mapX),
                                                             Math.Abs(nonPlayableEntity.PositionY - mapY));
                        var value = 1000d * distance / (2 * nonPlayableEntity.Speed);
                        Observable.Timer(TimeSpan.FromMilliseconds(value))
                        .Subscribe(
                            _ =>
                        {
                            nonPlayableEntity.PositionX = mapX;
                            nonPlayableEntity.PositionY = mapY;
                        });

                        nonPlayableEntity.LastMove = SystemTime.Now().AddMilliseconds(value);
                        nonPlayableEntity.MapInstance.Sessions.SendPacket(
                            nonPlayableEntity.GenerateMove(mapX, mapY));
                    }
                }
            }
        }