using Content.Server.Paper;
using Content.Server.Shuttles.Systems;
using Content.Server.Station.Components;
+using Content.Server.Stack;
+using Content.Shared.Stacks;
using Content.Shared.Cargo;
using Content.Shared.Cargo.BUI;
using Content.Shared.Cargo.Components;
using Robust.Shared.Random;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
+using Robust.Shared.Prototypes;
+using Content.Shared.Coordinates;
namespace Content.Server.Cargo.Systems;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
+ [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly MapLoaderSystem _map = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;
[Dependency] private readonly PricingSystem _pricing = default!;
[Dependency] private readonly ShuttleConsoleSystem _console = default!;
[Dependency] private readonly ShuttleSystem _shuttle = default!;
-
+ [Dependency] private readonly StackSystem _stack = default!;
public MapId? CargoMap { get; private set; }
private const float CallOffset = 50f;
SubscribeLocalEvent<CargoShuttleConsoleComponent, CargoCallShuttleMessage>(OnCargoShuttleCall);
SubscribeLocalEvent<CargoShuttleConsoleComponent, CargoRecallShuttleMessage>(RecallCargoShuttle);
+ SubscribeLocalEvent<CargoPalletConsoleComponent, CargoPalletSellMessage>(OnPalletSale);
+ SubscribeLocalEvent<CargoPalletConsoleComponent, CargoPalletAppraiseMessage>(OnPalletAppraise);
+ SubscribeLocalEvent<CargoPalletConsoleComponent, BoundUIOpenedEvent>(OnPalletUIOpen);
+
SubscribeLocalEvent<CargoPilotConsoleComponent, ConsoleShuttleEvent>(OnCargoGetConsole);
SubscribeLocalEvent<CargoPilotConsoleComponent, AfterActivatableUIOpenEvent>(OnCargoPilotConsoleOpen);
SubscribeLocalEvent<CargoPilotConsoleComponent, BoundUIClosedEvent>(OnCargoPilotConsoleClose);
}
}
+ private void UpdatePalletConsoleInterface(EntityUid uid, CargoPalletConsoleComponent component)
+ {
+ var bui = _uiSystem.GetUi(component.Owner, CargoPalletConsoleUiKey.Sale);
+ if (Transform(uid).GridUid is not EntityUid gridUid)
+ {
+ _uiSystem.SetUiState(bui,
+ new CargoPalletConsoleInterfaceState(0, 0, false));
+ return;
+ }
+ GetPalletGoods(gridUid, out var toSell, out var amount);
+ _uiSystem.SetUiState(bui,
+ new CargoPalletConsoleInterfaceState((int) amount, toSell.Count, true));
+ }
+
+ private void OnPalletUIOpen(EntityUid uid, CargoPalletConsoleComponent component, BoundUIOpenedEvent args)
+ {
+ var player = args.Session.AttachedEntity;
+
+ if (player == null)
+ return;
+
+ UpdatePalletConsoleInterface(uid, component);
+ }
+
+ /// <summary>
+ /// Ok so this is just the same thing as opening the UI, its a refresh button.
+ /// I know this would probably feel better if it were like predicted and dynamic as pallet contents change
+ /// However.
+ /// I dont want it to explode if cargo uses a conveyor to move 8000 pineapple slices or whatever, they are
+ /// known for their entity spam i wouldnt put it past them
+ /// </summary>
+
+ private void OnPalletAppraise(EntityUid uid, CargoPalletConsoleComponent component, CargoPalletAppraiseMessage args)
+ {
+ var player = args.Session.AttachedEntity;
+
+ if (player == null)
+ return;
+
+ UpdatePalletConsoleInterface(uid, component);
+ }
+
private void OnCargoShuttleConsoleStartup(EntityUid uid, CargoShuttleConsoleComponent component, ComponentStartup args)
{
var station = _station.GetOwningStation(uid);
if (component == null || shuttle == null || component.Orders.Count == 0)
return orders;
- var spaceRemaining = GetCargoSpace(shuttle);
+ var spaceRemaining = GetCargoSpace(shuttle.Owner);
for( var i = 0; i < component.Orders.Count && spaceRemaining > 0; i++)
{
var order = component.Orders[i];
/// <summary>
/// Get the amount of space the cargo shuttle can fit for orders.
/// </summary>
- private int GetCargoSpace(CargoShuttleComponent component)
+ private int GetCargoSpace(EntityUid gridUid)
{
- var space = GetCargoPallets(component).Count;
+ var space = GetCargoPallets(gridUid).Count;
return space;
}
- private List<CargoPalletComponent> GetCargoPallets(CargoShuttleComponent component)
+ private List<CargoPalletComponent> GetCargoPallets(EntityUid gridUid)
{
var pads = new List<CargoPalletComponent>();
foreach (var (comp, compXform) in EntityQuery<CargoPalletComponent, TransformComponent>(true))
{
- if (compXform.ParentUid != component.Owner ||
+ if (compXform.ParentUid != gridUid ||
!compXform.Anchored) continue;
pads.Add(comp);
_sawmill.Info($"Added cargo shuttle to {ToPrettyString(shuttleUid)}");
}
- private void SellPallets(CargoShuttleComponent component, StationBankAccountComponent bank)
+ private void SellPallets(EntityUid gridUid, out double amount)
{
- double amount = 0;
- var toSell = new HashSet<EntityUid>();
+ GetPalletGoods(gridUid, out var toSell, out amount);
+
+ _sawmill.Debug($"Cargo sold {toSell.Count} entities for {amount}");
+
+ foreach (var ent in toSell)
+ {
+ Del(ent);
+ }
+ }
+
+ private void GetPalletGoods(EntityUid gridUid, out HashSet<EntityUid> toSell, out double amount)
+ {
+ amount = 0;
var xformQuery = GetEntityQuery<TransformComponent>();
var blacklistQuery = GetEntityQuery<CargoSellBlacklistComponent>();
-
- foreach (var pallet in GetCargoPallets(component))
+ toSell = new HashSet<EntityUid>();
+ foreach (var pallet in GetCargoPallets(gridUid))
{
// Containers should already get the sell price of their children so can skip those.
foreach (var ent in _lookup.GetEntitiesIntersecting(pallet.Owner, LookupFlags.Dynamic | LookupFlags.Sundries | LookupFlags.Approximate))
amount += price;
}
}
-
- bank.Balance += (int) amount;
- _sawmill.Debug($"Cargo sold {toSell.Count} entities for {amount}");
-
- foreach (var ent in toSell)
- {
- Del(ent);
- }
}
private void SendToCargoMap(EntityUid uid, CargoShuttleComponent? component = null)
{
var xformQuery = GetEntityQuery<TransformComponent>();
- var pads = GetCargoPallets(shuttle);
+ var pads = GetCargoPallets(shuttle.Owner);
while (pads.Count > 0)
{
var coordinates = new EntityCoordinates(shuttle.Owner, xformQuery.GetComponent(_random.PickAndTake(pads).Owner).LocalPosition);
}
}
+ private void OnPalletSale(EntityUid uid, CargoPalletConsoleComponent component, CargoPalletSellMessage args)
+ {
+ var player = args.Session.AttachedEntity;
+
+ if (player == null)
+ return;
+
+ var bui = _uiSystem.GetUi(component.Owner, CargoPalletConsoleUiKey.Sale);
+ if (Transform(uid).GridUid is not EntityUid gridUid)
+ {
+ _uiSystem.SetUiState(bui,
+ new CargoPalletConsoleInterfaceState(0, 0, false));
+ return;
+ }
+
+ SellPallets(gridUid, out var price);
+ var stackPrototype = _prototypeManager.Index<StackPrototype>(component.CashType);
+ _stack.Spawn((int)price, stackPrototype, uid.ToCoordinates());
+ UpdatePalletConsoleInterface(uid, component);
+ }
+
private void RecallCargoShuttle(EntityUid uid, CargoShuttleConsoleComponent component, CargoRecallShuttleMessage args)
{
var player = args.Session.AttachedEntity;
if (player == null) return;
- var stationUid = _station.GetOwningStation(component.Owner);
+ var stationUid = _station.GetOwningStation(uid);
if (!TryComp<StationCargoOrderDatabaseComponent>(stationUid, out var orderDatabase) ||
!TryComp<StationBankAccountComponent>(stationUid, out var bank)) return;
return;
}
- SellPallets(shuttle, bank);
+ SellPallets((EntityUid) orderDatabase.Shuttle, out double price);
+ bank.Balance += (int) price;
_console.RefreshShuttleConsoles();
SendToCargoMap(orderDatabase.Shuttle.Value);
}