/// <summary> /// Sets region, x, and y, to be near entity. /// Also randomizes direction. /// </summary> /// <param name="entity"></param> /// <param name="range"></param> public void SetLocationNear(Entity entity, int range) { var rnd = RandomProvider.Get(); var pos = entity.GetPosition(); var target = pos.GetRandomInRange(range, rnd); var dir = (byte)rnd.Next(256); this.SetLocation(entity.RegionId, target.X, target.Y); this.Direction = dir; }
/// <summary> /// Returns all visible creatures in range of entity, excluding itself. /// </summary> /// <param name="range"></param> /// <returns></returns> public List<Creature> GetVisibleCreaturesInRange(Entity entity, int range = VisibleRange) { _creaturesRWLS.EnterReadLock(); try { return _creatures.Values.Where(a => a != entity && a.GetPosition().InRange(entity.GetPosition(), range) && !a.Conditions.Has(ConditionsA.Invisible)).ToList(); } finally { _creaturesRWLS.ExitReadLock(); } }
/// <summary> /// Broadcasts packet to all creatures in range of source. /// </summary> public void Broadcast(Packet packet, Entity source, bool sendToSource = true, int range = -1) { if (range < 0) range = VisibleRange; var pos = source.GetPosition(); lock (_clients) { foreach (var client in _clients) { if (!client.Controlling.GetPosition().InRange(pos, range)) continue; if (client.Controlling == source && !sendToSource) continue; client.Send(packet); } } }
/// <summary> /// Returns new list of all entities within range of source. /// </summary> public List<Entity> GetEntitiesInRange(Entity source, int range = -1) { if (range < 0) range = VisibleRange; var result = new List<Entity>(); _creaturesRWLS.EnterReadLock(); try { result.AddRange(_creatures.Values.Where(a => a.GetPosition().InRange(source.GetPosition(), range))); } finally { _creaturesRWLS.ExitReadLock(); } _itemsRWLS.EnterReadLock(); try { result.AddRange(_items.Values.Where(a => a.GetPosition().InRange(source.GetPosition(), range))); } finally { _itemsRWLS.ExitReadLock(); } _propsRWLS.EnterReadLock(); try { // All props are visible, but not all of them are in range. result.AddRange(_props.Values.Where(a => a.GetPosition().InRange(source.GetPosition(), range) && a.ServerSide)); } finally { _propsRWLS.ExitReadLock(); } return result; }
/// <summary> /// Returns all visible creatures in range of entity, excluding itself. /// </summary> /// <param name="entity"></param> /// <param name="range"></param> /// <returns></returns> public ICollection<Creature> GetVisibleCreaturesInRange(Entity entity, int range = VisibleRange) { return this.GetCreatures(a => a != entity && a.GetPosition().InRange(entity.GetPosition(), range) && !a.Conditions.Has(ConditionsA.Invisible)); }