/// <summary> /// Try to find the closest entity from whitelist on a current map /// Will return null if can't find anything /// </summary> private EntityUid?FindTargetFromWhitelist(EntityUid uid, EntityWhitelist whitelist, TransformComponent?transform = null) { if (!Resolve(uid, ref transform)) { return(null); } var mapId = transform.MapID; var ents = _entityLookup.GetEntitiesInMap(mapId); // sort all entities in distance increasing order var l = new SortedList <float, EntityUid>(); foreach (var e in ents) { if (whitelist.IsValid(e)) { var dist = (EntityManager.GetComponent <TransformComponent>(e).WorldPosition - transform.WorldPosition).LengthSquared; l.TryAdd(dist, e); } } // return uid with a smallest distacne return(l.Count > 0 ? l.First().Value : null); }
public async Task Test() { var serverOptions = new ServerContentIntegrationOption { ExtraPrototypes = Prototypes }; var server = StartServer(serverOptions); await server.WaitIdleAsync(); var mapManager = server.ResolveDependency <IMapManager>(); var sEntities = server.ResolveDependency <IEntityManager>(); await server.WaitAssertion(() => { var mapId = GetMainMapId(mapManager); var mapCoordinates = new MapCoordinates(0, 0, mapId); var validComponent = sEntities.SpawnEntity("ValidComponentDummy", mapCoordinates); var validTag = sEntities.SpawnEntity("ValidTagDummy", mapCoordinates); var invalidComponent = sEntities.SpawnEntity("InvalidComponentDummy", mapCoordinates); var invalidTag = sEntities.SpawnEntity("InvalidTagDummy", mapCoordinates); // Test instantiated on its own var whitelistInst = new EntityWhitelist { Components = new[] { $"{ValidComponent}" }, Tags = new[] { "ValidTag" } }; whitelistInst.UpdateRegistrations(); Assert.That(whitelistInst, Is.Not.Null); Assert.That(whitelistInst.Components, Is.Not.Null); Assert.That(whitelistInst.Tags, Is.Not.Null); Assert.That(whitelistInst.IsValid(validComponent), Is.True); Assert.That(whitelistInst.IsValid(validTag), Is.True); Assert.That(whitelistInst.IsValid(invalidComponent), Is.False); Assert.That(whitelistInst.IsValid(invalidTag), Is.False); // Test from serialized var dummy = sEntities.SpawnEntity("WhitelistDummy", mapCoordinates); var whitelistSer = sEntities.GetComponent <ItemSlotsComponent>(dummy).Slots.Values.First().Whitelist; Assert.That(whitelistSer, Is.Not.Null); Assert.That(whitelistSer.Components, Is.Not.Null); Assert.That(whitelistSer.Tags, Is.Not.Null); Assert.That(whitelistSer.IsValid(validComponent), Is.True); Assert.That(whitelistSer.IsValid(validTag), Is.True); Assert.That(whitelistSer.IsValid(invalidComponent), Is.False); Assert.That(whitelistSer.IsValid(invalidTag), Is.False); }); }