Cargo Pallet Sale Console (#14422)
This commit is contained in:
@@ -7,6 +7,8 @@ using Content.Server.UserInterface;
|
||||
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;
|
||||
@@ -27,6 +29,8 @@ using Robust.Shared.Player;
|
||||
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;
|
||||
|
||||
@@ -40,13 +44,14 @@ public sealed partial class CargoSystem
|
||||
[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;
|
||||
@@ -69,6 +74,10 @@ public sealed partial class CargoSystem
|
||||
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);
|
||||
@@ -144,6 +153,48 @@ public sealed partial class CargoSystem
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -209,7 +260,7 @@ public sealed partial class CargoSystem
|
||||
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];
|
||||
@@ -238,19 +289,19 @@ public sealed partial class CargoSystem
|
||||
/// <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);
|
||||
@@ -306,14 +357,25 @@ public sealed partial class CargoSystem
|
||||
_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))
|
||||
@@ -336,14 +398,6 @@ public sealed partial class CargoSystem
|
||||
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)
|
||||
@@ -424,7 +478,7 @@ public sealed partial class CargoSystem
|
||||
{
|
||||
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);
|
||||
@@ -435,13 +489,34 @@ public sealed partial class CargoSystem
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -465,7 +540,8 @@ public sealed partial class CargoSystem
|
||||
return;
|
||||
}
|
||||
|
||||
SellPallets(shuttle, bank);
|
||||
SellPallets((EntityUid) orderDatabase.Shuttle, out double price);
|
||||
bank.Balance += (int) price;
|
||||
_console.RefreshShuttleConsoles();
|
||||
SendToCargoMap(orderDatabase.Shuttle.Value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user