/// <summary> /// Gets the position of both the sending and receiving entity and checks if the receiver is in range of the sender. /// </summary> private void OnBeforePacketSent(EntityUid uid, WirelessNetworkComponent component, BeforePacketSentEvent args) { var ownPosition = EntityManager.GetComponent <TransformComponent>(component.Owner).WorldPosition; var position = EntityManager.GetComponent <TransformComponent>(args.Sender).WorldPosition; var distance = (ownPosition - position).Length; if (EntityManager.TryGetComponent <WirelessNetworkComponent?>(args.Sender, out var sendingComponent) && distance > sendingComponent.Range) { args.Cancel(); } }
/// <summary> /// Gets the position of both the sending and receiving entity and checks if the receiver is in range of the sender. /// </summary> private void OnBeforePacketSent(EntityUid uid, WirelessNetworkComponent component, BeforePacketSentEvent args) { var ownPosition = args.SenderPosition; var xform = Transform(uid); if (xform.MapID != args.SenderTransform.MapID || !TryComp <WirelessNetworkComponent?>(args.Sender, out var sendingComponent) || (ownPosition - xform.WorldPosition).Length > sendingComponent.Range) { args.Cancel(); } }
public async Task WirelessNetworkDeviceSendAndReceive() { await using var pairTracker = await PoolManager.GetServerClient(new PoolSettings { NoClient = true, ExtraPrototypes = Prototypes }); var server = pairTracker.Pair.Server; var mapManager = server.ResolveDependency <IMapManager>(); var entityManager = server.ResolveDependency <IEntityManager>(); var deviceNetSystem = entityManager.EntitySysManager.GetEntitySystem <DeviceNetworkSystem>(); var deviceNetTestSystem = entityManager.EntitySysManager.GetEntitySystem <DeviceNetworkTestSystem>(); EntityUid device1 = default; EntityUid device2 = default; DeviceNetworkComponent networkComponent1 = null; DeviceNetworkComponent networkComponent2 = null; WirelessNetworkComponent wirelessNetworkComponent = null; var testValue = "test"; var payload = new NetworkPayload { ["Test"] = testValue, ["testnumber"] = 1, ["testbool"] = true }; await server.WaitAssertion(() => { mapManager.CreateNewMapEntity(MapId.Nullspace); device1 = entityManager.SpawnEntity("DummyWirelessNetworkDevice", MapCoordinates.Nullspace); Assert.That(entityManager.TryGetComponent(device1, out networkComponent1), Is.True); Assert.That(entityManager.TryGetComponent(device1, out wirelessNetworkComponent), Is.True); Assert.That(networkComponent1.ReceiveFrequency != null, Is.True); Assert.That(networkComponent1.Address, Is.Not.EqualTo(string.Empty)); device2 = entityManager.SpawnEntity("DummyWirelessNetworkDevice", new MapCoordinates(new Robust.Shared.Maths.Vector2(0, 50), MapId.Nullspace)); Assert.That(entityManager.TryGetComponent(device2, out networkComponent2), Is.True); Assert.That(networkComponent2.ReceiveFrequency != null, Is.True); Assert.That(networkComponent2.Address, Is.Not.EqualTo(string.Empty)); Assert.That(networkComponent1.Address, Is.Not.EqualTo(networkComponent2.Address)); deviceNetSystem.QueuePacket(device1, networkComponent2.Address, payload, networkComponent2.ReceiveFrequency.Value); }); await server.WaitRunTicks(1); await server.WaitIdleAsync(); await server.WaitAssertion(() => { CollectionAssert.AreEqual(deviceNetTestSystem.LastPayload, payload); payload = new NetworkPayload { ["Wirelesstest"] = 5 }; wirelessNetworkComponent.Range = 0; deviceNetSystem.QueuePacket(device1, networkComponent2.Address, payload, networkComponent2.ReceiveFrequency.Value); }); await server.WaitRunTicks(1); await server.WaitIdleAsync(); await server.WaitAssertion(() => { CollectionAssert.AreNotEqual(deviceNetTestSystem.LastPayload, payload); }); await pairTracker.CleanReturnAsync(); }
public async Task WirelessNetworkDeviceSendAndReceive() { var options = new ServerContentIntegrationOption { ExtraPrototypes = Prototypes, ContentBeforeIoC = () => { IoCManager.Resolve <IEntitySystemManager>().LoadExtraSystemType <DeviceNetworkTestSystem>(); } }; var server = StartServerDummyTicker(options); await server.WaitIdleAsync(); var mapManager = server.ResolveDependency <IMapManager>(); var entityManager = server.ResolveDependency <IEntityManager>(); var deviceNetSystem = entityManager.EntitySysManager.GetEntitySystem <DeviceNetworkSystem>(); var deviceNetTestSystem = entityManager.EntitySysManager.GetEntitySystem <DeviceNetworkTestSystem>(); IEntity device1 = null; IEntity device2 = null; DeviceNetworkComponent networkComponent1 = null; DeviceNetworkComponent networkComponent2 = null; WirelessNetworkComponent wirelessNetworkComponent = null; var testValue = "test"; var payload = new NetworkPayload { ["Test"] = testValue, ["testnumber"] = 1, ["testbool"] = true }; server.Assert(() => { mapManager.CreateNewMapEntity(MapId.Nullspace); device1 = entityManager.SpawnEntity("DummyWirelessNetworkDevice", MapCoordinates.Nullspace); Assert.That(device1.TryGetComponent(out networkComponent1), Is.True); Assert.That(device1.TryGetComponent(out wirelessNetworkComponent), Is.True); Assert.That(networkComponent1.Open, Is.True); Assert.That(networkComponent1.Address, Is.Not.EqualTo(string.Empty)); device2 = entityManager.SpawnEntity("DummyWirelessNetworkDevice", new MapCoordinates(new Robust.Shared.Maths.Vector2(0, 50), MapId.Nullspace)); Assert.That(device2.TryGetComponent(out networkComponent2), Is.True); Assert.That(networkComponent2.Open, Is.True); Assert.That(networkComponent2.Address, Is.Not.EqualTo(string.Empty)); Assert.That(networkComponent1.Address, Is.Not.EqualTo(networkComponent2.Address)); deviceNetSystem.QueuePacket(device1.Uid, networkComponent2.Address, networkComponent2.Frequency, payload); }); await server.WaitRunTicks(1); await server.WaitIdleAsync(); server.Assert(() => { CollectionAssert.AreEqual(deviceNetTestSystem.LastPayload, payload); payload = new NetworkPayload { ["Wirelesstest"] = 5 }; wirelessNetworkComponent.Range = 0; deviceNetSystem.QueuePacket(device1.Uid, networkComponent2.Address, networkComponent2.Frequency, payload); }); await server.WaitRunTicks(1); await server.WaitIdleAsync(); server.Assert(() => { CollectionAssert.AreNotEqual(deviceNetTestSystem.LastPayload, payload); }); await server.WaitIdleAsync(); }