biggest gridinv update OF ALL TIME (#25834)

* 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>
This commit is contained in:
deltanedas
2024-03-28 06:31:47 +00:00
committed by GitHub
parent 65fa3ae211
commit 6863a7cc26
10 changed files with 188 additions and 5 deletions

View 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;