Files
tbd-station-14/Content.IntegrationTests/Tests/Interaction/Click/InteractionSystemTests.cs
collinlunn f2816e8081 Moves Hands to shared, some prediction (#3829)
* HandsGuiState

* Gui state setting methods

* code cleanup

* Removes TryGetHands

* ClientHand

* Gui Hands

* Refactor WIP 1

* Hand index

* refactors 2

* wip 3

* wip 4

* wiip 4

* wip 5

* wip 6

* wip 7

* wip 8

* wip 9

* wip 11

* Hand ui mostly looks fine

* hands gui cleanup 1

* cleanup 2

* wip 13

* hand enabled

* stuff

* Hands gui gap fix

* onpressed test

* hand gui buttons events work

* bag activation works

* fix item use

* todo comment

* hands activate fix

* Moves Client Hands back to using strings to identify active hand

* fixes action hand highlighting

* diff fix

* serverhand

* SharedHand

* SharedHand, IReadOnlyHand

* Client Hands only stores SharedHand

* cleanup server hands

* server hand container shutdown

* misc renames, refactors of serverhand

* stuff 1

* stuff 3

* server hand refactor 1

* Undo API changes to remove massive diff

* More API name fixes

* server hands cleanup 2

* cleanup 3

* dropping cleanup

* Cleanup 4

* MoveItemFromHand

* Stuff

* region sorting

* Hand Putter methods cleanup

* stuff 2

* Merges all of serverhand and clienthand into sharedhand

* Other hands systems, hack to make inhands update (gui state set every frame, visualzier updated every frame)

* GetFinalDropCoordinates cleanup

* SwapHands cleanup

* Moves server hands code to shared hands

* Fixed hand selected and deselected

* Naming fixes

* Server hands system cleanup

* Hands privacy fixes

* Client hand updates when containers are modified

* HeldItemVisualizer

* Fixes hand gui item status panel

* method name fix

* Swap hands prediction

* Dropping prediction

* Fixes pickup entity animation

* Fixes HeldItemsVisualizer

* moves item pickup to shared

* PR cleanup

* fixes hand enabling/disabling

* build fix

* Conflict fixes

* Fixes pickup animation

* Uses component directed message subscriptions

* event unsubscriptions in hand system

* unsubscribe fix

* CanInsertEntityIntoHand checks if hand is enabled

* Moving items from one hand to another checks if the hands can pick up and drop

* Fixes stop pulling not re-enabling hand

* Fixes pickup animation for entities containers on the floor

* Fixes using held items

* Fixes multiple hands guis appearing

* test fix

* removes obsolete system sunsubscribes

* Checks IsFirstTimePredicted before playing drop animation

* fixes hand item deleted crash

* Uses Get to get other system

* Replaces AppearanceComponent with SharedAppearanceComponent

* Replaces EnsureComponent with TryGetComponent

* Improves event class names

* Moves property up to top of class

* Moves code for determining the hand visualizer rsi state into the visualizer instead of being determined on hand component

* Eventbus todo comment

* Yaml fix for changed visualizer name

* Makes HandsVisuals a byte

* Removes state from HandsVisualizer

* Fixes hand using interaction method name

* Namespace changes fixes

* Fix for changed hand interaction method

* missing }

* conflict build fix

* Moves cleint HandsSystem to correct folder

* Moved namespace fix for interaction test

* Moves Handsvisualizer ot correct folder

* Moves SharedHandsSystem to correct folder

* Fixes errors from moving namespace of hand systems

* Fixes PDA component changes

* Fixes ActionsComponent diff

* Fixes inventory component diff

* fixes null ref

* Replaces obsolete Loc.GetString usage with fluent translations

* Fluent for hands disarming

* SwapHands and Drop user input specify to the server which hand

* Pickup animation WorldPosiiton todo

* Cleans up hands gui subscription handling

* Fixes change in ActionBlockerSystem access

* Namespace references fixes

* HandsComponent PlayerAttached/Detached messages are handled through eventbus

* Fixes GasCanisterSystem drop method usage

* Fix gameticker equipping method at new location

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2021-06-21 19:21:20 +10:00

424 lines
18 KiB
C#

using NUnit.Framework;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Reflection;
using System.Threading.Tasks;
using Content.Server.Hands.Components;
using Content.Server.Interaction;
using Content.Server.Items;
using Content.Shared.Interaction;
using Content.Shared.Weapons.Melee;
using Content.Shared.Hands.Components;
namespace Content.IntegrationTests.Tests.Interaction.Click
{
[TestFixture]
[TestOf(typeof(InteractionSystem))]
public class InteractionSystemTests : ContentIntegrationTest
{
const string PROTOTYPES = @"
- type: entity
id: DummyDebugWall
components:
- type: Physics
bodyType: Dynamic
fixtures:
- shape:
!type:PhysShapeAabb
bounds: ""-0.25,-0.25,0.25,0.25""
layer:
- MobMask
mask:
- MobMask
";
[Test]
public async Task InteractionTest()
{
var server = StartServerDummyTicker(new ServerContentIntegrationOption
{
ContentBeforeIoC = () =>
{
IoCManager.Resolve<IEntitySystemManager>().LoadExtraSystemType<TestInteractionSystem>();
}
});
await server.WaitIdleAsync();
var entityManager = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
var mapId = MapId.Nullspace;
var coords = MapCoordinates.Nullspace;
server.Assert(() =>
{
mapId = mapManager.CreateMap();
coords = new MapCoordinates(Vector2.Zero, mapId);
});
await server.WaitIdleAsync();
IEntity user = null;
IEntity target = null;
IEntity item = null;
server.Assert(() =>
{
user = entityManager.SpawnEntity(null, coords);
user.EnsureComponent<HandsComponent>().AddHand("hand", HandLocation.Left);
target = entityManager.SpawnEntity(null, coords);
item = entityManager.SpawnEntity(null, coords);
item.EnsureComponent<ItemComponent>();
});
await server.WaitRunTicks(1);
var entitySystemManager = server.ResolveDependency<IEntitySystemManager>();
Assert.That(entitySystemManager.TryGetEntitySystem<InteractionSystem>(out var interactionSystem));
Assert.That(entitySystemManager.TryGetEntitySystem<TestInteractionSystem>(out var testInteractionSystem));
var attack = false;
var interactUsing = false;
var interactHand = false;
server.Assert(() =>
{
testInteractionSystem.AttackEvent = (_, _, ev) => { Assert.That(ev.Target, Is.EqualTo(target.Uid)); attack = true; };
testInteractionSystem.InteractUsingEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactUsing = true; };
testInteractionSystem.InteractHandEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactHand = true; };
interactionSystem.DoAttack(user, target.Transform.Coordinates, false, target.Uid);
interactionSystem.UserInteraction(user, target.Transform.Coordinates, target.Uid);
Assert.That(attack);
Assert.That(interactUsing, Is.False);
Assert.That(interactHand);
Assert.That(user.TryGetComponent<HandsComponent>(out var hands));
Assert.That(hands.PutInHand(item.GetComponent<ItemComponent>()));
interactionSystem.UserInteraction(user, target.Transform.Coordinates, target.Uid);
Assert.That(interactUsing);
});
await server.WaitIdleAsync();
}
[Test]
public async Task InteractionObstructionTest()
{
var server = StartServerDummyTicker(new ServerContentIntegrationOption
{
ContentBeforeIoC = () =>
{
IoCManager.Resolve<IEntitySystemManager>().LoadExtraSystemType<TestInteractionSystem>();
},
ExtraPrototypes = PROTOTYPES
});
await server.WaitIdleAsync();
var entityManager = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
var mapId = MapId.Nullspace;
var coords = MapCoordinates.Nullspace;
server.Assert(() =>
{
mapId = mapManager.CreateMap();
coords = new MapCoordinates(Vector2.Zero, mapId);
});
await server.WaitIdleAsync();
IEntity user = null;
IEntity target = null;
IEntity item = null;
IEntity wall = null;
server.Assert(() =>
{
user = entityManager.SpawnEntity(null, coords);
user.EnsureComponent<HandsComponent>().AddHand("hand", HandLocation.Left);
target = entityManager.SpawnEntity(null, new MapCoordinates((1.9f, 0), mapId));
item = entityManager.SpawnEntity(null, coords);
item.EnsureComponent<ItemComponent>();
wall = entityManager.SpawnEntity("DummyDebugWall", new MapCoordinates((1, 0), user.Transform.MapID));
});
await server.WaitRunTicks(1);
var entitySystemManager = server.ResolveDependency<IEntitySystemManager>();
Assert.That(entitySystemManager.TryGetEntitySystem<InteractionSystem>(out var interactionSystem));
Assert.That(entitySystemManager.TryGetEntitySystem<TestInteractionSystem>(out var testInteractionSystem));
var attack = false;
var interactUsing = false;
var interactHand = false;
server.Assert(() =>
{
testInteractionSystem.AttackEvent = (_, _, ev) => { Assert.That(ev.Target, Is.EqualTo(target.Uid)); attack = true; };
testInteractionSystem.InteractUsingEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactUsing = true; };
testInteractionSystem.InteractHandEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactHand = true; };
interactionSystem.DoAttack(user, target.Transform.Coordinates, false, target.Uid);
interactionSystem.UserInteraction(user, target.Transform.Coordinates, target.Uid);
Assert.That(attack, Is.False);
Assert.That(interactUsing, Is.False);
Assert.That(interactHand, Is.False);
Assert.That(user.TryGetComponent<HandsComponent>(out var hands));
Assert.That(hands.PutInHand(item.GetComponent<ItemComponent>()));
interactionSystem.UserInteraction(user, target.Transform.Coordinates, target.Uid);
Assert.That(interactUsing, Is.False);
});
await server.WaitIdleAsync();
}
[Test]
public async Task InteractionInRangeTest()
{
var server = StartServerDummyTicker(new ServerContentIntegrationOption
{
ContentBeforeIoC = () =>
{
IoCManager.Resolve<IEntitySystemManager>().LoadExtraSystemType<TestInteractionSystem>();
}
});
await server.WaitIdleAsync();
var entityManager = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
var mapId = MapId.Nullspace;
var coords = MapCoordinates.Nullspace;
server.Assert(() =>
{
mapId = mapManager.CreateMap();
coords = new MapCoordinates(Vector2.Zero, mapId);
});
await server.WaitIdleAsync();
IEntity user = null;
IEntity target = null;
IEntity item = null;
server.Assert(() =>
{
user = entityManager.SpawnEntity(null, coords);
user.EnsureComponent<HandsComponent>().AddHand("hand", HandLocation.Left);
target = entityManager.SpawnEntity(null, new MapCoordinates((InteractionSystem.InteractionRange - 0.1f, 0), mapId));
item = entityManager.SpawnEntity(null, coords);
item.EnsureComponent<ItemComponent>();
});
await server.WaitRunTicks(1);
var entitySystemManager = server.ResolveDependency<IEntitySystemManager>();
Assert.That(entitySystemManager.TryGetEntitySystem<InteractionSystem>(out var interactionSystem));
Assert.That(entitySystemManager.TryGetEntitySystem<TestInteractionSystem>(out var testInteractionSystem));
var attack = false;
var interactUsing = false;
var interactHand = false;
server.Assert(() =>
{
testInteractionSystem.AttackEvent = (_, _, ev) => { Assert.That(ev.Target, Is.EqualTo(target.Uid)); attack = true; };
testInteractionSystem.InteractUsingEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactUsing = true; };
testInteractionSystem.InteractHandEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactHand = true; };
interactionSystem.DoAttack(user, target.Transform.Coordinates, false, target.Uid);
interactionSystem.UserInteraction(user, target.Transform.Coordinates, target.Uid);
Assert.That(attack);
Assert.That(interactUsing, Is.False);
Assert.That(interactHand);
Assert.That(user.TryGetComponent<HandsComponent>(out var hands));
Assert.That(hands.PutInHand(item.GetComponent<ItemComponent>()));
interactionSystem.UserInteraction(user, target.Transform.Coordinates, target.Uid);
Assert.That(interactUsing);
});
await server.WaitIdleAsync();
}
[Test]
public async Task InteractionOutOfRangeTest()
{
var server = StartServerDummyTicker(new ServerContentIntegrationOption
{
ContentBeforeIoC = () =>
{
IoCManager.Resolve<IEntitySystemManager>().LoadExtraSystemType<TestInteractionSystem>();
}
});
await server.WaitIdleAsync();
var entityManager = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
var mapId = MapId.Nullspace;
var coords = MapCoordinates.Nullspace;
server.Assert(() =>
{
mapId = mapManager.CreateMap();
coords = new MapCoordinates(Vector2.Zero, mapId);
});
await server.WaitIdleAsync();
IEntity user = null;
IEntity target = null;
IEntity item = null;
server.Assert(() =>
{
user = entityManager.SpawnEntity(null, coords);
user.EnsureComponent<HandsComponent>().AddHand("hand", HandLocation.Left);
target = entityManager.SpawnEntity(null, new MapCoordinates((InteractionSystem.InteractionRange, 0), mapId));
item = entityManager.SpawnEntity(null, coords);
item.EnsureComponent<ItemComponent>();
});
await server.WaitRunTicks(1);
var entitySystemManager = server.ResolveDependency<IEntitySystemManager>();
Assert.That(entitySystemManager.TryGetEntitySystem<InteractionSystem>(out var interactionSystem));
Assert.That(entitySystemManager.TryGetEntitySystem<TestInteractionSystem>(out var testInteractionSystem));
var attack = false;
var interactUsing = false;
var interactHand = false;
server.Assert(() =>
{
testInteractionSystem.AttackEvent = (_, _, ev) => { Assert.That(ev.Target, Is.EqualTo(target.Uid)); attack = true; };
testInteractionSystem.InteractUsingEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactUsing = true; };
testInteractionSystem.InteractHandEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactHand = true; };
interactionSystem.DoAttack(user, target.Transform.Coordinates, false, target.Uid);
interactionSystem.UserInteraction(user, target.Transform.Coordinates, target.Uid);
Assert.That(attack, Is.False);
Assert.That(interactUsing, Is.False);
Assert.That(interactHand, Is.False);
Assert.That(user.TryGetComponent<HandsComponent>(out var hands));
Assert.That(hands.PutInHand(item.GetComponent<ItemComponent>()));
interactionSystem.UserInteraction(user, target.Transform.Coordinates, target.Uid);
Assert.That(interactUsing, Is.False);
});
await server.WaitIdleAsync();
}
[Test]
public async Task InsideContainerInteractionBlockTest()
{
var server = StartServerDummyTicker(new ServerContentIntegrationOption
{
ContentBeforeIoC = () =>
{
IoCManager.Resolve<IEntitySystemManager>().LoadExtraSystemType<TestInteractionSystem>();
},
FailureLogLevel = Robust.Shared.Log.LogLevel.Error
});
await server.WaitIdleAsync();
var entityManager = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
var mapId = MapId.Nullspace;
var coords = MapCoordinates.Nullspace;
server.Assert(() =>
{
mapId = mapManager.CreateMap();
coords = new MapCoordinates(Vector2.Zero, mapId);
});
await server.WaitIdleAsync();
IEntity user = null;
IEntity target = null;
IEntity item = null;
IEntity containerEntity = null;
IContainer container = null;
server.Assert(() =>
{
user = entityManager.SpawnEntity(null, coords);
user.EnsureComponent<HandsComponent>().AddHand("hand", HandLocation.Left);
target = entityManager.SpawnEntity(null, coords);
item = entityManager.SpawnEntity(null, coords);
item.EnsureComponent<ItemComponent>();
containerEntity = entityManager.SpawnEntity(null, coords);
container = ContainerHelpers.EnsureContainer<Container>(containerEntity, "InteractionTestContainer");
});
await server.WaitRunTicks(1);
var entitySystemManager = server.ResolveDependency<IEntitySystemManager>();
Assert.That(entitySystemManager.TryGetEntitySystem<InteractionSystem>(out var interactionSystem));
Assert.That(entitySystemManager.TryGetEntitySystem<TestInteractionSystem>(out var testInteractionSystem));
await server.WaitIdleAsync();
var attack = false;
var interactUsing = false;
var interactHand = false;
server.Assert(() =>
{
Assert.That(container.Insert(user));
Assert.That(user.Transform.Parent.Owner, Is.EqualTo(containerEntity));
testInteractionSystem.AttackEvent = (_, _, ev) => { Assert.That(ev.Target, Is.EqualTo(containerEntity.Uid)); attack = true; };
testInteractionSystem.InteractUsingEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(containerEntity)); interactUsing = true; };
testInteractionSystem.InteractHandEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(containerEntity)); interactHand = true; };
interactionSystem.DoAttack(user, target.Transform.Coordinates, false, target.Uid);
interactionSystem.UserInteraction(user, target.Transform.Coordinates, target.Uid);
Assert.That(attack, Is.False);
Assert.That(interactUsing, Is.False);
Assert.That(interactHand, Is.False);
interactionSystem.DoAttack(user, containerEntity.Transform.Coordinates, false, containerEntity.Uid);
interactionSystem.UserInteraction(user, containerEntity.Transform.Coordinates, containerEntity.Uid);
Assert.That(attack);
Assert.That(interactUsing, Is.False);
Assert.That(interactHand);
Assert.That(user.TryGetComponent<HandsComponent>(out var hands));
Assert.That(hands.PutInHand(item.GetComponent<ItemComponent>()));
interactionSystem.UserInteraction(user, target.Transform.Coordinates, target.Uid);
Assert.That(interactUsing, Is.False);
interactionSystem.UserInteraction(user, containerEntity.Transform.Coordinates, containerEntity.Uid);
Assert.That(interactUsing, Is.True);
});
await server.WaitIdleAsync();
}
[Reflect(false)]
private class TestInteractionSystem : EntitySystem
{
public ComponentEventHandler<HandsComponent, ClickAttackEvent> AttackEvent;
public EntityEventHandler<InteractUsingEvent> InteractUsingEvent;
public EntityEventHandler<InteractHandEvent> InteractHandEvent;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<HandsComponent, ClickAttackEvent>((u, c, e) => AttackEvent?.Invoke(u, c, e));
SubscribeLocalEvent<InteractUsingEvent>((e) => InteractUsingEvent?.Invoke(e));
SubscribeLocalEvent<InteractHandEvent>((e) => InteractHandEvent?.Invoke(e));
}
}
}
}