]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
biggest gridinv update OF ALL TIME (#25834)
authordeltanedas <39013340+deltanedas@users.noreply.github.com>
Thu, 28 Mar 2024 06:31:47 +0000 (06:31 +0000)
committerGitHub <noreply@github.com>
Thu, 28 Mar 2024 06:31:47 +0000 (17:31 +1100)
* add SaveItemLocation keybind

* make item direction public to avoid having to change between Angle for no reason

* add item location saving

* show

* Added a better save keybind, made it draw saved positions, and trying to save in a position it has already been saved in removes that position.

* w

* code style

* Make taken spots appear blue

* style

* !

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
Co-authored-by: notquitehadouken <tripwiregamer@gmail.com>
Co-authored-by: I.K <45953835+notquitehadouken@users.noreply.github.com>
Content.Client/Input/ContentContexts.cs
Content.Client/Options/UI/Tabs/KeyRebindTab.xaml.cs
Content.Client/UserInterface/Systems/Storage/Controls/StorageContainer.cs
Content.Client/UserInterface/Systems/Storage/StorageUIController.cs
Content.Shared/Input/ContentKeyFunctions.cs
Content.Shared/Storage/EntitySystems/SharedStorageSystem.cs
Content.Shared/Storage/ItemStorageLocation.cs
Content.Shared/Storage/StorageComponent.cs
Resources/Locale/en-US/escape-menu/ui/options-menu.ftl
Resources/keybinds.yml

index 03f4f3f38b7e34d4af20a73c993e508392feebc3..589de6d6a7804e9965675b893cf1832221a6126e 100644 (file)
@@ -32,6 +32,7 @@ namespace Content.Client.Input
             common.AddFunction(ContentKeyFunctions.ToggleFullscreen);
             common.AddFunction(ContentKeyFunctions.MoveStoredItem);
             common.AddFunction(ContentKeyFunctions.RotateStoredItem);
+            common.AddFunction(ContentKeyFunctions.SaveItemLocation);
             common.AddFunction(ContentKeyFunctions.Point);
             common.AddFunction(ContentKeyFunctions.ZoomOut);
             common.AddFunction(ContentKeyFunctions.ZoomIn);
index f0537079b970381d79989f57157fba8e50e416cc..aca9efcfe26acd48381d6b60b701f63cda298ff5 100644 (file)
@@ -183,6 +183,7 @@ namespace Content.Client.Options.UI.Tabs
             AddButton(ContentKeyFunctions.SwapHands);
             AddButton(ContentKeyFunctions.MoveStoredItem);
             AddButton(ContentKeyFunctions.RotateStoredItem);
+            AddButton(ContentKeyFunctions.SaveItemLocation);
 
             AddHeader("ui-options-header-interaction-adv");
             AddButton(ContentKeyFunctions.SmartEquipBackpack);
index bd952fe9577744bb6e51746e8cbb040605441349..e39ac5d322d39e637939f8c6c155e7ea17562c39 100644 (file)
@@ -12,6 +12,7 @@ using Robust.Client.UserInterface;
 using Robust.Client.UserInterface.Controls;
 using Robust.Client.UserInterface.CustomControls;
 using Robust.Shared.Timing;
+using Robust.Shared.Utility;
 
 namespace Content.Client.UserInterface.Systems.Storage.Controls;
 
@@ -355,6 +356,40 @@ public sealed class StorageContainer : BaseWindow
             origin,
             currentLocation.Rotation);
 
+        foreach (var locations in storageComponent.SavedLocations)
+        {
+            if (!_entity.TryGetComponent<MetaDataComponent>(currentEnt, out var meta) || meta.EntityName != locations.Key)
+                continue;
+
+            float spot = 0;
+            var marked = new List<Control>();
+
+            foreach (var location in locations.Value)
+            {
+                var shape = itemSystem.GetAdjustedItemShape(currentEnt, location);
+                var bound = shape.GetBoundingBox();
+
+                var spotFree = storageSystem.ItemFitsInGridLocation(currentEnt, StorageEntity.Value, location);
+
+                if (spotFree)
+                    spot++;
+
+                for (var y = bound.Bottom; y <= bound.Top; y++)
+                {
+                    for (var x = bound.Left; x <= bound.Right; x++)
+                    {
+                        if (TryGetBackgroundCell(x, y, out var cell) && shape.Contains(x, y) && !marked.Contains(cell))
+                        {
+                            marked.Add(cell);
+                            cell.ModulateSelfOverride = spotFree
+                                ? Color.FromHsv((0.18f, 1 / spot, 0.5f / spot + 0.5f, 1f))
+                                : Color.FromHex("#2222CC");
+                        }
+                    }
+                }
+            }
+        }
+
         var validColor = usingInHand ? Color.Goldenrod : Color.FromHex("#1E8000");
 
         for (var y = itemBounding.Bottom; y <= itemBounding.Top; y++)
index 5f3ae3a2daca39ef0139308d606ceedf8062defb..b865b54dd0eaf68c7c2be63d60dea05a6b6f57b9 100644 (file)
@@ -240,6 +240,16 @@ public sealed class StorageUIController : UIController, IOnSystemChanged<Storage
 
             args.Handle();
         }
+        else if (args.Function == ContentKeyFunctions.SaveItemLocation)
+        {
+            if (_container?.StorageEntity is not {} storage)
+                return;
+
+            _entity.RaisePredictiveEvent(new StorageSaveItemLocationEvent(
+                _entity.GetNetEntity(control.Entity),
+                _entity.GetNetEntity(storage)));
+            args.Handle();
+        }
         else if (args.Function == ContentKeyFunctions.ExamineEntity)
         {
             _entity.System<ExamineSystem>().DoExamine(control.Entity);
index ee4a4e9023bc7c346ef3c8b357bdb7a027de1a8f..cf874434ec93eabe98565f2e4bd65b1631b3df88 100644 (file)
@@ -36,6 +36,7 @@ namespace Content.Shared.Input
         public static readonly BoundKeyFunction SwapHands = "SwapHands";
         public static readonly BoundKeyFunction MoveStoredItem = "MoveStoredItem";
         public static readonly BoundKeyFunction RotateStoredItem = "RotateStoredItem";
+        public static readonly BoundKeyFunction SaveItemLocation = "SaveItemLocation";
         public static readonly BoundKeyFunction ThrowItemInHand = "ThrowItemInHand";
         public static readonly BoundKeyFunction TryPullObject = "TryPullObject";
         public static readonly BoundKeyFunction MovePulledObject = "MovePulledObject";
index ef1b2b7c4417ec6d1349e63f802fa83675c692ce..9d364dded01ebd0c13d5533bffab807bdb5c6373 100644 (file)
@@ -97,6 +97,7 @@ public abstract class SharedStorageSystem : EntitySystem
         SubscribeAllEvent<StorageSetItemLocationEvent>(OnSetItemLocation);
         SubscribeAllEvent<StorageInsertItemIntoLocationEvent>(OnInsertItemIntoLocation);
         SubscribeAllEvent<StorageRemoveItemEvent>(OnRemoveItem);
+        SubscribeAllEvent<StorageSaveItemLocationEvent>(OnSaveItemLocation);
 
         SubscribeLocalEvent<StorageComponent, GotReclaimedEvent>(OnReclaimed);
 
@@ -117,7 +118,8 @@ public abstract class SharedStorageSystem : EntitySystem
             Grid = new List<Box2i>(component.Grid),
             IsUiOpen = component.IsUiOpen,
             MaxItemSize = component.MaxItemSize,
-            StoredItems = storedItems
+            StoredItems = storedItems,
+            SavedLocations = component.SavedLocations
         };
     }
 
@@ -138,6 +140,8 @@ public abstract class SharedStorageSystem : EntitySystem
             var ent = EnsureEntity<StorageComponent>(nent, uid);
             component.StoredItems[ent] = location;
         }
+
+        component.SavedLocations = state.SavedLocations;
     }
 
     public override void Shutdown()
@@ -536,6 +540,35 @@ public abstract class SharedStorageSystem : EntitySystem
         InsertAt((storageEnt, storageComp), (itemEnt, null), msg.Location, out _, player, stackAutomatically: false);
     }
 
+    // TODO: if/when someone cleans up this shitcode please make all these
+    // handlers use a shared helper for checking that the ui is open etc, thanks
+    private void OnSaveItemLocation(StorageSaveItemLocationEvent msg, EntitySessionEventArgs args)
+    {
+        if (args.SenderSession.AttachedEntity is not {} player)
+            return;
+
+        var storage = GetEntity(msg.Storage);
+        var item = GetEntity(msg.Item);
+
+        if (!TryComp<StorageComponent>(storage, out var storageComp))
+            return;
+
+        if (!_ui.TryGetUi(storage, StorageComponent.StorageUiKey.Key, out var bui) ||
+            !bui.SubscribedSessions.Contains(args.SenderSession))
+            return;
+
+        if (!Exists(item))
+        {
+            Log.Error($"Player {args.SenderSession} saved location of non-existent item {msg.Item} stored in {ToPrettyString(storage)}");
+            return;
+        }
+
+        if (!ActionBlocker.CanInteract(player, item))
+            return;
+
+        SaveItemLocation(storage, item);
+    }
+
     private void OnBoundUIOpen(EntityUid uid, StorageComponent storageComp, BoundUIOpenedEvent args)
     {
         if (!storageComp.IsUiOpen)
@@ -945,6 +978,10 @@ public abstract class SharedStorageSystem : EntitySystem
         if (!Resolve(storageEnt, ref storageEnt.Comp) || !Resolve(itemEnt, ref itemEnt.Comp))
             return false;
 
+        // if the item has an available saved location, use that
+        if (FindSavedLocation(storageEnt, itemEnt, out storageLocation))
+            return true;
+
         var storageBounding = storageEnt.Comp.Grid.GetBoundingBox();
 
         Angle startAngle;
@@ -987,6 +1024,76 @@ public abstract class SharedStorageSystem : EntitySystem
         return false;
     }
 
+    /// <summary>
+    /// Tries to find a saved location for an item from its name.
+    /// If none are saved or they are all blocked nothing is returned.
+    /// </summary>
+    public bool FindSavedLocation(
+        Entity<StorageComponent?> ent,
+        Entity<ItemComponent?> item,
+        [NotNullWhen(true)] out ItemStorageLocation? storageLocation)
+    {
+        storageLocation = null;
+        if (!Resolve(ent, ref ent.Comp))
+            return false;
+
+        var name = Name(item);
+        if (!ent.Comp.SavedLocations.TryGetValue(name, out var list))
+            return false;
+
+        foreach (var location in list)
+        {
+            if (ItemFitsInGridLocation(item, ent, location))
+            {
+                storageLocation = location;
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    /// <summary>
+    /// Saves an item's location in the grid for later insertion to use.
+    /// </summary>
+    public void SaveItemLocation(Entity<StorageComponent?> ent, Entity<MetaDataComponent?> item)
+    {
+        if (!Resolve(ent, ref ent.Comp))
+            return;
+
+        // needs to actually be stored in it somewhere to save it
+        if (!ent.Comp.StoredItems.TryGetValue(item, out var location))
+            return;
+
+        var name = Name(item, item.Comp);
+        if (ent.Comp.SavedLocations.TryGetValue(name, out var list))
+        {
+            // iterate to make sure its not already been saved
+            for (int i = 0; i < list.Count; i++)
+            {
+                var saved = list[i];
+                
+                if (saved == location)
+                {
+                    list.Remove(location);
+                    return;
+                }
+            }
+
+            list.Add(location);
+        }
+        else
+        {
+            list = new List<ItemStorageLocation>()
+            {
+                location
+            };
+            ent.Comp.SavedLocations[name] = list;
+        }
+
+        Dirty(ent, ent.Comp);
+    }
+
     /// <summary>
     /// Checks if an item fits into a specific spot on a storage grid.
     /// </summary>
@@ -1165,6 +1272,8 @@ public abstract class SharedStorageSystem : EntitySystem
 
         public Dictionary<NetEntity, ItemStorageLocation> StoredItems = new();
 
+        public Dictionary<string, List<ItemStorageLocation>> SavedLocations = new();
+
         public List<Box2i> Grid = new();
 
         public ProtoId<ItemSizePrototype>? MaxItemSize;
index a43be5a44faff74a9bbafe8f5dd6b156c377b061..25d55e1e1aeab76c4f04ae4a647a624332876273 100644 (file)
@@ -8,16 +8,16 @@ public partial record struct ItemStorageLocation
     /// <summary>
     /// The rotation, stored a cardinal direction in order to reduce rounding errors.
     /// </summary>
-    [DataField]
-    private Direction _rotation;
+    [DataField("_rotation")]
+    public Direction Direction;
 
     /// <summary>
     /// The rotation of the piece in storage.
     /// </summary>
     public Angle Rotation
     {
-        get => _rotation.ToAngle();
-        set => _rotation = value.GetCardinalDir();
+        get => Direction.ToAngle();
+        set => Direction = value.GetCardinalDir();
     }
 
     /// <summary>
index 98e80de0253ddd83c4076963598f35632fc1a7f0..2cae12f07a84d135ca0dafeb98168807fe58163b 100644 (file)
@@ -32,6 +32,14 @@ namespace Content.Shared.Storage
         [DataField, ViewVariables(VVAccess.ReadWrite)]
         public Dictionary<EntityUid, ItemStorageLocation> StoredItems = new();
 
+        /// <summary>
+        /// A dictionary storing each saved item to its location in the grid.
+        /// When trying to quick insert an item, if there is an empty location with the same name it will be placed there.
+        /// Multiple items with the same name can be saved, they will be checked individually.
+        /// </summary>
+        [DataField]
+        public Dictionary<string, List<ItemStorageLocation>> SavedLocations = new();
+
         /// <summary>
         /// A list of boxes that comprise a combined grid that determines the location that items can be stored.
         /// </summary>
@@ -171,6 +179,20 @@ namespace Content.Shared.Storage
         }
     }
 
+    [Serializable, NetSerializable]
+    public sealed class StorageSaveItemLocationEvent : EntityEventArgs
+    {
+        public readonly NetEntity Item;
+
+        public readonly NetEntity Storage;
+
+        public StorageSaveItemLocationEvent(NetEntity item, NetEntity storage)
+        {
+            Item = item;
+            Storage = storage;
+        }
+    }
+
 
     /// <summary>
     /// Network event for displaying an animation of entities flying into a storage entity
index 46f5df48ad668c9796baf3aca264dccd40dd2df6..ff56d542744b9114e915153d0c6583254b9c838b 100644 (file)
@@ -135,6 +135,7 @@ ui-options-function-examine-entity = Examine
 ui-options-function-swap-hands = Swap hands
 ui-options-function-move-stored-item = Move stored item
 ui-options-function-rotate-stored-item = Rotate stored item
+ui-options-function-save-item-location = Save item location
 ui-options-static-storage-ui = Lock storage window to hotbar
 
 ui-options-function-smart-equip-backpack = Smart-equip to backpack
index 346156159a7e0c77b2d54a1bfbb94db98a8d32f9..886fec35de86eca06b6988d28995da6ca9cc3a75 100644 (file)
@@ -171,6 +171,9 @@ binds:
 - function: RotateStoredItem
   type: State
   key: MouseRight
+- function: SaveItemLocation
+  type: State
+  key: MouseMiddle
 - function: Drop
   type: State
   key: Q