using System.Diagnostics.CodeAnalysis; using System.Numerics; using Content.Shared.ActionBlocker; using Content.Shared.CCVar; using Content.Shared.Hands.Components; using Content.Shared.Hands.EntitySystems; using Content.Shared.Interaction; using Content.Shared.Item; using Content.Shared.Popups; using Content.Shared.Tabletop.Components; using Content.Shared.Tabletop.Events; using Content.Shared.Verbs; using Robust.Shared.Configuration; using Robust.Shared.Network; using Robust.Shared.Player; using Robust.Shared.Serialization; using Robust.Shared.Utility; namespace Content.Shared.Tabletop; public abstract partial class SharedTabletopSystem : EntitySystem { [Dependency] private ActionBlockerSystem _actionBlocker = default!; [Dependency] private IConfigurationManager _cfg = default!; [Dependency] private SharedAppearanceSystem _appearance = default!; [Dependency] private SharedHandsSystem _hands = default!; [Dependency] private SharedInteractionSystem _interaction = default!; [Dependency] private SharedMapSystem _map = default!; [Dependency] private SharedPopupSystem _popup = default!; [Dependency] private SharedTransformSystem _transforms = default!; public override void Initialize() { base.Initialize(); SubscribeAllEvent(OnDraggingPlayerChanged); SubscribeLocalEvent>(AddPlayGameVerb); SubscribeLocalEvent(OnInteractUsing); SubscribeNetworkEvent(OnTabletopRequestTakeOut); SubscribeAllEvent(OnTabletopMove); } private void OnTabletopRequestTakeOut(TabletopRequestTakeOut msg, EntitySessionEventArgs args) { if (args.SenderSession is not { } playerSession) return; var table = GetEntity(msg.TableUid); if (!TryComp(table, out TabletopGameComponent? tabletop) || tabletop.Session is not { } session) return; if (!msg.Entity.IsValid()) return; var entity = GetEntity(msg.Entity); if (!HasComp(entity)) { _popup.PopupEntity(Loc.GetString("tabletop-error-remove-non-hologram"), table, args.SenderSession); return; } // Check if player is actually playing at this table. if (!session.Players.ContainsKey(playerSession)) return; // Find the entity, remove it from the session and set it's position to the tabletop. session.Entities.TryGetValue(entity, out var result); session.Entities.Remove(result); PredictedQueueDel(result); } private void OnInteractUsing(Entity ent, ref InteractUsingEvent args) { if (!_cfg.GetCVar(CCVars.GameTabletopPlace)) return; if (!HasComp(args.User)) return; if (ent.Comp.Session is not { } session) return; if (!_hands.TryGetActiveItem(ent.Owner, out var handEnt)) return; if (!HasComp(handEnt)) return; var meta = MetaData(handEnt.Value); var protoId = meta.EntityPrototype?.ID; var hologram = EntityManager.PredictedSpawn(protoId, session.Position.Offset(-1, 0)); // Make sure the entity can be dragged and can be removed, move it into the board game world and add it to the Entities hashmap. EnsureComp(hologram); EnsureComp(hologram); session.Entities.Add(hologram); _popup.PopupEntity(Loc.GetString("tabletop-added-piece"), ent.Owner, args.User); } /// /// Add a verb that allows the player to start playing a tabletop game. /// private void AddPlayGameVerb(Entity ent, ref GetVerbsEvent args) { if (!args.CanAccess || !args.CanInteract) return; if (!TryComp(args.User, out ActorComponent? actor)) return; var playVerb = new ActivationVerb() { Text = Loc.GetString("tabletop-verb-play-game"), Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/die.svg.192dpi.png")), Act = () => OpenSessionFor(actor.PlayerSession, ent.Owner) }; args.Verbs.Add(playVerb); } /// /// Move an entity which is dragged by the user, but check if they are allowed to do so and to these coordinates. /// protected virtual void OnTabletopMove(TabletopMoveEvent msg, EntitySessionEventArgs args) { if (args.SenderSession is not { AttachedEntity: { } playerEntity }) return; var table = GetEntity(msg.TableUid); var moved = GetEntity(msg.MovedEntityUid); if (!CanSeeTable(playerEntity, table) || !CanDrag(playerEntity, moved, out _)) return; // Move the entity and dirty it (we use the map ID from the entity so noone can try to be funny and move the item to another map) var transform = Comp(moved); _transforms.SetParent(moved, transform, _map.GetMapOrInvalid(transform.MapID)); _transforms.SetLocalPosition(moved, msg.Coordinates.Position, transform); } private void OnDraggingPlayerChanged(TabletopDraggingPlayerChangedEvent msg, EntitySessionEventArgs args) { var dragged = GetEntity(msg.DraggedEntityUid); if (!TryComp(dragged, out TabletopDraggableComponent? draggableComponent)) return; draggableComponent.DraggingPlayer = msg.IsDragging ? args.SenderSession.UserId : null; Dirty(dragged, draggableComponent); if (!TryComp(dragged, out AppearanceComponent? appearance)) return; if (draggableComponent.DraggingPlayer != null) { _appearance.SetData(dragged, TabletopItemVisuals.Scale, new Vector2(1.25f, 1.25f), appearance); _appearance.SetData(dragged, TabletopItemVisuals.DrawDepth, (int)DrawDepth.DrawDepth.Items + 1, appearance); } else { _appearance.SetData(dragged, TabletopItemVisuals.Scale, Vector2.One, appearance); _appearance.SetData(dragged, TabletopItemVisuals.DrawDepth, (int)DrawDepth.DrawDepth.Items, appearance); } } [Serializable, NetSerializable] public sealed class TabletopDraggableComponentState(NetUserId? draggingPlayer) : ComponentState { public NetUserId? DraggingPlayer = draggingPlayer; } [Serializable, NetSerializable] public sealed class TabletopRequestTakeOut : EntityEventArgs { public NetEntity Entity; public NetEntity TableUid; } #region Utility /// /// Whether the table exists, and the player can interact with it. /// /// The player entity to check. /// The table entity to check. protected bool CanSeeTable(EntityUid playerEntity, EntityUid? table) { // Table may have been deleted, hence TryComp. if (!TryComp(table, out MetaDataComponent? meta) || meta.EntityLifeStage >= EntityLifeStage.Terminating || (meta.Flags & MetaDataFlags.InContainer) == MetaDataFlags.InContainer) { return false; } return _interaction.InRangeUnobstructed(playerEntity, table.Value) && _actionBlocker.CanInteract(playerEntity, table); } protected bool CanDrag(EntityUid playerEntity, EntityUid target, [NotNullWhen(true)] out TabletopDraggableComponent? draggable) { if (!TryComp(target, out draggable)) return false; // CanSeeTable checks interaction action blockers. So no need to check them here. // If this ever changes, so that ghosts can spectate games, then the check needs to be moved here. return TryComp(playerEntity, out HandsComponent? hands) && hands.Hands.Count > 0; } #endregion }